blob: e74a82a91f90b0f7b011998194827f9f6a38500a [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{
389 if (argtypes[0]->tt_type == VAR_LIST
390 || argtypes[0]->tt_type == VAR_DICT)
391 return argtypes[0]->tt_member;
392 if (argtypes[0]->tt_type == VAR_BLOB)
393 return &t_number;
394 return &t_any;
395}
396
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200397 static type_T *
398ret_getreg(int argcount, type_T **argtypes UNUSED)
399{
400 // Assume that if the third argument is passed it's non-zero
401 if (argcount == 3)
402 return &t_list_string;
403 return &t_string;
404}
405
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200406 static type_T *
407ret_maparg(int argcount, type_T **argtypes UNUSED)
408{
409 // Assume that if the fourth argument is passed it's non-zero
410 if (argcount == 4)
411 return &t_dict_any;
412 return &t_string;
413}
414
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100415static type_T *ret_f_function(int argcount, type_T **argtypes);
416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200417/*
418 * Array with names and number of arguments of all internal functions
419 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
420 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200421typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200423 char *f_name; // function name
424 char f_min_argc; // minimal number of arguments
425 char f_max_argc; // maximal number of arguments
426 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100427 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
428 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200430 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200431} funcentry_T;
432
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200433// values for f_argtype; zero means it cannot be used as a method
434#define FEARG_1 1 // base is the first argument
435#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200436#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200437#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200438#define FEARG_LAST 9 // base is the last argument
439
Bram Moolenaar15c47602020-03-26 22:16:48 +0100440#ifdef FEAT_FLOAT
441# define FLOAT_FUNC(name) name
442#else
443# define FLOAT_FUNC(name) NULL
444#endif
445#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
446# define MATH_FUNC(name) name
447#else
448# define MATH_FUNC(name) NULL
449#endif
450#ifdef FEAT_TIMERS
451# define TIMER_FUNC(name) name
452#else
453# define TIMER_FUNC(name) NULL
454#endif
455#ifdef FEAT_JOB_CHANNEL
456# define JOB_FUNC(name) name
457#else
458# define JOB_FUNC(name) NULL
459#endif
460#ifdef FEAT_PROP_POPUP
461# define PROP_FUNC(name) name
462#else
463# define PROP_FUNC(name) NULL
464#endif
465#ifdef FEAT_SIGNS
466# define SIGN_FUNC(name) name
467#else
468# define SIGN_FUNC(name) NULL
469#endif
470#ifdef FEAT_SOUND
471# define SOUND_FUNC(name) name
472#else
473# define SOUND_FUNC(name) NULL
474#endif
475#ifdef FEAT_TERMINAL
476# define TERM_FUNC(name) name
477#else
478# define TERM_FUNC(name) NULL
479#endif
480
Bram Moolenaarac92e252019-08-03 21:58:38 +0200481static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200482{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100483 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
484 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200485 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100486 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200487 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200488 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100489 {"argc", 0, 1, 0, ret_number, f_argc},
490 {"argidx", 0, 0, 0, ret_number, f_argidx},
491 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200492 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100493 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100494 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
495 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200496 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100497 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar1d634542020-08-18 13:41:50 +0200498 {"assert_fails", 1, 4, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100499 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
500 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
501 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
502 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
503 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
504 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
505 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100506 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
507 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
508 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100509#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100510 f_balloon_gettext
511#else
512 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100513#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100514 },
515 {"balloon_show", 1, 1, FEARG_1, ret_void,
516#ifdef FEAT_BEVAL
517 f_balloon_show
518#else
519 NULL
520#endif
521 },
522 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
523#if defined(FEAT_BEVAL_TERM)
524 f_balloon_split
525#else
526 NULL
527#endif
528 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100529 {"browse", 4, 4, 0, ret_string, f_browse},
530 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
531 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
532 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
533 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
534 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
535 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
536 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
537 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
538 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
539 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
540 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
541 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
542 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
543 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
544 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
545 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
546 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100547 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
548 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
549 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
550 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
551 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
552 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
553 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
554 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
555 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
556 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
557 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
558 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
559 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
560 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
561 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
562 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
563 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
564 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
565 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100566 {"changenr", 0, 0, 0, ret_number, f_changenr},
567 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
Bram Moolenaar4e4473c2020-08-28 22:24:57 +0200568 {"charclass", 1, 1, FEARG_1, ret_number, f_charclass},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100569 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
570 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
571 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
572 {"col", 1, 1, FEARG_1, ret_number, f_col},
573 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
574 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
575 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
576 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
577 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200578 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100579 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
580 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100581 {"count", 2, 4, FEARG_1, ret_number, f_count},
582 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
583 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100584 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100585#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100586 f_debugbreak
587#else
588 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200589#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100590 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200591 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100592 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
593 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
594 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
595 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
596 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
597 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
598 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
599 {"environ", 0, 0, 0, ret_dict_string, f_environ},
600 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
601 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
602 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
603 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
604 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
605 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
606 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100607 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100608 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
609 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200610 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
612 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
613 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
614 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200615 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100616 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
617 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200618 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100619 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
620 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
621 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100622 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
623 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
624 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
625 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
626 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
627 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
628 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
629 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200630 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100631 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
632 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
633 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200634 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
636 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
637 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
638 {"getchar", 0, 1, 0, ret_number, f_getchar},
639 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
640 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
641 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
642 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
643 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
644 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
645 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
646 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
647 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
648 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
649 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
650 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
651 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
652 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
653 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
654 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
655 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
656 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200657 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200658 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100659 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
660 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
661 {"getpid", 0, 0, 0, ret_number, f_getpid},
662 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200663 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200664 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200665 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100666 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
667 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
668 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
669 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
670 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200671 {"gettext", 1, 1, FEARG_1, ret_string, f_gettext},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100672 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
673 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
674 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
675 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
676 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
677 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
678 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
679 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100680 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100681 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
682 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
683 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
684 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
685 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
686 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
687 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
688 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
689 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
690 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
691 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
692 {"hostname", 0, 0, 0, ret_string, f_hostname},
693 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
694 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
695 {"index", 2, 4, FEARG_1, ret_number, f_index},
696 {"input", 1, 3, FEARG_1, ret_string, f_input},
697 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
698 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
699 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
700 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
701 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200702 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100703 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
704 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
705 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100706 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100707 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100709 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100710 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
711 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
712 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
713 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
714 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
715 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100716 {"join", 1, 2, FEARG_1, ret_string, f_join},
717 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
718 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
719 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
720 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200721 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100722 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
723 {"len", 1, 1, FEARG_1, ret_number, f_len},
724 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
725 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
726 {"line", 1, 2, FEARG_1, ret_number, f_line},
727 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
728 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
729 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
730 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
731 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
732 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
733 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100734 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
735 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
736 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200737#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100738 f_luaeval
739#else
740 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200741#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100742 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100743 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200744 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100745 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200746 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100747 {"match", 2, 4, FEARG_1, ret_any, f_match},
748 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
749 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
750 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
751 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
752 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
753 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
754 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
755 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
756 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100758#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_menu_info
760#else
761 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100764 {"min", 1, 1, FEARG_1, ret_any, f_min},
765 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
766 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200768#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100769 f_mzeval
770#else
771 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200772#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100773 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100774 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
775 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
776 {"or", 2, 2, FEARG_1, ret_number, f_or},
777 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100780 f_perleval
781#else
782 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100784 },
785 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
786 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200787 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100788 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
789 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
790 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
791 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
792 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
793 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
794 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
795 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
796 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
797 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200798 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100799 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
800 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
801 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
802 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
803 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
804 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
805 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
806 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100807 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
808 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200809 {"prompt_getprompt", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_prompt_getprompt)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100810 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
811 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
812 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
813 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
814 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
815 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
816 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
817 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
818 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
819 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
820 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
821 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
822 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100823 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
824 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100825 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100827 f_py3eval
828#else
829 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100831 },
832 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100834 f_pyeval
835#else
836 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100838 },
839 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100840#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100841 f_pyxeval
842#else
843 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100844#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100845 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100846 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
847 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200848 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
849 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100850 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200851 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100852 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
853 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
854 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100855 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100856 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
857 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
858 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
859 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
860 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
861 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200862 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200863 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100864 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200865 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100866 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200867 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100868 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
869 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100870#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100871 f_rubyeval
872#else
873 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100874#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100875 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100876 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
877 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
878 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
879 {"screencol", 0, 0, 0, ret_number, f_screencol},
880 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
881 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
882 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200883 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200884 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100885 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
886 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
887 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200888 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100889 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
890 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
891 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
892 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200893 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100894 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
895 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
896 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
897 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
898 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
899 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
900 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
901 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
902 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
903 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
904 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
905 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
906 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
907 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100908 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200909#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100910 f_sha256
911#else
912 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200913#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100914 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100915 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
916 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100917 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
918 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
919 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
920 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
921 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
922 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
923 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
924 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
925 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200926 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100927 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
928 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200929 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100930 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
931 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
932 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
933 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100934 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
935 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
936 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
937 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100938 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100939 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
940 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100941 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100942 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
943 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
944 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
945 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
946 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100947 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200948#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100949 f_strftime
950#else
951 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100953 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100954 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
955 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
956 {"string", 1, 1, FEARG_1, ret_string, f_string},
957 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200958 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100959 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100960#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100961 f_strptime
962#else
963 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100964#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100965 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100966 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
967 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
968 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
969 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
970 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
971 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
972 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
973 {"synID", 3, 3, 0, ret_number, f_synID},
974 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
975 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
976 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
977 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
978 {"system", 1, 2, FEARG_1, ret_string, f_system},
979 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
980 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
981 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
982 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
983 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
984 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100985 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
986 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100987 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100988 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
989 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
990 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
991 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
992 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100993#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100994 f_term_getansicolors
995#else
996 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200997#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100998 },
999 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
1000 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
1001 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
1002 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1003 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1004 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1005 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1006 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1007 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1008 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1009 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1010 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1011 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001012#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001013 f_term_setansicolors
1014#else
1015 NULL
1016#endif
1017 },
1018 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1019 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1020 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1021 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1022 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1023 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001024 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001025 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1026 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1027 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1028 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1029 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1030 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1031 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1032 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001033 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001034 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001035 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001036 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001037 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001038 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001039 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1040 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1041 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1042 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001043 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001044#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001045 f_test_scrollbar
1046#else
1047 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001048#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001049 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001050 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1051 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1052 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1053 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001054 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001055 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1056 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1057 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1058 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1059 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001060 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1061 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1062 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001063 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001064 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001065 {"type", 1, 1, FEARG_1, ret_number, f_type},
1066 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1067 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1068 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1069 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1070 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1071 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1072 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1073 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1074 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1075 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1076 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1077 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1078 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1079 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1080 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1081 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1082 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1083 {"wincol", 0, 0, 0, ret_number, f_wincol},
1084 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1085 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1086 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1087 {"winline", 0, 0, 0, ret_number, f_winline},
1088 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1089 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1090 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1091 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1092 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1093 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1094 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1095 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001096};
1097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001098/*
1099 * Function given to ExpandGeneric() to obtain the list of internal
1100 * or user defined function names.
1101 */
1102 char_u *
1103get_function_name(expand_T *xp, int idx)
1104{
1105 static int intidx = -1;
1106 char_u *name;
1107
1108 if (idx == 0)
1109 intidx = -1;
1110 if (intidx < 0)
1111 {
1112 name = get_user_func_name(xp, idx);
1113 if (name != NULL)
1114 return name;
1115 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001116 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001117 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001118 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001119 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001120 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001121 STRCAT(IObuff, ")");
1122 return IObuff;
1123 }
1124
1125 return NULL;
1126}
1127
1128/*
1129 * Function given to ExpandGeneric() to obtain the list of internal or
1130 * user defined variable or function names.
1131 */
1132 char_u *
1133get_expr_name(expand_T *xp, int idx)
1134{
1135 static int intidx = -1;
1136 char_u *name;
1137
1138 if (idx == 0)
1139 intidx = -1;
1140 if (intidx < 0)
1141 {
1142 name = get_function_name(xp, idx);
1143 if (name != NULL)
1144 return name;
1145 }
1146 return get_user_var_name(xp, ++intidx);
1147}
1148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001149/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001150 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001151 * Return index, or -1 if not found or "implemented" is TRUE and the function
1152 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001153 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001154 static int
1155find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001156{
1157 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001158 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001159 int cmp;
1160 int x;
1161
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001162 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001163
1164 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001165 while (first <= last)
1166 {
1167 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001168 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001169 if (cmp < 0)
1170 last = x - 1;
1171 else if (cmp > 0)
1172 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001173 else if (implemented && global_functions[x].f_func == NULL)
1174 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001175 else
1176 return x;
1177 }
1178 return -1;
1179}
1180
Bram Moolenaar15c47602020-03-26 22:16:48 +01001181/*
1182 * Find internal function "name" in table "global_functions".
1183 * Return index, or -1 if not found or the function is not implemented.
1184 */
1185 int
1186find_internal_func(char_u *name)
1187{
1188 return find_internal_func_opt(name, TRUE);
1189}
1190
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001191 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001192has_internal_func(char_u *name)
1193{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001194 return find_internal_func_opt(name, TRUE) >= 0;
1195}
1196
1197 static int
1198has_internal_func_name(char_u *name)
1199{
1200 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001201}
1202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 char *
1204internal_func_name(int idx)
1205{
1206 return global_functions[idx].f_name;
1207}
1208
1209 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001210internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001212 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213}
1214
1215/*
1216 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001217 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1218 * first argument, 2 if method base is second argument, etc. 9 if method base
1219 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 */
1221 int
1222check_internal_func(int idx, int argcount)
1223{
1224 int res;
1225 char *name;
1226
1227 if (argcount < global_functions[idx].f_min_argc)
1228 res = FCERR_TOOFEW;
1229 else if (argcount > global_functions[idx].f_max_argc)
1230 res = FCERR_TOOMANY;
1231 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001232 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233
1234 name = internal_func_name(idx);
1235 if (res == FCERR_TOOMANY)
1236 semsg(_(e_toomanyarg), name);
1237 else
1238 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001239 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240}
1241
Bram Moolenaarac92e252019-08-03 21:58:38 +02001242 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001243call_internal_func(
1244 char_u *name,
1245 int argcount,
1246 typval_T *argvars,
1247 typval_T *rettv)
1248{
1249 int i;
1250
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001251 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001252 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001253 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001254 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001255 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001256 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001257 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001258 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001259 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001260 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001261}
1262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 void
1264call_internal_func_by_idx(
1265 int idx,
1266 typval_T *argvars,
1267 typval_T *rettv)
1268{
1269 global_functions[idx].f_func(argvars, rettv);
1270}
1271
Bram Moolenaarac92e252019-08-03 21:58:38 +02001272/*
1273 * Invoke a method for base->method().
1274 */
1275 int
1276call_internal_method(
1277 char_u *name,
1278 int argcount,
1279 typval_T *argvars,
1280 typval_T *rettv,
1281 typval_T *basetv)
1282{
1283 int i;
1284 int fi;
1285 typval_T argv[MAX_FUNC_ARGS + 1];
1286
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001287 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001288 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001289 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001290 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001291 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001292 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001293 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001294 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001295 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001296
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001297 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001298 {
1299 // base value goes last
1300 for (i = 0; i < argcount; ++i)
1301 argv[i] = argvars[i];
1302 argv[argcount] = *basetv;
1303 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001304 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001305 {
1306 // base value goes second
1307 argv[0] = argvars[0];
1308 argv[1] = *basetv;
1309 for (i = 1; i < argcount; ++i)
1310 argv[i + 1] = argvars[i];
1311 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001312 else if (global_functions[fi].f_argtype == FEARG_3)
1313 {
1314 // base value goes third
1315 argv[0] = argvars[0];
1316 argv[1] = argvars[1];
1317 argv[2] = *basetv;
1318 for (i = 2; i < argcount; ++i)
1319 argv[i + 1] = argvars[i];
1320 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001321 else if (global_functions[fi].f_argtype == FEARG_4)
1322 {
1323 // base value goes fourth
1324 argv[0] = argvars[0];
1325 argv[1] = argvars[1];
1326 argv[2] = argvars[2];
1327 argv[3] = *basetv;
1328 for (i = 3; i < argcount; ++i)
1329 argv[i + 1] = argvars[i];
1330 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001331 else
1332 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001333 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001334 argv[0] = *basetv;
1335 for (i = 0; i < argcount; ++i)
1336 argv[i + 1] = argvars[i];
1337 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001338 argv[argcount + 1].v_type = VAR_UNKNOWN;
1339
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001340 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001341 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342}
1343
1344/*
1345 * Return TRUE for a non-zero Number and a non-empty String.
1346 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001347 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348non_zero_arg(typval_T *argvars)
1349{
1350 return ((argvars[0].v_type == VAR_NUMBER
1351 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001352 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 && argvars[0].vval.v_number == VVAL_TRUE)
1354 || (argvars[0].v_type == VAR_STRING
1355 && argvars[0].vval.v_string != NULL
1356 && *argvars[0].vval.v_string != NUL));
1357}
1358
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360/*
1361 * Get the float value of "argvars[0]" into "f".
1362 * Returns FAIL when the argument is not a Number or Float.
1363 */
1364 static int
1365get_float_arg(typval_T *argvars, float_T *f)
1366{
1367 if (argvars[0].v_type == VAR_FLOAT)
1368 {
1369 *f = argvars[0].vval.v_float;
1370 return OK;
1371 }
1372 if (argvars[0].v_type == VAR_NUMBER)
1373 {
1374 *f = (float_T)argvars[0].vval.v_number;
1375 return OK;
1376 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001377 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001378 return FAIL;
1379}
1380
1381/*
1382 * "abs(expr)" function
1383 */
1384 static void
1385f_abs(typval_T *argvars, typval_T *rettv)
1386{
1387 if (argvars[0].v_type == VAR_FLOAT)
1388 {
1389 rettv->v_type = VAR_FLOAT;
1390 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1391 }
1392 else
1393 {
1394 varnumber_T n;
1395 int error = FALSE;
1396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001397 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 if (error)
1399 rettv->vval.v_number = -1;
1400 else if (n > 0)
1401 rettv->vval.v_number = n;
1402 else
1403 rettv->vval.v_number = -n;
1404 }
1405}
1406
1407/*
1408 * "acos()" function
1409 */
1410 static void
1411f_acos(typval_T *argvars, typval_T *rettv)
1412{
1413 float_T f = 0.0;
1414
1415 rettv->v_type = VAR_FLOAT;
1416 if (get_float_arg(argvars, &f) == OK)
1417 rettv->vval.v_float = acos(f);
1418 else
1419 rettv->vval.v_float = 0.0;
1420}
1421#endif
1422
1423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001424 * "and(expr, expr)" function
1425 */
1426 static void
1427f_and(typval_T *argvars, typval_T *rettv)
1428{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001429 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1430 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001431}
1432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001433#ifdef FEAT_FLOAT
1434/*
1435 * "asin()" function
1436 */
1437 static void
1438f_asin(typval_T *argvars, typval_T *rettv)
1439{
1440 float_T f = 0.0;
1441
1442 rettv->v_type = VAR_FLOAT;
1443 if (get_float_arg(argvars, &f) == OK)
1444 rettv->vval.v_float = asin(f);
1445 else
1446 rettv->vval.v_float = 0.0;
1447}
1448
1449/*
1450 * "atan()" function
1451 */
1452 static void
1453f_atan(typval_T *argvars, typval_T *rettv)
1454{
1455 float_T f = 0.0;
1456
1457 rettv->v_type = VAR_FLOAT;
1458 if (get_float_arg(argvars, &f) == OK)
1459 rettv->vval.v_float = atan(f);
1460 else
1461 rettv->vval.v_float = 0.0;
1462}
1463
1464/*
1465 * "atan2()" function
1466 */
1467 static void
1468f_atan2(typval_T *argvars, typval_T *rettv)
1469{
1470 float_T fx = 0.0, fy = 0.0;
1471
1472 rettv->v_type = VAR_FLOAT;
1473 if (get_float_arg(argvars, &fx) == OK
1474 && get_float_arg(&argvars[1], &fy) == OK)
1475 rettv->vval.v_float = atan2(fx, fy);
1476 else
1477 rettv->vval.v_float = 0.0;
1478}
1479#endif
1480
1481/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001482 * "balloon_show()" function
1483 */
1484#ifdef FEAT_BEVAL
1485 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001486f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1487{
1488 rettv->v_type = VAR_STRING;
1489 if (balloonEval != NULL)
1490 {
1491 if (balloonEval->msg == NULL)
1492 rettv->vval.v_string = NULL;
1493 else
1494 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1495 }
1496}
1497
1498 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001499f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1500{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001501 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001502 {
1503 if (argvars[0].v_type == VAR_LIST
1504# ifdef FEAT_GUI
1505 && !gui.in_use
1506# endif
1507 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001508 {
1509 list_T *l = argvars[0].vval.v_list;
1510
1511 // empty list removes the balloon
1512 post_balloon(balloonEval, NULL,
1513 l == NULL || l->lv_len == 0 ? NULL : l);
1514 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001515 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001516 {
1517 char_u *mesg = tv_get_string_chk(&argvars[0]);
1518
1519 if (mesg != NULL)
1520 // empty string removes the balloon
1521 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1522 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001523 }
1524}
1525
Bram Moolenaar669a8282017-11-19 20:13:05 +01001526# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001527 static void
1528f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1529{
1530 if (rettv_list_alloc(rettv) == OK)
1531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001532 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001533
1534 if (msg != NULL)
1535 {
1536 pumitem_T *array;
1537 int size = split_message(msg, &array);
1538 int i;
1539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001540 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001541 for (i = 1; i < size - 1; ++i)
1542 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001543 while (size > 0)
1544 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001545 vim_free(array);
1546 }
1547 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001548}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001549# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001550#endif
1551
1552/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001553 * Get the buffer from "arg" and give an error and return NULL if it is not
1554 * valid.
1555 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001556 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001557get_buf_arg(typval_T *arg)
1558{
1559 buf_T *buf;
1560
1561 ++emsg_off;
1562 buf = tv_get_buf(arg, FALSE);
1563 --emsg_off;
1564 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001565 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001566 return buf;
1567}
1568
1569/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 * "byte2line(byte)" function
1571 */
1572 static void
1573f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1574{
1575#ifndef FEAT_BYTEOFF
1576 rettv->vval.v_number = -1;
1577#else
1578 long boff = 0;
1579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001580 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 if (boff < 0)
1582 rettv->vval.v_number = -1;
1583 else
1584 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1585 (linenr_T)0, &boff);
1586#endif
1587}
1588
1589 static void
1590byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1591{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 char_u *str;
1594 varnumber_T idx;
1595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001596 str = tv_get_string_chk(&argvars[0]);
1597 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598 rettv->vval.v_number = -1;
1599 if (str == NULL || idx < 0)
1600 return;
1601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 t = str;
1603 for ( ; idx > 0; idx--)
1604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001605 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606 return;
1607 if (enc_utf8 && comp)
1608 t += utf_ptr2len(t);
1609 else
1610 t += (*mb_ptr2len)(t);
1611 }
1612 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001613}
1614
1615/*
1616 * "byteidx()" function
1617 */
1618 static void
1619f_byteidx(typval_T *argvars, typval_T *rettv)
1620{
1621 byteidx(argvars, rettv, FALSE);
1622}
1623
1624/*
1625 * "byteidxcomp()" function
1626 */
1627 static void
1628f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1629{
1630 byteidx(argvars, rettv, TRUE);
1631}
1632
1633/*
1634 * "call(func, arglist [, dict])" function
1635 */
1636 static void
1637f_call(typval_T *argvars, typval_T *rettv)
1638{
1639 char_u *func;
1640 partial_T *partial = NULL;
1641 dict_T *selfdict = NULL;
1642
1643 if (argvars[1].v_type != VAR_LIST)
1644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001645 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646 return;
1647 }
1648 if (argvars[1].vval.v_list == NULL)
1649 return;
1650
1651 if (argvars[0].v_type == VAR_FUNC)
1652 func = argvars[0].vval.v_string;
1653 else if (argvars[0].v_type == VAR_PARTIAL)
1654 {
1655 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001656 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657 }
1658 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001659 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001661 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662
1663 if (argvars[2].v_type != VAR_UNKNOWN)
1664 {
1665 if (argvars[2].v_type != VAR_DICT)
1666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001667 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668 return;
1669 }
1670 selfdict = argvars[2].vval.v_dict;
1671 }
1672
1673 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1674}
1675
1676#ifdef FEAT_FLOAT
1677/*
1678 * "ceil({float})" function
1679 */
1680 static void
1681f_ceil(typval_T *argvars, typval_T *rettv)
1682{
1683 float_T f = 0.0;
1684
1685 rettv->v_type = VAR_FLOAT;
1686 if (get_float_arg(argvars, &f) == OK)
1687 rettv->vval.v_float = ceil(f);
1688 else
1689 rettv->vval.v_float = 0.0;
1690}
1691#endif
1692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693/*
1694 * "changenr()" function
1695 */
1696 static void
1697f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1698{
1699 rettv->vval.v_number = curbuf->b_u_seq_cur;
1700}
1701
1702/*
1703 * "char2nr(string)" function
1704 */
1705 static void
1706f_char2nr(typval_T *argvars, typval_T *rettv)
1707{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708 if (has_mbyte)
1709 {
1710 int utf8 = 0;
1711
1712 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02001713 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714
1715 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001716 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001718 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719 }
1720 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001721 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001722}
1723
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001724 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001725get_optional_window(typval_T *argvars, int idx)
1726{
1727 win_T *win = curwin;
1728
1729 if (argvars[idx].v_type != VAR_UNKNOWN)
1730 {
1731 win = find_win_by_nr_or_id(&argvars[idx]);
1732 if (win == NULL)
1733 {
1734 emsg(_(e_invalwindow));
1735 return NULL;
1736 }
1737 }
1738 return win;
1739}
1740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001741/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001742 * "col(string)" function
1743 */
1744 static void
1745f_col(typval_T *argvars, typval_T *rettv)
1746{
1747 colnr_T col = 0;
1748 pos_T *fp;
1749 int fnum = curbuf->b_fnum;
1750
1751 fp = var2fpos(&argvars[0], FALSE, &fnum);
1752 if (fp != NULL && fnum == curbuf->b_fnum)
1753 {
1754 if (fp->col == MAXCOL)
1755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001756 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001757 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1758 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1759 else
1760 col = MAXCOL;
1761 }
1762 else
1763 {
1764 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001765 // col(".") when the cursor is on the NUL at the end of the line
1766 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001767 if (virtual_active() && fp == &curwin->w_cursor)
1768 {
1769 char_u *p = ml_get_cursor();
1770
1771 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1772 curwin->w_virtcol - curwin->w_cursor.coladd))
1773 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001774 int l;
1775
1776 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1777 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001778 }
1779 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780 }
1781 }
1782 rettv->vval.v_number = col;
1783}
1784
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001785/*
1786 * "confirm(message, buttons[, default [, type]])" function
1787 */
1788 static void
1789f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1790{
1791#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1792 char_u *message;
1793 char_u *buttons = NULL;
1794 char_u buf[NUMBUFLEN];
1795 char_u buf2[NUMBUFLEN];
1796 int def = 1;
1797 int type = VIM_GENERIC;
1798 char_u *typestr;
1799 int error = FALSE;
1800
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001801 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802 if (message == NULL)
1803 error = TRUE;
1804 if (argvars[1].v_type != VAR_UNKNOWN)
1805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 if (buttons == NULL)
1808 error = TRUE;
1809 if (argvars[2].v_type != VAR_UNKNOWN)
1810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001811 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001812 if (argvars[3].v_type != VAR_UNKNOWN)
1813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001814 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001815 if (typestr == NULL)
1816 error = TRUE;
1817 else
1818 {
1819 switch (TOUPPER_ASC(*typestr))
1820 {
1821 case 'E': type = VIM_ERROR; break;
1822 case 'Q': type = VIM_QUESTION; break;
1823 case 'I': type = VIM_INFO; break;
1824 case 'W': type = VIM_WARNING; break;
1825 case 'G': type = VIM_GENERIC; break;
1826 }
1827 }
1828 }
1829 }
1830 }
1831
1832 if (buttons == NULL || *buttons == NUL)
1833 buttons = (char_u *)_("&Ok");
1834
1835 if (!error)
1836 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1837 def, NULL, FALSE);
1838#endif
1839}
1840
1841/*
1842 * "copy()" function
1843 */
1844 static void
1845f_copy(typval_T *argvars, typval_T *rettv)
1846{
1847 item_copy(&argvars[0], rettv, FALSE, 0);
1848}
1849
1850#ifdef FEAT_FLOAT
1851/*
1852 * "cos()" function
1853 */
1854 static void
1855f_cos(typval_T *argvars, typval_T *rettv)
1856{
1857 float_T f = 0.0;
1858
1859 rettv->v_type = VAR_FLOAT;
1860 if (get_float_arg(argvars, &f) == OK)
1861 rettv->vval.v_float = cos(f);
1862 else
1863 rettv->vval.v_float = 0.0;
1864}
1865
1866/*
1867 * "cosh()" function
1868 */
1869 static void
1870f_cosh(typval_T *argvars, typval_T *rettv)
1871{
1872 float_T f = 0.0;
1873
1874 rettv->v_type = VAR_FLOAT;
1875 if (get_float_arg(argvars, &f) == OK)
1876 rettv->vval.v_float = cosh(f);
1877 else
1878 rettv->vval.v_float = 0.0;
1879}
1880#endif
1881
1882/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 * "cursor(lnum, col)" function, or
1884 * "cursor(list)"
1885 *
1886 * Moves the cursor to the specified line and column.
1887 * Returns 0 when the position could be set, -1 otherwise.
1888 */
1889 static void
1890f_cursor(typval_T *argvars, typval_T *rettv)
1891{
1892 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001893 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894 int set_curswant = TRUE;
1895
1896 rettv->vval.v_number = -1;
1897 if (argvars[1].v_type == VAR_UNKNOWN)
1898 {
1899 pos_T pos;
1900 colnr_T curswant = -1;
1901
1902 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 return;
1906 }
1907 line = pos.lnum;
1908 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 if (curswant >= 0)
1911 {
1912 curwin->w_curswant = curswant - 1;
1913 set_curswant = FALSE;
1914 }
1915 }
1916 else
1917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001918 line = tv_get_lnum(argvars);
1919 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001921 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001923 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001924 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 if (line > 0)
1926 curwin->w_cursor.lnum = line;
1927 if (col > 0)
1928 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001933 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 if (has_mbyte)
1935 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001936
1937 curwin->w_set_curswant = set_curswant;
1938 rettv->vval.v_number = 0;
1939}
1940
Bram Moolenaar4f974752019-02-17 17:44:42 +01001941#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001942/*
1943 * "debugbreak()" function
1944 */
1945 static void
1946f_debugbreak(typval_T *argvars, typval_T *rettv)
1947{
1948 int pid;
1949
1950 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001951 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001952 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001954 else
1955 {
1956 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1957
1958 if (hProcess != NULL)
1959 {
1960 DebugBreakProcess(hProcess);
1961 CloseHandle(hProcess);
1962 rettv->vval.v_number = OK;
1963 }
1964 }
1965}
1966#endif
1967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001968/*
1969 * "deepcopy()" function
1970 */
1971 static void
1972f_deepcopy(typval_T *argvars, typval_T *rettv)
1973{
1974 int noref = 0;
1975 int copyID;
1976
1977 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02001978 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001981 else
1982 {
1983 copyID = get_copyID();
1984 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1985 }
1986}
1987
1988/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 * "did_filetype()" function
1990 */
1991 static void
1992f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1993{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995}
1996
1997/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001998 * "echoraw({expr})" function
1999 */
2000 static void
2001f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2002{
2003 char_u *str = tv_get_string_chk(&argvars[0]);
2004
2005 if (str != NULL && *str != NUL)
2006 {
2007 out_str(str);
2008 out_flush();
2009 }
2010}
2011
2012/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002013 * "empty({expr})" function
2014 */
2015 static void
2016f_empty(typval_T *argvars, typval_T *rettv)
2017{
2018 int n = FALSE;
2019
2020 switch (argvars[0].v_type)
2021 {
2022 case VAR_STRING:
2023 case VAR_FUNC:
2024 n = argvars[0].vval.v_string == NULL
2025 || *argvars[0].vval.v_string == NUL;
2026 break;
2027 case VAR_PARTIAL:
2028 n = FALSE;
2029 break;
2030 case VAR_NUMBER:
2031 n = argvars[0].vval.v_number == 0;
2032 break;
2033 case VAR_FLOAT:
2034#ifdef FEAT_FLOAT
2035 n = argvars[0].vval.v_float == 0.0;
2036 break;
2037#endif
2038 case VAR_LIST:
2039 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002040 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 break;
2042 case VAR_DICT:
2043 n = argvars[0].vval.v_dict == NULL
2044 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2045 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002046 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 case VAR_SPECIAL:
2048 n = argvars[0].vval.v_number != VVAL_TRUE;
2049 break;
2050
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002051 case VAR_BLOB:
2052 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002053 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2054 break;
2055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056 case VAR_JOB:
2057#ifdef FEAT_JOB_CHANNEL
2058 n = argvars[0].vval.v_job == NULL
2059 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2060 break;
2061#endif
2062 case VAR_CHANNEL:
2063#ifdef FEAT_JOB_CHANNEL
2064 n = argvars[0].vval.v_channel == NULL
2065 || !channel_is_open(argvars[0].vval.v_channel);
2066 break;
2067#endif
2068 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002069 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002071 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002072 n = TRUE;
2073 break;
2074 }
2075
2076 rettv->vval.v_number = n;
2077}
2078
2079/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002080 * "environ()" function
2081 */
2082 static void
2083f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2084{
2085#if !defined(AMIGA)
2086 int i = 0;
2087 char_u *entry, *value;
2088# ifdef MSWIN
2089 extern wchar_t **_wenviron;
2090# else
2091 extern char **environ;
2092# endif
2093
2094 if (rettv_dict_alloc(rettv) != OK)
2095 return;
2096
2097# ifdef MSWIN
2098 if (*_wenviron == NULL)
2099 return;
2100# else
2101 if (*environ == NULL)
2102 return;
2103# endif
2104
2105 for (i = 0; ; ++i)
2106 {
2107# ifdef MSWIN
2108 short_u *p;
2109
2110 if ((p = (short_u *)_wenviron[i]) == NULL)
2111 return;
2112 entry = utf16_to_enc(p, NULL);
2113# else
2114 if ((entry = (char_u *)environ[i]) == NULL)
2115 return;
2116 entry = vim_strsave(entry);
2117# endif
2118 if (entry == NULL) // out of memory
2119 return;
2120 if ((value = vim_strchr(entry, '=')) == NULL)
2121 {
2122 vim_free(entry);
2123 continue;
2124 }
2125 *value++ = NUL;
2126 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2127 vim_free(entry);
2128 }
2129#endif
2130}
2131
2132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002133 * "escape({string}, {chars})" function
2134 */
2135 static void
2136f_escape(typval_T *argvars, typval_T *rettv)
2137{
2138 char_u buf[NUMBUFLEN];
2139
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002140 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2141 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 rettv->v_type = VAR_STRING;
2143}
2144
2145/*
2146 * "eval()" function
2147 */
2148 static void
2149f_eval(typval_T *argvars, typval_T *rettv)
2150{
2151 char_u *s, *p;
2152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002153 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 if (s != NULL)
2155 s = skipwhite(s);
2156
2157 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002158 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 {
2160 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002161 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 need_clr_eos = FALSE;
2163 rettv->v_type = VAR_NUMBER;
2164 rettv->vval.v_number = 0;
2165 }
2166 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002167 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168}
2169
2170/*
2171 * "eventhandler()" function
2172 */
2173 static void
2174f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2175{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002176 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177}
2178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002179static garray_T redir_execute_ga;
2180
2181/*
2182 * Append "value[value_len]" to the execute() output.
2183 */
2184 void
2185execute_redir_str(char_u *value, int value_len)
2186{
2187 int len;
2188
2189 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002190 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002191 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002192 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002193 if (ga_grow(&redir_execute_ga, len) == OK)
2194 {
2195 mch_memmove((char *)redir_execute_ga.ga_data
2196 + redir_execute_ga.ga_len, value, len);
2197 redir_execute_ga.ga_len += len;
2198 }
2199}
2200
2201/*
2202 * Get next line from a list.
2203 * Called by do_cmdline() to get the next line.
2204 * Returns allocated string, or NULL for end of function.
2205 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206 static char_u *
2207get_list_line(
2208 int c UNUSED,
2209 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002210 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002211 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002212{
2213 listitem_T **p = (listitem_T **)cookie;
2214 listitem_T *item = *p;
2215 char_u buf[NUMBUFLEN];
2216 char_u *s;
2217
2218 if (item == NULL)
2219 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002220 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221 *p = item->li_next;
2222 return s == NULL ? NULL : vim_strsave(s);
2223}
2224
2225/*
2226 * "execute()" function
2227 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002228 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002229execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230{
2231 char_u *cmd = NULL;
2232 list_T *list = NULL;
2233 int save_msg_silent = msg_silent;
2234 int save_emsg_silent = emsg_silent;
2235 int save_emsg_noredir = emsg_noredir;
2236 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002237 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002238 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002239 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002240 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002241
2242 rettv->vval.v_string = NULL;
2243 rettv->v_type = VAR_STRING;
2244
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002245 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002247 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002248 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002249 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250 return;
2251 ++list->lv_refcount;
2252 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002253 else if (argvars[arg_off].v_type == VAR_JOB
2254 || argvars[arg_off].v_type == VAR_CHANNEL)
2255 {
2256 emsg(_(e_inval_string));
2257 return;
2258 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 else
2260 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002261 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002262 if (cmd == NULL)
2263 return;
2264 }
2265
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002266 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 {
2268 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002269 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270
2271 if (s == NULL)
2272 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002273 if (*s == NUL)
2274 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275 if (STRNCMP(s, "silent", 6) == 0)
2276 ++msg_silent;
2277 if (STRCMP(s, "silent!") == 0)
2278 {
2279 emsg_silent = TRUE;
2280 emsg_noredir = TRUE;
2281 }
2282 }
2283 else
2284 ++msg_silent;
2285
2286 if (redir_execute)
2287 save_ga = redir_execute_ga;
2288 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2289 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002290 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002291 if (!echo_output)
2292 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002293
2294 if (cmd != NULL)
2295 do_cmdline_cmd(cmd);
2296 else
2297 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002298 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002300 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002301 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302 do_cmdline(NULL, get_list_line, (void *)&item,
2303 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2304 --list->lv_refcount;
2305 }
2306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002307 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002308 if (ga_grow(&redir_execute_ga, 1) == OK)
2309 {
2310 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2311 rettv->vval.v_string = redir_execute_ga.ga_data;
2312 }
2313 else
2314 {
2315 ga_clear(&redir_execute_ga);
2316 rettv->vval.v_string = NULL;
2317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002318 msg_silent = save_msg_silent;
2319 emsg_silent = save_emsg_silent;
2320 emsg_noredir = save_emsg_noredir;
2321
2322 redir_execute = save_redir_execute;
2323 if (redir_execute)
2324 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002325 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002327 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002328 if (echo_output)
2329 // When not working silently: put it in column zero. A following
2330 // "echon" will overwrite the message, unavoidably.
2331 msg_col = 0;
2332 else
2333 // When working silently: Put it back where it was, since nothing
2334 // should have been written.
2335 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002336}
2337
2338/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002339 * "execute()" function
2340 */
2341 static void
2342f_execute(typval_T *argvars, typval_T *rettv)
2343{
2344 execute_common(argvars, rettv, 0);
2345}
2346
2347/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002348 * "exists()" function
2349 */
2350 static void
2351f_exists(typval_T *argvars, typval_T *rettv)
2352{
2353 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002356 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002357 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002359 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360 if (mch_getenv(p + 1) != NULL)
2361 n = TRUE;
2362 else
2363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365 p = expand_env_save(p);
2366 if (p != NULL && *p != '$')
2367 n = TRUE;
2368 vim_free(p);
2369 }
2370 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002371 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002373 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002375 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002377 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002379 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002381 else if (*p == '?') // internal function only
2382 {
2383 n = has_internal_func_name(p + 1);
2384 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 else if (*p == ':')
2386 {
2387 n = cmd_exists(p + 1);
2388 }
2389 else if (*p == '#')
2390 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002391 if (p[1] == '#')
2392 n = autocmd_supported(p + 2);
2393 else
2394 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002396 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002398 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 }
2400
2401 rettv->vval.v_number = n;
2402}
2403
2404#ifdef FEAT_FLOAT
2405/*
2406 * "exp()" function
2407 */
2408 static void
2409f_exp(typval_T *argvars, typval_T *rettv)
2410{
2411 float_T f = 0.0;
2412
2413 rettv->v_type = VAR_FLOAT;
2414 if (get_float_arg(argvars, &f) == OK)
2415 rettv->vval.v_float = exp(f);
2416 else
2417 rettv->vval.v_float = 0.0;
2418}
2419#endif
2420
2421/*
2422 * "expand()" function
2423 */
2424 static void
2425f_expand(typval_T *argvars, typval_T *rettv)
2426{
2427 char_u *s;
2428 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002429 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2431 expand_T xpc;
2432 int error = FALSE;
2433 char_u *result;
2434
2435 rettv->v_type = VAR_STRING;
2436 if (argvars[1].v_type != VAR_UNKNOWN
2437 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002438 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002440 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002442 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443 if (*s == '%' || *s == '#' || *s == '<')
2444 {
2445 ++emsg_off;
2446 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2447 --emsg_off;
2448 if (rettv->v_type == VAR_LIST)
2449 {
2450 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2451 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002452 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453 }
2454 else
2455 rettv->vval.v_string = result;
2456 }
2457 else
2458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002459 // When the optional second argument is non-zero, don't remove matches
2460 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002462 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002463 options |= WILD_KEEP_ALL;
2464 if (!error)
2465 {
2466 ExpandInit(&xpc);
2467 xpc.xp_context = EXPAND_FILES;
2468 if (p_wic)
2469 options += WILD_ICASE;
2470 if (rettv->v_type == VAR_STRING)
2471 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2472 options, WILD_ALL);
2473 else if (rettv_list_alloc(rettv) != FAIL)
2474 {
2475 int i;
2476
2477 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2478 for (i = 0; i < xpc.xp_numfiles; i++)
2479 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2480 ExpandCleanup(&xpc);
2481 }
2482 }
2483 else
2484 rettv->vval.v_string = NULL;
2485 }
2486}
2487
2488/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002489 * "expandcmd()" function
2490 * Expand all the special characters in a command string.
2491 */
2492 static void
2493f_expandcmd(typval_T *argvars, typval_T *rettv)
2494{
2495 exarg_T eap;
2496 char_u *cmdstr;
2497 char *errormsg = NULL;
2498
2499 rettv->v_type = VAR_STRING;
2500 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2501
2502 memset(&eap, 0, sizeof(eap));
2503 eap.cmd = cmdstr;
2504 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002506 eap.usefilter = FALSE;
2507 eap.nextcmd = NULL;
2508 eap.cmdidx = CMD_USER;
2509
2510 expand_filename(&eap, &cmdstr, &errormsg);
2511 if (errormsg != NULL && *errormsg != NUL)
2512 emsg(errormsg);
2513
2514 rettv->vval.v_string = cmdstr;
2515}
2516
2517/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 * "feedkeys()" function
2519 */
2520 static void
2521f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2522{
2523 int remap = TRUE;
2524 int insert = FALSE;
2525 char_u *keys, *flags;
2526 char_u nbuf[NUMBUFLEN];
2527 int typed = FALSE;
2528 int execute = FALSE;
2529 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002530 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 char_u *keys_esc;
2532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002533 // This is not allowed in the sandbox. If the commands would still be
2534 // executed in the sandbox it would be OK, but it probably happens later,
2535 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 if (check_secure())
2537 return;
2538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002539 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540
2541 if (argvars[1].v_type != VAR_UNKNOWN)
2542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002543 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002544 for ( ; *flags != NUL; ++flags)
2545 {
2546 switch (*flags)
2547 {
2548 case 'n': remap = FALSE; break;
2549 case 'm': remap = TRUE; break;
2550 case 't': typed = TRUE; break;
2551 case 'i': insert = TRUE; break;
2552 case 'x': execute = TRUE; break;
2553 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002554 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002555 }
2556 }
2557 }
2558
2559 if (*keys != NUL || execute)
2560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002561 // Need to escape K_SPECIAL and CSI before putting the string in the
2562 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 keys_esc = vim_strsave_escape_csi(keys);
2564 if (keys_esc != NULL)
2565 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002566 if (lowlevel)
2567 {
2568#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002569 int idx;
2570 int len = (int)STRLEN(keys);
2571
2572 for (idx = 0; idx < len; ++idx)
2573 {
2574 // if a CTRL-C was typed, set got_int, similar to what
2575 // happens in fill_input_buf()
2576 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2577 got_int = TRUE;
2578 add_to_input_buf(keys + idx, 1);
2579 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002580#else
2581 emsg(_("E980: lowlevel input not supported"));
2582#endif
2583 }
2584 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002585 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002586 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002587 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002588 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002589#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002590 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002591#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002592 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002593 typebuf_was_filled = TRUE;
2594 }
2595 vim_free(keys_esc);
2596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 if (execute)
2598 {
2599 int save_msg_scroll = msg_scroll;
2600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002601 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 msg_scroll = FALSE;
2603
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002604 if (!dangerous)
2605 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002606 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002607 if (!dangerous)
2608 --ex_normal_busy;
2609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 msg_scroll |= save_msg_scroll;
2611 }
2612 }
2613 }
2614}
2615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616#ifdef FEAT_FLOAT
2617/*
2618 * "float2nr({float})" function
2619 */
2620 static void
2621f_float2nr(typval_T *argvars, typval_T *rettv)
2622{
2623 float_T f = 0.0;
2624
2625 if (get_float_arg(argvars, &f) == OK)
2626 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002627 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002628 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002629 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002630 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 else
2632 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002633 }
2634}
2635
2636/*
2637 * "floor({float})" function
2638 */
2639 static void
2640f_floor(typval_T *argvars, typval_T *rettv)
2641{
2642 float_T f = 0.0;
2643
2644 rettv->v_type = VAR_FLOAT;
2645 if (get_float_arg(argvars, &f) == OK)
2646 rettv->vval.v_float = floor(f);
2647 else
2648 rettv->vval.v_float = 0.0;
2649}
2650
2651/*
2652 * "fmod()" function
2653 */
2654 static void
2655f_fmod(typval_T *argvars, typval_T *rettv)
2656{
2657 float_T fx = 0.0, fy = 0.0;
2658
2659 rettv->v_type = VAR_FLOAT;
2660 if (get_float_arg(argvars, &fx) == OK
2661 && get_float_arg(&argvars[1], &fy) == OK)
2662 rettv->vval.v_float = fmod(fx, fy);
2663 else
2664 rettv->vval.v_float = 0.0;
2665}
2666#endif
2667
2668/*
2669 * "fnameescape({string})" function
2670 */
2671 static void
2672f_fnameescape(typval_T *argvars, typval_T *rettv)
2673{
2674 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002675 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676 rettv->v_type = VAR_STRING;
2677}
2678
2679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 * "foreground()" function
2681 */
2682 static void
2683f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2684{
2685#ifdef FEAT_GUI
2686 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002687 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002689 return;
2690 }
2691#endif
2692#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694#endif
2695}
2696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002698common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699{
2700 char_u *s;
2701 char_u *name;
2702 int use_string = FALSE;
2703 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002704 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002705 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706
2707 if (argvars[0].v_type == VAR_FUNC)
2708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002709 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710 s = argvars[0].vval.v_string;
2711 }
2712 else if (argvars[0].v_type == VAR_PARTIAL
2713 && argvars[0].vval.v_partial != NULL)
2714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002715 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002717 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 }
2719 else
2720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002721 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002722 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 use_string = TRUE;
2724 }
2725
Bram Moolenaar843b8842016-08-21 14:36:15 +02002726 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002727 {
2728 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002729 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002730 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2731 if (*name != NUL)
2732 s = NULL;
2733 }
2734
Bram Moolenaar843b8842016-08-21 14:36:15 +02002735 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2736 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002737 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002738 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002739 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002740 ? find_func(trans_name, is_global, NULL) == NULL
2741 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002742 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 else
2744 {
2745 int dict_idx = 0;
2746 int arg_idx = 0;
2747 list_T *list = NULL;
2748
2749 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2750 {
2751 char sid_buf[25];
2752 int off = *s == 's' ? 2 : 5;
2753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002754 // Expand s: and <SID> into <SNR>nr_, so that the function can
2755 // also be called from another script. Using trans_function_name()
2756 // would also work, but some plugins depend on the name being
2757 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002758 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002759 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002760 if (name != NULL)
2761 {
2762 STRCPY(name, sid_buf);
2763 STRCAT(name, s + off);
2764 }
2765 }
2766 else
2767 name = vim_strsave(s);
2768
2769 if (argvars[1].v_type != VAR_UNKNOWN)
2770 {
2771 if (argvars[2].v_type != VAR_UNKNOWN)
2772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002773 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 arg_idx = 1;
2775 dict_idx = 2;
2776 }
2777 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002778 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 dict_idx = 1;
2780 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002781 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 arg_idx = 1;
2783 if (dict_idx > 0)
2784 {
2785 if (argvars[dict_idx].v_type != VAR_DICT)
2786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002787 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002789 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 }
2791 if (argvars[dict_idx].vval.v_dict == NULL)
2792 dict_idx = 0;
2793 }
2794 if (arg_idx > 0)
2795 {
2796 if (argvars[arg_idx].v_type != VAR_LIST)
2797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002798 emsg(_("E923: Second argument of function() must be a list or 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 list = argvars[arg_idx].vval.v_list;
2803 if (list == NULL || list->lv_len == 0)
2804 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002805 else if (list->lv_len > MAX_FUNC_ARGS)
2806 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002807 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002808 vim_free(name);
2809 goto theend;
2810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 }
2812 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002813 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002815 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002817 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818 if (pt == NULL)
2819 vim_free(name);
2820 else
2821 {
2822 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2823 {
2824 listitem_T *li;
2825 int i = 0;
2826 int arg_len = 0;
2827 int lv_len = 0;
2828
2829 if (arg_pt != NULL)
2830 arg_len = arg_pt->pt_argc;
2831 if (list != NULL)
2832 lv_len = list->lv_len;
2833 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002834 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 if (pt->pt_argv == NULL)
2836 {
2837 vim_free(pt);
2838 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002839 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002841 for (i = 0; i < arg_len; i++)
2842 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2843 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002844 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002845 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002846 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002847 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002848 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002849 }
2850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // For "function(dict.func, [], dict)" and "func" is a partial
2852 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002853 if (dict_idx > 0)
2854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002855 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2857 ++pt->pt_dict->dv_refcount;
2858 }
2859 else if (arg_pt != NULL)
2860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002861 // If the dict was bound automatically the result is also
2862 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 pt->pt_dict = arg_pt->pt_dict;
2864 pt->pt_auto = arg_pt->pt_auto;
2865 if (pt->pt_dict != NULL)
2866 ++pt->pt_dict->dv_refcount;
2867 }
2868
2869 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002870 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2871 {
2872 pt->pt_func = arg_pt->pt_func;
2873 func_ptr_ref(pt->pt_func);
2874 vim_free(name);
2875 }
2876 else if (is_funcref)
2877 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002878 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002879 func_ptr_ref(pt->pt_func);
2880 vim_free(name);
2881 }
2882 else
2883 {
2884 pt->pt_name = name;
2885 func_ref(name);
2886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 }
2888 rettv->v_type = VAR_PARTIAL;
2889 rettv->vval.v_partial = pt;
2890 }
2891 else
2892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002893 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 rettv->v_type = VAR_FUNC;
2895 rettv->vval.v_string = name;
2896 func_ref(name);
2897 }
2898 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002899theend:
2900 vim_free(trans_name);
2901}
2902
2903/*
2904 * "funcref()" function
2905 */
2906 static void
2907f_funcref(typval_T *argvars, typval_T *rettv)
2908{
2909 common_function(argvars, rettv, TRUE);
2910}
2911
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002912 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002913ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002914{
2915 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2916 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002917 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002918}
2919
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002920/*
2921 * "function()" function
2922 */
2923 static void
2924f_function(typval_T *argvars, typval_T *rettv)
2925{
2926 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927}
2928
2929/*
2930 * "garbagecollect()" function
2931 */
2932 static void
2933f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2934{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002935 // This is postponed until we are back at the toplevel, because we may be
2936 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 want_garbage_collect = TRUE;
2938
Bram Moolenaar2df47312020-09-05 17:30:44 +02002939 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940 garbage_collect_at_exit = TRUE;
2941}
2942
2943/*
2944 * "get()" function
2945 */
2946 static void
2947f_get(typval_T *argvars, typval_T *rettv)
2948{
2949 listitem_T *li;
2950 list_T *l;
2951 dictitem_T *di;
2952 dict_T *d;
2953 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002954 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002956 if (argvars[0].v_type == VAR_BLOB)
2957 {
2958 int error = FALSE;
2959 int idx = tv_get_number_chk(&argvars[1], &error);
2960
2961 if (!error)
2962 {
2963 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002964 if (idx < 0)
2965 idx = blob_len(argvars[0].vval.v_blob) + idx;
2966 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2967 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002968 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002969 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002970 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002971 tv = rettv;
2972 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002973 }
2974 }
2975 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002976 {
2977 if ((l = argvars[0].vval.v_list) != NULL)
2978 {
2979 int error = FALSE;
2980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002981 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 if (!error && li != NULL)
2983 tv = &li->li_tv;
2984 }
2985 }
2986 else if (argvars[0].v_type == VAR_DICT)
2987 {
2988 if ((d = argvars[0].vval.v_dict) != NULL)
2989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002990 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 if (di != NULL)
2992 tv = &di->di_tv;
2993 }
2994 }
2995 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2996 {
2997 partial_T *pt;
2998 partial_T fref_pt;
2999
3000 if (argvars[0].v_type == VAR_PARTIAL)
3001 pt = argvars[0].vval.v_partial;
3002 else
3003 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003004 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 fref_pt.pt_name = argvars[0].vval.v_string;
3006 pt = &fref_pt;
3007 }
3008
3009 if (pt != NULL)
3010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003011 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003012 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013
3014 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3015 {
3016 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003017 n = partial_name(pt);
3018 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 rettv->vval.v_string = NULL;
3020 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003021 {
3022 rettv->vval.v_string = vim_strsave(n);
3023 if (rettv->v_type == VAR_FUNC)
3024 func_ref(rettv->vval.v_string);
3025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 }
3027 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003028 {
3029 what_is_dict = TRUE;
3030 if (pt->pt_dict != NULL)
3031 rettv_dict_set(rettv, pt->pt_dict);
3032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 else if (STRCMP(what, "args") == 0)
3034 {
3035 rettv->v_type = VAR_LIST;
3036 if (rettv_list_alloc(rettv) == OK)
3037 {
3038 int i;
3039
3040 for (i = 0; i < pt->pt_argc; ++i)
3041 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3042 }
3043 }
3044 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003045 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003046
3047 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3048 // third argument
3049 if (!what_is_dict)
3050 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 }
3052 }
3053 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003054 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055
3056 if (tv == NULL)
3057 {
3058 if (argvars[2].v_type != VAR_UNKNOWN)
3059 copy_tv(&argvars[2], rettv);
3060 }
3061 else
3062 copy_tv(tv, rettv);
3063}
3064
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003065/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003066 * "getchangelist()" function
3067 */
3068 static void
3069f_getchangelist(typval_T *argvars, typval_T *rettv)
3070{
3071#ifdef FEAT_JUMPLIST
3072 buf_T *buf;
3073 int i;
3074 list_T *l;
3075 dict_T *d;
3076#endif
3077
3078 if (rettv_list_alloc(rettv) != OK)
3079 return;
3080
3081#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003082 if (argvars[0].v_type == VAR_UNKNOWN)
3083 buf = curbuf;
3084 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003085 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003086 if (buf == NULL)
3087 return;
3088
3089 l = list_alloc();
3090 if (l == NULL)
3091 return;
3092
3093 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3094 return;
3095 /*
3096 * The current window change list index tracks only the position in the
3097 * current buffer change list. For other buffers, use the change list
3098 * length as the current index.
3099 */
3100 list_append_number(rettv->vval.v_list,
3101 (varnumber_T)((buf == curwin->w_buffer)
3102 ? curwin->w_changelistidx : buf->b_changelistlen));
3103
3104 for (i = 0; i < buf->b_changelistlen; ++i)
3105 {
3106 if (buf->b_changelist[i].lnum == 0)
3107 continue;
3108 if ((d = dict_alloc()) == NULL)
3109 return;
3110 if (list_append_dict(l, d) == FAIL)
3111 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003112 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3113 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003114 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003115 }
3116#endif
3117}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118
3119/*
3120 * "getcharsearch()" function
3121 */
3122 static void
3123f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3124{
3125 if (rettv_dict_alloc(rettv) != FAIL)
3126 {
3127 dict_T *dict = rettv->vval.v_dict;
3128
Bram Moolenaare0be1672018-07-08 16:50:37 +02003129 dict_add_string(dict, "char", last_csearch());
3130 dict_add_number(dict, "forward", last_csearch_forward());
3131 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 }
3133}
3134
3135/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003136 * "getenv()" function
3137 */
3138 static void
3139f_getenv(typval_T *argvars, typval_T *rettv)
3140{
3141 int mustfree = FALSE;
3142 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3143
3144 if (p == NULL)
3145 {
3146 rettv->v_type = VAR_SPECIAL;
3147 rettv->vval.v_number = VVAL_NULL;
3148 return;
3149 }
3150 if (!mustfree)
3151 p = vim_strsave(p);
3152 rettv->vval.v_string = p;
3153 rettv->v_type = VAR_STRING;
3154}
3155
3156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 * "getfontname()" function
3158 */
3159 static void
3160f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3161{
3162 rettv->v_type = VAR_STRING;
3163 rettv->vval.v_string = NULL;
3164#ifdef FEAT_GUI
3165 if (gui.in_use)
3166 {
3167 GuiFont font;
3168 char_u *name = NULL;
3169
3170 if (argvars[0].v_type == VAR_UNKNOWN)
3171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003172 // Get the "Normal" font. Either the name saved by
3173 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 font = gui.norm_font;
3175 name = hl_get_font_name();
3176 }
3177 else
3178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003180 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 return;
3182 font = gui_mch_get_font(name, FALSE);
3183 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 }
3186 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3187 if (argvars[0].v_type != VAR_UNKNOWN)
3188 gui_mch_free_font(font);
3189 }
3190#endif
3191}
3192
3193/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003194 * "getjumplist()" function
3195 */
3196 static void
3197f_getjumplist(typval_T *argvars, typval_T *rettv)
3198{
3199#ifdef FEAT_JUMPLIST
3200 win_T *wp;
3201 int i;
3202 list_T *l;
3203 dict_T *d;
3204#endif
3205
3206 if (rettv_list_alloc(rettv) != OK)
3207 return;
3208
3209#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003210 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003211 if (wp == NULL)
3212 return;
3213
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003214 cleanup_jumplist(wp, TRUE);
3215
Bram Moolenaar4f505882018-02-10 21:06:32 +01003216 l = list_alloc();
3217 if (l == NULL)
3218 return;
3219
3220 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3221 return;
3222 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3223
3224 for (i = 0; i < wp->w_jumplistlen; ++i)
3225 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003226 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3227 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003228 if ((d = dict_alloc()) == NULL)
3229 return;
3230 if (list_append_dict(l, d) == FAIL)
3231 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003232 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3233 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003234 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003236 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003237 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003238 }
3239#endif
3240}
3241
3242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243 * "getpid()" function
3244 */
3245 static void
3246f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3247{
3248 rettv->vval.v_number = mch_get_pid();
3249}
3250
3251 static void
3252getpos_both(
3253 typval_T *argvars,
3254 typval_T *rettv,
3255 int getcurpos)
3256{
3257 pos_T *fp;
3258 list_T *l;
3259 int fnum = -1;
3260
3261 if (rettv_list_alloc(rettv) == OK)
3262 {
3263 l = rettv->vval.v_list;
3264 if (getcurpos)
3265 fp = &curwin->w_cursor;
3266 else
3267 fp = var2fpos(&argvars[0], TRUE, &fnum);
3268 if (fnum != -1)
3269 list_append_number(l, (varnumber_T)fnum);
3270 else
3271 list_append_number(l, (varnumber_T)0);
3272 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3273 : (varnumber_T)0);
3274 list_append_number(l, (fp != NULL)
3275 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3276 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003277 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 (varnumber_T)0);
3279 if (getcurpos)
3280 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003281 int save_set_curswant = curwin->w_set_curswant;
3282 colnr_T save_curswant = curwin->w_curswant;
3283 colnr_T save_virtcol = curwin->w_virtcol;
3284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 update_curswant();
3286 list_append_number(l, curwin->w_curswant == MAXCOL ?
3287 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003288
3289 // Do not change "curswant", as it is unexpected that a get
3290 // function has a side effect.
3291 if (save_set_curswant)
3292 {
3293 curwin->w_set_curswant = save_set_curswant;
3294 curwin->w_curswant = save_curswant;
3295 curwin->w_virtcol = save_virtcol;
3296 curwin->w_valid &= ~VALID_VIRTCOL;
3297 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 }
3299 }
3300 else
3301 rettv->vval.v_number = FALSE;
3302}
3303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304/*
3305 * "getcurpos()" function
3306 */
3307 static void
3308f_getcurpos(typval_T *argvars, typval_T *rettv)
3309{
3310 getpos_both(argvars, rettv, TRUE);
3311}
3312
3313/*
3314 * "getpos(string)" function
3315 */
3316 static void
3317f_getpos(typval_T *argvars, typval_T *rettv)
3318{
3319 getpos_both(argvars, rettv, FALSE);
3320}
3321
3322/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 * "getreg()" function
3324 */
3325 static void
3326f_getreg(typval_T *argvars, typval_T *rettv)
3327{
3328 char_u *strregname;
3329 int regname;
3330 int arg2 = FALSE;
3331 int return_list = FALSE;
3332 int error = FALSE;
3333
3334 if (argvars[0].v_type != VAR_UNKNOWN)
3335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 error = strregname == NULL;
3338 if (argvars[1].v_type != VAR_UNKNOWN)
3339 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003340 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003342 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 }
3344 }
3345 else
3346 strregname = get_vim_var_str(VV_REG);
3347
3348 if (error)
3349 return;
3350
3351 regname = (strregname == NULL ? '"' : *strregname);
3352 if (regname == 0)
3353 regname = '"';
3354
3355 if (return_list)
3356 {
3357 rettv->v_type = VAR_LIST;
3358 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3359 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3360 if (rettv->vval.v_list == NULL)
3361 (void)rettv_list_alloc(rettv);
3362 else
3363 ++rettv->vval.v_list->lv_refcount;
3364 }
3365 else
3366 {
3367 rettv->v_type = VAR_STRING;
3368 rettv->vval.v_string = get_reg_contents(regname,
3369 arg2 ? GREG_EXPR_SRC : 0);
3370 }
3371}
3372
3373/*
3374 * "getregtype()" function
3375 */
3376 static void
3377f_getregtype(typval_T *argvars, typval_T *rettv)
3378{
3379 char_u *strregname;
3380 int regname;
3381 char_u buf[NUMBUFLEN + 2];
3382 long reglen = 0;
3383
3384 if (argvars[0].v_type != VAR_UNKNOWN)
3385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003386 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003387 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003388 {
3389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = NULL;
3391 return;
3392 }
3393 }
3394 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003396 strregname = get_vim_var_str(VV_REG);
3397
3398 regname = (strregname == NULL ? '"' : *strregname);
3399 if (regname == 0)
3400 regname = '"';
3401
3402 buf[0] = NUL;
3403 buf[1] = NUL;
3404 switch (get_reg_type(regname, &reglen))
3405 {
3406 case MLINE: buf[0] = 'V'; break;
3407 case MCHAR: buf[0] = 'v'; break;
3408 case MBLOCK:
3409 buf[0] = Ctrl_V;
3410 sprintf((char *)buf + 1, "%ld", reglen + 1);
3411 break;
3412 }
3413 rettv->v_type = VAR_STRING;
3414 rettv->vval.v_string = vim_strsave(buf);
3415}
3416
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003417/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003418 * "gettagstack()" function
3419 */
3420 static void
3421f_gettagstack(typval_T *argvars, typval_T *rettv)
3422{
3423 win_T *wp = curwin; // default is current window
3424
3425 if (rettv_dict_alloc(rettv) != OK)
3426 return;
3427
3428 if (argvars[0].v_type != VAR_UNKNOWN)
3429 {
3430 wp = find_win_by_nr_or_id(&argvars[0]);
3431 if (wp == NULL)
3432 return;
3433 }
3434
3435 get_tagstack(wp, rettv->vval.v_dict);
3436}
3437
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003438/*
3439 * "gettext()" function
3440 */
3441 static void
3442f_gettext(typval_T *argvars, typval_T *rettv)
3443{
3444 if (argvars[0].v_type != VAR_STRING
3445 || argvars[0].vval.v_string == NULL
3446 || *argvars[0].vval.v_string == NUL)
3447 {
3448 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3449 }
3450 else
3451 {
3452 rettv->v_type = VAR_STRING;
3453 rettv->vval.v_string = vim_strsave(
3454 (char_u *)_(argvars[0].vval.v_string));
3455 }
3456}
3457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459#include "version.h"
3460
3461/*
3462 * "has()" function
3463 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003464 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465f_has(typval_T *argvars, typval_T *rettv)
3466{
3467 int i;
3468 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003469 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003471 typedef struct {
3472 char *name;
3473 short present;
3474 } has_item_T;
3475 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003476 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003477 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003479 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003480#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003481 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003482#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003483 },
3484 {"arp",
3485#if defined(AMIGA) && defined(FEAT_ARP)
3486 1
3487#else
3488 0
3489#endif
3490 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003491 {"haiku",
3492#ifdef __HAIKU__
3493 1
3494#else
3495 0
3496#endif
3497 },
3498 {"bsd",
3499#if defined(BSD) && !defined(MACOS_X)
3500 1
3501#else
3502 0
3503#endif
3504 },
3505 {"hpux",
3506#ifdef hpux
3507 1
3508#else
3509 0
3510#endif
3511 },
3512 {"linux",
3513#ifdef __linux__
3514 1
3515#else
3516 0
3517#endif
3518 },
3519 {"mac", // Mac OS X (and, once, Mac OS Classic)
3520#ifdef MACOS_X
3521 1
3522#else
3523 0
3524#endif
3525 },
3526 {"osx", // Mac OS X
3527#ifdef MACOS_X
3528 1
3529#else
3530 0
3531#endif
3532 },
3533 {"macunix", // Mac OS X, with the darwin feature
3534#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3535 1
3536#else
3537 0
3538#endif
3539 },
3540 {"osxdarwin", // synonym for macunix
3541#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3542 1
3543#else
3544 0
3545#endif
3546 },
3547 {"qnx",
3548#ifdef __QNX__
3549 1
3550#else
3551 0
3552#endif
3553 },
3554 {"sun",
3555#ifdef SUN_SYSTEM
3556 1
3557#else
3558 0
3559#endif
3560 },
3561 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003563 1
3564#else
3565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003567 },
3568 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003570 1
3571#else
3572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003574 },
3575 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003576#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 1
3578#else
3579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003581 },
3582 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003583#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003584 1
3585#else
3586 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003588 },
3589 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003590#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003591 1
3592#else
3593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 },
3596 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003598 1
3599#else
3600 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003602 },
3603 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003605 1
3606#else
3607 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003609 },
3610 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003612 1
3613#else
3614 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003616 },
3617 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003619 1
3620#else
3621 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003623 },
3624 {"autocmd", 1},
3625 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003626#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003627 1
3628#else
3629 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003630#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003631 },
3632 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003633#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003634 1
3635#else
3636 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003637#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003638 },
3639 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003640#ifdef FEAT_BEVAL_GUI
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 {"balloon_multiline",
3647#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3648 // MS-Windows requires runtime check, see below
3649 1
3650#else
3651 0
3652#endif
3653 },
3654 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003655#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003656 1
3657#else
3658 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003659#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003660 },
3661 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
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 {"all_builtin_terms",
3669#if defined(ALL_BUILTIN_TCAPS)
3670 1
3671#else
3672 0
3673#endif
3674 },
3675 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003677 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003679 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003681 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003683 },
3684 {"byte_offset",
3685#ifdef FEAT_BYTEOFF
3686 1
3687#else
3688 0
3689#endif
3690 },
3691 {"channel",
3692#ifdef FEAT_JOB_CHANNEL
3693 1
3694#else
3695 0
3696#endif
3697 },
3698 {"cindent",
3699#ifdef FEAT_CINDENT
3700 1
3701#else
3702 0
3703#endif
3704 },
3705 {"clientserver",
3706#ifdef FEAT_CLIENTSERVER
3707 1
3708#else
3709 0
3710#endif
3711 },
3712 {"clipboard",
3713#ifdef FEAT_CLIPBOARD
3714 1
3715#else
3716 0
3717#endif
3718 },
3719 {"cmdline_compl", 1},
3720 {"cmdline_hist", 1},
3721 {"comments", 1},
3722 {"conceal",
3723#ifdef FEAT_CONCEAL
3724 1
3725#else
3726 0
3727#endif
3728 },
3729 {"cryptv",
3730#ifdef FEAT_CRYPT
3731 1
3732#else
3733 0
3734#endif
3735 },
3736 {"crypt-blowfish",
3737#ifdef FEAT_CRYPT
3738 1
3739#else
3740 0
3741#endif
3742 },
3743 {"crypt-blowfish2",
3744#ifdef FEAT_CRYPT
3745 1
3746#else
3747 0
3748#endif
3749 },
3750 {"cscope",
3751#ifdef FEAT_CSCOPE
3752 1
3753#else
3754 0
3755#endif
3756 },
3757 {"cursorbind", 1},
3758 {"cursorshape",
3759#ifdef CURSOR_SHAPE
3760 1
3761#else
3762 0
3763#endif
3764 },
3765 {"debug",
3766#ifdef DEBUG
3767 1
3768#else
3769 0
3770#endif
3771 },
3772 {"dialog_con",
3773#ifdef FEAT_CON_DIALOG
3774 1
3775#else
3776 0
3777#endif
3778 },
3779 {"dialog_gui",
3780#ifdef FEAT_GUI_DIALOG
3781 1
3782#else
3783 0
3784#endif
3785 },
3786 {"diff",
3787#ifdef FEAT_DIFF
3788 1
3789#else
3790 0
3791#endif
3792 },
3793 {"digraphs",
3794#ifdef FEAT_DIGRAPHS
3795 1
3796#else
3797 0
3798#endif
3799 },
3800 {"directx",
3801#ifdef FEAT_DIRECTX
3802 1
3803#else
3804 0
3805#endif
3806 },
3807 {"dnd",
3808#ifdef FEAT_DND
3809 1
3810#else
3811 0
3812#endif
3813 },
3814 {"emacs_tags",
3815#ifdef FEAT_EMACS_TAGS
3816 1
3817#else
3818 0
3819#endif
3820 },
3821 {"eval", 1}, // always present, of course!
3822 {"ex_extra", 1}, // graduated feature
3823 {"extra_search",
3824#ifdef FEAT_SEARCH_EXTRA
3825 1
3826#else
3827 0
3828#endif
3829 },
3830 {"file_in_path",
3831#ifdef FEAT_SEARCHPATH
3832 1
3833#else
3834 0
3835#endif
3836 },
3837 {"filterpipe",
3838#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3839 1
3840#else
3841 0
3842#endif
3843 },
3844 {"find_in_path",
3845#ifdef FEAT_FIND_ID
3846 1
3847#else
3848 0
3849#endif
3850 },
3851 {"float",
3852#ifdef FEAT_FLOAT
3853 1
3854#else
3855 0
3856#endif
3857 },
3858 {"folding",
3859#ifdef FEAT_FOLDING
3860 1
3861#else
3862 0
3863#endif
3864 },
3865 {"footer",
3866#ifdef FEAT_FOOTER
3867 1
3868#else
3869 0
3870#endif
3871 },
3872 {"fork",
3873#if !defined(USE_SYSTEM) && defined(UNIX)
3874 1
3875#else
3876 0
3877#endif
3878 },
3879 {"gettext",
3880#ifdef FEAT_GETTEXT
3881 1
3882#else
3883 0
3884#endif
3885 },
3886 {"gui",
3887#ifdef FEAT_GUI
3888 1
3889#else
3890 0
3891#endif
3892 },
3893 {"gui_neXtaw",
3894#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3895 1
3896#else
3897 0
3898#endif
3899 },
3900 {"gui_athena",
3901#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3902 1
3903#else
3904 0
3905#endif
3906 },
3907 {"gui_gtk",
3908#ifdef FEAT_GUI_GTK
3909 1
3910#else
3911 0
3912#endif
3913 },
3914 {"gui_gtk2",
3915#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3916 1
3917#else
3918 0
3919#endif
3920 },
3921 {"gui_gtk3",
3922#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3923 1
3924#else
3925 0
3926#endif
3927 },
3928 {"gui_gnome",
3929#ifdef FEAT_GUI_GNOME
3930 1
3931#else
3932 0
3933#endif
3934 },
3935 {"gui_haiku",
3936#ifdef FEAT_GUI_HAIKU
3937 1
3938#else
3939 0
3940#endif
3941 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003942 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003943 {"gui_motif",
3944#ifdef FEAT_GUI_MOTIF
3945 1
3946#else
3947 0
3948#endif
3949 },
3950 {"gui_photon",
3951#ifdef FEAT_GUI_PHOTON
3952 1
3953#else
3954 0
3955#endif
3956 },
3957 {"gui_win32",
3958#ifdef FEAT_GUI_MSWIN
3959 1
3960#else
3961 0
3962#endif
3963 },
3964 {"iconv",
3965#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3966 1
3967#else
3968 0
3969#endif
3970 },
3971 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003972 {"ipv6",
3973#ifdef FEAT_IPV6
3974 1
3975#else
3976 0
3977#endif
3978 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003979 {"job",
3980#ifdef FEAT_JOB_CHANNEL
3981 1
3982#else
3983 0
3984#endif
3985 },
3986 {"jumplist",
3987#ifdef FEAT_JUMPLIST
3988 1
3989#else
3990 0
3991#endif
3992 },
3993 {"keymap",
3994#ifdef FEAT_KEYMAP
3995 1
3996#else
3997 0
3998#endif
3999 },
4000 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4001 {"langmap",
4002#ifdef FEAT_LANGMAP
4003 1
4004#else
4005 0
4006#endif
4007 },
4008 {"libcall",
4009#ifdef FEAT_LIBCALL
4010 1
4011#else
4012 0
4013#endif
4014 },
4015 {"linebreak",
4016#ifdef FEAT_LINEBREAK
4017 1
4018#else
4019 0
4020#endif
4021 },
4022 {"lispindent",
4023#ifdef FEAT_LISP
4024 1
4025#else
4026 0
4027#endif
4028 },
4029 {"listcmds", 1},
4030 {"localmap", 1},
4031 {"lua",
4032#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4033 1
4034#else
4035 0
4036#endif
4037 },
4038 {"menu",
4039#ifdef FEAT_MENU
4040 1
4041#else
4042 0
4043#endif
4044 },
4045 {"mksession",
4046#ifdef FEAT_SESSION
4047 1
4048#else
4049 0
4050#endif
4051 },
4052 {"modify_fname", 1},
4053 {"mouse", 1},
4054 {"mouseshape",
4055#ifdef FEAT_MOUSESHAPE
4056 1
4057#else
4058 0
4059#endif
4060 },
4061 {"mouse_dec",
4062#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4063 1
4064#else
4065 0
4066#endif
4067 },
4068 {"mouse_gpm",
4069#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4070 1
4071#else
4072 0
4073#endif
4074 },
4075 {"mouse_jsbterm",
4076#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4077 1
4078#else
4079 0
4080#endif
4081 },
4082 {"mouse_netterm",
4083#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4084 1
4085#else
4086 0
4087#endif
4088 },
4089 {"mouse_pterm",
4090#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4091 1
4092#else
4093 0
4094#endif
4095 },
4096 {"mouse_sgr",
4097#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4098 1
4099#else
4100 0
4101#endif
4102 },
4103 {"mouse_sysmouse",
4104#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4105 1
4106#else
4107 0
4108#endif
4109 },
4110 {"mouse_urxvt",
4111#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4112 1
4113#else
4114 0
4115#endif
4116 },
4117 {"mouse_xterm",
4118#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4119 1
4120#else
4121 0
4122#endif
4123 },
4124 {"multi_byte", 1},
4125 {"multi_byte_ime",
4126#ifdef FEAT_MBYTE_IME
4127 1
4128#else
4129 0
4130#endif
4131 },
4132 {"multi_lang",
4133#ifdef FEAT_MULTI_LANG
4134 1
4135#else
4136 0
4137#endif
4138 },
4139 {"mzscheme",
4140#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4141 1
4142#else
4143 0
4144#endif
4145 },
4146 {"num64", 1},
4147 {"ole",
4148#ifdef FEAT_OLE
4149 1
4150#else
4151 0
4152#endif
4153 },
4154 {"packages",
4155#ifdef FEAT_EVAL
4156 1
4157#else
4158 0
4159#endif
4160 },
4161 {"path_extra",
4162#ifdef FEAT_PATH_EXTRA
4163 1
4164#else
4165 0
4166#endif
4167 },
4168 {"perl",
4169#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4170 1
4171#else
4172 0
4173#endif
4174 },
4175 {"persistent_undo",
4176#ifdef FEAT_PERSISTENT_UNDO
4177 1
4178#else
4179 0
4180#endif
4181 },
4182 {"python_compiled",
4183#if defined(FEAT_PYTHON)
4184 1
4185#else
4186 0
4187#endif
4188 },
4189 {"python_dynamic",
4190#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4191 1
4192#else
4193 0
4194#endif
4195 },
4196 {"python",
4197#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4198 1
4199#else
4200 0
4201#endif
4202 },
4203 {"pythonx",
4204#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4205 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4206 1
4207#else
4208 0
4209#endif
4210 },
4211 {"python3_compiled",
4212#if defined(FEAT_PYTHON3)
4213 1
4214#else
4215 0
4216#endif
4217 },
4218 {"python3_dynamic",
4219#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4220 1
4221#else
4222 0
4223#endif
4224 },
4225 {"python3",
4226#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4227 1
4228#else
4229 0
4230#endif
4231 },
4232 {"popupwin",
4233#ifdef FEAT_PROP_POPUP
4234 1
4235#else
4236 0
4237#endif
4238 },
4239 {"postscript",
4240#ifdef FEAT_POSTSCRIPT
4241 1
4242#else
4243 0
4244#endif
4245 },
4246 {"printer",
4247#ifdef FEAT_PRINTER
4248 1
4249#else
4250 0
4251#endif
4252 },
4253 {"profile",
4254#ifdef FEAT_PROFILE
4255 1
4256#else
4257 0
4258#endif
4259 },
4260 {"reltime",
4261#ifdef FEAT_RELTIME
4262 1
4263#else
4264 0
4265#endif
4266 },
4267 {"quickfix",
4268#ifdef FEAT_QUICKFIX
4269 1
4270#else
4271 0
4272#endif
4273 },
4274 {"rightleft",
4275#ifdef FEAT_RIGHTLEFT
4276 1
4277#else
4278 0
4279#endif
4280 },
4281 {"ruby",
4282#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4283 1
4284#else
4285 0
4286#endif
4287 },
4288 {"scrollbind", 1},
4289 {"showcmd",
4290#ifdef FEAT_CMDL_INFO
4291 1
4292#else
4293 0
4294#endif
4295 },
4296 {"cmdline_info",
4297#ifdef FEAT_CMDL_INFO
4298 1
4299#else
4300 0
4301#endif
4302 },
4303 {"signs",
4304#ifdef FEAT_SIGNS
4305 1
4306#else
4307 0
4308#endif
4309 },
4310 {"smartindent",
4311#ifdef FEAT_SMARTINDENT
4312 1
4313#else
4314 0
4315#endif
4316 },
4317 {"startuptime",
4318#ifdef STARTUPTIME
4319 1
4320#else
4321 0
4322#endif
4323 },
4324 {"statusline",
4325#ifdef FEAT_STL_OPT
4326 1
4327#else
4328 0
4329#endif
4330 },
4331 {"netbeans_intg",
4332#ifdef FEAT_NETBEANS_INTG
4333 1
4334#else
4335 0
4336#endif
4337 },
4338 {"sound",
4339#ifdef FEAT_SOUND
4340 1
4341#else
4342 0
4343#endif
4344 },
4345 {"spell",
4346#ifdef FEAT_SPELL
4347 1
4348#else
4349 0
4350#endif
4351 },
4352 {"syntax",
4353#ifdef FEAT_SYN_HL
4354 1
4355#else
4356 0
4357#endif
4358 },
4359 {"system",
4360#if defined(USE_SYSTEM) || !defined(UNIX)
4361 1
4362#else
4363 0
4364#endif
4365 },
4366 {"tag_binary",
4367#ifdef FEAT_TAG_BINS
4368 1
4369#else
4370 0
4371#endif
4372 },
4373 {"tcl",
4374#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4375 1
4376#else
4377 0
4378#endif
4379 },
4380 {"termguicolors",
4381#ifdef FEAT_TERMGUICOLORS
4382 1
4383#else
4384 0
4385#endif
4386 },
4387 {"terminal",
4388#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4389 1
4390#else
4391 0
4392#endif
4393 },
4394 {"terminfo",
4395#ifdef TERMINFO
4396 1
4397#else
4398 0
4399#endif
4400 },
4401 {"termresponse",
4402#ifdef FEAT_TERMRESPONSE
4403 1
4404#else
4405 0
4406#endif
4407 },
4408 {"textobjects",
4409#ifdef FEAT_TEXTOBJ
4410 1
4411#else
4412 0
4413#endif
4414 },
4415 {"textprop",
4416#ifdef FEAT_PROP_POPUP
4417 1
4418#else
4419 0
4420#endif
4421 },
4422 {"tgetent",
4423#ifdef HAVE_TGETENT
4424 1
4425#else
4426 0
4427#endif
4428 },
4429 {"timers",
4430#ifdef FEAT_TIMERS
4431 1
4432#else
4433 0
4434#endif
4435 },
4436 {"title",
4437#ifdef FEAT_TITLE
4438 1
4439#else
4440 0
4441#endif
4442 },
4443 {"toolbar",
4444#ifdef FEAT_TOOLBAR
4445 1
4446#else
4447 0
4448#endif
4449 },
4450 {"unnamedplus",
4451#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4452 1
4453#else
4454 0
4455#endif
4456 },
4457 {"user-commands", 1}, // was accidentally included in 5.4
4458 {"user_commands", 1},
4459 {"vartabs",
4460#ifdef FEAT_VARTABS
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"vertsplit", 1},
4467 {"viminfo",
4468#ifdef FEAT_VIMINFO
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"vimscript-1", 1},
4475 {"vimscript-2", 1},
4476 {"vimscript-3", 1},
4477 {"vimscript-4", 1},
4478 {"virtualedit", 1},
4479 {"visual", 1},
4480 {"visualextra", 1},
4481 {"vreplace", 1},
4482 {"vtp",
4483#ifdef FEAT_VTP
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"wildignore",
4490#ifdef FEAT_WILDIGN
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"wildmenu",
4497#ifdef FEAT_WILDMENU
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"windows", 1},
4504 {"winaltkeys",
4505#ifdef FEAT_WAK
4506 1
4507#else
4508 0
4509#endif
4510 },
4511 {"writebackup",
4512#ifdef FEAT_WRITEBACKUP
4513 1
4514#else
4515 0
4516#endif
4517 },
4518 {"xim",
4519#ifdef FEAT_XIM
4520 1
4521#else
4522 0
4523#endif
4524 },
4525 {"xfontset",
4526#ifdef FEAT_XFONTSET
4527 1
4528#else
4529 0
4530#endif
4531 },
4532 {"xpm",
4533#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4534 1
4535#else
4536 0
4537#endif
4538 },
4539 {"xpm_w32", // for backward compatibility
4540#ifdef FEAT_XPM_W32
4541 1
4542#else
4543 0
4544#endif
4545 },
4546 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004548 1
4549#else
4550 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 },
4553 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 1
4556#else
4557 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004558#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004559 },
4560 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004561#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 1
4563#else
4564 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 },
4567 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 1
4570#else
4571 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 },
4574 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 1
4577#else
4578 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 },
4581 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582 };
4583
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004584 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004585 for (i = 0; has_list[i].name != NULL; ++i)
4586 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004588 x = TRUE;
4589 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590 break;
4591 }
4592
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 // features also in has_list[] but sometimes enabled at runtime
4594 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004596 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004597 {
4598 // intentionally empty
4599 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004600#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601 else if (STRICMP(name, "balloon_multiline") == 0)
4602 n = multiline_balloon_available();
4603#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004604#ifdef VIMDLL
4605 else if (STRICMP(name, "filterpipe") == 0)
4606 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004607#endif
4608#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4609 else if (STRICMP(name, "iconv") == 0)
4610 n = iconv_enabled(FALSE);
4611#endif
4612#ifdef DYNAMIC_LUA
4613 else if (STRICMP(name, "lua") == 0)
4614 n = lua_enabled(FALSE);
4615#endif
4616#ifdef DYNAMIC_MZSCHEME
4617 else if (STRICMP(name, "mzscheme") == 0)
4618 n = mzscheme_enabled(FALSE);
4619#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004620#ifdef DYNAMIC_PERL
4621 else if (STRICMP(name, "perl") == 0)
4622 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624#ifdef DYNAMIC_PYTHON
4625 else if (STRICMP(name, "python") == 0)
4626 n = python_enabled(FALSE);
4627#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#ifdef DYNAMIC_PYTHON3
4629 else if (STRICMP(name, "python3") == 0)
4630 n = python3_enabled(FALSE);
4631#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004632#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4633 else if (STRICMP(name, "pythonx") == 0)
4634 {
4635# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4636 if (p_pyx == 0)
4637 n = python3_enabled(FALSE) || python_enabled(FALSE);
4638 else if (p_pyx == 3)
4639 n = python3_enabled(FALSE);
4640 else if (p_pyx == 2)
4641 n = python_enabled(FALSE);
4642# elif defined(DYNAMIC_PYTHON)
4643 n = python_enabled(FALSE);
4644# elif defined(DYNAMIC_PYTHON3)
4645 n = python3_enabled(FALSE);
4646# endif
4647 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004648#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004649#ifdef DYNAMIC_RUBY
4650 else if (STRICMP(name, "ruby") == 0)
4651 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004653#ifdef DYNAMIC_TCL
4654 else if (STRICMP(name, "tcl") == 0)
4655 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004656#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004657#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004658 else if (STRICMP(name, "terminal") == 0)
4659 n = terminal_enabled();
4660#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661 }
4662
Bram Moolenaar79296512020-03-22 16:17:14 +01004663 // features not in has_list[]
4664 if (x == FALSE)
4665 {
4666 if (STRNICMP(name, "patch", 5) == 0)
4667 {
4668 x = TRUE;
4669 if (name[5] == '-'
4670 && STRLEN(name) >= 11
4671 && vim_isdigit(name[6])
4672 && vim_isdigit(name[8])
4673 && vim_isdigit(name[10]))
4674 {
4675 int major = atoi((char *)name + 6);
4676 int minor = atoi((char *)name + 8);
4677
4678 // Expect "patch-9.9.01234".
4679 n = (major < VIM_VERSION_MAJOR
4680 || (major == VIM_VERSION_MAJOR
4681 && (minor < VIM_VERSION_MINOR
4682 || (minor == VIM_VERSION_MINOR
4683 && has_patch(atoi((char *)name + 10))))));
4684 }
4685 else
4686 n = has_patch(atoi((char *)name + 5));
4687 }
4688 else if (STRICMP(name, "vim_starting") == 0)
4689 {
4690 x = TRUE;
4691 n = (starting != 0);
4692 }
4693 else if (STRICMP(name, "ttyin") == 0)
4694 {
4695 x = TRUE;
4696 n = mch_input_isatty();
4697 }
4698 else if (STRICMP(name, "ttyout") == 0)
4699 {
4700 x = TRUE;
4701 n = stdout_isatty;
4702 }
4703 else if (STRICMP(name, "multi_byte_encoding") == 0)
4704 {
4705 x = TRUE;
4706 n = has_mbyte;
4707 }
4708 else if (STRICMP(name, "gui_running") == 0)
4709 {
4710 x = TRUE;
4711#ifdef FEAT_GUI
4712 n = (gui.in_use || gui.starting);
4713#endif
4714 }
4715 else if (STRICMP(name, "browse") == 0)
4716 {
4717 x = TRUE;
4718#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4719 n = gui.in_use; // gui_mch_browse() works when GUI is running
4720#endif
4721 }
4722 else if (STRICMP(name, "syntax_items") == 0)
4723 {
4724 x = TRUE;
4725#ifdef FEAT_SYN_HL
4726 n = syntax_present(curwin);
4727#endif
4728 }
4729 else if (STRICMP(name, "vcon") == 0)
4730 {
4731 x = TRUE;
4732#ifdef FEAT_VTP
4733 n = is_term_win32() && has_vtp_working();
4734#endif
4735 }
4736 else if (STRICMP(name, "netbeans_enabled") == 0)
4737 {
4738 x = TRUE;
4739#ifdef FEAT_NETBEANS_INTG
4740 n = netbeans_active();
4741#endif
4742 }
4743 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4744 {
4745 x = TRUE;
4746#ifdef FEAT_MOUSE_GPM
4747 n = gpm_enabled();
4748#endif
4749 }
4750 else if (STRICMP(name, "conpty") == 0)
4751 {
4752 x = TRUE;
4753#if defined(FEAT_TERMINAL) && defined(MSWIN)
4754 n = use_conpty();
4755#endif
4756 }
4757 else if (STRICMP(name, "clipboard_working") == 0)
4758 {
4759 x = TRUE;
4760#ifdef FEAT_CLIPBOARD
4761 n = clip_star.available;
4762#endif
4763 }
4764 }
4765
Bram Moolenaar04637e22020-09-05 18:45:29 +02004766 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01004767 // return whether feature could ever be enabled
4768 rettv->vval.v_number = x;
4769 else
4770 // return whether feature is enabled
4771 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004772}
4773
4774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 * "haslocaldir()" function
4776 */
4777 static void
4778f_haslocaldir(typval_T *argvars, typval_T *rettv)
4779{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004780 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004781 win_T *wp = NULL;
4782
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004783 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4784
4785 // Check for window-local and tab-local directories
4786 if (wp != NULL && wp->w_localdir != NULL)
4787 rettv->vval.v_number = 1;
4788 else if (tp != NULL && tp->tp_localdir != NULL)
4789 rettv->vval.v_number = 2;
4790 else
4791 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792}
4793
4794/*
4795 * "hasmapto()" function
4796 */
4797 static void
4798f_hasmapto(typval_T *argvars, typval_T *rettv)
4799{
4800 char_u *name;
4801 char_u *mode;
4802 char_u buf[NUMBUFLEN];
4803 int abbr = FALSE;
4804
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004806 if (argvars[1].v_type == VAR_UNKNOWN)
4807 mode = (char_u *)"nvo";
4808 else
4809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02004812 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004813 }
4814
4815 if (map_to_exists(name, mode, abbr))
4816 rettv->vval.v_number = TRUE;
4817 else
4818 rettv->vval.v_number = FALSE;
4819}
4820
4821/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004822 * "highlightID(name)" function
4823 */
4824 static void
4825f_hlID(typval_T *argvars, typval_T *rettv)
4826{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004827 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828}
4829
4830/*
4831 * "highlight_exists()" function
4832 */
4833 static void
4834f_hlexists(typval_T *argvars, typval_T *rettv)
4835{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837}
4838
4839/*
4840 * "hostname()" function
4841 */
4842 static void
4843f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4844{
4845 char_u hostname[256];
4846
4847 mch_get_host_name(hostname, 256);
4848 rettv->v_type = VAR_STRING;
4849 rettv->vval.v_string = vim_strsave(hostname);
4850}
4851
4852/*
4853 * iconv() function
4854 */
4855 static void
4856f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4857{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004858 char_u buf1[NUMBUFLEN];
4859 char_u buf2[NUMBUFLEN];
4860 char_u *from, *to, *str;
4861 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862
4863 rettv->v_type = VAR_STRING;
4864 rettv->vval.v_string = NULL;
4865
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004866 str = tv_get_string(&argvars[0]);
4867 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4868 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004869 vimconv.vc_type = CONV_NONE;
4870 convert_setup(&vimconv, from, to);
4871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 if (vimconv.vc_type == CONV_NONE)
4874 rettv->vval.v_string = vim_strsave(str);
4875 else
4876 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4877
4878 convert_setup(&vimconv, NULL, NULL);
4879 vim_free(from);
4880 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881}
4882
4883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884 * "index()" function
4885 */
4886 static void
4887f_index(typval_T *argvars, typval_T *rettv)
4888{
4889 list_T *l;
4890 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004891 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 long idx = 0;
4893 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004894 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895
4896 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004897 if (argvars[0].v_type == VAR_BLOB)
4898 {
4899 typval_T tv;
4900 int start = 0;
4901
4902 if (argvars[2].v_type != VAR_UNKNOWN)
4903 {
4904 start = tv_get_number_chk(&argvars[2], &error);
4905 if (error)
4906 return;
4907 }
4908 b = argvars[0].vval.v_blob;
4909 if (b == NULL)
4910 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004911 if (start < 0)
4912 {
4913 start = blob_len(b) + start;
4914 if (start < 0)
4915 start = 0;
4916 }
4917
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004918 for (idx = start; idx < blob_len(b); ++idx)
4919 {
4920 tv.v_type = VAR_NUMBER;
4921 tv.vval.v_number = blob_get(b, idx);
4922 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4923 {
4924 rettv->vval.v_number = idx;
4925 return;
4926 }
4927 }
4928 return;
4929 }
4930 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004932 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 return;
4934 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 l = argvars[0].vval.v_list;
4937 if (l != NULL)
4938 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004939 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 item = l->lv_first;
4941 if (argvars[2].v_type != VAR_UNKNOWN)
4942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // Start at specified item. Use the cached index that list_find()
4944 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004945 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004946 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02004948 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004949 if (error)
4950 item = NULL;
4951 }
4952
4953 for ( ; item != NULL; item = item->li_next, ++idx)
4954 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4955 {
4956 rettv->vval.v_number = idx;
4957 break;
4958 }
4959 }
4960}
4961
4962static int inputsecret_flag = 0;
4963
4964/*
4965 * "input()" function
4966 * Also handles inputsecret() when inputsecret is set.
4967 */
4968 static void
4969f_input(typval_T *argvars, typval_T *rettv)
4970{
4971 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4972}
4973
4974/*
4975 * "inputdialog()" function
4976 */
4977 static void
4978f_inputdialog(typval_T *argvars, typval_T *rettv)
4979{
4980#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004981 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004982 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4983 {
4984 char_u *message;
4985 char_u buf[NUMBUFLEN];
4986 char_u *defstr = (char_u *)"";
4987
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004988 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004990 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4992 else
4993 IObuff[0] = NUL;
4994 if (message != NULL && defstr != NULL
4995 && do_dialog(VIM_QUESTION, NULL, message,
4996 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4997 rettv->vval.v_string = vim_strsave(IObuff);
4998 else
4999 {
5000 if (message != NULL && defstr != NULL
5001 && argvars[1].v_type != VAR_UNKNOWN
5002 && argvars[2].v_type != VAR_UNKNOWN)
5003 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005005 else
5006 rettv->vval.v_string = NULL;
5007 }
5008 rettv->v_type = VAR_STRING;
5009 }
5010 else
5011#endif
5012 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5013}
5014
5015/*
5016 * "inputlist()" function
5017 */
5018 static void
5019f_inputlist(typval_T *argvars, typval_T *rettv)
5020{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005021 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 listitem_T *li;
5023 int selected;
5024 int mouse_used;
5025
5026#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // While starting up, there is no place to enter text. When running tests
5028 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005029 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030 return;
5031#endif
5032 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005034 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 return;
5036 }
5037
5038 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 msg_row = Rows - 1; // for when 'cmdheight' > 1
5040 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041 msg_scroll = TRUE;
5042 msg_clr_eos();
5043
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005044 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005045 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005046 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005048 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005049 msg_putchar('\n');
5050 }
5051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005052 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005053 selected = prompt_for_number(&mouse_used);
5054 if (mouse_used)
5055 selected -= lines_left;
5056
5057 rettv->vval.v_number = selected;
5058}
5059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5061
5062/*
5063 * "inputrestore()" function
5064 */
5065 static void
5066f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5067{
5068 if (ga_userinput.ga_len > 0)
5069 {
5070 --ga_userinput.ga_len;
5071 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5072 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005074 }
5075 else if (p_verbose > 1)
5076 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005077 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 }
5080}
5081
5082/*
5083 * "inputsave()" function
5084 */
5085 static void
5086f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5087{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089 if (ga_grow(&ga_userinput, 1) == OK)
5090 {
5091 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5092 + ga_userinput.ga_len);
5093 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 }
5096 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005097 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098}
5099
5100/*
5101 * "inputsecret()" function
5102 */
5103 static void
5104f_inputsecret(typval_T *argvars, typval_T *rettv)
5105{
5106 ++cmdline_star;
5107 ++inputsecret_flag;
5108 f_input(argvars, rettv);
5109 --cmdline_star;
5110 --inputsecret_flag;
5111}
5112
5113/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005114 * "interrupt()" function
5115 */
5116 static void
5117f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5118{
5119 got_int = TRUE;
5120}
5121
5122/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 * "invert(expr)" function
5124 */
5125 static void
5126f_invert(typval_T *argvars, typval_T *rettv)
5127{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005128 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005129}
5130
5131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132 * "islocked()" function
5133 */
5134 static void
5135f_islocked(typval_T *argvars, typval_T *rettv)
5136{
5137 lval_T lv;
5138 char_u *end;
5139 dictitem_T *di;
5140
5141 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005142 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005143 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 if (end != NULL && lv.ll_name != NULL)
5145 {
5146 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005147 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 else
5149 {
5150 if (lv.ll_tv == NULL)
5151 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005152 di = find_var(lv.ll_name, NULL, TRUE);
5153 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // Consider a variable locked when:
5156 // 1. the variable itself is locked
5157 // 2. the value of the variable is locked.
5158 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005159 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5160 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 }
5162 }
5163 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005164 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005166 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005168 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5170 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5173 }
5174 }
5175
5176 clear_lval(&lv);
5177}
5178
5179#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5180/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005181 * "isinf()" function
5182 */
5183 static void
5184f_isinf(typval_T *argvars, typval_T *rettv)
5185{
5186 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5187 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5188}
5189
5190/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005191 * "isnan()" function
5192 */
5193 static void
5194f_isnan(typval_T *argvars, typval_T *rettv)
5195{
5196 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5197 && isnan(argvars[0].vval.v_float);
5198}
5199#endif
5200
5201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 * "last_buffer_nr()" function.
5203 */
5204 static void
5205f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5206{
5207 int n = 0;
5208 buf_T *buf;
5209
Bram Moolenaar29323592016-07-24 22:04:11 +02005210 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211 if (n < buf->b_fnum)
5212 n = buf->b_fnum;
5213
5214 rettv->vval.v_number = n;
5215}
5216
5217/*
5218 * "len()" function
5219 */
5220 static void
5221f_len(typval_T *argvars, typval_T *rettv)
5222{
5223 switch (argvars[0].v_type)
5224 {
5225 case VAR_STRING:
5226 case VAR_NUMBER:
5227 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005230 case VAR_BLOB:
5231 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5232 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 case VAR_LIST:
5234 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5235 break;
5236 case VAR_DICT:
5237 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5238 break;
5239 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005240 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005242 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243 case VAR_SPECIAL:
5244 case VAR_FLOAT:
5245 case VAR_FUNC:
5246 case VAR_PARTIAL:
5247 case VAR_JOB:
5248 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005249 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005250 break;
5251 }
5252}
5253
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005255libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256{
5257#ifdef FEAT_LIBCALL
5258 char_u *string_in;
5259 char_u **string_result;
5260 int nr_result;
5261#endif
5262
5263 rettv->v_type = type;
5264 if (type != VAR_NUMBER)
5265 rettv->vval.v_string = NULL;
5266
5267 if (check_restricted() || check_secure())
5268 return;
5269
5270#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5273 {
5274 string_in = NULL;
5275 if (argvars[2].v_type == VAR_STRING)
5276 string_in = argvars[2].vval.v_string;
5277 if (type == VAR_NUMBER)
5278 string_result = NULL;
5279 else
5280 string_result = &rettv->vval.v_string;
5281 if (mch_libcall(argvars[0].vval.v_string,
5282 argvars[1].vval.v_string,
5283 string_in,
5284 argvars[2].vval.v_number,
5285 string_result,
5286 &nr_result) == OK
5287 && type == VAR_NUMBER)
5288 rettv->vval.v_number = nr_result;
5289 }
5290#endif
5291}
5292
5293/*
5294 * "libcall()" function
5295 */
5296 static void
5297f_libcall(typval_T *argvars, typval_T *rettv)
5298{
5299 libcall_common(argvars, rettv, VAR_STRING);
5300}
5301
5302/*
5303 * "libcallnr()" function
5304 */
5305 static void
5306f_libcallnr(typval_T *argvars, typval_T *rettv)
5307{
5308 libcall_common(argvars, rettv, VAR_NUMBER);
5309}
5310
5311/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005312 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 */
5314 static void
5315f_line(typval_T *argvars, typval_T *rettv)
5316{
5317 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005318 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005320 int id;
5321 tabpage_T *tp;
5322 win_T *wp;
5323 win_T *save_curwin;
5324 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005326 if (argvars[1].v_type != VAR_UNKNOWN)
5327 {
5328 // use window specified in the second argument
5329 id = (int)tv_get_number(&argvars[1]);
5330 wp = win_id2wp_tp(id, &tp);
5331 if (wp != NULL && tp != NULL)
5332 {
5333 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5334 == OK)
5335 {
5336 check_cursor();
5337 fp = var2fpos(&argvars[0], TRUE, &fnum);
5338 }
5339 restore_win_noblock(save_curwin, save_curtab, TRUE);
5340 }
5341 }
5342 else
5343 // use current window
5344 fp = var2fpos(&argvars[0], TRUE, &fnum);
5345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005346 if (fp != NULL)
5347 lnum = fp->lnum;
5348 rettv->vval.v_number = lnum;
5349}
5350
5351/*
5352 * "line2byte(lnum)" function
5353 */
5354 static void
5355f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5356{
5357#ifndef FEAT_BYTEOFF
5358 rettv->vval.v_number = -1;
5359#else
5360 linenr_T lnum;
5361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005362 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5364 rettv->vval.v_number = -1;
5365 else
5366 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5367 if (rettv->vval.v_number >= 0)
5368 ++rettv->vval.v_number;
5369#endif
5370}
5371
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372#ifdef FEAT_FLOAT
5373/*
5374 * "log()" function
5375 */
5376 static void
5377f_log(typval_T *argvars, typval_T *rettv)
5378{
5379 float_T f = 0.0;
5380
5381 rettv->v_type = VAR_FLOAT;
5382 if (get_float_arg(argvars, &f) == OK)
5383 rettv->vval.v_float = log(f);
5384 else
5385 rettv->vval.v_float = 0.0;
5386}
5387
5388/*
5389 * "log10()" function
5390 */
5391 static void
5392f_log10(typval_T *argvars, typval_T *rettv)
5393{
5394 float_T f = 0.0;
5395
5396 rettv->v_type = VAR_FLOAT;
5397 if (get_float_arg(argvars, &f) == OK)
5398 rettv->vval.v_float = log10(f);
5399 else
5400 rettv->vval.v_float = 0.0;
5401}
5402#endif
5403
5404#ifdef FEAT_LUA
5405/*
5406 * "luaeval()" function
5407 */
5408 static void
5409f_luaeval(typval_T *argvars, typval_T *rettv)
5410{
5411 char_u *str;
5412 char_u buf[NUMBUFLEN];
5413
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005414 if (check_restricted() || check_secure())
5415 return;
5416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005417 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418 do_luaeval(str, argvars + 1, rettv);
5419}
5420#endif
5421
5422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423 * "maparg()" function
5424 */
5425 static void
5426f_maparg(typval_T *argvars, typval_T *rettv)
5427{
5428 get_maparg(argvars, rettv, TRUE);
5429}
5430
5431/*
5432 * "mapcheck()" function
5433 */
5434 static void
5435f_mapcheck(typval_T *argvars, typval_T *rettv)
5436{
5437 get_maparg(argvars, rettv, FALSE);
5438}
5439
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005440typedef enum
5441{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005442 MATCH_END, // matchend()
5443 MATCH_MATCH, // match()
5444 MATCH_STR, // matchstr()
5445 MATCH_LIST, // matchlist()
5446 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005447} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005448
5449 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005450find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451{
5452 char_u *str = NULL;
5453 long len = 0;
5454 char_u *expr = NULL;
5455 char_u *pat;
5456 regmatch_T regmatch;
5457 char_u patbuf[NUMBUFLEN];
5458 char_u strbuf[NUMBUFLEN];
5459 char_u *save_cpo;
5460 long start = 0;
5461 long nth = 1;
5462 colnr_T startcol = 0;
5463 int match = 0;
5464 list_T *l = NULL;
5465 listitem_T *li = NULL;
5466 long idx = 0;
5467 char_u *tofree = NULL;
5468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005470 save_cpo = p_cpo;
5471 p_cpo = (char_u *)"";
5472
5473 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005474 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 // type MATCH_LIST: return empty list when there are no matches.
5477 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478 if (rettv_list_alloc(rettv) == FAIL)
5479 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005480 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481 && (list_append_string(rettv->vval.v_list,
5482 (char_u *)"", 0) == FAIL
5483 || list_append_number(rettv->vval.v_list,
5484 (varnumber_T)-1) == FAIL
5485 || list_append_number(rettv->vval.v_list,
5486 (varnumber_T)-1) == FAIL
5487 || list_append_number(rettv->vval.v_list,
5488 (varnumber_T)-1) == FAIL))
5489 {
5490 list_free(rettv->vval.v_list);
5491 rettv->vval.v_list = NULL;
5492 goto theend;
5493 }
5494 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005495 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 {
5497 rettv->v_type = VAR_STRING;
5498 rettv->vval.v_string = NULL;
5499 }
5500
5501 if (argvars[0].v_type == VAR_LIST)
5502 {
5503 if ((l = argvars[0].vval.v_list) == NULL)
5504 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005505 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 li = l->lv_first;
5507 }
5508 else
5509 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005510 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 len = (long)STRLEN(str);
5512 }
5513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005514 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515 if (pat == NULL)
5516 goto theend;
5517
5518 if (argvars[2].v_type != VAR_UNKNOWN)
5519 {
5520 int error = FALSE;
5521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005522 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 if (error)
5524 goto theend;
5525 if (l != NULL)
5526 {
5527 li = list_find(l, start);
5528 if (li == NULL)
5529 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005530 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531 }
5532 else
5533 {
5534 if (start < 0)
5535 start = 0;
5536 if (start > len)
5537 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // When "count" argument is there ignore matches before "start",
5539 // otherwise skip part of the string. Differs when pattern is "^"
5540 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 if (argvars[3].v_type != VAR_UNKNOWN)
5542 startcol = start;
5543 else
5544 {
5545 str += start;
5546 len -= start;
5547 }
5548 }
5549
5550 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005551 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 if (error)
5553 goto theend;
5554 }
5555
5556 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5557 if (regmatch.regprog != NULL)
5558 {
5559 regmatch.rm_ic = p_ic;
5560
5561 for (;;)
5562 {
5563 if (l != NULL)
5564 {
5565 if (li == NULL)
5566 {
5567 match = FALSE;
5568 break;
5569 }
5570 vim_free(tofree);
5571 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5572 if (str == NULL)
5573 break;
5574 }
5575
5576 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5577
5578 if (match && --nth <= 0)
5579 break;
5580 if (l == NULL && !match)
5581 break;
5582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005583 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005584 if (l != NULL)
5585 {
5586 li = li->li_next;
5587 ++idx;
5588 }
5589 else
5590 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 startcol = (colnr_T)(regmatch.startp[0]
5592 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593 if (startcol > (colnr_T)len
5594 || str + startcol <= regmatch.startp[0])
5595 {
5596 match = FALSE;
5597 break;
5598 }
5599 }
5600 }
5601
5602 if (match)
5603 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005604 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 {
5606 listitem_T *li1 = rettv->vval.v_list->lv_first;
5607 listitem_T *li2 = li1->li_next;
5608 listitem_T *li3 = li2->li_next;
5609 listitem_T *li4 = li3->li_next;
5610
5611 vim_free(li1->li_tv.vval.v_string);
5612 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005613 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 li3->li_tv.vval.v_number =
5615 (varnumber_T)(regmatch.startp[0] - expr);
5616 li4->li_tv.vval.v_number =
5617 (varnumber_T)(regmatch.endp[0] - expr);
5618 if (l != NULL)
5619 li2->li_tv.vval.v_number = (varnumber_T)idx;
5620 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005621 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 {
5623 int i;
5624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005625 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626 for (i = 0; i < NSUBEXP; ++i)
5627 {
5628 if (regmatch.endp[i] == NULL)
5629 {
5630 if (list_append_string(rettv->vval.v_list,
5631 (char_u *)"", 0) == FAIL)
5632 break;
5633 }
5634 else if (list_append_string(rettv->vval.v_list,
5635 regmatch.startp[i],
5636 (int)(regmatch.endp[i] - regmatch.startp[i]))
5637 == FAIL)
5638 break;
5639 }
5640 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005641 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005644 if (l != NULL)
5645 copy_tv(&li->li_tv, rettv);
5646 else
5647 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005648 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 }
5650 else if (l != NULL)
5651 rettv->vval.v_number = idx;
5652 else
5653 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005654 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005655 rettv->vval.v_number =
5656 (varnumber_T)(regmatch.startp[0] - str);
5657 else
5658 rettv->vval.v_number =
5659 (varnumber_T)(regmatch.endp[0] - str);
5660 rettv->vval.v_number += (varnumber_T)(str - expr);
5661 }
5662 }
5663 vim_regfree(regmatch.regprog);
5664 }
5665
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005666theend:
5667 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005668 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669 listitem_remove(rettv->vval.v_list,
5670 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671 vim_free(tofree);
5672 p_cpo = save_cpo;
5673}
5674
5675/*
5676 * "match()" function
5677 */
5678 static void
5679f_match(typval_T *argvars, typval_T *rettv)
5680{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005681 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682}
5683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684/*
5685 * "matchend()" function
5686 */
5687 static void
5688f_matchend(typval_T *argvars, typval_T *rettv)
5689{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005690 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005691}
5692
5693/*
5694 * "matchlist()" function
5695 */
5696 static void
5697f_matchlist(typval_T *argvars, typval_T *rettv)
5698{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005699 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700}
5701
5702/*
5703 * "matchstr()" function
5704 */
5705 static void
5706f_matchstr(typval_T *argvars, typval_T *rettv)
5707{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005708 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005709}
5710
5711/*
5712 * "matchstrpos()" function
5713 */
5714 static void
5715f_matchstrpos(typval_T *argvars, typval_T *rettv)
5716{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005717 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718}
5719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005720 static void
5721max_min(typval_T *argvars, typval_T *rettv, int domax)
5722{
5723 varnumber_T n = 0;
5724 varnumber_T i;
5725 int error = FALSE;
5726
5727 if (argvars[0].v_type == VAR_LIST)
5728 {
5729 list_T *l;
5730 listitem_T *li;
5731
5732 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005733 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005734 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005735 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005737 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5738 n = l->lv_u.nonmat.lv_start;
5739 else
5740 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5741 * l->lv_u.nonmat.lv_stride;
5742 }
5743 else
5744 {
5745 li = l->lv_first;
5746 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005747 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005748 n = tv_get_number_chk(&li->li_tv, &error);
5749 for (;;)
5750 {
5751 li = li->li_next;
5752 if (li == NULL)
5753 break;
5754 i = tv_get_number_chk(&li->li_tv, &error);
5755 if (domax ? i > n : i < n)
5756 n = i;
5757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 }
5759 }
5760 }
5761 }
5762 else if (argvars[0].v_type == VAR_DICT)
5763 {
5764 dict_T *d;
5765 int first = TRUE;
5766 hashitem_T *hi;
5767 int todo;
5768
5769 d = argvars[0].vval.v_dict;
5770 if (d != NULL)
5771 {
5772 todo = (int)d->dv_hashtab.ht_used;
5773 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5774 {
5775 if (!HASHITEM_EMPTY(hi))
5776 {
5777 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005778 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005779 if (first)
5780 {
5781 n = i;
5782 first = FALSE;
5783 }
5784 else if (domax ? i > n : i < n)
5785 n = i;
5786 }
5787 }
5788 }
5789 }
5790 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005791 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 rettv->vval.v_number = error ? 0 : n;
5793}
5794
5795/*
5796 * "max()" function
5797 */
5798 static void
5799f_max(typval_T *argvars, typval_T *rettv)
5800{
5801 max_min(argvars, rettv, TRUE);
5802}
5803
5804/*
5805 * "min()" function
5806 */
5807 static void
5808f_min(typval_T *argvars, typval_T *rettv)
5809{
5810 max_min(argvars, rettv, FALSE);
5811}
5812
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813#if defined(FEAT_MZSCHEME) || defined(PROTO)
5814/*
5815 * "mzeval()" function
5816 */
5817 static void
5818f_mzeval(typval_T *argvars, typval_T *rettv)
5819{
5820 char_u *str;
5821 char_u buf[NUMBUFLEN];
5822
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005823 if (check_restricted() || check_secure())
5824 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005825 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005826 do_mzeval(str, rettv);
5827}
5828
5829 void
5830mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5831{
5832 typval_T argvars[3];
5833
5834 argvars[0].v_type = VAR_STRING;
5835 argvars[0].vval.v_string = name;
5836 copy_tv(args, &argvars[1]);
5837 argvars[2].v_type = VAR_UNKNOWN;
5838 f_call(argvars, rettv);
5839 clear_tv(&argvars[1]);
5840}
5841#endif
5842
5843/*
5844 * "nextnonblank()" function
5845 */
5846 static void
5847f_nextnonblank(typval_T *argvars, typval_T *rettv)
5848{
5849 linenr_T lnum;
5850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005851 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 {
5853 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5854 {
5855 lnum = 0;
5856 break;
5857 }
5858 if (*skipwhite(ml_get(lnum)) != NUL)
5859 break;
5860 }
5861 rettv->vval.v_number = lnum;
5862}
5863
5864/*
5865 * "nr2char()" function
5866 */
5867 static void
5868f_nr2char(typval_T *argvars, typval_T *rettv)
5869{
5870 char_u buf[NUMBUFLEN];
5871
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005872 if (has_mbyte)
5873 {
5874 int utf8 = 0;
5875
5876 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02005877 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005879 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 }
5883 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 buf[1] = NUL;
5887 }
5888 rettv->v_type = VAR_STRING;
5889 rettv->vval.v_string = vim_strsave(buf);
5890}
5891
5892/*
5893 * "or(expr, expr)" function
5894 */
5895 static void
5896f_or(typval_T *argvars, typval_T *rettv)
5897{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005898 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5899 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900}
5901
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902#ifdef FEAT_PERL
5903/*
5904 * "perleval()" function
5905 */
5906 static void
5907f_perleval(typval_T *argvars, typval_T *rettv)
5908{
5909 char_u *str;
5910 char_u buf[NUMBUFLEN];
5911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005912 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 do_perleval(str, rettv);
5914}
5915#endif
5916
5917#ifdef FEAT_FLOAT
5918/*
5919 * "pow()" function
5920 */
5921 static void
5922f_pow(typval_T *argvars, typval_T *rettv)
5923{
5924 float_T fx = 0.0, fy = 0.0;
5925
5926 rettv->v_type = VAR_FLOAT;
5927 if (get_float_arg(argvars, &fx) == OK
5928 && get_float_arg(&argvars[1], &fy) == OK)
5929 rettv->vval.v_float = pow(fx, fy);
5930 else
5931 rettv->vval.v_float = 0.0;
5932}
5933#endif
5934
5935/*
5936 * "prevnonblank()" function
5937 */
5938 static void
5939f_prevnonblank(typval_T *argvars, typval_T *rettv)
5940{
5941 linenr_T lnum;
5942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005943 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5945 lnum = 0;
5946 else
5947 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5948 --lnum;
5949 rettv->vval.v_number = lnum;
5950}
5951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005952// This dummy va_list is here because:
5953// - passing a NULL pointer doesn't work when va_list isn't a pointer
5954// - locally in the function results in a "used before set" warning
5955// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956static va_list ap;
5957
5958/*
5959 * "printf()" function
5960 */
5961 static void
5962f_printf(typval_T *argvars, typval_T *rettv)
5963{
5964 char_u buf[NUMBUFLEN];
5965 int len;
5966 char_u *s;
5967 int saved_did_emsg = did_emsg;
5968 char *fmt;
5969
5970 rettv->v_type = VAR_STRING;
5971 rettv->vval.v_string = NULL;
5972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005973 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005975 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005976 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 if (!did_emsg)
5978 {
5979 s = alloc(len + 1);
5980 if (s != NULL)
5981 {
5982 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005983 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5984 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 }
5986 }
5987 did_emsg |= saved_did_emsg;
5988}
5989
5990/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005991 * "pum_getpos()" function
5992 */
5993 static void
5994f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5995{
5996 if (rettv_dict_alloc(rettv) != OK)
5997 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005998 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005999}
6000
6001/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 * "pumvisible()" function
6003 */
6004 static void
6005f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6006{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 if (pum_visible())
6008 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009}
6010
6011#ifdef FEAT_PYTHON3
6012/*
6013 * "py3eval()" function
6014 */
6015 static void
6016f_py3eval(typval_T *argvars, typval_T *rettv)
6017{
6018 char_u *str;
6019 char_u buf[NUMBUFLEN];
6020
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006021 if (check_restricted() || check_secure())
6022 return;
6023
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006024 if (p_pyx == 0)
6025 p_pyx = 3;
6026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006027 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 do_py3eval(str, rettv);
6029}
6030#endif
6031
6032#ifdef FEAT_PYTHON
6033/*
6034 * "pyeval()" function
6035 */
6036 static void
6037f_pyeval(typval_T *argvars, typval_T *rettv)
6038{
6039 char_u *str;
6040 char_u buf[NUMBUFLEN];
6041
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006042 if (check_restricted() || check_secure())
6043 return;
6044
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006045 if (p_pyx == 0)
6046 p_pyx = 2;
6047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006048 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 do_pyeval(str, rettv);
6050}
6051#endif
6052
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006053#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6054/*
6055 * "pyxeval()" function
6056 */
6057 static void
6058f_pyxeval(typval_T *argvars, typval_T *rettv)
6059{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006060 if (check_restricted() || check_secure())
6061 return;
6062
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006063# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6064 init_pyxversion();
6065 if (p_pyx == 2)
6066 f_pyeval(argvars, rettv);
6067 else
6068 f_py3eval(argvars, rettv);
6069# elif defined(FEAT_PYTHON)
6070 f_pyeval(argvars, rettv);
6071# elif defined(FEAT_PYTHON3)
6072 f_py3eval(argvars, rettv);
6073# endif
6074}
6075#endif
6076
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006077static UINT32_T srand_seed_for_testing = 0;
6078static int srand_seed_for_testing_is_used = FALSE;
6079
6080 static void
6081f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6082{
6083 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006084 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006085 else
6086 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006087 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6088 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006089 }
6090}
6091
6092 static void
6093init_srand(UINT32_T *x)
6094{
6095#ifndef MSWIN
6096 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6097#endif
6098
6099 if (srand_seed_for_testing_is_used)
6100 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006101 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006102 return;
6103 }
6104#ifndef MSWIN
6105 if (dev_urandom_state != FAIL)
6106 {
6107 int fd = open("/dev/urandom", O_RDONLY);
6108 struct {
6109 union {
6110 UINT32_T number;
6111 char bytes[sizeof(UINT32_T)];
6112 } contents;
6113 } buf;
6114
6115 // Attempt reading /dev/urandom.
6116 if (fd == -1)
6117 dev_urandom_state = FAIL;
6118 else
6119 {
6120 buf.contents.number = 0;
6121 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6122 != sizeof(UINT32_T))
6123 dev_urandom_state = FAIL;
6124 else
6125 {
6126 dev_urandom_state = OK;
6127 *x = buf.contents.number;
6128 }
6129 close(fd);
6130 }
6131 }
6132 if (dev_urandom_state != OK)
6133 // Reading /dev/urandom doesn't work, fall back to time().
6134#endif
6135 *x = vim_time();
6136}
6137
6138#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6139#define SPLITMIX32(x, z) ( \
6140 z = (x += 0x9e3779b9), \
6141 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6142 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6143 z ^ (z >> 16) \
6144 )
6145#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6146 result = ROTL(y * 5, 7) * 9; \
6147 t = y << 9; \
6148 z ^= x; \
6149 w ^= y; \
6150 y ^= z, x ^= w; \
6151 z ^= t; \
6152 w = ROTL(w, 11);
6153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006154/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006155 * "rand()" function
6156 */
6157 static void
6158f_rand(typval_T *argvars, typval_T *rettv)
6159{
6160 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006161 static UINT32_T gx, gy, gz, gw;
6162 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006163 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006164 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006165
6166 if (argvars[0].v_type == VAR_UNKNOWN)
6167 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006168 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006169 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006170 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006171 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006172 init_srand(&x);
6173
6174 gx = SPLITMIX32(x, z);
6175 gy = SPLITMIX32(x, z);
6176 gz = SPLITMIX32(x, z);
6177 gw = SPLITMIX32(x, z);
6178 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006179 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006180
6181 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006182 }
6183 else if (argvars[0].v_type == VAR_LIST)
6184 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006185 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006186 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006187 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006188
6189 lx = list_find(l, 0L);
6190 ly = list_find(l, 1L);
6191 lz = list_find(l, 2L);
6192 lw = list_find(l, 3L);
6193 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6194 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6195 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6196 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6197 x = (UINT32_T)lx->li_tv.vval.v_number;
6198 y = (UINT32_T)ly->li_tv.vval.v_number;
6199 z = (UINT32_T)lz->li_tv.vval.v_number;
6200 w = (UINT32_T)lw->li_tv.vval.v_number;
6201
6202 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6203
6204 lx->li_tv.vval.v_number = (varnumber_T)x;
6205 ly->li_tv.vval.v_number = (varnumber_T)y;
6206 lz->li_tv.vval.v_number = (varnumber_T)z;
6207 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006208 }
6209 else
6210 goto theend;
6211
6212 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006213 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006214 return;
6215
6216theend:
6217 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006218 rettv->v_type = VAR_NUMBER;
6219 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006220}
6221
6222/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006223 * "srand()" function
6224 */
6225 static void
6226f_srand(typval_T *argvars, typval_T *rettv)
6227{
6228 UINT32_T x = 0, z;
6229
6230 if (rettv_list_alloc(rettv) == FAIL)
6231 return;
6232 if (argvars[0].v_type == VAR_UNKNOWN)
6233 {
6234 init_srand(&x);
6235 }
6236 else
6237 {
6238 int error = FALSE;
6239
6240 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6241 if (error)
6242 return;
6243 }
6244
6245 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6246 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6247 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6248 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6249}
6250
6251#undef ROTL
6252#undef SPLITMIX32
6253#undef SHUFFLE_XOSHIRO128STARSTAR
6254
6255/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 * "range()" function
6257 */
6258 static void
6259f_range(typval_T *argvars, typval_T *rettv)
6260{
6261 varnumber_T start;
6262 varnumber_T end;
6263 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 int error = FALSE;
6265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006266 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 if (argvars[1].v_type == VAR_UNKNOWN)
6268 {
6269 end = start - 1;
6270 start = 0;
6271 }
6272 else
6273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006274 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 }
6278
6279 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006280 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006282 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006284 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 list_T *list = rettv->vval.v_list;
6288
6289 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006290 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 // be called.
6292 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006293 list->lv_u.nonmat.lv_start = start;
6294 list->lv_u.nonmat.lv_end = end;
6295 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006296 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 }
6298}
6299
6300/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006301 * Materialize "list".
6302 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 */
6304 void
6305range_list_materialize(list_T *list)
6306{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006307 varnumber_T start = list->lv_u.nonmat.lv_start;
6308 varnumber_T end = list->lv_u.nonmat.lv_end;
6309 int stride = list->lv_u.nonmat.lv_stride;
6310 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006311
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006312 list->lv_first = NULL;
6313 list->lv_u.mat.lv_last = NULL;
6314 list->lv_len = 0;
6315 list->lv_u.mat.lv_idx_item = NULL;
6316 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6317 if (list_append_number(list, (varnumber_T)i) == FAIL)
6318 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319}
6320
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006321/*
6322 * "getreginfo()" function
6323 */
6324 static void
6325f_getreginfo(typval_T *argvars, typval_T *rettv)
6326{
6327 char_u *strregname;
6328 int regname;
6329 char_u buf[NUMBUFLEN + 2];
6330 long reglen = 0;
6331 dict_T *dict;
6332 list_T *list;
6333
6334 if (argvars[0].v_type != VAR_UNKNOWN)
6335 {
6336 strregname = tv_get_string_chk(&argvars[0]);
6337 if (strregname == NULL)
6338 return;
6339 }
6340 else
6341 strregname = get_vim_var_str(VV_REG);
6342
6343 regname = (strregname == NULL ? '"' : *strregname);
6344 if (regname == 0 || regname == '@')
6345 regname = '"';
6346
6347 if (rettv_dict_alloc(rettv) == FAIL)
6348 return;
6349 dict = rettv->vval.v_dict;
6350
6351 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6352 if (list == NULL)
6353 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006354 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006355
6356 buf[0] = NUL;
6357 buf[1] = NUL;
6358 switch (get_reg_type(regname, &reglen))
6359 {
6360 case MLINE: buf[0] = 'V'; break;
6361 case MCHAR: buf[0] = 'v'; break;
6362 case MBLOCK:
6363 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6364 reglen + 1);
6365 break;
6366 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006367 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006368
6369 buf[0] = get_register_name(get_unname_register());
6370 buf[1] = NUL;
6371 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006372 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006373 else
6374 {
6375 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6376
6377 if (item != NULL)
6378 {
6379 item->di_tv.v_type = VAR_SPECIAL;
6380 item->di_tv.vval.v_number = regname == buf[0]
6381 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006382 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006383 }
6384 }
6385}
6386
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006387 static void
6388return_register(int regname, typval_T *rettv)
6389{
6390 char_u buf[2] = {0, 0};
6391
6392 buf[0] = (char_u)regname;
6393 rettv->v_type = VAR_STRING;
6394 rettv->vval.v_string = vim_strsave(buf);
6395}
6396
6397/*
6398 * "reg_executing()" function
6399 */
6400 static void
6401f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6402{
6403 return_register(reg_executing, rettv);
6404}
6405
6406/*
6407 * "reg_recording()" function
6408 */
6409 static void
6410f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6411{
6412 return_register(reg_recording, rettv);
6413}
6414
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006415/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006416 * "rename({from}, {to})" function
6417 */
6418 static void
6419f_rename(typval_T *argvars, typval_T *rettv)
6420{
6421 char_u buf[NUMBUFLEN];
6422
6423 if (check_restricted() || check_secure())
6424 rettv->vval.v_number = -1;
6425 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006426 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6427 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006428}
6429
6430/*
6431 * "repeat()" function
6432 */
6433 static void
6434f_repeat(typval_T *argvars, typval_T *rettv)
6435{
6436 char_u *p;
6437 int n;
6438 int slen;
6439 int len;
6440 char_u *r;
6441 int i;
6442
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006443 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444 if (argvars[0].v_type == VAR_LIST)
6445 {
6446 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6447 while (n-- > 0)
6448 if (list_extend(rettv->vval.v_list,
6449 argvars[0].vval.v_list, NULL) == FAIL)
6450 break;
6451 }
6452 else
6453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006454 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455 rettv->v_type = VAR_STRING;
6456 rettv->vval.v_string = NULL;
6457
6458 slen = (int)STRLEN(p);
6459 len = slen * n;
6460 if (len <= 0)
6461 return;
6462
6463 r = alloc(len + 1);
6464 if (r != NULL)
6465 {
6466 for (i = 0; i < n; i++)
6467 mch_memmove(r + i * slen, p, (size_t)slen);
6468 r[len] = NUL;
6469 }
6470
6471 rettv->vval.v_string = r;
6472 }
6473}
6474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006475#define SP_NOMOVE 0x01 // don't move cursor
6476#define SP_REPEAT 0x02 // repeat to find outer pair
6477#define SP_RETCOUNT 0x04 // return matchcount
6478#define SP_SETPCMARK 0x08 // set previous context mark
6479#define SP_START 0x10 // accept match at start position
6480#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6481#define SP_END 0x40 // leave cursor at end of match
6482#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484/*
6485 * Get flags for a search function.
6486 * Possibly sets "p_ws".
6487 * Returns BACKWARD, FORWARD or zero (for an error).
6488 */
6489 static int
6490get_search_arg(typval_T *varp, int *flagsp)
6491{
6492 int dir = FORWARD;
6493 char_u *flags;
6494 char_u nbuf[NUMBUFLEN];
6495 int mask;
6496
6497 if (varp->v_type != VAR_UNKNOWN)
6498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006499 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006501 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006502 while (*flags != NUL)
6503 {
6504 switch (*flags)
6505 {
6506 case 'b': dir = BACKWARD; break;
6507 case 'w': p_ws = TRUE; break;
6508 case 'W': p_ws = FALSE; break;
6509 default: mask = 0;
6510 if (flagsp != NULL)
6511 switch (*flags)
6512 {
6513 case 'c': mask = SP_START; break;
6514 case 'e': mask = SP_END; break;
6515 case 'm': mask = SP_RETCOUNT; break;
6516 case 'n': mask = SP_NOMOVE; break;
6517 case 'p': mask = SP_SUBPAT; break;
6518 case 'r': mask = SP_REPEAT; break;
6519 case 's': mask = SP_SETPCMARK; break;
6520 case 'z': mask = SP_COLUMN; break;
6521 }
6522 if (mask == 0)
6523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006524 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525 dir = 0;
6526 }
6527 else
6528 *flagsp |= mask;
6529 }
6530 if (dir == 0)
6531 break;
6532 ++flags;
6533 }
6534 }
6535 return dir;
6536}
6537
6538/*
6539 * Shared by search() and searchpos() functions.
6540 */
6541 static int
6542search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6543{
6544 int flags;
6545 char_u *pat;
6546 pos_T pos;
6547 pos_T save_cursor;
6548 int save_p_ws = p_ws;
6549 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006550 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006553 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006554 long time_limit = 0;
6555#endif
6556 int options = SEARCH_KEEP;
6557 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006558 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006559 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006560 pos_T firstpos;
6561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006562 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006563 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 if (dir == 0)
6565 goto theend;
6566 flags = *flagsp;
6567 if (flags & SP_START)
6568 options |= SEARCH_START;
6569 if (flags & SP_END)
6570 options |= SEARCH_END;
6571 if (flags & SP_COLUMN)
6572 options |= SEARCH_COL;
6573
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006574 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006577 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578 if (lnum_stop < 0)
6579 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580 if (argvars[3].v_type != VAR_UNKNOWN)
6581 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006582#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006583 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 if (time_limit < 0)
6585 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006587 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006588 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589 }
6590
6591#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006592 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 profile_setlimit(time_limit, &tm);
6594#endif
6595
6596 /*
6597 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6598 * Check to make sure only those flags are set.
6599 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6600 * flags cannot be set. Check for that condition also.
6601 */
6602 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6603 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006605 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606 goto theend;
6607 }
6608
6609 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006610 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006611 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006612 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6613#ifdef FEAT_RELTIME
6614 sia.sa_tm = &tm;
6615#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006616
6617 // Repeat until {skip} returns FALSE.
6618 for (;;)
6619 {
6620 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006621 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006622 // finding the first match again means there is no match where {skip}
6623 // evaluates to zero.
6624 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6625 subpatnum = FAIL;
6626
Bram Moolenaara9c01042020-06-07 14:50:50 +02006627 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006628 // didn't find it or no skip argument
6629 break;
6630 firstpos = pos;
6631
Bram Moolenaara9c01042020-06-07 14:50:50 +02006632 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006633 {
6634 int do_skip;
6635 int err;
6636 pos_T save_pos = curwin->w_cursor;
6637
6638 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006639 err = FALSE;
6640 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006641 curwin->w_cursor = save_pos;
6642 if (err)
6643 {
6644 // Evaluating {skip} caused an error, break here.
6645 subpatnum = FAIL;
6646 break;
6647 }
6648 if (!do_skip)
6649 break;
6650 }
6651 }
6652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 if (subpatnum != FAIL)
6654 {
6655 if (flags & SP_SUBPAT)
6656 retval = subpatnum;
6657 else
6658 retval = pos.lnum;
6659 if (flags & SP_SETPCMARK)
6660 setpcmark();
6661 curwin->w_cursor = pos;
6662 if (match_pos != NULL)
6663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006664 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 match_pos->lnum = pos.lnum;
6666 match_pos->col = pos.col + 1;
6667 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006668 // "/$" will put the cursor after the end of the line, may need to
6669 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 check_cursor();
6671 }
6672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006673 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674 if (flags & SP_NOMOVE)
6675 curwin->w_cursor = save_cursor;
6676 else
6677 curwin->w_set_curswant = TRUE;
6678theend:
6679 p_ws = save_p_ws;
6680
6681 return retval;
6682}
6683
6684#ifdef FEAT_FLOAT
6685
6686/*
6687 * round() is not in C90, use ceil() or floor() instead.
6688 */
6689 float_T
6690vim_round(float_T f)
6691{
6692 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6693}
6694
6695/*
6696 * "round({float})" function
6697 */
6698 static void
6699f_round(typval_T *argvars, typval_T *rettv)
6700{
6701 float_T f = 0.0;
6702
6703 rettv->v_type = VAR_FLOAT;
6704 if (get_float_arg(argvars, &f) == OK)
6705 rettv->vval.v_float = vim_round(f);
6706 else
6707 rettv->vval.v_float = 0.0;
6708}
6709#endif
6710
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006711#ifdef FEAT_RUBY
6712/*
6713 * "rubyeval()" function
6714 */
6715 static void
6716f_rubyeval(typval_T *argvars, typval_T *rettv)
6717{
6718 char_u *str;
6719 char_u buf[NUMBUFLEN];
6720
6721 str = tv_get_string_buf(&argvars[0], buf);
6722 do_rubyeval(str, rettv);
6723}
6724#endif
6725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726/*
6727 * "screenattr()" function
6728 */
6729 static void
6730f_screenattr(typval_T *argvars, typval_T *rettv)
6731{
6732 int row;
6733 int col;
6734 int c;
6735
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006736 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6737 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 if (row < 0 || row >= screen_Rows
6739 || col < 0 || col >= screen_Columns)
6740 c = -1;
6741 else
6742 c = ScreenAttrs[LineOffset[row] + col];
6743 rettv->vval.v_number = c;
6744}
6745
6746/*
6747 * "screenchar()" function
6748 */
6749 static void
6750f_screenchar(typval_T *argvars, typval_T *rettv)
6751{
6752 int row;
6753 int col;
6754 int off;
6755 int c;
6756
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006757 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6758 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006759 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760 c = -1;
6761 else
6762 {
6763 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764 if (enc_utf8 && ScreenLinesUC[off] != 0)
6765 c = ScreenLinesUC[off];
6766 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006767 c = ScreenLines[off];
6768 }
6769 rettv->vval.v_number = c;
6770}
6771
6772/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006773 * "screenchars()" function
6774 */
6775 static void
6776f_screenchars(typval_T *argvars, typval_T *rettv)
6777{
6778 int row;
6779 int col;
6780 int off;
6781 int c;
6782 int i;
6783
6784 if (rettv_list_alloc(rettv) == FAIL)
6785 return;
6786 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6787 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6788 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6789 return;
6790
6791 off = LineOffset[row] + col;
6792 if (enc_utf8 && ScreenLinesUC[off] != 0)
6793 c = ScreenLinesUC[off];
6794 else
6795 c = ScreenLines[off];
6796 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6797
6798 if (enc_utf8)
6799
6800 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6801 list_append_number(rettv->vval.v_list,
6802 (varnumber_T)ScreenLinesC[i][off]);
6803}
6804
6805/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 * "screencol()" function
6807 *
6808 * First column is 1 to be consistent with virtcol().
6809 */
6810 static void
6811f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6812{
6813 rettv->vval.v_number = screen_screencol() + 1;
6814}
6815
6816/*
6817 * "screenrow()" function
6818 */
6819 static void
6820f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6821{
6822 rettv->vval.v_number = screen_screenrow() + 1;
6823}
6824
6825/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006826 * "screenstring()" function
6827 */
6828 static void
6829f_screenstring(typval_T *argvars, typval_T *rettv)
6830{
6831 int row;
6832 int col;
6833 int off;
6834 int c;
6835 int i;
6836 char_u buf[MB_MAXBYTES + 1];
6837 int buflen = 0;
6838
6839 rettv->vval.v_string = NULL;
6840 rettv->v_type = VAR_STRING;
6841
6842 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6843 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6844 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6845 return;
6846
6847 off = LineOffset[row] + col;
6848 if (enc_utf8 && ScreenLinesUC[off] != 0)
6849 c = ScreenLinesUC[off];
6850 else
6851 c = ScreenLines[off];
6852 buflen += mb_char2bytes(c, buf);
6853
6854 if (enc_utf8)
6855 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6856 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6857
6858 buf[buflen] = NUL;
6859 rettv->vval.v_string = vim_strsave(buf);
6860}
6861
6862/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863 * "search()" function
6864 */
6865 static void
6866f_search(typval_T *argvars, typval_T *rettv)
6867{
6868 int flags = 0;
6869
6870 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6871}
6872
6873/*
6874 * "searchdecl()" function
6875 */
6876 static void
6877f_searchdecl(typval_T *argvars, typval_T *rettv)
6878{
Bram Moolenaar30788d32020-09-05 21:35:16 +02006879 int locally = TRUE;
6880 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 int error = FALSE;
6882 char_u *name;
6883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006884 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006886 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 if (argvars[1].v_type != VAR_UNKNOWN)
6888 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02006889 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02006891 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 }
6893 if (!error && name != NULL)
6894 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6895 locally, thisblock, SEARCH_KEEP) == FAIL;
6896}
6897
6898/*
6899 * Used by searchpair() and searchpairpos()
6900 */
6901 static int
6902searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6903{
6904 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006905 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 int save_p_ws = p_ws;
6907 int dir;
6908 int flags = 0;
6909 char_u nbuf1[NUMBUFLEN];
6910 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006911 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 long lnum_stop = 0;
6913 long time_limit = 0;
6914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 // Get the three pattern arguments: start, middle, end. Will result in an
6916 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 spat = tv_get_string_chk(&argvars[0]);
6918 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6919 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006921 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 // Handle the optional fourth argument: flags
6924 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (dir == 0)
6926 goto theend;
6927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 // Don't accept SP_END or SP_SUBPAT.
6929 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 if ((flags & (SP_END | SP_SUBPAT)) != 0
6931 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006933 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 goto theend;
6935 }
6936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006937 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 if (flags & SP_REPEAT)
6939 p_ws = FALSE;
6940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006941 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (argvars[3].v_type == VAR_UNKNOWN
6943 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006944 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 else
6946 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006947 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006948 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006949
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 if (argvars[5].v_type != VAR_UNKNOWN)
6951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006952 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958#ifdef FEAT_RELTIME
6959 if (argvars[6].v_type != VAR_UNKNOWN)
6960 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006961 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006964 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 }
6968#endif
6969 }
6970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971
6972 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6973 match_pos, lnum_stop, time_limit);
6974
6975theend:
6976 p_ws = save_p_ws;
6977
6978 return retval;
6979}
6980
6981/*
6982 * "searchpair()" function
6983 */
6984 static void
6985f_searchpair(typval_T *argvars, typval_T *rettv)
6986{
6987 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6988}
6989
6990/*
6991 * "searchpairpos()" function
6992 */
6993 static void
6994f_searchpairpos(typval_T *argvars, typval_T *rettv)
6995{
6996 pos_T match_pos;
6997 int lnum = 0;
6998 int col = 0;
6999
7000 if (rettv_list_alloc(rettv) == FAIL)
7001 return;
7002
7003 if (searchpair_cmn(argvars, &match_pos) > 0)
7004 {
7005 lnum = match_pos.lnum;
7006 col = match_pos.col;
7007 }
7008
7009 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7010 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7011}
7012
7013/*
7014 * Search for a start/middle/end thing.
7015 * Used by searchpair(), see its documentation for the details.
7016 * Returns 0 or -1 for no match,
7017 */
7018 long
7019do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007020 char_u *spat, // start pattern
7021 char_u *mpat, // middle pattern
7022 char_u *epat, // end pattern
7023 int dir, // BACKWARD or FORWARD
7024 typval_T *skip, // skip expression
7025 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007027 linenr_T lnum_stop, // stop at this line if not zero
7028 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007029{
7030 char_u *save_cpo;
7031 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7032 long retval = 0;
7033 pos_T pos;
7034 pos_T firstpos;
7035 pos_T foundpos;
7036 pos_T save_cursor;
7037 pos_T save_pos;
7038 int n;
7039 int r;
7040 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007041 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 int err;
7043 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007044#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007046#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007048 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 save_cpo = p_cpo;
7050 p_cpo = empty_option;
7051
7052#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007053 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 profile_setlimit(time_limit, &tm);
7055#endif
7056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007057 // Make two search patterns: start/end (pat2, for in nested pairs) and
7058 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007059 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7060 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007061 if (pat2 == NULL || pat3 == NULL)
7062 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007063 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 if (*mpat == NUL)
7065 STRCPY(pat3, pat2);
7066 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007067 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 spat, epat, mpat);
7069 if (flags & SP_START)
7070 options |= SEARCH_START;
7071
Bram Moolenaar48570482017-10-30 21:48:41 +01007072 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007073 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007075 save_cursor = curwin->w_cursor;
7076 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007077 CLEAR_POS(&firstpos);
7078 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 pat = pat3;
7080 for (;;)
7081 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007082 searchit_arg_T sia;
7083
Bram Moolenaara80faa82020-04-12 19:37:17 +02007084 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007085 sia.sa_stop_lnum = lnum_stop;
7086#ifdef FEAT_RELTIME
7087 sia.sa_tm = &tm;
7088#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007089 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007090 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007091 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007092 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 break;
7094
7095 if (firstpos.lnum == 0)
7096 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007097 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007099 // Found the same position again. Can happen with a pattern that
7100 // has "\zs" at the end and searching backwards. Advance one
7101 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 if (dir == BACKWARD)
7103 decl(&pos);
7104 else
7105 incl(&pos);
7106 }
7107 foundpos = pos;
7108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 options &= ~SEARCH_START;
7111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007112 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007113 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 {
7115 save_pos = curwin->w_cursor;
7116 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007117 err = FALSE;
7118 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 curwin->w_cursor = save_pos;
7120 if (err)
7121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007122 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 curwin->w_cursor = save_cursor;
7124 retval = -1;
7125 break;
7126 }
7127 if (r)
7128 continue;
7129 }
7130
7131 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7132 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007133 // Found end when searching backwards or start when searching
7134 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007135 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007136 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 }
7138 else
7139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007140 // Found end when searching forward or start when searching
7141 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007143 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 }
7145
7146 if (nest == 0)
7147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007148 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 if (flags & SP_RETCOUNT)
7150 ++retval;
7151 else
7152 retval = pos.lnum;
7153 if (flags & SP_SETPCMARK)
7154 setpcmark();
7155 curwin->w_cursor = pos;
7156 if (!(flags & SP_REPEAT))
7157 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007158 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 }
7160 }
7161
7162 if (match_pos != NULL)
7163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007164 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 match_pos->lnum = curwin->w_cursor.lnum;
7166 match_pos->col = curwin->w_cursor.col + 1;
7167 }
7168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007169 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170 if ((flags & SP_NOMOVE) || retval == 0)
7171 curwin->w_cursor = save_cursor;
7172
7173theend:
7174 vim_free(pat2);
7175 vim_free(pat3);
7176 if (p_cpo == empty_option)
7177 p_cpo = save_cpo;
7178 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007179 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007180 free_string_option(save_cpo);
7181
7182 return retval;
7183}
7184
7185/*
7186 * "searchpos()" function
7187 */
7188 static void
7189f_searchpos(typval_T *argvars, typval_T *rettv)
7190{
7191 pos_T match_pos;
7192 int lnum = 0;
7193 int col = 0;
7194 int n;
7195 int flags = 0;
7196
7197 if (rettv_list_alloc(rettv) == FAIL)
7198 return;
7199
7200 n = search_cmn(argvars, &match_pos, &flags);
7201 if (n > 0)
7202 {
7203 lnum = match_pos.lnum;
7204 col = match_pos.col;
7205 }
7206
7207 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7208 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7209 if (flags & SP_SUBPAT)
7210 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7211}
7212
7213 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7215{
7216 dict_T *d;
7217 dictitem_T *di;
7218 char_u *csearch;
7219
7220 if (argvars[0].v_type != VAR_DICT)
7221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007222 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223 return;
7224 }
7225
7226 if ((d = argvars[0].vval.v_dict) != NULL)
7227 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007228 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 if (csearch != NULL)
7230 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 if (enc_utf8)
7232 {
7233 int pcc[MAX_MCO];
7234 int c = utfc_ptr2char(csearch, pcc);
7235
7236 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7237 }
7238 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007240 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 }
7242
7243 di = dict_find(d, (char_u *)"forward", -1);
7244 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007245 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 ? FORWARD : BACKWARD);
7247
7248 di = dict_find(d, (char_u *)"until", -1);
7249 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007250 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 }
7252}
7253
7254/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007255 * "setenv()" function
7256 */
7257 static void
7258f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7259{
7260 char_u namebuf[NUMBUFLEN];
7261 char_u valbuf[NUMBUFLEN];
7262 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7263
7264 if (argvars[1].v_type == VAR_SPECIAL
7265 && argvars[1].vval.v_number == VVAL_NULL)
7266 vim_unsetenv(name);
7267 else
7268 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7269}
7270
7271/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 * "setfperm({fname}, {mode})" function
7273 */
7274 static void
7275f_setfperm(typval_T *argvars, typval_T *rettv)
7276{
7277 char_u *fname;
7278 char_u modebuf[NUMBUFLEN];
7279 char_u *mode_str;
7280 int i;
7281 int mask;
7282 int mode = 0;
7283
7284 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007285 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 if (fname == NULL)
7287 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007288 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 if (mode_str == NULL)
7290 return;
7291 if (STRLEN(mode_str) != 9)
7292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007293 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 return;
7295 }
7296
7297 mask = 1;
7298 for (i = 8; i >= 0; --i)
7299 {
7300 if (mode_str[i] != '-')
7301 mode |= mask;
7302 mask = mask << 1;
7303 }
7304 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7305}
7306
7307/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007308 * "setpos()" function
7309 */
7310 static void
7311f_setpos(typval_T *argvars, typval_T *rettv)
7312{
7313 pos_T pos;
7314 int fnum;
7315 char_u *name;
7316 colnr_T curswant = -1;
7317
7318 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (name != NULL)
7321 {
7322 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7323 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007324 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 pos.col = 0;
7326 if (name[0] == '.' && name[1] == NUL)
7327 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007328 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007329 curwin->w_cursor = pos;
7330 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007332 curwin->w_curswant = curswant - 1;
7333 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007335 check_cursor();
7336 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007337 }
7338 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007340 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 if (setmark_pos(name[1], &pos, fnum) == OK)
7342 rettv->vval.v_number = 0;
7343 }
7344 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007345 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 }
7347 }
7348}
7349
7350/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007351 * Translate a register type string to the yank type and block length
7352 */
7353 static int
7354get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7355{
7356 char_u *stropt = *pp;
7357 switch (*stropt)
7358 {
7359 case 'v': case 'c': // character-wise selection
7360 *yank_type = MCHAR;
7361 break;
7362 case 'V': case 'l': // line-wise selection
7363 *yank_type = MLINE;
7364 break;
7365 case 'b': case Ctrl_V: // block-wise selection
7366 *yank_type = MBLOCK;
7367 if (VIM_ISDIGIT(stropt[1]))
7368 {
7369 ++stropt;
7370 *block_len = getdigits(&stropt) - 1;
7371 --stropt;
7372 }
7373 break;
7374 default:
7375 return FAIL;
7376 }
7377 *pp = stropt;
7378 return OK;
7379}
7380
7381/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 * "setreg()" function
7383 */
7384 static void
7385f_setreg(typval_T *argvars, typval_T *rettv)
7386{
7387 int regname;
7388 char_u *strregname;
7389 char_u *stropt;
7390 char_u *strval;
7391 int append;
7392 char_u yank_type;
7393 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007394 typval_T *regcontents;
7395 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007397 pointreg = 0;
7398 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399 block_len = -1;
7400 yank_type = MAUTO;
7401 append = FALSE;
7402
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007403 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007404 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007405
7406 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007407 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007408 regname = *strregname;
7409 if (regname == 0 || regname == '@')
7410 regname = '"';
7411
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007412 if (argvars[1].v_type == VAR_DICT)
7413 {
7414 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007415 dictitem_T *di;
7416
7417 if (d == NULL || d->dv_hashtab.ht_used == 0)
7418 {
7419 // Empty dict, clear the register (like setreg(0, []))
7420 char_u *lstval[2] = {NULL, NULL};
7421 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7422 return;
7423 }
7424
7425 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007426 if (di != NULL)
7427 regcontents = &di->di_tv;
7428
7429 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7430 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007431 {
7432 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7433
7434 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007435 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007436 semsg(_(e_invargval), "value");
7437 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007438 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007439 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007440
7441 if (regname == '"')
7442 {
7443 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7444 if (stropt != NULL)
7445 {
7446 pointreg = *stropt;
7447 regname = pointreg;
7448 }
7449 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007450 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007451 pointreg = regname;
7452 }
7453 else
7454 regcontents = &argvars[1];
7455
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 if (argvars[2].v_type != VAR_UNKNOWN)
7457 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007458 if (yank_type != MAUTO)
7459 {
7460 semsg(_(e_toomanyarg), "setreg");
7461 return;
7462 }
7463
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007464 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007466 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 for (; *stropt != NUL; ++stropt)
7468 switch (*stropt)
7469 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 append = TRUE;
7472 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007473 default:
7474 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 }
7476 }
7477
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007478 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 {
7480 char_u **lstval;
7481 char_u **allocval;
7482 char_u buf[NUMBUFLEN];
7483 char_u **curval;
7484 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007485 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 listitem_T *li;
7487 int len;
7488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007489 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490 len = ll == NULL ? 0 : ll->lv_len;
7491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007492 // First half: use for pointers to result lines; second half: use for
7493 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007494 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 if (lstval == NULL)
7496 return;
7497 curval = lstval;
7498 allocval = lstval + len + 2;
7499 curallocval = allocval;
7500
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007501 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007503 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007504 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007505 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007506 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 if (strval == NULL)
7508 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007509 if (strval == buf)
7510 {
7511 // Need to make a copy, next tv_get_string_buf_chk() will
7512 // overwrite the string.
7513 strval = vim_strsave(buf);
7514 if (strval == NULL)
7515 goto free_lstval;
7516 *curallocval++ = strval;
7517 }
7518 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 }
7521 *curval++ = NULL;
7522
7523 write_reg_contents_lst(regname, lstval, -1,
7524 append, yank_type, block_len);
7525free_lstval:
7526 while (curallocval > allocval)
7527 vim_free(*--curallocval);
7528 vim_free(lstval);
7529 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007530 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007532 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007533 if (strval == NULL)
7534 return;
7535 write_reg_contents_ex(regname, strval, -1,
7536 append, yank_type, block_len);
7537 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007538 if (pointreg != 0)
7539 get_yank_register(pointreg, TRUE);
7540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 rettv->vval.v_number = 0;
7542}
7543
7544/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007545 * "settagstack()" function
7546 */
7547 static void
7548f_settagstack(typval_T *argvars, typval_T *rettv)
7549{
7550 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7551 win_T *wp;
7552 dict_T *d;
7553 int action = 'r';
7554
7555 rettv->vval.v_number = -1;
7556
7557 // first argument: window number or id
7558 wp = find_win_by_nr_or_id(&argvars[0]);
7559 if (wp == NULL)
7560 return;
7561
7562 // second argument: dict with items to set in the tag stack
7563 if (argvars[1].v_type != VAR_DICT)
7564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007565 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007566 return;
7567 }
7568 d = argvars[1].vval.v_dict;
7569 if (d == NULL)
7570 return;
7571
7572 // third argument: action - 'a' for append and 'r' for replace.
7573 // default is to replace the stack.
7574 if (argvars[2].v_type == VAR_UNKNOWN)
7575 action = 'r';
7576 else if (argvars[2].v_type == VAR_STRING)
7577 {
7578 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007579 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007580 if (actstr == NULL)
7581 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007582 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7583 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007584 action = *actstr;
7585 else
7586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007588 return;
7589 }
7590 }
7591 else
7592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007594 return;
7595 }
7596
7597 if (set_tagstack(wp, d, action) == OK)
7598 rettv->vval.v_number = 0;
7599}
7600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601#ifdef FEAT_CRYPT
7602/*
7603 * "sha256({string})" function
7604 */
7605 static void
7606f_sha256(typval_T *argvars, typval_T *rettv)
7607{
7608 char_u *p;
7609
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007610 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 rettv->vval.v_string = vim_strsave(
7612 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7613 rettv->v_type = VAR_STRING;
7614}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007615#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616
7617/*
7618 * "shellescape({string})" function
7619 */
7620 static void
7621f_shellescape(typval_T *argvars, typval_T *rettv)
7622{
Bram Moolenaar20615522017-06-05 18:46:26 +02007623 int do_special = non_zero_arg(&argvars[1]);
7624
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007626 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627 rettv->v_type = VAR_STRING;
7628}
7629
7630/*
7631 * shiftwidth() function
7632 */
7633 static void
7634f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7635{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007636 rettv->vval.v_number = 0;
7637
7638 if (argvars[0].v_type != VAR_UNKNOWN)
7639 {
7640 long col;
7641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007642 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007643 if (col < 0)
7644 return; // type error; errmsg already given
7645#ifdef FEAT_VARTABS
7646 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7647 return;
7648#endif
7649 }
7650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 rettv->vval.v_number = get_sw_value(curbuf);
7652}
7653
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654#ifdef FEAT_FLOAT
7655/*
7656 * "sin()" function
7657 */
7658 static void
7659f_sin(typval_T *argvars, typval_T *rettv)
7660{
7661 float_T f = 0.0;
7662
7663 rettv->v_type = VAR_FLOAT;
7664 if (get_float_arg(argvars, &f) == OK)
7665 rettv->vval.v_float = sin(f);
7666 else
7667 rettv->vval.v_float = 0.0;
7668}
7669
7670/*
7671 * "sinh()" function
7672 */
7673 static void
7674f_sinh(typval_T *argvars, typval_T *rettv)
7675{
7676 float_T f = 0.0;
7677
7678 rettv->v_type = VAR_FLOAT;
7679 if (get_float_arg(argvars, &f) == OK)
7680 rettv->vval.v_float = sinh(f);
7681 else
7682 rettv->vval.v_float = 0.0;
7683}
7684#endif
7685
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007686/*
7687 * "soundfold({word})" function
7688 */
7689 static void
7690f_soundfold(typval_T *argvars, typval_T *rettv)
7691{
7692 char_u *s;
7693
7694 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007695 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696#ifdef FEAT_SPELL
7697 rettv->vval.v_string = eval_soundfold(s);
7698#else
7699 rettv->vval.v_string = vim_strsave(s);
7700#endif
7701}
7702
7703/*
7704 * "spellbadword()" function
7705 */
7706 static void
7707f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7708{
7709 char_u *word = (char_u *)"";
7710 hlf_T attr = HLF_COUNT;
7711 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007712#ifdef FEAT_SPELL
7713 int wo_spell_save = curwin->w_p_spell;
7714
7715 if (!curwin->w_p_spell)
7716 {
7717 did_set_spelllang(curwin);
7718 curwin->w_p_spell = TRUE;
7719 }
7720
7721 if (*curwin->w_s->b_p_spl == NUL)
7722 {
7723 emsg(_(e_no_spell));
7724 curwin->w_p_spell = wo_spell_save;
7725 return;
7726 }
7727#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728
7729 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007730 {
7731#ifdef FEAT_SPELL
7732 curwin->w_p_spell = wo_spell_save;
7733#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007734 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736
7737#ifdef FEAT_SPELL
7738 if (argvars[0].v_type == VAR_UNKNOWN)
7739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007740 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7742 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007743 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007745 curwin->w_set_curswant = TRUE;
7746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007748 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007750 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 int capcol = -1;
7752
7753 if (str != NULL)
7754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007755 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 while (*str != NUL)
7757 {
7758 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7759 if (attr != HLF_COUNT)
7760 {
7761 word = str;
7762 break;
7763 }
7764 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007765 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007766 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 }
7768 }
7769 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007770 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771#endif
7772
7773 list_append_string(rettv->vval.v_list, word, len);
7774 list_append_string(rettv->vval.v_list, (char_u *)(
7775 attr == HLF_SPB ? "bad" :
7776 attr == HLF_SPR ? "rare" :
7777 attr == HLF_SPL ? "local" :
7778 attr == HLF_SPC ? "caps" :
7779 ""), -1);
7780}
7781
7782/*
7783 * "spellsuggest()" function
7784 */
7785 static void
7786f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7787{
7788#ifdef FEAT_SPELL
7789 char_u *str;
7790 int typeerr = FALSE;
7791 int maxcount;
7792 garray_T ga;
7793 int i;
7794 listitem_T *li;
7795 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007796 int wo_spell_save = curwin->w_p_spell;
7797
7798 if (!curwin->w_p_spell)
7799 {
7800 did_set_spelllang(curwin);
7801 curwin->w_p_spell = TRUE;
7802 }
7803
7804 if (*curwin->w_s->b_p_spl == NUL)
7805 {
7806 emsg(_(e_no_spell));
7807 curwin->w_p_spell = wo_spell_save;
7808 return;
7809 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810#endif
7811
7812 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007813 {
7814#ifdef FEAT_SPELL
7815 curwin->w_p_spell = wo_spell_save;
7816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007818 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819
7820#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007821 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007823 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 if (argvars[1].v_type != VAR_UNKNOWN)
7825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007826 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 if (maxcount <= 0)
7828 return;
7829 if (argvars[2].v_type != VAR_UNKNOWN)
7830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007831 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 if (typeerr)
7833 return;
7834 }
7835 }
7836 else
7837 maxcount = 25;
7838
7839 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7840
7841 for (i = 0; i < ga.ga_len; ++i)
7842 {
7843 str = ((char_u **)ga.ga_data)[i];
7844
7845 li = listitem_alloc();
7846 if (li == NULL)
7847 vim_free(str);
7848 else
7849 {
7850 li->li_tv.v_type = VAR_STRING;
7851 li->li_tv.v_lock = 0;
7852 li->li_tv.vval.v_string = str;
7853 list_append(rettv->vval.v_list, li);
7854 }
7855 }
7856 ga_clear(&ga);
7857 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007858 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859#endif
7860}
7861
7862 static void
7863f_split(typval_T *argvars, typval_T *rettv)
7864{
7865 char_u *str;
7866 char_u *end;
7867 char_u *pat = NULL;
7868 regmatch_T regmatch;
7869 char_u patbuf[NUMBUFLEN];
7870 char_u *save_cpo;
7871 int match;
7872 colnr_T col = 0;
7873 int keepempty = FALSE;
7874 int typeerr = FALSE;
7875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007876 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 save_cpo = p_cpo;
7878 p_cpo = (char_u *)"";
7879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007880 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 if (argvars[1].v_type != VAR_UNKNOWN)
7882 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007883 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 if (pat == NULL)
7885 typeerr = TRUE;
7886 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007887 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 }
7889 if (pat == NULL || *pat == NUL)
7890 pat = (char_u *)"[\\x01- ]\\+";
7891
7892 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007893 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007895 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007896
7897 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7898 if (regmatch.regprog != NULL)
7899 {
7900 regmatch.rm_ic = FALSE;
7901 while (*str != NUL || keepempty)
7902 {
7903 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007904 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 else
7906 match = vim_regexec_nl(&regmatch, str, col);
7907 if (match)
7908 end = regmatch.startp[0];
7909 else
7910 end = str + STRLEN(str);
7911 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7912 && *str != NUL && match && end < regmatch.endp[0]))
7913 {
7914 if (list_append_string(rettv->vval.v_list, str,
7915 (int)(end - str)) == FAIL)
7916 break;
7917 }
7918 if (!match)
7919 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007920 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 if (regmatch.endp[0] > str)
7922 col = 0;
7923 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007924 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 str = regmatch.endp[0];
7927 }
7928
7929 vim_regfree(regmatch.regprog);
7930 }
7931
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007932theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 p_cpo = save_cpo;
7934}
7935
7936#ifdef FEAT_FLOAT
7937/*
7938 * "sqrt()" function
7939 */
7940 static void
7941f_sqrt(typval_T *argvars, typval_T *rettv)
7942{
7943 float_T f = 0.0;
7944
7945 rettv->v_type = VAR_FLOAT;
7946 if (get_float_arg(argvars, &f) == OK)
7947 rettv->vval.v_float = sqrt(f);
7948 else
7949 rettv->vval.v_float = 0.0;
7950}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007951#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007953#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007954/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 * "str2float()" function
7956 */
7957 static void
7958f_str2float(typval_T *argvars, typval_T *rettv)
7959{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007960 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007961 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962
Bram Moolenaar08243d22017-01-10 16:12:29 +01007963 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 p = skipwhite(p + 1);
7965 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007966 if (isneg)
7967 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968 rettv->v_type = VAR_FLOAT;
7969}
7970#endif
7971
7972/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007973 * "str2list()" function
7974 */
7975 static void
7976f_str2list(typval_T *argvars, typval_T *rettv)
7977{
7978 char_u *p;
7979 int utf8 = FALSE;
7980
7981 if (rettv_list_alloc(rettv) == FAIL)
7982 return;
7983
7984 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02007985 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02007986
7987 p = tv_get_string(&argvars[0]);
7988
7989 if (has_mbyte || utf8)
7990 {
7991 int (*ptr2len)(char_u *);
7992 int (*ptr2char)(char_u *);
7993
7994 if (utf8 || enc_utf8)
7995 {
7996 ptr2len = utf_ptr2len;
7997 ptr2char = utf_ptr2char;
7998 }
7999 else
8000 {
8001 ptr2len = mb_ptr2len;
8002 ptr2char = mb_ptr2char;
8003 }
8004
8005 for ( ; *p != NUL; p += (*ptr2len)(p))
8006 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8007 }
8008 else
8009 for ( ; *p != NUL; ++p)
8010 list_append_number(rettv->vval.v_list, *p);
8011}
8012
8013/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 * "str2nr()" function
8015 */
8016 static void
8017f_str2nr(typval_T *argvars, typval_T *rettv)
8018{
8019 int base = 10;
8020 char_u *p;
8021 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008022 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008023 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024
8025 if (argvars[1].v_type != VAR_UNKNOWN)
8026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008027 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 if (base != 2 && base != 8 && base != 10 && base != 16)
8029 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008030 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 return;
8032 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008033 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8034 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 }
8036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008037 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008038 isneg = (*p == '-');
8039 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 p = skipwhite(p + 1);
8041 switch (base)
8042 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008043 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008044 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008045 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008047 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8048 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008049 if (isneg)
8050 rettv->vval.v_number = -n;
8051 else
8052 rettv->vval.v_number = n;
8053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054}
8055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056/*
8057 * "strgetchar()" function
8058 */
8059 static void
8060f_strgetchar(typval_T *argvars, typval_T *rettv)
8061{
8062 char_u *str;
8063 int len;
8064 int error = FALSE;
8065 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008066 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067
8068 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008069 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 if (str == NULL)
8071 return;
8072 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008073 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 if (error)
8075 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076
Bram Moolenaar13505972019-01-24 15:04:48 +01008077 while (charidx >= 0 && byteidx < len)
8078 {
8079 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008080 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008081 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8082 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008084 --charidx;
8085 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087}
8088
8089/*
8090 * "stridx()" function
8091 */
8092 static void
8093f_stridx(typval_T *argvars, typval_T *rettv)
8094{
8095 char_u buf[NUMBUFLEN];
8096 char_u *needle;
8097 char_u *haystack;
8098 char_u *save_haystack;
8099 char_u *pos;
8100 int start_idx;
8101
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008102 needle = tv_get_string_chk(&argvars[1]);
8103 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 rettv->vval.v_number = -1;
8105 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008106 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107
8108 if (argvars[2].v_type != VAR_UNKNOWN)
8109 {
8110 int error = FALSE;
8111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008112 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 if (error || start_idx >= (int)STRLEN(haystack))
8114 return;
8115 if (start_idx >= 0)
8116 haystack += start_idx;
8117 }
8118
8119 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8120 if (pos != NULL)
8121 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8122}
8123
8124/*
8125 * "string()" function
8126 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008127 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128f_string(typval_T *argvars, typval_T *rettv)
8129{
8130 char_u *tofree;
8131 char_u numbuf[NUMBUFLEN];
8132
8133 rettv->v_type = VAR_STRING;
8134 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8135 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008136 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 if (rettv->vval.v_string != NULL && tofree == NULL)
8138 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8139}
8140
8141/*
8142 * "strlen()" function
8143 */
8144 static void
8145f_strlen(typval_T *argvars, typval_T *rettv)
8146{
8147 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008148 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149}
8150
8151/*
8152 * "strchars()" function
8153 */
8154 static void
8155f_strchars(typval_T *argvars, typval_T *rettv)
8156{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008157 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159 varnumber_T len = 0;
8160 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161
8162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008163 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008165 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 else
8167 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8169 while (*s != NUL)
8170 {
8171 func_mb_ptr2char_adv(&s);
8172 ++len;
8173 }
8174 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175 }
8176}
8177
8178/*
8179 * "strdisplaywidth()" function
8180 */
8181 static void
8182f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8183{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008184 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 int col = 0;
8186
8187 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008188 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189
8190 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8191}
8192
8193/*
8194 * "strwidth()" function
8195 */
8196 static void
8197f_strwidth(typval_T *argvars, typval_T *rettv)
8198{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008199 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200
Bram Moolenaar13505972019-01-24 15:04:48 +01008201 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202}
8203
8204/*
8205 * "strcharpart()" function
8206 */
8207 static void
8208f_strcharpart(typval_T *argvars, typval_T *rettv)
8209{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210 char_u *p;
8211 int nchar;
8212 int nbyte = 0;
8213 int charlen;
8214 int len = 0;
8215 int slen;
8216 int error = FALSE;
8217
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008218 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219 slen = (int)STRLEN(p);
8220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008221 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 if (!error)
8223 {
8224 if (nchar > 0)
8225 while (nchar > 0 && nbyte < slen)
8226 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008227 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228 --nchar;
8229 }
8230 else
8231 nbyte = nchar;
8232 if (argvars[2].v_type != VAR_UNKNOWN)
8233 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008234 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235 while (charlen > 0 && nbyte + len < slen)
8236 {
8237 int off = nbyte + len;
8238
8239 if (off < 0)
8240 len += 1;
8241 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008242 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 --charlen;
8244 }
8245 }
8246 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008247 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 }
8249
8250 /*
8251 * Only return the overlap between the specified part and the actual
8252 * string.
8253 */
8254 if (nbyte < 0)
8255 {
8256 len += nbyte;
8257 nbyte = 0;
8258 }
8259 else if (nbyte > slen)
8260 nbyte = slen;
8261 if (len < 0)
8262 len = 0;
8263 else if (nbyte + len > slen)
8264 len = slen - nbyte;
8265
8266 rettv->v_type = VAR_STRING;
8267 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268}
8269
8270/*
8271 * "strpart()" function
8272 */
8273 static void
8274f_strpart(typval_T *argvars, typval_T *rettv)
8275{
8276 char_u *p;
8277 int n;
8278 int len;
8279 int slen;
8280 int error = FALSE;
8281
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008282 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 slen = (int)STRLEN(p);
8284
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008285 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 if (error)
8287 len = 0;
8288 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008289 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008291 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008293 // Only return the overlap between the specified part and the actual
8294 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 if (n < 0)
8296 {
8297 len += n;
8298 n = 0;
8299 }
8300 else if (n > slen)
8301 n = slen;
8302 if (len < 0)
8303 len = 0;
8304 else if (n + len > slen)
8305 len = slen - n;
8306
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008307 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8308 {
8309 int off;
8310
8311 // length in characters
8312 for (off = n; off < slen && len > 0; --len)
8313 off += mb_ptr2len(p + off);
8314 len = off - n;
8315 }
8316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008317 rettv->v_type = VAR_STRING;
8318 rettv->vval.v_string = vim_strnsave(p + n, len);
8319}
8320
8321/*
8322 * "strridx()" function
8323 */
8324 static void
8325f_strridx(typval_T *argvars, typval_T *rettv)
8326{
8327 char_u buf[NUMBUFLEN];
8328 char_u *needle;
8329 char_u *haystack;
8330 char_u *rest;
8331 char_u *lastmatch = NULL;
8332 int haystack_len, end_idx;
8333
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008334 needle = tv_get_string_chk(&argvars[1]);
8335 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336
8337 rettv->vval.v_number = -1;
8338 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008339 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340
8341 haystack_len = (int)STRLEN(haystack);
8342 if (argvars[2].v_type != VAR_UNKNOWN)
8343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008344 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008345 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008347 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 }
8349 else
8350 end_idx = haystack_len;
8351
8352 if (*needle == NUL)
8353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008354 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355 lastmatch = haystack + end_idx;
8356 }
8357 else
8358 {
8359 for (rest = haystack; *rest != '\0'; ++rest)
8360 {
8361 rest = (char_u *)strstr((char *)rest, (char *)needle);
8362 if (rest == NULL || rest > haystack + end_idx)
8363 break;
8364 lastmatch = rest;
8365 }
8366 }
8367
8368 if (lastmatch == NULL)
8369 rettv->vval.v_number = -1;
8370 else
8371 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8372}
8373
8374/*
8375 * "strtrans()" function
8376 */
8377 static void
8378f_strtrans(typval_T *argvars, typval_T *rettv)
8379{
8380 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008381 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382}
8383
8384/*
8385 * "submatch()" function
8386 */
8387 static void
8388f_submatch(typval_T *argvars, typval_T *rettv)
8389{
8390 int error = FALSE;
8391 int no;
8392 int retList = 0;
8393
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008394 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 if (error)
8396 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008397 if (no < 0 || no >= NSUBEXP)
8398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008399 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008400 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008401 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 if (error)
8405 return;
8406
8407 if (retList == 0)
8408 {
8409 rettv->v_type = VAR_STRING;
8410 rettv->vval.v_string = reg_submatch(no);
8411 }
8412 else
8413 {
8414 rettv->v_type = VAR_LIST;
8415 rettv->vval.v_list = reg_submatch_list(no);
8416 }
8417}
8418
8419/*
8420 * "substitute()" function
8421 */
8422 static void
8423f_substitute(typval_T *argvars, typval_T *rettv)
8424{
8425 char_u patbuf[NUMBUFLEN];
8426 char_u subbuf[NUMBUFLEN];
8427 char_u flagsbuf[NUMBUFLEN];
8428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008429 char_u *str = tv_get_string_chk(&argvars[0]);
8430 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008431 char_u *sub = NULL;
8432 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008435 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8436 expr = &argvars[2];
8437 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008438 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008441 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8442 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443 rettv->vval.v_string = NULL;
8444 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008445 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446}
8447
8448/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008449 * "swapinfo(swap_filename)" function
8450 */
8451 static void
8452f_swapinfo(typval_T *argvars, typval_T *rettv)
8453{
8454 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008455 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008456}
8457
8458/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008459 * "swapname(expr)" function
8460 */
8461 static void
8462f_swapname(typval_T *argvars, typval_T *rettv)
8463{
8464 buf_T *buf;
8465
8466 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008467 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008468 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8469 || buf->b_ml.ml_mfp->mf_fname == NULL)
8470 rettv->vval.v_string = NULL;
8471 else
8472 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8473}
8474
8475/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 * "synID(lnum, col, trans)" function
8477 */
8478 static void
8479f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8480{
8481 int id = 0;
8482#ifdef FEAT_SYN_HL
8483 linenr_T lnum;
8484 colnr_T col;
8485 int trans;
8486 int transerr = FALSE;
8487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008488 lnum = tv_get_lnum(argvars); // -1 on type error
8489 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008490 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491
8492 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8493 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8494 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8495#endif
8496
8497 rettv->vval.v_number = id;
8498}
8499
8500/*
8501 * "synIDattr(id, what [, mode])" function
8502 */
8503 static void
8504f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8505{
8506 char_u *p = NULL;
8507#ifdef FEAT_SYN_HL
8508 int id;
8509 char_u *what;
8510 char_u *mode;
8511 char_u modebuf[NUMBUFLEN];
8512 int modec;
8513
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008514 id = (int)tv_get_number(&argvars[0]);
8515 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 if (argvars[2].v_type != VAR_UNKNOWN)
8517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008518 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 modec = TOLOWER_ASC(mode[0]);
8520 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 }
8523 else
8524 {
8525#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8526 if (USE_24BIT)
8527 modec = 'g';
8528 else
8529#endif
8530 if (t_colors > 1)
8531 modec = 'c';
8532 else
8533 modec = 't';
8534 }
8535
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 switch (TOLOWER_ASC(what[0]))
8537 {
8538 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008539 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008541 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 p = highlight_has_attr(id, HL_BOLD, modec);
8543 break;
8544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008545 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 p = highlight_color(id, what, modec);
8547 break;
8548
8549 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008552 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 p = highlight_has_attr(id, HL_ITALIC, modec);
8554 break;
8555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008556 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008557 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 break;
8559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008560 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 p = highlight_has_attr(id, HL_INVERSE, modec);
8562 break;
8563
8564 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008565 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008567 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008568 else if (TOLOWER_ASC(what[1]) == 't' &&
8569 TOLOWER_ASC(what[2]) == 'r')
8570 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008571 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 p = highlight_has_attr(id, HL_STANDOUT, modec);
8573 break;
8574
8575 case 'u':
8576 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008577 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8579 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008580 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8582 break;
8583 }
8584
8585 if (p != NULL)
8586 p = vim_strsave(p);
8587#endif
8588 rettv->v_type = VAR_STRING;
8589 rettv->vval.v_string = p;
8590}
8591
8592/*
8593 * "synIDtrans(id)" function
8594 */
8595 static void
8596f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8597{
8598 int id;
8599
8600#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008601 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602
8603 if (id > 0)
8604 id = syn_get_final_id(id);
8605 else
8606#endif
8607 id = 0;
8608
8609 rettv->vval.v_number = id;
8610}
8611
8612/*
8613 * "synconcealed(lnum, col)" function
8614 */
8615 static void
8616f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8617{
8618#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8619 linenr_T lnum;
8620 colnr_T col;
8621 int syntax_flags = 0;
8622 int cchar;
8623 int matchid = 0;
8624 char_u str[NUMBUFLEN];
8625#endif
8626
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008627 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628
8629#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008630 lnum = tv_get_lnum(argvars); // -1 on type error
8631 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632
Bram Moolenaara80faa82020-04-12 19:37:17 +02008633 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634
8635 if (rettv_list_alloc(rettv) != FAIL)
8636 {
8637 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8638 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8639 && curwin->w_p_cole > 0)
8640 {
8641 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8642 syntax_flags = get_syntax_info(&matchid);
8643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008644 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8646 {
8647 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008648 if (cchar == NUL && curwin->w_p_cole == 1)
8649 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 if (cchar != NUL)
8651 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 if (has_mbyte)
8653 (*mb_char2bytes)(cchar, str);
8654 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 str[0] = cchar;
8656 }
8657 }
8658 }
8659
8660 list_append_number(rettv->vval.v_list,
8661 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008662 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663 list_append_string(rettv->vval.v_list, str, -1);
8664 list_append_number(rettv->vval.v_list, matchid);
8665 }
8666#endif
8667}
8668
8669/*
8670 * "synstack(lnum, col)" function
8671 */
8672 static void
8673f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8674{
8675#ifdef FEAT_SYN_HL
8676 linenr_T lnum;
8677 colnr_T col;
8678 int i;
8679 int id;
8680#endif
8681
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008682 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683
8684#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008685 lnum = tv_get_lnum(argvars); // -1 on type error
8686 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687
8688 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8689 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8690 && rettv_list_alloc(rettv) != FAIL)
8691 {
8692 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8693 for (i = 0; ; ++i)
8694 {
8695 id = syn_get_stack_item(i);
8696 if (id < 0)
8697 break;
8698 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8699 break;
8700 }
8701 }
8702#endif
8703}
8704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705/*
8706 * "tabpagebuflist()" function
8707 */
8708 static void
8709f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8710{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 tabpage_T *tp;
8712 win_T *wp = NULL;
8713
8714 if (argvars[0].v_type == VAR_UNKNOWN)
8715 wp = firstwin;
8716 else
8717 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 if (tp != NULL)
8720 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8721 }
8722 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8723 {
8724 for (; wp != NULL; wp = wp->w_next)
8725 if (list_append_number(rettv->vval.v_list,
8726 wp->w_buffer->b_fnum) == FAIL)
8727 break;
8728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729}
8730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 * "tagfiles()" function
8733 */
8734 static void
8735f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8736{
8737 char_u *fname;
8738 tagname_T tn;
8739 int first;
8740
8741 if (rettv_list_alloc(rettv) == FAIL)
8742 return;
8743 fname = alloc(MAXPATHL);
8744 if (fname == NULL)
8745 return;
8746
8747 for (first = TRUE; ; first = FALSE)
8748 if (get_tagfname(&tn, first, fname) == FAIL
8749 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8750 break;
8751 tagname_free(&tn);
8752 vim_free(fname);
8753}
8754
8755/*
8756 * "taglist()" function
8757 */
8758 static void
8759f_taglist(typval_T *argvars, typval_T *rettv)
8760{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008761 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 char_u *tag_pattern;
8763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008764 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765
8766 rettv->vval.v_number = FALSE;
8767 if (*tag_pattern == NUL)
8768 return;
8769
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008771 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008773 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774}
8775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776#ifdef FEAT_FLOAT
8777/*
8778 * "tan()" function
8779 */
8780 static void
8781f_tan(typval_T *argvars, typval_T *rettv)
8782{
8783 float_T f = 0.0;
8784
8785 rettv->v_type = VAR_FLOAT;
8786 if (get_float_arg(argvars, &f) == OK)
8787 rettv->vval.v_float = tan(f);
8788 else
8789 rettv->vval.v_float = 0.0;
8790}
8791
8792/*
8793 * "tanh()" function
8794 */
8795 static void
8796f_tanh(typval_T *argvars, typval_T *rettv)
8797{
8798 float_T f = 0.0;
8799
8800 rettv->v_type = VAR_FLOAT;
8801 if (get_float_arg(argvars, &f) == OK)
8802 rettv->vval.v_float = tanh(f);
8803 else
8804 rettv->vval.v_float = 0.0;
8805}
8806#endif
8807
8808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 * "tolower(string)" function
8810 */
8811 static void
8812f_tolower(typval_T *argvars, typval_T *rettv)
8813{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008815 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816}
8817
8818/*
8819 * "toupper(string)" function
8820 */
8821 static void
8822f_toupper(typval_T *argvars, typval_T *rettv)
8823{
8824 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008825 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826}
8827
8828/*
8829 * "tr(string, fromstr, tostr)" function
8830 */
8831 static void
8832f_tr(typval_T *argvars, typval_T *rettv)
8833{
8834 char_u *in_str;
8835 char_u *fromstr;
8836 char_u *tostr;
8837 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 int inlen;
8839 int fromlen;
8840 int tolen;
8841 int idx;
8842 char_u *cpstr;
8843 int cplen;
8844 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 char_u buf[NUMBUFLEN];
8846 char_u buf2[NUMBUFLEN];
8847 garray_T ga;
8848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008849 in_str = tv_get_string(&argvars[0]);
8850 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8851 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008853 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 rettv->v_type = VAR_STRING;
8855 rettv->vval.v_string = NULL;
8856 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008857 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 ga_init2(&ga, (int)sizeof(char), 80);
8859
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008861 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (STRLEN(fromstr) != STRLEN(tostr))
8863 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008865 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 ga_clear(&ga);
8867 return;
8868 }
8869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008870 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871 while (*in_str != NUL)
8872 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 if (has_mbyte)
8874 {
8875 inlen = (*mb_ptr2len)(in_str);
8876 cpstr = in_str;
8877 cplen = inlen;
8878 idx = 0;
8879 for (p = fromstr; *p != NUL; p += fromlen)
8880 {
8881 fromlen = (*mb_ptr2len)(p);
8882 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8883 {
8884 for (p = tostr; *p != NUL; p += tolen)
8885 {
8886 tolen = (*mb_ptr2len)(p);
8887 if (idx-- == 0)
8888 {
8889 cplen = tolen;
8890 cpstr = p;
8891 break;
8892 }
8893 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008894 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 goto error;
8896 break;
8897 }
8898 ++idx;
8899 }
8900
8901 if (first && cpstr == in_str)
8902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008903 // Check that fromstr and tostr have the same number of
8904 // (multi-byte) characters. Done only once when a character
8905 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906 first = FALSE;
8907 for (p = tostr; *p != NUL; p += tolen)
8908 {
8909 tolen = (*mb_ptr2len)(p);
8910 --idx;
8911 }
8912 if (idx != 0)
8913 goto error;
8914 }
8915
8916 (void)ga_grow(&ga, cplen);
8917 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8918 ga.ga_len += cplen;
8919
8920 in_str += inlen;
8921 }
8922 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008924 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 p = vim_strchr(fromstr, *in_str);
8926 if (p != NULL)
8927 ga_append(&ga, tostr[p - fromstr]);
8928 else
8929 ga_append(&ga, *in_str);
8930 ++in_str;
8931 }
8932 }
8933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008934 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 (void)ga_grow(&ga, 1);
8936 ga_append(&ga, NUL);
8937
8938 rettv->vval.v_string = ga.ga_data;
8939}
8940
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008941/*
8942 * "trim({expr})" function
8943 */
8944 static void
8945f_trim(typval_T *argvars, typval_T *rettv)
8946{
8947 char_u buf1[NUMBUFLEN];
8948 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008949 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008950 char_u *mask = NULL;
8951 char_u *tail;
8952 char_u *prev;
8953 char_u *p;
8954 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008955 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008956
8957 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008958 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008959 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008960 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008961
8962 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008963 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008964 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008965
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008967 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008968 int error = 0;
8969
8970 // leading or trailing characters to trim
8971 dir = (int)tv_get_number_chk(&argvars[2], &error);
8972 if (error)
8973 return;
8974 if (dir < 0 || dir > 2)
8975 {
8976 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8977 return;
8978 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008979 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008980 }
8981
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008982 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008983 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008984 // Trim leading characters
8985 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008986 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008987 c1 = PTR2CHAR(head);
8988 if (mask == NULL)
8989 {
8990 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008991 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008992 }
8993 else
8994 {
8995 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8996 if (c1 == PTR2CHAR(p))
8997 break;
8998 if (*p == NUL)
8999 break;
9000 }
9001 MB_PTR_ADV(head);
9002 }
9003 }
9004
9005 tail = head + STRLEN(head);
9006 if (dir == 0 || dir == 2)
9007 {
9008 // Trim trailing characters
9009 for (; tail > head; tail = prev)
9010 {
9011 prev = tail;
9012 MB_PTR_BACK(head, prev);
9013 c1 = PTR2CHAR(prev);
9014 if (mask == NULL)
9015 {
9016 if (c1 > ' ' && c1 != 0xa0)
9017 break;
9018 }
9019 else
9020 {
9021 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9022 if (c1 == PTR2CHAR(p))
9023 break;
9024 if (*p == NUL)
9025 break;
9026 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009027 }
9028 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009029 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009030}
9031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032#ifdef FEAT_FLOAT
9033/*
9034 * "trunc({float})" function
9035 */
9036 static void
9037f_trunc(typval_T *argvars, typval_T *rettv)
9038{
9039 float_T f = 0.0;
9040
9041 rettv->v_type = VAR_FLOAT;
9042 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009043 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9045 else
9046 rettv->vval.v_float = 0.0;
9047}
9048#endif
9049
9050/*
9051 * "type(expr)" function
9052 */
9053 static void
9054f_type(typval_T *argvars, typval_T *rettv)
9055{
9056 int n = -1;
9057
9058 switch (argvars[0].v_type)
9059 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009060 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9061 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009063 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9064 case VAR_LIST: n = VAR_TYPE_LIST; break;
9065 case VAR_DICT: n = VAR_TYPE_DICT; break;
9066 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9067 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9068 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009069 case VAR_JOB: n = VAR_TYPE_JOB; break;
9070 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009071 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009073 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009074 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009075 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 n = -1;
9077 break;
9078 }
9079 rettv->vval.v_number = n;
9080}
9081
9082/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 * "virtcol(string)" function
9084 */
9085 static void
9086f_virtcol(typval_T *argvars, typval_T *rettv)
9087{
9088 colnr_T vcol = 0;
9089 pos_T *fp;
9090 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009091 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009092
9093 fp = var2fpos(&argvars[0], FALSE, &fnum);
9094 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9095 && fnum == curbuf->b_fnum)
9096 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009097 // Limit the column to a valid value, getvvcol() doesn't check.
9098 if (fp->col < 0)
9099 fp->col = 0;
9100 else
9101 {
9102 len = (int)STRLEN(ml_get(fp->lnum));
9103 if (fp->col > len)
9104 fp->col = len;
9105 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 getvvcol(curwin, fp, NULL, NULL, &vcol);
9107 ++vcol;
9108 }
9109
9110 rettv->vval.v_number = vcol;
9111}
9112
9113/*
9114 * "visualmode()" function
9115 */
9116 static void
9117f_visualmode(typval_T *argvars, typval_T *rettv)
9118{
9119 char_u str[2];
9120
9121 rettv->v_type = VAR_STRING;
9122 str[0] = curbuf->b_visual_mode_eval;
9123 str[1] = NUL;
9124 rettv->vval.v_string = vim_strsave(str);
9125
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009126 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009127 if (non_zero_arg(&argvars[0]))
9128 curbuf->b_visual_mode_eval = NUL;
9129}
9130
9131/*
9132 * "wildmenumode()" function
9133 */
9134 static void
9135f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9136{
9137#ifdef FEAT_WILDMENU
9138 if (wild_menu_showing)
9139 rettv->vval.v_number = 1;
9140#endif
9141}
9142
9143/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009144 * "windowsversion()" function
9145 */
9146 static void
9147f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9148{
9149 rettv->v_type = VAR_STRING;
9150 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9151}
9152
9153/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 * "wordcount()" function
9155 */
9156 static void
9157f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9158{
9159 if (rettv_dict_alloc(rettv) == FAIL)
9160 return;
9161 cursor_pos_info(rettv->vval.v_dict);
9162}
9163
9164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 * "xor(expr, expr)" function
9166 */
9167 static void
9168f_xor(typval_T *argvars, typval_T *rettv)
9169{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009170 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9171 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009172}
9173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009174#endif // FEAT_EVAL