blob: 9bdeb52d9e721534725b2003c2de67a9f8a56d1d [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},
782 {"pathshorten", 1, 1, 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;
2439
2440 rettv->v_type = VAR_STRING;
2441 if (argvars[1].v_type != VAR_UNKNOWN
2442 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002443 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002444 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002445 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002447 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (*s == '%' || *s == '#' || *s == '<')
2449 {
2450 ++emsg_off;
2451 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2452 --emsg_off;
2453 if (rettv->v_type == VAR_LIST)
2454 {
2455 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2456 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002457 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 }
2459 else
2460 rettv->vval.v_string = result;
2461 }
2462 else
2463 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002464 // When the optional second argument is non-zero, don't remove matches
2465 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002467 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468 options |= WILD_KEEP_ALL;
2469 if (!error)
2470 {
2471 ExpandInit(&xpc);
2472 xpc.xp_context = EXPAND_FILES;
2473 if (p_wic)
2474 options += WILD_ICASE;
2475 if (rettv->v_type == VAR_STRING)
2476 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2477 options, WILD_ALL);
2478 else if (rettv_list_alloc(rettv) != FAIL)
2479 {
2480 int i;
2481
2482 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2483 for (i = 0; i < xpc.xp_numfiles; i++)
2484 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2485 ExpandCleanup(&xpc);
2486 }
2487 }
2488 else
2489 rettv->vval.v_string = NULL;
2490 }
2491}
2492
2493/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002494 * "expandcmd()" function
2495 * Expand all the special characters in a command string.
2496 */
2497 static void
2498f_expandcmd(typval_T *argvars, typval_T *rettv)
2499{
2500 exarg_T eap;
2501 char_u *cmdstr;
2502 char *errormsg = NULL;
2503
2504 rettv->v_type = VAR_STRING;
2505 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2506
2507 memset(&eap, 0, sizeof(eap));
2508 eap.cmd = cmdstr;
2509 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002510 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002511 eap.usefilter = FALSE;
2512 eap.nextcmd = NULL;
2513 eap.cmdidx = CMD_USER;
2514
2515 expand_filename(&eap, &cmdstr, &errormsg);
2516 if (errormsg != NULL && *errormsg != NUL)
2517 emsg(errormsg);
2518
2519 rettv->vval.v_string = cmdstr;
2520}
2521
2522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 * "feedkeys()" function
2524 */
2525 static void
2526f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2527{
2528 int remap = TRUE;
2529 int insert = FALSE;
2530 char_u *keys, *flags;
2531 char_u nbuf[NUMBUFLEN];
2532 int typed = FALSE;
2533 int execute = FALSE;
2534 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002535 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 char_u *keys_esc;
2537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002538 // This is not allowed in the sandbox. If the commands would still be
2539 // executed in the sandbox it would be OK, but it probably happens later,
2540 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (check_secure())
2542 return;
2543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545
2546 if (argvars[1].v_type != VAR_UNKNOWN)
2547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002548 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 for ( ; *flags != NUL; ++flags)
2550 {
2551 switch (*flags)
2552 {
2553 case 'n': remap = FALSE; break;
2554 case 'm': remap = TRUE; break;
2555 case 't': typed = TRUE; break;
2556 case 'i': insert = TRUE; break;
2557 case 'x': execute = TRUE; break;
2558 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002559 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002560 }
2561 }
2562 }
2563
2564 if (*keys != NUL || execute)
2565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // Need to escape K_SPECIAL and CSI before putting the string in the
2567 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002568 keys_esc = vim_strsave_escape_csi(keys);
2569 if (keys_esc != NULL)
2570 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002571 if (lowlevel)
2572 {
2573#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002574 int idx;
2575 int len = (int)STRLEN(keys);
2576
2577 for (idx = 0; idx < len; ++idx)
2578 {
2579 // if a CTRL-C was typed, set got_int, similar to what
2580 // happens in fill_input_buf()
2581 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2582 got_int = TRUE;
2583 add_to_input_buf(keys + idx, 1);
2584 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002585#else
2586 emsg(_("E980: lowlevel input not supported"));
2587#endif
2588 }
2589 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002590 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002591 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002593 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002594#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002595 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002596#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002597 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002598 typebuf_was_filled = TRUE;
2599 }
2600 vim_free(keys_esc);
2601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 if (execute)
2603 {
2604 int save_msg_scroll = msg_scroll;
2605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002606 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 msg_scroll = FALSE;
2608
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002609 if (!dangerous)
2610 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002611 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002612 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02002613 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002614 --ex_normal_busy;
Bram Moolenaar189832b2020-09-23 12:29:11 +02002615#ifdef FEAT_PROP_POPUP
2616 if (ex_normal_busy == 0)
2617 ex_normal_busy_done = FALSE;
2618#endif
2619 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 msg_scroll |= save_msg_scroll;
2622 }
2623 }
2624 }
2625}
2626
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627#ifdef FEAT_FLOAT
2628/*
2629 * "float2nr({float})" function
2630 */
2631 static void
2632f_float2nr(typval_T *argvars, typval_T *rettv)
2633{
2634 float_T f = 0.0;
2635
2636 if (get_float_arg(argvars, &f) == OK)
2637 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002638 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002639 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002640 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002641 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 else
2643 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 }
2645}
2646
2647/*
2648 * "floor({float})" function
2649 */
2650 static void
2651f_floor(typval_T *argvars, typval_T *rettv)
2652{
2653 float_T f = 0.0;
2654
2655 rettv->v_type = VAR_FLOAT;
2656 if (get_float_arg(argvars, &f) == OK)
2657 rettv->vval.v_float = floor(f);
2658 else
2659 rettv->vval.v_float = 0.0;
2660}
2661
2662/*
2663 * "fmod()" function
2664 */
2665 static void
2666f_fmod(typval_T *argvars, typval_T *rettv)
2667{
2668 float_T fx = 0.0, fy = 0.0;
2669
2670 rettv->v_type = VAR_FLOAT;
2671 if (get_float_arg(argvars, &fx) == OK
2672 && get_float_arg(&argvars[1], &fy) == OK)
2673 rettv->vval.v_float = fmod(fx, fy);
2674 else
2675 rettv->vval.v_float = 0.0;
2676}
2677#endif
2678
2679/*
2680 * "fnameescape({string})" function
2681 */
2682 static void
2683f_fnameescape(typval_T *argvars, typval_T *rettv)
2684{
2685 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002686 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 rettv->v_type = VAR_STRING;
2688}
2689
2690/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 * "foreground()" function
2692 */
2693 static void
2694f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2695{
2696#ifdef FEAT_GUI
2697 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002698 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002700 return;
2701 }
2702#endif
2703#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705#endif
2706}
2707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002709common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710{
2711 char_u *s;
2712 char_u *name;
2713 int use_string = FALSE;
2714 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002715 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002716 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717
2718 if (argvars[0].v_type == VAR_FUNC)
2719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002720 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 s = argvars[0].vval.v_string;
2722 }
2723 else if (argvars[0].v_type == VAR_PARTIAL
2724 && argvars[0].vval.v_partial != NULL)
2725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002726 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002728 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729 }
2730 else
2731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002732 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002733 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002734 use_string = TRUE;
2735 }
2736
Bram Moolenaar843b8842016-08-21 14:36:15 +02002737 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002738 {
2739 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002740 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002741 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2742 if (*name != NUL)
2743 s = NULL;
2744 }
2745
Bram Moolenaar843b8842016-08-21 14:36:15 +02002746 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2747 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002748 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002750 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002751 ? find_func(trans_name, is_global, NULL) == NULL
2752 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002753 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002754 else
2755 {
2756 int dict_idx = 0;
2757 int arg_idx = 0;
2758 list_T *list = NULL;
2759
2760 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2761 {
2762 char sid_buf[25];
2763 int off = *s == 's' ? 2 : 5;
2764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002765 // Expand s: and <SID> into <SNR>nr_, so that the function can
2766 // also be called from another script. Using trans_function_name()
2767 // would also work, but some plugins depend on the name being
2768 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002769 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002770 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 if (name != NULL)
2772 {
2773 STRCPY(name, sid_buf);
2774 STRCAT(name, s + off);
2775 }
2776 }
2777 else
2778 name = vim_strsave(s);
2779
2780 if (argvars[1].v_type != VAR_UNKNOWN)
2781 {
2782 if (argvars[2].v_type != VAR_UNKNOWN)
2783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002784 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 arg_idx = 1;
2786 dict_idx = 2;
2787 }
2788 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002789 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 dict_idx = 1;
2791 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002792 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 arg_idx = 1;
2794 if (dict_idx > 0)
2795 {
2796 if (argvars[dict_idx].v_type != VAR_DICT)
2797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002798 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002800 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002801 }
2802 if (argvars[dict_idx].vval.v_dict == NULL)
2803 dict_idx = 0;
2804 }
2805 if (arg_idx > 0)
2806 {
2807 if (argvars[arg_idx].v_type != VAR_LIST)
2808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002809 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002811 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 }
2813 list = argvars[arg_idx].vval.v_list;
2814 if (list == NULL || list->lv_len == 0)
2815 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002816 else if (list->lv_len > MAX_FUNC_ARGS)
2817 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002818 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002819 vim_free(name);
2820 goto theend;
2821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 }
2823 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002824 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002825 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002826 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002828 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002829 if (pt == NULL)
2830 vim_free(name);
2831 else
2832 {
2833 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2834 {
2835 listitem_T *li;
2836 int i = 0;
2837 int arg_len = 0;
2838 int lv_len = 0;
2839
2840 if (arg_pt != NULL)
2841 arg_len = arg_pt->pt_argc;
2842 if (list != NULL)
2843 lv_len = list->lv_len;
2844 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002845 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 if (pt->pt_argv == NULL)
2847 {
2848 vim_free(pt);
2849 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002850 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002852 for (i = 0; i < arg_len; i++)
2853 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2854 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002855 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002856 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002857 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002858 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 }
2861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 // For "function(dict.func, [], dict)" and "func" is a partial
2863 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864 if (dict_idx > 0)
2865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002867 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2868 ++pt->pt_dict->dv_refcount;
2869 }
2870 else if (arg_pt != NULL)
2871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 // If the dict was bound automatically the result is also
2873 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874 pt->pt_dict = arg_pt->pt_dict;
2875 pt->pt_auto = arg_pt->pt_auto;
2876 if (pt->pt_dict != NULL)
2877 ++pt->pt_dict->dv_refcount;
2878 }
2879
2880 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002881 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2882 {
2883 pt->pt_func = arg_pt->pt_func;
2884 func_ptr_ref(pt->pt_func);
2885 vim_free(name);
2886 }
2887 else if (is_funcref)
2888 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002889 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002890 func_ptr_ref(pt->pt_func);
2891 vim_free(name);
2892 }
2893 else
2894 {
2895 pt->pt_name = name;
2896 func_ref(name);
2897 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898 }
2899 rettv->v_type = VAR_PARTIAL;
2900 rettv->vval.v_partial = pt;
2901 }
2902 else
2903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002904 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 rettv->v_type = VAR_FUNC;
2906 rettv->vval.v_string = name;
2907 func_ref(name);
2908 }
2909 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002910theend:
2911 vim_free(trans_name);
2912}
2913
2914/*
2915 * "funcref()" function
2916 */
2917 static void
2918f_funcref(typval_T *argvars, typval_T *rettv)
2919{
2920 common_function(argvars, rettv, TRUE);
2921}
2922
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002923 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002924ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002925{
2926 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2927 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02002928 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002929}
2930
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002931/*
2932 * "function()" function
2933 */
2934 static void
2935f_function(typval_T *argvars, typval_T *rettv)
2936{
2937 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938}
2939
2940/*
2941 * "garbagecollect()" function
2942 */
2943 static void
2944f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2945{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002946 // This is postponed until we are back at the toplevel, because we may be
2947 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948 want_garbage_collect = TRUE;
2949
Bram Moolenaar2df47312020-09-05 17:30:44 +02002950 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 garbage_collect_at_exit = TRUE;
2952}
2953
2954/*
2955 * "get()" function
2956 */
2957 static void
2958f_get(typval_T *argvars, typval_T *rettv)
2959{
2960 listitem_T *li;
2961 list_T *l;
2962 dictitem_T *di;
2963 dict_T *d;
2964 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002965 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002967 if (argvars[0].v_type == VAR_BLOB)
2968 {
2969 int error = FALSE;
2970 int idx = tv_get_number_chk(&argvars[1], &error);
2971
2972 if (!error)
2973 {
2974 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002975 if (idx < 0)
2976 idx = blob_len(argvars[0].vval.v_blob) + idx;
2977 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2978 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002979 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002980 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002981 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002982 tv = rettv;
2983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002984 }
2985 }
2986 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 {
2988 if ((l = argvars[0].vval.v_list) != NULL)
2989 {
2990 int error = FALSE;
2991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 if (!error && li != NULL)
2994 tv = &li->li_tv;
2995 }
2996 }
2997 else if (argvars[0].v_type == VAR_DICT)
2998 {
2999 if ((d = argvars[0].vval.v_dict) != NULL)
3000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003001 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 if (di != NULL)
3003 tv = &di->di_tv;
3004 }
3005 }
3006 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3007 {
3008 partial_T *pt;
3009 partial_T fref_pt;
3010
3011 if (argvars[0].v_type == VAR_PARTIAL)
3012 pt = argvars[0].vval.v_partial;
3013 else
3014 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003015 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 fref_pt.pt_name = argvars[0].vval.v_string;
3017 pt = &fref_pt;
3018 }
3019
3020 if (pt != NULL)
3021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003023 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024
3025 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3026 {
3027 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003028 n = partial_name(pt);
3029 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 rettv->vval.v_string = NULL;
3031 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003032 {
3033 rettv->vval.v_string = vim_strsave(n);
3034 if (rettv->v_type == VAR_FUNC)
3035 func_ref(rettv->vval.v_string);
3036 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 }
3038 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003039 {
3040 what_is_dict = TRUE;
3041 if (pt->pt_dict != NULL)
3042 rettv_dict_set(rettv, pt->pt_dict);
3043 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044 else if (STRCMP(what, "args") == 0)
3045 {
3046 rettv->v_type = VAR_LIST;
3047 if (rettv_list_alloc(rettv) == OK)
3048 {
3049 int i;
3050
3051 for (i = 0; i < pt->pt_argc; ++i)
3052 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3053 }
3054 }
3055 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003056 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003057
3058 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3059 // third argument
3060 if (!what_is_dict)
3061 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 }
3063 }
3064 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003065 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066
3067 if (tv == NULL)
3068 {
3069 if (argvars[2].v_type != VAR_UNKNOWN)
3070 copy_tv(&argvars[2], rettv);
3071 }
3072 else
3073 copy_tv(tv, rettv);
3074}
3075
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003076/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003077 * "getchangelist()" function
3078 */
3079 static void
3080f_getchangelist(typval_T *argvars, typval_T *rettv)
3081{
3082#ifdef FEAT_JUMPLIST
3083 buf_T *buf;
3084 int i;
3085 list_T *l;
3086 dict_T *d;
3087#endif
3088
3089 if (rettv_list_alloc(rettv) != OK)
3090 return;
3091
3092#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003093 if (argvars[0].v_type == VAR_UNKNOWN)
3094 buf = curbuf;
3095 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003096 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003097 if (buf == NULL)
3098 return;
3099
3100 l = list_alloc();
3101 if (l == NULL)
3102 return;
3103
3104 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3105 return;
3106 /*
3107 * The current window change list index tracks only the position in the
3108 * current buffer change list. For other buffers, use the change list
3109 * length as the current index.
3110 */
3111 list_append_number(rettv->vval.v_list,
3112 (varnumber_T)((buf == curwin->w_buffer)
3113 ? curwin->w_changelistidx : buf->b_changelistlen));
3114
3115 for (i = 0; i < buf->b_changelistlen; ++i)
3116 {
3117 if (buf->b_changelist[i].lnum == 0)
3118 continue;
3119 if ((d = dict_alloc()) == NULL)
3120 return;
3121 if (list_append_dict(l, d) == FAIL)
3122 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003123 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3124 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003125 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003126 }
3127#endif
3128}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129
3130/*
3131 * "getcharsearch()" function
3132 */
3133 static void
3134f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3135{
3136 if (rettv_dict_alloc(rettv) != FAIL)
3137 {
3138 dict_T *dict = rettv->vval.v_dict;
3139
Bram Moolenaare0be1672018-07-08 16:50:37 +02003140 dict_add_string(dict, "char", last_csearch());
3141 dict_add_number(dict, "forward", last_csearch_forward());
3142 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 }
3144}
3145
3146/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003147 * "getenv()" function
3148 */
3149 static void
3150f_getenv(typval_T *argvars, typval_T *rettv)
3151{
3152 int mustfree = FALSE;
3153 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3154
3155 if (p == NULL)
3156 {
3157 rettv->v_type = VAR_SPECIAL;
3158 rettv->vval.v_number = VVAL_NULL;
3159 return;
3160 }
3161 if (!mustfree)
3162 p = vim_strsave(p);
3163 rettv->vval.v_string = p;
3164 rettv->v_type = VAR_STRING;
3165}
3166
3167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168 * "getfontname()" function
3169 */
3170 static void
3171f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3172{
3173 rettv->v_type = VAR_STRING;
3174 rettv->vval.v_string = NULL;
3175#ifdef FEAT_GUI
3176 if (gui.in_use)
3177 {
3178 GuiFont font;
3179 char_u *name = NULL;
3180
3181 if (argvars[0].v_type == VAR_UNKNOWN)
3182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // Get the "Normal" font. Either the name saved by
3184 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 font = gui.norm_font;
3186 name = hl_get_font_name();
3187 }
3188 else
3189 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003190 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003191 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003192 return;
3193 font = gui_mch_get_font(name, FALSE);
3194 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003195 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003196 }
3197 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3198 if (argvars[0].v_type != VAR_UNKNOWN)
3199 gui_mch_free_font(font);
3200 }
3201#endif
3202}
3203
3204/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003205 * "getjumplist()" function
3206 */
3207 static void
3208f_getjumplist(typval_T *argvars, typval_T *rettv)
3209{
3210#ifdef FEAT_JUMPLIST
3211 win_T *wp;
3212 int i;
3213 list_T *l;
3214 dict_T *d;
3215#endif
3216
3217 if (rettv_list_alloc(rettv) != OK)
3218 return;
3219
3220#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003221 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003222 if (wp == NULL)
3223 return;
3224
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003225 cleanup_jumplist(wp, TRUE);
3226
Bram Moolenaar4f505882018-02-10 21:06:32 +01003227 l = list_alloc();
3228 if (l == NULL)
3229 return;
3230
3231 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3232 return;
3233 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3234
3235 for (i = 0; i < wp->w_jumplistlen; ++i)
3236 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003237 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3238 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003239 if ((d = dict_alloc()) == NULL)
3240 return;
3241 if (list_append_dict(l, d) == FAIL)
3242 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003243 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3244 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003245 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003246 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003247 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003248 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003249 }
3250#endif
3251}
3252
3253/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254 * "getpid()" function
3255 */
3256 static void
3257f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3258{
3259 rettv->vval.v_number = mch_get_pid();
3260}
3261
3262 static void
3263getpos_both(
3264 typval_T *argvars,
3265 typval_T *rettv,
3266 int getcurpos)
3267{
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003268 pos_T *fp = NULL;
3269 win_T *wp = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 list_T *l;
3271 int fnum = -1;
3272
3273 if (rettv_list_alloc(rettv) == OK)
3274 {
3275 l = rettv->vval.v_list;
3276 if (getcurpos)
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003277 {
3278 if (argvars[0].v_type != VAR_UNKNOWN)
3279 {
3280 wp = find_win_by_nr_or_id(&argvars[0]);
3281 if (wp != NULL)
3282 fp = &wp->w_cursor;
3283 }
3284 else
3285 fp = &curwin->w_cursor;
3286 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 else
3288 fp = var2fpos(&argvars[0], TRUE, &fnum);
3289 if (fnum != -1)
3290 list_append_number(l, (varnumber_T)fnum);
3291 else
3292 list_append_number(l, (varnumber_T)0);
3293 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3294 : (varnumber_T)0);
3295 list_append_number(l, (fp != NULL)
3296 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3297 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003298 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 (varnumber_T)0);
3300 if (getcurpos)
3301 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003302 int save_set_curswant = curwin->w_set_curswant;
3303 colnr_T save_curswant = curwin->w_curswant;
3304 colnr_T save_virtcol = curwin->w_virtcol;
3305
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003306 if (wp == curwin)
3307 update_curswant();
3308 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3309 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003310
3311 // Do not change "curswant", as it is unexpected that a get
3312 // function has a side effect.
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003313 if (wp == curwin && save_set_curswant)
Bram Moolenaar19a66852019-03-07 11:25:32 +01003314 {
3315 curwin->w_set_curswant = save_set_curswant;
3316 curwin->w_curswant = save_curswant;
3317 curwin->w_virtcol = save_virtcol;
3318 curwin->w_valid &= ~VALID_VIRTCOL;
3319 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003320 }
3321 }
3322 else
3323 rettv->vval.v_number = FALSE;
3324}
3325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326/*
3327 * "getcurpos()" function
3328 */
3329 static void
3330f_getcurpos(typval_T *argvars, typval_T *rettv)
3331{
3332 getpos_both(argvars, rettv, TRUE);
3333}
3334
3335/*
3336 * "getpos(string)" function
3337 */
3338 static void
3339f_getpos(typval_T *argvars, typval_T *rettv)
3340{
3341 getpos_both(argvars, rettv, FALSE);
3342}
3343
3344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 * "getreg()" function
3346 */
3347 static void
3348f_getreg(typval_T *argvars, typval_T *rettv)
3349{
3350 char_u *strregname;
3351 int regname;
3352 int arg2 = FALSE;
3353 int return_list = FALSE;
3354 int error = FALSE;
3355
3356 if (argvars[0].v_type != VAR_UNKNOWN)
3357 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 error = strregname == NULL;
3360 if (argvars[1].v_type != VAR_UNKNOWN)
3361 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003362 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003364 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 }
3366 }
3367 else
3368 strregname = get_vim_var_str(VV_REG);
3369
3370 if (error)
3371 return;
3372
3373 regname = (strregname == NULL ? '"' : *strregname);
3374 if (regname == 0)
3375 regname = '"';
3376
3377 if (return_list)
3378 {
3379 rettv->v_type = VAR_LIST;
3380 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3381 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3382 if (rettv->vval.v_list == NULL)
3383 (void)rettv_list_alloc(rettv);
3384 else
3385 ++rettv->vval.v_list->lv_refcount;
3386 }
3387 else
3388 {
3389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = get_reg_contents(regname,
3391 arg2 ? GREG_EXPR_SRC : 0);
3392 }
3393}
3394
3395/*
3396 * "getregtype()" function
3397 */
3398 static void
3399f_getregtype(typval_T *argvars, typval_T *rettv)
3400{
3401 char_u *strregname;
3402 int regname;
3403 char_u buf[NUMBUFLEN + 2];
3404 long reglen = 0;
3405
3406 if (argvars[0].v_type != VAR_UNKNOWN)
3407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003408 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003409 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410 {
3411 rettv->v_type = VAR_STRING;
3412 rettv->vval.v_string = NULL;
3413 return;
3414 }
3415 }
3416 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003417 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 strregname = get_vim_var_str(VV_REG);
3419
3420 regname = (strregname == NULL ? '"' : *strregname);
3421 if (regname == 0)
3422 regname = '"';
3423
3424 buf[0] = NUL;
3425 buf[1] = NUL;
3426 switch (get_reg_type(regname, &reglen))
3427 {
3428 case MLINE: buf[0] = 'V'; break;
3429 case MCHAR: buf[0] = 'v'; break;
3430 case MBLOCK:
3431 buf[0] = Ctrl_V;
3432 sprintf((char *)buf + 1, "%ld", reglen + 1);
3433 break;
3434 }
3435 rettv->v_type = VAR_STRING;
3436 rettv->vval.v_string = vim_strsave(buf);
3437}
3438
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003439/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003440 * "gettagstack()" function
3441 */
3442 static void
3443f_gettagstack(typval_T *argvars, typval_T *rettv)
3444{
3445 win_T *wp = curwin; // default is current window
3446
3447 if (rettv_dict_alloc(rettv) != OK)
3448 return;
3449
3450 if (argvars[0].v_type != VAR_UNKNOWN)
3451 {
3452 wp = find_win_by_nr_or_id(&argvars[0]);
3453 if (wp == NULL)
3454 return;
3455 }
3456
3457 get_tagstack(wp, rettv->vval.v_dict);
3458}
3459
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003460/*
3461 * "gettext()" function
3462 */
3463 static void
3464f_gettext(typval_T *argvars, typval_T *rettv)
3465{
3466 if (argvars[0].v_type != VAR_STRING
3467 || argvars[0].vval.v_string == NULL
3468 || *argvars[0].vval.v_string == NUL)
3469 {
3470 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3471 }
3472 else
3473 {
3474 rettv->v_type = VAR_STRING;
3475 rettv->vval.v_string = vim_strsave(
3476 (char_u *)_(argvars[0].vval.v_string));
3477 }
3478}
3479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481#include "version.h"
3482
3483/*
3484 * "has()" function
3485 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003486 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487f_has(typval_T *argvars, typval_T *rettv)
3488{
3489 int i;
3490 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003491 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003492 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003493 typedef struct {
3494 char *name;
3495 short present;
3496 } has_item_T;
3497 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003499 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003501 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003502#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003503 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003504#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003505 },
3506 {"arp",
3507#if defined(AMIGA) && defined(FEAT_ARP)
3508 1
3509#else
3510 0
3511#endif
3512 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003513 {"haiku",
3514#ifdef __HAIKU__
3515 1
3516#else
3517 0
3518#endif
3519 },
3520 {"bsd",
3521#if defined(BSD) && !defined(MACOS_X)
3522 1
3523#else
3524 0
3525#endif
3526 },
3527 {"hpux",
3528#ifdef hpux
3529 1
3530#else
3531 0
3532#endif
3533 },
3534 {"linux",
3535#ifdef __linux__
3536 1
3537#else
3538 0
3539#endif
3540 },
3541 {"mac", // Mac OS X (and, once, Mac OS Classic)
3542#ifdef MACOS_X
3543 1
3544#else
3545 0
3546#endif
3547 },
3548 {"osx", // Mac OS X
3549#ifdef MACOS_X
3550 1
3551#else
3552 0
3553#endif
3554 },
3555 {"macunix", // Mac OS X, with the darwin feature
3556#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3557 1
3558#else
3559 0
3560#endif
3561 },
3562 {"osxdarwin", // synonym for macunix
3563#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3564 1
3565#else
3566 0
3567#endif
3568 },
3569 {"qnx",
3570#ifdef __QNX__
3571 1
3572#else
3573 0
3574#endif
3575 },
3576 {"sun",
3577#ifdef SUN_SYSTEM
3578 1
3579#else
3580 0
3581#endif
3582 },
3583 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003585 1
3586#else
3587 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003589 },
3590 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003592 1
3593#else
3594 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003596 },
3597 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003598#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 1
3600#else
3601 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003603 },
3604 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003605#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 1
3607#else
3608 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003610 },
3611 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003612#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 1
3614#else
3615 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 },
3618 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003620 1
3621#else
3622 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003624 },
3625 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003627 1
3628#else
3629 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003631 },
3632 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003634 1
3635#else
3636 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003638 },
3639 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003641 1
3642#else
3643 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003645 },
3646 {"autocmd", 1},
3647 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003648#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003649 1
3650#else
3651 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003652#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003653 },
3654 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003655#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003656 1
3657#else
3658 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003659#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003660 },
3661 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003662#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003663 1
3664#else
3665 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003667 },
3668 {"balloon_multiline",
3669#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3670 // MS-Windows requires runtime check, see below
3671 1
3672#else
3673 0
3674#endif
3675 },
3676 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003677#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003678 1
3679#else
3680 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003681#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003682 },
3683 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003685 1
3686#else
3687 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003689 },
3690 {"all_builtin_terms",
3691#if defined(ALL_BUILTIN_TCAPS)
3692 1
3693#else
3694 0
3695#endif
3696 },
3697 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003699 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003701 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003703 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003705 },
3706 {"byte_offset",
3707#ifdef FEAT_BYTEOFF
3708 1
3709#else
3710 0
3711#endif
3712 },
3713 {"channel",
3714#ifdef FEAT_JOB_CHANNEL
3715 1
3716#else
3717 0
3718#endif
3719 },
3720 {"cindent",
3721#ifdef FEAT_CINDENT
3722 1
3723#else
3724 0
3725#endif
3726 },
3727 {"clientserver",
3728#ifdef FEAT_CLIENTSERVER
3729 1
3730#else
3731 0
3732#endif
3733 },
3734 {"clipboard",
3735#ifdef FEAT_CLIPBOARD
3736 1
3737#else
3738 0
3739#endif
3740 },
3741 {"cmdline_compl", 1},
3742 {"cmdline_hist", 1},
3743 {"comments", 1},
3744 {"conceal",
3745#ifdef FEAT_CONCEAL
3746 1
3747#else
3748 0
3749#endif
3750 },
3751 {"cryptv",
3752#ifdef FEAT_CRYPT
3753 1
3754#else
3755 0
3756#endif
3757 },
3758 {"crypt-blowfish",
3759#ifdef FEAT_CRYPT
3760 1
3761#else
3762 0
3763#endif
3764 },
3765 {"crypt-blowfish2",
3766#ifdef FEAT_CRYPT
3767 1
3768#else
3769 0
3770#endif
3771 },
3772 {"cscope",
3773#ifdef FEAT_CSCOPE
3774 1
3775#else
3776 0
3777#endif
3778 },
3779 {"cursorbind", 1},
3780 {"cursorshape",
3781#ifdef CURSOR_SHAPE
3782 1
3783#else
3784 0
3785#endif
3786 },
3787 {"debug",
3788#ifdef DEBUG
3789 1
3790#else
3791 0
3792#endif
3793 },
3794 {"dialog_con",
3795#ifdef FEAT_CON_DIALOG
3796 1
3797#else
3798 0
3799#endif
3800 },
3801 {"dialog_gui",
3802#ifdef FEAT_GUI_DIALOG
3803 1
3804#else
3805 0
3806#endif
3807 },
3808 {"diff",
3809#ifdef FEAT_DIFF
3810 1
3811#else
3812 0
3813#endif
3814 },
3815 {"digraphs",
3816#ifdef FEAT_DIGRAPHS
3817 1
3818#else
3819 0
3820#endif
3821 },
3822 {"directx",
3823#ifdef FEAT_DIRECTX
3824 1
3825#else
3826 0
3827#endif
3828 },
3829 {"dnd",
3830#ifdef FEAT_DND
3831 1
3832#else
3833 0
3834#endif
3835 },
3836 {"emacs_tags",
3837#ifdef FEAT_EMACS_TAGS
3838 1
3839#else
3840 0
3841#endif
3842 },
3843 {"eval", 1}, // always present, of course!
3844 {"ex_extra", 1}, // graduated feature
3845 {"extra_search",
3846#ifdef FEAT_SEARCH_EXTRA
3847 1
3848#else
3849 0
3850#endif
3851 },
3852 {"file_in_path",
3853#ifdef FEAT_SEARCHPATH
3854 1
3855#else
3856 0
3857#endif
3858 },
3859 {"filterpipe",
3860#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3861 1
3862#else
3863 0
3864#endif
3865 },
3866 {"find_in_path",
3867#ifdef FEAT_FIND_ID
3868 1
3869#else
3870 0
3871#endif
3872 },
3873 {"float",
3874#ifdef FEAT_FLOAT
3875 1
3876#else
3877 0
3878#endif
3879 },
3880 {"folding",
3881#ifdef FEAT_FOLDING
3882 1
3883#else
3884 0
3885#endif
3886 },
3887 {"footer",
3888#ifdef FEAT_FOOTER
3889 1
3890#else
3891 0
3892#endif
3893 },
3894 {"fork",
3895#if !defined(USE_SYSTEM) && defined(UNIX)
3896 1
3897#else
3898 0
3899#endif
3900 },
3901 {"gettext",
3902#ifdef FEAT_GETTEXT
3903 1
3904#else
3905 0
3906#endif
3907 },
3908 {"gui",
3909#ifdef FEAT_GUI
3910 1
3911#else
3912 0
3913#endif
3914 },
3915 {"gui_neXtaw",
3916#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3917 1
3918#else
3919 0
3920#endif
3921 },
3922 {"gui_athena",
3923#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3924 1
3925#else
3926 0
3927#endif
3928 },
3929 {"gui_gtk",
3930#ifdef FEAT_GUI_GTK
3931 1
3932#else
3933 0
3934#endif
3935 },
3936 {"gui_gtk2",
3937#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3938 1
3939#else
3940 0
3941#endif
3942 },
3943 {"gui_gtk3",
3944#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3945 1
3946#else
3947 0
3948#endif
3949 },
3950 {"gui_gnome",
3951#ifdef FEAT_GUI_GNOME
3952 1
3953#else
3954 0
3955#endif
3956 },
3957 {"gui_haiku",
3958#ifdef FEAT_GUI_HAIKU
3959 1
3960#else
3961 0
3962#endif
3963 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003964 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003965 {"gui_motif",
3966#ifdef FEAT_GUI_MOTIF
3967 1
3968#else
3969 0
3970#endif
3971 },
3972 {"gui_photon",
3973#ifdef FEAT_GUI_PHOTON
3974 1
3975#else
3976 0
3977#endif
3978 },
3979 {"gui_win32",
3980#ifdef FEAT_GUI_MSWIN
3981 1
3982#else
3983 0
3984#endif
3985 },
3986 {"iconv",
3987#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3988 1
3989#else
3990 0
3991#endif
3992 },
3993 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003994 {"ipv6",
3995#ifdef FEAT_IPV6
3996 1
3997#else
3998 0
3999#endif
4000 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004001 {"job",
4002#ifdef FEAT_JOB_CHANNEL
4003 1
4004#else
4005 0
4006#endif
4007 },
4008 {"jumplist",
4009#ifdef FEAT_JUMPLIST
4010 1
4011#else
4012 0
4013#endif
4014 },
4015 {"keymap",
4016#ifdef FEAT_KEYMAP
4017 1
4018#else
4019 0
4020#endif
4021 },
4022 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4023 {"langmap",
4024#ifdef FEAT_LANGMAP
4025 1
4026#else
4027 0
4028#endif
4029 },
4030 {"libcall",
4031#ifdef FEAT_LIBCALL
4032 1
4033#else
4034 0
4035#endif
4036 },
4037 {"linebreak",
4038#ifdef FEAT_LINEBREAK
4039 1
4040#else
4041 0
4042#endif
4043 },
4044 {"lispindent",
4045#ifdef FEAT_LISP
4046 1
4047#else
4048 0
4049#endif
4050 },
4051 {"listcmds", 1},
4052 {"localmap", 1},
4053 {"lua",
4054#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4055 1
4056#else
4057 0
4058#endif
4059 },
4060 {"menu",
4061#ifdef FEAT_MENU
4062 1
4063#else
4064 0
4065#endif
4066 },
4067 {"mksession",
4068#ifdef FEAT_SESSION
4069 1
4070#else
4071 0
4072#endif
4073 },
4074 {"modify_fname", 1},
4075 {"mouse", 1},
4076 {"mouseshape",
4077#ifdef FEAT_MOUSESHAPE
4078 1
4079#else
4080 0
4081#endif
4082 },
4083 {"mouse_dec",
4084#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4085 1
4086#else
4087 0
4088#endif
4089 },
4090 {"mouse_gpm",
4091#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4092 1
4093#else
4094 0
4095#endif
4096 },
4097 {"mouse_jsbterm",
4098#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4099 1
4100#else
4101 0
4102#endif
4103 },
4104 {"mouse_netterm",
4105#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4106 1
4107#else
4108 0
4109#endif
4110 },
4111 {"mouse_pterm",
4112#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4113 1
4114#else
4115 0
4116#endif
4117 },
4118 {"mouse_sgr",
4119#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4120 1
4121#else
4122 0
4123#endif
4124 },
4125 {"mouse_sysmouse",
4126#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4127 1
4128#else
4129 0
4130#endif
4131 },
4132 {"mouse_urxvt",
4133#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4134 1
4135#else
4136 0
4137#endif
4138 },
4139 {"mouse_xterm",
4140#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4141 1
4142#else
4143 0
4144#endif
4145 },
4146 {"multi_byte", 1},
4147 {"multi_byte_ime",
4148#ifdef FEAT_MBYTE_IME
4149 1
4150#else
4151 0
4152#endif
4153 },
4154 {"multi_lang",
4155#ifdef FEAT_MULTI_LANG
4156 1
4157#else
4158 0
4159#endif
4160 },
4161 {"mzscheme",
4162#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4163 1
4164#else
4165 0
4166#endif
4167 },
4168 {"num64", 1},
4169 {"ole",
4170#ifdef FEAT_OLE
4171 1
4172#else
4173 0
4174#endif
4175 },
4176 {"packages",
4177#ifdef FEAT_EVAL
4178 1
4179#else
4180 0
4181#endif
4182 },
4183 {"path_extra",
4184#ifdef FEAT_PATH_EXTRA
4185 1
4186#else
4187 0
4188#endif
4189 },
4190 {"perl",
4191#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4192 1
4193#else
4194 0
4195#endif
4196 },
4197 {"persistent_undo",
4198#ifdef FEAT_PERSISTENT_UNDO
4199 1
4200#else
4201 0
4202#endif
4203 },
4204 {"python_compiled",
4205#if defined(FEAT_PYTHON)
4206 1
4207#else
4208 0
4209#endif
4210 },
4211 {"python_dynamic",
4212#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4213 1
4214#else
4215 0
4216#endif
4217 },
4218 {"python",
4219#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4220 1
4221#else
4222 0
4223#endif
4224 },
4225 {"pythonx",
4226#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4227 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"python3_compiled",
4234#if defined(FEAT_PYTHON3)
4235 1
4236#else
4237 0
4238#endif
4239 },
4240 {"python3_dynamic",
4241#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4242 1
4243#else
4244 0
4245#endif
4246 },
4247 {"python3",
4248#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4249 1
4250#else
4251 0
4252#endif
4253 },
4254 {"popupwin",
4255#ifdef FEAT_PROP_POPUP
4256 1
4257#else
4258 0
4259#endif
4260 },
4261 {"postscript",
4262#ifdef FEAT_POSTSCRIPT
4263 1
4264#else
4265 0
4266#endif
4267 },
4268 {"printer",
4269#ifdef FEAT_PRINTER
4270 1
4271#else
4272 0
4273#endif
4274 },
4275 {"profile",
4276#ifdef FEAT_PROFILE
4277 1
4278#else
4279 0
4280#endif
4281 },
4282 {"reltime",
4283#ifdef FEAT_RELTIME
4284 1
4285#else
4286 0
4287#endif
4288 },
4289 {"quickfix",
4290#ifdef FEAT_QUICKFIX
4291 1
4292#else
4293 0
4294#endif
4295 },
4296 {"rightleft",
4297#ifdef FEAT_RIGHTLEFT
4298 1
4299#else
4300 0
4301#endif
4302 },
4303 {"ruby",
4304#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4305 1
4306#else
4307 0
4308#endif
4309 },
4310 {"scrollbind", 1},
4311 {"showcmd",
4312#ifdef FEAT_CMDL_INFO
4313 1
4314#else
4315 0
4316#endif
4317 },
4318 {"cmdline_info",
4319#ifdef FEAT_CMDL_INFO
4320 1
4321#else
4322 0
4323#endif
4324 },
4325 {"signs",
4326#ifdef FEAT_SIGNS
4327 1
4328#else
4329 0
4330#endif
4331 },
4332 {"smartindent",
4333#ifdef FEAT_SMARTINDENT
4334 1
4335#else
4336 0
4337#endif
4338 },
4339 {"startuptime",
4340#ifdef STARTUPTIME
4341 1
4342#else
4343 0
4344#endif
4345 },
4346 {"statusline",
4347#ifdef FEAT_STL_OPT
4348 1
4349#else
4350 0
4351#endif
4352 },
4353 {"netbeans_intg",
4354#ifdef FEAT_NETBEANS_INTG
4355 1
4356#else
4357 0
4358#endif
4359 },
4360 {"sound",
4361#ifdef FEAT_SOUND
4362 1
4363#else
4364 0
4365#endif
4366 },
4367 {"spell",
4368#ifdef FEAT_SPELL
4369 1
4370#else
4371 0
4372#endif
4373 },
4374 {"syntax",
4375#ifdef FEAT_SYN_HL
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"system",
4382#if defined(USE_SYSTEM) || !defined(UNIX)
4383 1
4384#else
4385 0
4386#endif
4387 },
4388 {"tag_binary",
4389#ifdef FEAT_TAG_BINS
4390 1
4391#else
4392 0
4393#endif
4394 },
4395 {"tcl",
4396#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"termguicolors",
4403#ifdef FEAT_TERMGUICOLORS
4404 1
4405#else
4406 0
4407#endif
4408 },
4409 {"terminal",
4410#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4411 1
4412#else
4413 0
4414#endif
4415 },
4416 {"terminfo",
4417#ifdef TERMINFO
4418 1
4419#else
4420 0
4421#endif
4422 },
4423 {"termresponse",
4424#ifdef FEAT_TERMRESPONSE
4425 1
4426#else
4427 0
4428#endif
4429 },
4430 {"textobjects",
4431#ifdef FEAT_TEXTOBJ
4432 1
4433#else
4434 0
4435#endif
4436 },
4437 {"textprop",
4438#ifdef FEAT_PROP_POPUP
4439 1
4440#else
4441 0
4442#endif
4443 },
4444 {"tgetent",
4445#ifdef HAVE_TGETENT
4446 1
4447#else
4448 0
4449#endif
4450 },
4451 {"timers",
4452#ifdef FEAT_TIMERS
4453 1
4454#else
4455 0
4456#endif
4457 },
4458 {"title",
4459#ifdef FEAT_TITLE
4460 1
4461#else
4462 0
4463#endif
4464 },
4465 {"toolbar",
4466#ifdef FEAT_TOOLBAR
4467 1
4468#else
4469 0
4470#endif
4471 },
4472 {"unnamedplus",
4473#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4474 1
4475#else
4476 0
4477#endif
4478 },
4479 {"user-commands", 1}, // was accidentally included in 5.4
4480 {"user_commands", 1},
4481 {"vartabs",
4482#ifdef FEAT_VARTABS
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"vertsplit", 1},
4489 {"viminfo",
4490#ifdef FEAT_VIMINFO
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"vimscript-1", 1},
4497 {"vimscript-2", 1},
4498 {"vimscript-3", 1},
4499 {"vimscript-4", 1},
4500 {"virtualedit", 1},
4501 {"visual", 1},
4502 {"visualextra", 1},
4503 {"vreplace", 1},
4504 {"vtp",
4505#ifdef FEAT_VTP
4506 1
4507#else
4508 0
4509#endif
4510 },
4511 {"wildignore",
4512#ifdef FEAT_WILDIGN
4513 1
4514#else
4515 0
4516#endif
4517 },
4518 {"wildmenu",
4519#ifdef FEAT_WILDMENU
4520 1
4521#else
4522 0
4523#endif
4524 },
4525 {"windows", 1},
4526 {"winaltkeys",
4527#ifdef FEAT_WAK
4528 1
4529#else
4530 0
4531#endif
4532 },
4533 {"writebackup",
4534#ifdef FEAT_WRITEBACKUP
4535 1
4536#else
4537 0
4538#endif
4539 },
4540 {"xim",
4541#ifdef FEAT_XIM
4542 1
4543#else
4544 0
4545#endif
4546 },
4547 {"xfontset",
4548#ifdef FEAT_XFONTSET
4549 1
4550#else
4551 0
4552#endif
4553 },
4554 {"xpm",
4555#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4556 1
4557#else
4558 0
4559#endif
4560 },
4561 {"xpm_w32", // for backward compatibility
4562#ifdef FEAT_XPM_W32
4563 1
4564#else
4565 0
4566#endif
4567 },
4568 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 1
4571#else
4572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 },
4575 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 1
4578#else
4579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 },
4582 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 1
4585#else
4586 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 },
4589 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004591 1
4592#else
4593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004595 },
4596 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004598 1
4599#else
4600 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004602 },
4603 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604 };
4605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004606 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004607 for (i = 0; has_list[i].name != NULL; ++i)
4608 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004610 x = TRUE;
4611 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612 break;
4613 }
4614
Bram Moolenaar79296512020-03-22 16:17:14 +01004615 // features also in has_list[] but sometimes enabled at runtime
4616 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004618 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004619 {
4620 // intentionally empty
4621 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004622#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623 else if (STRICMP(name, "balloon_multiline") == 0)
4624 n = multiline_balloon_available();
4625#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004626#ifdef VIMDLL
4627 else if (STRICMP(name, "filterpipe") == 0)
4628 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#endif
4630#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4631 else if (STRICMP(name, "iconv") == 0)
4632 n = iconv_enabled(FALSE);
4633#endif
4634#ifdef DYNAMIC_LUA
4635 else if (STRICMP(name, "lua") == 0)
4636 n = lua_enabled(FALSE);
4637#endif
4638#ifdef DYNAMIC_MZSCHEME
4639 else if (STRICMP(name, "mzscheme") == 0)
4640 n = mzscheme_enabled(FALSE);
4641#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004642#ifdef DYNAMIC_PERL
4643 else if (STRICMP(name, "perl") == 0)
4644 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004645#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004646#ifdef DYNAMIC_PYTHON
4647 else if (STRICMP(name, "python") == 0)
4648 n = python_enabled(FALSE);
4649#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004650#ifdef DYNAMIC_PYTHON3
4651 else if (STRICMP(name, "python3") == 0)
4652 n = python3_enabled(FALSE);
4653#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004654#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4655 else if (STRICMP(name, "pythonx") == 0)
4656 {
4657# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4658 if (p_pyx == 0)
4659 n = python3_enabled(FALSE) || python_enabled(FALSE);
4660 else if (p_pyx == 3)
4661 n = python3_enabled(FALSE);
4662 else if (p_pyx == 2)
4663 n = python_enabled(FALSE);
4664# elif defined(DYNAMIC_PYTHON)
4665 n = python_enabled(FALSE);
4666# elif defined(DYNAMIC_PYTHON3)
4667 n = python3_enabled(FALSE);
4668# endif
4669 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004671#ifdef DYNAMIC_RUBY
4672 else if (STRICMP(name, "ruby") == 0)
4673 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004675#ifdef DYNAMIC_TCL
4676 else if (STRICMP(name, "tcl") == 0)
4677 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004678#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004679#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004680 else if (STRICMP(name, "terminal") == 0)
4681 n = terminal_enabled();
4682#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 }
4684
Bram Moolenaar79296512020-03-22 16:17:14 +01004685 // features not in has_list[]
4686 if (x == FALSE)
4687 {
4688 if (STRNICMP(name, "patch", 5) == 0)
4689 {
4690 x = TRUE;
4691 if (name[5] == '-'
4692 && STRLEN(name) >= 11
4693 && vim_isdigit(name[6])
4694 && vim_isdigit(name[8])
4695 && vim_isdigit(name[10]))
4696 {
4697 int major = atoi((char *)name + 6);
4698 int minor = atoi((char *)name + 8);
4699
4700 // Expect "patch-9.9.01234".
4701 n = (major < VIM_VERSION_MAJOR
4702 || (major == VIM_VERSION_MAJOR
4703 && (minor < VIM_VERSION_MINOR
4704 || (minor == VIM_VERSION_MINOR
4705 && has_patch(atoi((char *)name + 10))))));
4706 }
4707 else
4708 n = has_patch(atoi((char *)name + 5));
4709 }
4710 else if (STRICMP(name, "vim_starting") == 0)
4711 {
4712 x = TRUE;
4713 n = (starting != 0);
4714 }
4715 else if (STRICMP(name, "ttyin") == 0)
4716 {
4717 x = TRUE;
4718 n = mch_input_isatty();
4719 }
4720 else if (STRICMP(name, "ttyout") == 0)
4721 {
4722 x = TRUE;
4723 n = stdout_isatty;
4724 }
4725 else if (STRICMP(name, "multi_byte_encoding") == 0)
4726 {
4727 x = TRUE;
4728 n = has_mbyte;
4729 }
4730 else if (STRICMP(name, "gui_running") == 0)
4731 {
4732 x = TRUE;
4733#ifdef FEAT_GUI
4734 n = (gui.in_use || gui.starting);
4735#endif
4736 }
4737 else if (STRICMP(name, "browse") == 0)
4738 {
4739 x = TRUE;
4740#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4741 n = gui.in_use; // gui_mch_browse() works when GUI is running
4742#endif
4743 }
4744 else if (STRICMP(name, "syntax_items") == 0)
4745 {
4746 x = TRUE;
4747#ifdef FEAT_SYN_HL
4748 n = syntax_present(curwin);
4749#endif
4750 }
4751 else if (STRICMP(name, "vcon") == 0)
4752 {
4753 x = TRUE;
4754#ifdef FEAT_VTP
4755 n = is_term_win32() && has_vtp_working();
4756#endif
4757 }
4758 else if (STRICMP(name, "netbeans_enabled") == 0)
4759 {
4760 x = TRUE;
4761#ifdef FEAT_NETBEANS_INTG
4762 n = netbeans_active();
4763#endif
4764 }
4765 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4766 {
4767 x = TRUE;
4768#ifdef FEAT_MOUSE_GPM
4769 n = gpm_enabled();
4770#endif
4771 }
4772 else if (STRICMP(name, "conpty") == 0)
4773 {
4774 x = TRUE;
4775#if defined(FEAT_TERMINAL) && defined(MSWIN)
4776 n = use_conpty();
4777#endif
4778 }
4779 else if (STRICMP(name, "clipboard_working") == 0)
4780 {
4781 x = TRUE;
4782#ifdef FEAT_CLIPBOARD
4783 n = clip_star.available;
4784#endif
4785 }
4786 }
4787
Bram Moolenaar04637e22020-09-05 18:45:29 +02004788 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01004789 // return whether feature could ever be enabled
4790 rettv->vval.v_number = x;
4791 else
4792 // return whether feature is enabled
4793 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004794}
4795
4796/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797 * "haslocaldir()" function
4798 */
4799 static void
4800f_haslocaldir(typval_T *argvars, typval_T *rettv)
4801{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004802 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004803 win_T *wp = NULL;
4804
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004805 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4806
4807 // Check for window-local and tab-local directories
4808 if (wp != NULL && wp->w_localdir != NULL)
4809 rettv->vval.v_number = 1;
4810 else if (tp != NULL && tp->tp_localdir != NULL)
4811 rettv->vval.v_number = 2;
4812 else
4813 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004814}
4815
4816/*
4817 * "hasmapto()" function
4818 */
4819 static void
4820f_hasmapto(typval_T *argvars, typval_T *rettv)
4821{
4822 char_u *name;
4823 char_u *mode;
4824 char_u buf[NUMBUFLEN];
4825 int abbr = FALSE;
4826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004827 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828 if (argvars[1].v_type == VAR_UNKNOWN)
4829 mode = (char_u *)"nvo";
4830 else
4831 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004833 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02004834 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004835 }
4836
4837 if (map_to_exists(name, mode, abbr))
4838 rettv->vval.v_number = TRUE;
4839 else
4840 rettv->vval.v_number = FALSE;
4841}
4842
4843/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844 * "highlightID(name)" function
4845 */
4846 static void
4847f_hlID(typval_T *argvars, typval_T *rettv)
4848{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850}
4851
4852/*
4853 * "highlight_exists()" function
4854 */
4855 static void
4856f_hlexists(typval_T *argvars, typval_T *rettv)
4857{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004858 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859}
4860
4861/*
4862 * "hostname()" function
4863 */
4864 static void
4865f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4866{
4867 char_u hostname[256];
4868
4869 mch_get_host_name(hostname, 256);
4870 rettv->v_type = VAR_STRING;
4871 rettv->vval.v_string = vim_strsave(hostname);
4872}
4873
4874/*
4875 * iconv() function
4876 */
4877 static void
4878f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4879{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880 char_u buf1[NUMBUFLEN];
4881 char_u buf2[NUMBUFLEN];
4882 char_u *from, *to, *str;
4883 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884
4885 rettv->v_type = VAR_STRING;
4886 rettv->vval.v_string = NULL;
4887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004888 str = tv_get_string(&argvars[0]);
4889 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4890 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004891 vimconv.vc_type = CONV_NONE;
4892 convert_setup(&vimconv, from, to);
4893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004894 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 if (vimconv.vc_type == CONV_NONE)
4896 rettv->vval.v_string = vim_strsave(str);
4897 else
4898 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4899
4900 convert_setup(&vimconv, NULL, NULL);
4901 vim_free(from);
4902 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903}
4904
4905/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906 * "index()" function
4907 */
4908 static void
4909f_index(typval_T *argvars, typval_T *rettv)
4910{
4911 list_T *l;
4912 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004913 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 long idx = 0;
4915 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004916 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917
4918 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004919 if (argvars[0].v_type == VAR_BLOB)
4920 {
4921 typval_T tv;
4922 int start = 0;
4923
4924 if (argvars[2].v_type != VAR_UNKNOWN)
4925 {
4926 start = tv_get_number_chk(&argvars[2], &error);
4927 if (error)
4928 return;
4929 }
4930 b = argvars[0].vval.v_blob;
4931 if (b == NULL)
4932 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004933 if (start < 0)
4934 {
4935 start = blob_len(b) + start;
4936 if (start < 0)
4937 start = 0;
4938 }
4939
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004940 for (idx = start; idx < blob_len(b); ++idx)
4941 {
4942 tv.v_type = VAR_NUMBER;
4943 tv.vval.v_number = blob_get(b, idx);
4944 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4945 {
4946 rettv->vval.v_number = idx;
4947 return;
4948 }
4949 }
4950 return;
4951 }
4952 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004954 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004955 return;
4956 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004957
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 l = argvars[0].vval.v_list;
4959 if (l != NULL)
4960 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004961 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 item = l->lv_first;
4963 if (argvars[2].v_type != VAR_UNKNOWN)
4964 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 // Start at specified item. Use the cached index that list_find()
4966 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004967 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004968 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02004970 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 if (error)
4972 item = NULL;
4973 }
4974
4975 for ( ; item != NULL; item = item->li_next, ++idx)
4976 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4977 {
4978 rettv->vval.v_number = idx;
4979 break;
4980 }
4981 }
4982}
4983
4984static int inputsecret_flag = 0;
4985
4986/*
4987 * "input()" function
4988 * Also handles inputsecret() when inputsecret is set.
4989 */
4990 static void
4991f_input(typval_T *argvars, typval_T *rettv)
4992{
4993 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4994}
4995
4996/*
4997 * "inputdialog()" function
4998 */
4999 static void
5000f_inputdialog(typval_T *argvars, typval_T *rettv)
5001{
5002#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005004 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5005 {
5006 char_u *message;
5007 char_u buf[NUMBUFLEN];
5008 char_u *defstr = (char_u *)"";
5009
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005010 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005012 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5014 else
5015 IObuff[0] = NUL;
5016 if (message != NULL && defstr != NULL
5017 && do_dialog(VIM_QUESTION, NULL, message,
5018 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5019 rettv->vval.v_string = vim_strsave(IObuff);
5020 else
5021 {
5022 if (message != NULL && defstr != NULL
5023 && argvars[1].v_type != VAR_UNKNOWN
5024 && argvars[2].v_type != VAR_UNKNOWN)
5025 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005026 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 else
5028 rettv->vval.v_string = NULL;
5029 }
5030 rettv->v_type = VAR_STRING;
5031 }
5032 else
5033#endif
5034 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5035}
5036
5037/*
5038 * "inputlist()" function
5039 */
5040 static void
5041f_inputlist(typval_T *argvars, typval_T *rettv)
5042{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005043 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 listitem_T *li;
5045 int selected;
5046 int mouse_used;
5047
5048#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 // While starting up, there is no place to enter text. When running tests
5050 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005051 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052 return;
5053#endif
5054 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5055 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005056 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 return;
5058 }
5059
5060 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 msg_row = Rows - 1; // for when 'cmdheight' > 1
5062 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005063 msg_scroll = TRUE;
5064 msg_clr_eos();
5065
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005066 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005067 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005068 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005070 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005071 msg_putchar('\n');
5072 }
5073
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005074 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 selected = prompt_for_number(&mouse_used);
5076 if (mouse_used)
5077 selected -= lines_left;
5078
5079 rettv->vval.v_number = selected;
5080}
5081
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005082static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5083
5084/*
5085 * "inputrestore()" function
5086 */
5087 static void
5088f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5089{
5090 if (ga_userinput.ga_len > 0)
5091 {
5092 --ga_userinput.ga_len;
5093 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5094 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096 }
5097 else if (p_verbose > 1)
5098 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005099 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 }
5102}
5103
5104/*
5105 * "inputsave()" function
5106 */
5107 static void
5108f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5109{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111 if (ga_grow(&ga_userinput, 1) == OK)
5112 {
5113 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5114 + ga_userinput.ga_len);
5115 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117 }
5118 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120}
5121
5122/*
5123 * "inputsecret()" function
5124 */
5125 static void
5126f_inputsecret(typval_T *argvars, typval_T *rettv)
5127{
5128 ++cmdline_star;
5129 ++inputsecret_flag;
5130 f_input(argvars, rettv);
5131 --cmdline_star;
5132 --inputsecret_flag;
5133}
5134
5135/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005136 * "interrupt()" function
5137 */
5138 static void
5139f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5140{
5141 got_int = TRUE;
5142}
5143
5144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 * "invert(expr)" function
5146 */
5147 static void
5148f_invert(typval_T *argvars, typval_T *rettv)
5149{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005150 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151}
5152
5153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 * "islocked()" function
5155 */
5156 static void
5157f_islocked(typval_T *argvars, typval_T *rettv)
5158{
5159 lval_T lv;
5160 char_u *end;
5161 dictitem_T *di;
5162
5163 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005165 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 if (end != NULL && lv.ll_name != NULL)
5167 {
5168 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005169 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005170 else
5171 {
5172 if (lv.ll_tv == NULL)
5173 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005174 di = find_var(lv.ll_name, NULL, TRUE);
5175 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005177 // Consider a variable locked when:
5178 // 1. the variable itself is locked
5179 // 2. the value of the variable is locked.
5180 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005181 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5182 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 }
5184 }
5185 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005186 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005187 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005188 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005190 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005191 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5192 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005194 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5195 }
5196 }
5197
5198 clear_lval(&lv);
5199}
5200
5201#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5202/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005203 * "isinf()" function
5204 */
5205 static void
5206f_isinf(typval_T *argvars, typval_T *rettv)
5207{
5208 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5209 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5210}
5211
5212/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005213 * "isnan()" function
5214 */
5215 static void
5216f_isnan(typval_T *argvars, typval_T *rettv)
5217{
5218 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5219 && isnan(argvars[0].vval.v_float);
5220}
5221#endif
5222
5223/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224 * "last_buffer_nr()" function.
5225 */
5226 static void
5227f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5228{
5229 int n = 0;
5230 buf_T *buf;
5231
Bram Moolenaar29323592016-07-24 22:04:11 +02005232 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 if (n < buf->b_fnum)
5234 n = buf->b_fnum;
5235
5236 rettv->vval.v_number = n;
5237}
5238
5239/*
5240 * "len()" function
5241 */
5242 static void
5243f_len(typval_T *argvars, typval_T *rettv)
5244{
5245 switch (argvars[0].v_type)
5246 {
5247 case VAR_STRING:
5248 case VAR_NUMBER:
5249 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005250 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005251 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005252 case VAR_BLOB:
5253 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5254 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 case VAR_LIST:
5256 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5257 break;
5258 case VAR_DICT:
5259 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5260 break;
5261 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005262 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005264 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005265 case VAR_SPECIAL:
5266 case VAR_FLOAT:
5267 case VAR_FUNC:
5268 case VAR_PARTIAL:
5269 case VAR_JOB:
5270 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005271 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272 break;
5273 }
5274}
5275
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005276 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005277libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005278{
5279#ifdef FEAT_LIBCALL
5280 char_u *string_in;
5281 char_u **string_result;
5282 int nr_result;
5283#endif
5284
5285 rettv->v_type = type;
5286 if (type != VAR_NUMBER)
5287 rettv->vval.v_string = NULL;
5288
5289 if (check_restricted() || check_secure())
5290 return;
5291
5292#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5295 {
5296 string_in = NULL;
5297 if (argvars[2].v_type == VAR_STRING)
5298 string_in = argvars[2].vval.v_string;
5299 if (type == VAR_NUMBER)
5300 string_result = NULL;
5301 else
5302 string_result = &rettv->vval.v_string;
5303 if (mch_libcall(argvars[0].vval.v_string,
5304 argvars[1].vval.v_string,
5305 string_in,
5306 argvars[2].vval.v_number,
5307 string_result,
5308 &nr_result) == OK
5309 && type == VAR_NUMBER)
5310 rettv->vval.v_number = nr_result;
5311 }
5312#endif
5313}
5314
5315/*
5316 * "libcall()" function
5317 */
5318 static void
5319f_libcall(typval_T *argvars, typval_T *rettv)
5320{
5321 libcall_common(argvars, rettv, VAR_STRING);
5322}
5323
5324/*
5325 * "libcallnr()" function
5326 */
5327 static void
5328f_libcallnr(typval_T *argvars, typval_T *rettv)
5329{
5330 libcall_common(argvars, rettv, VAR_NUMBER);
5331}
5332
5333/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005334 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335 */
5336 static void
5337f_line(typval_T *argvars, typval_T *rettv)
5338{
5339 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005340 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005342 int id;
5343 tabpage_T *tp;
5344 win_T *wp;
5345 win_T *save_curwin;
5346 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005348 if (argvars[1].v_type != VAR_UNKNOWN)
5349 {
5350 // use window specified in the second argument
5351 id = (int)tv_get_number(&argvars[1]);
5352 wp = win_id2wp_tp(id, &tp);
5353 if (wp != NULL && tp != NULL)
5354 {
5355 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5356 == OK)
5357 {
5358 check_cursor();
5359 fp = var2fpos(&argvars[0], TRUE, &fnum);
5360 }
5361 restore_win_noblock(save_curwin, save_curtab, TRUE);
5362 }
5363 }
5364 else
5365 // use current window
5366 fp = var2fpos(&argvars[0], TRUE, &fnum);
5367
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 if (fp != NULL)
5369 lnum = fp->lnum;
5370 rettv->vval.v_number = lnum;
5371}
5372
5373/*
5374 * "line2byte(lnum)" function
5375 */
5376 static void
5377f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5378{
5379#ifndef FEAT_BYTEOFF
5380 rettv->vval.v_number = -1;
5381#else
5382 linenr_T lnum;
5383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005384 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5386 rettv->vval.v_number = -1;
5387 else
5388 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5389 if (rettv->vval.v_number >= 0)
5390 ++rettv->vval.v_number;
5391#endif
5392}
5393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394#ifdef FEAT_FLOAT
5395/*
5396 * "log()" function
5397 */
5398 static void
5399f_log(typval_T *argvars, typval_T *rettv)
5400{
5401 float_T f = 0.0;
5402
5403 rettv->v_type = VAR_FLOAT;
5404 if (get_float_arg(argvars, &f) == OK)
5405 rettv->vval.v_float = log(f);
5406 else
5407 rettv->vval.v_float = 0.0;
5408}
5409
5410/*
5411 * "log10()" function
5412 */
5413 static void
5414f_log10(typval_T *argvars, typval_T *rettv)
5415{
5416 float_T f = 0.0;
5417
5418 rettv->v_type = VAR_FLOAT;
5419 if (get_float_arg(argvars, &f) == OK)
5420 rettv->vval.v_float = log10(f);
5421 else
5422 rettv->vval.v_float = 0.0;
5423}
5424#endif
5425
5426#ifdef FEAT_LUA
5427/*
5428 * "luaeval()" function
5429 */
5430 static void
5431f_luaeval(typval_T *argvars, typval_T *rettv)
5432{
5433 char_u *str;
5434 char_u buf[NUMBUFLEN];
5435
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005436 if (check_restricted() || check_secure())
5437 return;
5438
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005439 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005440 do_luaeval(str, argvars + 1, rettv);
5441}
5442#endif
5443
5444/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005445 * "maparg()" function
5446 */
5447 static void
5448f_maparg(typval_T *argvars, typval_T *rettv)
5449{
5450 get_maparg(argvars, rettv, TRUE);
5451}
5452
5453/*
5454 * "mapcheck()" function
5455 */
5456 static void
5457f_mapcheck(typval_T *argvars, typval_T *rettv)
5458{
5459 get_maparg(argvars, rettv, FALSE);
5460}
5461
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005462typedef enum
5463{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005464 MATCH_END, // matchend()
5465 MATCH_MATCH, // match()
5466 MATCH_STR, // matchstr()
5467 MATCH_LIST, // matchlist()
5468 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005469} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470
5471 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005472find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473{
5474 char_u *str = NULL;
5475 long len = 0;
5476 char_u *expr = NULL;
5477 char_u *pat;
5478 regmatch_T regmatch;
5479 char_u patbuf[NUMBUFLEN];
5480 char_u strbuf[NUMBUFLEN];
5481 char_u *save_cpo;
5482 long start = 0;
5483 long nth = 1;
5484 colnr_T startcol = 0;
5485 int match = 0;
5486 list_T *l = NULL;
5487 listitem_T *li = NULL;
5488 long idx = 0;
5489 char_u *tofree = NULL;
5490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005491 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 save_cpo = p_cpo;
5493 p_cpo = (char_u *)"";
5494
5495 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005496 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005498 // type MATCH_LIST: return empty list when there are no matches.
5499 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 if (rettv_list_alloc(rettv) == FAIL)
5501 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005502 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503 && (list_append_string(rettv->vval.v_list,
5504 (char_u *)"", 0) == FAIL
5505 || list_append_number(rettv->vval.v_list,
5506 (varnumber_T)-1) == FAIL
5507 || list_append_number(rettv->vval.v_list,
5508 (varnumber_T)-1) == FAIL
5509 || list_append_number(rettv->vval.v_list,
5510 (varnumber_T)-1) == FAIL))
5511 {
5512 list_free(rettv->vval.v_list);
5513 rettv->vval.v_list = NULL;
5514 goto theend;
5515 }
5516 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005517 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518 {
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = NULL;
5521 }
5522
5523 if (argvars[0].v_type == VAR_LIST)
5524 {
5525 if ((l = argvars[0].vval.v_list) == NULL)
5526 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005527 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 li = l->lv_first;
5529 }
5530 else
5531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005532 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533 len = (long)STRLEN(str);
5534 }
5535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005536 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537 if (pat == NULL)
5538 goto theend;
5539
5540 if (argvars[2].v_type != VAR_UNKNOWN)
5541 {
5542 int error = FALSE;
5543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005544 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 if (error)
5546 goto theend;
5547 if (l != NULL)
5548 {
5549 li = list_find(l, start);
5550 if (li == NULL)
5551 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005552 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 }
5554 else
5555 {
5556 if (start < 0)
5557 start = 0;
5558 if (start > len)
5559 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005560 // When "count" argument is there ignore matches before "start",
5561 // otherwise skip part of the string. Differs when pattern is "^"
5562 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 if (argvars[3].v_type != VAR_UNKNOWN)
5564 startcol = start;
5565 else
5566 {
5567 str += start;
5568 len -= start;
5569 }
5570 }
5571
5572 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005573 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 if (error)
5575 goto theend;
5576 }
5577
5578 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5579 if (regmatch.regprog != NULL)
5580 {
5581 regmatch.rm_ic = p_ic;
5582
5583 for (;;)
5584 {
5585 if (l != NULL)
5586 {
5587 if (li == NULL)
5588 {
5589 match = FALSE;
5590 break;
5591 }
5592 vim_free(tofree);
5593 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5594 if (str == NULL)
5595 break;
5596 }
5597
5598 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5599
5600 if (match && --nth <= 0)
5601 break;
5602 if (l == NULL && !match)
5603 break;
5604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 if (l != NULL)
5607 {
5608 li = li->li_next;
5609 ++idx;
5610 }
5611 else
5612 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 startcol = (colnr_T)(regmatch.startp[0]
5614 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 if (startcol > (colnr_T)len
5616 || str + startcol <= regmatch.startp[0])
5617 {
5618 match = FALSE;
5619 break;
5620 }
5621 }
5622 }
5623
5624 if (match)
5625 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005626 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 {
5628 listitem_T *li1 = rettv->vval.v_list->lv_first;
5629 listitem_T *li2 = li1->li_next;
5630 listitem_T *li3 = li2->li_next;
5631 listitem_T *li4 = li3->li_next;
5632
5633 vim_free(li1->li_tv.vval.v_string);
5634 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005635 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 li3->li_tv.vval.v_number =
5637 (varnumber_T)(regmatch.startp[0] - expr);
5638 li4->li_tv.vval.v_number =
5639 (varnumber_T)(regmatch.endp[0] - expr);
5640 if (l != NULL)
5641 li2->li_tv.vval.v_number = (varnumber_T)idx;
5642 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005643 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005644 {
5645 int i;
5646
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 for (i = 0; i < NSUBEXP; ++i)
5649 {
5650 if (regmatch.endp[i] == NULL)
5651 {
5652 if (list_append_string(rettv->vval.v_list,
5653 (char_u *)"", 0) == FAIL)
5654 break;
5655 }
5656 else if (list_append_string(rettv->vval.v_list,
5657 regmatch.startp[i],
5658 (int)(regmatch.endp[i] - regmatch.startp[i]))
5659 == FAIL)
5660 break;
5661 }
5662 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005663 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005665 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666 if (l != NULL)
5667 copy_tv(&li->li_tv, rettv);
5668 else
5669 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005670 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671 }
5672 else if (l != NULL)
5673 rettv->vval.v_number = idx;
5674 else
5675 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005676 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005677 rettv->vval.v_number =
5678 (varnumber_T)(regmatch.startp[0] - str);
5679 else
5680 rettv->vval.v_number =
5681 (varnumber_T)(regmatch.endp[0] - str);
5682 rettv->vval.v_number += (varnumber_T)(str - expr);
5683 }
5684 }
5685 vim_regfree(regmatch.regprog);
5686 }
5687
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005688theend:
5689 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005690 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005691 listitem_remove(rettv->vval.v_list,
5692 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 vim_free(tofree);
5694 p_cpo = save_cpo;
5695}
5696
5697/*
5698 * "match()" function
5699 */
5700 static void
5701f_match(typval_T *argvars, typval_T *rettv)
5702{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005703 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704}
5705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706/*
5707 * "matchend()" function
5708 */
5709 static void
5710f_matchend(typval_T *argvars, typval_T *rettv)
5711{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005712 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005713}
5714
5715/*
5716 * "matchlist()" function
5717 */
5718 static void
5719f_matchlist(typval_T *argvars, typval_T *rettv)
5720{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005721 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722}
5723
5724/*
5725 * "matchstr()" function
5726 */
5727 static void
5728f_matchstr(typval_T *argvars, typval_T *rettv)
5729{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005730 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005731}
5732
5733/*
5734 * "matchstrpos()" function
5735 */
5736 static void
5737f_matchstrpos(typval_T *argvars, typval_T *rettv)
5738{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005739 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740}
5741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005742 static void
5743max_min(typval_T *argvars, typval_T *rettv, int domax)
5744{
5745 varnumber_T n = 0;
5746 varnumber_T i;
5747 int error = FALSE;
5748
5749 if (argvars[0].v_type == VAR_LIST)
5750 {
5751 list_T *l;
5752 listitem_T *li;
5753
5754 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005755 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005757 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005759 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5760 n = l->lv_u.nonmat.lv_start;
5761 else
5762 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5763 * l->lv_u.nonmat.lv_stride;
5764 }
5765 else
5766 {
5767 li = l->lv_first;
5768 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005770 n = tv_get_number_chk(&li->li_tv, &error);
5771 for (;;)
5772 {
5773 li = li->li_next;
5774 if (li == NULL)
5775 break;
5776 i = tv_get_number_chk(&li->li_tv, &error);
5777 if (domax ? i > n : i < n)
5778 n = i;
5779 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 }
5781 }
5782 }
5783 }
5784 else if (argvars[0].v_type == VAR_DICT)
5785 {
5786 dict_T *d;
5787 int first = TRUE;
5788 hashitem_T *hi;
5789 int todo;
5790
5791 d = argvars[0].vval.v_dict;
5792 if (d != NULL)
5793 {
5794 todo = (int)d->dv_hashtab.ht_used;
5795 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5796 {
5797 if (!HASHITEM_EMPTY(hi))
5798 {
5799 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005800 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 if (first)
5802 {
5803 n = i;
5804 first = FALSE;
5805 }
5806 else if (domax ? i > n : i < n)
5807 n = i;
5808 }
5809 }
5810 }
5811 }
5812 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 rettv->vval.v_number = error ? 0 : n;
5815}
5816
5817/*
5818 * "max()" function
5819 */
5820 static void
5821f_max(typval_T *argvars, typval_T *rettv)
5822{
5823 max_min(argvars, rettv, TRUE);
5824}
5825
5826/*
5827 * "min()" function
5828 */
5829 static void
5830f_min(typval_T *argvars, typval_T *rettv)
5831{
5832 max_min(argvars, rettv, FALSE);
5833}
5834
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005835#if defined(FEAT_MZSCHEME) || defined(PROTO)
5836/*
5837 * "mzeval()" function
5838 */
5839 static void
5840f_mzeval(typval_T *argvars, typval_T *rettv)
5841{
5842 char_u *str;
5843 char_u buf[NUMBUFLEN];
5844
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005845 if (check_restricted() || check_secure())
5846 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005847 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005848 do_mzeval(str, rettv);
5849}
5850
5851 void
5852mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5853{
5854 typval_T argvars[3];
5855
5856 argvars[0].v_type = VAR_STRING;
5857 argvars[0].vval.v_string = name;
5858 copy_tv(args, &argvars[1]);
5859 argvars[2].v_type = VAR_UNKNOWN;
5860 f_call(argvars, rettv);
5861 clear_tv(&argvars[1]);
5862}
5863#endif
5864
5865/*
5866 * "nextnonblank()" function
5867 */
5868 static void
5869f_nextnonblank(typval_T *argvars, typval_T *rettv)
5870{
5871 linenr_T lnum;
5872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005873 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005874 {
5875 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5876 {
5877 lnum = 0;
5878 break;
5879 }
5880 if (*skipwhite(ml_get(lnum)) != NUL)
5881 break;
5882 }
5883 rettv->vval.v_number = lnum;
5884}
5885
5886/*
5887 * "nr2char()" function
5888 */
5889 static void
5890f_nr2char(typval_T *argvars, typval_T *rettv)
5891{
5892 char_u buf[NUMBUFLEN];
5893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 if (has_mbyte)
5895 {
5896 int utf8 = 0;
5897
5898 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02005899 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005901 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005903 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904 }
5905 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 buf[1] = NUL;
5909 }
5910 rettv->v_type = VAR_STRING;
5911 rettv->vval.v_string = vim_strsave(buf);
5912}
5913
5914/*
5915 * "or(expr, expr)" function
5916 */
5917 static void
5918f_or(typval_T *argvars, typval_T *rettv)
5919{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005920 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5921 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005922}
5923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924#ifdef FEAT_PERL
5925/*
5926 * "perleval()" function
5927 */
5928 static void
5929f_perleval(typval_T *argvars, typval_T *rettv)
5930{
5931 char_u *str;
5932 char_u buf[NUMBUFLEN];
5933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005934 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935 do_perleval(str, rettv);
5936}
5937#endif
5938
5939#ifdef FEAT_FLOAT
5940/*
5941 * "pow()" function
5942 */
5943 static void
5944f_pow(typval_T *argvars, typval_T *rettv)
5945{
5946 float_T fx = 0.0, fy = 0.0;
5947
5948 rettv->v_type = VAR_FLOAT;
5949 if (get_float_arg(argvars, &fx) == OK
5950 && get_float_arg(&argvars[1], &fy) == OK)
5951 rettv->vval.v_float = pow(fx, fy);
5952 else
5953 rettv->vval.v_float = 0.0;
5954}
5955#endif
5956
5957/*
5958 * "prevnonblank()" function
5959 */
5960 static void
5961f_prevnonblank(typval_T *argvars, typval_T *rettv)
5962{
5963 linenr_T lnum;
5964
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005965 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5967 lnum = 0;
5968 else
5969 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5970 --lnum;
5971 rettv->vval.v_number = lnum;
5972}
5973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974// This dummy va_list is here because:
5975// - passing a NULL pointer doesn't work when va_list isn't a pointer
5976// - locally in the function results in a "used before set" warning
5977// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978static va_list ap;
5979
5980/*
5981 * "printf()" function
5982 */
5983 static void
5984f_printf(typval_T *argvars, typval_T *rettv)
5985{
5986 char_u buf[NUMBUFLEN];
5987 int len;
5988 char_u *s;
5989 int saved_did_emsg = did_emsg;
5990 char *fmt;
5991
5992 rettv->v_type = VAR_STRING;
5993 rettv->vval.v_string = NULL;
5994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005995 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005997 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005998 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999 if (!did_emsg)
6000 {
6001 s = alloc(len + 1);
6002 if (s != NULL)
6003 {
6004 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006005 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6006 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 }
6008 }
6009 did_emsg |= saved_did_emsg;
6010}
6011
6012/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006013 * "pum_getpos()" function
6014 */
6015 static void
6016f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6017{
6018 if (rettv_dict_alloc(rettv) != OK)
6019 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006020 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006021}
6022
6023/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 * "pumvisible()" function
6025 */
6026 static void
6027f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6028{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 if (pum_visible())
6030 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031}
6032
6033#ifdef FEAT_PYTHON3
6034/*
6035 * "py3eval()" function
6036 */
6037 static void
6038f_py3eval(typval_T *argvars, typval_T *rettv)
6039{
6040 char_u *str;
6041 char_u buf[NUMBUFLEN];
6042
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006043 if (check_restricted() || check_secure())
6044 return;
6045
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006046 if (p_pyx == 0)
6047 p_pyx = 3;
6048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 do_py3eval(str, rettv);
6051}
6052#endif
6053
6054#ifdef FEAT_PYTHON
6055/*
6056 * "pyeval()" function
6057 */
6058 static void
6059f_pyeval(typval_T *argvars, typval_T *rettv)
6060{
6061 char_u *str;
6062 char_u buf[NUMBUFLEN];
6063
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006064 if (check_restricted() || check_secure())
6065 return;
6066
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006067 if (p_pyx == 0)
6068 p_pyx = 2;
6069
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006070 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006071 do_pyeval(str, rettv);
6072}
6073#endif
6074
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006075#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6076/*
6077 * "pyxeval()" function
6078 */
6079 static void
6080f_pyxeval(typval_T *argvars, typval_T *rettv)
6081{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006082 if (check_restricted() || check_secure())
6083 return;
6084
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006085# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6086 init_pyxversion();
6087 if (p_pyx == 2)
6088 f_pyeval(argvars, rettv);
6089 else
6090 f_py3eval(argvars, rettv);
6091# elif defined(FEAT_PYTHON)
6092 f_pyeval(argvars, rettv);
6093# elif defined(FEAT_PYTHON3)
6094 f_py3eval(argvars, rettv);
6095# endif
6096}
6097#endif
6098
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006099static UINT32_T srand_seed_for_testing = 0;
6100static int srand_seed_for_testing_is_used = FALSE;
6101
6102 static void
6103f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6104{
6105 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006106 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006107 else
6108 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006109 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6110 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006111 }
6112}
6113
6114 static void
6115init_srand(UINT32_T *x)
6116{
6117#ifndef MSWIN
6118 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6119#endif
6120
6121 if (srand_seed_for_testing_is_used)
6122 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006123 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006124 return;
6125 }
6126#ifndef MSWIN
6127 if (dev_urandom_state != FAIL)
6128 {
6129 int fd = open("/dev/urandom", O_RDONLY);
6130 struct {
6131 union {
6132 UINT32_T number;
6133 char bytes[sizeof(UINT32_T)];
6134 } contents;
6135 } buf;
6136
6137 // Attempt reading /dev/urandom.
6138 if (fd == -1)
6139 dev_urandom_state = FAIL;
6140 else
6141 {
6142 buf.contents.number = 0;
6143 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6144 != sizeof(UINT32_T))
6145 dev_urandom_state = FAIL;
6146 else
6147 {
6148 dev_urandom_state = OK;
6149 *x = buf.contents.number;
6150 }
6151 close(fd);
6152 }
6153 }
6154 if (dev_urandom_state != OK)
6155 // Reading /dev/urandom doesn't work, fall back to time().
6156#endif
6157 *x = vim_time();
6158}
6159
6160#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6161#define SPLITMIX32(x, z) ( \
6162 z = (x += 0x9e3779b9), \
6163 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6164 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6165 z ^ (z >> 16) \
6166 )
6167#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6168 result = ROTL(y * 5, 7) * 9; \
6169 t = y << 9; \
6170 z ^= x; \
6171 w ^= y; \
6172 y ^= z, x ^= w; \
6173 z ^= t; \
6174 w = ROTL(w, 11);
6175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006177 * "rand()" function
6178 */
6179 static void
6180f_rand(typval_T *argvars, typval_T *rettv)
6181{
6182 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006183 static UINT32_T gx, gy, gz, gw;
6184 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006185 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006186 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006187
6188 if (argvars[0].v_type == VAR_UNKNOWN)
6189 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006190 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006191 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006192 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006193 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006194 init_srand(&x);
6195
6196 gx = SPLITMIX32(x, z);
6197 gy = SPLITMIX32(x, z);
6198 gz = SPLITMIX32(x, z);
6199 gw = SPLITMIX32(x, z);
6200 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006201 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006202
6203 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006204 }
6205 else if (argvars[0].v_type == VAR_LIST)
6206 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006207 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006208 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006209 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006210
6211 lx = list_find(l, 0L);
6212 ly = list_find(l, 1L);
6213 lz = list_find(l, 2L);
6214 lw = list_find(l, 3L);
6215 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6216 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6217 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6218 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6219 x = (UINT32_T)lx->li_tv.vval.v_number;
6220 y = (UINT32_T)ly->li_tv.vval.v_number;
6221 z = (UINT32_T)lz->li_tv.vval.v_number;
6222 w = (UINT32_T)lw->li_tv.vval.v_number;
6223
6224 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6225
6226 lx->li_tv.vval.v_number = (varnumber_T)x;
6227 ly->li_tv.vval.v_number = (varnumber_T)y;
6228 lz->li_tv.vval.v_number = (varnumber_T)z;
6229 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006230 }
6231 else
6232 goto theend;
6233
6234 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006235 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006236 return;
6237
6238theend:
6239 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006240 rettv->v_type = VAR_NUMBER;
6241 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006242}
6243
6244/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006245 * "srand()" function
6246 */
6247 static void
6248f_srand(typval_T *argvars, typval_T *rettv)
6249{
6250 UINT32_T x = 0, z;
6251
6252 if (rettv_list_alloc(rettv) == FAIL)
6253 return;
6254 if (argvars[0].v_type == VAR_UNKNOWN)
6255 {
6256 init_srand(&x);
6257 }
6258 else
6259 {
6260 int error = FALSE;
6261
6262 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6263 if (error)
6264 return;
6265 }
6266
6267 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6268 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6269 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6270 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6271}
6272
6273#undef ROTL
6274#undef SPLITMIX32
6275#undef SHUFFLE_XOSHIRO128STARSTAR
6276
6277/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 * "range()" function
6279 */
6280 static void
6281f_range(typval_T *argvars, typval_T *rettv)
6282{
6283 varnumber_T start;
6284 varnumber_T end;
6285 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 int error = FALSE;
6287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006288 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 if (argvars[1].v_type == VAR_UNKNOWN)
6290 {
6291 end = start - 1;
6292 start = 0;
6293 }
6294 else
6295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006298 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 }
6300
6301 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006302 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006304 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006306 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 list_T *list = rettv->vval.v_list;
6310
6311 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006312 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313 // be called.
6314 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006315 list->lv_u.nonmat.lv_start = start;
6316 list->lv_u.nonmat.lv_end = end;
6317 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006318 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006319 }
6320}
6321
6322/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006323 * Materialize "list".
6324 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 */
6326 void
6327range_list_materialize(list_T *list)
6328{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006329 varnumber_T start = list->lv_u.nonmat.lv_start;
6330 varnumber_T end = list->lv_u.nonmat.lv_end;
6331 int stride = list->lv_u.nonmat.lv_stride;
6332 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006334 list->lv_first = NULL;
6335 list->lv_u.mat.lv_last = NULL;
6336 list->lv_len = 0;
6337 list->lv_u.mat.lv_idx_item = NULL;
6338 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6339 if (list_append_number(list, (varnumber_T)i) == FAIL)
6340 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341}
6342
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006343/*
6344 * "getreginfo()" function
6345 */
6346 static void
6347f_getreginfo(typval_T *argvars, typval_T *rettv)
6348{
6349 char_u *strregname;
6350 int regname;
6351 char_u buf[NUMBUFLEN + 2];
6352 long reglen = 0;
6353 dict_T *dict;
6354 list_T *list;
6355
6356 if (argvars[0].v_type != VAR_UNKNOWN)
6357 {
6358 strregname = tv_get_string_chk(&argvars[0]);
6359 if (strregname == NULL)
6360 return;
6361 }
6362 else
6363 strregname = get_vim_var_str(VV_REG);
6364
6365 regname = (strregname == NULL ? '"' : *strregname);
6366 if (regname == 0 || regname == '@')
6367 regname = '"';
6368
6369 if (rettv_dict_alloc(rettv) == FAIL)
6370 return;
6371 dict = rettv->vval.v_dict;
6372
6373 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6374 if (list == NULL)
6375 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006376 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006377
6378 buf[0] = NUL;
6379 buf[1] = NUL;
6380 switch (get_reg_type(regname, &reglen))
6381 {
6382 case MLINE: buf[0] = 'V'; break;
6383 case MCHAR: buf[0] = 'v'; break;
6384 case MBLOCK:
6385 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6386 reglen + 1);
6387 break;
6388 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006389 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006390
6391 buf[0] = get_register_name(get_unname_register());
6392 buf[1] = NUL;
6393 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006394 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006395 else
6396 {
6397 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6398
6399 if (item != NULL)
6400 {
6401 item->di_tv.v_type = VAR_SPECIAL;
6402 item->di_tv.vval.v_number = regname == buf[0]
6403 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006404 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006405 }
6406 }
6407}
6408
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006409 static void
6410return_register(int regname, typval_T *rettv)
6411{
6412 char_u buf[2] = {0, 0};
6413
6414 buf[0] = (char_u)regname;
6415 rettv->v_type = VAR_STRING;
6416 rettv->vval.v_string = vim_strsave(buf);
6417}
6418
6419/*
6420 * "reg_executing()" function
6421 */
6422 static void
6423f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6424{
6425 return_register(reg_executing, rettv);
6426}
6427
6428/*
6429 * "reg_recording()" function
6430 */
6431 static void
6432f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6433{
6434 return_register(reg_recording, rettv);
6435}
6436
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006437/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 * "rename({from}, {to})" function
6439 */
6440 static void
6441f_rename(typval_T *argvars, typval_T *rettv)
6442{
6443 char_u buf[NUMBUFLEN];
6444
6445 if (check_restricted() || check_secure())
6446 rettv->vval.v_number = -1;
6447 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006448 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6449 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006450}
6451
6452/*
6453 * "repeat()" function
6454 */
6455 static void
6456f_repeat(typval_T *argvars, typval_T *rettv)
6457{
6458 char_u *p;
6459 int n;
6460 int slen;
6461 int len;
6462 char_u *r;
6463 int i;
6464
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006465 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466 if (argvars[0].v_type == VAR_LIST)
6467 {
6468 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6469 while (n-- > 0)
6470 if (list_extend(rettv->vval.v_list,
6471 argvars[0].vval.v_list, NULL) == FAIL)
6472 break;
6473 }
6474 else
6475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006476 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 rettv->v_type = VAR_STRING;
6478 rettv->vval.v_string = NULL;
6479
6480 slen = (int)STRLEN(p);
6481 len = slen * n;
6482 if (len <= 0)
6483 return;
6484
6485 r = alloc(len + 1);
6486 if (r != NULL)
6487 {
6488 for (i = 0; i < n; i++)
6489 mch_memmove(r + i * slen, p, (size_t)slen);
6490 r[len] = NUL;
6491 }
6492
6493 rettv->vval.v_string = r;
6494 }
6495}
6496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006497#define SP_NOMOVE 0x01 // don't move cursor
6498#define SP_REPEAT 0x02 // repeat to find outer pair
6499#define SP_RETCOUNT 0x04 // return matchcount
6500#define SP_SETPCMARK 0x08 // set previous context mark
6501#define SP_START 0x10 // accept match at start position
6502#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6503#define SP_END 0x40 // leave cursor at end of match
6504#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506/*
6507 * Get flags for a search function.
6508 * Possibly sets "p_ws".
6509 * Returns BACKWARD, FORWARD or zero (for an error).
6510 */
6511 static int
6512get_search_arg(typval_T *varp, int *flagsp)
6513{
6514 int dir = FORWARD;
6515 char_u *flags;
6516 char_u nbuf[NUMBUFLEN];
6517 int mask;
6518
6519 if (varp->v_type != VAR_UNKNOWN)
6520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006521 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006523 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524 while (*flags != NUL)
6525 {
6526 switch (*flags)
6527 {
6528 case 'b': dir = BACKWARD; break;
6529 case 'w': p_ws = TRUE; break;
6530 case 'W': p_ws = FALSE; break;
6531 default: mask = 0;
6532 if (flagsp != NULL)
6533 switch (*flags)
6534 {
6535 case 'c': mask = SP_START; break;
6536 case 'e': mask = SP_END; break;
6537 case 'm': mask = SP_RETCOUNT; break;
6538 case 'n': mask = SP_NOMOVE; break;
6539 case 'p': mask = SP_SUBPAT; break;
6540 case 'r': mask = SP_REPEAT; break;
6541 case 's': mask = SP_SETPCMARK; break;
6542 case 'z': mask = SP_COLUMN; break;
6543 }
6544 if (mask == 0)
6545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006546 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547 dir = 0;
6548 }
6549 else
6550 *flagsp |= mask;
6551 }
6552 if (dir == 0)
6553 break;
6554 ++flags;
6555 }
6556 }
6557 return dir;
6558}
6559
6560/*
6561 * Shared by search() and searchpos() functions.
6562 */
6563 static int
6564search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6565{
6566 int flags;
6567 char_u *pat;
6568 pos_T pos;
6569 pos_T save_cursor;
6570 int save_p_ws = p_ws;
6571 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006574#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006575 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 long time_limit = 0;
6577#endif
6578 int options = SEARCH_KEEP;
6579 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006580 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006581 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006582 pos_T firstpos;
6583
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006584 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006585 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 if (dir == 0)
6587 goto theend;
6588 flags = *flagsp;
6589 if (flags & SP_START)
6590 options |= SEARCH_START;
6591 if (flags & SP_END)
6592 options |= SEARCH_END;
6593 if (flags & SP_COLUMN)
6594 options |= SEARCH_COL;
6595
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006596 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006599 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 if (lnum_stop < 0)
6601 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602 if (argvars[3].v_type != VAR_UNKNOWN)
6603 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006604#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006605 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606 if (time_limit < 0)
6607 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006609 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006610 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 }
6612
6613#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615 profile_setlimit(time_limit, &tm);
6616#endif
6617
6618 /*
6619 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6620 * Check to make sure only those flags are set.
6621 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6622 * flags cannot be set. Check for that condition also.
6623 */
6624 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6625 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006627 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 goto theend;
6629 }
6630
6631 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006632 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006633 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006634 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6635#ifdef FEAT_RELTIME
6636 sia.sa_tm = &tm;
6637#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006638
6639 // Repeat until {skip} returns FALSE.
6640 for (;;)
6641 {
6642 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006643 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006644 // finding the first match again means there is no match where {skip}
6645 // evaluates to zero.
6646 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6647 subpatnum = FAIL;
6648
Bram Moolenaara9c01042020-06-07 14:50:50 +02006649 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006650 // didn't find it or no skip argument
6651 break;
6652 firstpos = pos;
6653
Bram Moolenaara9c01042020-06-07 14:50:50 +02006654 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006655 {
6656 int do_skip;
6657 int err;
6658 pos_T save_pos = curwin->w_cursor;
6659
6660 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006661 err = FALSE;
6662 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006663 curwin->w_cursor = save_pos;
6664 if (err)
6665 {
6666 // Evaluating {skip} caused an error, break here.
6667 subpatnum = FAIL;
6668 break;
6669 }
6670 if (!do_skip)
6671 break;
6672 }
6673 }
6674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 if (subpatnum != FAIL)
6676 {
6677 if (flags & SP_SUBPAT)
6678 retval = subpatnum;
6679 else
6680 retval = pos.lnum;
6681 if (flags & SP_SETPCMARK)
6682 setpcmark();
6683 curwin->w_cursor = pos;
6684 if (match_pos != NULL)
6685 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006686 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687 match_pos->lnum = pos.lnum;
6688 match_pos->col = pos.col + 1;
6689 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690 // "/$" will put the cursor after the end of the line, may need to
6691 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692 check_cursor();
6693 }
6694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006696 if (flags & SP_NOMOVE)
6697 curwin->w_cursor = save_cursor;
6698 else
6699 curwin->w_set_curswant = TRUE;
6700theend:
6701 p_ws = save_p_ws;
6702
6703 return retval;
6704}
6705
6706#ifdef FEAT_FLOAT
6707
6708/*
6709 * round() is not in C90, use ceil() or floor() instead.
6710 */
6711 float_T
6712vim_round(float_T f)
6713{
6714 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6715}
6716
6717/*
6718 * "round({float})" function
6719 */
6720 static void
6721f_round(typval_T *argvars, typval_T *rettv)
6722{
6723 float_T f = 0.0;
6724
6725 rettv->v_type = VAR_FLOAT;
6726 if (get_float_arg(argvars, &f) == OK)
6727 rettv->vval.v_float = vim_round(f);
6728 else
6729 rettv->vval.v_float = 0.0;
6730}
6731#endif
6732
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006733#ifdef FEAT_RUBY
6734/*
6735 * "rubyeval()" function
6736 */
6737 static void
6738f_rubyeval(typval_T *argvars, typval_T *rettv)
6739{
6740 char_u *str;
6741 char_u buf[NUMBUFLEN];
6742
6743 str = tv_get_string_buf(&argvars[0], buf);
6744 do_rubyeval(str, rettv);
6745}
6746#endif
6747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748/*
6749 * "screenattr()" function
6750 */
6751 static void
6752f_screenattr(typval_T *argvars, typval_T *rettv)
6753{
6754 int row;
6755 int col;
6756 int c;
6757
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006758 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6759 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760 if (row < 0 || row >= screen_Rows
6761 || col < 0 || col >= screen_Columns)
6762 c = -1;
6763 else
6764 c = ScreenAttrs[LineOffset[row] + col];
6765 rettv->vval.v_number = c;
6766}
6767
6768/*
6769 * "screenchar()" function
6770 */
6771 static void
6772f_screenchar(typval_T *argvars, typval_T *rettv)
6773{
6774 int row;
6775 int col;
6776 int off;
6777 int c;
6778
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006779 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6780 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006781 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 c = -1;
6783 else
6784 {
6785 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 if (enc_utf8 && ScreenLinesUC[off] != 0)
6787 c = ScreenLinesUC[off];
6788 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006789 c = ScreenLines[off];
6790 }
6791 rettv->vval.v_number = c;
6792}
6793
6794/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006795 * "screenchars()" function
6796 */
6797 static void
6798f_screenchars(typval_T *argvars, typval_T *rettv)
6799{
6800 int row;
6801 int col;
6802 int off;
6803 int c;
6804 int i;
6805
6806 if (rettv_list_alloc(rettv) == FAIL)
6807 return;
6808 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6809 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6810 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6811 return;
6812
6813 off = LineOffset[row] + col;
6814 if (enc_utf8 && ScreenLinesUC[off] != 0)
6815 c = ScreenLinesUC[off];
6816 else
6817 c = ScreenLines[off];
6818 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6819
6820 if (enc_utf8)
6821
6822 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6823 list_append_number(rettv->vval.v_list,
6824 (varnumber_T)ScreenLinesC[i][off]);
6825}
6826
6827/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828 * "screencol()" function
6829 *
6830 * First column is 1 to be consistent with virtcol().
6831 */
6832 static void
6833f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6834{
6835 rettv->vval.v_number = screen_screencol() + 1;
6836}
6837
6838/*
6839 * "screenrow()" function
6840 */
6841 static void
6842f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6843{
6844 rettv->vval.v_number = screen_screenrow() + 1;
6845}
6846
6847/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006848 * "screenstring()" function
6849 */
6850 static void
6851f_screenstring(typval_T *argvars, typval_T *rettv)
6852{
6853 int row;
6854 int col;
6855 int off;
6856 int c;
6857 int i;
6858 char_u buf[MB_MAXBYTES + 1];
6859 int buflen = 0;
6860
6861 rettv->vval.v_string = NULL;
6862 rettv->v_type = VAR_STRING;
6863
6864 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6865 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6866 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6867 return;
6868
6869 off = LineOffset[row] + col;
6870 if (enc_utf8 && ScreenLinesUC[off] != 0)
6871 c = ScreenLinesUC[off];
6872 else
6873 c = ScreenLines[off];
6874 buflen += mb_char2bytes(c, buf);
6875
6876 if (enc_utf8)
6877 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6878 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6879
6880 buf[buflen] = NUL;
6881 rettv->vval.v_string = vim_strsave(buf);
6882}
6883
6884/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006885 * "search()" function
6886 */
6887 static void
6888f_search(typval_T *argvars, typval_T *rettv)
6889{
6890 int flags = 0;
6891
6892 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6893}
6894
6895/*
6896 * "searchdecl()" function
6897 */
6898 static void
6899f_searchdecl(typval_T *argvars, typval_T *rettv)
6900{
Bram Moolenaar30788d32020-09-05 21:35:16 +02006901 int locally = TRUE;
6902 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 int error = FALSE;
6904 char_u *name;
6905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006906 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 if (argvars[1].v_type != VAR_UNKNOWN)
6910 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02006911 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02006913 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 }
6915 if (!error && name != NULL)
6916 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6917 locally, thisblock, SEARCH_KEEP) == FAIL;
6918}
6919
6920/*
6921 * Used by searchpair() and searchpairpos()
6922 */
6923 static int
6924searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6925{
6926 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006927 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 int save_p_ws = p_ws;
6929 int dir;
6930 int flags = 0;
6931 char_u nbuf1[NUMBUFLEN];
6932 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006933 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 long lnum_stop = 0;
6935 long time_limit = 0;
6936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006937 // Get the three pattern arguments: start, middle, end. Will result in an
6938 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 spat = tv_get_string_chk(&argvars[0]);
6940 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6941 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006943 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 // Handle the optional fourth argument: flags
6946 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 if (dir == 0)
6948 goto theend;
6949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006950 // Don't accept SP_END or SP_SUBPAT.
6951 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 if ((flags & (SP_END | SP_SUBPAT)) != 0
6953 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 goto theend;
6957 }
6958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006959 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006960 if (flags & SP_REPEAT)
6961 p_ws = FALSE;
6962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006963 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006964 if (argvars[3].v_type == VAR_UNKNOWN
6965 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006966 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 else
6968 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006969 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006970 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 if (argvars[5].v_type != VAR_UNKNOWN)
6973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006974 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006975 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006977 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980#ifdef FEAT_RELTIME
6981 if (argvars[6].v_type != VAR_UNKNOWN)
6982 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006983 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006986 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
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 }
6990#endif
6991 }
6992 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993
6994 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6995 match_pos, lnum_stop, time_limit);
6996
6997theend:
6998 p_ws = save_p_ws;
6999
7000 return retval;
7001}
7002
7003/*
7004 * "searchpair()" function
7005 */
7006 static void
7007f_searchpair(typval_T *argvars, typval_T *rettv)
7008{
7009 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7010}
7011
7012/*
7013 * "searchpairpos()" function
7014 */
7015 static void
7016f_searchpairpos(typval_T *argvars, typval_T *rettv)
7017{
7018 pos_T match_pos;
7019 int lnum = 0;
7020 int col = 0;
7021
7022 if (rettv_list_alloc(rettv) == FAIL)
7023 return;
7024
7025 if (searchpair_cmn(argvars, &match_pos) > 0)
7026 {
7027 lnum = match_pos.lnum;
7028 col = match_pos.col;
7029 }
7030
7031 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7032 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7033}
7034
7035/*
7036 * Search for a start/middle/end thing.
7037 * Used by searchpair(), see its documentation for the details.
7038 * Returns 0 or -1 for no match,
7039 */
7040 long
7041do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007042 char_u *spat, // start pattern
7043 char_u *mpat, // middle pattern
7044 char_u *epat, // end pattern
7045 int dir, // BACKWARD or FORWARD
7046 typval_T *skip, // skip expression
7047 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007049 linenr_T lnum_stop, // stop at this line if not zero
7050 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051{
7052 char_u *save_cpo;
7053 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7054 long retval = 0;
7055 pos_T pos;
7056 pos_T firstpos;
7057 pos_T foundpos;
7058 pos_T save_cursor;
7059 pos_T save_pos;
7060 int n;
7061 int r;
7062 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007063 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 int err;
7065 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007066#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007068#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007070 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 save_cpo = p_cpo;
7072 p_cpo = empty_option;
7073
7074#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007075 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 profile_setlimit(time_limit, &tm);
7077#endif
7078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // Make two search patterns: start/end (pat2, for in nested pairs) and
7080 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007081 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7082 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 if (pat2 == NULL || pat3 == NULL)
7084 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007085 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086 if (*mpat == NUL)
7087 STRCPY(pat3, pat2);
7088 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007089 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 spat, epat, mpat);
7091 if (flags & SP_START)
7092 options |= SEARCH_START;
7093
Bram Moolenaar48570482017-10-30 21:48:41 +01007094 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007095 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 save_cursor = curwin->w_cursor;
7098 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007099 CLEAR_POS(&firstpos);
7100 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 pat = pat3;
7102 for (;;)
7103 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007104 searchit_arg_T sia;
7105
Bram Moolenaara80faa82020-04-12 19:37:17 +02007106 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007107 sia.sa_stop_lnum = lnum_stop;
7108#ifdef FEAT_RELTIME
7109 sia.sa_tm = &tm;
7110#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007111 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007112 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007113 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 break;
7116
7117 if (firstpos.lnum == 0)
7118 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007119 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007121 // Found the same position again. Can happen with a pattern that
7122 // has "\zs" at the end and searching backwards. Advance one
7123 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 if (dir == BACKWARD)
7125 decl(&pos);
7126 else
7127 incl(&pos);
7128 }
7129 foundpos = pos;
7130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007131 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 options &= ~SEARCH_START;
7133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007134 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007135 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 {
7137 save_pos = curwin->w_cursor;
7138 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007139 err = FALSE;
7140 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007141 curwin->w_cursor = save_pos;
7142 if (err)
7143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007144 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 curwin->w_cursor = save_cursor;
7146 retval = -1;
7147 break;
7148 }
7149 if (r)
7150 continue;
7151 }
7152
7153 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007155 // Found end when searching backwards or start when searching
7156 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007158 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 }
7160 else
7161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007162 // Found end when searching forward or start when searching
7163 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007165 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 }
7167
7168 if (nest == 0)
7169 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007170 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 if (flags & SP_RETCOUNT)
7172 ++retval;
7173 else
7174 retval = pos.lnum;
7175 if (flags & SP_SETPCMARK)
7176 setpcmark();
7177 curwin->w_cursor = pos;
7178 if (!(flags & SP_REPEAT))
7179 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007180 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181 }
7182 }
7183
7184 if (match_pos != NULL)
7185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007186 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 match_pos->lnum = curwin->w_cursor.lnum;
7188 match_pos->col = curwin->w_cursor.col + 1;
7189 }
7190
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007191 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 if ((flags & SP_NOMOVE) || retval == 0)
7193 curwin->w_cursor = save_cursor;
7194
7195theend:
7196 vim_free(pat2);
7197 vim_free(pat3);
7198 if (p_cpo == empty_option)
7199 p_cpo = save_cpo;
7200 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007201 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202 free_string_option(save_cpo);
7203
7204 return retval;
7205}
7206
7207/*
7208 * "searchpos()" function
7209 */
7210 static void
7211f_searchpos(typval_T *argvars, typval_T *rettv)
7212{
7213 pos_T match_pos;
7214 int lnum = 0;
7215 int col = 0;
7216 int n;
7217 int flags = 0;
7218
7219 if (rettv_list_alloc(rettv) == FAIL)
7220 return;
7221
7222 n = search_cmn(argvars, &match_pos, &flags);
7223 if (n > 0)
7224 {
7225 lnum = match_pos.lnum;
7226 col = match_pos.col;
7227 }
7228
7229 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7230 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7231 if (flags & SP_SUBPAT)
7232 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7233}
7234
7235 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7237{
7238 dict_T *d;
7239 dictitem_T *di;
7240 char_u *csearch;
7241
7242 if (argvars[0].v_type != VAR_DICT)
7243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007244 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 return;
7246 }
7247
7248 if ((d = argvars[0].vval.v_dict) != NULL)
7249 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007250 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 if (csearch != NULL)
7252 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 if (enc_utf8)
7254 {
7255 int pcc[MAX_MCO];
7256 int c = utfc_ptr2char(csearch, pcc);
7257
7258 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7259 }
7260 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007262 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263 }
7264
7265 di = dict_find(d, (char_u *)"forward", -1);
7266 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007267 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007268 ? FORWARD : BACKWARD);
7269
7270 di = dict_find(d, (char_u *)"until", -1);
7271 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007272 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 }
7274}
7275
7276/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007277 * "setenv()" function
7278 */
7279 static void
7280f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7281{
7282 char_u namebuf[NUMBUFLEN];
7283 char_u valbuf[NUMBUFLEN];
7284 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7285
7286 if (argvars[1].v_type == VAR_SPECIAL
7287 && argvars[1].vval.v_number == VVAL_NULL)
7288 vim_unsetenv(name);
7289 else
7290 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7291}
7292
7293/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 * "setfperm({fname}, {mode})" function
7295 */
7296 static void
7297f_setfperm(typval_T *argvars, typval_T *rettv)
7298{
7299 char_u *fname;
7300 char_u modebuf[NUMBUFLEN];
7301 char_u *mode_str;
7302 int i;
7303 int mask;
7304 int mode = 0;
7305
7306 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007307 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007308 if (fname == NULL)
7309 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007310 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007311 if (mode_str == NULL)
7312 return;
7313 if (STRLEN(mode_str) != 9)
7314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007315 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007316 return;
7317 }
7318
7319 mask = 1;
7320 for (i = 8; i >= 0; --i)
7321 {
7322 if (mode_str[i] != '-')
7323 mode |= mask;
7324 mask = mask << 1;
7325 }
7326 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7327}
7328
7329/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 * "setpos()" function
7331 */
7332 static void
7333f_setpos(typval_T *argvars, typval_T *rettv)
7334{
7335 pos_T pos;
7336 int fnum;
7337 char_u *name;
7338 colnr_T curswant = -1;
7339
7340 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007341 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342 if (name != NULL)
7343 {
7344 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7345 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007346 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347 pos.col = 0;
7348 if (name[0] == '.' && name[1] == NUL)
7349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007350 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007351 curwin->w_cursor = pos;
7352 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007354 curwin->w_curswant = curswant - 1;
7355 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007356 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007357 check_cursor();
7358 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 }
7360 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7361 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007362 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 if (setmark_pos(name[1], &pos, fnum) == OK)
7364 rettv->vval.v_number = 0;
7365 }
7366 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007367 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 }
7369 }
7370}
7371
7372/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007373 * Translate a register type string to the yank type and block length
7374 */
7375 static int
7376get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7377{
7378 char_u *stropt = *pp;
7379 switch (*stropt)
7380 {
7381 case 'v': case 'c': // character-wise selection
7382 *yank_type = MCHAR;
7383 break;
7384 case 'V': case 'l': // line-wise selection
7385 *yank_type = MLINE;
7386 break;
7387 case 'b': case Ctrl_V: // block-wise selection
7388 *yank_type = MBLOCK;
7389 if (VIM_ISDIGIT(stropt[1]))
7390 {
7391 ++stropt;
7392 *block_len = getdigits(&stropt) - 1;
7393 --stropt;
7394 }
7395 break;
7396 default:
7397 return FAIL;
7398 }
7399 *pp = stropt;
7400 return OK;
7401}
7402
7403/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404 * "setreg()" function
7405 */
7406 static void
7407f_setreg(typval_T *argvars, typval_T *rettv)
7408{
7409 int regname;
7410 char_u *strregname;
7411 char_u *stropt;
7412 char_u *strval;
7413 int append;
7414 char_u yank_type;
7415 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007416 typval_T *regcontents;
7417 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007419 pointreg = 0;
7420 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 block_len = -1;
7422 yank_type = MAUTO;
7423 append = FALSE;
7424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007425 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007426 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427
7428 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007429 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 regname = *strregname;
7431 if (regname == 0 || regname == '@')
7432 regname = '"';
7433
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007434 if (argvars[1].v_type == VAR_DICT)
7435 {
7436 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007437 dictitem_T *di;
7438
7439 if (d == NULL || d->dv_hashtab.ht_used == 0)
7440 {
7441 // Empty dict, clear the register (like setreg(0, []))
7442 char_u *lstval[2] = {NULL, NULL};
7443 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7444 return;
7445 }
7446
7447 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007448 if (di != NULL)
7449 regcontents = &di->di_tv;
7450
7451 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7452 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007453 {
7454 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7455
7456 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007457 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007458 semsg(_(e_invargval), "value");
7459 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007460 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007461 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007462
7463 if (regname == '"')
7464 {
7465 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7466 if (stropt != NULL)
7467 {
7468 pointreg = *stropt;
7469 regname = pointreg;
7470 }
7471 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007472 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007473 pointreg = regname;
7474 }
7475 else
7476 regcontents = &argvars[1];
7477
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 if (argvars[2].v_type != VAR_UNKNOWN)
7479 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007480 if (yank_type != MAUTO)
7481 {
7482 semsg(_(e_toomanyarg), "setreg");
7483 return;
7484 }
7485
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007486 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007488 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 for (; *stropt != NUL; ++stropt)
7490 switch (*stropt)
7491 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007492 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493 append = TRUE;
7494 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007495 default:
7496 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497 }
7498 }
7499
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007500 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501 {
7502 char_u **lstval;
7503 char_u **allocval;
7504 char_u buf[NUMBUFLEN];
7505 char_u **curval;
7506 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007507 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 listitem_T *li;
7509 int len;
7510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007511 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 len = ll == NULL ? 0 : ll->lv_len;
7513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007514 // First half: use for pointers to result lines; second half: use for
7515 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007516 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 if (lstval == NULL)
7518 return;
7519 curval = lstval;
7520 allocval = lstval + len + 2;
7521 curallocval = allocval;
7522
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007523 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007525 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007526 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007528 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529 if (strval == NULL)
7530 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007531 if (strval == buf)
7532 {
7533 // Need to make a copy, next tv_get_string_buf_chk() will
7534 // overwrite the string.
7535 strval = vim_strsave(buf);
7536 if (strval == NULL)
7537 goto free_lstval;
7538 *curallocval++ = strval;
7539 }
7540 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542 }
7543 *curval++ = NULL;
7544
7545 write_reg_contents_lst(regname, lstval, -1,
7546 append, yank_type, block_len);
7547free_lstval:
7548 while (curallocval > allocval)
7549 vim_free(*--curallocval);
7550 vim_free(lstval);
7551 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007552 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007554 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555 if (strval == NULL)
7556 return;
7557 write_reg_contents_ex(regname, strval, -1,
7558 append, yank_type, block_len);
7559 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007560 if (pointreg != 0)
7561 get_yank_register(pointreg, TRUE);
7562
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563 rettv->vval.v_number = 0;
7564}
7565
7566/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007567 * "settagstack()" function
7568 */
7569 static void
7570f_settagstack(typval_T *argvars, typval_T *rettv)
7571{
7572 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7573 win_T *wp;
7574 dict_T *d;
7575 int action = 'r';
7576
7577 rettv->vval.v_number = -1;
7578
7579 // first argument: window number or id
7580 wp = find_win_by_nr_or_id(&argvars[0]);
7581 if (wp == NULL)
7582 return;
7583
7584 // second argument: dict with items to set in the tag stack
7585 if (argvars[1].v_type != VAR_DICT)
7586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007588 return;
7589 }
7590 d = argvars[1].vval.v_dict;
7591 if (d == NULL)
7592 return;
7593
7594 // third argument: action - 'a' for append and 'r' for replace.
7595 // default is to replace the stack.
7596 if (argvars[2].v_type == VAR_UNKNOWN)
7597 action = 'r';
7598 else if (argvars[2].v_type == VAR_STRING)
7599 {
7600 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007601 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007602 if (actstr == NULL)
7603 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007604 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7605 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007606 action = *actstr;
7607 else
7608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007610 return;
7611 }
7612 }
7613 else
7614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007616 return;
7617 }
7618
7619 if (set_tagstack(wp, d, action) == OK)
7620 rettv->vval.v_number = 0;
7621}
7622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623#ifdef FEAT_CRYPT
7624/*
7625 * "sha256({string})" function
7626 */
7627 static void
7628f_sha256(typval_T *argvars, typval_T *rettv)
7629{
7630 char_u *p;
7631
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007632 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633 rettv->vval.v_string = vim_strsave(
7634 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7635 rettv->v_type = VAR_STRING;
7636}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007637#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638
7639/*
7640 * "shellescape({string})" function
7641 */
7642 static void
7643f_shellescape(typval_T *argvars, typval_T *rettv)
7644{
Bram Moolenaar20615522017-06-05 18:46:26 +02007645 int do_special = non_zero_arg(&argvars[1]);
7646
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007648 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 rettv->v_type = VAR_STRING;
7650}
7651
7652/*
7653 * shiftwidth() function
7654 */
7655 static void
7656f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7657{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007658 rettv->vval.v_number = 0;
7659
7660 if (argvars[0].v_type != VAR_UNKNOWN)
7661 {
7662 long col;
7663
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007665 if (col < 0)
7666 return; // type error; errmsg already given
7667#ifdef FEAT_VARTABS
7668 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7669 return;
7670#endif
7671 }
7672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 rettv->vval.v_number = get_sw_value(curbuf);
7674}
7675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676#ifdef FEAT_FLOAT
7677/*
7678 * "sin()" function
7679 */
7680 static void
7681f_sin(typval_T *argvars, typval_T *rettv)
7682{
7683 float_T f = 0.0;
7684
7685 rettv->v_type = VAR_FLOAT;
7686 if (get_float_arg(argvars, &f) == OK)
7687 rettv->vval.v_float = sin(f);
7688 else
7689 rettv->vval.v_float = 0.0;
7690}
7691
7692/*
7693 * "sinh()" function
7694 */
7695 static void
7696f_sinh(typval_T *argvars, typval_T *rettv)
7697{
7698 float_T f = 0.0;
7699
7700 rettv->v_type = VAR_FLOAT;
7701 if (get_float_arg(argvars, &f) == OK)
7702 rettv->vval.v_float = sinh(f);
7703 else
7704 rettv->vval.v_float = 0.0;
7705}
7706#endif
7707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708/*
7709 * "soundfold({word})" function
7710 */
7711 static void
7712f_soundfold(typval_T *argvars, typval_T *rettv)
7713{
7714 char_u *s;
7715
7716 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007717 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718#ifdef FEAT_SPELL
7719 rettv->vval.v_string = eval_soundfold(s);
7720#else
7721 rettv->vval.v_string = vim_strsave(s);
7722#endif
7723}
7724
7725/*
7726 * "spellbadword()" function
7727 */
7728 static void
7729f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7730{
7731 char_u *word = (char_u *)"";
7732 hlf_T attr = HLF_COUNT;
7733 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007734#ifdef FEAT_SPELL
7735 int wo_spell_save = curwin->w_p_spell;
7736
7737 if (!curwin->w_p_spell)
7738 {
7739 did_set_spelllang(curwin);
7740 curwin->w_p_spell = TRUE;
7741 }
7742
7743 if (*curwin->w_s->b_p_spl == NUL)
7744 {
7745 emsg(_(e_no_spell));
7746 curwin->w_p_spell = wo_spell_save;
7747 return;
7748 }
7749#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750
7751 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007752 {
7753#ifdef FEAT_SPELL
7754 curwin->w_p_spell = wo_spell_save;
7755#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758
7759#ifdef FEAT_SPELL
7760 if (argvars[0].v_type == VAR_UNKNOWN)
7761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007762 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7764 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007765 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007767 curwin->w_set_curswant = TRUE;
7768 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007770 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007772 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773 int capcol = -1;
7774
7775 if (str != NULL)
7776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007777 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 while (*str != NUL)
7779 {
7780 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7781 if (attr != HLF_COUNT)
7782 {
7783 word = str;
7784 break;
7785 }
7786 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007787 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007788 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 }
7790 }
7791 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007792 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793#endif
7794
7795 list_append_string(rettv->vval.v_list, word, len);
7796 list_append_string(rettv->vval.v_list, (char_u *)(
7797 attr == HLF_SPB ? "bad" :
7798 attr == HLF_SPR ? "rare" :
7799 attr == HLF_SPL ? "local" :
7800 attr == HLF_SPC ? "caps" :
7801 ""), -1);
7802}
7803
7804/*
7805 * "spellsuggest()" function
7806 */
7807 static void
7808f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7809{
7810#ifdef FEAT_SPELL
7811 char_u *str;
7812 int typeerr = FALSE;
7813 int maxcount;
7814 garray_T ga;
7815 int i;
7816 listitem_T *li;
7817 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007818 int wo_spell_save = curwin->w_p_spell;
7819
7820 if (!curwin->w_p_spell)
7821 {
7822 did_set_spelllang(curwin);
7823 curwin->w_p_spell = TRUE;
7824 }
7825
7826 if (*curwin->w_s->b_p_spl == NUL)
7827 {
7828 emsg(_(e_no_spell));
7829 curwin->w_p_spell = wo_spell_save;
7830 return;
7831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832#endif
7833
7834 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007835 {
7836#ifdef FEAT_SPELL
7837 curwin->w_p_spell = wo_spell_save;
7838#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007840 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841
7842#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007843 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007845 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 if (argvars[1].v_type != VAR_UNKNOWN)
7847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007848 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 if (maxcount <= 0)
7850 return;
7851 if (argvars[2].v_type != VAR_UNKNOWN)
7852 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02007853 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854 if (typeerr)
7855 return;
7856 }
7857 }
7858 else
7859 maxcount = 25;
7860
7861 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7862
7863 for (i = 0; i < ga.ga_len; ++i)
7864 {
7865 str = ((char_u **)ga.ga_data)[i];
7866
7867 li = listitem_alloc();
7868 if (li == NULL)
7869 vim_free(str);
7870 else
7871 {
7872 li->li_tv.v_type = VAR_STRING;
7873 li->li_tv.v_lock = 0;
7874 li->li_tv.vval.v_string = str;
7875 list_append(rettv->vval.v_list, li);
7876 }
7877 }
7878 ga_clear(&ga);
7879 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007880 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881#endif
7882}
7883
7884 static void
7885f_split(typval_T *argvars, typval_T *rettv)
7886{
7887 char_u *str;
7888 char_u *end;
7889 char_u *pat = NULL;
7890 regmatch_T regmatch;
7891 char_u patbuf[NUMBUFLEN];
7892 char_u *save_cpo;
7893 int match;
7894 colnr_T col = 0;
7895 int keepempty = FALSE;
7896 int typeerr = FALSE;
7897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007898 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 save_cpo = p_cpo;
7900 p_cpo = (char_u *)"";
7901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007902 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 if (argvars[1].v_type != VAR_UNKNOWN)
7904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 if (pat == NULL)
7907 typeerr = TRUE;
7908 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02007909 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 }
7911 if (pat == NULL || *pat == NUL)
7912 pat = (char_u *)"[\\x01- ]\\+";
7913
7914 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007915 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007917 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918
7919 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7920 if (regmatch.regprog != NULL)
7921 {
7922 regmatch.rm_ic = FALSE;
7923 while (*str != NUL || keepempty)
7924 {
7925 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007926 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 else
7928 match = vim_regexec_nl(&regmatch, str, col);
7929 if (match)
7930 end = regmatch.startp[0];
7931 else
7932 end = str + STRLEN(str);
7933 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7934 && *str != NUL && match && end < regmatch.endp[0]))
7935 {
7936 if (list_append_string(rettv->vval.v_list, str,
7937 (int)(end - str)) == FAIL)
7938 break;
7939 }
7940 if (!match)
7941 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007942 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943 if (regmatch.endp[0] > str)
7944 col = 0;
7945 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007946 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 str = regmatch.endp[0];
7949 }
7950
7951 vim_regfree(regmatch.regprog);
7952 }
7953
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007954theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 p_cpo = save_cpo;
7956}
7957
7958#ifdef FEAT_FLOAT
7959/*
7960 * "sqrt()" function
7961 */
7962 static void
7963f_sqrt(typval_T *argvars, typval_T *rettv)
7964{
7965 float_T f = 0.0;
7966
7967 rettv->v_type = VAR_FLOAT;
7968 if (get_float_arg(argvars, &f) == OK)
7969 rettv->vval.v_float = sqrt(f);
7970 else
7971 rettv->vval.v_float = 0.0;
7972}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007975#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007976/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 * "str2float()" function
7978 */
7979 static void
7980f_str2float(typval_T *argvars, typval_T *rettv)
7981{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007982 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007983 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984
Bram Moolenaar08243d22017-01-10 16:12:29 +01007985 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007986 p = skipwhite(p + 1);
7987 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007988 if (isneg)
7989 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 rettv->v_type = VAR_FLOAT;
7991}
7992#endif
7993
7994/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007995 * "str2list()" function
7996 */
7997 static void
7998f_str2list(typval_T *argvars, typval_T *rettv)
7999{
8000 char_u *p;
8001 int utf8 = FALSE;
8002
8003 if (rettv_list_alloc(rettv) == FAIL)
8004 return;
8005
8006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008007 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008008
8009 p = tv_get_string(&argvars[0]);
8010
8011 if (has_mbyte || utf8)
8012 {
8013 int (*ptr2len)(char_u *);
8014 int (*ptr2char)(char_u *);
8015
8016 if (utf8 || enc_utf8)
8017 {
8018 ptr2len = utf_ptr2len;
8019 ptr2char = utf_ptr2char;
8020 }
8021 else
8022 {
8023 ptr2len = mb_ptr2len;
8024 ptr2char = mb_ptr2char;
8025 }
8026
8027 for ( ; *p != NUL; p += (*ptr2len)(p))
8028 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8029 }
8030 else
8031 for ( ; *p != NUL; ++p)
8032 list_append_number(rettv->vval.v_list, *p);
8033}
8034
8035/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 * "str2nr()" function
8037 */
8038 static void
8039f_str2nr(typval_T *argvars, typval_T *rettv)
8040{
8041 int base = 10;
8042 char_u *p;
8043 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008044 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008045 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046
8047 if (argvars[1].v_type != VAR_UNKNOWN)
8048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008049 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 if (base != 2 && base != 8 && base != 10 && base != 16)
8051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008052 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 return;
8054 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008055 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008056 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 }
8058
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008059 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008060 isneg = (*p == '-');
8061 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 p = skipwhite(p + 1);
8063 switch (base)
8064 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008065 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008066 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008067 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008069 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8070 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008071 if (isneg)
8072 rettv->vval.v_number = -n;
8073 else
8074 rettv->vval.v_number = n;
8075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076}
8077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078/*
8079 * "strgetchar()" function
8080 */
8081 static void
8082f_strgetchar(typval_T *argvars, typval_T *rettv)
8083{
8084 char_u *str;
8085 int len;
8086 int error = FALSE;
8087 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008088 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089
8090 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008091 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 if (str == NULL)
8093 return;
8094 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008095 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 if (error)
8097 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098
Bram Moolenaar13505972019-01-24 15:04:48 +01008099 while (charidx >= 0 && byteidx < len)
8100 {
8101 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008103 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8104 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008106 --charidx;
8107 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109}
8110
8111/*
8112 * "stridx()" function
8113 */
8114 static void
8115f_stridx(typval_T *argvars, typval_T *rettv)
8116{
8117 char_u buf[NUMBUFLEN];
8118 char_u *needle;
8119 char_u *haystack;
8120 char_u *save_haystack;
8121 char_u *pos;
8122 int start_idx;
8123
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008124 needle = tv_get_string_chk(&argvars[1]);
8125 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 rettv->vval.v_number = -1;
8127 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008128 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129
8130 if (argvars[2].v_type != VAR_UNKNOWN)
8131 {
8132 int error = FALSE;
8133
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008134 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 if (error || start_idx >= (int)STRLEN(haystack))
8136 return;
8137 if (start_idx >= 0)
8138 haystack += start_idx;
8139 }
8140
8141 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8142 if (pos != NULL)
8143 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8144}
8145
8146/*
8147 * "string()" function
8148 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008149 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150f_string(typval_T *argvars, typval_T *rettv)
8151{
8152 char_u *tofree;
8153 char_u numbuf[NUMBUFLEN];
8154
8155 rettv->v_type = VAR_STRING;
8156 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8157 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008158 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159 if (rettv->vval.v_string != NULL && tofree == NULL)
8160 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8161}
8162
8163/*
8164 * "strlen()" function
8165 */
8166 static void
8167f_strlen(typval_T *argvars, typval_T *rettv)
8168{
8169 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008170 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171}
8172
8173/*
8174 * "strchars()" function
8175 */
8176 static void
8177f_strchars(typval_T *argvars, typval_T *rettv)
8178{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008179 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02008180 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 varnumber_T len = 0;
8182 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183
8184 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008185 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008187 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 else
8189 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8191 while (*s != NUL)
8192 {
8193 func_mb_ptr2char_adv(&s);
8194 ++len;
8195 }
8196 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 }
8198}
8199
8200/*
8201 * "strdisplaywidth()" function
8202 */
8203 static void
8204f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8205{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008206 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 int col = 0;
8208
8209 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008210 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211
8212 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8213}
8214
8215/*
8216 * "strwidth()" function
8217 */
8218 static void
8219f_strwidth(typval_T *argvars, typval_T *rettv)
8220{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008221 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222
Bram Moolenaar13505972019-01-24 15:04:48 +01008223 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008224}
8225
8226/*
8227 * "strcharpart()" function
8228 */
8229 static void
8230f_strcharpart(typval_T *argvars, typval_T *rettv)
8231{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008232 char_u *p;
8233 int nchar;
8234 int nbyte = 0;
8235 int charlen;
8236 int len = 0;
8237 int slen;
8238 int error = FALSE;
8239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008240 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 slen = (int)STRLEN(p);
8242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008243 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 if (!error)
8245 {
8246 if (nchar > 0)
8247 while (nchar > 0 && nbyte < slen)
8248 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008249 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 --nchar;
8251 }
8252 else
8253 nbyte = nchar;
8254 if (argvars[2].v_type != VAR_UNKNOWN)
8255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 while (charlen > 0 && nbyte + len < slen)
8258 {
8259 int off = nbyte + len;
8260
8261 if (off < 0)
8262 len += 1;
8263 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008264 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 --charlen;
8266 }
8267 }
8268 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008269 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 }
8271
8272 /*
8273 * Only return the overlap between the specified part and the actual
8274 * string.
8275 */
8276 if (nbyte < 0)
8277 {
8278 len += nbyte;
8279 nbyte = 0;
8280 }
8281 else if (nbyte > slen)
8282 nbyte = slen;
8283 if (len < 0)
8284 len = 0;
8285 else if (nbyte + len > slen)
8286 len = slen - nbyte;
8287
8288 rettv->v_type = VAR_STRING;
8289 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290}
8291
8292/*
8293 * "strpart()" function
8294 */
8295 static void
8296f_strpart(typval_T *argvars, typval_T *rettv)
8297{
8298 char_u *p;
8299 int n;
8300 int len;
8301 int slen;
8302 int error = FALSE;
8303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 slen = (int)STRLEN(p);
8306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 if (error)
8309 len = 0;
8310 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008311 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008313 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008315 // Only return the overlap between the specified part and the actual
8316 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 if (n < 0)
8318 {
8319 len += n;
8320 n = 0;
8321 }
8322 else if (n > slen)
8323 n = slen;
8324 if (len < 0)
8325 len = 0;
8326 else if (n + len > slen)
8327 len = slen - n;
8328
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008329 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8330 {
8331 int off;
8332
8333 // length in characters
8334 for (off = n; off < slen && len > 0; --len)
8335 off += mb_ptr2len(p + off);
8336 len = off - n;
8337 }
8338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 rettv->v_type = VAR_STRING;
8340 rettv->vval.v_string = vim_strnsave(p + n, len);
8341}
8342
8343/*
8344 * "strridx()" function
8345 */
8346 static void
8347f_strridx(typval_T *argvars, typval_T *rettv)
8348{
8349 char_u buf[NUMBUFLEN];
8350 char_u *needle;
8351 char_u *haystack;
8352 char_u *rest;
8353 char_u *lastmatch = NULL;
8354 int haystack_len, end_idx;
8355
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008356 needle = tv_get_string_chk(&argvars[1]);
8357 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358
8359 rettv->vval.v_number = -1;
8360 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008361 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362
8363 haystack_len = (int)STRLEN(haystack);
8364 if (argvars[2].v_type != VAR_UNKNOWN)
8365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008366 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008367 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008369 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 }
8371 else
8372 end_idx = haystack_len;
8373
8374 if (*needle == NUL)
8375 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008376 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 lastmatch = haystack + end_idx;
8378 }
8379 else
8380 {
8381 for (rest = haystack; *rest != '\0'; ++rest)
8382 {
8383 rest = (char_u *)strstr((char *)rest, (char *)needle);
8384 if (rest == NULL || rest > haystack + end_idx)
8385 break;
8386 lastmatch = rest;
8387 }
8388 }
8389
8390 if (lastmatch == NULL)
8391 rettv->vval.v_number = -1;
8392 else
8393 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8394}
8395
8396/*
8397 * "strtrans()" function
8398 */
8399 static void
8400f_strtrans(typval_T *argvars, typval_T *rettv)
8401{
8402 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
8406/*
8407 * "submatch()" function
8408 */
8409 static void
8410f_submatch(typval_T *argvars, typval_T *rettv)
8411{
8412 int error = FALSE;
8413 int no;
8414 int retList = 0;
8415
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 if (error)
8418 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008419 if (no < 0 || no >= NSUBEXP)
8420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008421 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008422 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008423 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02008425 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 if (error)
8427 return;
8428
8429 if (retList == 0)
8430 {
8431 rettv->v_type = VAR_STRING;
8432 rettv->vval.v_string = reg_submatch(no);
8433 }
8434 else
8435 {
8436 rettv->v_type = VAR_LIST;
8437 rettv->vval.v_list = reg_submatch_list(no);
8438 }
8439}
8440
8441/*
8442 * "substitute()" function
8443 */
8444 static void
8445f_substitute(typval_T *argvars, typval_T *rettv)
8446{
8447 char_u patbuf[NUMBUFLEN];
8448 char_u subbuf[NUMBUFLEN];
8449 char_u flagsbuf[NUMBUFLEN];
8450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008451 char_u *str = tv_get_string_chk(&argvars[0]);
8452 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008453 char_u *sub = NULL;
8454 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008455 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008457 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8458 expr = &argvars[2];
8459 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008460 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008463 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8464 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 rettv->vval.v_string = NULL;
8466 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008467 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468}
8469
8470/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008471 * "swapinfo(swap_filename)" function
8472 */
8473 static void
8474f_swapinfo(typval_T *argvars, typval_T *rettv)
8475{
8476 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008477 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008478}
8479
8480/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008481 * "swapname(expr)" function
8482 */
8483 static void
8484f_swapname(typval_T *argvars, typval_T *rettv)
8485{
8486 buf_T *buf;
8487
8488 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008489 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008490 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8491 || buf->b_ml.ml_mfp->mf_fname == NULL)
8492 rettv->vval.v_string = NULL;
8493 else
8494 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8495}
8496
8497/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 * "synID(lnum, col, trans)" function
8499 */
8500 static void
8501f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8502{
8503 int id = 0;
8504#ifdef FEAT_SYN_HL
8505 linenr_T lnum;
8506 colnr_T col;
8507 int trans;
8508 int transerr = FALSE;
8509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008510 lnum = tv_get_lnum(argvars); // -1 on type error
8511 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02008512 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513
8514 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8515 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8516 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8517#endif
8518
8519 rettv->vval.v_number = id;
8520}
8521
8522/*
8523 * "synIDattr(id, what [, mode])" function
8524 */
8525 static void
8526f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8527{
8528 char_u *p = NULL;
8529#ifdef FEAT_SYN_HL
8530 int id;
8531 char_u *what;
8532 char_u *mode;
8533 char_u modebuf[NUMBUFLEN];
8534 int modec;
8535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008536 id = (int)tv_get_number(&argvars[0]);
8537 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008540 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 modec = TOLOWER_ASC(mode[0]);
8542 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008543 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 }
8545 else
8546 {
8547#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8548 if (USE_24BIT)
8549 modec = 'g';
8550 else
8551#endif
8552 if (t_colors > 1)
8553 modec = 'c';
8554 else
8555 modec = 't';
8556 }
8557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 switch (TOLOWER_ASC(what[0]))
8559 {
8560 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008561 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008563 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 p = highlight_has_attr(id, HL_BOLD, modec);
8565 break;
8566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008567 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 p = highlight_color(id, what, modec);
8569 break;
8570
8571 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008572 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008574 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 p = highlight_has_attr(id, HL_ITALIC, modec);
8576 break;
8577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008578 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008579 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580 break;
8581
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008582 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 p = highlight_has_attr(id, HL_INVERSE, modec);
8584 break;
8585
8586 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008587 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008589 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008590 else if (TOLOWER_ASC(what[1]) == 't' &&
8591 TOLOWER_ASC(what[2]) == 'r')
8592 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008593 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 p = highlight_has_attr(id, HL_STANDOUT, modec);
8595 break;
8596
8597 case 'u':
8598 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008599 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8601 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008602 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8604 break;
8605 }
8606
8607 if (p != NULL)
8608 p = vim_strsave(p);
8609#endif
8610 rettv->v_type = VAR_STRING;
8611 rettv->vval.v_string = p;
8612}
8613
8614/*
8615 * "synIDtrans(id)" function
8616 */
8617 static void
8618f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8619{
8620 int id;
8621
8622#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624
8625 if (id > 0)
8626 id = syn_get_final_id(id);
8627 else
8628#endif
8629 id = 0;
8630
8631 rettv->vval.v_number = id;
8632}
8633
8634/*
8635 * "synconcealed(lnum, col)" function
8636 */
8637 static void
8638f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8639{
8640#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8641 linenr_T lnum;
8642 colnr_T col;
8643 int syntax_flags = 0;
8644 int cchar;
8645 int matchid = 0;
8646 char_u str[NUMBUFLEN];
8647#endif
8648
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008649 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650
8651#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008652 lnum = tv_get_lnum(argvars); // -1 on type error
8653 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654
Bram Moolenaara80faa82020-04-12 19:37:17 +02008655 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656
8657 if (rettv_list_alloc(rettv) != FAIL)
8658 {
8659 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8660 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8661 && curwin->w_p_cole > 0)
8662 {
8663 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8664 syntax_flags = get_syntax_info(&matchid);
8665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008666 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8668 {
8669 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008670 if (cchar == NUL && curwin->w_p_cole == 1)
8671 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 if (cchar != NUL)
8673 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 if (has_mbyte)
8675 (*mb_char2bytes)(cchar, str);
8676 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 str[0] = cchar;
8678 }
8679 }
8680 }
8681
8682 list_append_number(rettv->vval.v_list,
8683 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008684 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685 list_append_string(rettv->vval.v_list, str, -1);
8686 list_append_number(rettv->vval.v_list, matchid);
8687 }
8688#endif
8689}
8690
8691/*
8692 * "synstack(lnum, col)" function
8693 */
8694 static void
8695f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8696{
8697#ifdef FEAT_SYN_HL
8698 linenr_T lnum;
8699 colnr_T col;
8700 int i;
8701 int id;
8702#endif
8703
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008704 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705
8706#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008707 lnum = tv_get_lnum(argvars); // -1 on type error
8708 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709
8710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8711 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8712 && rettv_list_alloc(rettv) != FAIL)
8713 {
8714 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8715 for (i = 0; ; ++i)
8716 {
8717 id = syn_get_stack_item(i);
8718 if (id < 0)
8719 break;
8720 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8721 break;
8722 }
8723 }
8724#endif
8725}
8726
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727/*
8728 * "tabpagebuflist()" function
8729 */
8730 static void
8731f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8732{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 tabpage_T *tp;
8734 win_T *wp = NULL;
8735
8736 if (argvars[0].v_type == VAR_UNKNOWN)
8737 wp = firstwin;
8738 else
8739 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008740 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 if (tp != NULL)
8742 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8743 }
8744 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8745 {
8746 for (; wp != NULL; wp = wp->w_next)
8747 if (list_append_number(rettv->vval.v_list,
8748 wp->w_buffer->b_fnum) == FAIL)
8749 break;
8750 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751}
8752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 * "tagfiles()" function
8755 */
8756 static void
8757f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8758{
8759 char_u *fname;
8760 tagname_T tn;
8761 int first;
8762
8763 if (rettv_list_alloc(rettv) == FAIL)
8764 return;
8765 fname = alloc(MAXPATHL);
8766 if (fname == NULL)
8767 return;
8768
8769 for (first = TRUE; ; first = FALSE)
8770 if (get_tagfname(&tn, first, fname) == FAIL
8771 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8772 break;
8773 tagname_free(&tn);
8774 vim_free(fname);
8775}
8776
8777/*
8778 * "taglist()" function
8779 */
8780 static void
8781f_taglist(typval_T *argvars, typval_T *rettv)
8782{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008783 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 char_u *tag_pattern;
8785
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008786 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787
8788 rettv->vval.v_number = FALSE;
8789 if (*tag_pattern == NUL)
8790 return;
8791
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008792 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008793 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008795 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796}
8797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798#ifdef FEAT_FLOAT
8799/*
8800 * "tan()" function
8801 */
8802 static void
8803f_tan(typval_T *argvars, typval_T *rettv)
8804{
8805 float_T f = 0.0;
8806
8807 rettv->v_type = VAR_FLOAT;
8808 if (get_float_arg(argvars, &f) == OK)
8809 rettv->vval.v_float = tan(f);
8810 else
8811 rettv->vval.v_float = 0.0;
8812}
8813
8814/*
8815 * "tanh()" function
8816 */
8817 static void
8818f_tanh(typval_T *argvars, typval_T *rettv)
8819{
8820 float_T f = 0.0;
8821
8822 rettv->v_type = VAR_FLOAT;
8823 if (get_float_arg(argvars, &f) == OK)
8824 rettv->vval.v_float = tanh(f);
8825 else
8826 rettv->vval.v_float = 0.0;
8827}
8828#endif
8829
8830/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 * "tolower(string)" function
8832 */
8833 static void
8834f_tolower(typval_T *argvars, typval_T *rettv)
8835{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838}
8839
8840/*
8841 * "toupper(string)" function
8842 */
8843 static void
8844f_toupper(typval_T *argvars, typval_T *rettv)
8845{
8846 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008847 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848}
8849
8850/*
8851 * "tr(string, fromstr, tostr)" function
8852 */
8853 static void
8854f_tr(typval_T *argvars, typval_T *rettv)
8855{
8856 char_u *in_str;
8857 char_u *fromstr;
8858 char_u *tostr;
8859 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 int inlen;
8861 int fromlen;
8862 int tolen;
8863 int idx;
8864 char_u *cpstr;
8865 int cplen;
8866 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 char_u buf[NUMBUFLEN];
8868 char_u buf2[NUMBUFLEN];
8869 garray_T ga;
8870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008871 in_str = tv_get_string(&argvars[0]);
8872 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8873 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008875 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 rettv->v_type = VAR_STRING;
8877 rettv->vval.v_string = NULL;
8878 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008879 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 ga_init2(&ga, (int)sizeof(char), 80);
8881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008883 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 if (STRLEN(fromstr) != STRLEN(tostr))
8885 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008887 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 ga_clear(&ga);
8889 return;
8890 }
8891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008892 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 while (*in_str != NUL)
8894 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 if (has_mbyte)
8896 {
8897 inlen = (*mb_ptr2len)(in_str);
8898 cpstr = in_str;
8899 cplen = inlen;
8900 idx = 0;
8901 for (p = fromstr; *p != NUL; p += fromlen)
8902 {
8903 fromlen = (*mb_ptr2len)(p);
8904 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8905 {
8906 for (p = tostr; *p != NUL; p += tolen)
8907 {
8908 tolen = (*mb_ptr2len)(p);
8909 if (idx-- == 0)
8910 {
8911 cplen = tolen;
8912 cpstr = p;
8913 break;
8914 }
8915 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008916 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 goto error;
8918 break;
8919 }
8920 ++idx;
8921 }
8922
8923 if (first && cpstr == in_str)
8924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008925 // Check that fromstr and tostr have the same number of
8926 // (multi-byte) characters. Done only once when a character
8927 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 first = FALSE;
8929 for (p = tostr; *p != NUL; p += tolen)
8930 {
8931 tolen = (*mb_ptr2len)(p);
8932 --idx;
8933 }
8934 if (idx != 0)
8935 goto error;
8936 }
8937
8938 (void)ga_grow(&ga, cplen);
8939 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8940 ga.ga_len += cplen;
8941
8942 in_str += inlen;
8943 }
8944 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008946 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 p = vim_strchr(fromstr, *in_str);
8948 if (p != NULL)
8949 ga_append(&ga, tostr[p - fromstr]);
8950 else
8951 ga_append(&ga, *in_str);
8952 ++in_str;
8953 }
8954 }
8955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008956 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008957 (void)ga_grow(&ga, 1);
8958 ga_append(&ga, NUL);
8959
8960 rettv->vval.v_string = ga.ga_data;
8961}
8962
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008963/*
8964 * "trim({expr})" function
8965 */
8966 static void
8967f_trim(typval_T *argvars, typval_T *rettv)
8968{
8969 char_u buf1[NUMBUFLEN];
8970 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008971 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008972 char_u *mask = NULL;
8973 char_u *tail;
8974 char_u *prev;
8975 char_u *p;
8976 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008977 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008978
8979 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008980 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008981 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008982 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008983
8984 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008986 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008987
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008989 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008990 int error = 0;
8991
8992 // leading or trailing characters to trim
8993 dir = (int)tv_get_number_chk(&argvars[2], &error);
8994 if (error)
8995 return;
8996 if (dir < 0 || dir > 2)
8997 {
8998 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8999 return;
9000 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009001 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009002 }
9003
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009004 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009005 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009006 // Trim leading characters
9007 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009008 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009009 c1 = PTR2CHAR(head);
9010 if (mask == NULL)
9011 {
9012 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009013 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009014 }
9015 else
9016 {
9017 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9018 if (c1 == PTR2CHAR(p))
9019 break;
9020 if (*p == NUL)
9021 break;
9022 }
9023 MB_PTR_ADV(head);
9024 }
9025 }
9026
9027 tail = head + STRLEN(head);
9028 if (dir == 0 || dir == 2)
9029 {
9030 // Trim trailing characters
9031 for (; tail > head; tail = prev)
9032 {
9033 prev = tail;
9034 MB_PTR_BACK(head, prev);
9035 c1 = PTR2CHAR(prev);
9036 if (mask == NULL)
9037 {
9038 if (c1 > ' ' && c1 != 0xa0)
9039 break;
9040 }
9041 else
9042 {
9043 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9044 if (c1 == PTR2CHAR(p))
9045 break;
9046 if (*p == NUL)
9047 break;
9048 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009049 }
9050 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009051 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009052}
9053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054#ifdef FEAT_FLOAT
9055/*
9056 * "trunc({float})" function
9057 */
9058 static void
9059f_trunc(typval_T *argvars, typval_T *rettv)
9060{
9061 float_T f = 0.0;
9062
9063 rettv->v_type = VAR_FLOAT;
9064 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009065 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9067 else
9068 rettv->vval.v_float = 0.0;
9069}
9070#endif
9071
9072/*
9073 * "type(expr)" function
9074 */
9075 static void
9076f_type(typval_T *argvars, typval_T *rettv)
9077{
9078 int n = -1;
9079
9080 switch (argvars[0].v_type)
9081 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009082 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9083 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009085 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9086 case VAR_LIST: n = VAR_TYPE_LIST; break;
9087 case VAR_DICT: n = VAR_TYPE_DICT; break;
9088 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9089 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9090 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009091 case VAR_JOB: n = VAR_TYPE_JOB; break;
9092 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009093 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009094 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009095 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009096 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009097 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009098 n = -1;
9099 break;
9100 }
9101 rettv->vval.v_number = n;
9102}
9103
9104/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 * "virtcol(string)" function
9106 */
9107 static void
9108f_virtcol(typval_T *argvars, typval_T *rettv)
9109{
9110 colnr_T vcol = 0;
9111 pos_T *fp;
9112 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009113 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114
9115 fp = var2fpos(&argvars[0], FALSE, &fnum);
9116 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9117 && fnum == curbuf->b_fnum)
9118 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009119 // Limit the column to a valid value, getvvcol() doesn't check.
9120 if (fp->col < 0)
9121 fp->col = 0;
9122 else
9123 {
9124 len = (int)STRLEN(ml_get(fp->lnum));
9125 if (fp->col > len)
9126 fp->col = len;
9127 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 getvvcol(curwin, fp, NULL, NULL, &vcol);
9129 ++vcol;
9130 }
9131
9132 rettv->vval.v_number = vcol;
9133}
9134
9135/*
9136 * "visualmode()" function
9137 */
9138 static void
9139f_visualmode(typval_T *argvars, typval_T *rettv)
9140{
9141 char_u str[2];
9142
9143 rettv->v_type = VAR_STRING;
9144 str[0] = curbuf->b_visual_mode_eval;
9145 str[1] = NUL;
9146 rettv->vval.v_string = vim_strsave(str);
9147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009148 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 if (non_zero_arg(&argvars[0]))
9150 curbuf->b_visual_mode_eval = NUL;
9151}
9152
9153/*
9154 * "wildmenumode()" function
9155 */
9156 static void
9157f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9158{
9159#ifdef FEAT_WILDMENU
9160 if (wild_menu_showing)
9161 rettv->vval.v_number = 1;
9162#endif
9163}
9164
9165/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009166 * "windowsversion()" function
9167 */
9168 static void
9169f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9170{
9171 rettv->v_type = VAR_STRING;
9172 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9173}
9174
9175/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009176 * "wordcount()" function
9177 */
9178 static void
9179f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9180{
9181 if (rettv_dict_alloc(rettv) == FAIL)
9182 return;
9183 cursor_pos_info(rettv->vval.v_dict);
9184}
9185
9186/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 * "xor(expr, expr)" function
9188 */
9189 static void
9190f_xor(typval_T *argvars, typval_T *rettv)
9191{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009192 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9193 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009194}
9195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009196#endif // FEAT_EVAL