blob: 533f5b95dda10297886721d76e8db50cb69418a9 [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 Moolenaar9bd5d872020-09-06 21:47:48 +0200498 {"assert_fails", 1, 5, 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},
Bram Moolenaar635414d2020-09-11 22:25:15 +0200753 {"matchfuzzy", 2, 2, FEARG_1, ret_list_string, f_matchfuzzy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100754 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
755 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
756 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
757 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100758 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100759#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 f_menu_info
761#else
762 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100763#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100765 {"min", 1, 1, FEARG_1, ret_any, f_min},
766 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
767 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100768 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200769#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100770 f_mzeval
771#else
772 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100774 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100775 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
776 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
777 {"or", 2, 2, FEARG_1, ret_number, f_or},
778 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100779 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100781 f_perleval
782#else
783 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100785 },
786 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
787 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200788 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100789 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
790 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
791 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
792 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
793 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
794 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
795 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
796 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
797 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
798 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200799 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100800 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
801 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
802 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
803 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
804 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
805 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
806 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
807 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100808 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
809 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200810 {"prompt_getprompt", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_prompt_getprompt)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100811 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
812 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
813 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
814 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
815 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
816 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
817 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
818 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
819 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
820 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
821 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
822 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
823 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100824 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
825 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100826 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100828 f_py3eval
829#else
830 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100832 },
833 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100835 f_pyeval
836#else
837 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100839 },
840 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100841#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 f_pyxeval
843#else
844 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100845#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100847 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
848 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200849 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
850 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100851 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200852 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100853 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
854 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
855 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100856 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100857 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
858 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
859 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
860 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
861 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
862 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200863 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200864 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100865 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200866 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100867 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200868 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100869 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
870 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100871#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100872 f_rubyeval
873#else
874 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100875#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100876 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100877 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
878 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
879 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
880 {"screencol", 0, 0, 0, ret_number, f_screencol},
881 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
882 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
883 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200884 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200885 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100886 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
887 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
888 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200889 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100890 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
891 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
892 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
893 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200894 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100895 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
896 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
897 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
898 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
899 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
900 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
901 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
902 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
903 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
904 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
905 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
906 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
907 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
908 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100909 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100911 f_sha256
912#else
913 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100916 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
917 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100918 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
919 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
920 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
921 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
922 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
923 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
924 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
925 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
926 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200927 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100928 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
929 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200930 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100931 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
932 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
933 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
934 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100935 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
936 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
937 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
938 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100939 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100940 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
941 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100942 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100943 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
944 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
945 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
946 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
947 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100948 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100950 f_strftime
951#else
952 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100955 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
956 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
957 {"string", 1, 1, FEARG_1, ret_string, f_string},
958 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200959 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100960 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100961#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100962 f_strptime
963#else
964 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100965#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100966 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100967 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
968 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
969 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
970 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
971 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
972 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
973 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
974 {"synID", 3, 3, 0, ret_number, f_synID},
975 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
976 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
977 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
978 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
979 {"system", 1, 2, FEARG_1, ret_string, f_system},
980 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
981 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
982 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
983 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
984 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
985 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100986 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
987 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100988 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100989 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
990 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
991 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
992 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
993 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100994#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100995 f_term_getansicolors
996#else
997 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200998#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100999 },
1000 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
1001 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
1002 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
1003 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1004 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1005 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1006 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1007 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1008 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1009 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1010 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1011 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1012 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001013#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001014 f_term_setansicolors
1015#else
1016 NULL
1017#endif
1018 },
1019 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1020 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1021 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1022 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1023 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1024 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001025 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001026 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1027 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1028 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1029 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1030 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1031 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1032 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1033 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001034 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001035 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001036 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001037 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001038 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001039 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001040 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1041 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1042 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1043 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001044 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001045#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001046 f_test_scrollbar
1047#else
1048 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001049#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001051 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1052 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1053 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1054 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001055 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001056 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1057 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1058 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1059 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1060 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001061 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1062 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1063 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001064 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001065 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001066 {"type", 1, 1, FEARG_1, ret_number, f_type},
1067 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1068 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1069 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1070 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1071 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1072 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1073 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1074 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1075 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1076 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1077 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1078 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1079 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1080 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1081 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1082 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1083 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1084 {"wincol", 0, 0, 0, ret_number, f_wincol},
1085 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1086 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1087 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1088 {"winline", 0, 0, 0, ret_number, f_winline},
1089 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1090 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1091 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1092 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1093 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1094 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1095 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1096 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001097};
1098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001099/*
1100 * Function given to ExpandGeneric() to obtain the list of internal
1101 * or user defined function names.
1102 */
1103 char_u *
1104get_function_name(expand_T *xp, int idx)
1105{
1106 static int intidx = -1;
1107 char_u *name;
1108
1109 if (idx == 0)
1110 intidx = -1;
1111 if (intidx < 0)
1112 {
1113 name = get_user_func_name(xp, idx);
1114 if (name != NULL)
1115 return name;
1116 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001117 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001118 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001119 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001120 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001121 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001122 STRCAT(IObuff, ")");
1123 return IObuff;
1124 }
1125
1126 return NULL;
1127}
1128
1129/*
1130 * Function given to ExpandGeneric() to obtain the list of internal or
1131 * user defined variable or function names.
1132 */
1133 char_u *
1134get_expr_name(expand_T *xp, int idx)
1135{
1136 static int intidx = -1;
1137 char_u *name;
1138
1139 if (idx == 0)
1140 intidx = -1;
1141 if (intidx < 0)
1142 {
1143 name = get_function_name(xp, idx);
1144 if (name != NULL)
1145 return name;
1146 }
1147 return get_user_var_name(xp, ++intidx);
1148}
1149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001150/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001151 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001152 * Return index, or -1 if not found or "implemented" is TRUE and the function
1153 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001155 static int
1156find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001157{
1158 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001159 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001160 int cmp;
1161 int x;
1162
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001163 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001164
1165 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001166 while (first <= last)
1167 {
1168 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001169 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001170 if (cmp < 0)
1171 last = x - 1;
1172 else if (cmp > 0)
1173 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001174 else if (implemented && global_functions[x].f_func == NULL)
1175 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001176 else
1177 return x;
1178 }
1179 return -1;
1180}
1181
Bram Moolenaar15c47602020-03-26 22:16:48 +01001182/*
1183 * Find internal function "name" in table "global_functions".
1184 * Return index, or -1 if not found or the function is not implemented.
1185 */
1186 int
1187find_internal_func(char_u *name)
1188{
1189 return find_internal_func_opt(name, TRUE);
1190}
1191
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001192 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001193has_internal_func(char_u *name)
1194{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001195 return find_internal_func_opt(name, TRUE) >= 0;
1196}
1197
1198 static int
1199has_internal_func_name(char_u *name)
1200{
1201 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001202}
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 char *
1205internal_func_name(int idx)
1206{
1207 return global_functions[idx].f_name;
1208}
1209
1210 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001211internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001213 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214}
1215
1216/*
1217 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001218 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1219 * first argument, 2 if method base is second argument, etc. 9 if method base
1220 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 */
1222 int
1223check_internal_func(int idx, int argcount)
1224{
1225 int res;
1226 char *name;
1227
1228 if (argcount < global_functions[idx].f_min_argc)
1229 res = FCERR_TOOFEW;
1230 else if (argcount > global_functions[idx].f_max_argc)
1231 res = FCERR_TOOMANY;
1232 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001233 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
1235 name = internal_func_name(idx);
1236 if (res == FCERR_TOOMANY)
1237 semsg(_(e_toomanyarg), name);
1238 else
1239 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001240 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241}
1242
Bram Moolenaarac92e252019-08-03 21:58:38 +02001243 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001244call_internal_func(
1245 char_u *name,
1246 int argcount,
1247 typval_T *argvars,
1248 typval_T *rettv)
1249{
1250 int i;
1251
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001252 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001254 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001255 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001256 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001257 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001258 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001260 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001261 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001262}
1263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 void
1265call_internal_func_by_idx(
1266 int idx,
1267 typval_T *argvars,
1268 typval_T *rettv)
1269{
1270 global_functions[idx].f_func(argvars, rettv);
1271}
1272
Bram Moolenaarac92e252019-08-03 21:58:38 +02001273/*
1274 * Invoke a method for base->method().
1275 */
1276 int
1277call_internal_method(
1278 char_u *name,
1279 int argcount,
1280 typval_T *argvars,
1281 typval_T *rettv,
1282 typval_T *basetv)
1283{
1284 int i;
1285 int fi;
1286 typval_T argv[MAX_FUNC_ARGS + 1];
1287
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001288 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001289 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001290 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001291 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001292 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001293 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001294 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001295 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001296 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001297
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001298 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001299 {
1300 // base value goes last
1301 for (i = 0; i < argcount; ++i)
1302 argv[i] = argvars[i];
1303 argv[argcount] = *basetv;
1304 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001305 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001306 {
1307 // base value goes second
1308 argv[0] = argvars[0];
1309 argv[1] = *basetv;
1310 for (i = 1; i < argcount; ++i)
1311 argv[i + 1] = argvars[i];
1312 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001313 else if (global_functions[fi].f_argtype == FEARG_3)
1314 {
1315 // base value goes third
1316 argv[0] = argvars[0];
1317 argv[1] = argvars[1];
1318 argv[2] = *basetv;
1319 for (i = 2; i < argcount; ++i)
1320 argv[i + 1] = argvars[i];
1321 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001322 else if (global_functions[fi].f_argtype == FEARG_4)
1323 {
1324 // base value goes fourth
1325 argv[0] = argvars[0];
1326 argv[1] = argvars[1];
1327 argv[2] = argvars[2];
1328 argv[3] = *basetv;
1329 for (i = 3; i < argcount; ++i)
1330 argv[i + 1] = argvars[i];
1331 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001332 else
1333 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001334 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001335 argv[0] = *basetv;
1336 for (i = 0; i < argcount; ++i)
1337 argv[i + 1] = argvars[i];
1338 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001339 argv[argcount + 1].v_type = VAR_UNKNOWN;
1340
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001341 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001342 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343}
1344
1345/*
1346 * Return TRUE for a non-zero Number and a non-empty String.
1347 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001348 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349non_zero_arg(typval_T *argvars)
1350{
1351 return ((argvars[0].v_type == VAR_NUMBER
1352 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001353 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354 && argvars[0].vval.v_number == VVAL_TRUE)
1355 || (argvars[0].v_type == VAR_STRING
1356 && argvars[0].vval.v_string != NULL
1357 && *argvars[0].vval.v_string != NUL));
1358}
1359
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361/*
1362 * Get the float value of "argvars[0]" into "f".
1363 * Returns FAIL when the argument is not a Number or Float.
1364 */
1365 static int
1366get_float_arg(typval_T *argvars, float_T *f)
1367{
1368 if (argvars[0].v_type == VAR_FLOAT)
1369 {
1370 *f = argvars[0].vval.v_float;
1371 return OK;
1372 }
1373 if (argvars[0].v_type == VAR_NUMBER)
1374 {
1375 *f = (float_T)argvars[0].vval.v_number;
1376 return OK;
1377 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001378 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001379 return FAIL;
1380}
1381
1382/*
1383 * "abs(expr)" function
1384 */
1385 static void
1386f_abs(typval_T *argvars, typval_T *rettv)
1387{
1388 if (argvars[0].v_type == VAR_FLOAT)
1389 {
1390 rettv->v_type = VAR_FLOAT;
1391 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1392 }
1393 else
1394 {
1395 varnumber_T n;
1396 int error = FALSE;
1397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001398 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 if (error)
1400 rettv->vval.v_number = -1;
1401 else if (n > 0)
1402 rettv->vval.v_number = n;
1403 else
1404 rettv->vval.v_number = -n;
1405 }
1406}
1407
1408/*
1409 * "acos()" function
1410 */
1411 static void
1412f_acos(typval_T *argvars, typval_T *rettv)
1413{
1414 float_T f = 0.0;
1415
1416 rettv->v_type = VAR_FLOAT;
1417 if (get_float_arg(argvars, &f) == OK)
1418 rettv->vval.v_float = acos(f);
1419 else
1420 rettv->vval.v_float = 0.0;
1421}
1422#endif
1423
1424/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001425 * "and(expr, expr)" function
1426 */
1427 static void
1428f_and(typval_T *argvars, typval_T *rettv)
1429{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001430 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1431 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001432}
1433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001434#ifdef FEAT_FLOAT
1435/*
1436 * "asin()" function
1437 */
1438 static void
1439f_asin(typval_T *argvars, typval_T *rettv)
1440{
1441 float_T f = 0.0;
1442
1443 rettv->v_type = VAR_FLOAT;
1444 if (get_float_arg(argvars, &f) == OK)
1445 rettv->vval.v_float = asin(f);
1446 else
1447 rettv->vval.v_float = 0.0;
1448}
1449
1450/*
1451 * "atan()" function
1452 */
1453 static void
1454f_atan(typval_T *argvars, typval_T *rettv)
1455{
1456 float_T f = 0.0;
1457
1458 rettv->v_type = VAR_FLOAT;
1459 if (get_float_arg(argvars, &f) == OK)
1460 rettv->vval.v_float = atan(f);
1461 else
1462 rettv->vval.v_float = 0.0;
1463}
1464
1465/*
1466 * "atan2()" function
1467 */
1468 static void
1469f_atan2(typval_T *argvars, typval_T *rettv)
1470{
1471 float_T fx = 0.0, fy = 0.0;
1472
1473 rettv->v_type = VAR_FLOAT;
1474 if (get_float_arg(argvars, &fx) == OK
1475 && get_float_arg(&argvars[1], &fy) == OK)
1476 rettv->vval.v_float = atan2(fx, fy);
1477 else
1478 rettv->vval.v_float = 0.0;
1479}
1480#endif
1481
1482/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001483 * "balloon_show()" function
1484 */
1485#ifdef FEAT_BEVAL
1486 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001487f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1488{
1489 rettv->v_type = VAR_STRING;
1490 if (balloonEval != NULL)
1491 {
1492 if (balloonEval->msg == NULL)
1493 rettv->vval.v_string = NULL;
1494 else
1495 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1496 }
1497}
1498
1499 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001500f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1501{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001502 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001503 {
1504 if (argvars[0].v_type == VAR_LIST
1505# ifdef FEAT_GUI
1506 && !gui.in_use
1507# endif
1508 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001509 {
1510 list_T *l = argvars[0].vval.v_list;
1511
1512 // empty list removes the balloon
1513 post_balloon(balloonEval, NULL,
1514 l == NULL || l->lv_len == 0 ? NULL : l);
1515 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001516 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001517 {
1518 char_u *mesg = tv_get_string_chk(&argvars[0]);
1519
1520 if (mesg != NULL)
1521 // empty string removes the balloon
1522 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1523 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001524 }
1525}
1526
Bram Moolenaar669a8282017-11-19 20:13:05 +01001527# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001528 static void
1529f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1530{
1531 if (rettv_list_alloc(rettv) == OK)
1532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001533 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001534
1535 if (msg != NULL)
1536 {
1537 pumitem_T *array;
1538 int size = split_message(msg, &array);
1539 int i;
1540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001541 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001542 for (i = 1; i < size - 1; ++i)
1543 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001544 while (size > 0)
1545 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001546 vim_free(array);
1547 }
1548 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001549}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001550# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001551#endif
1552
1553/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001554 * Get the buffer from "arg" and give an error and return NULL if it is not
1555 * valid.
1556 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001557 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001558get_buf_arg(typval_T *arg)
1559{
1560 buf_T *buf;
1561
1562 ++emsg_off;
1563 buf = tv_get_buf(arg, FALSE);
1564 --emsg_off;
1565 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001566 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001567 return buf;
1568}
1569
1570/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571 * "byte2line(byte)" function
1572 */
1573 static void
1574f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1575{
1576#ifndef FEAT_BYTEOFF
1577 rettv->vval.v_number = -1;
1578#else
1579 long boff = 0;
1580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001581 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001582 if (boff < 0)
1583 rettv->vval.v_number = -1;
1584 else
1585 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1586 (linenr_T)0, &boff);
1587#endif
1588}
1589
1590 static void
1591byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1592{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 char_u *str;
1595 varnumber_T idx;
1596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 str = tv_get_string_chk(&argvars[0]);
1598 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599 rettv->vval.v_number = -1;
1600 if (str == NULL || idx < 0)
1601 return;
1602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 t = str;
1604 for ( ; idx > 0; idx--)
1605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001606 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 return;
1608 if (enc_utf8 && comp)
1609 t += utf_ptr2len(t);
1610 else
1611 t += (*mb_ptr2len)(t);
1612 }
1613 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614}
1615
1616/*
1617 * "byteidx()" function
1618 */
1619 static void
1620f_byteidx(typval_T *argvars, typval_T *rettv)
1621{
1622 byteidx(argvars, rettv, FALSE);
1623}
1624
1625/*
1626 * "byteidxcomp()" function
1627 */
1628 static void
1629f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1630{
1631 byteidx(argvars, rettv, TRUE);
1632}
1633
1634/*
1635 * "call(func, arglist [, dict])" function
1636 */
1637 static void
1638f_call(typval_T *argvars, typval_T *rettv)
1639{
1640 char_u *func;
1641 partial_T *partial = NULL;
1642 dict_T *selfdict = NULL;
1643
1644 if (argvars[1].v_type != VAR_LIST)
1645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647 return;
1648 }
1649 if (argvars[1].vval.v_list == NULL)
1650 return;
1651
1652 if (argvars[0].v_type == VAR_FUNC)
1653 func = argvars[0].vval.v_string;
1654 else if (argvars[0].v_type == VAR_PARTIAL)
1655 {
1656 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001657 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658 }
1659 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001660 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001662 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663
1664 if (argvars[2].v_type != VAR_UNKNOWN)
1665 {
1666 if (argvars[2].v_type != VAR_DICT)
1667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001668 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669 return;
1670 }
1671 selfdict = argvars[2].vval.v_dict;
1672 }
1673
1674 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1675}
1676
1677#ifdef FEAT_FLOAT
1678/*
1679 * "ceil({float})" function
1680 */
1681 static void
1682f_ceil(typval_T *argvars, typval_T *rettv)
1683{
1684 float_T f = 0.0;
1685
1686 rettv->v_type = VAR_FLOAT;
1687 if (get_float_arg(argvars, &f) == OK)
1688 rettv->vval.v_float = ceil(f);
1689 else
1690 rettv->vval.v_float = 0.0;
1691}
1692#endif
1693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694/*
1695 * "changenr()" function
1696 */
1697 static void
1698f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1699{
1700 rettv->vval.v_number = curbuf->b_u_seq_cur;
1701}
1702
1703/*
1704 * "char2nr(string)" function
1705 */
1706 static void
1707f_char2nr(typval_T *argvars, typval_T *rettv)
1708{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001709 if (has_mbyte)
1710 {
1711 int utf8 = 0;
1712
1713 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02001714 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715
1716 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001717 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720 }
1721 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001722 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723}
1724
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001725 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001726get_optional_window(typval_T *argvars, int idx)
1727{
1728 win_T *win = curwin;
1729
1730 if (argvars[idx].v_type != VAR_UNKNOWN)
1731 {
1732 win = find_win_by_nr_or_id(&argvars[idx]);
1733 if (win == NULL)
1734 {
1735 emsg(_(e_invalwindow));
1736 return NULL;
1737 }
1738 }
1739 return win;
1740}
1741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001742/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743 * "col(string)" function
1744 */
1745 static void
1746f_col(typval_T *argvars, typval_T *rettv)
1747{
1748 colnr_T col = 0;
1749 pos_T *fp;
1750 int fnum = curbuf->b_fnum;
1751
1752 fp = var2fpos(&argvars[0], FALSE, &fnum);
1753 if (fp != NULL && fnum == curbuf->b_fnum)
1754 {
1755 if (fp->col == MAXCOL)
1756 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001757 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001758 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1759 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1760 else
1761 col = MAXCOL;
1762 }
1763 else
1764 {
1765 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001766 // col(".") when the cursor is on the NUL at the end of the line
1767 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001768 if (virtual_active() && fp == &curwin->w_cursor)
1769 {
1770 char_u *p = ml_get_cursor();
1771
1772 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1773 curwin->w_virtcol - curwin->w_cursor.coladd))
1774 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001775 int l;
1776
1777 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1778 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 }
1780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001781 }
1782 }
1783 rettv->vval.v_number = col;
1784}
1785
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001786/*
1787 * "confirm(message, buttons[, default [, type]])" function
1788 */
1789 static void
1790f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1791{
1792#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1793 char_u *message;
1794 char_u *buttons = NULL;
1795 char_u buf[NUMBUFLEN];
1796 char_u buf2[NUMBUFLEN];
1797 int def = 1;
1798 int type = VIM_GENERIC;
1799 char_u *typestr;
1800 int error = FALSE;
1801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001802 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001803 if (message == NULL)
1804 error = TRUE;
1805 if (argvars[1].v_type != VAR_UNKNOWN)
1806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001807 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001808 if (buttons == NULL)
1809 error = TRUE;
1810 if (argvars[2].v_type != VAR_UNKNOWN)
1811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001812 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001813 if (argvars[3].v_type != VAR_UNKNOWN)
1814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001815 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001816 if (typestr == NULL)
1817 error = TRUE;
1818 else
1819 {
1820 switch (TOUPPER_ASC(*typestr))
1821 {
1822 case 'E': type = VIM_ERROR; break;
1823 case 'Q': type = VIM_QUESTION; break;
1824 case 'I': type = VIM_INFO; break;
1825 case 'W': type = VIM_WARNING; break;
1826 case 'G': type = VIM_GENERIC; break;
1827 }
1828 }
1829 }
1830 }
1831 }
1832
1833 if (buttons == NULL || *buttons == NUL)
1834 buttons = (char_u *)_("&Ok");
1835
1836 if (!error)
1837 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1838 def, NULL, FALSE);
1839#endif
1840}
1841
1842/*
1843 * "copy()" function
1844 */
1845 static void
1846f_copy(typval_T *argvars, typval_T *rettv)
1847{
1848 item_copy(&argvars[0], rettv, FALSE, 0);
1849}
1850
1851#ifdef FEAT_FLOAT
1852/*
1853 * "cos()" function
1854 */
1855 static void
1856f_cos(typval_T *argvars, typval_T *rettv)
1857{
1858 float_T f = 0.0;
1859
1860 rettv->v_type = VAR_FLOAT;
1861 if (get_float_arg(argvars, &f) == OK)
1862 rettv->vval.v_float = cos(f);
1863 else
1864 rettv->vval.v_float = 0.0;
1865}
1866
1867/*
1868 * "cosh()" function
1869 */
1870 static void
1871f_cosh(typval_T *argvars, typval_T *rettv)
1872{
1873 float_T f = 0.0;
1874
1875 rettv->v_type = VAR_FLOAT;
1876 if (get_float_arg(argvars, &f) == OK)
1877 rettv->vval.v_float = cosh(f);
1878 else
1879 rettv->vval.v_float = 0.0;
1880}
1881#endif
1882
1883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884 * "cursor(lnum, col)" function, or
1885 * "cursor(list)"
1886 *
1887 * Moves the cursor to the specified line and column.
1888 * Returns 0 when the position could be set, -1 otherwise.
1889 */
1890 static void
1891f_cursor(typval_T *argvars, typval_T *rettv)
1892{
1893 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 int set_curswant = TRUE;
1896
1897 rettv->vval.v_number = -1;
1898 if (argvars[1].v_type == VAR_UNKNOWN)
1899 {
1900 pos_T pos;
1901 colnr_T curswant = -1;
1902
1903 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 return;
1907 }
1908 line = pos.lnum;
1909 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 if (curswant >= 0)
1912 {
1913 curwin->w_curswant = curswant - 1;
1914 set_curswant = FALSE;
1915 }
1916 }
1917 else
1918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001919 line = tv_get_lnum(argvars);
1920 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001922 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001924 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 if (line > 0)
1927 curwin->w_cursor.lnum = line;
1928 if (col > 0)
1929 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001932 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001934 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 if (has_mbyte)
1936 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937
1938 curwin->w_set_curswant = set_curswant;
1939 rettv->vval.v_number = 0;
1940}
1941
Bram Moolenaar4f974752019-02-17 17:44:42 +01001942#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001943/*
1944 * "debugbreak()" function
1945 */
1946 static void
1947f_debugbreak(typval_T *argvars, typval_T *rettv)
1948{
1949 int pid;
1950
1951 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001952 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001953 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001954 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001955 else
1956 {
1957 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1958
1959 if (hProcess != NULL)
1960 {
1961 DebugBreakProcess(hProcess);
1962 CloseHandle(hProcess);
1963 rettv->vval.v_number = OK;
1964 }
1965 }
1966}
1967#endif
1968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001969/*
1970 * "deepcopy()" function
1971 */
1972 static void
1973f_deepcopy(typval_T *argvars, typval_T *rettv)
1974{
1975 int noref = 0;
1976 int copyID;
1977
1978 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02001979 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001980 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001981 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001982 else
1983 {
1984 copyID = get_copyID();
1985 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1986 }
1987}
1988
1989/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990 * "did_filetype()" function
1991 */
1992 static void
1993f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1994{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001996}
1997
1998/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001999 * "echoraw({expr})" function
2000 */
2001 static void
2002f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2003{
2004 char_u *str = tv_get_string_chk(&argvars[0]);
2005
2006 if (str != NULL && *str != NUL)
2007 {
2008 out_str(str);
2009 out_flush();
2010 }
2011}
2012
2013/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014 * "empty({expr})" function
2015 */
2016 static void
2017f_empty(typval_T *argvars, typval_T *rettv)
2018{
2019 int n = FALSE;
2020
2021 switch (argvars[0].v_type)
2022 {
2023 case VAR_STRING:
2024 case VAR_FUNC:
2025 n = argvars[0].vval.v_string == NULL
2026 || *argvars[0].vval.v_string == NUL;
2027 break;
2028 case VAR_PARTIAL:
2029 n = FALSE;
2030 break;
2031 case VAR_NUMBER:
2032 n = argvars[0].vval.v_number == 0;
2033 break;
2034 case VAR_FLOAT:
2035#ifdef FEAT_FLOAT
2036 n = argvars[0].vval.v_float == 0.0;
2037 break;
2038#endif
2039 case VAR_LIST:
2040 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002041 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 break;
2043 case VAR_DICT:
2044 n = argvars[0].vval.v_dict == NULL
2045 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2046 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002047 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002048 case VAR_SPECIAL:
2049 n = argvars[0].vval.v_number != VVAL_TRUE;
2050 break;
2051
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002052 case VAR_BLOB:
2053 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002054 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2055 break;
2056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002057 case VAR_JOB:
2058#ifdef FEAT_JOB_CHANNEL
2059 n = argvars[0].vval.v_job == NULL
2060 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2061 break;
2062#endif
2063 case VAR_CHANNEL:
2064#ifdef FEAT_JOB_CHANNEL
2065 n = argvars[0].vval.v_channel == NULL
2066 || !channel_is_open(argvars[0].vval.v_channel);
2067 break;
2068#endif
2069 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002070 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002072 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 n = TRUE;
2074 break;
2075 }
2076
2077 rettv->vval.v_number = n;
2078}
2079
2080/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002081 * "environ()" function
2082 */
2083 static void
2084f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2085{
2086#if !defined(AMIGA)
2087 int i = 0;
2088 char_u *entry, *value;
2089# ifdef MSWIN
2090 extern wchar_t **_wenviron;
2091# else
2092 extern char **environ;
2093# endif
2094
2095 if (rettv_dict_alloc(rettv) != OK)
2096 return;
2097
2098# ifdef MSWIN
2099 if (*_wenviron == NULL)
2100 return;
2101# else
2102 if (*environ == NULL)
2103 return;
2104# endif
2105
2106 for (i = 0; ; ++i)
2107 {
2108# ifdef MSWIN
2109 short_u *p;
2110
2111 if ((p = (short_u *)_wenviron[i]) == NULL)
2112 return;
2113 entry = utf16_to_enc(p, NULL);
2114# else
2115 if ((entry = (char_u *)environ[i]) == NULL)
2116 return;
2117 entry = vim_strsave(entry);
2118# endif
2119 if (entry == NULL) // out of memory
2120 return;
2121 if ((value = vim_strchr(entry, '=')) == NULL)
2122 {
2123 vim_free(entry);
2124 continue;
2125 }
2126 *value++ = NUL;
2127 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2128 vim_free(entry);
2129 }
2130#endif
2131}
2132
2133/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002134 * "escape({string}, {chars})" function
2135 */
2136 static void
2137f_escape(typval_T *argvars, typval_T *rettv)
2138{
2139 char_u buf[NUMBUFLEN];
2140
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2142 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 rettv->v_type = VAR_STRING;
2144}
2145
2146/*
2147 * "eval()" function
2148 */
2149 static void
2150f_eval(typval_T *argvars, typval_T *rettv)
2151{
2152 char_u *s, *p;
2153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002154 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 if (s != NULL)
2156 s = skipwhite(s);
2157
2158 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 {
2161 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002162 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 need_clr_eos = FALSE;
2164 rettv->v_type = VAR_NUMBER;
2165 rettv->vval.v_number = 0;
2166 }
2167 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002168 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169}
2170
2171/*
2172 * "eventhandler()" function
2173 */
2174 static void
2175f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2176{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002177 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178}
2179
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002180static garray_T redir_execute_ga;
2181
2182/*
2183 * Append "value[value_len]" to the execute() output.
2184 */
2185 void
2186execute_redir_str(char_u *value, int value_len)
2187{
2188 int len;
2189
2190 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002191 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002193 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002194 if (ga_grow(&redir_execute_ga, len) == OK)
2195 {
2196 mch_memmove((char *)redir_execute_ga.ga_data
2197 + redir_execute_ga.ga_len, value, len);
2198 redir_execute_ga.ga_len += len;
2199 }
2200}
2201
2202/*
2203 * Get next line from a list.
2204 * Called by do_cmdline() to get the next line.
2205 * Returns allocated string, or NULL for end of function.
2206 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 static char_u *
2208get_list_line(
2209 int c UNUSED,
2210 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002211 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002212 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002213{
2214 listitem_T **p = (listitem_T **)cookie;
2215 listitem_T *item = *p;
2216 char_u buf[NUMBUFLEN];
2217 char_u *s;
2218
2219 if (item == NULL)
2220 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002221 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 *p = item->li_next;
2223 return s == NULL ? NULL : vim_strsave(s);
2224}
2225
2226/*
2227 * "execute()" function
2228 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002229 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002230execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002231{
2232 char_u *cmd = NULL;
2233 list_T *list = NULL;
2234 int save_msg_silent = msg_silent;
2235 int save_emsg_silent = emsg_silent;
2236 int save_emsg_noredir = emsg_noredir;
2237 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002238 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002240 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002241 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242
2243 rettv->vval.v_string = NULL;
2244 rettv->v_type = VAR_STRING;
2245
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002246 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002247 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002248 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002249 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002250 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002251 return;
2252 ++list->lv_refcount;
2253 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002254 else if (argvars[arg_off].v_type == VAR_JOB
2255 || argvars[arg_off].v_type == VAR_CHANNEL)
2256 {
2257 emsg(_(e_inval_string));
2258 return;
2259 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002260 else
2261 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002262 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002263 if (cmd == NULL)
2264 return;
2265 }
2266
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002267 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 {
2269 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002270 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271
2272 if (s == NULL)
2273 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002274 if (*s == NUL)
2275 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276 if (STRNCMP(s, "silent", 6) == 0)
2277 ++msg_silent;
2278 if (STRCMP(s, "silent!") == 0)
2279 {
2280 emsg_silent = TRUE;
2281 emsg_noredir = TRUE;
2282 }
2283 }
2284 else
2285 ++msg_silent;
2286
2287 if (redir_execute)
2288 save_ga = redir_execute_ga;
2289 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2290 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002291 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002292 if (!echo_output)
2293 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294
2295 if (cmd != NULL)
2296 do_cmdline_cmd(cmd);
2297 else
2298 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002299 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002301 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002302 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002303 do_cmdline(NULL, get_list_line, (void *)&item,
2304 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2305 --list->lv_refcount;
2306 }
2307
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002308 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002309 if (ga_grow(&redir_execute_ga, 1) == OK)
2310 {
2311 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2312 rettv->vval.v_string = redir_execute_ga.ga_data;
2313 }
2314 else
2315 {
2316 ga_clear(&redir_execute_ga);
2317 rettv->vval.v_string = NULL;
2318 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002319 msg_silent = save_msg_silent;
2320 emsg_silent = save_emsg_silent;
2321 emsg_noredir = save_emsg_noredir;
2322
2323 redir_execute = save_redir_execute;
2324 if (redir_execute)
2325 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002326 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002327
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002328 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002329 if (echo_output)
2330 // When not working silently: put it in column zero. A following
2331 // "echon" will overwrite the message, unavoidably.
2332 msg_col = 0;
2333 else
2334 // When working silently: Put it back where it was, since nothing
2335 // should have been written.
2336 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337}
2338
2339/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002340 * "execute()" function
2341 */
2342 static void
2343f_execute(typval_T *argvars, typval_T *rettv)
2344{
2345 execute_common(argvars, rettv, 0);
2346}
2347
2348/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002349 * "exists()" function
2350 */
2351 static void
2352f_exists(typval_T *argvars, typval_T *rettv)
2353{
2354 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002357 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002360 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 if (mch_getenv(p + 1) != NULL)
2362 n = TRUE;
2363 else
2364 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002365 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002366 p = expand_env_save(p);
2367 if (p != NULL && *p != '$')
2368 n = TRUE;
2369 vim_free(p);
2370 }
2371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002372 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002374 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002378 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002380 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002382 else if (*p == '?') // internal function only
2383 {
2384 n = has_internal_func_name(p + 1);
2385 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 else if (*p == ':')
2387 {
2388 n = cmd_exists(p + 1);
2389 }
2390 else if (*p == '#')
2391 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002392 if (p[1] == '#')
2393 n = autocmd_supported(p + 2);
2394 else
2395 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002397 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002399 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 }
2401
2402 rettv->vval.v_number = n;
2403}
2404
2405#ifdef FEAT_FLOAT
2406/*
2407 * "exp()" function
2408 */
2409 static void
2410f_exp(typval_T *argvars, typval_T *rettv)
2411{
2412 float_T f = 0.0;
2413
2414 rettv->v_type = VAR_FLOAT;
2415 if (get_float_arg(argvars, &f) == OK)
2416 rettv->vval.v_float = exp(f);
2417 else
2418 rettv->vval.v_float = 0.0;
2419}
2420#endif
2421
2422/*
2423 * "expand()" function
2424 */
2425 static void
2426f_expand(typval_T *argvars, typval_T *rettv)
2427{
2428 char_u *s;
2429 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002430 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2432 expand_T xpc;
2433 int error = FALSE;
2434 char_u *result;
2435
2436 rettv->v_type = VAR_STRING;
2437 if (argvars[1].v_type != VAR_UNKNOWN
2438 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002439 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002441 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002442
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002443 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002444 if (*s == '%' || *s == '#' || *s == '<')
2445 {
2446 ++emsg_off;
2447 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2448 --emsg_off;
2449 if (rettv->v_type == VAR_LIST)
2450 {
2451 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2452 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002453 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 }
2455 else
2456 rettv->vval.v_string = result;
2457 }
2458 else
2459 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002460 // When the optional second argument is non-zero, don't remove matches
2461 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002463 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002464 options |= WILD_KEEP_ALL;
2465 if (!error)
2466 {
2467 ExpandInit(&xpc);
2468 xpc.xp_context = EXPAND_FILES;
2469 if (p_wic)
2470 options += WILD_ICASE;
2471 if (rettv->v_type == VAR_STRING)
2472 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2473 options, WILD_ALL);
2474 else if (rettv_list_alloc(rettv) != FAIL)
2475 {
2476 int i;
2477
2478 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2479 for (i = 0; i < xpc.xp_numfiles; i++)
2480 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2481 ExpandCleanup(&xpc);
2482 }
2483 }
2484 else
2485 rettv->vval.v_string = NULL;
2486 }
2487}
2488
2489/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002490 * "expandcmd()" function
2491 * Expand all the special characters in a command string.
2492 */
2493 static void
2494f_expandcmd(typval_T *argvars, typval_T *rettv)
2495{
2496 exarg_T eap;
2497 char_u *cmdstr;
2498 char *errormsg = NULL;
2499
2500 rettv->v_type = VAR_STRING;
2501 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2502
2503 memset(&eap, 0, sizeof(eap));
2504 eap.cmd = cmdstr;
2505 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002506 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002507 eap.usefilter = FALSE;
2508 eap.nextcmd = NULL;
2509 eap.cmdidx = CMD_USER;
2510
2511 expand_filename(&eap, &cmdstr, &errormsg);
2512 if (errormsg != NULL && *errormsg != NUL)
2513 emsg(errormsg);
2514
2515 rettv->vval.v_string = cmdstr;
2516}
2517
2518/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519 * "feedkeys()" function
2520 */
2521 static void
2522f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2523{
2524 int remap = TRUE;
2525 int insert = FALSE;
2526 char_u *keys, *flags;
2527 char_u nbuf[NUMBUFLEN];
2528 int typed = FALSE;
2529 int execute = FALSE;
2530 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002531 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 char_u *keys_esc;
2533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002534 // This is not allowed in the sandbox. If the commands would still be
2535 // executed in the sandbox it would be OK, but it probably happens later,
2536 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 if (check_secure())
2538 return;
2539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541
2542 if (argvars[1].v_type != VAR_UNKNOWN)
2543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 for ( ; *flags != NUL; ++flags)
2546 {
2547 switch (*flags)
2548 {
2549 case 'n': remap = FALSE; break;
2550 case 'm': remap = TRUE; break;
2551 case 't': typed = TRUE; break;
2552 case 'i': insert = TRUE; break;
2553 case 'x': execute = TRUE; break;
2554 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002555 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 }
2557 }
2558 }
2559
2560 if (*keys != NUL || execute)
2561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002562 // Need to escape K_SPECIAL and CSI before putting the string in the
2563 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002564 keys_esc = vim_strsave_escape_csi(keys);
2565 if (keys_esc != NULL)
2566 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002567 if (lowlevel)
2568 {
2569#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002570 int idx;
2571 int len = (int)STRLEN(keys);
2572
2573 for (idx = 0; idx < len; ++idx)
2574 {
2575 // if a CTRL-C was typed, set got_int, similar to what
2576 // happens in fill_input_buf()
2577 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2578 got_int = TRUE;
2579 add_to_input_buf(keys + idx, 1);
2580 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002581#else
2582 emsg(_("E980: lowlevel input not supported"));
2583#endif
2584 }
2585 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002586 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002587 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002589 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002590#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002591 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002592#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002593 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002594 typebuf_was_filled = TRUE;
2595 }
2596 vim_free(keys_esc);
2597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 if (execute)
2599 {
2600 int save_msg_scroll = msg_scroll;
2601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002602 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 msg_scroll = FALSE;
2604
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002605 if (!dangerous)
2606 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002607 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002608 if (!dangerous)
2609 --ex_normal_busy;
2610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611 msg_scroll |= save_msg_scroll;
2612 }
2613 }
2614 }
2615}
2616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617#ifdef FEAT_FLOAT
2618/*
2619 * "float2nr({float})" function
2620 */
2621 static void
2622f_float2nr(typval_T *argvars, typval_T *rettv)
2623{
2624 float_T f = 0.0;
2625
2626 if (get_float_arg(argvars, &f) == OK)
2627 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002628 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002629 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002630 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002631 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632 else
2633 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 }
2635}
2636
2637/*
2638 * "floor({float})" function
2639 */
2640 static void
2641f_floor(typval_T *argvars, typval_T *rettv)
2642{
2643 float_T f = 0.0;
2644
2645 rettv->v_type = VAR_FLOAT;
2646 if (get_float_arg(argvars, &f) == OK)
2647 rettv->vval.v_float = floor(f);
2648 else
2649 rettv->vval.v_float = 0.0;
2650}
2651
2652/*
2653 * "fmod()" function
2654 */
2655 static void
2656f_fmod(typval_T *argvars, typval_T *rettv)
2657{
2658 float_T fx = 0.0, fy = 0.0;
2659
2660 rettv->v_type = VAR_FLOAT;
2661 if (get_float_arg(argvars, &fx) == OK
2662 && get_float_arg(&argvars[1], &fy) == OK)
2663 rettv->vval.v_float = fmod(fx, fy);
2664 else
2665 rettv->vval.v_float = 0.0;
2666}
2667#endif
2668
2669/*
2670 * "fnameescape({string})" function
2671 */
2672 static void
2673f_fnameescape(typval_T *argvars, typval_T *rettv)
2674{
2675 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 rettv->v_type = VAR_STRING;
2678}
2679
2680/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 * "foreground()" function
2682 */
2683 static void
2684f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2685{
2686#ifdef FEAT_GUI
2687 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002690 return;
2691 }
2692#endif
2693#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695#endif
2696}
2697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002699common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700{
2701 char_u *s;
2702 char_u *name;
2703 int use_string = FALSE;
2704 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002705 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002706 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707
2708 if (argvars[0].v_type == VAR_FUNC)
2709 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002710 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 s = argvars[0].vval.v_string;
2712 }
2713 else if (argvars[0].v_type == VAR_PARTIAL
2714 && argvars[0].vval.v_partial != NULL)
2715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002716 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002718 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 }
2720 else
2721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002722 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002723 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002724 use_string = TRUE;
2725 }
2726
Bram Moolenaar843b8842016-08-21 14:36:15 +02002727 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002728 {
2729 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002730 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002731 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2732 if (*name != NUL)
2733 s = NULL;
2734 }
2735
Bram Moolenaar843b8842016-08-21 14:36:15 +02002736 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2737 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002738 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002739 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002740 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002741 ? find_func(trans_name, is_global, NULL) == NULL
2742 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002743 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002744 else
2745 {
2746 int dict_idx = 0;
2747 int arg_idx = 0;
2748 list_T *list = NULL;
2749
2750 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2751 {
2752 char sid_buf[25];
2753 int off = *s == 's' ? 2 : 5;
2754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002755 // Expand s: and <SID> into <SNR>nr_, so that the function can
2756 // also be called from another script. Using trans_function_name()
2757 // would also work, but some plugins depend on the name being
2758 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002759 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002760 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 if (name != NULL)
2762 {
2763 STRCPY(name, sid_buf);
2764 STRCAT(name, s + off);
2765 }
2766 }
2767 else
2768 name = vim_strsave(s);
2769
2770 if (argvars[1].v_type != VAR_UNKNOWN)
2771 {
2772 if (argvars[2].v_type != VAR_UNKNOWN)
2773 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 arg_idx = 1;
2776 dict_idx = 2;
2777 }
2778 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002779 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 dict_idx = 1;
2781 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002782 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 arg_idx = 1;
2784 if (dict_idx > 0)
2785 {
2786 if (argvars[dict_idx].v_type != VAR_DICT)
2787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002788 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002790 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791 }
2792 if (argvars[dict_idx].vval.v_dict == NULL)
2793 dict_idx = 0;
2794 }
2795 if (arg_idx > 0)
2796 {
2797 if (argvars[arg_idx].v_type != VAR_LIST)
2798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002799 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002801 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 }
2803 list = argvars[arg_idx].vval.v_list;
2804 if (list == NULL || list->lv_len == 0)
2805 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002806 else if (list->lv_len > MAX_FUNC_ARGS)
2807 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002808 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002809 vim_free(name);
2810 goto theend;
2811 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 }
2813 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002814 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002816 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 if (pt == NULL)
2820 vim_free(name);
2821 else
2822 {
2823 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2824 {
2825 listitem_T *li;
2826 int i = 0;
2827 int arg_len = 0;
2828 int lv_len = 0;
2829
2830 if (arg_pt != NULL)
2831 arg_len = arg_pt->pt_argc;
2832 if (list != NULL)
2833 lv_len = list->lv_len;
2834 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002835 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 if (pt->pt_argv == NULL)
2837 {
2838 vim_free(pt);
2839 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002840 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002841 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002842 for (i = 0; i < arg_len; i++)
2843 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2844 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002845 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002846 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002847 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002848 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002849 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 }
2851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002852 // For "function(dict.func, [], dict)" and "func" is a partial
2853 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854 if (dict_idx > 0)
2855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002857 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2858 ++pt->pt_dict->dv_refcount;
2859 }
2860 else if (arg_pt != NULL)
2861 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 // If the dict was bound automatically the result is also
2863 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864 pt->pt_dict = arg_pt->pt_dict;
2865 pt->pt_auto = arg_pt->pt_auto;
2866 if (pt->pt_dict != NULL)
2867 ++pt->pt_dict->dv_refcount;
2868 }
2869
2870 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002871 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2872 {
2873 pt->pt_func = arg_pt->pt_func;
2874 func_ptr_ref(pt->pt_func);
2875 vim_free(name);
2876 }
2877 else if (is_funcref)
2878 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002879 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002880 func_ptr_ref(pt->pt_func);
2881 vim_free(name);
2882 }
2883 else
2884 {
2885 pt->pt_name = name;
2886 func_ref(name);
2887 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002888 }
2889 rettv->v_type = VAR_PARTIAL;
2890 rettv->vval.v_partial = pt;
2891 }
2892 else
2893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002894 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895 rettv->v_type = VAR_FUNC;
2896 rettv->vval.v_string = name;
2897 func_ref(name);
2898 }
2899 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002900theend:
2901 vim_free(trans_name);
2902}
2903
2904/*
2905 * "funcref()" function
2906 */
2907 static void
2908f_funcref(typval_T *argvars, typval_T *rettv)
2909{
2910 common_function(argvars, rettv, TRUE);
2911}
2912
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002913 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002914ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002915{
2916 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2917 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002918 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002919}
2920
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002921/*
2922 * "function()" function
2923 */
2924 static void
2925f_function(typval_T *argvars, typval_T *rettv)
2926{
2927 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002928}
2929
2930/*
2931 * "garbagecollect()" function
2932 */
2933 static void
2934f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2935{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002936 // This is postponed until we are back at the toplevel, because we may be
2937 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 want_garbage_collect = TRUE;
2939
Bram Moolenaar2df47312020-09-05 17:30:44 +02002940 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 garbage_collect_at_exit = TRUE;
2942}
2943
2944/*
2945 * "get()" function
2946 */
2947 static void
2948f_get(typval_T *argvars, typval_T *rettv)
2949{
2950 listitem_T *li;
2951 list_T *l;
2952 dictitem_T *di;
2953 dict_T *d;
2954 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002955 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002957 if (argvars[0].v_type == VAR_BLOB)
2958 {
2959 int error = FALSE;
2960 int idx = tv_get_number_chk(&argvars[1], &error);
2961
2962 if (!error)
2963 {
2964 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002965 if (idx < 0)
2966 idx = blob_len(argvars[0].vval.v_blob) + idx;
2967 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2968 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002969 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002970 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002971 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002972 tv = rettv;
2973 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002974 }
2975 }
2976 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 {
2978 if ((l = argvars[0].vval.v_list) != NULL)
2979 {
2980 int error = FALSE;
2981
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 if (!error && li != NULL)
2984 tv = &li->li_tv;
2985 }
2986 }
2987 else if (argvars[0].v_type == VAR_DICT)
2988 {
2989 if ((d = argvars[0].vval.v_dict) != NULL)
2990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002991 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 if (di != NULL)
2993 tv = &di->di_tv;
2994 }
2995 }
2996 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2997 {
2998 partial_T *pt;
2999 partial_T fref_pt;
3000
3001 if (argvars[0].v_type == VAR_PARTIAL)
3002 pt = argvars[0].vval.v_partial;
3003 else
3004 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003005 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 fref_pt.pt_name = argvars[0].vval.v_string;
3007 pt = &fref_pt;
3008 }
3009
3010 if (pt != NULL)
3011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003013 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014
3015 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3016 {
3017 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003018 n = partial_name(pt);
3019 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 rettv->vval.v_string = NULL;
3021 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003022 {
3023 rettv->vval.v_string = vim_strsave(n);
3024 if (rettv->v_type == VAR_FUNC)
3025 func_ref(rettv->vval.v_string);
3026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 }
3028 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003029 {
3030 what_is_dict = TRUE;
3031 if (pt->pt_dict != NULL)
3032 rettv_dict_set(rettv, pt->pt_dict);
3033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034 else if (STRCMP(what, "args") == 0)
3035 {
3036 rettv->v_type = VAR_LIST;
3037 if (rettv_list_alloc(rettv) == OK)
3038 {
3039 int i;
3040
3041 for (i = 0; i < pt->pt_argc; ++i)
3042 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3043 }
3044 }
3045 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003046 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003047
3048 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3049 // third argument
3050 if (!what_is_dict)
3051 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 }
3053 }
3054 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003055 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003056
3057 if (tv == NULL)
3058 {
3059 if (argvars[2].v_type != VAR_UNKNOWN)
3060 copy_tv(&argvars[2], rettv);
3061 }
3062 else
3063 copy_tv(tv, rettv);
3064}
3065
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003066/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003067 * "getchangelist()" function
3068 */
3069 static void
3070f_getchangelist(typval_T *argvars, typval_T *rettv)
3071{
3072#ifdef FEAT_JUMPLIST
3073 buf_T *buf;
3074 int i;
3075 list_T *l;
3076 dict_T *d;
3077#endif
3078
3079 if (rettv_list_alloc(rettv) != OK)
3080 return;
3081
3082#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003083 if (argvars[0].v_type == VAR_UNKNOWN)
3084 buf = curbuf;
3085 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003086 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003087 if (buf == NULL)
3088 return;
3089
3090 l = list_alloc();
3091 if (l == NULL)
3092 return;
3093
3094 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3095 return;
3096 /*
3097 * The current window change list index tracks only the position in the
3098 * current buffer change list. For other buffers, use the change list
3099 * length as the current index.
3100 */
3101 list_append_number(rettv->vval.v_list,
3102 (varnumber_T)((buf == curwin->w_buffer)
3103 ? curwin->w_changelistidx : buf->b_changelistlen));
3104
3105 for (i = 0; i < buf->b_changelistlen; ++i)
3106 {
3107 if (buf->b_changelist[i].lnum == 0)
3108 continue;
3109 if ((d = dict_alloc()) == NULL)
3110 return;
3111 if (list_append_dict(l, d) == FAIL)
3112 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003113 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3114 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003115 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003116 }
3117#endif
3118}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119
3120/*
3121 * "getcharsearch()" function
3122 */
3123 static void
3124f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3125{
3126 if (rettv_dict_alloc(rettv) != FAIL)
3127 {
3128 dict_T *dict = rettv->vval.v_dict;
3129
Bram Moolenaare0be1672018-07-08 16:50:37 +02003130 dict_add_string(dict, "char", last_csearch());
3131 dict_add_number(dict, "forward", last_csearch_forward());
3132 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 }
3134}
3135
3136/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003137 * "getenv()" function
3138 */
3139 static void
3140f_getenv(typval_T *argvars, typval_T *rettv)
3141{
3142 int mustfree = FALSE;
3143 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3144
3145 if (p == NULL)
3146 {
3147 rettv->v_type = VAR_SPECIAL;
3148 rettv->vval.v_number = VVAL_NULL;
3149 return;
3150 }
3151 if (!mustfree)
3152 p = vim_strsave(p);
3153 rettv->vval.v_string = p;
3154 rettv->v_type = VAR_STRING;
3155}
3156
3157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 * "getfontname()" function
3159 */
3160 static void
3161f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3162{
3163 rettv->v_type = VAR_STRING;
3164 rettv->vval.v_string = NULL;
3165#ifdef FEAT_GUI
3166 if (gui.in_use)
3167 {
3168 GuiFont font;
3169 char_u *name = NULL;
3170
3171 if (argvars[0].v_type == VAR_UNKNOWN)
3172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003173 // Get the "Normal" font. Either the name saved by
3174 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 font = gui.norm_font;
3176 name = hl_get_font_name();
3177 }
3178 else
3179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003180 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182 return;
3183 font = gui_mch_get_font(name, FALSE);
3184 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 }
3187 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3188 if (argvars[0].v_type != VAR_UNKNOWN)
3189 gui_mch_free_font(font);
3190 }
3191#endif
3192}
3193
3194/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003195 * "getjumplist()" function
3196 */
3197 static void
3198f_getjumplist(typval_T *argvars, typval_T *rettv)
3199{
3200#ifdef FEAT_JUMPLIST
3201 win_T *wp;
3202 int i;
3203 list_T *l;
3204 dict_T *d;
3205#endif
3206
3207 if (rettv_list_alloc(rettv) != OK)
3208 return;
3209
3210#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003211 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003212 if (wp == NULL)
3213 return;
3214
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003215 cleanup_jumplist(wp, TRUE);
3216
Bram Moolenaar4f505882018-02-10 21:06:32 +01003217 l = list_alloc();
3218 if (l == NULL)
3219 return;
3220
3221 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3222 return;
3223 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3224
3225 for (i = 0; i < wp->w_jumplistlen; ++i)
3226 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003227 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3228 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003229 if ((d = dict_alloc()) == NULL)
3230 return;
3231 if (list_append_dict(l, d) == FAIL)
3232 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003233 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3234 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003237 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003238 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003239 }
3240#endif
3241}
3242
3243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 * "getpid()" function
3245 */
3246 static void
3247f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3248{
3249 rettv->vval.v_number = mch_get_pid();
3250}
3251
3252 static void
3253getpos_both(
3254 typval_T *argvars,
3255 typval_T *rettv,
3256 int getcurpos)
3257{
3258 pos_T *fp;
3259 list_T *l;
3260 int fnum = -1;
3261
3262 if (rettv_list_alloc(rettv) == OK)
3263 {
3264 l = rettv->vval.v_list;
3265 if (getcurpos)
3266 fp = &curwin->w_cursor;
3267 else
3268 fp = var2fpos(&argvars[0], TRUE, &fnum);
3269 if (fnum != -1)
3270 list_append_number(l, (varnumber_T)fnum);
3271 else
3272 list_append_number(l, (varnumber_T)0);
3273 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3274 : (varnumber_T)0);
3275 list_append_number(l, (fp != NULL)
3276 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3277 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003278 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 (varnumber_T)0);
3280 if (getcurpos)
3281 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003282 int save_set_curswant = curwin->w_set_curswant;
3283 colnr_T save_curswant = curwin->w_curswant;
3284 colnr_T save_virtcol = curwin->w_virtcol;
3285
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 update_curswant();
3287 list_append_number(l, curwin->w_curswant == MAXCOL ?
3288 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003289
3290 // Do not change "curswant", as it is unexpected that a get
3291 // function has a side effect.
3292 if (save_set_curswant)
3293 {
3294 curwin->w_set_curswant = save_set_curswant;
3295 curwin->w_curswant = save_curswant;
3296 curwin->w_virtcol = save_virtcol;
3297 curwin->w_valid &= ~VALID_VIRTCOL;
3298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
3300 }
3301 else
3302 rettv->vval.v_number = FALSE;
3303}
3304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305/*
3306 * "getcurpos()" function
3307 */
3308 static void
3309f_getcurpos(typval_T *argvars, typval_T *rettv)
3310{
3311 getpos_both(argvars, rettv, TRUE);
3312}
3313
3314/*
3315 * "getpos(string)" function
3316 */
3317 static void
3318f_getpos(typval_T *argvars, typval_T *rettv)
3319{
3320 getpos_both(argvars, rettv, FALSE);
3321}
3322
3323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 * "getreg()" function
3325 */
3326 static void
3327f_getreg(typval_T *argvars, typval_T *rettv)
3328{
3329 char_u *strregname;
3330 int regname;
3331 int arg2 = FALSE;
3332 int return_list = FALSE;
3333 int error = FALSE;
3334
3335 if (argvars[0].v_type != VAR_UNKNOWN)
3336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 error = strregname == NULL;
3339 if (argvars[1].v_type != VAR_UNKNOWN)
3340 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003341 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003343 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 }
3345 }
3346 else
3347 strregname = get_vim_var_str(VV_REG);
3348
3349 if (error)
3350 return;
3351
3352 regname = (strregname == NULL ? '"' : *strregname);
3353 if (regname == 0)
3354 regname = '"';
3355
3356 if (return_list)
3357 {
3358 rettv->v_type = VAR_LIST;
3359 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3360 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3361 if (rettv->vval.v_list == NULL)
3362 (void)rettv_list_alloc(rettv);
3363 else
3364 ++rettv->vval.v_list->lv_refcount;
3365 }
3366 else
3367 {
3368 rettv->v_type = VAR_STRING;
3369 rettv->vval.v_string = get_reg_contents(regname,
3370 arg2 ? GREG_EXPR_SRC : 0);
3371 }
3372}
3373
3374/*
3375 * "getregtype()" function
3376 */
3377 static void
3378f_getregtype(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *strregname;
3381 int regname;
3382 char_u buf[NUMBUFLEN + 2];
3383 long reglen = 0;
3384
3385 if (argvars[0].v_type != VAR_UNKNOWN)
3386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003387 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 {
3390 rettv->v_type = VAR_STRING;
3391 rettv->vval.v_string = NULL;
3392 return;
3393 }
3394 }
3395 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 strregname = get_vim_var_str(VV_REG);
3398
3399 regname = (strregname == NULL ? '"' : *strregname);
3400 if (regname == 0)
3401 regname = '"';
3402
3403 buf[0] = NUL;
3404 buf[1] = NUL;
3405 switch (get_reg_type(regname, &reglen))
3406 {
3407 case MLINE: buf[0] = 'V'; break;
3408 case MCHAR: buf[0] = 'v'; break;
3409 case MBLOCK:
3410 buf[0] = Ctrl_V;
3411 sprintf((char *)buf + 1, "%ld", reglen + 1);
3412 break;
3413 }
3414 rettv->v_type = VAR_STRING;
3415 rettv->vval.v_string = vim_strsave(buf);
3416}
3417
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003418/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003419 * "gettagstack()" function
3420 */
3421 static void
3422f_gettagstack(typval_T *argvars, typval_T *rettv)
3423{
3424 win_T *wp = curwin; // default is current window
3425
3426 if (rettv_dict_alloc(rettv) != OK)
3427 return;
3428
3429 if (argvars[0].v_type != VAR_UNKNOWN)
3430 {
3431 wp = find_win_by_nr_or_id(&argvars[0]);
3432 if (wp == NULL)
3433 return;
3434 }
3435
3436 get_tagstack(wp, rettv->vval.v_dict);
3437}
3438
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003439/*
3440 * "gettext()" function
3441 */
3442 static void
3443f_gettext(typval_T *argvars, typval_T *rettv)
3444{
3445 if (argvars[0].v_type != VAR_STRING
3446 || argvars[0].vval.v_string == NULL
3447 || *argvars[0].vval.v_string == NUL)
3448 {
3449 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3450 }
3451 else
3452 {
3453 rettv->v_type = VAR_STRING;
3454 rettv->vval.v_string = vim_strsave(
3455 (char_u *)_(argvars[0].vval.v_string));
3456 }
3457}
3458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003459// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460#include "version.h"
3461
3462/*
3463 * "has()" function
3464 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003465 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003466f_has(typval_T *argvars, typval_T *rettv)
3467{
3468 int i;
3469 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003470 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003472 typedef struct {
3473 char *name;
3474 short present;
3475 } has_item_T;
3476 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003478 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003479#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003480 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003481#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003482 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003483#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003484 },
3485 {"arp",
3486#if defined(AMIGA) && defined(FEAT_ARP)
3487 1
3488#else
3489 0
3490#endif
3491 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003492 {"haiku",
3493#ifdef __HAIKU__
3494 1
3495#else
3496 0
3497#endif
3498 },
3499 {"bsd",
3500#if defined(BSD) && !defined(MACOS_X)
3501 1
3502#else
3503 0
3504#endif
3505 },
3506 {"hpux",
3507#ifdef hpux
3508 1
3509#else
3510 0
3511#endif
3512 },
3513 {"linux",
3514#ifdef __linux__
3515 1
3516#else
3517 0
3518#endif
3519 },
3520 {"mac", // Mac OS X (and, once, Mac OS Classic)
3521#ifdef MACOS_X
3522 1
3523#else
3524 0
3525#endif
3526 },
3527 {"osx", // Mac OS X
3528#ifdef MACOS_X
3529 1
3530#else
3531 0
3532#endif
3533 },
3534 {"macunix", // Mac OS X, with the darwin feature
3535#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3536 1
3537#else
3538 0
3539#endif
3540 },
3541 {"osxdarwin", // synonym for macunix
3542#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3543 1
3544#else
3545 0
3546#endif
3547 },
3548 {"qnx",
3549#ifdef __QNX__
3550 1
3551#else
3552 0
3553#endif
3554 },
3555 {"sun",
3556#ifdef SUN_SYSTEM
3557 1
3558#else
3559 0
3560#endif
3561 },
3562 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003563#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003564 1
3565#else
3566 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003568 },
3569 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003571 1
3572#else
3573 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003575 },
3576 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003577#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003578 1
3579#else
3580 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003582 },
3583 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003584#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003585 1
3586#else
3587 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003589 },
3590 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003591#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003592 1
3593#else
3594 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003596 },
3597 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 1
3600#else
3601 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003603 },
3604 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 1
3607#else
3608 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003610 },
3611 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 1
3614#else
3615 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 },
3618 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003620 1
3621#else
3622 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003624 },
3625 {"autocmd", 1},
3626 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003627#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003628 1
3629#else
3630 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003632 },
3633 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003634#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003635 1
3636#else
3637 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003638#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003639 },
3640 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003641#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003642 1
3643#else
3644 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003646 },
3647 {"balloon_multiline",
3648#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3649 // MS-Windows requires runtime check, see below
3650 1
3651#else
3652 0
3653#endif
3654 },
3655 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003656#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003657 1
3658#else
3659 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003660#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003661 },
3662 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003664 1
3665#else
3666 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003668 },
3669 {"all_builtin_terms",
3670#if defined(ALL_BUILTIN_TCAPS)
3671 1
3672#else
3673 0
3674#endif
3675 },
3676 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003678 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003680 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003682 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003684 },
3685 {"byte_offset",
3686#ifdef FEAT_BYTEOFF
3687 1
3688#else
3689 0
3690#endif
3691 },
3692 {"channel",
3693#ifdef FEAT_JOB_CHANNEL
3694 1
3695#else
3696 0
3697#endif
3698 },
3699 {"cindent",
3700#ifdef FEAT_CINDENT
3701 1
3702#else
3703 0
3704#endif
3705 },
3706 {"clientserver",
3707#ifdef FEAT_CLIENTSERVER
3708 1
3709#else
3710 0
3711#endif
3712 },
3713 {"clipboard",
3714#ifdef FEAT_CLIPBOARD
3715 1
3716#else
3717 0
3718#endif
3719 },
3720 {"cmdline_compl", 1},
3721 {"cmdline_hist", 1},
3722 {"comments", 1},
3723 {"conceal",
3724#ifdef FEAT_CONCEAL
3725 1
3726#else
3727 0
3728#endif
3729 },
3730 {"cryptv",
3731#ifdef FEAT_CRYPT
3732 1
3733#else
3734 0
3735#endif
3736 },
3737 {"crypt-blowfish",
3738#ifdef FEAT_CRYPT
3739 1
3740#else
3741 0
3742#endif
3743 },
3744 {"crypt-blowfish2",
3745#ifdef FEAT_CRYPT
3746 1
3747#else
3748 0
3749#endif
3750 },
3751 {"cscope",
3752#ifdef FEAT_CSCOPE
3753 1
3754#else
3755 0
3756#endif
3757 },
3758 {"cursorbind", 1},
3759 {"cursorshape",
3760#ifdef CURSOR_SHAPE
3761 1
3762#else
3763 0
3764#endif
3765 },
3766 {"debug",
3767#ifdef DEBUG
3768 1
3769#else
3770 0
3771#endif
3772 },
3773 {"dialog_con",
3774#ifdef FEAT_CON_DIALOG
3775 1
3776#else
3777 0
3778#endif
3779 },
3780 {"dialog_gui",
3781#ifdef FEAT_GUI_DIALOG
3782 1
3783#else
3784 0
3785#endif
3786 },
3787 {"diff",
3788#ifdef FEAT_DIFF
3789 1
3790#else
3791 0
3792#endif
3793 },
3794 {"digraphs",
3795#ifdef FEAT_DIGRAPHS
3796 1
3797#else
3798 0
3799#endif
3800 },
3801 {"directx",
3802#ifdef FEAT_DIRECTX
3803 1
3804#else
3805 0
3806#endif
3807 },
3808 {"dnd",
3809#ifdef FEAT_DND
3810 1
3811#else
3812 0
3813#endif
3814 },
3815 {"emacs_tags",
3816#ifdef FEAT_EMACS_TAGS
3817 1
3818#else
3819 0
3820#endif
3821 },
3822 {"eval", 1}, // always present, of course!
3823 {"ex_extra", 1}, // graduated feature
3824 {"extra_search",
3825#ifdef FEAT_SEARCH_EXTRA
3826 1
3827#else
3828 0
3829#endif
3830 },
3831 {"file_in_path",
3832#ifdef FEAT_SEARCHPATH
3833 1
3834#else
3835 0
3836#endif
3837 },
3838 {"filterpipe",
3839#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3840 1
3841#else
3842 0
3843#endif
3844 },
3845 {"find_in_path",
3846#ifdef FEAT_FIND_ID
3847 1
3848#else
3849 0
3850#endif
3851 },
3852 {"float",
3853#ifdef FEAT_FLOAT
3854 1
3855#else
3856 0
3857#endif
3858 },
3859 {"folding",
3860#ifdef FEAT_FOLDING
3861 1
3862#else
3863 0
3864#endif
3865 },
3866 {"footer",
3867#ifdef FEAT_FOOTER
3868 1
3869#else
3870 0
3871#endif
3872 },
3873 {"fork",
3874#if !defined(USE_SYSTEM) && defined(UNIX)
3875 1
3876#else
3877 0
3878#endif
3879 },
3880 {"gettext",
3881#ifdef FEAT_GETTEXT
3882 1
3883#else
3884 0
3885#endif
3886 },
3887 {"gui",
3888#ifdef FEAT_GUI
3889 1
3890#else
3891 0
3892#endif
3893 },
3894 {"gui_neXtaw",
3895#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3896 1
3897#else
3898 0
3899#endif
3900 },
3901 {"gui_athena",
3902#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3903 1
3904#else
3905 0
3906#endif
3907 },
3908 {"gui_gtk",
3909#ifdef FEAT_GUI_GTK
3910 1
3911#else
3912 0
3913#endif
3914 },
3915 {"gui_gtk2",
3916#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3917 1
3918#else
3919 0
3920#endif
3921 },
3922 {"gui_gtk3",
3923#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3924 1
3925#else
3926 0
3927#endif
3928 },
3929 {"gui_gnome",
3930#ifdef FEAT_GUI_GNOME
3931 1
3932#else
3933 0
3934#endif
3935 },
3936 {"gui_haiku",
3937#ifdef FEAT_GUI_HAIKU
3938 1
3939#else
3940 0
3941#endif
3942 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003943 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003944 {"gui_motif",
3945#ifdef FEAT_GUI_MOTIF
3946 1
3947#else
3948 0
3949#endif
3950 },
3951 {"gui_photon",
3952#ifdef FEAT_GUI_PHOTON
3953 1
3954#else
3955 0
3956#endif
3957 },
3958 {"gui_win32",
3959#ifdef FEAT_GUI_MSWIN
3960 1
3961#else
3962 0
3963#endif
3964 },
3965 {"iconv",
3966#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3967 1
3968#else
3969 0
3970#endif
3971 },
3972 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003973 {"ipv6",
3974#ifdef FEAT_IPV6
3975 1
3976#else
3977 0
3978#endif
3979 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003980 {"job",
3981#ifdef FEAT_JOB_CHANNEL
3982 1
3983#else
3984 0
3985#endif
3986 },
3987 {"jumplist",
3988#ifdef FEAT_JUMPLIST
3989 1
3990#else
3991 0
3992#endif
3993 },
3994 {"keymap",
3995#ifdef FEAT_KEYMAP
3996 1
3997#else
3998 0
3999#endif
4000 },
4001 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4002 {"langmap",
4003#ifdef FEAT_LANGMAP
4004 1
4005#else
4006 0
4007#endif
4008 },
4009 {"libcall",
4010#ifdef FEAT_LIBCALL
4011 1
4012#else
4013 0
4014#endif
4015 },
4016 {"linebreak",
4017#ifdef FEAT_LINEBREAK
4018 1
4019#else
4020 0
4021#endif
4022 },
4023 {"lispindent",
4024#ifdef FEAT_LISP
4025 1
4026#else
4027 0
4028#endif
4029 },
4030 {"listcmds", 1},
4031 {"localmap", 1},
4032 {"lua",
4033#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4034 1
4035#else
4036 0
4037#endif
4038 },
4039 {"menu",
4040#ifdef FEAT_MENU
4041 1
4042#else
4043 0
4044#endif
4045 },
4046 {"mksession",
4047#ifdef FEAT_SESSION
4048 1
4049#else
4050 0
4051#endif
4052 },
4053 {"modify_fname", 1},
4054 {"mouse", 1},
4055 {"mouseshape",
4056#ifdef FEAT_MOUSESHAPE
4057 1
4058#else
4059 0
4060#endif
4061 },
4062 {"mouse_dec",
4063#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4064 1
4065#else
4066 0
4067#endif
4068 },
4069 {"mouse_gpm",
4070#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4071 1
4072#else
4073 0
4074#endif
4075 },
4076 {"mouse_jsbterm",
4077#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4078 1
4079#else
4080 0
4081#endif
4082 },
4083 {"mouse_netterm",
4084#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4085 1
4086#else
4087 0
4088#endif
4089 },
4090 {"mouse_pterm",
4091#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4092 1
4093#else
4094 0
4095#endif
4096 },
4097 {"mouse_sgr",
4098#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4099 1
4100#else
4101 0
4102#endif
4103 },
4104 {"mouse_sysmouse",
4105#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4106 1
4107#else
4108 0
4109#endif
4110 },
4111 {"mouse_urxvt",
4112#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4113 1
4114#else
4115 0
4116#endif
4117 },
4118 {"mouse_xterm",
4119#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4120 1
4121#else
4122 0
4123#endif
4124 },
4125 {"multi_byte", 1},
4126 {"multi_byte_ime",
4127#ifdef FEAT_MBYTE_IME
4128 1
4129#else
4130 0
4131#endif
4132 },
4133 {"multi_lang",
4134#ifdef FEAT_MULTI_LANG
4135 1
4136#else
4137 0
4138#endif
4139 },
4140 {"mzscheme",
4141#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4142 1
4143#else
4144 0
4145#endif
4146 },
4147 {"num64", 1},
4148 {"ole",
4149#ifdef FEAT_OLE
4150 1
4151#else
4152 0
4153#endif
4154 },
4155 {"packages",
4156#ifdef FEAT_EVAL
4157 1
4158#else
4159 0
4160#endif
4161 },
4162 {"path_extra",
4163#ifdef FEAT_PATH_EXTRA
4164 1
4165#else
4166 0
4167#endif
4168 },
4169 {"perl",
4170#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4171 1
4172#else
4173 0
4174#endif
4175 },
4176 {"persistent_undo",
4177#ifdef FEAT_PERSISTENT_UNDO
4178 1
4179#else
4180 0
4181#endif
4182 },
4183 {"python_compiled",
4184#if defined(FEAT_PYTHON)
4185 1
4186#else
4187 0
4188#endif
4189 },
4190 {"python_dynamic",
4191#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4192 1
4193#else
4194 0
4195#endif
4196 },
4197 {"python",
4198#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4199 1
4200#else
4201 0
4202#endif
4203 },
4204 {"pythonx",
4205#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4206 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4207 1
4208#else
4209 0
4210#endif
4211 },
4212 {"python3_compiled",
4213#if defined(FEAT_PYTHON3)
4214 1
4215#else
4216 0
4217#endif
4218 },
4219 {"python3_dynamic",
4220#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4221 1
4222#else
4223 0
4224#endif
4225 },
4226 {"python3",
4227#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"popupwin",
4234#ifdef FEAT_PROP_POPUP
4235 1
4236#else
4237 0
4238#endif
4239 },
4240 {"postscript",
4241#ifdef FEAT_POSTSCRIPT
4242 1
4243#else
4244 0
4245#endif
4246 },
4247 {"printer",
4248#ifdef FEAT_PRINTER
4249 1
4250#else
4251 0
4252#endif
4253 },
4254 {"profile",
4255#ifdef FEAT_PROFILE
4256 1
4257#else
4258 0
4259#endif
4260 },
4261 {"reltime",
4262#ifdef FEAT_RELTIME
4263 1
4264#else
4265 0
4266#endif
4267 },
4268 {"quickfix",
4269#ifdef FEAT_QUICKFIX
4270 1
4271#else
4272 0
4273#endif
4274 },
4275 {"rightleft",
4276#ifdef FEAT_RIGHTLEFT
4277 1
4278#else
4279 0
4280#endif
4281 },
4282 {"ruby",
4283#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4284 1
4285#else
4286 0
4287#endif
4288 },
4289 {"scrollbind", 1},
4290 {"showcmd",
4291#ifdef FEAT_CMDL_INFO
4292 1
4293#else
4294 0
4295#endif
4296 },
4297 {"cmdline_info",
4298#ifdef FEAT_CMDL_INFO
4299 1
4300#else
4301 0
4302#endif
4303 },
4304 {"signs",
4305#ifdef FEAT_SIGNS
4306 1
4307#else
4308 0
4309#endif
4310 },
4311 {"smartindent",
4312#ifdef FEAT_SMARTINDENT
4313 1
4314#else
4315 0
4316#endif
4317 },
4318 {"startuptime",
4319#ifdef STARTUPTIME
4320 1
4321#else
4322 0
4323#endif
4324 },
4325 {"statusline",
4326#ifdef FEAT_STL_OPT
4327 1
4328#else
4329 0
4330#endif
4331 },
4332 {"netbeans_intg",
4333#ifdef FEAT_NETBEANS_INTG
4334 1
4335#else
4336 0
4337#endif
4338 },
4339 {"sound",
4340#ifdef FEAT_SOUND
4341 1
4342#else
4343 0
4344#endif
4345 },
4346 {"spell",
4347#ifdef FEAT_SPELL
4348 1
4349#else
4350 0
4351#endif
4352 },
4353 {"syntax",
4354#ifdef FEAT_SYN_HL
4355 1
4356#else
4357 0
4358#endif
4359 },
4360 {"system",
4361#if defined(USE_SYSTEM) || !defined(UNIX)
4362 1
4363#else
4364 0
4365#endif
4366 },
4367 {"tag_binary",
4368#ifdef FEAT_TAG_BINS
4369 1
4370#else
4371 0
4372#endif
4373 },
4374 {"tcl",
4375#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"termguicolors",
4382#ifdef FEAT_TERMGUICOLORS
4383 1
4384#else
4385 0
4386#endif
4387 },
4388 {"terminal",
4389#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4390 1
4391#else
4392 0
4393#endif
4394 },
4395 {"terminfo",
4396#ifdef TERMINFO
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"termresponse",
4403#ifdef FEAT_TERMRESPONSE
4404 1
4405#else
4406 0
4407#endif
4408 },
4409 {"textobjects",
4410#ifdef FEAT_TEXTOBJ
4411 1
4412#else
4413 0
4414#endif
4415 },
4416 {"textprop",
4417#ifdef FEAT_PROP_POPUP
4418 1
4419#else
4420 0
4421#endif
4422 },
4423 {"tgetent",
4424#ifdef HAVE_TGETENT
4425 1
4426#else
4427 0
4428#endif
4429 },
4430 {"timers",
4431#ifdef FEAT_TIMERS
4432 1
4433#else
4434 0
4435#endif
4436 },
4437 {"title",
4438#ifdef FEAT_TITLE
4439 1
4440#else
4441 0
4442#endif
4443 },
4444 {"toolbar",
4445#ifdef FEAT_TOOLBAR
4446 1
4447#else
4448 0
4449#endif
4450 },
4451 {"unnamedplus",
4452#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4453 1
4454#else
4455 0
4456#endif
4457 },
4458 {"user-commands", 1}, // was accidentally included in 5.4
4459 {"user_commands", 1},
4460 {"vartabs",
4461#ifdef FEAT_VARTABS
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"vertsplit", 1},
4468 {"viminfo",
4469#ifdef FEAT_VIMINFO
4470 1
4471#else
4472 0
4473#endif
4474 },
4475 {"vimscript-1", 1},
4476 {"vimscript-2", 1},
4477 {"vimscript-3", 1},
4478 {"vimscript-4", 1},
4479 {"virtualedit", 1},
4480 {"visual", 1},
4481 {"visualextra", 1},
4482 {"vreplace", 1},
4483 {"vtp",
4484#ifdef FEAT_VTP
4485 1
4486#else
4487 0
4488#endif
4489 },
4490 {"wildignore",
4491#ifdef FEAT_WILDIGN
4492 1
4493#else
4494 0
4495#endif
4496 },
4497 {"wildmenu",
4498#ifdef FEAT_WILDMENU
4499 1
4500#else
4501 0
4502#endif
4503 },
4504 {"windows", 1},
4505 {"winaltkeys",
4506#ifdef FEAT_WAK
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"writebackup",
4513#ifdef FEAT_WRITEBACKUP
4514 1
4515#else
4516 0
4517#endif
4518 },
4519 {"xim",
4520#ifdef FEAT_XIM
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"xfontset",
4527#ifdef FEAT_XFONTSET
4528 1
4529#else
4530 0
4531#endif
4532 },
4533 {"xpm",
4534#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4535 1
4536#else
4537 0
4538#endif
4539 },
4540 {"xpm_w32", // for backward compatibility
4541#ifdef FEAT_XPM_W32
4542 1
4543#else
4544 0
4545#endif
4546 },
4547 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 1
4550#else
4551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 },
4554 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 1
4557#else
4558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 },
4561 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 1
4564#else
4565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 },
4568 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 1
4571#else
4572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 },
4575 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 1
4578#else
4579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 },
4582 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583 };
4584
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004585 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004586 for (i = 0; has_list[i].name != NULL; ++i)
4587 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 x = TRUE;
4590 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 break;
4592 }
4593
Bram Moolenaar79296512020-03-22 16:17:14 +01004594 // features also in has_list[] but sometimes enabled at runtime
4595 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004597 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004598 {
4599 // intentionally empty
4600 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004601#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004602 else if (STRICMP(name, "balloon_multiline") == 0)
4603 n = multiline_balloon_available();
4604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004605#ifdef VIMDLL
4606 else if (STRICMP(name, "filterpipe") == 0)
4607 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608#endif
4609#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4610 else if (STRICMP(name, "iconv") == 0)
4611 n = iconv_enabled(FALSE);
4612#endif
4613#ifdef DYNAMIC_LUA
4614 else if (STRICMP(name, "lua") == 0)
4615 n = lua_enabled(FALSE);
4616#endif
4617#ifdef DYNAMIC_MZSCHEME
4618 else if (STRICMP(name, "mzscheme") == 0)
4619 n = mzscheme_enabled(FALSE);
4620#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004621#ifdef DYNAMIC_PERL
4622 else if (STRICMP(name, "perl") == 0)
4623 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625#ifdef DYNAMIC_PYTHON
4626 else if (STRICMP(name, "python") == 0)
4627 n = python_enabled(FALSE);
4628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#ifdef DYNAMIC_PYTHON3
4630 else if (STRICMP(name, "python3") == 0)
4631 n = python3_enabled(FALSE);
4632#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004633#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4634 else if (STRICMP(name, "pythonx") == 0)
4635 {
4636# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4637 if (p_pyx == 0)
4638 n = python3_enabled(FALSE) || python_enabled(FALSE);
4639 else if (p_pyx == 3)
4640 n = python3_enabled(FALSE);
4641 else if (p_pyx == 2)
4642 n = python_enabled(FALSE);
4643# elif defined(DYNAMIC_PYTHON)
4644 n = python_enabled(FALSE);
4645# elif defined(DYNAMIC_PYTHON3)
4646 n = python3_enabled(FALSE);
4647# endif
4648 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004649#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004650#ifdef DYNAMIC_RUBY
4651 else if (STRICMP(name, "ruby") == 0)
4652 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004653#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004654#ifdef DYNAMIC_TCL
4655 else if (STRICMP(name, "tcl") == 0)
4656 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004657#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004658#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004659 else if (STRICMP(name, "terminal") == 0)
4660 n = terminal_enabled();
4661#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 }
4663
Bram Moolenaar79296512020-03-22 16:17:14 +01004664 // features not in has_list[]
4665 if (x == FALSE)
4666 {
4667 if (STRNICMP(name, "patch", 5) == 0)
4668 {
4669 x = TRUE;
4670 if (name[5] == '-'
4671 && STRLEN(name) >= 11
4672 && vim_isdigit(name[6])
4673 && vim_isdigit(name[8])
4674 && vim_isdigit(name[10]))
4675 {
4676 int major = atoi((char *)name + 6);
4677 int minor = atoi((char *)name + 8);
4678
4679 // Expect "patch-9.9.01234".
4680 n = (major < VIM_VERSION_MAJOR
4681 || (major == VIM_VERSION_MAJOR
4682 && (minor < VIM_VERSION_MINOR
4683 || (minor == VIM_VERSION_MINOR
4684 && has_patch(atoi((char *)name + 10))))));
4685 }
4686 else
4687 n = has_patch(atoi((char *)name + 5));
4688 }
4689 else if (STRICMP(name, "vim_starting") == 0)
4690 {
4691 x = TRUE;
4692 n = (starting != 0);
4693 }
4694 else if (STRICMP(name, "ttyin") == 0)
4695 {
4696 x = TRUE;
4697 n = mch_input_isatty();
4698 }
4699 else if (STRICMP(name, "ttyout") == 0)
4700 {
4701 x = TRUE;
4702 n = stdout_isatty;
4703 }
4704 else if (STRICMP(name, "multi_byte_encoding") == 0)
4705 {
4706 x = TRUE;
4707 n = has_mbyte;
4708 }
4709 else if (STRICMP(name, "gui_running") == 0)
4710 {
4711 x = TRUE;
4712#ifdef FEAT_GUI
4713 n = (gui.in_use || gui.starting);
4714#endif
4715 }
4716 else if (STRICMP(name, "browse") == 0)
4717 {
4718 x = TRUE;
4719#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4720 n = gui.in_use; // gui_mch_browse() works when GUI is running
4721#endif
4722 }
4723 else if (STRICMP(name, "syntax_items") == 0)
4724 {
4725 x = TRUE;
4726#ifdef FEAT_SYN_HL
4727 n = syntax_present(curwin);
4728#endif
4729 }
4730 else if (STRICMP(name, "vcon") == 0)
4731 {
4732 x = TRUE;
4733#ifdef FEAT_VTP
4734 n = is_term_win32() && has_vtp_working();
4735#endif
4736 }
4737 else if (STRICMP(name, "netbeans_enabled") == 0)
4738 {
4739 x = TRUE;
4740#ifdef FEAT_NETBEANS_INTG
4741 n = netbeans_active();
4742#endif
4743 }
4744 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4745 {
4746 x = TRUE;
4747#ifdef FEAT_MOUSE_GPM
4748 n = gpm_enabled();
4749#endif
4750 }
4751 else if (STRICMP(name, "conpty") == 0)
4752 {
4753 x = TRUE;
4754#if defined(FEAT_TERMINAL) && defined(MSWIN)
4755 n = use_conpty();
4756#endif
4757 }
4758 else if (STRICMP(name, "clipboard_working") == 0)
4759 {
4760 x = TRUE;
4761#ifdef FEAT_CLIPBOARD
4762 n = clip_star.available;
4763#endif
4764 }
4765 }
4766
Bram Moolenaar04637e22020-09-05 18:45:29 +02004767 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01004768 // return whether feature could ever be enabled
4769 rettv->vval.v_number = x;
4770 else
4771 // return whether feature is enabled
4772 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773}
4774
4775/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004776 * "haslocaldir()" function
4777 */
4778 static void
4779f_haslocaldir(typval_T *argvars, typval_T *rettv)
4780{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004781 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782 win_T *wp = NULL;
4783
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004784 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4785
4786 // Check for window-local and tab-local directories
4787 if (wp != NULL && wp->w_localdir != NULL)
4788 rettv->vval.v_number = 1;
4789 else if (tp != NULL && tp->tp_localdir != NULL)
4790 rettv->vval.v_number = 2;
4791 else
4792 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793}
4794
4795/*
4796 * "hasmapto()" function
4797 */
4798 static void
4799f_hasmapto(typval_T *argvars, typval_T *rettv)
4800{
4801 char_u *name;
4802 char_u *mode;
4803 char_u buf[NUMBUFLEN];
4804 int abbr = FALSE;
4805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004806 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004807 if (argvars[1].v_type == VAR_UNKNOWN)
4808 mode = (char_u *)"nvo";
4809 else
4810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004811 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004812 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02004813 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004814 }
4815
4816 if (map_to_exists(name, mode, abbr))
4817 rettv->vval.v_number = TRUE;
4818 else
4819 rettv->vval.v_number = FALSE;
4820}
4821
4822/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004823 * "highlightID(name)" function
4824 */
4825 static void
4826f_hlID(typval_T *argvars, typval_T *rettv)
4827{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004828 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829}
4830
4831/*
4832 * "highlight_exists()" function
4833 */
4834 static void
4835f_hlexists(typval_T *argvars, typval_T *rettv)
4836{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004837 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838}
4839
4840/*
4841 * "hostname()" function
4842 */
4843 static void
4844f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4845{
4846 char_u hostname[256];
4847
4848 mch_get_host_name(hostname, 256);
4849 rettv->v_type = VAR_STRING;
4850 rettv->vval.v_string = vim_strsave(hostname);
4851}
4852
4853/*
4854 * iconv() function
4855 */
4856 static void
4857f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4858{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859 char_u buf1[NUMBUFLEN];
4860 char_u buf2[NUMBUFLEN];
4861 char_u *from, *to, *str;
4862 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863
4864 rettv->v_type = VAR_STRING;
4865 rettv->vval.v_string = NULL;
4866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004867 str = tv_get_string(&argvars[0]);
4868 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4869 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 vimconv.vc_type = CONV_NONE;
4871 convert_setup(&vimconv, from, to);
4872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 if (vimconv.vc_type == CONV_NONE)
4875 rettv->vval.v_string = vim_strsave(str);
4876 else
4877 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4878
4879 convert_setup(&vimconv, NULL, NULL);
4880 vim_free(from);
4881 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882}
4883
4884/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 * "index()" function
4886 */
4887 static void
4888f_index(typval_T *argvars, typval_T *rettv)
4889{
4890 list_T *l;
4891 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004892 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893 long idx = 0;
4894 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004895 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896
4897 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004898 if (argvars[0].v_type == VAR_BLOB)
4899 {
4900 typval_T tv;
4901 int start = 0;
4902
4903 if (argvars[2].v_type != VAR_UNKNOWN)
4904 {
4905 start = tv_get_number_chk(&argvars[2], &error);
4906 if (error)
4907 return;
4908 }
4909 b = argvars[0].vval.v_blob;
4910 if (b == NULL)
4911 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004912 if (start < 0)
4913 {
4914 start = blob_len(b) + start;
4915 if (start < 0)
4916 start = 0;
4917 }
4918
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004919 for (idx = start; idx < blob_len(b); ++idx)
4920 {
4921 tv.v_type = VAR_NUMBER;
4922 tv.vval.v_number = blob_get(b, idx);
4923 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4924 {
4925 rettv->vval.v_number = idx;
4926 return;
4927 }
4928 }
4929 return;
4930 }
4931 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004932 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004933 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004934 return;
4935 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 l = argvars[0].vval.v_list;
4938 if (l != NULL)
4939 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004940 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 item = l->lv_first;
4942 if (argvars[2].v_type != VAR_UNKNOWN)
4943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // Start at specified item. Use the cached index that list_find()
4945 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004946 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004947 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004948 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02004949 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950 if (error)
4951 item = NULL;
4952 }
4953
4954 for ( ; item != NULL; item = item->li_next, ++idx)
4955 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4956 {
4957 rettv->vval.v_number = idx;
4958 break;
4959 }
4960 }
4961}
4962
4963static int inputsecret_flag = 0;
4964
4965/*
4966 * "input()" function
4967 * Also handles inputsecret() when inputsecret is set.
4968 */
4969 static void
4970f_input(typval_T *argvars, typval_T *rettv)
4971{
4972 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4973}
4974
4975/*
4976 * "inputdialog()" function
4977 */
4978 static void
4979f_inputdialog(typval_T *argvars, typval_T *rettv)
4980{
4981#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004982 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004983 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4984 {
4985 char_u *message;
4986 char_u buf[NUMBUFLEN];
4987 char_u *defstr = (char_u *)"";
4988
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004989 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004990 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004991 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4993 else
4994 IObuff[0] = NUL;
4995 if (message != NULL && defstr != NULL
4996 && do_dialog(VIM_QUESTION, NULL, message,
4997 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4998 rettv->vval.v_string = vim_strsave(IObuff);
4999 else
5000 {
5001 if (message != NULL && defstr != NULL
5002 && argvars[1].v_type != VAR_UNKNOWN
5003 && argvars[2].v_type != VAR_UNKNOWN)
5004 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005005 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005006 else
5007 rettv->vval.v_string = NULL;
5008 }
5009 rettv->v_type = VAR_STRING;
5010 }
5011 else
5012#endif
5013 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5014}
5015
5016/*
5017 * "inputlist()" function
5018 */
5019 static void
5020f_inputlist(typval_T *argvars, typval_T *rettv)
5021{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005022 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 listitem_T *li;
5024 int selected;
5025 int mouse_used;
5026
5027#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 // While starting up, there is no place to enter text. When running tests
5029 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005030 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 return;
5032#endif
5033 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005035 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 return;
5037 }
5038
5039 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 msg_row = Rows - 1; // for when 'cmdheight' > 1
5041 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042 msg_scroll = TRUE;
5043 msg_clr_eos();
5044
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005045 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005046 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005047 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005048 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005049 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005050 msg_putchar('\n');
5051 }
5052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054 selected = prompt_for_number(&mouse_used);
5055 if (mouse_used)
5056 selected -= lines_left;
5057
5058 rettv->vval.v_number = selected;
5059}
5060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005061static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5062
5063/*
5064 * "inputrestore()" function
5065 */
5066 static void
5067f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5068{
5069 if (ga_userinput.ga_len > 0)
5070 {
5071 --ga_userinput.ga_len;
5072 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5073 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005074 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 }
5076 else if (p_verbose > 1)
5077 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005078 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005080 }
5081}
5082
5083/*
5084 * "inputsave()" function
5085 */
5086 static void
5087f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5088{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090 if (ga_grow(&ga_userinput, 1) == OK)
5091 {
5092 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5093 + ga_userinput.ga_len);
5094 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096 }
5097 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099}
5100
5101/*
5102 * "inputsecret()" function
5103 */
5104 static void
5105f_inputsecret(typval_T *argvars, typval_T *rettv)
5106{
5107 ++cmdline_star;
5108 ++inputsecret_flag;
5109 f_input(argvars, rettv);
5110 --cmdline_star;
5111 --inputsecret_flag;
5112}
5113
5114/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005115 * "interrupt()" function
5116 */
5117 static void
5118f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5119{
5120 got_int = TRUE;
5121}
5122
5123/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 * "invert(expr)" function
5125 */
5126 static void
5127f_invert(typval_T *argvars, typval_T *rettv)
5128{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005129 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130}
5131
5132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005133 * "islocked()" function
5134 */
5135 static void
5136f_islocked(typval_T *argvars, typval_T *rettv)
5137{
5138 lval_T lv;
5139 char_u *end;
5140 dictitem_T *di;
5141
5142 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005143 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005144 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 if (end != NULL && lv.ll_name != NULL)
5146 {
5147 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005148 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 else
5150 {
5151 if (lv.ll_tv == NULL)
5152 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005153 di = find_var(lv.ll_name, NULL, TRUE);
5154 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 // Consider a variable locked when:
5157 // 1. the variable itself is locked
5158 // 2. the value of the variable is locked.
5159 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005160 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5161 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005162 }
5163 }
5164 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005165 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005167 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005168 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005170 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5171 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5174 }
5175 }
5176
5177 clear_lval(&lv);
5178}
5179
5180#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5181/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005182 * "isinf()" function
5183 */
5184 static void
5185f_isinf(typval_T *argvars, typval_T *rettv)
5186{
5187 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5188 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5189}
5190
5191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 * "isnan()" function
5193 */
5194 static void
5195f_isnan(typval_T *argvars, typval_T *rettv)
5196{
5197 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5198 && isnan(argvars[0].vval.v_float);
5199}
5200#endif
5201
5202/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005203 * "last_buffer_nr()" function.
5204 */
5205 static void
5206f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5207{
5208 int n = 0;
5209 buf_T *buf;
5210
Bram Moolenaar29323592016-07-24 22:04:11 +02005211 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212 if (n < buf->b_fnum)
5213 n = buf->b_fnum;
5214
5215 rettv->vval.v_number = n;
5216}
5217
5218/*
5219 * "len()" function
5220 */
5221 static void
5222f_len(typval_T *argvars, typval_T *rettv)
5223{
5224 switch (argvars[0].v_type)
5225 {
5226 case VAR_STRING:
5227 case VAR_NUMBER:
5228 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005229 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005231 case VAR_BLOB:
5232 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5233 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 case VAR_LIST:
5235 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5236 break;
5237 case VAR_DICT:
5238 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5239 break;
5240 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005241 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005243 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244 case VAR_SPECIAL:
5245 case VAR_FLOAT:
5246 case VAR_FUNC:
5247 case VAR_PARTIAL:
5248 case VAR_JOB:
5249 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005250 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005251 break;
5252 }
5253}
5254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005256libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005257{
5258#ifdef FEAT_LIBCALL
5259 char_u *string_in;
5260 char_u **string_result;
5261 int nr_result;
5262#endif
5263
5264 rettv->v_type = type;
5265 if (type != VAR_NUMBER)
5266 rettv->vval.v_string = NULL;
5267
5268 if (check_restricted() || check_secure())
5269 return;
5270
5271#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005272 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005273 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5274 {
5275 string_in = NULL;
5276 if (argvars[2].v_type == VAR_STRING)
5277 string_in = argvars[2].vval.v_string;
5278 if (type == VAR_NUMBER)
5279 string_result = NULL;
5280 else
5281 string_result = &rettv->vval.v_string;
5282 if (mch_libcall(argvars[0].vval.v_string,
5283 argvars[1].vval.v_string,
5284 string_in,
5285 argvars[2].vval.v_number,
5286 string_result,
5287 &nr_result) == OK
5288 && type == VAR_NUMBER)
5289 rettv->vval.v_number = nr_result;
5290 }
5291#endif
5292}
5293
5294/*
5295 * "libcall()" function
5296 */
5297 static void
5298f_libcall(typval_T *argvars, typval_T *rettv)
5299{
5300 libcall_common(argvars, rettv, VAR_STRING);
5301}
5302
5303/*
5304 * "libcallnr()" function
5305 */
5306 static void
5307f_libcallnr(typval_T *argvars, typval_T *rettv)
5308{
5309 libcall_common(argvars, rettv, VAR_NUMBER);
5310}
5311
5312/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005313 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314 */
5315 static void
5316f_line(typval_T *argvars, typval_T *rettv)
5317{
5318 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005319 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005320 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005321 int id;
5322 tabpage_T *tp;
5323 win_T *wp;
5324 win_T *save_curwin;
5325 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005327 if (argvars[1].v_type != VAR_UNKNOWN)
5328 {
5329 // use window specified in the second argument
5330 id = (int)tv_get_number(&argvars[1]);
5331 wp = win_id2wp_tp(id, &tp);
5332 if (wp != NULL && tp != NULL)
5333 {
5334 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5335 == OK)
5336 {
5337 check_cursor();
5338 fp = var2fpos(&argvars[0], TRUE, &fnum);
5339 }
5340 restore_win_noblock(save_curwin, save_curtab, TRUE);
5341 }
5342 }
5343 else
5344 // use current window
5345 fp = var2fpos(&argvars[0], TRUE, &fnum);
5346
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347 if (fp != NULL)
5348 lnum = fp->lnum;
5349 rettv->vval.v_number = lnum;
5350}
5351
5352/*
5353 * "line2byte(lnum)" function
5354 */
5355 static void
5356f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5357{
5358#ifndef FEAT_BYTEOFF
5359 rettv->vval.v_number = -1;
5360#else
5361 linenr_T lnum;
5362
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005363 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5365 rettv->vval.v_number = -1;
5366 else
5367 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5368 if (rettv->vval.v_number >= 0)
5369 ++rettv->vval.v_number;
5370#endif
5371}
5372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373#ifdef FEAT_FLOAT
5374/*
5375 * "log()" function
5376 */
5377 static void
5378f_log(typval_T *argvars, typval_T *rettv)
5379{
5380 float_T f = 0.0;
5381
5382 rettv->v_type = VAR_FLOAT;
5383 if (get_float_arg(argvars, &f) == OK)
5384 rettv->vval.v_float = log(f);
5385 else
5386 rettv->vval.v_float = 0.0;
5387}
5388
5389/*
5390 * "log10()" function
5391 */
5392 static void
5393f_log10(typval_T *argvars, typval_T *rettv)
5394{
5395 float_T f = 0.0;
5396
5397 rettv->v_type = VAR_FLOAT;
5398 if (get_float_arg(argvars, &f) == OK)
5399 rettv->vval.v_float = log10(f);
5400 else
5401 rettv->vval.v_float = 0.0;
5402}
5403#endif
5404
5405#ifdef FEAT_LUA
5406/*
5407 * "luaeval()" function
5408 */
5409 static void
5410f_luaeval(typval_T *argvars, typval_T *rettv)
5411{
5412 char_u *str;
5413 char_u buf[NUMBUFLEN];
5414
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005415 if (check_restricted() || check_secure())
5416 return;
5417
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005418 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005419 do_luaeval(str, argvars + 1, rettv);
5420}
5421#endif
5422
5423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424 * "maparg()" function
5425 */
5426 static void
5427f_maparg(typval_T *argvars, typval_T *rettv)
5428{
5429 get_maparg(argvars, rettv, TRUE);
5430}
5431
5432/*
5433 * "mapcheck()" function
5434 */
5435 static void
5436f_mapcheck(typval_T *argvars, typval_T *rettv)
5437{
5438 get_maparg(argvars, rettv, FALSE);
5439}
5440
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005441typedef enum
5442{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 MATCH_END, // matchend()
5444 MATCH_MATCH, // match()
5445 MATCH_STR, // matchstr()
5446 MATCH_LIST, // matchlist()
5447 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005448} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449
5450 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005451find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452{
5453 char_u *str = NULL;
5454 long len = 0;
5455 char_u *expr = NULL;
5456 char_u *pat;
5457 regmatch_T regmatch;
5458 char_u patbuf[NUMBUFLEN];
5459 char_u strbuf[NUMBUFLEN];
5460 char_u *save_cpo;
5461 long start = 0;
5462 long nth = 1;
5463 colnr_T startcol = 0;
5464 int match = 0;
5465 list_T *l = NULL;
5466 listitem_T *li = NULL;
5467 long idx = 0;
5468 char_u *tofree = NULL;
5469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471 save_cpo = p_cpo;
5472 p_cpo = (char_u *)"";
5473
5474 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005475 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 // type MATCH_LIST: return empty list when there are no matches.
5478 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479 if (rettv_list_alloc(rettv) == FAIL)
5480 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005481 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 && (list_append_string(rettv->vval.v_list,
5483 (char_u *)"", 0) == FAIL
5484 || list_append_number(rettv->vval.v_list,
5485 (varnumber_T)-1) == FAIL
5486 || list_append_number(rettv->vval.v_list,
5487 (varnumber_T)-1) == FAIL
5488 || list_append_number(rettv->vval.v_list,
5489 (varnumber_T)-1) == FAIL))
5490 {
5491 list_free(rettv->vval.v_list);
5492 rettv->vval.v_list = NULL;
5493 goto theend;
5494 }
5495 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005496 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 {
5498 rettv->v_type = VAR_STRING;
5499 rettv->vval.v_string = NULL;
5500 }
5501
5502 if (argvars[0].v_type == VAR_LIST)
5503 {
5504 if ((l = argvars[0].vval.v_list) == NULL)
5505 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005506 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507 li = l->lv_first;
5508 }
5509 else
5510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005511 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 len = (long)STRLEN(str);
5513 }
5514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005515 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516 if (pat == NULL)
5517 goto theend;
5518
5519 if (argvars[2].v_type != VAR_UNKNOWN)
5520 {
5521 int error = FALSE;
5522
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005523 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005524 if (error)
5525 goto theend;
5526 if (l != NULL)
5527 {
5528 li = list_find(l, start);
5529 if (li == NULL)
5530 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005531 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005532 }
5533 else
5534 {
5535 if (start < 0)
5536 start = 0;
5537 if (start > len)
5538 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 // When "count" argument is there ignore matches before "start",
5540 // otherwise skip part of the string. Differs when pattern is "^"
5541 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542 if (argvars[3].v_type != VAR_UNKNOWN)
5543 startcol = start;
5544 else
5545 {
5546 str += start;
5547 len -= start;
5548 }
5549 }
5550
5551 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005552 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 if (error)
5554 goto theend;
5555 }
5556
5557 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5558 if (regmatch.regprog != NULL)
5559 {
5560 regmatch.rm_ic = p_ic;
5561
5562 for (;;)
5563 {
5564 if (l != NULL)
5565 {
5566 if (li == NULL)
5567 {
5568 match = FALSE;
5569 break;
5570 }
5571 vim_free(tofree);
5572 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5573 if (str == NULL)
5574 break;
5575 }
5576
5577 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5578
5579 if (match && --nth <= 0)
5580 break;
5581 if (l == NULL && !match)
5582 break;
5583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005584 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 if (l != NULL)
5586 {
5587 li = li->li_next;
5588 ++idx;
5589 }
5590 else
5591 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 startcol = (colnr_T)(regmatch.startp[0]
5593 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594 if (startcol > (colnr_T)len
5595 || str + startcol <= regmatch.startp[0])
5596 {
5597 match = FALSE;
5598 break;
5599 }
5600 }
5601 }
5602
5603 if (match)
5604 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005605 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 {
5607 listitem_T *li1 = rettv->vval.v_list->lv_first;
5608 listitem_T *li2 = li1->li_next;
5609 listitem_T *li3 = li2->li_next;
5610 listitem_T *li4 = li3->li_next;
5611
5612 vim_free(li1->li_tv.vval.v_string);
5613 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005614 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 li3->li_tv.vval.v_number =
5616 (varnumber_T)(regmatch.startp[0] - expr);
5617 li4->li_tv.vval.v_number =
5618 (varnumber_T)(regmatch.endp[0] - expr);
5619 if (l != NULL)
5620 li2->li_tv.vval.v_number = (varnumber_T)idx;
5621 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005622 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 {
5624 int i;
5625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 for (i = 0; i < NSUBEXP; ++i)
5628 {
5629 if (regmatch.endp[i] == NULL)
5630 {
5631 if (list_append_string(rettv->vval.v_list,
5632 (char_u *)"", 0) == FAIL)
5633 break;
5634 }
5635 else if (list_append_string(rettv->vval.v_list,
5636 regmatch.startp[i],
5637 (int)(regmatch.endp[i] - regmatch.startp[i]))
5638 == FAIL)
5639 break;
5640 }
5641 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005642 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005644 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 if (l != NULL)
5646 copy_tv(&li->li_tv, rettv);
5647 else
5648 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005649 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005650 }
5651 else if (l != NULL)
5652 rettv->vval.v_number = idx;
5653 else
5654 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005655 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 rettv->vval.v_number =
5657 (varnumber_T)(regmatch.startp[0] - str);
5658 else
5659 rettv->vval.v_number =
5660 (varnumber_T)(regmatch.endp[0] - str);
5661 rettv->vval.v_number += (varnumber_T)(str - expr);
5662 }
5663 }
5664 vim_regfree(regmatch.regprog);
5665 }
5666
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005667theend:
5668 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005669 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005670 listitem_remove(rettv->vval.v_list,
5671 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672 vim_free(tofree);
5673 p_cpo = save_cpo;
5674}
5675
5676/*
5677 * "match()" function
5678 */
5679 static void
5680f_match(typval_T *argvars, typval_T *rettv)
5681{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005682 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005683}
5684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685/*
5686 * "matchend()" function
5687 */
5688 static void
5689f_matchend(typval_T *argvars, typval_T *rettv)
5690{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005691 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692}
5693
5694/*
5695 * "matchlist()" function
5696 */
5697 static void
5698f_matchlist(typval_T *argvars, typval_T *rettv)
5699{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005700 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701}
5702
5703/*
5704 * "matchstr()" function
5705 */
5706 static void
5707f_matchstr(typval_T *argvars, typval_T *rettv)
5708{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005709 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710}
5711
5712/*
5713 * "matchstrpos()" function
5714 */
5715 static void
5716f_matchstrpos(typval_T *argvars, typval_T *rettv)
5717{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005718 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719}
5720
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005721 static void
5722max_min(typval_T *argvars, typval_T *rettv, int domax)
5723{
5724 varnumber_T n = 0;
5725 varnumber_T i;
5726 int error = FALSE;
5727
5728 if (argvars[0].v_type == VAR_LIST)
5729 {
5730 list_T *l;
5731 listitem_T *li;
5732
5733 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005734 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005736 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005737 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005738 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5739 n = l->lv_u.nonmat.lv_start;
5740 else
5741 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5742 * l->lv_u.nonmat.lv_stride;
5743 }
5744 else
5745 {
5746 li = l->lv_first;
5747 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005748 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005749 n = tv_get_number_chk(&li->li_tv, &error);
5750 for (;;)
5751 {
5752 li = li->li_next;
5753 if (li == NULL)
5754 break;
5755 i = tv_get_number_chk(&li->li_tv, &error);
5756 if (domax ? i > n : i < n)
5757 n = i;
5758 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005759 }
5760 }
5761 }
5762 }
5763 else if (argvars[0].v_type == VAR_DICT)
5764 {
5765 dict_T *d;
5766 int first = TRUE;
5767 hashitem_T *hi;
5768 int todo;
5769
5770 d = argvars[0].vval.v_dict;
5771 if (d != NULL)
5772 {
5773 todo = (int)d->dv_hashtab.ht_used;
5774 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5775 {
5776 if (!HASHITEM_EMPTY(hi))
5777 {
5778 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005779 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 if (first)
5781 {
5782 n = i;
5783 first = FALSE;
5784 }
5785 else if (domax ? i > n : i < n)
5786 n = i;
5787 }
5788 }
5789 }
5790 }
5791 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005792 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 rettv->vval.v_number = error ? 0 : n;
5794}
5795
5796/*
5797 * "max()" function
5798 */
5799 static void
5800f_max(typval_T *argvars, typval_T *rettv)
5801{
5802 max_min(argvars, rettv, TRUE);
5803}
5804
5805/*
5806 * "min()" function
5807 */
5808 static void
5809f_min(typval_T *argvars, typval_T *rettv)
5810{
5811 max_min(argvars, rettv, FALSE);
5812}
5813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814#if defined(FEAT_MZSCHEME) || defined(PROTO)
5815/*
5816 * "mzeval()" function
5817 */
5818 static void
5819f_mzeval(typval_T *argvars, typval_T *rettv)
5820{
5821 char_u *str;
5822 char_u buf[NUMBUFLEN];
5823
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005824 if (check_restricted() || check_secure())
5825 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005826 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827 do_mzeval(str, rettv);
5828}
5829
5830 void
5831mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5832{
5833 typval_T argvars[3];
5834
5835 argvars[0].v_type = VAR_STRING;
5836 argvars[0].vval.v_string = name;
5837 copy_tv(args, &argvars[1]);
5838 argvars[2].v_type = VAR_UNKNOWN;
5839 f_call(argvars, rettv);
5840 clear_tv(&argvars[1]);
5841}
5842#endif
5843
5844/*
5845 * "nextnonblank()" function
5846 */
5847 static void
5848f_nextnonblank(typval_T *argvars, typval_T *rettv)
5849{
5850 linenr_T lnum;
5851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005852 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 {
5854 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5855 {
5856 lnum = 0;
5857 break;
5858 }
5859 if (*skipwhite(ml_get(lnum)) != NUL)
5860 break;
5861 }
5862 rettv->vval.v_number = lnum;
5863}
5864
5865/*
5866 * "nr2char()" function
5867 */
5868 static void
5869f_nr2char(typval_T *argvars, typval_T *rettv)
5870{
5871 char_u buf[NUMBUFLEN];
5872
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 if (has_mbyte)
5874 {
5875 int utf8 = 0;
5876
5877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02005878 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005879 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005880 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005882 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883 }
5884 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005886 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887 buf[1] = NUL;
5888 }
5889 rettv->v_type = VAR_STRING;
5890 rettv->vval.v_string = vim_strsave(buf);
5891}
5892
5893/*
5894 * "or(expr, expr)" function
5895 */
5896 static void
5897f_or(typval_T *argvars, typval_T *rettv)
5898{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005899 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5900 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901}
5902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903#ifdef FEAT_PERL
5904/*
5905 * "perleval()" function
5906 */
5907 static void
5908f_perleval(typval_T *argvars, typval_T *rettv)
5909{
5910 char_u *str;
5911 char_u buf[NUMBUFLEN];
5912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005913 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005914 do_perleval(str, rettv);
5915}
5916#endif
5917
5918#ifdef FEAT_FLOAT
5919/*
5920 * "pow()" function
5921 */
5922 static void
5923f_pow(typval_T *argvars, typval_T *rettv)
5924{
5925 float_T fx = 0.0, fy = 0.0;
5926
5927 rettv->v_type = VAR_FLOAT;
5928 if (get_float_arg(argvars, &fx) == OK
5929 && get_float_arg(&argvars[1], &fy) == OK)
5930 rettv->vval.v_float = pow(fx, fy);
5931 else
5932 rettv->vval.v_float = 0.0;
5933}
5934#endif
5935
5936/*
5937 * "prevnonblank()" function
5938 */
5939 static void
5940f_prevnonblank(typval_T *argvars, typval_T *rettv)
5941{
5942 linenr_T lnum;
5943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005945 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5946 lnum = 0;
5947 else
5948 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5949 --lnum;
5950 rettv->vval.v_number = lnum;
5951}
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953// This dummy va_list is here because:
5954// - passing a NULL pointer doesn't work when va_list isn't a pointer
5955// - locally in the function results in a "used before set" warning
5956// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957static va_list ap;
5958
5959/*
5960 * "printf()" function
5961 */
5962 static void
5963f_printf(typval_T *argvars, typval_T *rettv)
5964{
5965 char_u buf[NUMBUFLEN];
5966 int len;
5967 char_u *s;
5968 int saved_did_emsg = did_emsg;
5969 char *fmt;
5970
5971 rettv->v_type = VAR_STRING;
5972 rettv->vval.v_string = NULL;
5973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005974 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005976 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005977 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 if (!did_emsg)
5979 {
5980 s = alloc(len + 1);
5981 if (s != NULL)
5982 {
5983 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005984 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5985 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986 }
5987 }
5988 did_emsg |= saved_did_emsg;
5989}
5990
5991/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005992 * "pum_getpos()" function
5993 */
5994 static void
5995f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5996{
5997 if (rettv_dict_alloc(rettv) != OK)
5998 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005999 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006000}
6001
6002/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006003 * "pumvisible()" function
6004 */
6005 static void
6006f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6007{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 if (pum_visible())
6009 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010}
6011
6012#ifdef FEAT_PYTHON3
6013/*
6014 * "py3eval()" function
6015 */
6016 static void
6017f_py3eval(typval_T *argvars, typval_T *rettv)
6018{
6019 char_u *str;
6020 char_u buf[NUMBUFLEN];
6021
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006022 if (check_restricted() || check_secure())
6023 return;
6024
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006025 if (p_pyx == 0)
6026 p_pyx = 3;
6027
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006028 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 do_py3eval(str, rettv);
6030}
6031#endif
6032
6033#ifdef FEAT_PYTHON
6034/*
6035 * "pyeval()" function
6036 */
6037 static void
6038f_pyeval(typval_T *argvars, typval_T *rettv)
6039{
6040 char_u *str;
6041 char_u buf[NUMBUFLEN];
6042
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006043 if (check_restricted() || check_secure())
6044 return;
6045
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006046 if (p_pyx == 0)
6047 p_pyx = 2;
6048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 do_pyeval(str, rettv);
6051}
6052#endif
6053
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006054#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6055/*
6056 * "pyxeval()" function
6057 */
6058 static void
6059f_pyxeval(typval_T *argvars, typval_T *rettv)
6060{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006061 if (check_restricted() || check_secure())
6062 return;
6063
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006064# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6065 init_pyxversion();
6066 if (p_pyx == 2)
6067 f_pyeval(argvars, rettv);
6068 else
6069 f_py3eval(argvars, rettv);
6070# elif defined(FEAT_PYTHON)
6071 f_pyeval(argvars, rettv);
6072# elif defined(FEAT_PYTHON3)
6073 f_py3eval(argvars, rettv);
6074# endif
6075}
6076#endif
6077
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006078static UINT32_T srand_seed_for_testing = 0;
6079static int srand_seed_for_testing_is_used = FALSE;
6080
6081 static void
6082f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6083{
6084 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006085 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006086 else
6087 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006088 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6089 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006090 }
6091}
6092
6093 static void
6094init_srand(UINT32_T *x)
6095{
6096#ifndef MSWIN
6097 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6098#endif
6099
6100 if (srand_seed_for_testing_is_used)
6101 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006102 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006103 return;
6104 }
6105#ifndef MSWIN
6106 if (dev_urandom_state != FAIL)
6107 {
6108 int fd = open("/dev/urandom", O_RDONLY);
6109 struct {
6110 union {
6111 UINT32_T number;
6112 char bytes[sizeof(UINT32_T)];
6113 } contents;
6114 } buf;
6115
6116 // Attempt reading /dev/urandom.
6117 if (fd == -1)
6118 dev_urandom_state = FAIL;
6119 else
6120 {
6121 buf.contents.number = 0;
6122 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6123 != sizeof(UINT32_T))
6124 dev_urandom_state = FAIL;
6125 else
6126 {
6127 dev_urandom_state = OK;
6128 *x = buf.contents.number;
6129 }
6130 close(fd);
6131 }
6132 }
6133 if (dev_urandom_state != OK)
6134 // Reading /dev/urandom doesn't work, fall back to time().
6135#endif
6136 *x = vim_time();
6137}
6138
6139#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6140#define SPLITMIX32(x, z) ( \
6141 z = (x += 0x9e3779b9), \
6142 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6143 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6144 z ^ (z >> 16) \
6145 )
6146#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6147 result = ROTL(y * 5, 7) * 9; \
6148 t = y << 9; \
6149 z ^= x; \
6150 w ^= y; \
6151 y ^= z, x ^= w; \
6152 z ^= t; \
6153 w = ROTL(w, 11);
6154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006156 * "rand()" function
6157 */
6158 static void
6159f_rand(typval_T *argvars, typval_T *rettv)
6160{
6161 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006162 static UINT32_T gx, gy, gz, gw;
6163 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006164 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006165 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006166
6167 if (argvars[0].v_type == VAR_UNKNOWN)
6168 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006169 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006170 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006171 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006172 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006173 init_srand(&x);
6174
6175 gx = SPLITMIX32(x, z);
6176 gy = SPLITMIX32(x, z);
6177 gz = SPLITMIX32(x, z);
6178 gw = SPLITMIX32(x, z);
6179 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006180 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006181
6182 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006183 }
6184 else if (argvars[0].v_type == VAR_LIST)
6185 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006186 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006187 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006188 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006189
6190 lx = list_find(l, 0L);
6191 ly = list_find(l, 1L);
6192 lz = list_find(l, 2L);
6193 lw = list_find(l, 3L);
6194 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6195 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6196 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6197 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6198 x = (UINT32_T)lx->li_tv.vval.v_number;
6199 y = (UINT32_T)ly->li_tv.vval.v_number;
6200 z = (UINT32_T)lz->li_tv.vval.v_number;
6201 w = (UINT32_T)lw->li_tv.vval.v_number;
6202
6203 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6204
6205 lx->li_tv.vval.v_number = (varnumber_T)x;
6206 ly->li_tv.vval.v_number = (varnumber_T)y;
6207 lz->li_tv.vval.v_number = (varnumber_T)z;
6208 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006209 }
6210 else
6211 goto theend;
6212
6213 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006214 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006215 return;
6216
6217theend:
6218 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006219 rettv->v_type = VAR_NUMBER;
6220 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006221}
6222
6223/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006224 * "srand()" function
6225 */
6226 static void
6227f_srand(typval_T *argvars, typval_T *rettv)
6228{
6229 UINT32_T x = 0, z;
6230
6231 if (rettv_list_alloc(rettv) == FAIL)
6232 return;
6233 if (argvars[0].v_type == VAR_UNKNOWN)
6234 {
6235 init_srand(&x);
6236 }
6237 else
6238 {
6239 int error = FALSE;
6240
6241 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6242 if (error)
6243 return;
6244 }
6245
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 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6250}
6251
6252#undef ROTL
6253#undef SPLITMIX32
6254#undef SHUFFLE_XOSHIRO128STARSTAR
6255
6256/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 * "range()" function
6258 */
6259 static void
6260f_range(typval_T *argvars, typval_T *rettv)
6261{
6262 varnumber_T start;
6263 varnumber_T end;
6264 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 int error = FALSE;
6266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006267 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 if (argvars[1].v_type == VAR_UNKNOWN)
6269 {
6270 end = start - 1;
6271 start = 0;
6272 }
6273 else
6274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006275 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006277 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 }
6279
6280 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006281 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006283 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006285 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006288 list_T *list = rettv->vval.v_list;
6289
6290 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006291 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 // be called.
6293 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006294 list->lv_u.nonmat.lv_start = start;
6295 list->lv_u.nonmat.lv_end = end;
6296 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006297 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298 }
6299}
6300
6301/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006302 * Materialize "list".
6303 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 */
6305 void
6306range_list_materialize(list_T *list)
6307{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006308 varnumber_T start = list->lv_u.nonmat.lv_start;
6309 varnumber_T end = list->lv_u.nonmat.lv_end;
6310 int stride = list->lv_u.nonmat.lv_stride;
6311 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006312
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006313 list->lv_first = NULL;
6314 list->lv_u.mat.lv_last = NULL;
6315 list->lv_len = 0;
6316 list->lv_u.mat.lv_idx_item = NULL;
6317 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6318 if (list_append_number(list, (varnumber_T)i) == FAIL)
6319 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320}
6321
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006322/*
6323 * "getreginfo()" function
6324 */
6325 static void
6326f_getreginfo(typval_T *argvars, typval_T *rettv)
6327{
6328 char_u *strregname;
6329 int regname;
6330 char_u buf[NUMBUFLEN + 2];
6331 long reglen = 0;
6332 dict_T *dict;
6333 list_T *list;
6334
6335 if (argvars[0].v_type != VAR_UNKNOWN)
6336 {
6337 strregname = tv_get_string_chk(&argvars[0]);
6338 if (strregname == NULL)
6339 return;
6340 }
6341 else
6342 strregname = get_vim_var_str(VV_REG);
6343
6344 regname = (strregname == NULL ? '"' : *strregname);
6345 if (regname == 0 || regname == '@')
6346 regname = '"';
6347
6348 if (rettv_dict_alloc(rettv) == FAIL)
6349 return;
6350 dict = rettv->vval.v_dict;
6351
6352 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6353 if (list == NULL)
6354 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006355 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006356
6357 buf[0] = NUL;
6358 buf[1] = NUL;
6359 switch (get_reg_type(regname, &reglen))
6360 {
6361 case MLINE: buf[0] = 'V'; break;
6362 case MCHAR: buf[0] = 'v'; break;
6363 case MBLOCK:
6364 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6365 reglen + 1);
6366 break;
6367 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006368 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006369
6370 buf[0] = get_register_name(get_unname_register());
6371 buf[1] = NUL;
6372 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006373 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006374 else
6375 {
6376 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6377
6378 if (item != NULL)
6379 {
6380 item->di_tv.v_type = VAR_SPECIAL;
6381 item->di_tv.vval.v_number = regname == buf[0]
6382 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006383 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006384 }
6385 }
6386}
6387
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006388 static void
6389return_register(int regname, typval_T *rettv)
6390{
6391 char_u buf[2] = {0, 0};
6392
6393 buf[0] = (char_u)regname;
6394 rettv->v_type = VAR_STRING;
6395 rettv->vval.v_string = vim_strsave(buf);
6396}
6397
6398/*
6399 * "reg_executing()" function
6400 */
6401 static void
6402f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6403{
6404 return_register(reg_executing, rettv);
6405}
6406
6407/*
6408 * "reg_recording()" function
6409 */
6410 static void
6411f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6412{
6413 return_register(reg_recording, rettv);
6414}
6415
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417 * "rename({from}, {to})" function
6418 */
6419 static void
6420f_rename(typval_T *argvars, typval_T *rettv)
6421{
6422 char_u buf[NUMBUFLEN];
6423
6424 if (check_restricted() || check_secure())
6425 rettv->vval.v_number = -1;
6426 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006427 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6428 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429}
6430
6431/*
6432 * "repeat()" function
6433 */
6434 static void
6435f_repeat(typval_T *argvars, typval_T *rettv)
6436{
6437 char_u *p;
6438 int n;
6439 int slen;
6440 int len;
6441 char_u *r;
6442 int i;
6443
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006444 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 if (argvars[0].v_type == VAR_LIST)
6446 {
6447 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6448 while (n-- > 0)
6449 if (list_extend(rettv->vval.v_list,
6450 argvars[0].vval.v_list, NULL) == FAIL)
6451 break;
6452 }
6453 else
6454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006455 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456 rettv->v_type = VAR_STRING;
6457 rettv->vval.v_string = NULL;
6458
6459 slen = (int)STRLEN(p);
6460 len = slen * n;
6461 if (len <= 0)
6462 return;
6463
6464 r = alloc(len + 1);
6465 if (r != NULL)
6466 {
6467 for (i = 0; i < n; i++)
6468 mch_memmove(r + i * slen, p, (size_t)slen);
6469 r[len] = NUL;
6470 }
6471
6472 rettv->vval.v_string = r;
6473 }
6474}
6475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006476#define SP_NOMOVE 0x01 // don't move cursor
6477#define SP_REPEAT 0x02 // repeat to find outer pair
6478#define SP_RETCOUNT 0x04 // return matchcount
6479#define SP_SETPCMARK 0x08 // set previous context mark
6480#define SP_START 0x10 // accept match at start position
6481#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6482#define SP_END 0x40 // leave cursor at end of match
6483#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485/*
6486 * Get flags for a search function.
6487 * Possibly sets "p_ws".
6488 * Returns BACKWARD, FORWARD or zero (for an error).
6489 */
6490 static int
6491get_search_arg(typval_T *varp, int *flagsp)
6492{
6493 int dir = FORWARD;
6494 char_u *flags;
6495 char_u nbuf[NUMBUFLEN];
6496 int mask;
6497
6498 if (varp->v_type != VAR_UNKNOWN)
6499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006500 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006502 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 while (*flags != NUL)
6504 {
6505 switch (*flags)
6506 {
6507 case 'b': dir = BACKWARD; break;
6508 case 'w': p_ws = TRUE; break;
6509 case 'W': p_ws = FALSE; break;
6510 default: mask = 0;
6511 if (flagsp != NULL)
6512 switch (*flags)
6513 {
6514 case 'c': mask = SP_START; break;
6515 case 'e': mask = SP_END; break;
6516 case 'm': mask = SP_RETCOUNT; break;
6517 case 'n': mask = SP_NOMOVE; break;
6518 case 'p': mask = SP_SUBPAT; break;
6519 case 'r': mask = SP_REPEAT; break;
6520 case 's': mask = SP_SETPCMARK; break;
6521 case 'z': mask = SP_COLUMN; break;
6522 }
6523 if (mask == 0)
6524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006525 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526 dir = 0;
6527 }
6528 else
6529 *flagsp |= mask;
6530 }
6531 if (dir == 0)
6532 break;
6533 ++flags;
6534 }
6535 }
6536 return dir;
6537}
6538
6539/*
6540 * Shared by search() and searchpos() functions.
6541 */
6542 static int
6543search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6544{
6545 int flags;
6546 char_u *pat;
6547 pos_T pos;
6548 pos_T save_cursor;
6549 int save_p_ws = p_ws;
6550 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006551 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006554 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555 long time_limit = 0;
6556#endif
6557 int options = SEARCH_KEEP;
6558 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006559 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006560 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006561 pos_T firstpos;
6562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006563 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 if (dir == 0)
6566 goto theend;
6567 flags = *flagsp;
6568 if (flags & SP_START)
6569 options |= SEARCH_START;
6570 if (flags & SP_END)
6571 options |= SEARCH_END;
6572 if (flags & SP_COLUMN)
6573 options |= SEARCH_COL;
6574
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006575 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006578 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579 if (lnum_stop < 0)
6580 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581 if (argvars[3].v_type != VAR_UNKNOWN)
6582 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006583#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006584 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585 if (time_limit < 0)
6586 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006588 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590 }
6591
6592#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594 profile_setlimit(time_limit, &tm);
6595#endif
6596
6597 /*
6598 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6599 * Check to make sure only those flags are set.
6600 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6601 * flags cannot be set. Check for that condition also.
6602 */
6603 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6604 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006606 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 goto theend;
6608 }
6609
6610 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006611 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006612 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006613 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6614#ifdef FEAT_RELTIME
6615 sia.sa_tm = &tm;
6616#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006617
6618 // Repeat until {skip} returns FALSE.
6619 for (;;)
6620 {
6621 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006622 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006623 // finding the first match again means there is no match where {skip}
6624 // evaluates to zero.
6625 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6626 subpatnum = FAIL;
6627
Bram Moolenaara9c01042020-06-07 14:50:50 +02006628 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006629 // didn't find it or no skip argument
6630 break;
6631 firstpos = pos;
6632
Bram Moolenaara9c01042020-06-07 14:50:50 +02006633 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006634 {
6635 int do_skip;
6636 int err;
6637 pos_T save_pos = curwin->w_cursor;
6638
6639 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006640 err = FALSE;
6641 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006642 curwin->w_cursor = save_pos;
6643 if (err)
6644 {
6645 // Evaluating {skip} caused an error, break here.
6646 subpatnum = FAIL;
6647 break;
6648 }
6649 if (!do_skip)
6650 break;
6651 }
6652 }
6653
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654 if (subpatnum != FAIL)
6655 {
6656 if (flags & SP_SUBPAT)
6657 retval = subpatnum;
6658 else
6659 retval = pos.lnum;
6660 if (flags & SP_SETPCMARK)
6661 setpcmark();
6662 curwin->w_cursor = pos;
6663 if (match_pos != NULL)
6664 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006665 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666 match_pos->lnum = pos.lnum;
6667 match_pos->col = pos.col + 1;
6668 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006669 // "/$" will put the cursor after the end of the line, may need to
6670 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 check_cursor();
6672 }
6673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006674 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 if (flags & SP_NOMOVE)
6676 curwin->w_cursor = save_cursor;
6677 else
6678 curwin->w_set_curswant = TRUE;
6679theend:
6680 p_ws = save_p_ws;
6681
6682 return retval;
6683}
6684
6685#ifdef FEAT_FLOAT
6686
6687/*
6688 * round() is not in C90, use ceil() or floor() instead.
6689 */
6690 float_T
6691vim_round(float_T f)
6692{
6693 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6694}
6695
6696/*
6697 * "round({float})" function
6698 */
6699 static void
6700f_round(typval_T *argvars, typval_T *rettv)
6701{
6702 float_T f = 0.0;
6703
6704 rettv->v_type = VAR_FLOAT;
6705 if (get_float_arg(argvars, &f) == OK)
6706 rettv->vval.v_float = vim_round(f);
6707 else
6708 rettv->vval.v_float = 0.0;
6709}
6710#endif
6711
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006712#ifdef FEAT_RUBY
6713/*
6714 * "rubyeval()" function
6715 */
6716 static void
6717f_rubyeval(typval_T *argvars, typval_T *rettv)
6718{
6719 char_u *str;
6720 char_u buf[NUMBUFLEN];
6721
6722 str = tv_get_string_buf(&argvars[0], buf);
6723 do_rubyeval(str, rettv);
6724}
6725#endif
6726
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727/*
6728 * "screenattr()" function
6729 */
6730 static void
6731f_screenattr(typval_T *argvars, typval_T *rettv)
6732{
6733 int row;
6734 int col;
6735 int c;
6736
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006737 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6738 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 if (row < 0 || row >= screen_Rows
6740 || col < 0 || col >= screen_Columns)
6741 c = -1;
6742 else
6743 c = ScreenAttrs[LineOffset[row] + col];
6744 rettv->vval.v_number = c;
6745}
6746
6747/*
6748 * "screenchar()" function
6749 */
6750 static void
6751f_screenchar(typval_T *argvars, typval_T *rettv)
6752{
6753 int row;
6754 int col;
6755 int off;
6756 int c;
6757
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006758 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6759 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006760 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761 c = -1;
6762 else
6763 {
6764 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 if (enc_utf8 && ScreenLinesUC[off] != 0)
6766 c = ScreenLinesUC[off];
6767 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 c = ScreenLines[off];
6769 }
6770 rettv->vval.v_number = c;
6771}
6772
6773/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006774 * "screenchars()" function
6775 */
6776 static void
6777f_screenchars(typval_T *argvars, typval_T *rettv)
6778{
6779 int row;
6780 int col;
6781 int off;
6782 int c;
6783 int i;
6784
6785 if (rettv_list_alloc(rettv) == FAIL)
6786 return;
6787 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6788 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6789 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6790 return;
6791
6792 off = LineOffset[row] + col;
6793 if (enc_utf8 && ScreenLinesUC[off] != 0)
6794 c = ScreenLinesUC[off];
6795 else
6796 c = ScreenLines[off];
6797 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6798
6799 if (enc_utf8)
6800
6801 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6802 list_append_number(rettv->vval.v_list,
6803 (varnumber_T)ScreenLinesC[i][off]);
6804}
6805
6806/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 * "screencol()" function
6808 *
6809 * First column is 1 to be consistent with virtcol().
6810 */
6811 static void
6812f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6813{
6814 rettv->vval.v_number = screen_screencol() + 1;
6815}
6816
6817/*
6818 * "screenrow()" function
6819 */
6820 static void
6821f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6822{
6823 rettv->vval.v_number = screen_screenrow() + 1;
6824}
6825
6826/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006827 * "screenstring()" function
6828 */
6829 static void
6830f_screenstring(typval_T *argvars, typval_T *rettv)
6831{
6832 int row;
6833 int col;
6834 int off;
6835 int c;
6836 int i;
6837 char_u buf[MB_MAXBYTES + 1];
6838 int buflen = 0;
6839
6840 rettv->vval.v_string = NULL;
6841 rettv->v_type = VAR_STRING;
6842
6843 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6844 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6845 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6846 return;
6847
6848 off = LineOffset[row] + col;
6849 if (enc_utf8 && ScreenLinesUC[off] != 0)
6850 c = ScreenLinesUC[off];
6851 else
6852 c = ScreenLines[off];
6853 buflen += mb_char2bytes(c, buf);
6854
6855 if (enc_utf8)
6856 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6857 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6858
6859 buf[buflen] = NUL;
6860 rettv->vval.v_string = vim_strsave(buf);
6861}
6862
6863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 * "search()" function
6865 */
6866 static void
6867f_search(typval_T *argvars, typval_T *rettv)
6868{
6869 int flags = 0;
6870
6871 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6872}
6873
6874/*
6875 * "searchdecl()" function
6876 */
6877 static void
6878f_searchdecl(typval_T *argvars, typval_T *rettv)
6879{
Bram Moolenaar30788d32020-09-05 21:35:16 +02006880 int locally = TRUE;
6881 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 int error = FALSE;
6883 char_u *name;
6884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006885 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006887 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 if (argvars[1].v_type != VAR_UNKNOWN)
6889 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02006890 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02006892 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 }
6894 if (!error && name != NULL)
6895 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6896 locally, thisblock, SEARCH_KEEP) == FAIL;
6897}
6898
6899/*
6900 * Used by searchpair() and searchpairpos()
6901 */
6902 static int
6903searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6904{
6905 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006906 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 int save_p_ws = p_ws;
6908 int dir;
6909 int flags = 0;
6910 char_u nbuf1[NUMBUFLEN];
6911 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006912 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 long lnum_stop = 0;
6914 long time_limit = 0;
6915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006916 // Get the three pattern arguments: start, middle, end. Will result in an
6917 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006918 spat = tv_get_string_chk(&argvars[0]);
6919 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6920 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006922 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006924 // Handle the optional fourth argument: flags
6925 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926 if (dir == 0)
6927 goto theend;
6928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006929 // Don't accept SP_END or SP_SUBPAT.
6930 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if ((flags & (SP_END | SP_SUBPAT)) != 0
6932 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006934 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 goto theend;
6936 }
6937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006938 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 if (flags & SP_REPEAT)
6940 p_ws = FALSE;
6941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006942 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 if (argvars[3].v_type == VAR_UNKNOWN
6944 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006945 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 else
6947 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006948 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006949 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 if (argvars[5].v_type != VAR_UNKNOWN)
6952 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006953 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006954 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006956 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006958 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959#ifdef FEAT_RELTIME
6960 if (argvars[6].v_type != VAR_UNKNOWN)
6961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006962 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006965 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006967 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968 }
6969#endif
6970 }
6971 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972
6973 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6974 match_pos, lnum_stop, time_limit);
6975
6976theend:
6977 p_ws = save_p_ws;
6978
6979 return retval;
6980}
6981
6982/*
6983 * "searchpair()" function
6984 */
6985 static void
6986f_searchpair(typval_T *argvars, typval_T *rettv)
6987{
6988 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6989}
6990
6991/*
6992 * "searchpairpos()" function
6993 */
6994 static void
6995f_searchpairpos(typval_T *argvars, typval_T *rettv)
6996{
6997 pos_T match_pos;
6998 int lnum = 0;
6999 int col = 0;
7000
7001 if (rettv_list_alloc(rettv) == FAIL)
7002 return;
7003
7004 if (searchpair_cmn(argvars, &match_pos) > 0)
7005 {
7006 lnum = match_pos.lnum;
7007 col = match_pos.col;
7008 }
7009
7010 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7011 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7012}
7013
7014/*
7015 * Search for a start/middle/end thing.
7016 * Used by searchpair(), see its documentation for the details.
7017 * Returns 0 or -1 for no match,
7018 */
7019 long
7020do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007021 char_u *spat, // start pattern
7022 char_u *mpat, // middle pattern
7023 char_u *epat, // end pattern
7024 int dir, // BACKWARD or FORWARD
7025 typval_T *skip, // skip expression
7026 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007028 linenr_T lnum_stop, // stop at this line if not zero
7029 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030{
7031 char_u *save_cpo;
7032 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7033 long retval = 0;
7034 pos_T pos;
7035 pos_T firstpos;
7036 pos_T foundpos;
7037 pos_T save_cursor;
7038 pos_T save_pos;
7039 int n;
7040 int r;
7041 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007042 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 int err;
7044 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007045#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007047#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007049 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 save_cpo = p_cpo;
7051 p_cpo = empty_option;
7052
7053#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007054 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 profile_setlimit(time_limit, &tm);
7056#endif
7057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007058 // Make two search patterns: start/end (pat2, for in nested pairs) and
7059 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007060 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7061 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 if (pat2 == NULL || pat3 == NULL)
7063 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007064 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065 if (*mpat == NUL)
7066 STRCPY(pat3, pat2);
7067 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007068 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 spat, epat, mpat);
7070 if (flags & SP_START)
7071 options |= SEARCH_START;
7072
Bram Moolenaar48570482017-10-30 21:48:41 +01007073 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007074 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 save_cursor = curwin->w_cursor;
7077 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007078 CLEAR_POS(&firstpos);
7079 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 pat = pat3;
7081 for (;;)
7082 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007083 searchit_arg_T sia;
7084
Bram Moolenaara80faa82020-04-12 19:37:17 +02007085 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007086 sia.sa_stop_lnum = lnum_stop;
7087#ifdef FEAT_RELTIME
7088 sia.sa_tm = &tm;
7089#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007090 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007091 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007092 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007093 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 break;
7095
7096 if (firstpos.lnum == 0)
7097 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007098 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007100 // Found the same position again. Can happen with a pattern that
7101 // has "\zs" at the end and searching backwards. Advance one
7102 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 if (dir == BACKWARD)
7104 decl(&pos);
7105 else
7106 incl(&pos);
7107 }
7108 foundpos = pos;
7109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007110 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111 options &= ~SEARCH_START;
7112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007113 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007114 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 {
7116 save_pos = curwin->w_cursor;
7117 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007118 err = FALSE;
7119 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 curwin->w_cursor = save_pos;
7121 if (err)
7122 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 curwin->w_cursor = save_cursor;
7125 retval = -1;
7126 break;
7127 }
7128 if (r)
7129 continue;
7130 }
7131
7132 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7133 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007134 // Found end when searching backwards or start when searching
7135 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007137 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 }
7139 else
7140 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007141 // Found end when searching forward or start when searching
7142 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007143 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007144 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 }
7146
7147 if (nest == 0)
7148 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007149 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 if (flags & SP_RETCOUNT)
7151 ++retval;
7152 else
7153 retval = pos.lnum;
7154 if (flags & SP_SETPCMARK)
7155 setpcmark();
7156 curwin->w_cursor = pos;
7157 if (!(flags & SP_REPEAT))
7158 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007159 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160 }
7161 }
7162
7163 if (match_pos != NULL)
7164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007165 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 match_pos->lnum = curwin->w_cursor.lnum;
7167 match_pos->col = curwin->w_cursor.col + 1;
7168 }
7169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007170 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171 if ((flags & SP_NOMOVE) || retval == 0)
7172 curwin->w_cursor = save_cursor;
7173
7174theend:
7175 vim_free(pat2);
7176 vim_free(pat3);
7177 if (p_cpo == empty_option)
7178 p_cpo = save_cpo;
7179 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007180 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181 free_string_option(save_cpo);
7182
7183 return retval;
7184}
7185
7186/*
7187 * "searchpos()" function
7188 */
7189 static void
7190f_searchpos(typval_T *argvars, typval_T *rettv)
7191{
7192 pos_T match_pos;
7193 int lnum = 0;
7194 int col = 0;
7195 int n;
7196 int flags = 0;
7197
7198 if (rettv_list_alloc(rettv) == FAIL)
7199 return;
7200
7201 n = search_cmn(argvars, &match_pos, &flags);
7202 if (n > 0)
7203 {
7204 lnum = match_pos.lnum;
7205 col = match_pos.col;
7206 }
7207
7208 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7209 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7210 if (flags & SP_SUBPAT)
7211 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7212}
7213
7214 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7216{
7217 dict_T *d;
7218 dictitem_T *di;
7219 char_u *csearch;
7220
7221 if (argvars[0].v_type != VAR_DICT)
7222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007223 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 return;
7225 }
7226
7227 if ((d = argvars[0].vval.v_dict) != NULL)
7228 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007229 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007230 if (csearch != NULL)
7231 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 if (enc_utf8)
7233 {
7234 int pcc[MAX_MCO];
7235 int c = utfc_ptr2char(csearch, pcc);
7236
7237 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7238 }
7239 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007240 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007241 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242 }
7243
7244 di = dict_find(d, (char_u *)"forward", -1);
7245 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007246 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 ? FORWARD : BACKWARD);
7248
7249 di = dict_find(d, (char_u *)"until", -1);
7250 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007251 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 }
7253}
7254
7255/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007256 * "setenv()" function
7257 */
7258 static void
7259f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7260{
7261 char_u namebuf[NUMBUFLEN];
7262 char_u valbuf[NUMBUFLEN];
7263 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7264
7265 if (argvars[1].v_type == VAR_SPECIAL
7266 && argvars[1].vval.v_number == VVAL_NULL)
7267 vim_unsetenv(name);
7268 else
7269 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7270}
7271
7272/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 * "setfperm({fname}, {mode})" function
7274 */
7275 static void
7276f_setfperm(typval_T *argvars, typval_T *rettv)
7277{
7278 char_u *fname;
7279 char_u modebuf[NUMBUFLEN];
7280 char_u *mode_str;
7281 int i;
7282 int mask;
7283 int mode = 0;
7284
7285 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007286 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 if (fname == NULL)
7288 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 if (mode_str == NULL)
7291 return;
7292 if (STRLEN(mode_str) != 9)
7293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007294 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 return;
7296 }
7297
7298 mask = 1;
7299 for (i = 8; i >= 0; --i)
7300 {
7301 if (mode_str[i] != '-')
7302 mode |= mask;
7303 mask = mask << 1;
7304 }
7305 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7306}
7307
7308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 * "setpos()" function
7310 */
7311 static void
7312f_setpos(typval_T *argvars, typval_T *rettv)
7313{
7314 pos_T pos;
7315 int fnum;
7316 char_u *name;
7317 colnr_T curswant = -1;
7318
7319 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007320 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 if (name != NULL)
7322 {
7323 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7324 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007325 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 pos.col = 0;
7327 if (name[0] == '.' && name[1] == NUL)
7328 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007329 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007330 curwin->w_cursor = pos;
7331 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007333 curwin->w_curswant = curswant - 1;
7334 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007335 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007336 check_cursor();
7337 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 }
7339 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007341 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342 if (setmark_pos(name[1], &pos, fnum) == OK)
7343 rettv->vval.v_number = 0;
7344 }
7345 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007346 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007347 }
7348 }
7349}
7350
7351/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007352 * Translate a register type string to the yank type and block length
7353 */
7354 static int
7355get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7356{
7357 char_u *stropt = *pp;
7358 switch (*stropt)
7359 {
7360 case 'v': case 'c': // character-wise selection
7361 *yank_type = MCHAR;
7362 break;
7363 case 'V': case 'l': // line-wise selection
7364 *yank_type = MLINE;
7365 break;
7366 case 'b': case Ctrl_V: // block-wise selection
7367 *yank_type = MBLOCK;
7368 if (VIM_ISDIGIT(stropt[1]))
7369 {
7370 ++stropt;
7371 *block_len = getdigits(&stropt) - 1;
7372 --stropt;
7373 }
7374 break;
7375 default:
7376 return FAIL;
7377 }
7378 *pp = stropt;
7379 return OK;
7380}
7381
7382/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383 * "setreg()" function
7384 */
7385 static void
7386f_setreg(typval_T *argvars, typval_T *rettv)
7387{
7388 int regname;
7389 char_u *strregname;
7390 char_u *stropt;
7391 char_u *strval;
7392 int append;
7393 char_u yank_type;
7394 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007395 typval_T *regcontents;
7396 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007398 pointreg = 0;
7399 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400 block_len = -1;
7401 yank_type = MAUTO;
7402 append = FALSE;
7403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007405 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406
7407 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007408 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409 regname = *strregname;
7410 if (regname == 0 || regname == '@')
7411 regname = '"';
7412
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007413 if (argvars[1].v_type == VAR_DICT)
7414 {
7415 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007416 dictitem_T *di;
7417
7418 if (d == NULL || d->dv_hashtab.ht_used == 0)
7419 {
7420 // Empty dict, clear the register (like setreg(0, []))
7421 char_u *lstval[2] = {NULL, NULL};
7422 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7423 return;
7424 }
7425
7426 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007427 if (di != NULL)
7428 regcontents = &di->di_tv;
7429
7430 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7431 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007432 {
7433 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7434
7435 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007436 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007437 semsg(_(e_invargval), "value");
7438 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007439 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007440 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007441
7442 if (regname == '"')
7443 {
7444 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7445 if (stropt != NULL)
7446 {
7447 pointreg = *stropt;
7448 regname = pointreg;
7449 }
7450 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007451 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007452 pointreg = regname;
7453 }
7454 else
7455 regcontents = &argvars[1];
7456
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007457 if (argvars[2].v_type != VAR_UNKNOWN)
7458 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007459 if (yank_type != MAUTO)
7460 {
7461 semsg(_(e_toomanyarg), "setreg");
7462 return;
7463 }
7464
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007465 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007466 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007467 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468 for (; *stropt != NUL; ++stropt)
7469 switch (*stropt)
7470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007471 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 append = TRUE;
7473 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007474 default:
7475 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 }
7477 }
7478
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007479 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 {
7481 char_u **lstval;
7482 char_u **allocval;
7483 char_u buf[NUMBUFLEN];
7484 char_u **curval;
7485 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007486 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 listitem_T *li;
7488 int len;
7489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007490 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 len = ll == NULL ? 0 : ll->lv_len;
7492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007493 // First half: use for pointers to result lines; second half: use for
7494 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007495 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 if (lstval == NULL)
7497 return;
7498 curval = lstval;
7499 allocval = lstval + len + 2;
7500 curallocval = allocval;
7501
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007502 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007504 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007505 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007507 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 if (strval == NULL)
7509 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007510 if (strval == buf)
7511 {
7512 // Need to make a copy, next tv_get_string_buf_chk() will
7513 // overwrite the string.
7514 strval = vim_strsave(buf);
7515 if (strval == NULL)
7516 goto free_lstval;
7517 *curallocval++ = strval;
7518 }
7519 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 }
7522 *curval++ = NULL;
7523
7524 write_reg_contents_lst(regname, lstval, -1,
7525 append, yank_type, block_len);
7526free_lstval:
7527 while (curallocval > allocval)
7528 vim_free(*--curallocval);
7529 vim_free(lstval);
7530 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007531 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007533 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534 if (strval == NULL)
7535 return;
7536 write_reg_contents_ex(regname, strval, -1,
7537 append, yank_type, block_len);
7538 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007539 if (pointreg != 0)
7540 get_yank_register(pointreg, TRUE);
7541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542 rettv->vval.v_number = 0;
7543}
7544
7545/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007546 * "settagstack()" function
7547 */
7548 static void
7549f_settagstack(typval_T *argvars, typval_T *rettv)
7550{
7551 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7552 win_T *wp;
7553 dict_T *d;
7554 int action = 'r';
7555
7556 rettv->vval.v_number = -1;
7557
7558 // first argument: window number or id
7559 wp = find_win_by_nr_or_id(&argvars[0]);
7560 if (wp == NULL)
7561 return;
7562
7563 // second argument: dict with items to set in the tag stack
7564 if (argvars[1].v_type != VAR_DICT)
7565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007566 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007567 return;
7568 }
7569 d = argvars[1].vval.v_dict;
7570 if (d == NULL)
7571 return;
7572
7573 // third argument: action - 'a' for append and 'r' for replace.
7574 // default is to replace the stack.
7575 if (argvars[2].v_type == VAR_UNKNOWN)
7576 action = 'r';
7577 else if (argvars[2].v_type == VAR_STRING)
7578 {
7579 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007580 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007581 if (actstr == NULL)
7582 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007583 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7584 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007585 action = *actstr;
7586 else
7587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007588 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007589 return;
7590 }
7591 }
7592 else
7593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007594 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007595 return;
7596 }
7597
7598 if (set_tagstack(wp, d, action) == OK)
7599 rettv->vval.v_number = 0;
7600}
7601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602#ifdef FEAT_CRYPT
7603/*
7604 * "sha256({string})" function
7605 */
7606 static void
7607f_sha256(typval_T *argvars, typval_T *rettv)
7608{
7609 char_u *p;
7610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 rettv->vval.v_string = vim_strsave(
7613 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7614 rettv->v_type = VAR_STRING;
7615}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007616#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617
7618/*
7619 * "shellescape({string})" function
7620 */
7621 static void
7622f_shellescape(typval_T *argvars, typval_T *rettv)
7623{
Bram Moolenaar20615522017-06-05 18:46:26 +02007624 int do_special = non_zero_arg(&argvars[1]);
7625
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007627 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 rettv->v_type = VAR_STRING;
7629}
7630
7631/*
7632 * shiftwidth() function
7633 */
7634 static void
7635f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7636{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007637 rettv->vval.v_number = 0;
7638
7639 if (argvars[0].v_type != VAR_UNKNOWN)
7640 {
7641 long col;
7642
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007643 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007644 if (col < 0)
7645 return; // type error; errmsg already given
7646#ifdef FEAT_VARTABS
7647 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7648 return;
7649#endif
7650 }
7651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 rettv->vval.v_number = get_sw_value(curbuf);
7653}
7654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655#ifdef FEAT_FLOAT
7656/*
7657 * "sin()" function
7658 */
7659 static void
7660f_sin(typval_T *argvars, typval_T *rettv)
7661{
7662 float_T f = 0.0;
7663
7664 rettv->v_type = VAR_FLOAT;
7665 if (get_float_arg(argvars, &f) == OK)
7666 rettv->vval.v_float = sin(f);
7667 else
7668 rettv->vval.v_float = 0.0;
7669}
7670
7671/*
7672 * "sinh()" function
7673 */
7674 static void
7675f_sinh(typval_T *argvars, typval_T *rettv)
7676{
7677 float_T f = 0.0;
7678
7679 rettv->v_type = VAR_FLOAT;
7680 if (get_float_arg(argvars, &f) == OK)
7681 rettv->vval.v_float = sinh(f);
7682 else
7683 rettv->vval.v_float = 0.0;
7684}
7685#endif
7686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687/*
7688 * "soundfold({word})" function
7689 */
7690 static void
7691f_soundfold(typval_T *argvars, typval_T *rettv)
7692{
7693 char_u *s;
7694
7695 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007696 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697#ifdef FEAT_SPELL
7698 rettv->vval.v_string = eval_soundfold(s);
7699#else
7700 rettv->vval.v_string = vim_strsave(s);
7701#endif
7702}
7703
7704/*
7705 * "spellbadword()" function
7706 */
7707 static void
7708f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7709{
7710 char_u *word = (char_u *)"";
7711 hlf_T attr = HLF_COUNT;
7712 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007713#ifdef FEAT_SPELL
7714 int wo_spell_save = curwin->w_p_spell;
7715
7716 if (!curwin->w_p_spell)
7717 {
7718 did_set_spelllang(curwin);
7719 curwin->w_p_spell = TRUE;
7720 }
7721
7722 if (*curwin->w_s->b_p_spl == NUL)
7723 {
7724 emsg(_(e_no_spell));
7725 curwin->w_p_spell = wo_spell_save;
7726 return;
7727 }
7728#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729
7730 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007731 {
7732#ifdef FEAT_SPELL
7733 curwin->w_p_spell = wo_spell_save;
7734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007736 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737
7738#ifdef FEAT_SPELL
7739 if (argvars[0].v_type == VAR_UNKNOWN)
7740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007741 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7743 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007744 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007746 curwin->w_set_curswant = TRUE;
7747 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007749 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007751 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 int capcol = -1;
7753
7754 if (str != NULL)
7755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007756 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 while (*str != NUL)
7758 {
7759 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7760 if (attr != HLF_COUNT)
7761 {
7762 word = str;
7763 break;
7764 }
7765 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007766 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007767 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007768 }
7769 }
7770 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007771 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772#endif
7773
7774 list_append_string(rettv->vval.v_list, word, len);
7775 list_append_string(rettv->vval.v_list, (char_u *)(
7776 attr == HLF_SPB ? "bad" :
7777 attr == HLF_SPR ? "rare" :
7778 attr == HLF_SPL ? "local" :
7779 attr == HLF_SPC ? "caps" :
7780 ""), -1);
7781}
7782
7783/*
7784 * "spellsuggest()" function
7785 */
7786 static void
7787f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7788{
7789#ifdef FEAT_SPELL
7790 char_u *str;
7791 int typeerr = FALSE;
7792 int maxcount;
7793 garray_T ga;
7794 int i;
7795 listitem_T *li;
7796 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007797 int wo_spell_save = curwin->w_p_spell;
7798
7799 if (!curwin->w_p_spell)
7800 {
7801 did_set_spelllang(curwin);
7802 curwin->w_p_spell = TRUE;
7803 }
7804
7805 if (*curwin->w_s->b_p_spl == NUL)
7806 {
7807 emsg(_(e_no_spell));
7808 curwin->w_p_spell = wo_spell_save;
7809 return;
7810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811#endif
7812
7813 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007814 {
7815#ifdef FEAT_SPELL
7816 curwin->w_p_spell = wo_spell_save;
7817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007819 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820
7821#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007822 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007824 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 if (argvars[1].v_type != VAR_UNKNOWN)
7826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007827 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 if (maxcount <= 0)
7829 return;
7830 if (argvars[2].v_type != VAR_UNKNOWN)
7831 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02007832 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 if (typeerr)
7834 return;
7835 }
7836 }
7837 else
7838 maxcount = 25;
7839
7840 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7841
7842 for (i = 0; i < ga.ga_len; ++i)
7843 {
7844 str = ((char_u **)ga.ga_data)[i];
7845
7846 li = listitem_alloc();
7847 if (li == NULL)
7848 vim_free(str);
7849 else
7850 {
7851 li->li_tv.v_type = VAR_STRING;
7852 li->li_tv.v_lock = 0;
7853 li->li_tv.vval.v_string = str;
7854 list_append(rettv->vval.v_list, li);
7855 }
7856 }
7857 ga_clear(&ga);
7858 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007859 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860#endif
7861}
7862
7863 static void
7864f_split(typval_T *argvars, typval_T *rettv)
7865{
7866 char_u *str;
7867 char_u *end;
7868 char_u *pat = NULL;
7869 regmatch_T regmatch;
7870 char_u patbuf[NUMBUFLEN];
7871 char_u *save_cpo;
7872 int match;
7873 colnr_T col = 0;
7874 int keepempty = FALSE;
7875 int typeerr = FALSE;
7876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007877 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 save_cpo = p_cpo;
7879 p_cpo = (char_u *)"";
7880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007881 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 if (argvars[1].v_type != VAR_UNKNOWN)
7883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007884 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 if (pat == NULL)
7886 typeerr = TRUE;
7887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02007888 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 }
7890 if (pat == NULL || *pat == NUL)
7891 pat = (char_u *)"[\\x01- ]\\+";
7892
7893 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007894 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007896 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897
7898 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7899 if (regmatch.regprog != NULL)
7900 {
7901 regmatch.rm_ic = FALSE;
7902 while (*str != NUL || keepempty)
7903 {
7904 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007905 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 else
7907 match = vim_regexec_nl(&regmatch, str, col);
7908 if (match)
7909 end = regmatch.startp[0];
7910 else
7911 end = str + STRLEN(str);
7912 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7913 && *str != NUL && match && end < regmatch.endp[0]))
7914 {
7915 if (list_append_string(rettv->vval.v_list, str,
7916 (int)(end - str)) == FAIL)
7917 break;
7918 }
7919 if (!match)
7920 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007921 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922 if (regmatch.endp[0] > str)
7923 col = 0;
7924 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007925 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 str = regmatch.endp[0];
7928 }
7929
7930 vim_regfree(regmatch.regprog);
7931 }
7932
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007933theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 p_cpo = save_cpo;
7935}
7936
7937#ifdef FEAT_FLOAT
7938/*
7939 * "sqrt()" function
7940 */
7941 static void
7942f_sqrt(typval_T *argvars, typval_T *rettv)
7943{
7944 float_T f = 0.0;
7945
7946 rettv->v_type = VAR_FLOAT;
7947 if (get_float_arg(argvars, &f) == OK)
7948 rettv->vval.v_float = sqrt(f);
7949 else
7950 rettv->vval.v_float = 0.0;
7951}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007952#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007954#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007955/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956 * "str2float()" function
7957 */
7958 static void
7959f_str2float(typval_T *argvars, typval_T *rettv)
7960{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007961 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007962 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963
Bram Moolenaar08243d22017-01-10 16:12:29 +01007964 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 p = skipwhite(p + 1);
7966 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007967 if (isneg)
7968 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 rettv->v_type = VAR_FLOAT;
7970}
7971#endif
7972
7973/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007974 * "str2list()" function
7975 */
7976 static void
7977f_str2list(typval_T *argvars, typval_T *rettv)
7978{
7979 char_u *p;
7980 int utf8 = FALSE;
7981
7982 if (rettv_list_alloc(rettv) == FAIL)
7983 return;
7984
7985 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02007986 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02007987
7988 p = tv_get_string(&argvars[0]);
7989
7990 if (has_mbyte || utf8)
7991 {
7992 int (*ptr2len)(char_u *);
7993 int (*ptr2char)(char_u *);
7994
7995 if (utf8 || enc_utf8)
7996 {
7997 ptr2len = utf_ptr2len;
7998 ptr2char = utf_ptr2char;
7999 }
8000 else
8001 {
8002 ptr2len = mb_ptr2len;
8003 ptr2char = mb_ptr2char;
8004 }
8005
8006 for ( ; *p != NUL; p += (*ptr2len)(p))
8007 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8008 }
8009 else
8010 for ( ; *p != NUL; ++p)
8011 list_append_number(rettv->vval.v_list, *p);
8012}
8013
8014/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 * "str2nr()" function
8016 */
8017 static void
8018f_str2nr(typval_T *argvars, typval_T *rettv)
8019{
8020 int base = 10;
8021 char_u *p;
8022 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008023 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008024 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025
8026 if (argvars[1].v_type != VAR_UNKNOWN)
8027 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008028 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 if (base != 2 && base != 8 && base != 10 && base != 16)
8030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008031 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 return;
8033 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008034 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008035 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 }
8037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008038 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008039 isneg = (*p == '-');
8040 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041 p = skipwhite(p + 1);
8042 switch (base)
8043 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008044 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008045 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008046 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008048 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8049 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008050 if (isneg)
8051 rettv->vval.v_number = -n;
8052 else
8053 rettv->vval.v_number = n;
8054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055}
8056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057/*
8058 * "strgetchar()" function
8059 */
8060 static void
8061f_strgetchar(typval_T *argvars, typval_T *rettv)
8062{
8063 char_u *str;
8064 int len;
8065 int error = FALSE;
8066 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008067 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068
8069 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008070 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 if (str == NULL)
8072 return;
8073 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008074 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 if (error)
8076 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077
Bram Moolenaar13505972019-01-24 15:04:48 +01008078 while (charidx >= 0 && byteidx < len)
8079 {
8080 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008082 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8083 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008085 --charidx;
8086 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088}
8089
8090/*
8091 * "stridx()" function
8092 */
8093 static void
8094f_stridx(typval_T *argvars, typval_T *rettv)
8095{
8096 char_u buf[NUMBUFLEN];
8097 char_u *needle;
8098 char_u *haystack;
8099 char_u *save_haystack;
8100 char_u *pos;
8101 int start_idx;
8102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008103 needle = tv_get_string_chk(&argvars[1]);
8104 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 rettv->vval.v_number = -1;
8106 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008107 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108
8109 if (argvars[2].v_type != VAR_UNKNOWN)
8110 {
8111 int error = FALSE;
8112
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008113 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008114 if (error || start_idx >= (int)STRLEN(haystack))
8115 return;
8116 if (start_idx >= 0)
8117 haystack += start_idx;
8118 }
8119
8120 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8121 if (pos != NULL)
8122 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8123}
8124
8125/*
8126 * "string()" function
8127 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008128 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129f_string(typval_T *argvars, typval_T *rettv)
8130{
8131 char_u *tofree;
8132 char_u numbuf[NUMBUFLEN];
8133
8134 rettv->v_type = VAR_STRING;
8135 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8136 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008137 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 if (rettv->vval.v_string != NULL && tofree == NULL)
8139 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8140}
8141
8142/*
8143 * "strlen()" function
8144 */
8145 static void
8146f_strlen(typval_T *argvars, typval_T *rettv)
8147{
8148 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008149 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150}
8151
8152/*
8153 * "strchars()" function
8154 */
8155 static void
8156f_strchars(typval_T *argvars, typval_T *rettv)
8157{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008158 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02008159 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 varnumber_T len = 0;
8161 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162
8163 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008164 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008166 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 else
8168 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8170 while (*s != NUL)
8171 {
8172 func_mb_ptr2char_adv(&s);
8173 ++len;
8174 }
8175 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 }
8177}
8178
8179/*
8180 * "strdisplaywidth()" function
8181 */
8182 static void
8183f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8184{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008185 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 int col = 0;
8187
8188 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008189 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190
8191 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8192}
8193
8194/*
8195 * "strwidth()" function
8196 */
8197 static void
8198f_strwidth(typval_T *argvars, typval_T *rettv)
8199{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008200 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201
Bram Moolenaar13505972019-01-24 15:04:48 +01008202 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203}
8204
8205/*
8206 * "strcharpart()" function
8207 */
8208 static void
8209f_strcharpart(typval_T *argvars, typval_T *rettv)
8210{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 char_u *p;
8212 int nchar;
8213 int nbyte = 0;
8214 int charlen;
8215 int len = 0;
8216 int slen;
8217 int error = FALSE;
8218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 slen = (int)STRLEN(p);
8221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008222 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 if (!error)
8224 {
8225 if (nchar > 0)
8226 while (nchar > 0 && nbyte < slen)
8227 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008228 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229 --nchar;
8230 }
8231 else
8232 nbyte = nchar;
8233 if (argvars[2].v_type != VAR_UNKNOWN)
8234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008235 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 while (charlen > 0 && nbyte + len < slen)
8237 {
8238 int off = nbyte + len;
8239
8240 if (off < 0)
8241 len += 1;
8242 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008243 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 --charlen;
8245 }
8246 }
8247 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008248 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 }
8250
8251 /*
8252 * Only return the overlap between the specified part and the actual
8253 * string.
8254 */
8255 if (nbyte < 0)
8256 {
8257 len += nbyte;
8258 nbyte = 0;
8259 }
8260 else if (nbyte > slen)
8261 nbyte = slen;
8262 if (len < 0)
8263 len = 0;
8264 else if (nbyte + len > slen)
8265 len = slen - nbyte;
8266
8267 rettv->v_type = VAR_STRING;
8268 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008269}
8270
8271/*
8272 * "strpart()" function
8273 */
8274 static void
8275f_strpart(typval_T *argvars, typval_T *rettv)
8276{
8277 char_u *p;
8278 int n;
8279 int len;
8280 int slen;
8281 int error = FALSE;
8282
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008283 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 slen = (int)STRLEN(p);
8285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008286 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 if (error)
8288 len = 0;
8289 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008290 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008292 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008294 // Only return the overlap between the specified part and the actual
8295 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 if (n < 0)
8297 {
8298 len += n;
8299 n = 0;
8300 }
8301 else if (n > slen)
8302 n = slen;
8303 if (len < 0)
8304 len = 0;
8305 else if (n + len > slen)
8306 len = slen - n;
8307
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008308 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8309 {
8310 int off;
8311
8312 // length in characters
8313 for (off = n; off < slen && len > 0; --len)
8314 off += mb_ptr2len(p + off);
8315 len = off - n;
8316 }
8317
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 rettv->v_type = VAR_STRING;
8319 rettv->vval.v_string = vim_strnsave(p + n, len);
8320}
8321
8322/*
8323 * "strridx()" function
8324 */
8325 static void
8326f_strridx(typval_T *argvars, typval_T *rettv)
8327{
8328 char_u buf[NUMBUFLEN];
8329 char_u *needle;
8330 char_u *haystack;
8331 char_u *rest;
8332 char_u *lastmatch = NULL;
8333 int haystack_len, end_idx;
8334
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008335 needle = tv_get_string_chk(&argvars[1]);
8336 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337
8338 rettv->vval.v_number = -1;
8339 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008340 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341
8342 haystack_len = (int)STRLEN(haystack);
8343 if (argvars[2].v_type != VAR_UNKNOWN)
8344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008345 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008346 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008348 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 }
8350 else
8351 end_idx = haystack_len;
8352
8353 if (*needle == NUL)
8354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008355 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 lastmatch = haystack + end_idx;
8357 }
8358 else
8359 {
8360 for (rest = haystack; *rest != '\0'; ++rest)
8361 {
8362 rest = (char_u *)strstr((char *)rest, (char *)needle);
8363 if (rest == NULL || rest > haystack + end_idx)
8364 break;
8365 lastmatch = rest;
8366 }
8367 }
8368
8369 if (lastmatch == NULL)
8370 rettv->vval.v_number = -1;
8371 else
8372 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8373}
8374
8375/*
8376 * "strtrans()" function
8377 */
8378 static void
8379f_strtrans(typval_T *argvars, typval_T *rettv)
8380{
8381 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008382 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383}
8384
8385/*
8386 * "submatch()" function
8387 */
8388 static void
8389f_submatch(typval_T *argvars, typval_T *rettv)
8390{
8391 int error = FALSE;
8392 int no;
8393 int retList = 0;
8394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008395 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 if (error)
8397 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008398 if (no < 0 || no >= NSUBEXP)
8399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008400 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008401 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008402 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02008404 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405 if (error)
8406 return;
8407
8408 if (retList == 0)
8409 {
8410 rettv->v_type = VAR_STRING;
8411 rettv->vval.v_string = reg_submatch(no);
8412 }
8413 else
8414 {
8415 rettv->v_type = VAR_LIST;
8416 rettv->vval.v_list = reg_submatch_list(no);
8417 }
8418}
8419
8420/*
8421 * "substitute()" function
8422 */
8423 static void
8424f_substitute(typval_T *argvars, typval_T *rettv)
8425{
8426 char_u patbuf[NUMBUFLEN];
8427 char_u subbuf[NUMBUFLEN];
8428 char_u flagsbuf[NUMBUFLEN];
8429
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008430 char_u *str = tv_get_string_chk(&argvars[0]);
8431 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008432 char_u *sub = NULL;
8433 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008434 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008435
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008436 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8437 expr = &argvars[2];
8438 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008439 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008441 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008442 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8443 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 rettv->vval.v_string = NULL;
8445 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008446 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447}
8448
8449/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008450 * "swapinfo(swap_filename)" function
8451 */
8452 static void
8453f_swapinfo(typval_T *argvars, typval_T *rettv)
8454{
8455 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008456 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008457}
8458
8459/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008460 * "swapname(expr)" function
8461 */
8462 static void
8463f_swapname(typval_T *argvars, typval_T *rettv)
8464{
8465 buf_T *buf;
8466
8467 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008468 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008469 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8470 || buf->b_ml.ml_mfp->mf_fname == NULL)
8471 rettv->vval.v_string = NULL;
8472 else
8473 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8474}
8475
8476/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 * "synID(lnum, col, trans)" function
8478 */
8479 static void
8480f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8481{
8482 int id = 0;
8483#ifdef FEAT_SYN_HL
8484 linenr_T lnum;
8485 colnr_T col;
8486 int trans;
8487 int transerr = FALSE;
8488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008489 lnum = tv_get_lnum(argvars); // -1 on type error
8490 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02008491 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492
8493 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8494 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8495 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8496#endif
8497
8498 rettv->vval.v_number = id;
8499}
8500
8501/*
8502 * "synIDattr(id, what [, mode])" function
8503 */
8504 static void
8505f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8506{
8507 char_u *p = NULL;
8508#ifdef FEAT_SYN_HL
8509 int id;
8510 char_u *what;
8511 char_u *mode;
8512 char_u modebuf[NUMBUFLEN];
8513 int modec;
8514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008515 id = (int)tv_get_number(&argvars[0]);
8516 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 if (argvars[2].v_type != VAR_UNKNOWN)
8518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008519 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 modec = TOLOWER_ASC(mode[0]);
8521 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008522 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 }
8524 else
8525 {
8526#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8527 if (USE_24BIT)
8528 modec = 'g';
8529 else
8530#endif
8531 if (t_colors > 1)
8532 modec = 'c';
8533 else
8534 modec = 't';
8535 }
8536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 switch (TOLOWER_ASC(what[0]))
8538 {
8539 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008540 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008542 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 p = highlight_has_attr(id, HL_BOLD, modec);
8544 break;
8545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008546 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 p = highlight_color(id, what, modec);
8548 break;
8549
8550 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008551 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008553 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 p = highlight_has_attr(id, HL_ITALIC, modec);
8555 break;
8556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008557 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008558 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 break;
8560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008561 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 p = highlight_has_attr(id, HL_INVERSE, modec);
8563 break;
8564
8565 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008566 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008568 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008569 else if (TOLOWER_ASC(what[1]) == 't' &&
8570 TOLOWER_ASC(what[2]) == 'r')
8571 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008572 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573 p = highlight_has_attr(id, HL_STANDOUT, modec);
8574 break;
8575
8576 case 'u':
8577 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008578 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8580 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008581 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8583 break;
8584 }
8585
8586 if (p != NULL)
8587 p = vim_strsave(p);
8588#endif
8589 rettv->v_type = VAR_STRING;
8590 rettv->vval.v_string = p;
8591}
8592
8593/*
8594 * "synIDtrans(id)" function
8595 */
8596 static void
8597f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8598{
8599 int id;
8600
8601#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008602 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603
8604 if (id > 0)
8605 id = syn_get_final_id(id);
8606 else
8607#endif
8608 id = 0;
8609
8610 rettv->vval.v_number = id;
8611}
8612
8613/*
8614 * "synconcealed(lnum, col)" function
8615 */
8616 static void
8617f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8618{
8619#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8620 linenr_T lnum;
8621 colnr_T col;
8622 int syntax_flags = 0;
8623 int cchar;
8624 int matchid = 0;
8625 char_u str[NUMBUFLEN];
8626#endif
8627
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008628 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629
8630#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008631 lnum = tv_get_lnum(argvars); // -1 on type error
8632 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633
Bram Moolenaara80faa82020-04-12 19:37:17 +02008634 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635
8636 if (rettv_list_alloc(rettv) != FAIL)
8637 {
8638 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8639 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8640 && curwin->w_p_cole > 0)
8641 {
8642 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8643 syntax_flags = get_syntax_info(&matchid);
8644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008645 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8647 {
8648 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008649 if (cchar == NUL && curwin->w_p_cole == 1)
8650 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 if (cchar != NUL)
8652 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 if (has_mbyte)
8654 (*mb_char2bytes)(cchar, str);
8655 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 str[0] = cchar;
8657 }
8658 }
8659 }
8660
8661 list_append_number(rettv->vval.v_list,
8662 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008663 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 list_append_string(rettv->vval.v_list, str, -1);
8665 list_append_number(rettv->vval.v_list, matchid);
8666 }
8667#endif
8668}
8669
8670/*
8671 * "synstack(lnum, col)" function
8672 */
8673 static void
8674f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8675{
8676#ifdef FEAT_SYN_HL
8677 linenr_T lnum;
8678 colnr_T col;
8679 int i;
8680 int id;
8681#endif
8682
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008683 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684
8685#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008686 lnum = tv_get_lnum(argvars); // -1 on type error
8687 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688
8689 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8690 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8691 && rettv_list_alloc(rettv) != FAIL)
8692 {
8693 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8694 for (i = 0; ; ++i)
8695 {
8696 id = syn_get_stack_item(i);
8697 if (id < 0)
8698 break;
8699 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8700 break;
8701 }
8702 }
8703#endif
8704}
8705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706/*
8707 * "tabpagebuflist()" function
8708 */
8709 static void
8710f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8711{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 tabpage_T *tp;
8713 win_T *wp = NULL;
8714
8715 if (argvars[0].v_type == VAR_UNKNOWN)
8716 wp = firstwin;
8717 else
8718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008719 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 if (tp != NULL)
8721 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8722 }
8723 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8724 {
8725 for (; wp != NULL; wp = wp->w_next)
8726 if (list_append_number(rettv->vval.v_list,
8727 wp->w_buffer->b_fnum) == FAIL)
8728 break;
8729 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730}
8731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 * "tagfiles()" function
8734 */
8735 static void
8736f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8737{
8738 char_u *fname;
8739 tagname_T tn;
8740 int first;
8741
8742 if (rettv_list_alloc(rettv) == FAIL)
8743 return;
8744 fname = alloc(MAXPATHL);
8745 if (fname == NULL)
8746 return;
8747
8748 for (first = TRUE; ; first = FALSE)
8749 if (get_tagfname(&tn, first, fname) == FAIL
8750 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8751 break;
8752 tagname_free(&tn);
8753 vim_free(fname);
8754}
8755
8756/*
8757 * "taglist()" function
8758 */
8759 static void
8760f_taglist(typval_T *argvars, typval_T *rettv)
8761{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008762 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763 char_u *tag_pattern;
8764
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008765 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766
8767 rettv->vval.v_number = FALSE;
8768 if (*tag_pattern == NUL)
8769 return;
8770
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008772 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008774 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775}
8776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777#ifdef FEAT_FLOAT
8778/*
8779 * "tan()" function
8780 */
8781 static void
8782f_tan(typval_T *argvars, typval_T *rettv)
8783{
8784 float_T f = 0.0;
8785
8786 rettv->v_type = VAR_FLOAT;
8787 if (get_float_arg(argvars, &f) == OK)
8788 rettv->vval.v_float = tan(f);
8789 else
8790 rettv->vval.v_float = 0.0;
8791}
8792
8793/*
8794 * "tanh()" function
8795 */
8796 static void
8797f_tanh(typval_T *argvars, typval_T *rettv)
8798{
8799 float_T f = 0.0;
8800
8801 rettv->v_type = VAR_FLOAT;
8802 if (get_float_arg(argvars, &f) == OK)
8803 rettv->vval.v_float = tanh(f);
8804 else
8805 rettv->vval.v_float = 0.0;
8806}
8807#endif
8808
8809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810 * "tolower(string)" function
8811 */
8812 static void
8813f_tolower(typval_T *argvars, typval_T *rettv)
8814{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008816 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817}
8818
8819/*
8820 * "toupper(string)" function
8821 */
8822 static void
8823f_toupper(typval_T *argvars, typval_T *rettv)
8824{
8825 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008826 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827}
8828
8829/*
8830 * "tr(string, fromstr, tostr)" function
8831 */
8832 static void
8833f_tr(typval_T *argvars, typval_T *rettv)
8834{
8835 char_u *in_str;
8836 char_u *fromstr;
8837 char_u *tostr;
8838 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 int inlen;
8840 int fromlen;
8841 int tolen;
8842 int idx;
8843 char_u *cpstr;
8844 int cplen;
8845 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 char_u buf[NUMBUFLEN];
8847 char_u buf2[NUMBUFLEN];
8848 garray_T ga;
8849
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008850 in_str = tv_get_string(&argvars[0]);
8851 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8852 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008854 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 rettv->v_type = VAR_STRING;
8856 rettv->vval.v_string = NULL;
8857 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008858 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 ga_init2(&ga, (int)sizeof(char), 80);
8860
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008862 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 if (STRLEN(fromstr) != STRLEN(tostr))
8864 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008866 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 ga_clear(&ga);
8868 return;
8869 }
8870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008871 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 while (*in_str != NUL)
8873 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (has_mbyte)
8875 {
8876 inlen = (*mb_ptr2len)(in_str);
8877 cpstr = in_str;
8878 cplen = inlen;
8879 idx = 0;
8880 for (p = fromstr; *p != NUL; p += fromlen)
8881 {
8882 fromlen = (*mb_ptr2len)(p);
8883 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8884 {
8885 for (p = tostr; *p != NUL; p += tolen)
8886 {
8887 tolen = (*mb_ptr2len)(p);
8888 if (idx-- == 0)
8889 {
8890 cplen = tolen;
8891 cpstr = p;
8892 break;
8893 }
8894 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008895 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 goto error;
8897 break;
8898 }
8899 ++idx;
8900 }
8901
8902 if (first && cpstr == in_str)
8903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008904 // Check that fromstr and tostr have the same number of
8905 // (multi-byte) characters. Done only once when a character
8906 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 first = FALSE;
8908 for (p = tostr; *p != NUL; p += tolen)
8909 {
8910 tolen = (*mb_ptr2len)(p);
8911 --idx;
8912 }
8913 if (idx != 0)
8914 goto error;
8915 }
8916
8917 (void)ga_grow(&ga, cplen);
8918 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8919 ga.ga_len += cplen;
8920
8921 in_str += inlen;
8922 }
8923 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008925 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 p = vim_strchr(fromstr, *in_str);
8927 if (p != NULL)
8928 ga_append(&ga, tostr[p - fromstr]);
8929 else
8930 ga_append(&ga, *in_str);
8931 ++in_str;
8932 }
8933 }
8934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008935 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 (void)ga_grow(&ga, 1);
8937 ga_append(&ga, NUL);
8938
8939 rettv->vval.v_string = ga.ga_data;
8940}
8941
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008942/*
8943 * "trim({expr})" function
8944 */
8945 static void
8946f_trim(typval_T *argvars, typval_T *rettv)
8947{
8948 char_u buf1[NUMBUFLEN];
8949 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008950 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008951 char_u *mask = NULL;
8952 char_u *tail;
8953 char_u *prev;
8954 char_u *p;
8955 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008956 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008957
8958 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008959 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008960 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008961 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008962
8963 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008965 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008966
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008968 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008969 int error = 0;
8970
8971 // leading or trailing characters to trim
8972 dir = (int)tv_get_number_chk(&argvars[2], &error);
8973 if (error)
8974 return;
8975 if (dir < 0 || dir > 2)
8976 {
8977 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8978 return;
8979 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008980 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008981 }
8982
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008983 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008984 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008985 // Trim leading characters
8986 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008987 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008988 c1 = PTR2CHAR(head);
8989 if (mask == NULL)
8990 {
8991 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008992 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008993 }
8994 else
8995 {
8996 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8997 if (c1 == PTR2CHAR(p))
8998 break;
8999 if (*p == NUL)
9000 break;
9001 }
9002 MB_PTR_ADV(head);
9003 }
9004 }
9005
9006 tail = head + STRLEN(head);
9007 if (dir == 0 || dir == 2)
9008 {
9009 // Trim trailing characters
9010 for (; tail > head; tail = prev)
9011 {
9012 prev = tail;
9013 MB_PTR_BACK(head, prev);
9014 c1 = PTR2CHAR(prev);
9015 if (mask == NULL)
9016 {
9017 if (c1 > ' ' && c1 != 0xa0)
9018 break;
9019 }
9020 else
9021 {
9022 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9023 if (c1 == PTR2CHAR(p))
9024 break;
9025 if (*p == NUL)
9026 break;
9027 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009028 }
9029 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009030 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009031}
9032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033#ifdef FEAT_FLOAT
9034/*
9035 * "trunc({float})" function
9036 */
9037 static void
9038f_trunc(typval_T *argvars, typval_T *rettv)
9039{
9040 float_T f = 0.0;
9041
9042 rettv->v_type = VAR_FLOAT;
9043 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009044 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9046 else
9047 rettv->vval.v_float = 0.0;
9048}
9049#endif
9050
9051/*
9052 * "type(expr)" function
9053 */
9054 static void
9055f_type(typval_T *argvars, typval_T *rettv)
9056{
9057 int n = -1;
9058
9059 switch (argvars[0].v_type)
9060 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009061 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9062 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009064 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9065 case VAR_LIST: n = VAR_TYPE_LIST; break;
9066 case VAR_DICT: n = VAR_TYPE_DICT; break;
9067 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9068 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9069 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009070 case VAR_JOB: n = VAR_TYPE_JOB; break;
9071 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009072 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009073 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009074 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009075 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009076 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 n = -1;
9078 break;
9079 }
9080 rettv->vval.v_number = n;
9081}
9082
9083/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 * "virtcol(string)" function
9085 */
9086 static void
9087f_virtcol(typval_T *argvars, typval_T *rettv)
9088{
9089 colnr_T vcol = 0;
9090 pos_T *fp;
9091 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009092 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093
9094 fp = var2fpos(&argvars[0], FALSE, &fnum);
9095 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9096 && fnum == curbuf->b_fnum)
9097 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009098 // Limit the column to a valid value, getvvcol() doesn't check.
9099 if (fp->col < 0)
9100 fp->col = 0;
9101 else
9102 {
9103 len = (int)STRLEN(ml_get(fp->lnum));
9104 if (fp->col > len)
9105 fp->col = len;
9106 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 getvvcol(curwin, fp, NULL, NULL, &vcol);
9108 ++vcol;
9109 }
9110
9111 rettv->vval.v_number = vcol;
9112}
9113
9114/*
9115 * "visualmode()" function
9116 */
9117 static void
9118f_visualmode(typval_T *argvars, typval_T *rettv)
9119{
9120 char_u str[2];
9121
9122 rettv->v_type = VAR_STRING;
9123 str[0] = curbuf->b_visual_mode_eval;
9124 str[1] = NUL;
9125 rettv->vval.v_string = vim_strsave(str);
9126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009127 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 if (non_zero_arg(&argvars[0]))
9129 curbuf->b_visual_mode_eval = NUL;
9130}
9131
9132/*
9133 * "wildmenumode()" function
9134 */
9135 static void
9136f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9137{
9138#ifdef FEAT_WILDMENU
9139 if (wild_menu_showing)
9140 rettv->vval.v_number = 1;
9141#endif
9142}
9143
9144/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009145 * "windowsversion()" function
9146 */
9147 static void
9148f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9149{
9150 rettv->v_type = VAR_STRING;
9151 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9152}
9153
9154/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155 * "wordcount()" function
9156 */
9157 static void
9158f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9159{
9160 if (rettv_dict_alloc(rettv) == FAIL)
9161 return;
9162 cursor_pos_info(rettv->vval.v_dict);
9163}
9164
9165/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166 * "xor(expr, expr)" function
9167 */
9168 static void
9169f_xor(typval_T *argvars, typval_T *rettv)
9170{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009171 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9172 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009173}
9174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009175#endif // FEAT_EVAL