blob: de723aaef77f1523544b9e2564b59d64f3634b65 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200100static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
102static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_hlID(typval_T *argvars, typval_T *rettv);
104static void f_hlexists(typval_T *argvars, typval_T *rettv);
105static void f_hostname(typval_T *argvars, typval_T *rettv);
106static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_index(typval_T *argvars, typval_T *rettv);
108static void f_input(typval_T *argvars, typval_T *rettv);
109static void f_inputdialog(typval_T *argvars, typval_T *rettv);
110static void f_inputlist(typval_T *argvars, typval_T *rettv);
111static void f_inputrestore(typval_T *argvars, typval_T *rettv);
112static void f_inputsave(typval_T *argvars, typval_T *rettv);
113static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100114static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_islocked(typval_T *argvars, typval_T *rettv);
117#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200118static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_isnan(typval_T *argvars, typval_T *rettv);
120#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
122static void f_len(typval_T *argvars, typval_T *rettv);
123static void f_libcall(typval_T *argvars, typval_T *rettv);
124static void f_libcallnr(typval_T *argvars, typval_T *rettv);
125static void f_line(typval_T *argvars, typval_T *rettv);
126static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127#ifdef FEAT_FLOAT
128static void f_log(typval_T *argvars, typval_T *rettv);
129static void f_log10(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_LUA
132static void f_luaeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_maparg(typval_T *argvars, typval_T *rettv);
135static void f_mapcheck(typval_T *argvars, typval_T *rettv);
136static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200137static void f_matchend(typval_T *argvars, typval_T *rettv);
138static void f_matchlist(typval_T *argvars, typval_T *rettv);
139static void f_matchstr(typval_T *argvars, typval_T *rettv);
140static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
141static void f_max(typval_T *argvars, typval_T *rettv);
142static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200143#ifdef FEAT_MZSCHEME
144static void f_mzeval(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
147static void f_nr2char(typval_T *argvars, typval_T *rettv);
148static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200149#ifdef FEAT_PERL
150static void f_perleval(typval_T *argvars, typval_T *rettv);
151#endif
152#ifdef FEAT_FLOAT
153static void f_pow(typval_T *argvars, typval_T *rettv);
154#endif
155static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
156static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200157static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_pumvisible(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_PYTHON3
160static void f_py3eval(typval_T *argvars, typval_T *rettv);
161#endif
162#ifdef FEAT_PYTHON
163static void f_pyeval(typval_T *argvars, typval_T *rettv);
164#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100165#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
166static void f_pyxeval(typval_T *argvars, typval_T *rettv);
167#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100168static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100169static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200171static void f_reg_executing(typval_T *argvars, typval_T *rettv);
172static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_rename(typval_T *argvars, typval_T *rettv);
174static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175#ifdef FEAT_FLOAT
176static void f_round(typval_T *argvars, typval_T *rettv);
177#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100178#ifdef FEAT_RUBY
179static void f_rubyeval(typval_T *argvars, typval_T *rettv);
180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_screenattr(typval_T *argvars, typval_T *rettv);
182static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100183static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_screencol(typval_T *argvars, typval_T *rettv);
185static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100186static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_search(typval_T *argvars, typval_T *rettv);
188static void f_searchdecl(typval_T *argvars, typval_T *rettv);
189static void f_searchpair(typval_T *argvars, typval_T *rettv);
190static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
191static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200193static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100197static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198#ifdef FEAT_CRYPT
199static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200200#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_shellescape(typval_T *argvars, typval_T *rettv);
202static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200203#ifdef FEAT_FLOAT
204static void f_sin(typval_T *argvars, typval_T *rettv);
205static void f_sinh(typval_T *argvars, typval_T *rettv);
206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_soundfold(typval_T *argvars, typval_T *rettv);
208static void f_spellbadword(typval_T *argvars, typval_T *rettv);
209static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
210static void f_split(typval_T *argvars, typval_T *rettv);
211#ifdef FEAT_FLOAT
212static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100213#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100214static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100215#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200216static void f_str2float(typval_T *argvars, typval_T *rettv);
217#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200218static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200219static void f_str2nr(typval_T *argvars, typval_T *rettv);
220static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200221static void f_strgetchar(typval_T *argvars, typval_T *rettv);
222static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200223static void f_strlen(typval_T *argvars, typval_T *rettv);
224static void f_strcharpart(typval_T *argvars, typval_T *rettv);
225static void f_strpart(typval_T *argvars, typval_T *rettv);
226static void f_strridx(typval_T *argvars, typval_T *rettv);
227static void f_strtrans(typval_T *argvars, typval_T *rettv);
228static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
229static void f_strwidth(typval_T *argvars, typval_T *rettv);
230static void f_submatch(typval_T *argvars, typval_T *rettv);
231static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200232static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200233static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_synID(typval_T *argvars, typval_T *rettv);
235static void f_synIDattr(typval_T *argvars, typval_T *rettv);
236static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
237static void f_synstack(typval_T *argvars, typval_T *rettv);
238static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_taglist(typval_T *argvars, typval_T *rettv);
241static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200242#ifdef FEAT_FLOAT
243static void f_tan(typval_T *argvars, typval_T *rettv);
244static void f_tanh(typval_T *argvars, typval_T *rettv);
245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tolower(typval_T *argvars, typval_T *rettv);
247static void f_toupper(typval_T *argvars, typval_T *rettv);
248static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100249static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200250#ifdef FEAT_FLOAT
251static void f_trunc(typval_T *argvars, typval_T *rettv);
252#endif
253static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200254static void f_virtcol(typval_T *argvars, typval_T *rettv);
255static void f_visualmode(typval_T *argvars, typval_T *rettv);
256static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100257static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200258static void f_wordcount(typval_T *argvars, typval_T *rettv);
259static void f_xor(typval_T *argvars, typval_T *rettv);
260
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100261
262 static type_T *
263ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
264{
265 return &t_void;
266}
267 static type_T *
268ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
269{
270 return &t_any;
271}
272 static type_T *
273ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
274{
275 return &t_number;
276}
277 static type_T *
278ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
279{
280 return &t_float;
281}
282 static type_T *
283ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
284{
285 return &t_string;
286}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200287 static type_T *
288ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100289{
290 return &t_list_any;
291}
292 static type_T *
293ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
294{
295 return &t_list_number;
296}
297 static type_T *
298ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
299{
300 return &t_list_string;
301}
302 static type_T *
303ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
304{
305 return &t_list_dict_any;
306}
307 static type_T *
308ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
309{
310 return &t_dict_any;
311}
312 static type_T *
313ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
314{
315 return &t_dict_number;
316}
317 static type_T *
318ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
319{
320 return &t_dict_string;
321}
322 static type_T *
323ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
324{
325 return &t_blob;
326}
327 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200328ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100329{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200330 return &t_func_any;
331}
332 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100333ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
334{
335 return &t_channel;
336}
337 static type_T *
338ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
339{
340 return &t_job;
341}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100342
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200343 static type_T *
344ret_first_arg(int argcount, type_T **argtypes)
345{
346 if (argcount > 0)
347 return argtypes[0];
348 return &t_void;
349}
350
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200351/*
352 * Used for getqflist(): returns list if there is no argument, dict if there is
353 * one.
354 */
355 static type_T *
356ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
357{
358 if (argcount > 0)
359 return &t_dict_any;
360 return &t_list_dict_any;
361}
362
363/*
364 * Used for getloclist(): returns list if there is one argument, dict if there
365 * are two.
366 */
367 static type_T *
368ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
369{
370 if (argcount > 1)
371 return &t_dict_any;
372 return &t_list_dict_any;
373}
374
Bram Moolenaar846178a2020-07-05 17:04:13 +0200375 static type_T *
376ret_argv(int argcount, type_T **argtypes UNUSED)
377{
378 // argv() returns list of strings
379 if (argcount == 0)
380 return &t_list_string;
381
382 // argv(0) returns a string, but argv(-1] returns a list
383 return &t_any;
384}
385
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200386 static type_T *
387ret_remove(int argcount UNUSED, type_T **argtypes)
388{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200389 if (argtypes != NULL)
390 {
391 if (argtypes[0]->tt_type == VAR_LIST
392 || argtypes[0]->tt_type == VAR_DICT)
393 return argtypes[0]->tt_member;
394 if (argtypes[0]->tt_type == VAR_BLOB)
395 return &t_number;
396 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200397 return &t_any;
398}
399
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200400 static type_T *
401ret_getreg(int argcount, type_T **argtypes UNUSED)
402{
403 // Assume that if the third argument is passed it's non-zero
404 if (argcount == 3)
405 return &t_list_string;
406 return &t_string;
407}
408
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200409 static type_T *
410ret_maparg(int argcount, type_T **argtypes UNUSED)
411{
412 // Assume that if the fourth argument is passed it's non-zero
413 if (argcount == 4)
414 return &t_dict_any;
415 return &t_string;
416}
417
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100418static type_T *ret_f_function(int argcount, type_T **argtypes);
419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420/*
421 * Array with names and number of arguments of all internal functions
422 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
423 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200424typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200426 char *f_name; // function name
427 char f_min_argc; // minimal number of arguments
428 char f_max_argc; // maximal number of arguments
429 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100430 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
431 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200432 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200433 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200434} funcentry_T;
435
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200436// values for f_argtype; zero means it cannot be used as a method
437#define FEARG_1 1 // base is the first argument
438#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200439#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200440#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200441#define FEARG_LAST 9 // base is the last argument
442
Bram Moolenaar15c47602020-03-26 22:16:48 +0100443#ifdef FEAT_FLOAT
444# define FLOAT_FUNC(name) name
445#else
446# define FLOAT_FUNC(name) NULL
447#endif
448#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
449# define MATH_FUNC(name) name
450#else
451# define MATH_FUNC(name) NULL
452#endif
453#ifdef FEAT_TIMERS
454# define TIMER_FUNC(name) name
455#else
456# define TIMER_FUNC(name) NULL
457#endif
458#ifdef FEAT_JOB_CHANNEL
459# define JOB_FUNC(name) name
460#else
461# define JOB_FUNC(name) NULL
462#endif
463#ifdef FEAT_PROP_POPUP
464# define PROP_FUNC(name) name
465#else
466# define PROP_FUNC(name) NULL
467#endif
468#ifdef FEAT_SIGNS
469# define SIGN_FUNC(name) name
470#else
471# define SIGN_FUNC(name) NULL
472#endif
473#ifdef FEAT_SOUND
474# define SOUND_FUNC(name) name
475#else
476# define SOUND_FUNC(name) NULL
477#endif
478#ifdef FEAT_TERMINAL
479# define TERM_FUNC(name) name
480#else
481# define TERM_FUNC(name) NULL
482#endif
483
Bram Moolenaarac92e252019-08-03 21:58:38 +0200484static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100486 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
487 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200488 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100489 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200490 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200491 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100492 {"argc", 0, 1, 0, ret_number, f_argc},
493 {"argidx", 0, 0, 0, ret_number, f_argidx},
494 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200495 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100496 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100497 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
498 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200499 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100500 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar9bd5d872020-09-06 21:47:48 +0200501 {"assert_fails", 1, 5, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100502 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
503 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
504 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
505 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
506 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
507 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
508 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100509 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
510 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
511 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100513 f_balloon_gettext
514#else
515 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100516#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100517 },
518 {"balloon_show", 1, 1, FEARG_1, ret_void,
519#ifdef FEAT_BEVAL
520 f_balloon_show
521#else
522 NULL
523#endif
524 },
525 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
526#if defined(FEAT_BEVAL_TERM)
527 f_balloon_split
528#else
529 NULL
530#endif
531 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100532 {"browse", 4, 4, 0, ret_string, f_browse},
533 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
534 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
535 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
536 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
537 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
538 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
539 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
540 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
541 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
542 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
543 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
544 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
545 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
546 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
547 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
548 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
549 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100550 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
551 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
552 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
553 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
554 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
555 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
556 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
557 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
558 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
559 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
560 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
561 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
562 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
563 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
564 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
565 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
566 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
567 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
568 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100569 {"changenr", 0, 0, 0, ret_number, f_changenr},
570 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
Bram Moolenaar4e4473c2020-08-28 22:24:57 +0200571 {"charclass", 1, 1, FEARG_1, ret_number, f_charclass},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100572 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
573 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
574 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
575 {"col", 1, 1, FEARG_1, ret_number, f_col},
576 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
577 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
578 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
579 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
580 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200581 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100582 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
583 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100584 {"count", 2, 4, FEARG_1, ret_number, f_count},
585 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
586 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100587 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100588#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100589 f_debugbreak
590#else
591 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200592#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100593 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200594 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100595 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
596 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
597 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
598 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
599 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
600 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
601 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
602 {"environ", 0, 0, 0, ret_dict_string, f_environ},
603 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
604 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
605 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
606 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
607 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
608 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
609 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100610 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
612 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200613 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100614 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
615 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
616 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
617 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200618 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100619 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
620 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200621 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100622 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
623 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
624 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100625 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
626 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
627 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
628 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
629 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
630 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
631 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
632 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200633 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
635 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
636 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200637 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100638 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
639 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
640 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
641 {"getchar", 0, 1, 0, ret_number, f_getchar},
642 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
643 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
644 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
645 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
646 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
647 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
648 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
649 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
650 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
651 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
652 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
653 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
654 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
655 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
656 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
657 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
658 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
659 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200660 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200661 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100662 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
663 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
664 {"getpid", 0, 0, 0, ret_number, f_getpid},
665 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200666 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200667 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200668 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100669 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
670 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
671 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
672 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
673 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200674 {"gettext", 1, 1, FEARG_1, ret_string, f_gettext},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100675 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
676 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
677 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
678 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
679 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
680 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
681 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
682 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100683 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100684 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
685 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
686 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
687 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
688 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
689 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
690 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
691 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
692 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
693 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
694 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
695 {"hostname", 0, 0, 0, ret_string, f_hostname},
696 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
697 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
698 {"index", 2, 4, FEARG_1, ret_number, f_index},
699 {"input", 1, 3, FEARG_1, ret_string, f_input},
700 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
701 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
702 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
703 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
704 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200705 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100706 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
707 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
708 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100709 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100710 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100711 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100712 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100713 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
714 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
715 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
716 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
717 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
718 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100719 {"join", 1, 2, FEARG_1, ret_string, f_join},
720 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
721 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
722 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
723 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200724 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100725 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
726 {"len", 1, 1, FEARG_1, ret_number, f_len},
727 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
728 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
729 {"line", 1, 2, FEARG_1, ret_number, f_line},
730 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
731 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
732 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
733 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
734 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
735 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
736 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
738 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
739 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200740#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 f_luaeval
742#else
743 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100745 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100746 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200747 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100748 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200749 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100750 {"match", 2, 4, FEARG_1, ret_any, f_match},
751 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
752 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
753 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
754 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
755 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200756 {"matchfuzzy", 2, 3, FEARG_1, ret_list_string, f_matchfuzzy},
757 {"matchfuzzypos", 2, 3, FEARG_1, ret_list_any, f_matchfuzzypos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100758 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
759 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
760 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
761 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100762 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100763#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764 f_menu_info
765#else
766 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100767#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100768 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100769 {"min", 1, 1, FEARG_1, ret_any, f_min},
770 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
771 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100772 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100774 f_mzeval
775#else
776 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100779 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
780 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
781 {"or", 2, 2, FEARG_1, ret_number, f_or},
782 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100783 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100785 f_perleval
786#else
787 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100789 },
790 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
791 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200792 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100793 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
794 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
795 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
796 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
797 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
798 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
799 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
800 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
801 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
802 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200803 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100804 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
805 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
806 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
807 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
808 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
809 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
810 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
811 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100812 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
813 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200814 {"prompt_getprompt", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_prompt_getprompt)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100815 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
816 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
817 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
818 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
819 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
820 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
821 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
822 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
823 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
824 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
825 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
826 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
827 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100828 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
829 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100830 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100832 f_py3eval
833#else
834 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100836 },
837 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100839 f_pyeval
840#else
841 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100843 },
844 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 f_pyxeval
847#else
848 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100849#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100850 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100851 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
852 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200853 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
854 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100855 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200856 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100857 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
858 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
859 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100860 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100861 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
862 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
863 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
864 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
865 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
866 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200867 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200868 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100869 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200870 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100871 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200872 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100873 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
874 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100875#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100876 f_rubyeval
877#else
878 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100879#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100880 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100881 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
882 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
883 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
884 {"screencol", 0, 0, 0, ret_number, f_screencol},
885 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
886 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
887 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200888 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200889 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100890 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
891 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
892 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200893 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100894 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
895 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
896 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
897 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200898 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100899 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
900 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
901 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
902 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
903 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
904 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
905 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
906 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
907 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
908 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
909 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
910 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
911 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
912 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100913 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 f_sha256
916#else
917 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100919 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100920 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
921 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100922 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
923 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
924 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
925 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
926 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
927 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
928 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
929 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
930 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200931 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100932 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
933 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200934 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100935 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
936 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
937 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
938 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100939 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
940 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
941 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
942 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100943 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100944 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
945 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100946 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100947 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
948 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
949 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
950 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
951 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100952 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 f_strftime
955#else
956 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100958 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100959 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
960 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
961 {"string", 1, 1, FEARG_1, ret_string, f_string},
962 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200963 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100964 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100965#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100966 f_strptime
967#else
968 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100969#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100970 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100971 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
972 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
973 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
974 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
975 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
976 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
977 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
978 {"synID", 3, 3, 0, ret_number, f_synID},
979 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
980 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
981 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
982 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
983 {"system", 1, 2, FEARG_1, ret_string, f_system},
984 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
985 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
986 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
987 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
988 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
989 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100990 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
991 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100992 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100993 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
994 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
995 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
996 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
997 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100998#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100999 f_term_getansicolors
1000#else
1001 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001002#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001003 },
1004 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
1005 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
1006 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
1007 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1008 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1009 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1010 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1011 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1012 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1013 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1014 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1015 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1016 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001017#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001018 f_term_setansicolors
1019#else
1020 NULL
1021#endif
1022 },
1023 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1024 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1025 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1026 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1027 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1028 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001029 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001030 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1031 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1032 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1033 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1034 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1035 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1036 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1037 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001038 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001039 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001040 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001041 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001042 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001043 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001044 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1045 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1046 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1047 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001048 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001049#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 f_test_scrollbar
1051#else
1052 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001053#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001054 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001055 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1056 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1057 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1058 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001059 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001060 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1061 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1062 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1063 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1064 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001065 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1066 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1067 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001068 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001069 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001070 {"type", 1, 1, FEARG_1, ret_number, f_type},
1071 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1072 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1073 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1074 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1075 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1076 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1077 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1078 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1079 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1080 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1081 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1082 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1083 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1084 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1085 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1086 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1087 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1088 {"wincol", 0, 0, 0, ret_number, f_wincol},
1089 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1090 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1091 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1092 {"winline", 0, 0, 0, ret_number, f_winline},
1093 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1094 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1095 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1096 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1097 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1098 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1099 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1100 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001101};
1102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001103/*
1104 * Function given to ExpandGeneric() to obtain the list of internal
1105 * or user defined function names.
1106 */
1107 char_u *
1108get_function_name(expand_T *xp, int idx)
1109{
1110 static int intidx = -1;
1111 char_u *name;
1112
1113 if (idx == 0)
1114 intidx = -1;
1115 if (intidx < 0)
1116 {
1117 name = get_user_func_name(xp, idx);
1118 if (name != NULL)
1119 return name;
1120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001121 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001122 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001123 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001124 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001125 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126 STRCAT(IObuff, ")");
1127 return IObuff;
1128 }
1129
1130 return NULL;
1131}
1132
1133/*
1134 * Function given to ExpandGeneric() to obtain the list of internal or
1135 * user defined variable or function names.
1136 */
1137 char_u *
1138get_expr_name(expand_T *xp, int idx)
1139{
1140 static int intidx = -1;
1141 char_u *name;
1142
1143 if (idx == 0)
1144 intidx = -1;
1145 if (intidx < 0)
1146 {
1147 name = get_function_name(xp, idx);
1148 if (name != NULL)
1149 return name;
1150 }
1151 return get_user_var_name(xp, ++intidx);
1152}
1153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001155 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001156 * Return index, or -1 if not found or "implemented" is TRUE and the function
1157 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001158 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001159 static int
1160find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001161{
1162 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001163 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164 int cmp;
1165 int x;
1166
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001167 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001168
1169 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001170 while (first <= last)
1171 {
1172 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001173 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174 if (cmp < 0)
1175 last = x - 1;
1176 else if (cmp > 0)
1177 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001178 else if (implemented && global_functions[x].f_func == NULL)
1179 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001180 else
1181 return x;
1182 }
1183 return -1;
1184}
1185
Bram Moolenaar15c47602020-03-26 22:16:48 +01001186/*
1187 * Find internal function "name" in table "global_functions".
1188 * Return index, or -1 if not found or the function is not implemented.
1189 */
1190 int
1191find_internal_func(char_u *name)
1192{
1193 return find_internal_func_opt(name, TRUE);
1194}
1195
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001197has_internal_func(char_u *name)
1198{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001199 return find_internal_func_opt(name, TRUE) >= 0;
1200}
1201
1202 static int
1203has_internal_func_name(char_u *name)
1204{
1205 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001206}
1207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 char *
1209internal_func_name(int idx)
1210{
1211 return global_functions[idx].f_name;
1212}
1213
1214 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001215internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001217 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218}
1219
1220/*
1221 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001222 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1223 * first argument, 2 if method base is second argument, etc. 9 if method base
1224 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 */
1226 int
1227check_internal_func(int idx, int argcount)
1228{
1229 int res;
1230 char *name;
1231
1232 if (argcount < global_functions[idx].f_min_argc)
1233 res = FCERR_TOOFEW;
1234 else if (argcount > global_functions[idx].f_max_argc)
1235 res = FCERR_TOOMANY;
1236 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001237 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238
1239 name = internal_func_name(idx);
1240 if (res == FCERR_TOOMANY)
1241 semsg(_(e_toomanyarg), name);
1242 else
1243 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001244 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245}
1246
Bram Moolenaarac92e252019-08-03 21:58:38 +02001247 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001248call_internal_func(
1249 char_u *name,
1250 int argcount,
1251 typval_T *argvars,
1252 typval_T *rettv)
1253{
1254 int i;
1255
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001256 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001258 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001259 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001260 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001261 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001262 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001263 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001264 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001265 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001266}
1267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 void
1269call_internal_func_by_idx(
1270 int idx,
1271 typval_T *argvars,
1272 typval_T *rettv)
1273{
1274 global_functions[idx].f_func(argvars, rettv);
1275}
1276
Bram Moolenaarac92e252019-08-03 21:58:38 +02001277/*
1278 * Invoke a method for base->method().
1279 */
1280 int
1281call_internal_method(
1282 char_u *name,
1283 int argcount,
1284 typval_T *argvars,
1285 typval_T *rettv,
1286 typval_T *basetv)
1287{
1288 int i;
1289 int fi;
1290 typval_T argv[MAX_FUNC_ARGS + 1];
1291
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001292 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001293 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001294 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001295 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001296 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001297 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001298 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001299 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001300 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001301
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001302 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001303 {
1304 // base value goes last
1305 for (i = 0; i < argcount; ++i)
1306 argv[i] = argvars[i];
1307 argv[argcount] = *basetv;
1308 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001309 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001310 {
1311 // base value goes second
1312 argv[0] = argvars[0];
1313 argv[1] = *basetv;
1314 for (i = 1; i < argcount; ++i)
1315 argv[i + 1] = argvars[i];
1316 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001317 else if (global_functions[fi].f_argtype == FEARG_3)
1318 {
1319 // base value goes third
1320 argv[0] = argvars[0];
1321 argv[1] = argvars[1];
1322 argv[2] = *basetv;
1323 for (i = 2; i < argcount; ++i)
1324 argv[i + 1] = argvars[i];
1325 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001326 else if (global_functions[fi].f_argtype == FEARG_4)
1327 {
1328 // base value goes fourth
1329 argv[0] = argvars[0];
1330 argv[1] = argvars[1];
1331 argv[2] = argvars[2];
1332 argv[3] = *basetv;
1333 for (i = 3; i < argcount; ++i)
1334 argv[i + 1] = argvars[i];
1335 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001336 else
1337 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001338 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001339 argv[0] = *basetv;
1340 for (i = 0; i < argcount; ++i)
1341 argv[i + 1] = argvars[i];
1342 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001343 argv[argcount + 1].v_type = VAR_UNKNOWN;
1344
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001345 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001346 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347}
1348
1349/*
1350 * Return TRUE for a non-zero Number and a non-empty String.
1351 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001352 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353non_zero_arg(typval_T *argvars)
1354{
1355 return ((argvars[0].v_type == VAR_NUMBER
1356 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001357 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358 && argvars[0].vval.v_number == VVAL_TRUE)
1359 || (argvars[0].v_type == VAR_STRING
1360 && argvars[0].vval.v_string != NULL
1361 && *argvars[0].vval.v_string != NUL));
1362}
1363
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365/*
1366 * Get the float value of "argvars[0]" into "f".
1367 * Returns FAIL when the argument is not a Number or Float.
1368 */
1369 static int
1370get_float_arg(typval_T *argvars, float_T *f)
1371{
1372 if (argvars[0].v_type == VAR_FLOAT)
1373 {
1374 *f = argvars[0].vval.v_float;
1375 return OK;
1376 }
1377 if (argvars[0].v_type == VAR_NUMBER)
1378 {
1379 *f = (float_T)argvars[0].vval.v_number;
1380 return OK;
1381 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001382 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 return FAIL;
1384}
1385
1386/*
1387 * "abs(expr)" function
1388 */
1389 static void
1390f_abs(typval_T *argvars, typval_T *rettv)
1391{
1392 if (argvars[0].v_type == VAR_FLOAT)
1393 {
1394 rettv->v_type = VAR_FLOAT;
1395 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1396 }
1397 else
1398 {
1399 varnumber_T n;
1400 int error = FALSE;
1401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 if (error)
1404 rettv->vval.v_number = -1;
1405 else if (n > 0)
1406 rettv->vval.v_number = n;
1407 else
1408 rettv->vval.v_number = -n;
1409 }
1410}
1411
1412/*
1413 * "acos()" function
1414 */
1415 static void
1416f_acos(typval_T *argvars, typval_T *rettv)
1417{
1418 float_T f = 0.0;
1419
1420 rettv->v_type = VAR_FLOAT;
1421 if (get_float_arg(argvars, &f) == OK)
1422 rettv->vval.v_float = acos(f);
1423 else
1424 rettv->vval.v_float = 0.0;
1425}
1426#endif
1427
1428/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001429 * "and(expr, expr)" function
1430 */
1431 static void
1432f_and(typval_T *argvars, typval_T *rettv)
1433{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001434 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1435 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001436}
1437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438#ifdef FEAT_FLOAT
1439/*
1440 * "asin()" function
1441 */
1442 static void
1443f_asin(typval_T *argvars, typval_T *rettv)
1444{
1445 float_T f = 0.0;
1446
1447 rettv->v_type = VAR_FLOAT;
1448 if (get_float_arg(argvars, &f) == OK)
1449 rettv->vval.v_float = asin(f);
1450 else
1451 rettv->vval.v_float = 0.0;
1452}
1453
1454/*
1455 * "atan()" function
1456 */
1457 static void
1458f_atan(typval_T *argvars, typval_T *rettv)
1459{
1460 float_T f = 0.0;
1461
1462 rettv->v_type = VAR_FLOAT;
1463 if (get_float_arg(argvars, &f) == OK)
1464 rettv->vval.v_float = atan(f);
1465 else
1466 rettv->vval.v_float = 0.0;
1467}
1468
1469/*
1470 * "atan2()" function
1471 */
1472 static void
1473f_atan2(typval_T *argvars, typval_T *rettv)
1474{
1475 float_T fx = 0.0, fy = 0.0;
1476
1477 rettv->v_type = VAR_FLOAT;
1478 if (get_float_arg(argvars, &fx) == OK
1479 && get_float_arg(&argvars[1], &fy) == OK)
1480 rettv->vval.v_float = atan2(fx, fy);
1481 else
1482 rettv->vval.v_float = 0.0;
1483}
1484#endif
1485
1486/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001487 * "balloon_show()" function
1488 */
1489#ifdef FEAT_BEVAL
1490 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001491f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1492{
1493 rettv->v_type = VAR_STRING;
1494 if (balloonEval != NULL)
1495 {
1496 if (balloonEval->msg == NULL)
1497 rettv->vval.v_string = NULL;
1498 else
1499 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1500 }
1501}
1502
1503 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001504f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1505{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001506 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001507 {
1508 if (argvars[0].v_type == VAR_LIST
1509# ifdef FEAT_GUI
1510 && !gui.in_use
1511# endif
1512 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001513 {
1514 list_T *l = argvars[0].vval.v_list;
1515
1516 // empty list removes the balloon
1517 post_balloon(balloonEval, NULL,
1518 l == NULL || l->lv_len == 0 ? NULL : l);
1519 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001520 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001521 {
1522 char_u *mesg = tv_get_string_chk(&argvars[0]);
1523
1524 if (mesg != NULL)
1525 // empty string removes the balloon
1526 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1527 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001528 }
1529}
1530
Bram Moolenaar669a8282017-11-19 20:13:05 +01001531# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001532 static void
1533f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1534{
1535 if (rettv_list_alloc(rettv) == OK)
1536 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001537 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001538
1539 if (msg != NULL)
1540 {
1541 pumitem_T *array;
1542 int size = split_message(msg, &array);
1543 int i;
1544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001545 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001546 for (i = 1; i < size - 1; ++i)
1547 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001548 while (size > 0)
1549 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001550 vim_free(array);
1551 }
1552 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001553}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001554# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001555#endif
1556
1557/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001558 * Get the buffer from "arg" and give an error and return NULL if it is not
1559 * valid.
1560 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001561 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001562get_buf_arg(typval_T *arg)
1563{
1564 buf_T *buf;
1565
1566 ++emsg_off;
1567 buf = tv_get_buf(arg, FALSE);
1568 --emsg_off;
1569 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001570 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001571 return buf;
1572}
1573
1574/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 * "byte2line(byte)" function
1576 */
1577 static void
1578f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1579{
1580#ifndef FEAT_BYTEOFF
1581 rettv->vval.v_number = -1;
1582#else
1583 long boff = 0;
1584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001585 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001586 if (boff < 0)
1587 rettv->vval.v_number = -1;
1588 else
1589 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1590 (linenr_T)0, &boff);
1591#endif
1592}
1593
1594 static void
1595byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1596{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598 char_u *str;
1599 varnumber_T idx;
1600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 str = tv_get_string_chk(&argvars[0]);
1602 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 rettv->vval.v_number = -1;
1604 if (str == NULL || idx < 0)
1605 return;
1606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 t = str;
1608 for ( ; idx > 0; idx--)
1609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001610 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001611 return;
1612 if (enc_utf8 && comp)
1613 t += utf_ptr2len(t);
1614 else
1615 t += (*mb_ptr2len)(t);
1616 }
1617 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001618}
1619
1620/*
1621 * "byteidx()" function
1622 */
1623 static void
1624f_byteidx(typval_T *argvars, typval_T *rettv)
1625{
1626 byteidx(argvars, rettv, FALSE);
1627}
1628
1629/*
1630 * "byteidxcomp()" function
1631 */
1632 static void
1633f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1634{
1635 byteidx(argvars, rettv, TRUE);
1636}
1637
1638/*
1639 * "call(func, arglist [, dict])" function
1640 */
1641 static void
1642f_call(typval_T *argvars, typval_T *rettv)
1643{
1644 char_u *func;
1645 partial_T *partial = NULL;
1646 dict_T *selfdict = NULL;
1647
1648 if (argvars[1].v_type != VAR_LIST)
1649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001650 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651 return;
1652 }
1653 if (argvars[1].vval.v_list == NULL)
1654 return;
1655
1656 if (argvars[0].v_type == VAR_FUNC)
1657 func = argvars[0].vval.v_string;
1658 else if (argvars[0].v_type == VAR_PARTIAL)
1659 {
1660 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662 }
1663 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001664 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667
1668 if (argvars[2].v_type != VAR_UNKNOWN)
1669 {
1670 if (argvars[2].v_type != VAR_DICT)
1671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001672 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 return;
1674 }
1675 selfdict = argvars[2].vval.v_dict;
1676 }
1677
1678 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1679}
1680
1681#ifdef FEAT_FLOAT
1682/*
1683 * "ceil({float})" function
1684 */
1685 static void
1686f_ceil(typval_T *argvars, typval_T *rettv)
1687{
1688 float_T f = 0.0;
1689
1690 rettv->v_type = VAR_FLOAT;
1691 if (get_float_arg(argvars, &f) == OK)
1692 rettv->vval.v_float = ceil(f);
1693 else
1694 rettv->vval.v_float = 0.0;
1695}
1696#endif
1697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698/*
1699 * "changenr()" function
1700 */
1701 static void
1702f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1703{
1704 rettv->vval.v_number = curbuf->b_u_seq_cur;
1705}
1706
1707/*
1708 * "char2nr(string)" function
1709 */
1710 static void
1711f_char2nr(typval_T *argvars, typval_T *rettv)
1712{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713 if (has_mbyte)
1714 {
1715 int utf8 = 0;
1716
1717 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02001718 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719
1720 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001721 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001722 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001723 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 }
1725 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001726 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727}
1728
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001729 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001730get_optional_window(typval_T *argvars, int idx)
1731{
1732 win_T *win = curwin;
1733
1734 if (argvars[idx].v_type != VAR_UNKNOWN)
1735 {
1736 win = find_win_by_nr_or_id(&argvars[idx]);
1737 if (win == NULL)
1738 {
1739 emsg(_(e_invalwindow));
1740 return NULL;
1741 }
1742 }
1743 return win;
1744}
1745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001747 * "col(string)" function
1748 */
1749 static void
1750f_col(typval_T *argvars, typval_T *rettv)
1751{
1752 colnr_T col = 0;
1753 pos_T *fp;
1754 int fnum = curbuf->b_fnum;
1755
1756 fp = var2fpos(&argvars[0], FALSE, &fnum);
1757 if (fp != NULL && fnum == curbuf->b_fnum)
1758 {
1759 if (fp->col == MAXCOL)
1760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001761 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1763 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1764 else
1765 col = MAXCOL;
1766 }
1767 else
1768 {
1769 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001770 // col(".") when the cursor is on the NUL at the end of the line
1771 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001772 if (virtual_active() && fp == &curwin->w_cursor)
1773 {
1774 char_u *p = ml_get_cursor();
1775
1776 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1777 curwin->w_virtcol - curwin->w_cursor.coladd))
1778 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 int l;
1780
1781 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1782 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001783 }
1784 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001785 }
1786 }
1787 rettv->vval.v_number = col;
1788}
1789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001790/*
1791 * "confirm(message, buttons[, default [, type]])" function
1792 */
1793 static void
1794f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1795{
1796#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1797 char_u *message;
1798 char_u *buttons = NULL;
1799 char_u buf[NUMBUFLEN];
1800 char_u buf2[NUMBUFLEN];
1801 int def = 1;
1802 int type = VIM_GENERIC;
1803 char_u *typestr;
1804 int error = FALSE;
1805
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 if (message == NULL)
1808 error = TRUE;
1809 if (argvars[1].v_type != VAR_UNKNOWN)
1810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001811 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001812 if (buttons == NULL)
1813 error = TRUE;
1814 if (argvars[2].v_type != VAR_UNKNOWN)
1815 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001816 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001817 if (argvars[3].v_type != VAR_UNKNOWN)
1818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001819 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001820 if (typestr == NULL)
1821 error = TRUE;
1822 else
1823 {
1824 switch (TOUPPER_ASC(*typestr))
1825 {
1826 case 'E': type = VIM_ERROR; break;
1827 case 'Q': type = VIM_QUESTION; break;
1828 case 'I': type = VIM_INFO; break;
1829 case 'W': type = VIM_WARNING; break;
1830 case 'G': type = VIM_GENERIC; break;
1831 }
1832 }
1833 }
1834 }
1835 }
1836
1837 if (buttons == NULL || *buttons == NUL)
1838 buttons = (char_u *)_("&Ok");
1839
1840 if (!error)
1841 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1842 def, NULL, FALSE);
1843#endif
1844}
1845
1846/*
1847 * "copy()" function
1848 */
1849 static void
1850f_copy(typval_T *argvars, typval_T *rettv)
1851{
1852 item_copy(&argvars[0], rettv, FALSE, 0);
1853}
1854
1855#ifdef FEAT_FLOAT
1856/*
1857 * "cos()" function
1858 */
1859 static void
1860f_cos(typval_T *argvars, typval_T *rettv)
1861{
1862 float_T f = 0.0;
1863
1864 rettv->v_type = VAR_FLOAT;
1865 if (get_float_arg(argvars, &f) == OK)
1866 rettv->vval.v_float = cos(f);
1867 else
1868 rettv->vval.v_float = 0.0;
1869}
1870
1871/*
1872 * "cosh()" function
1873 */
1874 static void
1875f_cosh(typval_T *argvars, typval_T *rettv)
1876{
1877 float_T f = 0.0;
1878
1879 rettv->v_type = VAR_FLOAT;
1880 if (get_float_arg(argvars, &f) == OK)
1881 rettv->vval.v_float = cosh(f);
1882 else
1883 rettv->vval.v_float = 0.0;
1884}
1885#endif
1886
1887/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 * "cursor(lnum, col)" function, or
1889 * "cursor(list)"
1890 *
1891 * Moves the cursor to the specified line and column.
1892 * Returns 0 when the position could be set, -1 otherwise.
1893 */
1894 static void
1895f_cursor(typval_T *argvars, typval_T *rettv)
1896{
1897 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899 int set_curswant = TRUE;
1900
1901 rettv->vval.v_number = -1;
1902 if (argvars[1].v_type == VAR_UNKNOWN)
1903 {
1904 pos_T pos;
1905 colnr_T curswant = -1;
1906
1907 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001909 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910 return;
1911 }
1912 line = pos.lnum;
1913 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001914 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 if (curswant >= 0)
1916 {
1917 curwin->w_curswant = curswant - 1;
1918 set_curswant = FALSE;
1919 }
1920 }
1921 else
1922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001923 line = tv_get_lnum(argvars);
1924 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001926 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001928 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930 if (line > 0)
1931 curwin->w_cursor.lnum = line;
1932 if (col > 0)
1933 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 if (has_mbyte)
1940 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941
1942 curwin->w_set_curswant = set_curswant;
1943 rettv->vval.v_number = 0;
1944}
1945
Bram Moolenaar4f974752019-02-17 17:44:42 +01001946#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001947/*
1948 * "debugbreak()" function
1949 */
1950 static void
1951f_debugbreak(typval_T *argvars, typval_T *rettv)
1952{
1953 int pid;
1954
1955 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001956 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001957 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001959 else
1960 {
1961 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1962
1963 if (hProcess != NULL)
1964 {
1965 DebugBreakProcess(hProcess);
1966 CloseHandle(hProcess);
1967 rettv->vval.v_number = OK;
1968 }
1969 }
1970}
1971#endif
1972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001973/*
1974 * "deepcopy()" function
1975 */
1976 static void
1977f_deepcopy(typval_T *argvars, typval_T *rettv)
1978{
1979 int noref = 0;
1980 int copyID;
1981
1982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar44b4a242020-09-05 17:18:28 +02001983 noref = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001984 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 else
1987 {
1988 copyID = get_copyID();
1989 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1990 }
1991}
1992
1993/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 * "did_filetype()" function
1995 */
1996 static void
1997f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1998{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000}
2001
2002/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002003 * "echoraw({expr})" function
2004 */
2005 static void
2006f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2007{
2008 char_u *str = tv_get_string_chk(&argvars[0]);
2009
2010 if (str != NULL && *str != NUL)
2011 {
2012 out_str(str);
2013 out_flush();
2014 }
2015}
2016
2017/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 * "empty({expr})" function
2019 */
2020 static void
2021f_empty(typval_T *argvars, typval_T *rettv)
2022{
2023 int n = FALSE;
2024
2025 switch (argvars[0].v_type)
2026 {
2027 case VAR_STRING:
2028 case VAR_FUNC:
2029 n = argvars[0].vval.v_string == NULL
2030 || *argvars[0].vval.v_string == NUL;
2031 break;
2032 case VAR_PARTIAL:
2033 n = FALSE;
2034 break;
2035 case VAR_NUMBER:
2036 n = argvars[0].vval.v_number == 0;
2037 break;
2038 case VAR_FLOAT:
2039#ifdef FEAT_FLOAT
2040 n = argvars[0].vval.v_float == 0.0;
2041 break;
2042#endif
2043 case VAR_LIST:
2044 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002045 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 break;
2047 case VAR_DICT:
2048 n = argvars[0].vval.v_dict == NULL
2049 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2050 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002051 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 case VAR_SPECIAL:
2053 n = argvars[0].vval.v_number != VVAL_TRUE;
2054 break;
2055
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002056 case VAR_BLOB:
2057 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002058 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2059 break;
2060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 case VAR_JOB:
2062#ifdef FEAT_JOB_CHANNEL
2063 n = argvars[0].vval.v_job == NULL
2064 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2065 break;
2066#endif
2067 case VAR_CHANNEL:
2068#ifdef FEAT_JOB_CHANNEL
2069 n = argvars[0].vval.v_channel == NULL
2070 || !channel_is_open(argvars[0].vval.v_channel);
2071 break;
2072#endif
2073 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002074 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002076 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002077 n = TRUE;
2078 break;
2079 }
2080
2081 rettv->vval.v_number = n;
2082}
2083
2084/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002085 * "environ()" function
2086 */
2087 static void
2088f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2089{
2090#if !defined(AMIGA)
2091 int i = 0;
2092 char_u *entry, *value;
2093# ifdef MSWIN
2094 extern wchar_t **_wenviron;
2095# else
2096 extern char **environ;
2097# endif
2098
2099 if (rettv_dict_alloc(rettv) != OK)
2100 return;
2101
2102# ifdef MSWIN
2103 if (*_wenviron == NULL)
2104 return;
2105# else
2106 if (*environ == NULL)
2107 return;
2108# endif
2109
2110 for (i = 0; ; ++i)
2111 {
2112# ifdef MSWIN
2113 short_u *p;
2114
2115 if ((p = (short_u *)_wenviron[i]) == NULL)
2116 return;
2117 entry = utf16_to_enc(p, NULL);
2118# else
2119 if ((entry = (char_u *)environ[i]) == NULL)
2120 return;
2121 entry = vim_strsave(entry);
2122# endif
2123 if (entry == NULL) // out of memory
2124 return;
2125 if ((value = vim_strchr(entry, '=')) == NULL)
2126 {
2127 vim_free(entry);
2128 continue;
2129 }
2130 *value++ = NUL;
2131 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2132 vim_free(entry);
2133 }
2134#endif
2135}
2136
2137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 * "escape({string}, {chars})" function
2139 */
2140 static void
2141f_escape(typval_T *argvars, typval_T *rettv)
2142{
2143 char_u buf[NUMBUFLEN];
2144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002145 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2146 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002147 rettv->v_type = VAR_STRING;
2148}
2149
2150/*
2151 * "eval()" function
2152 */
2153 static void
2154f_eval(typval_T *argvars, typval_T *rettv)
2155{
2156 char_u *s, *p;
2157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 if (s != NULL)
2160 s = skipwhite(s);
2161
2162 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002163 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 {
2165 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002166 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 need_clr_eos = FALSE;
2168 rettv->v_type = VAR_NUMBER;
2169 rettv->vval.v_number = 0;
2170 }
2171 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002172 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173}
2174
2175/*
2176 * "eventhandler()" function
2177 */
2178 static void
2179f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2180{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002181 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182}
2183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184static garray_T redir_execute_ga;
2185
2186/*
2187 * Append "value[value_len]" to the execute() output.
2188 */
2189 void
2190execute_redir_str(char_u *value, int value_len)
2191{
2192 int len;
2193
2194 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002195 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002196 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002197 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002198 if (ga_grow(&redir_execute_ga, len) == OK)
2199 {
2200 mch_memmove((char *)redir_execute_ga.ga_data
2201 + redir_execute_ga.ga_len, value, len);
2202 redir_execute_ga.ga_len += len;
2203 }
2204}
2205
2206/*
2207 * Get next line from a list.
2208 * Called by do_cmdline() to get the next line.
2209 * Returns allocated string, or NULL for end of function.
2210 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 static char_u *
2212get_list_line(
2213 int c UNUSED,
2214 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002215 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002216 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217{
2218 listitem_T **p = (listitem_T **)cookie;
2219 listitem_T *item = *p;
2220 char_u buf[NUMBUFLEN];
2221 char_u *s;
2222
2223 if (item == NULL)
2224 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002225 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 *p = item->li_next;
2227 return s == NULL ? NULL : vim_strsave(s);
2228}
2229
2230/*
2231 * "execute()" function
2232 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002233 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002234execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235{
2236 char_u *cmd = NULL;
2237 list_T *list = NULL;
2238 int save_msg_silent = msg_silent;
2239 int save_emsg_silent = emsg_silent;
2240 int save_emsg_noredir = emsg_noredir;
2241 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002242 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002244 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002245 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246
2247 rettv->vval.v_string = NULL;
2248 rettv->v_type = VAR_STRING;
2249
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002250 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002251 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002252 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002253 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002254 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 return;
2256 ++list->lv_refcount;
2257 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002258 else if (argvars[arg_off].v_type == VAR_JOB
2259 || argvars[arg_off].v_type == VAR_CHANNEL)
2260 {
2261 emsg(_(e_inval_string));
2262 return;
2263 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 else
2265 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002266 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267 if (cmd == NULL)
2268 return;
2269 }
2270
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002271 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 {
2273 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002274 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275
2276 if (s == NULL)
2277 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002278 if (*s == NUL)
2279 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 if (STRNCMP(s, "silent", 6) == 0)
2281 ++msg_silent;
2282 if (STRCMP(s, "silent!") == 0)
2283 {
2284 emsg_silent = TRUE;
2285 emsg_noredir = TRUE;
2286 }
2287 }
2288 else
2289 ++msg_silent;
2290
2291 if (redir_execute)
2292 save_ga = redir_execute_ga;
2293 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2294 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002295 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002296 if (!echo_output)
2297 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298
2299 if (cmd != NULL)
2300 do_cmdline_cmd(cmd);
2301 else
2302 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002303 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002305 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002306 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 do_cmdline(NULL, get_list_line, (void *)&item,
2308 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2309 --list->lv_refcount;
2310 }
2311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002313 if (ga_grow(&redir_execute_ga, 1) == OK)
2314 {
2315 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2316 rettv->vval.v_string = redir_execute_ga.ga_data;
2317 }
2318 else
2319 {
2320 ga_clear(&redir_execute_ga);
2321 rettv->vval.v_string = NULL;
2322 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323 msg_silent = save_msg_silent;
2324 emsg_silent = save_emsg_silent;
2325 emsg_noredir = save_emsg_noredir;
2326
2327 redir_execute = save_redir_execute;
2328 if (redir_execute)
2329 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002330 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002332 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002333 if (echo_output)
2334 // When not working silently: put it in column zero. A following
2335 // "echon" will overwrite the message, unavoidably.
2336 msg_col = 0;
2337 else
2338 // When working silently: Put it back where it was, since nothing
2339 // should have been written.
2340 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341}
2342
2343/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002344 * "execute()" function
2345 */
2346 static void
2347f_execute(typval_T *argvars, typval_T *rettv)
2348{
2349 execute_common(argvars, rettv, 0);
2350}
2351
2352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 * "exists()" function
2354 */
2355 static void
2356f_exists(typval_T *argvars, typval_T *rettv)
2357{
2358 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002361 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365 if (mch_getenv(p + 1) != NULL)
2366 n = TRUE;
2367 else
2368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002369 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 p = expand_env_save(p);
2371 if (p != NULL && *p != '$')
2372 n = TRUE;
2373 vim_free(p);
2374 }
2375 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002378 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002380 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002382 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002384 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002386 else if (*p == '?') // internal function only
2387 {
2388 n = has_internal_func_name(p + 1);
2389 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 else if (*p == ':')
2391 {
2392 n = cmd_exists(p + 1);
2393 }
2394 else if (*p == '#')
2395 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 if (p[1] == '#')
2397 n = autocmd_supported(p + 2);
2398 else
2399 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002401 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002403 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 }
2405
2406 rettv->vval.v_number = n;
2407}
2408
2409#ifdef FEAT_FLOAT
2410/*
2411 * "exp()" function
2412 */
2413 static void
2414f_exp(typval_T *argvars, typval_T *rettv)
2415{
2416 float_T f = 0.0;
2417
2418 rettv->v_type = VAR_FLOAT;
2419 if (get_float_arg(argvars, &f) == OK)
2420 rettv->vval.v_float = exp(f);
2421 else
2422 rettv->vval.v_float = 0.0;
2423}
2424#endif
2425
2426/*
2427 * "expand()" function
2428 */
2429 static void
2430f_expand(typval_T *argvars, typval_T *rettv)
2431{
2432 char_u *s;
2433 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002434 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2436 expand_T xpc;
2437 int error = FALSE;
2438 char_u *result;
2439
2440 rettv->v_type = VAR_STRING;
2441 if (argvars[1].v_type != VAR_UNKNOWN
2442 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002443 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002444 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002445 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002447 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (*s == '%' || *s == '#' || *s == '<')
2449 {
2450 ++emsg_off;
2451 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2452 --emsg_off;
2453 if (rettv->v_type == VAR_LIST)
2454 {
2455 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2456 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002457 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 }
2459 else
2460 rettv->vval.v_string = result;
2461 }
2462 else
2463 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002464 // When the optional second argument is non-zero, don't remove matches
2465 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02002467 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468 options |= WILD_KEEP_ALL;
2469 if (!error)
2470 {
2471 ExpandInit(&xpc);
2472 xpc.xp_context = EXPAND_FILES;
2473 if (p_wic)
2474 options += WILD_ICASE;
2475 if (rettv->v_type == VAR_STRING)
2476 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2477 options, WILD_ALL);
2478 else if (rettv_list_alloc(rettv) != FAIL)
2479 {
2480 int i;
2481
2482 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2483 for (i = 0; i < xpc.xp_numfiles; i++)
2484 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2485 ExpandCleanup(&xpc);
2486 }
2487 }
2488 else
2489 rettv->vval.v_string = NULL;
2490 }
2491}
2492
2493/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002494 * "expandcmd()" function
2495 * Expand all the special characters in a command string.
2496 */
2497 static void
2498f_expandcmd(typval_T *argvars, typval_T *rettv)
2499{
2500 exarg_T eap;
2501 char_u *cmdstr;
2502 char *errormsg = NULL;
2503
2504 rettv->v_type = VAR_STRING;
2505 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2506
2507 memset(&eap, 0, sizeof(eap));
2508 eap.cmd = cmdstr;
2509 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002510 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002511 eap.usefilter = FALSE;
2512 eap.nextcmd = NULL;
2513 eap.cmdidx = CMD_USER;
2514
2515 expand_filename(&eap, &cmdstr, &errormsg);
2516 if (errormsg != NULL && *errormsg != NUL)
2517 emsg(errormsg);
2518
2519 rettv->vval.v_string = cmdstr;
2520}
2521
2522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 * "feedkeys()" function
2524 */
2525 static void
2526f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2527{
2528 int remap = TRUE;
2529 int insert = FALSE;
2530 char_u *keys, *flags;
2531 char_u nbuf[NUMBUFLEN];
2532 int typed = FALSE;
2533 int execute = FALSE;
2534 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002535 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 char_u *keys_esc;
2537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002538 // This is not allowed in the sandbox. If the commands would still be
2539 // executed in the sandbox it would be OK, but it probably happens later,
2540 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (check_secure())
2542 return;
2543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002544 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545
2546 if (argvars[1].v_type != VAR_UNKNOWN)
2547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002548 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 for ( ; *flags != NUL; ++flags)
2550 {
2551 switch (*flags)
2552 {
2553 case 'n': remap = FALSE; break;
2554 case 'm': remap = TRUE; break;
2555 case 't': typed = TRUE; break;
2556 case 'i': insert = TRUE; break;
2557 case 'x': execute = TRUE; break;
2558 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002559 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002560 }
2561 }
2562 }
2563
2564 if (*keys != NUL || execute)
2565 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002566 // Need to escape K_SPECIAL and CSI before putting the string in the
2567 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002568 keys_esc = vim_strsave_escape_csi(keys);
2569 if (keys_esc != NULL)
2570 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002571 if (lowlevel)
2572 {
2573#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002574 int idx;
2575 int len = (int)STRLEN(keys);
2576
2577 for (idx = 0; idx < len; ++idx)
2578 {
2579 // if a CTRL-C was typed, set got_int, similar to what
2580 // happens in fill_input_buf()
2581 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2582 got_int = TRUE;
2583 add_to_input_buf(keys + idx, 1);
2584 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002585#else
2586 emsg(_("E980: lowlevel input not supported"));
2587#endif
2588 }
2589 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002590 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002591 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002593 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002594#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002595 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002596#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002597 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002598 typebuf_was_filled = TRUE;
2599 }
2600 vim_free(keys_esc);
2601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 if (execute)
2603 {
2604 int save_msg_scroll = msg_scroll;
2605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002606 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 msg_scroll = FALSE;
2608
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002609 if (!dangerous)
2610 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002611 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002612 if (!dangerous)
2613 --ex_normal_busy;
2614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002615 msg_scroll |= save_msg_scroll;
2616 }
2617 }
2618 }
2619}
2620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621#ifdef FEAT_FLOAT
2622/*
2623 * "float2nr({float})" function
2624 */
2625 static void
2626f_float2nr(typval_T *argvars, typval_T *rettv)
2627{
2628 float_T f = 0.0;
2629
2630 if (get_float_arg(argvars, &f) == OK)
2631 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002632 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002633 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002634 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002635 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 else
2637 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 }
2639}
2640
2641/*
2642 * "floor({float})" function
2643 */
2644 static void
2645f_floor(typval_T *argvars, typval_T *rettv)
2646{
2647 float_T f = 0.0;
2648
2649 rettv->v_type = VAR_FLOAT;
2650 if (get_float_arg(argvars, &f) == OK)
2651 rettv->vval.v_float = floor(f);
2652 else
2653 rettv->vval.v_float = 0.0;
2654}
2655
2656/*
2657 * "fmod()" function
2658 */
2659 static void
2660f_fmod(typval_T *argvars, typval_T *rettv)
2661{
2662 float_T fx = 0.0, fy = 0.0;
2663
2664 rettv->v_type = VAR_FLOAT;
2665 if (get_float_arg(argvars, &fx) == OK
2666 && get_float_arg(&argvars[1], &fy) == OK)
2667 rettv->vval.v_float = fmod(fx, fy);
2668 else
2669 rettv->vval.v_float = 0.0;
2670}
2671#endif
2672
2673/*
2674 * "fnameescape({string})" function
2675 */
2676 static void
2677f_fnameescape(typval_T *argvars, typval_T *rettv)
2678{
2679 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002680 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 rettv->v_type = VAR_STRING;
2682}
2683
2684/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 * "foreground()" function
2686 */
2687 static void
2688f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2689{
2690#ifdef FEAT_GUI
2691 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002692 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002694 return;
2695 }
2696#endif
2697#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699#endif
2700}
2701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002703common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002704{
2705 char_u *s;
2706 char_u *name;
2707 int use_string = FALSE;
2708 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002709 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002710 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711
2712 if (argvars[0].v_type == VAR_FUNC)
2713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002714 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 s = argvars[0].vval.v_string;
2716 }
2717 else if (argvars[0].v_type == VAR_PARTIAL
2718 && argvars[0].vval.v_partial != NULL)
2719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002720 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002722 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 }
2724 else
2725 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002726 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002727 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 use_string = TRUE;
2729 }
2730
Bram Moolenaar843b8842016-08-21 14:36:15 +02002731 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002732 {
2733 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002734 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002735 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2736 if (*name != NUL)
2737 s = NULL;
2738 }
2739
Bram Moolenaar843b8842016-08-21 14:36:15 +02002740 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2741 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002742 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002743 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002744 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002745 ? find_func(trans_name, is_global, NULL) == NULL
2746 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002747 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002748 else
2749 {
2750 int dict_idx = 0;
2751 int arg_idx = 0;
2752 list_T *list = NULL;
2753
2754 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2755 {
2756 char sid_buf[25];
2757 int off = *s == 's' ? 2 : 5;
2758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // Expand s: and <SID> into <SNR>nr_, so that the function can
2760 // also be called from another script. Using trans_function_name()
2761 // would also work, but some plugins depend on the name being
2762 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002763 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002764 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 if (name != NULL)
2766 {
2767 STRCPY(name, sid_buf);
2768 STRCAT(name, s + off);
2769 }
2770 }
2771 else
2772 name = vim_strsave(s);
2773
2774 if (argvars[1].v_type != VAR_UNKNOWN)
2775 {
2776 if (argvars[2].v_type != VAR_UNKNOWN)
2777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002778 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 arg_idx = 1;
2780 dict_idx = 2;
2781 }
2782 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002783 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 dict_idx = 1;
2785 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002786 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 arg_idx = 1;
2788 if (dict_idx > 0)
2789 {
2790 if (argvars[dict_idx].v_type != VAR_DICT)
2791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002792 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002793 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002794 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 }
2796 if (argvars[dict_idx].vval.v_dict == NULL)
2797 dict_idx = 0;
2798 }
2799 if (arg_idx > 0)
2800 {
2801 if (argvars[arg_idx].v_type != VAR_LIST)
2802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002803 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002804 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002805 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806 }
2807 list = argvars[arg_idx].vval.v_list;
2808 if (list == NULL || list->lv_len == 0)
2809 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002810 else if (list->lv_len > MAX_FUNC_ARGS)
2811 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002812 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002813 vim_free(name);
2814 goto theend;
2815 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 }
2817 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002818 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002819 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002820 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002822 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002823 if (pt == NULL)
2824 vim_free(name);
2825 else
2826 {
2827 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2828 {
2829 listitem_T *li;
2830 int i = 0;
2831 int arg_len = 0;
2832 int lv_len = 0;
2833
2834 if (arg_pt != NULL)
2835 arg_len = arg_pt->pt_argc;
2836 if (list != NULL)
2837 lv_len = list->lv_len;
2838 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002839 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 if (pt->pt_argv == NULL)
2841 {
2842 vim_free(pt);
2843 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002844 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002845 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002846 for (i = 0; i < arg_len; i++)
2847 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2848 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002849 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002850 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002851 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002852 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002853 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854 }
2855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 // For "function(dict.func, [], dict)" and "func" is a partial
2857 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 if (dict_idx > 0)
2859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002860 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002861 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2862 ++pt->pt_dict->dv_refcount;
2863 }
2864 else if (arg_pt != NULL)
2865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002866 // If the dict was bound automatically the result is also
2867 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 pt->pt_dict = arg_pt->pt_dict;
2869 pt->pt_auto = arg_pt->pt_auto;
2870 if (pt->pt_dict != NULL)
2871 ++pt->pt_dict->dv_refcount;
2872 }
2873
2874 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2876 {
2877 pt->pt_func = arg_pt->pt_func;
2878 func_ptr_ref(pt->pt_func);
2879 vim_free(name);
2880 }
2881 else if (is_funcref)
2882 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002883 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002884 func_ptr_ref(pt->pt_func);
2885 vim_free(name);
2886 }
2887 else
2888 {
2889 pt->pt_name = name;
2890 func_ref(name);
2891 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 }
2893 rettv->v_type = VAR_PARTIAL;
2894 rettv->vval.v_partial = pt;
2895 }
2896 else
2897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002898 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 rettv->v_type = VAR_FUNC;
2900 rettv->vval.v_string = name;
2901 func_ref(name);
2902 }
2903 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002904theend:
2905 vim_free(trans_name);
2906}
2907
2908/*
2909 * "funcref()" function
2910 */
2911 static void
2912f_funcref(typval_T *argvars, typval_T *rettv)
2913{
2914 common_function(argvars, rettv, TRUE);
2915}
2916
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002917 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002918ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002919{
2920 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2921 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02002922 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002923}
2924
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002925/*
2926 * "function()" function
2927 */
2928 static void
2929f_function(typval_T *argvars, typval_T *rettv)
2930{
2931 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932}
2933
2934/*
2935 * "garbagecollect()" function
2936 */
2937 static void
2938f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2939{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002940 // This is postponed until we are back at the toplevel, because we may be
2941 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 want_garbage_collect = TRUE;
2943
Bram Moolenaar2df47312020-09-05 17:30:44 +02002944 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 garbage_collect_at_exit = TRUE;
2946}
2947
2948/*
2949 * "get()" function
2950 */
2951 static void
2952f_get(typval_T *argvars, typval_T *rettv)
2953{
2954 listitem_T *li;
2955 list_T *l;
2956 dictitem_T *di;
2957 dict_T *d;
2958 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002959 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002961 if (argvars[0].v_type == VAR_BLOB)
2962 {
2963 int error = FALSE;
2964 int idx = tv_get_number_chk(&argvars[1], &error);
2965
2966 if (!error)
2967 {
2968 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002969 if (idx < 0)
2970 idx = blob_len(argvars[0].vval.v_blob) + idx;
2971 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2972 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002973 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002974 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002975 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002976 tv = rettv;
2977 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002978 }
2979 }
2980 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 {
2982 if ((l = argvars[0].vval.v_list) != NULL)
2983 {
2984 int error = FALSE;
2985
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002986 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 if (!error && li != NULL)
2988 tv = &li->li_tv;
2989 }
2990 }
2991 else if (argvars[0].v_type == VAR_DICT)
2992 {
2993 if ((d = argvars[0].vval.v_dict) != NULL)
2994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002995 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 if (di != NULL)
2997 tv = &di->di_tv;
2998 }
2999 }
3000 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3001 {
3002 partial_T *pt;
3003 partial_T fref_pt;
3004
3005 if (argvars[0].v_type == VAR_PARTIAL)
3006 pt = argvars[0].vval.v_partial;
3007 else
3008 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003009 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 fref_pt.pt_name = argvars[0].vval.v_string;
3011 pt = &fref_pt;
3012 }
3013
3014 if (pt != NULL)
3015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003017 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018
3019 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3020 {
3021 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003022 n = partial_name(pt);
3023 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 rettv->vval.v_string = NULL;
3025 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003026 {
3027 rettv->vval.v_string = vim_strsave(n);
3028 if (rettv->v_type == VAR_FUNC)
3029 func_ref(rettv->vval.v_string);
3030 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 }
3032 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003033 {
3034 what_is_dict = TRUE;
3035 if (pt->pt_dict != NULL)
3036 rettv_dict_set(rettv, pt->pt_dict);
3037 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 else if (STRCMP(what, "args") == 0)
3039 {
3040 rettv->v_type = VAR_LIST;
3041 if (rettv_list_alloc(rettv) == OK)
3042 {
3043 int i;
3044
3045 for (i = 0; i < pt->pt_argc; ++i)
3046 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3047 }
3048 }
3049 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003051
3052 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3053 // third argument
3054 if (!what_is_dict)
3055 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003056 }
3057 }
3058 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003059 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060
3061 if (tv == NULL)
3062 {
3063 if (argvars[2].v_type != VAR_UNKNOWN)
3064 copy_tv(&argvars[2], rettv);
3065 }
3066 else
3067 copy_tv(tv, rettv);
3068}
3069
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003070/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003071 * "getchangelist()" function
3072 */
3073 static void
3074f_getchangelist(typval_T *argvars, typval_T *rettv)
3075{
3076#ifdef FEAT_JUMPLIST
3077 buf_T *buf;
3078 int i;
3079 list_T *l;
3080 dict_T *d;
3081#endif
3082
3083 if (rettv_list_alloc(rettv) != OK)
3084 return;
3085
3086#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003087 if (argvars[0].v_type == VAR_UNKNOWN)
3088 buf = curbuf;
3089 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003090 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003091 if (buf == NULL)
3092 return;
3093
3094 l = list_alloc();
3095 if (l == NULL)
3096 return;
3097
3098 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3099 return;
3100 /*
3101 * The current window change list index tracks only the position in the
3102 * current buffer change list. For other buffers, use the change list
3103 * length as the current index.
3104 */
3105 list_append_number(rettv->vval.v_list,
3106 (varnumber_T)((buf == curwin->w_buffer)
3107 ? curwin->w_changelistidx : buf->b_changelistlen));
3108
3109 for (i = 0; i < buf->b_changelistlen; ++i)
3110 {
3111 if (buf->b_changelist[i].lnum == 0)
3112 continue;
3113 if ((d = dict_alloc()) == NULL)
3114 return;
3115 if (list_append_dict(l, d) == FAIL)
3116 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003117 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3118 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003119 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003120 }
3121#endif
3122}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123
3124/*
3125 * "getcharsearch()" function
3126 */
3127 static void
3128f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3129{
3130 if (rettv_dict_alloc(rettv) != FAIL)
3131 {
3132 dict_T *dict = rettv->vval.v_dict;
3133
Bram Moolenaare0be1672018-07-08 16:50:37 +02003134 dict_add_string(dict, "char", last_csearch());
3135 dict_add_number(dict, "forward", last_csearch_forward());
3136 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137 }
3138}
3139
3140/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003141 * "getenv()" function
3142 */
3143 static void
3144f_getenv(typval_T *argvars, typval_T *rettv)
3145{
3146 int mustfree = FALSE;
3147 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3148
3149 if (p == NULL)
3150 {
3151 rettv->v_type = VAR_SPECIAL;
3152 rettv->vval.v_number = VVAL_NULL;
3153 return;
3154 }
3155 if (!mustfree)
3156 p = vim_strsave(p);
3157 rettv->vval.v_string = p;
3158 rettv->v_type = VAR_STRING;
3159}
3160
3161/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 * "getfontname()" function
3163 */
3164 static void
3165f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3166{
3167 rettv->v_type = VAR_STRING;
3168 rettv->vval.v_string = NULL;
3169#ifdef FEAT_GUI
3170 if (gui.in_use)
3171 {
3172 GuiFont font;
3173 char_u *name = NULL;
3174
3175 if (argvars[0].v_type == VAR_UNKNOWN)
3176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003177 // Get the "Normal" font. Either the name saved by
3178 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 font = gui.norm_font;
3180 name = hl_get_font_name();
3181 }
3182 else
3183 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003184 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 return;
3187 font = gui_mch_get_font(name, FALSE);
3188 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003189 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190 }
3191 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3192 if (argvars[0].v_type != VAR_UNKNOWN)
3193 gui_mch_free_font(font);
3194 }
3195#endif
3196}
3197
3198/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003199 * "getjumplist()" function
3200 */
3201 static void
3202f_getjumplist(typval_T *argvars, typval_T *rettv)
3203{
3204#ifdef FEAT_JUMPLIST
3205 win_T *wp;
3206 int i;
3207 list_T *l;
3208 dict_T *d;
3209#endif
3210
3211 if (rettv_list_alloc(rettv) != OK)
3212 return;
3213
3214#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003215 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003216 if (wp == NULL)
3217 return;
3218
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003219 cleanup_jumplist(wp, TRUE);
3220
Bram Moolenaar4f505882018-02-10 21:06:32 +01003221 l = list_alloc();
3222 if (l == NULL)
3223 return;
3224
3225 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3226 return;
3227 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3228
3229 for (i = 0; i < wp->w_jumplistlen; ++i)
3230 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003231 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3232 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003233 if ((d = dict_alloc()) == NULL)
3234 return;
3235 if (list_append_dict(l, d) == FAIL)
3236 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003237 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3238 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003239 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003240 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003241 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003242 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003243 }
3244#endif
3245}
3246
3247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248 * "getpid()" function
3249 */
3250 static void
3251f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3252{
3253 rettv->vval.v_number = mch_get_pid();
3254}
3255
3256 static void
3257getpos_both(
3258 typval_T *argvars,
3259 typval_T *rettv,
3260 int getcurpos)
3261{
3262 pos_T *fp;
3263 list_T *l;
3264 int fnum = -1;
3265
3266 if (rettv_list_alloc(rettv) == OK)
3267 {
3268 l = rettv->vval.v_list;
3269 if (getcurpos)
3270 fp = &curwin->w_cursor;
3271 else
3272 fp = var2fpos(&argvars[0], TRUE, &fnum);
3273 if (fnum != -1)
3274 list_append_number(l, (varnumber_T)fnum);
3275 else
3276 list_append_number(l, (varnumber_T)0);
3277 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3278 : (varnumber_T)0);
3279 list_append_number(l, (fp != NULL)
3280 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3281 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003282 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283 (varnumber_T)0);
3284 if (getcurpos)
3285 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003286 int save_set_curswant = curwin->w_set_curswant;
3287 colnr_T save_curswant = curwin->w_curswant;
3288 colnr_T save_virtcol = curwin->w_virtcol;
3289
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 update_curswant();
3291 list_append_number(l, curwin->w_curswant == MAXCOL ?
3292 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003293
3294 // Do not change "curswant", as it is unexpected that a get
3295 // function has a side effect.
3296 if (save_set_curswant)
3297 {
3298 curwin->w_set_curswant = save_set_curswant;
3299 curwin->w_curswant = save_curswant;
3300 curwin->w_virtcol = save_virtcol;
3301 curwin->w_valid &= ~VALID_VIRTCOL;
3302 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 }
3304 }
3305 else
3306 rettv->vval.v_number = FALSE;
3307}
3308
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309/*
3310 * "getcurpos()" function
3311 */
3312 static void
3313f_getcurpos(typval_T *argvars, typval_T *rettv)
3314{
3315 getpos_both(argvars, rettv, TRUE);
3316}
3317
3318/*
3319 * "getpos(string)" function
3320 */
3321 static void
3322f_getpos(typval_T *argvars, typval_T *rettv)
3323{
3324 getpos_both(argvars, rettv, FALSE);
3325}
3326
3327/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 * "getreg()" function
3329 */
3330 static void
3331f_getreg(typval_T *argvars, typval_T *rettv)
3332{
3333 char_u *strregname;
3334 int regname;
3335 int arg2 = FALSE;
3336 int return_list = FALSE;
3337 int error = FALSE;
3338
3339 if (argvars[0].v_type != VAR_UNKNOWN)
3340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003341 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 error = strregname == NULL;
3343 if (argvars[1].v_type != VAR_UNKNOWN)
3344 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003345 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02003347 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 }
3349 }
3350 else
3351 strregname = get_vim_var_str(VV_REG);
3352
3353 if (error)
3354 return;
3355
3356 regname = (strregname == NULL ? '"' : *strregname);
3357 if (regname == 0)
3358 regname = '"';
3359
3360 if (return_list)
3361 {
3362 rettv->v_type = VAR_LIST;
3363 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3364 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3365 if (rettv->vval.v_list == NULL)
3366 (void)rettv_list_alloc(rettv);
3367 else
3368 ++rettv->vval.v_list->lv_refcount;
3369 }
3370 else
3371 {
3372 rettv->v_type = VAR_STRING;
3373 rettv->vval.v_string = get_reg_contents(regname,
3374 arg2 ? GREG_EXPR_SRC : 0);
3375 }
3376}
3377
3378/*
3379 * "getregtype()" function
3380 */
3381 static void
3382f_getregtype(typval_T *argvars, typval_T *rettv)
3383{
3384 char_u *strregname;
3385 int regname;
3386 char_u buf[NUMBUFLEN + 2];
3387 long reglen = 0;
3388
3389 if (argvars[0].v_type != VAR_UNKNOWN)
3390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003391 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003392 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 {
3394 rettv->v_type = VAR_STRING;
3395 rettv->vval.v_string = NULL;
3396 return;
3397 }
3398 }
3399 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003400 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 strregname = get_vim_var_str(VV_REG);
3402
3403 regname = (strregname == NULL ? '"' : *strregname);
3404 if (regname == 0)
3405 regname = '"';
3406
3407 buf[0] = NUL;
3408 buf[1] = NUL;
3409 switch (get_reg_type(regname, &reglen))
3410 {
3411 case MLINE: buf[0] = 'V'; break;
3412 case MCHAR: buf[0] = 'v'; break;
3413 case MBLOCK:
3414 buf[0] = Ctrl_V;
3415 sprintf((char *)buf + 1, "%ld", reglen + 1);
3416 break;
3417 }
3418 rettv->v_type = VAR_STRING;
3419 rettv->vval.v_string = vim_strsave(buf);
3420}
3421
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003422/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003423 * "gettagstack()" function
3424 */
3425 static void
3426f_gettagstack(typval_T *argvars, typval_T *rettv)
3427{
3428 win_T *wp = curwin; // default is current window
3429
3430 if (rettv_dict_alloc(rettv) != OK)
3431 return;
3432
3433 if (argvars[0].v_type != VAR_UNKNOWN)
3434 {
3435 wp = find_win_by_nr_or_id(&argvars[0]);
3436 if (wp == NULL)
3437 return;
3438 }
3439
3440 get_tagstack(wp, rettv->vval.v_dict);
3441}
3442
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003443/*
3444 * "gettext()" function
3445 */
3446 static void
3447f_gettext(typval_T *argvars, typval_T *rettv)
3448{
3449 if (argvars[0].v_type != VAR_STRING
3450 || argvars[0].vval.v_string == NULL
3451 || *argvars[0].vval.v_string == NUL)
3452 {
3453 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3454 }
3455 else
3456 {
3457 rettv->v_type = VAR_STRING;
3458 rettv->vval.v_string = vim_strsave(
3459 (char_u *)_(argvars[0].vval.v_string));
3460 }
3461}
3462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464#include "version.h"
3465
3466/*
3467 * "has()" function
3468 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003469 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470f_has(typval_T *argvars, typval_T *rettv)
3471{
3472 int i;
3473 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003474 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003475 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003476 typedef struct {
3477 char *name;
3478 short present;
3479 } has_item_T;
3480 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003482 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003484 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003485#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003486 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003487#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003488 },
3489 {"arp",
3490#if defined(AMIGA) && defined(FEAT_ARP)
3491 1
3492#else
3493 0
3494#endif
3495 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003496 {"haiku",
3497#ifdef __HAIKU__
3498 1
3499#else
3500 0
3501#endif
3502 },
3503 {"bsd",
3504#if defined(BSD) && !defined(MACOS_X)
3505 1
3506#else
3507 0
3508#endif
3509 },
3510 {"hpux",
3511#ifdef hpux
3512 1
3513#else
3514 0
3515#endif
3516 },
3517 {"linux",
3518#ifdef __linux__
3519 1
3520#else
3521 0
3522#endif
3523 },
3524 {"mac", // Mac OS X (and, once, Mac OS Classic)
3525#ifdef MACOS_X
3526 1
3527#else
3528 0
3529#endif
3530 },
3531 {"osx", // Mac OS X
3532#ifdef MACOS_X
3533 1
3534#else
3535 0
3536#endif
3537 },
3538 {"macunix", // Mac OS X, with the darwin feature
3539#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3540 1
3541#else
3542 0
3543#endif
3544 },
3545 {"osxdarwin", // synonym for macunix
3546#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3547 1
3548#else
3549 0
3550#endif
3551 },
3552 {"qnx",
3553#ifdef __QNX__
3554 1
3555#else
3556 0
3557#endif
3558 },
3559 {"sun",
3560#ifdef SUN_SYSTEM
3561 1
3562#else
3563 0
3564#endif
3565 },
3566 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003568 1
3569#else
3570 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003572 },
3573 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003575 1
3576#else
3577 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003579 },
3580 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003581#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003582 1
3583#else
3584 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003586 },
3587 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003588#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003589 1
3590#else
3591 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003592#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003593 },
3594 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003595#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003596 1
3597#else
3598 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003600 },
3601 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003603 1
3604#else
3605 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003607 },
3608 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003610 1
3611#else
3612 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003614 },
3615 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 1
3618#else
3619 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003621 },
3622 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003624 1
3625#else
3626 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003628 },
3629 {"autocmd", 1},
3630 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003631#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003632 1
3633#else
3634 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003635#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003636 },
3637 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003638#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003639 1
3640#else
3641 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003642#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003643 },
3644 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003645#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003646 1
3647#else
3648 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003650 },
3651 {"balloon_multiline",
3652#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3653 // MS-Windows requires runtime check, see below
3654 1
3655#else
3656 0
3657#endif
3658 },
3659 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003660#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003661 1
3662#else
3663 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003664#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003665 },
3666 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003668 1
3669#else
3670 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003672 },
3673 {"all_builtin_terms",
3674#if defined(ALL_BUILTIN_TCAPS)
3675 1
3676#else
3677 0
3678#endif
3679 },
3680 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003681#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003682 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003684 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003686 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003687#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003688 },
3689 {"byte_offset",
3690#ifdef FEAT_BYTEOFF
3691 1
3692#else
3693 0
3694#endif
3695 },
3696 {"channel",
3697#ifdef FEAT_JOB_CHANNEL
3698 1
3699#else
3700 0
3701#endif
3702 },
3703 {"cindent",
3704#ifdef FEAT_CINDENT
3705 1
3706#else
3707 0
3708#endif
3709 },
3710 {"clientserver",
3711#ifdef FEAT_CLIENTSERVER
3712 1
3713#else
3714 0
3715#endif
3716 },
3717 {"clipboard",
3718#ifdef FEAT_CLIPBOARD
3719 1
3720#else
3721 0
3722#endif
3723 },
3724 {"cmdline_compl", 1},
3725 {"cmdline_hist", 1},
3726 {"comments", 1},
3727 {"conceal",
3728#ifdef FEAT_CONCEAL
3729 1
3730#else
3731 0
3732#endif
3733 },
3734 {"cryptv",
3735#ifdef FEAT_CRYPT
3736 1
3737#else
3738 0
3739#endif
3740 },
3741 {"crypt-blowfish",
3742#ifdef FEAT_CRYPT
3743 1
3744#else
3745 0
3746#endif
3747 },
3748 {"crypt-blowfish2",
3749#ifdef FEAT_CRYPT
3750 1
3751#else
3752 0
3753#endif
3754 },
3755 {"cscope",
3756#ifdef FEAT_CSCOPE
3757 1
3758#else
3759 0
3760#endif
3761 },
3762 {"cursorbind", 1},
3763 {"cursorshape",
3764#ifdef CURSOR_SHAPE
3765 1
3766#else
3767 0
3768#endif
3769 },
3770 {"debug",
3771#ifdef DEBUG
3772 1
3773#else
3774 0
3775#endif
3776 },
3777 {"dialog_con",
3778#ifdef FEAT_CON_DIALOG
3779 1
3780#else
3781 0
3782#endif
3783 },
3784 {"dialog_gui",
3785#ifdef FEAT_GUI_DIALOG
3786 1
3787#else
3788 0
3789#endif
3790 },
3791 {"diff",
3792#ifdef FEAT_DIFF
3793 1
3794#else
3795 0
3796#endif
3797 },
3798 {"digraphs",
3799#ifdef FEAT_DIGRAPHS
3800 1
3801#else
3802 0
3803#endif
3804 },
3805 {"directx",
3806#ifdef FEAT_DIRECTX
3807 1
3808#else
3809 0
3810#endif
3811 },
3812 {"dnd",
3813#ifdef FEAT_DND
3814 1
3815#else
3816 0
3817#endif
3818 },
3819 {"emacs_tags",
3820#ifdef FEAT_EMACS_TAGS
3821 1
3822#else
3823 0
3824#endif
3825 },
3826 {"eval", 1}, // always present, of course!
3827 {"ex_extra", 1}, // graduated feature
3828 {"extra_search",
3829#ifdef FEAT_SEARCH_EXTRA
3830 1
3831#else
3832 0
3833#endif
3834 },
3835 {"file_in_path",
3836#ifdef FEAT_SEARCHPATH
3837 1
3838#else
3839 0
3840#endif
3841 },
3842 {"filterpipe",
3843#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3844 1
3845#else
3846 0
3847#endif
3848 },
3849 {"find_in_path",
3850#ifdef FEAT_FIND_ID
3851 1
3852#else
3853 0
3854#endif
3855 },
3856 {"float",
3857#ifdef FEAT_FLOAT
3858 1
3859#else
3860 0
3861#endif
3862 },
3863 {"folding",
3864#ifdef FEAT_FOLDING
3865 1
3866#else
3867 0
3868#endif
3869 },
3870 {"footer",
3871#ifdef FEAT_FOOTER
3872 1
3873#else
3874 0
3875#endif
3876 },
3877 {"fork",
3878#if !defined(USE_SYSTEM) && defined(UNIX)
3879 1
3880#else
3881 0
3882#endif
3883 },
3884 {"gettext",
3885#ifdef FEAT_GETTEXT
3886 1
3887#else
3888 0
3889#endif
3890 },
3891 {"gui",
3892#ifdef FEAT_GUI
3893 1
3894#else
3895 0
3896#endif
3897 },
3898 {"gui_neXtaw",
3899#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3900 1
3901#else
3902 0
3903#endif
3904 },
3905 {"gui_athena",
3906#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3907 1
3908#else
3909 0
3910#endif
3911 },
3912 {"gui_gtk",
3913#ifdef FEAT_GUI_GTK
3914 1
3915#else
3916 0
3917#endif
3918 },
3919 {"gui_gtk2",
3920#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3921 1
3922#else
3923 0
3924#endif
3925 },
3926 {"gui_gtk3",
3927#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3928 1
3929#else
3930 0
3931#endif
3932 },
3933 {"gui_gnome",
3934#ifdef FEAT_GUI_GNOME
3935 1
3936#else
3937 0
3938#endif
3939 },
3940 {"gui_haiku",
3941#ifdef FEAT_GUI_HAIKU
3942 1
3943#else
3944 0
3945#endif
3946 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003947 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003948 {"gui_motif",
3949#ifdef FEAT_GUI_MOTIF
3950 1
3951#else
3952 0
3953#endif
3954 },
3955 {"gui_photon",
3956#ifdef FEAT_GUI_PHOTON
3957 1
3958#else
3959 0
3960#endif
3961 },
3962 {"gui_win32",
3963#ifdef FEAT_GUI_MSWIN
3964 1
3965#else
3966 0
3967#endif
3968 },
3969 {"iconv",
3970#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3971 1
3972#else
3973 0
3974#endif
3975 },
3976 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003977 {"ipv6",
3978#ifdef FEAT_IPV6
3979 1
3980#else
3981 0
3982#endif
3983 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003984 {"job",
3985#ifdef FEAT_JOB_CHANNEL
3986 1
3987#else
3988 0
3989#endif
3990 },
3991 {"jumplist",
3992#ifdef FEAT_JUMPLIST
3993 1
3994#else
3995 0
3996#endif
3997 },
3998 {"keymap",
3999#ifdef FEAT_KEYMAP
4000 1
4001#else
4002 0
4003#endif
4004 },
4005 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4006 {"langmap",
4007#ifdef FEAT_LANGMAP
4008 1
4009#else
4010 0
4011#endif
4012 },
4013 {"libcall",
4014#ifdef FEAT_LIBCALL
4015 1
4016#else
4017 0
4018#endif
4019 },
4020 {"linebreak",
4021#ifdef FEAT_LINEBREAK
4022 1
4023#else
4024 0
4025#endif
4026 },
4027 {"lispindent",
4028#ifdef FEAT_LISP
4029 1
4030#else
4031 0
4032#endif
4033 },
4034 {"listcmds", 1},
4035 {"localmap", 1},
4036 {"lua",
4037#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4038 1
4039#else
4040 0
4041#endif
4042 },
4043 {"menu",
4044#ifdef FEAT_MENU
4045 1
4046#else
4047 0
4048#endif
4049 },
4050 {"mksession",
4051#ifdef FEAT_SESSION
4052 1
4053#else
4054 0
4055#endif
4056 },
4057 {"modify_fname", 1},
4058 {"mouse", 1},
4059 {"mouseshape",
4060#ifdef FEAT_MOUSESHAPE
4061 1
4062#else
4063 0
4064#endif
4065 },
4066 {"mouse_dec",
4067#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4068 1
4069#else
4070 0
4071#endif
4072 },
4073 {"mouse_gpm",
4074#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4075 1
4076#else
4077 0
4078#endif
4079 },
4080 {"mouse_jsbterm",
4081#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4082 1
4083#else
4084 0
4085#endif
4086 },
4087 {"mouse_netterm",
4088#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4089 1
4090#else
4091 0
4092#endif
4093 },
4094 {"mouse_pterm",
4095#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4096 1
4097#else
4098 0
4099#endif
4100 },
4101 {"mouse_sgr",
4102#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4103 1
4104#else
4105 0
4106#endif
4107 },
4108 {"mouse_sysmouse",
4109#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4110 1
4111#else
4112 0
4113#endif
4114 },
4115 {"mouse_urxvt",
4116#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4117 1
4118#else
4119 0
4120#endif
4121 },
4122 {"mouse_xterm",
4123#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4124 1
4125#else
4126 0
4127#endif
4128 },
4129 {"multi_byte", 1},
4130 {"multi_byte_ime",
4131#ifdef FEAT_MBYTE_IME
4132 1
4133#else
4134 0
4135#endif
4136 },
4137 {"multi_lang",
4138#ifdef FEAT_MULTI_LANG
4139 1
4140#else
4141 0
4142#endif
4143 },
4144 {"mzscheme",
4145#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4146 1
4147#else
4148 0
4149#endif
4150 },
4151 {"num64", 1},
4152 {"ole",
4153#ifdef FEAT_OLE
4154 1
4155#else
4156 0
4157#endif
4158 },
4159 {"packages",
4160#ifdef FEAT_EVAL
4161 1
4162#else
4163 0
4164#endif
4165 },
4166 {"path_extra",
4167#ifdef FEAT_PATH_EXTRA
4168 1
4169#else
4170 0
4171#endif
4172 },
4173 {"perl",
4174#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4175 1
4176#else
4177 0
4178#endif
4179 },
4180 {"persistent_undo",
4181#ifdef FEAT_PERSISTENT_UNDO
4182 1
4183#else
4184 0
4185#endif
4186 },
4187 {"python_compiled",
4188#if defined(FEAT_PYTHON)
4189 1
4190#else
4191 0
4192#endif
4193 },
4194 {"python_dynamic",
4195#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4196 1
4197#else
4198 0
4199#endif
4200 },
4201 {"python",
4202#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4203 1
4204#else
4205 0
4206#endif
4207 },
4208 {"pythonx",
4209#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4210 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4211 1
4212#else
4213 0
4214#endif
4215 },
4216 {"python3_compiled",
4217#if defined(FEAT_PYTHON3)
4218 1
4219#else
4220 0
4221#endif
4222 },
4223 {"python3_dynamic",
4224#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4225 1
4226#else
4227 0
4228#endif
4229 },
4230 {"python3",
4231#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4232 1
4233#else
4234 0
4235#endif
4236 },
4237 {"popupwin",
4238#ifdef FEAT_PROP_POPUP
4239 1
4240#else
4241 0
4242#endif
4243 },
4244 {"postscript",
4245#ifdef FEAT_POSTSCRIPT
4246 1
4247#else
4248 0
4249#endif
4250 },
4251 {"printer",
4252#ifdef FEAT_PRINTER
4253 1
4254#else
4255 0
4256#endif
4257 },
4258 {"profile",
4259#ifdef FEAT_PROFILE
4260 1
4261#else
4262 0
4263#endif
4264 },
4265 {"reltime",
4266#ifdef FEAT_RELTIME
4267 1
4268#else
4269 0
4270#endif
4271 },
4272 {"quickfix",
4273#ifdef FEAT_QUICKFIX
4274 1
4275#else
4276 0
4277#endif
4278 },
4279 {"rightleft",
4280#ifdef FEAT_RIGHTLEFT
4281 1
4282#else
4283 0
4284#endif
4285 },
4286 {"ruby",
4287#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4288 1
4289#else
4290 0
4291#endif
4292 },
4293 {"scrollbind", 1},
4294 {"showcmd",
4295#ifdef FEAT_CMDL_INFO
4296 1
4297#else
4298 0
4299#endif
4300 },
4301 {"cmdline_info",
4302#ifdef FEAT_CMDL_INFO
4303 1
4304#else
4305 0
4306#endif
4307 },
4308 {"signs",
4309#ifdef FEAT_SIGNS
4310 1
4311#else
4312 0
4313#endif
4314 },
4315 {"smartindent",
4316#ifdef FEAT_SMARTINDENT
4317 1
4318#else
4319 0
4320#endif
4321 },
4322 {"startuptime",
4323#ifdef STARTUPTIME
4324 1
4325#else
4326 0
4327#endif
4328 },
4329 {"statusline",
4330#ifdef FEAT_STL_OPT
4331 1
4332#else
4333 0
4334#endif
4335 },
4336 {"netbeans_intg",
4337#ifdef FEAT_NETBEANS_INTG
4338 1
4339#else
4340 0
4341#endif
4342 },
4343 {"sound",
4344#ifdef FEAT_SOUND
4345 1
4346#else
4347 0
4348#endif
4349 },
4350 {"spell",
4351#ifdef FEAT_SPELL
4352 1
4353#else
4354 0
4355#endif
4356 },
4357 {"syntax",
4358#ifdef FEAT_SYN_HL
4359 1
4360#else
4361 0
4362#endif
4363 },
4364 {"system",
4365#if defined(USE_SYSTEM) || !defined(UNIX)
4366 1
4367#else
4368 0
4369#endif
4370 },
4371 {"tag_binary",
4372#ifdef FEAT_TAG_BINS
4373 1
4374#else
4375 0
4376#endif
4377 },
4378 {"tcl",
4379#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4380 1
4381#else
4382 0
4383#endif
4384 },
4385 {"termguicolors",
4386#ifdef FEAT_TERMGUICOLORS
4387 1
4388#else
4389 0
4390#endif
4391 },
4392 {"terminal",
4393#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4394 1
4395#else
4396 0
4397#endif
4398 },
4399 {"terminfo",
4400#ifdef TERMINFO
4401 1
4402#else
4403 0
4404#endif
4405 },
4406 {"termresponse",
4407#ifdef FEAT_TERMRESPONSE
4408 1
4409#else
4410 0
4411#endif
4412 },
4413 {"textobjects",
4414#ifdef FEAT_TEXTOBJ
4415 1
4416#else
4417 0
4418#endif
4419 },
4420 {"textprop",
4421#ifdef FEAT_PROP_POPUP
4422 1
4423#else
4424 0
4425#endif
4426 },
4427 {"tgetent",
4428#ifdef HAVE_TGETENT
4429 1
4430#else
4431 0
4432#endif
4433 },
4434 {"timers",
4435#ifdef FEAT_TIMERS
4436 1
4437#else
4438 0
4439#endif
4440 },
4441 {"title",
4442#ifdef FEAT_TITLE
4443 1
4444#else
4445 0
4446#endif
4447 },
4448 {"toolbar",
4449#ifdef FEAT_TOOLBAR
4450 1
4451#else
4452 0
4453#endif
4454 },
4455 {"unnamedplus",
4456#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4457 1
4458#else
4459 0
4460#endif
4461 },
4462 {"user-commands", 1}, // was accidentally included in 5.4
4463 {"user_commands", 1},
4464 {"vartabs",
4465#ifdef FEAT_VARTABS
4466 1
4467#else
4468 0
4469#endif
4470 },
4471 {"vertsplit", 1},
4472 {"viminfo",
4473#ifdef FEAT_VIMINFO
4474 1
4475#else
4476 0
4477#endif
4478 },
4479 {"vimscript-1", 1},
4480 {"vimscript-2", 1},
4481 {"vimscript-3", 1},
4482 {"vimscript-4", 1},
4483 {"virtualedit", 1},
4484 {"visual", 1},
4485 {"visualextra", 1},
4486 {"vreplace", 1},
4487 {"vtp",
4488#ifdef FEAT_VTP
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"wildignore",
4495#ifdef FEAT_WILDIGN
4496 1
4497#else
4498 0
4499#endif
4500 },
4501 {"wildmenu",
4502#ifdef FEAT_WILDMENU
4503 1
4504#else
4505 0
4506#endif
4507 },
4508 {"windows", 1},
4509 {"winaltkeys",
4510#ifdef FEAT_WAK
4511 1
4512#else
4513 0
4514#endif
4515 },
4516 {"writebackup",
4517#ifdef FEAT_WRITEBACKUP
4518 1
4519#else
4520 0
4521#endif
4522 },
4523 {"xim",
4524#ifdef FEAT_XIM
4525 1
4526#else
4527 0
4528#endif
4529 },
4530 {"xfontset",
4531#ifdef FEAT_XFONTSET
4532 1
4533#else
4534 0
4535#endif
4536 },
4537 {"xpm",
4538#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4539 1
4540#else
4541 0
4542#endif
4543 },
4544 {"xpm_w32", // for backward compatibility
4545#ifdef FEAT_XPM_W32
4546 1
4547#else
4548 0
4549#endif
4550 },
4551 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 1
4554#else
4555 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004557 },
4558 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 1
4561#else
4562 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004564 },
4565 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 1
4568#else
4569 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004571 },
4572 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 1
4575#else
4576 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004578 },
4579 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004581 1
4582#else
4583 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004585 },
4586 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587 };
4588
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004590 for (i = 0; has_list[i].name != NULL; ++i)
4591 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004593 x = TRUE;
4594 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 break;
4596 }
4597
Bram Moolenaar79296512020-03-22 16:17:14 +01004598 // features also in has_list[] but sometimes enabled at runtime
4599 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004600 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004601 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004602 {
4603 // intentionally empty
4604 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004605#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606 else if (STRICMP(name, "balloon_multiline") == 0)
4607 n = multiline_balloon_available();
4608#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004609#ifdef VIMDLL
4610 else if (STRICMP(name, "filterpipe") == 0)
4611 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612#endif
4613#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4614 else if (STRICMP(name, "iconv") == 0)
4615 n = iconv_enabled(FALSE);
4616#endif
4617#ifdef DYNAMIC_LUA
4618 else if (STRICMP(name, "lua") == 0)
4619 n = lua_enabled(FALSE);
4620#endif
4621#ifdef DYNAMIC_MZSCHEME
4622 else if (STRICMP(name, "mzscheme") == 0)
4623 n = mzscheme_enabled(FALSE);
4624#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004625#ifdef DYNAMIC_PERL
4626 else if (STRICMP(name, "perl") == 0)
4627 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#ifdef DYNAMIC_PYTHON
4630 else if (STRICMP(name, "python") == 0)
4631 n = python_enabled(FALSE);
4632#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633#ifdef DYNAMIC_PYTHON3
4634 else if (STRICMP(name, "python3") == 0)
4635 n = python3_enabled(FALSE);
4636#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004637#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4638 else if (STRICMP(name, "pythonx") == 0)
4639 {
4640# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4641 if (p_pyx == 0)
4642 n = python3_enabled(FALSE) || python_enabled(FALSE);
4643 else if (p_pyx == 3)
4644 n = python3_enabled(FALSE);
4645 else if (p_pyx == 2)
4646 n = python_enabled(FALSE);
4647# elif defined(DYNAMIC_PYTHON)
4648 n = python_enabled(FALSE);
4649# elif defined(DYNAMIC_PYTHON3)
4650 n = python3_enabled(FALSE);
4651# endif
4652 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004653#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004654#ifdef DYNAMIC_RUBY
4655 else if (STRICMP(name, "ruby") == 0)
4656 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004657#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004658#ifdef DYNAMIC_TCL
4659 else if (STRICMP(name, "tcl") == 0)
4660 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004661#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004662#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004663 else if (STRICMP(name, "terminal") == 0)
4664 n = terminal_enabled();
4665#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004666 }
4667
Bram Moolenaar79296512020-03-22 16:17:14 +01004668 // features not in has_list[]
4669 if (x == FALSE)
4670 {
4671 if (STRNICMP(name, "patch", 5) == 0)
4672 {
4673 x = TRUE;
4674 if (name[5] == '-'
4675 && STRLEN(name) >= 11
4676 && vim_isdigit(name[6])
4677 && vim_isdigit(name[8])
4678 && vim_isdigit(name[10]))
4679 {
4680 int major = atoi((char *)name + 6);
4681 int minor = atoi((char *)name + 8);
4682
4683 // Expect "patch-9.9.01234".
4684 n = (major < VIM_VERSION_MAJOR
4685 || (major == VIM_VERSION_MAJOR
4686 && (minor < VIM_VERSION_MINOR
4687 || (minor == VIM_VERSION_MINOR
4688 && has_patch(atoi((char *)name + 10))))));
4689 }
4690 else
4691 n = has_patch(atoi((char *)name + 5));
4692 }
4693 else if (STRICMP(name, "vim_starting") == 0)
4694 {
4695 x = TRUE;
4696 n = (starting != 0);
4697 }
4698 else if (STRICMP(name, "ttyin") == 0)
4699 {
4700 x = TRUE;
4701 n = mch_input_isatty();
4702 }
4703 else if (STRICMP(name, "ttyout") == 0)
4704 {
4705 x = TRUE;
4706 n = stdout_isatty;
4707 }
4708 else if (STRICMP(name, "multi_byte_encoding") == 0)
4709 {
4710 x = TRUE;
4711 n = has_mbyte;
4712 }
4713 else if (STRICMP(name, "gui_running") == 0)
4714 {
4715 x = TRUE;
4716#ifdef FEAT_GUI
4717 n = (gui.in_use || gui.starting);
4718#endif
4719 }
4720 else if (STRICMP(name, "browse") == 0)
4721 {
4722 x = TRUE;
4723#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4724 n = gui.in_use; // gui_mch_browse() works when GUI is running
4725#endif
4726 }
4727 else if (STRICMP(name, "syntax_items") == 0)
4728 {
4729 x = TRUE;
4730#ifdef FEAT_SYN_HL
4731 n = syntax_present(curwin);
4732#endif
4733 }
4734 else if (STRICMP(name, "vcon") == 0)
4735 {
4736 x = TRUE;
4737#ifdef FEAT_VTP
4738 n = is_term_win32() && has_vtp_working();
4739#endif
4740 }
4741 else if (STRICMP(name, "netbeans_enabled") == 0)
4742 {
4743 x = TRUE;
4744#ifdef FEAT_NETBEANS_INTG
4745 n = netbeans_active();
4746#endif
4747 }
4748 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4749 {
4750 x = TRUE;
4751#ifdef FEAT_MOUSE_GPM
4752 n = gpm_enabled();
4753#endif
4754 }
4755 else if (STRICMP(name, "conpty") == 0)
4756 {
4757 x = TRUE;
4758#if defined(FEAT_TERMINAL) && defined(MSWIN)
4759 n = use_conpty();
4760#endif
4761 }
4762 else if (STRICMP(name, "clipboard_working") == 0)
4763 {
4764 x = TRUE;
4765#ifdef FEAT_CLIPBOARD
4766 n = clip_star.available;
4767#endif
4768 }
4769 }
4770
Bram Moolenaar04637e22020-09-05 18:45:29 +02004771 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01004772 // return whether feature could ever be enabled
4773 rettv->vval.v_number = x;
4774 else
4775 // return whether feature is enabled
4776 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004777}
4778
4779/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 * "haslocaldir()" function
4781 */
4782 static void
4783f_haslocaldir(typval_T *argvars, typval_T *rettv)
4784{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004785 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004786 win_T *wp = NULL;
4787
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004788 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4789
4790 // Check for window-local and tab-local directories
4791 if (wp != NULL && wp->w_localdir != NULL)
4792 rettv->vval.v_number = 1;
4793 else if (tp != NULL && tp->tp_localdir != NULL)
4794 rettv->vval.v_number = 2;
4795 else
4796 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797}
4798
4799/*
4800 * "hasmapto()" function
4801 */
4802 static void
4803f_hasmapto(typval_T *argvars, typval_T *rettv)
4804{
4805 char_u *name;
4806 char_u *mode;
4807 char_u buf[NUMBUFLEN];
4808 int abbr = FALSE;
4809
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004811 if (argvars[1].v_type == VAR_UNKNOWN)
4812 mode = (char_u *)"nvo";
4813 else
4814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004815 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02004817 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818 }
4819
4820 if (map_to_exists(name, mode, abbr))
4821 rettv->vval.v_number = TRUE;
4822 else
4823 rettv->vval.v_number = FALSE;
4824}
4825
4826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004827 * "highlightID(name)" function
4828 */
4829 static void
4830f_hlID(typval_T *argvars, typval_T *rettv)
4831{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004833}
4834
4835/*
4836 * "highlight_exists()" function
4837 */
4838 static void
4839f_hlexists(typval_T *argvars, typval_T *rettv)
4840{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004841 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842}
4843
4844/*
4845 * "hostname()" function
4846 */
4847 static void
4848f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4849{
4850 char_u hostname[256];
4851
4852 mch_get_host_name(hostname, 256);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = vim_strsave(hostname);
4855}
4856
4857/*
4858 * iconv() function
4859 */
4860 static void
4861f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4862{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 char_u buf1[NUMBUFLEN];
4864 char_u buf2[NUMBUFLEN];
4865 char_u *from, *to, *str;
4866 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004867
4868 rettv->v_type = VAR_STRING;
4869 rettv->vval.v_string = NULL;
4870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004871 str = tv_get_string(&argvars[0]);
4872 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4873 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 vimconv.vc_type = CONV_NONE;
4875 convert_setup(&vimconv, from, to);
4876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004878 if (vimconv.vc_type == CONV_NONE)
4879 rettv->vval.v_string = vim_strsave(str);
4880 else
4881 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4882
4883 convert_setup(&vimconv, NULL, NULL);
4884 vim_free(from);
4885 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886}
4887
4888/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 * "index()" function
4890 */
4891 static void
4892f_index(typval_T *argvars, typval_T *rettv)
4893{
4894 list_T *l;
4895 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004896 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004897 long idx = 0;
4898 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004899 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900
4901 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004902 if (argvars[0].v_type == VAR_BLOB)
4903 {
4904 typval_T tv;
4905 int start = 0;
4906
4907 if (argvars[2].v_type != VAR_UNKNOWN)
4908 {
4909 start = tv_get_number_chk(&argvars[2], &error);
4910 if (error)
4911 return;
4912 }
4913 b = argvars[0].vval.v_blob;
4914 if (b == NULL)
4915 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004916 if (start < 0)
4917 {
4918 start = blob_len(b) + start;
4919 if (start < 0)
4920 start = 0;
4921 }
4922
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004923 for (idx = start; idx < blob_len(b); ++idx)
4924 {
4925 tv.v_type = VAR_NUMBER;
4926 tv.vval.v_number = blob_get(b, idx);
4927 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4928 {
4929 rettv->vval.v_number = idx;
4930 return;
4931 }
4932 }
4933 return;
4934 }
4935 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004937 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 return;
4939 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 l = argvars[0].vval.v_list;
4942 if (l != NULL)
4943 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004944 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945 item = l->lv_first;
4946 if (argvars[2].v_type != VAR_UNKNOWN)
4947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // Start at specified item. Use the cached index that list_find()
4949 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004950 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004951 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004952 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02004953 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 if (error)
4955 item = NULL;
4956 }
4957
4958 for ( ; item != NULL; item = item->li_next, ++idx)
4959 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4960 {
4961 rettv->vval.v_number = idx;
4962 break;
4963 }
4964 }
4965}
4966
4967static int inputsecret_flag = 0;
4968
4969/*
4970 * "input()" function
4971 * Also handles inputsecret() when inputsecret is set.
4972 */
4973 static void
4974f_input(typval_T *argvars, typval_T *rettv)
4975{
4976 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4977}
4978
4979/*
4980 * "inputdialog()" function
4981 */
4982 static void
4983f_inputdialog(typval_T *argvars, typval_T *rettv)
4984{
4985#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004987 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4988 {
4989 char_u *message;
4990 char_u buf[NUMBUFLEN];
4991 char_u *defstr = (char_u *)"";
4992
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004993 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004994 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004995 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004996 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4997 else
4998 IObuff[0] = NUL;
4999 if (message != NULL && defstr != NULL
5000 && do_dialog(VIM_QUESTION, NULL, message,
5001 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5002 rettv->vval.v_string = vim_strsave(IObuff);
5003 else
5004 {
5005 if (message != NULL && defstr != NULL
5006 && argvars[1].v_type != VAR_UNKNOWN
5007 && argvars[2].v_type != VAR_UNKNOWN)
5008 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010 else
5011 rettv->vval.v_string = NULL;
5012 }
5013 rettv->v_type = VAR_STRING;
5014 }
5015 else
5016#endif
5017 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5018}
5019
5020/*
5021 * "inputlist()" function
5022 */
5023 static void
5024f_inputlist(typval_T *argvars, typval_T *rettv)
5025{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005026 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 listitem_T *li;
5028 int selected;
5029 int mouse_used;
5030
5031#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // While starting up, there is no place to enter text. When running tests
5033 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005034 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 return;
5036#endif
5037 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005039 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040 return;
5041 }
5042
5043 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 msg_row = Rows - 1; // for when 'cmdheight' > 1
5045 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005046 msg_scroll = TRUE;
5047 msg_clr_eos();
5048
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005049 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005050 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005051 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005053 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054 msg_putchar('\n');
5055 }
5056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005058 selected = prompt_for_number(&mouse_used);
5059 if (mouse_used)
5060 selected -= lines_left;
5061
5062 rettv->vval.v_number = selected;
5063}
5064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5066
5067/*
5068 * "inputrestore()" function
5069 */
5070 static void
5071f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5072{
5073 if (ga_userinput.ga_len > 0)
5074 {
5075 --ga_userinput.ga_len;
5076 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5077 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 }
5080 else if (p_verbose > 1)
5081 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005082 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 }
5085}
5086
5087/*
5088 * "inputsave()" function
5089 */
5090 static void
5091f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5092{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005093 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005094 if (ga_grow(&ga_userinput, 1) == OK)
5095 {
5096 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5097 + ga_userinput.ga_len);
5098 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005099 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 }
5101 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103}
5104
5105/*
5106 * "inputsecret()" function
5107 */
5108 static void
5109f_inputsecret(typval_T *argvars, typval_T *rettv)
5110{
5111 ++cmdline_star;
5112 ++inputsecret_flag;
5113 f_input(argvars, rettv);
5114 --cmdline_star;
5115 --inputsecret_flag;
5116}
5117
5118/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005119 * "interrupt()" function
5120 */
5121 static void
5122f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5123{
5124 got_int = TRUE;
5125}
5126
5127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 * "invert(expr)" function
5129 */
5130 static void
5131f_invert(typval_T *argvars, typval_T *rettv)
5132{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005133 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134}
5135
5136/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 * "islocked()" function
5138 */
5139 static void
5140f_islocked(typval_T *argvars, typval_T *rettv)
5141{
5142 lval_T lv;
5143 char_u *end;
5144 dictitem_T *di;
5145
5146 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005147 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005148 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 if (end != NULL && lv.ll_name != NULL)
5150 {
5151 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005152 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153 else
5154 {
5155 if (lv.ll_tv == NULL)
5156 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005157 di = find_var(lv.ll_name, NULL, TRUE);
5158 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005160 // Consider a variable locked when:
5161 // 1. the variable itself is locked
5162 // 2. the value of the variable is locked.
5163 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005164 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5165 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 }
5167 }
5168 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005169 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005170 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005171 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005173 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005174 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5175 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005176 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5178 }
5179 }
5180
5181 clear_lval(&lv);
5182}
5183
5184#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5185/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005186 * "isinf()" function
5187 */
5188 static void
5189f_isinf(typval_T *argvars, typval_T *rettv)
5190{
5191 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5192 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5193}
5194
5195/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 * "isnan()" function
5197 */
5198 static void
5199f_isnan(typval_T *argvars, typval_T *rettv)
5200{
5201 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5202 && isnan(argvars[0].vval.v_float);
5203}
5204#endif
5205
5206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005207 * "last_buffer_nr()" function.
5208 */
5209 static void
5210f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5211{
5212 int n = 0;
5213 buf_T *buf;
5214
Bram Moolenaar29323592016-07-24 22:04:11 +02005215 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005216 if (n < buf->b_fnum)
5217 n = buf->b_fnum;
5218
5219 rettv->vval.v_number = n;
5220}
5221
5222/*
5223 * "len()" function
5224 */
5225 static void
5226f_len(typval_T *argvars, typval_T *rettv)
5227{
5228 switch (argvars[0].v_type)
5229 {
5230 case VAR_STRING:
5231 case VAR_NUMBER:
5232 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005233 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005235 case VAR_BLOB:
5236 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5237 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005238 case VAR_LIST:
5239 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5240 break;
5241 case VAR_DICT:
5242 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5243 break;
5244 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005245 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005247 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248 case VAR_SPECIAL:
5249 case VAR_FLOAT:
5250 case VAR_FUNC:
5251 case VAR_PARTIAL:
5252 case VAR_JOB:
5253 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005254 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 break;
5256 }
5257}
5258
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005260libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261{
5262#ifdef FEAT_LIBCALL
5263 char_u *string_in;
5264 char_u **string_result;
5265 int nr_result;
5266#endif
5267
5268 rettv->v_type = type;
5269 if (type != VAR_NUMBER)
5270 rettv->vval.v_string = NULL;
5271
5272 if (check_restricted() || check_secure())
5273 return;
5274
5275#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5278 {
5279 string_in = NULL;
5280 if (argvars[2].v_type == VAR_STRING)
5281 string_in = argvars[2].vval.v_string;
5282 if (type == VAR_NUMBER)
5283 string_result = NULL;
5284 else
5285 string_result = &rettv->vval.v_string;
5286 if (mch_libcall(argvars[0].vval.v_string,
5287 argvars[1].vval.v_string,
5288 string_in,
5289 argvars[2].vval.v_number,
5290 string_result,
5291 &nr_result) == OK
5292 && type == VAR_NUMBER)
5293 rettv->vval.v_number = nr_result;
5294 }
5295#endif
5296}
5297
5298/*
5299 * "libcall()" function
5300 */
5301 static void
5302f_libcall(typval_T *argvars, typval_T *rettv)
5303{
5304 libcall_common(argvars, rettv, VAR_STRING);
5305}
5306
5307/*
5308 * "libcallnr()" function
5309 */
5310 static void
5311f_libcallnr(typval_T *argvars, typval_T *rettv)
5312{
5313 libcall_common(argvars, rettv, VAR_NUMBER);
5314}
5315
5316/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005317 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 */
5319 static void
5320f_line(typval_T *argvars, typval_T *rettv)
5321{
5322 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005323 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005325 int id;
5326 tabpage_T *tp;
5327 win_T *wp;
5328 win_T *save_curwin;
5329 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005331 if (argvars[1].v_type != VAR_UNKNOWN)
5332 {
5333 // use window specified in the second argument
5334 id = (int)tv_get_number(&argvars[1]);
5335 wp = win_id2wp_tp(id, &tp);
5336 if (wp != NULL && tp != NULL)
5337 {
5338 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5339 == OK)
5340 {
5341 check_cursor();
5342 fp = var2fpos(&argvars[0], TRUE, &fnum);
5343 }
5344 restore_win_noblock(save_curwin, save_curtab, TRUE);
5345 }
5346 }
5347 else
5348 // use current window
5349 fp = var2fpos(&argvars[0], TRUE, &fnum);
5350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351 if (fp != NULL)
5352 lnum = fp->lnum;
5353 rettv->vval.v_number = lnum;
5354}
5355
5356/*
5357 * "line2byte(lnum)" function
5358 */
5359 static void
5360f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5361{
5362#ifndef FEAT_BYTEOFF
5363 rettv->vval.v_number = -1;
5364#else
5365 linenr_T lnum;
5366
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005367 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5369 rettv->vval.v_number = -1;
5370 else
5371 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5372 if (rettv->vval.v_number >= 0)
5373 ++rettv->vval.v_number;
5374#endif
5375}
5376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377#ifdef FEAT_FLOAT
5378/*
5379 * "log()" function
5380 */
5381 static void
5382f_log(typval_T *argvars, typval_T *rettv)
5383{
5384 float_T f = 0.0;
5385
5386 rettv->v_type = VAR_FLOAT;
5387 if (get_float_arg(argvars, &f) == OK)
5388 rettv->vval.v_float = log(f);
5389 else
5390 rettv->vval.v_float = 0.0;
5391}
5392
5393/*
5394 * "log10()" function
5395 */
5396 static void
5397f_log10(typval_T *argvars, typval_T *rettv)
5398{
5399 float_T f = 0.0;
5400
5401 rettv->v_type = VAR_FLOAT;
5402 if (get_float_arg(argvars, &f) == OK)
5403 rettv->vval.v_float = log10(f);
5404 else
5405 rettv->vval.v_float = 0.0;
5406}
5407#endif
5408
5409#ifdef FEAT_LUA
5410/*
5411 * "luaeval()" function
5412 */
5413 static void
5414f_luaeval(typval_T *argvars, typval_T *rettv)
5415{
5416 char_u *str;
5417 char_u buf[NUMBUFLEN];
5418
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005419 if (check_restricted() || check_secure())
5420 return;
5421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005422 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423 do_luaeval(str, argvars + 1, rettv);
5424}
5425#endif
5426
5427/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 * "maparg()" function
5429 */
5430 static void
5431f_maparg(typval_T *argvars, typval_T *rettv)
5432{
5433 get_maparg(argvars, rettv, TRUE);
5434}
5435
5436/*
5437 * "mapcheck()" function
5438 */
5439 static void
5440f_mapcheck(typval_T *argvars, typval_T *rettv)
5441{
5442 get_maparg(argvars, rettv, FALSE);
5443}
5444
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005445typedef enum
5446{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 MATCH_END, // matchend()
5448 MATCH_MATCH, // match()
5449 MATCH_STR, // matchstr()
5450 MATCH_LIST, // matchlist()
5451 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005452} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453
5454 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005455find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456{
5457 char_u *str = NULL;
5458 long len = 0;
5459 char_u *expr = NULL;
5460 char_u *pat;
5461 regmatch_T regmatch;
5462 char_u patbuf[NUMBUFLEN];
5463 char_u strbuf[NUMBUFLEN];
5464 char_u *save_cpo;
5465 long start = 0;
5466 long nth = 1;
5467 colnr_T startcol = 0;
5468 int match = 0;
5469 list_T *l = NULL;
5470 listitem_T *li = NULL;
5471 long idx = 0;
5472 char_u *tofree = NULL;
5473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475 save_cpo = p_cpo;
5476 p_cpo = (char_u *)"";
5477
5478 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005479 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005481 // type MATCH_LIST: return empty list when there are no matches.
5482 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005483 if (rettv_list_alloc(rettv) == FAIL)
5484 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005485 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 && (list_append_string(rettv->vval.v_list,
5487 (char_u *)"", 0) == FAIL
5488 || list_append_number(rettv->vval.v_list,
5489 (varnumber_T)-1) == FAIL
5490 || list_append_number(rettv->vval.v_list,
5491 (varnumber_T)-1) == FAIL
5492 || list_append_number(rettv->vval.v_list,
5493 (varnumber_T)-1) == FAIL))
5494 {
5495 list_free(rettv->vval.v_list);
5496 rettv->vval.v_list = NULL;
5497 goto theend;
5498 }
5499 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005500 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 {
5502 rettv->v_type = VAR_STRING;
5503 rettv->vval.v_string = NULL;
5504 }
5505
5506 if (argvars[0].v_type == VAR_LIST)
5507 {
5508 if ((l = argvars[0].vval.v_list) == NULL)
5509 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005510 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 li = l->lv_first;
5512 }
5513 else
5514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005515 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516 len = (long)STRLEN(str);
5517 }
5518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005519 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005520 if (pat == NULL)
5521 goto theend;
5522
5523 if (argvars[2].v_type != VAR_UNKNOWN)
5524 {
5525 int error = FALSE;
5526
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005527 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 if (error)
5529 goto theend;
5530 if (l != NULL)
5531 {
5532 li = list_find(l, start);
5533 if (li == NULL)
5534 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005535 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 }
5537 else
5538 {
5539 if (start < 0)
5540 start = 0;
5541 if (start > len)
5542 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 // When "count" argument is there ignore matches before "start",
5544 // otherwise skip part of the string. Differs when pattern is "^"
5545 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 if (argvars[3].v_type != VAR_UNKNOWN)
5547 startcol = start;
5548 else
5549 {
5550 str += start;
5551 len -= start;
5552 }
5553 }
5554
5555 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 if (error)
5558 goto theend;
5559 }
5560
5561 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5562 if (regmatch.regprog != NULL)
5563 {
5564 regmatch.rm_ic = p_ic;
5565
5566 for (;;)
5567 {
5568 if (l != NULL)
5569 {
5570 if (li == NULL)
5571 {
5572 match = FALSE;
5573 break;
5574 }
5575 vim_free(tofree);
5576 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5577 if (str == NULL)
5578 break;
5579 }
5580
5581 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5582
5583 if (match && --nth <= 0)
5584 break;
5585 if (l == NULL && !match)
5586 break;
5587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 if (l != NULL)
5590 {
5591 li = li->li_next;
5592 ++idx;
5593 }
5594 else
5595 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 startcol = (colnr_T)(regmatch.startp[0]
5597 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005598 if (startcol > (colnr_T)len
5599 || str + startcol <= regmatch.startp[0])
5600 {
5601 match = FALSE;
5602 break;
5603 }
5604 }
5605 }
5606
5607 if (match)
5608 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005609 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 {
5611 listitem_T *li1 = rettv->vval.v_list->lv_first;
5612 listitem_T *li2 = li1->li_next;
5613 listitem_T *li3 = li2->li_next;
5614 listitem_T *li4 = li3->li_next;
5615
5616 vim_free(li1->li_tv.vval.v_string);
5617 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005618 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005619 li3->li_tv.vval.v_number =
5620 (varnumber_T)(regmatch.startp[0] - expr);
5621 li4->li_tv.vval.v_number =
5622 (varnumber_T)(regmatch.endp[0] - expr);
5623 if (l != NULL)
5624 li2->li_tv.vval.v_number = (varnumber_T)idx;
5625 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005626 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 {
5628 int i;
5629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 for (i = 0; i < NSUBEXP; ++i)
5632 {
5633 if (regmatch.endp[i] == NULL)
5634 {
5635 if (list_append_string(rettv->vval.v_list,
5636 (char_u *)"", 0) == FAIL)
5637 break;
5638 }
5639 else if (list_append_string(rettv->vval.v_list,
5640 regmatch.startp[i],
5641 (int)(regmatch.endp[i] - regmatch.startp[i]))
5642 == FAIL)
5643 break;
5644 }
5645 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005646 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 if (l != NULL)
5650 copy_tv(&li->li_tv, rettv);
5651 else
5652 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005653 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 }
5655 else if (l != NULL)
5656 rettv->vval.v_number = idx;
5657 else
5658 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005659 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660 rettv->vval.v_number =
5661 (varnumber_T)(regmatch.startp[0] - str);
5662 else
5663 rettv->vval.v_number =
5664 (varnumber_T)(regmatch.endp[0] - str);
5665 rettv->vval.v_number += (varnumber_T)(str - expr);
5666 }
5667 }
5668 vim_regfree(regmatch.regprog);
5669 }
5670
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005671theend:
5672 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005674 listitem_remove(rettv->vval.v_list,
5675 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676 vim_free(tofree);
5677 p_cpo = save_cpo;
5678}
5679
5680/*
5681 * "match()" function
5682 */
5683 static void
5684f_match(typval_T *argvars, typval_T *rettv)
5685{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005686 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687}
5688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689/*
5690 * "matchend()" function
5691 */
5692 static void
5693f_matchend(typval_T *argvars, typval_T *rettv)
5694{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005695 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696}
5697
5698/*
5699 * "matchlist()" function
5700 */
5701 static void
5702f_matchlist(typval_T *argvars, typval_T *rettv)
5703{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005704 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705}
5706
5707/*
5708 * "matchstr()" function
5709 */
5710 static void
5711f_matchstr(typval_T *argvars, typval_T *rettv)
5712{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005713 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714}
5715
5716/*
5717 * "matchstrpos()" function
5718 */
5719 static void
5720f_matchstrpos(typval_T *argvars, typval_T *rettv)
5721{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005722 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723}
5724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 static void
5726max_min(typval_T *argvars, typval_T *rettv, int domax)
5727{
5728 varnumber_T n = 0;
5729 varnumber_T i;
5730 int error = FALSE;
5731
5732 if (argvars[0].v_type == VAR_LIST)
5733 {
5734 list_T *l;
5735 listitem_T *li;
5736
5737 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005738 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005739 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005740 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005741 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005742 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5743 n = l->lv_u.nonmat.lv_start;
5744 else
5745 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5746 * l->lv_u.nonmat.lv_stride;
5747 }
5748 else
5749 {
5750 li = l->lv_first;
5751 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005752 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005753 n = tv_get_number_chk(&li->li_tv, &error);
5754 for (;;)
5755 {
5756 li = li->li_next;
5757 if (li == NULL)
5758 break;
5759 i = tv_get_number_chk(&li->li_tv, &error);
5760 if (domax ? i > n : i < n)
5761 n = i;
5762 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005763 }
5764 }
5765 }
5766 }
5767 else if (argvars[0].v_type == VAR_DICT)
5768 {
5769 dict_T *d;
5770 int first = TRUE;
5771 hashitem_T *hi;
5772 int todo;
5773
5774 d = argvars[0].vval.v_dict;
5775 if (d != NULL)
5776 {
5777 todo = (int)d->dv_hashtab.ht_used;
5778 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5779 {
5780 if (!HASHITEM_EMPTY(hi))
5781 {
5782 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005783 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 if (first)
5785 {
5786 n = i;
5787 first = FALSE;
5788 }
5789 else if (domax ? i > n : i < n)
5790 n = i;
5791 }
5792 }
5793 }
5794 }
5795 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005796 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005797 rettv->vval.v_number = error ? 0 : n;
5798}
5799
5800/*
5801 * "max()" function
5802 */
5803 static void
5804f_max(typval_T *argvars, typval_T *rettv)
5805{
5806 max_min(argvars, rettv, TRUE);
5807}
5808
5809/*
5810 * "min()" function
5811 */
5812 static void
5813f_min(typval_T *argvars, typval_T *rettv)
5814{
5815 max_min(argvars, rettv, FALSE);
5816}
5817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005818#if defined(FEAT_MZSCHEME) || defined(PROTO)
5819/*
5820 * "mzeval()" function
5821 */
5822 static void
5823f_mzeval(typval_T *argvars, typval_T *rettv)
5824{
5825 char_u *str;
5826 char_u buf[NUMBUFLEN];
5827
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005828 if (check_restricted() || check_secure())
5829 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 do_mzeval(str, rettv);
5832}
5833
5834 void
5835mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5836{
5837 typval_T argvars[3];
5838
5839 argvars[0].v_type = VAR_STRING;
5840 argvars[0].vval.v_string = name;
5841 copy_tv(args, &argvars[1]);
5842 argvars[2].v_type = VAR_UNKNOWN;
5843 f_call(argvars, rettv);
5844 clear_tv(&argvars[1]);
5845}
5846#endif
5847
5848/*
5849 * "nextnonblank()" function
5850 */
5851 static void
5852f_nextnonblank(typval_T *argvars, typval_T *rettv)
5853{
5854 linenr_T lnum;
5855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 {
5858 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5859 {
5860 lnum = 0;
5861 break;
5862 }
5863 if (*skipwhite(ml_get(lnum)) != NUL)
5864 break;
5865 }
5866 rettv->vval.v_number = lnum;
5867}
5868
5869/*
5870 * "nr2char()" function
5871 */
5872 static void
5873f_nr2char(typval_T *argvars, typval_T *rettv)
5874{
5875 char_u buf[NUMBUFLEN];
5876
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005877 if (has_mbyte)
5878 {
5879 int utf8 = 0;
5880
5881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02005882 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005884 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005886 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887 }
5888 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005890 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 buf[1] = NUL;
5892 }
5893 rettv->v_type = VAR_STRING;
5894 rettv->vval.v_string = vim_strsave(buf);
5895}
5896
5897/*
5898 * "or(expr, expr)" function
5899 */
5900 static void
5901f_or(typval_T *argvars, typval_T *rettv)
5902{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005903 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5904 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005905}
5906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005907#ifdef FEAT_PERL
5908/*
5909 * "perleval()" function
5910 */
5911 static void
5912f_perleval(typval_T *argvars, typval_T *rettv)
5913{
5914 char_u *str;
5915 char_u buf[NUMBUFLEN];
5916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005917 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 do_perleval(str, rettv);
5919}
5920#endif
5921
5922#ifdef FEAT_FLOAT
5923/*
5924 * "pow()" function
5925 */
5926 static void
5927f_pow(typval_T *argvars, typval_T *rettv)
5928{
5929 float_T fx = 0.0, fy = 0.0;
5930
5931 rettv->v_type = VAR_FLOAT;
5932 if (get_float_arg(argvars, &fx) == OK
5933 && get_float_arg(&argvars[1], &fy) == OK)
5934 rettv->vval.v_float = pow(fx, fy);
5935 else
5936 rettv->vval.v_float = 0.0;
5937}
5938#endif
5939
5940/*
5941 * "prevnonblank()" function
5942 */
5943 static void
5944f_prevnonblank(typval_T *argvars, typval_T *rettv)
5945{
5946 linenr_T lnum;
5947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005948 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005949 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5950 lnum = 0;
5951 else
5952 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5953 --lnum;
5954 rettv->vval.v_number = lnum;
5955}
5956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957// This dummy va_list is here because:
5958// - passing a NULL pointer doesn't work when va_list isn't a pointer
5959// - locally in the function results in a "used before set" warning
5960// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961static va_list ap;
5962
5963/*
5964 * "printf()" function
5965 */
5966 static void
5967f_printf(typval_T *argvars, typval_T *rettv)
5968{
5969 char_u buf[NUMBUFLEN];
5970 int len;
5971 char_u *s;
5972 int saved_did_emsg = did_emsg;
5973 char *fmt;
5974
5975 rettv->v_type = VAR_STRING;
5976 rettv->vval.v_string = NULL;
5977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005978 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005980 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005981 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 if (!did_emsg)
5983 {
5984 s = alloc(len + 1);
5985 if (s != NULL)
5986 {
5987 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005988 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5989 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990 }
5991 }
5992 did_emsg |= saved_did_emsg;
5993}
5994
5995/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005996 * "pum_getpos()" function
5997 */
5998 static void
5999f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6000{
6001 if (rettv_dict_alloc(rettv) != OK)
6002 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006003 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006004}
6005
6006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 * "pumvisible()" function
6008 */
6009 static void
6010f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6011{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 if (pum_visible())
6013 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014}
6015
6016#ifdef FEAT_PYTHON3
6017/*
6018 * "py3eval()" function
6019 */
6020 static void
6021f_py3eval(typval_T *argvars, typval_T *rettv)
6022{
6023 char_u *str;
6024 char_u buf[NUMBUFLEN];
6025
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006026 if (check_restricted() || check_secure())
6027 return;
6028
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006029 if (p_pyx == 0)
6030 p_pyx = 3;
6031
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006032 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 do_py3eval(str, rettv);
6034}
6035#endif
6036
6037#ifdef FEAT_PYTHON
6038/*
6039 * "pyeval()" function
6040 */
6041 static void
6042f_pyeval(typval_T *argvars, typval_T *rettv)
6043{
6044 char_u *str;
6045 char_u buf[NUMBUFLEN];
6046
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006047 if (check_restricted() || check_secure())
6048 return;
6049
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006050 if (p_pyx == 0)
6051 p_pyx = 2;
6052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006053 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 do_pyeval(str, rettv);
6055}
6056#endif
6057
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006058#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6059/*
6060 * "pyxeval()" function
6061 */
6062 static void
6063f_pyxeval(typval_T *argvars, typval_T *rettv)
6064{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006065 if (check_restricted() || check_secure())
6066 return;
6067
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006068# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6069 init_pyxversion();
6070 if (p_pyx == 2)
6071 f_pyeval(argvars, rettv);
6072 else
6073 f_py3eval(argvars, rettv);
6074# elif defined(FEAT_PYTHON)
6075 f_pyeval(argvars, rettv);
6076# elif defined(FEAT_PYTHON3)
6077 f_py3eval(argvars, rettv);
6078# endif
6079}
6080#endif
6081
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006082static UINT32_T srand_seed_for_testing = 0;
6083static int srand_seed_for_testing_is_used = FALSE;
6084
6085 static void
6086f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6087{
6088 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006089 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006090 else
6091 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006092 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6093 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006094 }
6095}
6096
6097 static void
6098init_srand(UINT32_T *x)
6099{
6100#ifndef MSWIN
6101 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6102#endif
6103
6104 if (srand_seed_for_testing_is_used)
6105 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006106 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006107 return;
6108 }
6109#ifndef MSWIN
6110 if (dev_urandom_state != FAIL)
6111 {
6112 int fd = open("/dev/urandom", O_RDONLY);
6113 struct {
6114 union {
6115 UINT32_T number;
6116 char bytes[sizeof(UINT32_T)];
6117 } contents;
6118 } buf;
6119
6120 // Attempt reading /dev/urandom.
6121 if (fd == -1)
6122 dev_urandom_state = FAIL;
6123 else
6124 {
6125 buf.contents.number = 0;
6126 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6127 != sizeof(UINT32_T))
6128 dev_urandom_state = FAIL;
6129 else
6130 {
6131 dev_urandom_state = OK;
6132 *x = buf.contents.number;
6133 }
6134 close(fd);
6135 }
6136 }
6137 if (dev_urandom_state != OK)
6138 // Reading /dev/urandom doesn't work, fall back to time().
6139#endif
6140 *x = vim_time();
6141}
6142
6143#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6144#define SPLITMIX32(x, z) ( \
6145 z = (x += 0x9e3779b9), \
6146 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6147 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6148 z ^ (z >> 16) \
6149 )
6150#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6151 result = ROTL(y * 5, 7) * 9; \
6152 t = y << 9; \
6153 z ^= x; \
6154 w ^= y; \
6155 y ^= z, x ^= w; \
6156 z ^= t; \
6157 w = ROTL(w, 11);
6158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006160 * "rand()" function
6161 */
6162 static void
6163f_rand(typval_T *argvars, typval_T *rettv)
6164{
6165 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006166 static UINT32_T gx, gy, gz, gw;
6167 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006168 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006169 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006170
6171 if (argvars[0].v_type == VAR_UNKNOWN)
6172 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006173 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006174 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006175 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006176 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006177 init_srand(&x);
6178
6179 gx = SPLITMIX32(x, z);
6180 gy = SPLITMIX32(x, z);
6181 gz = SPLITMIX32(x, z);
6182 gw = SPLITMIX32(x, z);
6183 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006184 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006185
6186 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006187 }
6188 else if (argvars[0].v_type == VAR_LIST)
6189 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006190 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006191 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006192 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006193
6194 lx = list_find(l, 0L);
6195 ly = list_find(l, 1L);
6196 lz = list_find(l, 2L);
6197 lw = list_find(l, 3L);
6198 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6199 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6200 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6201 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6202 x = (UINT32_T)lx->li_tv.vval.v_number;
6203 y = (UINT32_T)ly->li_tv.vval.v_number;
6204 z = (UINT32_T)lz->li_tv.vval.v_number;
6205 w = (UINT32_T)lw->li_tv.vval.v_number;
6206
6207 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6208
6209 lx->li_tv.vval.v_number = (varnumber_T)x;
6210 ly->li_tv.vval.v_number = (varnumber_T)y;
6211 lz->li_tv.vval.v_number = (varnumber_T)z;
6212 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006213 }
6214 else
6215 goto theend;
6216
6217 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006218 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006219 return;
6220
6221theend:
6222 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006223 rettv->v_type = VAR_NUMBER;
6224 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006225}
6226
6227/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006228 * "srand()" function
6229 */
6230 static void
6231f_srand(typval_T *argvars, typval_T *rettv)
6232{
6233 UINT32_T x = 0, z;
6234
6235 if (rettv_list_alloc(rettv) == FAIL)
6236 return;
6237 if (argvars[0].v_type == VAR_UNKNOWN)
6238 {
6239 init_srand(&x);
6240 }
6241 else
6242 {
6243 int error = FALSE;
6244
6245 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6246 if (error)
6247 return;
6248 }
6249
6250 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6251 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6252 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6253 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6254}
6255
6256#undef ROTL
6257#undef SPLITMIX32
6258#undef SHUFFLE_XOSHIRO128STARSTAR
6259
6260/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 * "range()" function
6262 */
6263 static void
6264f_range(typval_T *argvars, typval_T *rettv)
6265{
6266 varnumber_T start;
6267 varnumber_T end;
6268 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 int error = FALSE;
6270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006271 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 if (argvars[1].v_type == VAR_UNKNOWN)
6273 {
6274 end = start - 1;
6275 start = 0;
6276 }
6277 else
6278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006279 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006280 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006281 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 }
6283
6284 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006287 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006289 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292 list_T *list = rettv->vval.v_list;
6293
6294 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006295 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 // be called.
6297 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006298 list->lv_u.nonmat.lv_start = start;
6299 list->lv_u.nonmat.lv_end = end;
6300 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006301 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 }
6303}
6304
6305/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006306 * Materialize "list".
6307 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 */
6309 void
6310range_list_materialize(list_T *list)
6311{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006312 varnumber_T start = list->lv_u.nonmat.lv_start;
6313 varnumber_T end = list->lv_u.nonmat.lv_end;
6314 int stride = list->lv_u.nonmat.lv_stride;
6315 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006317 list->lv_first = NULL;
6318 list->lv_u.mat.lv_last = NULL;
6319 list->lv_len = 0;
6320 list->lv_u.mat.lv_idx_item = NULL;
6321 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6322 if (list_append_number(list, (varnumber_T)i) == FAIL)
6323 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006324}
6325
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006326/*
6327 * "getreginfo()" function
6328 */
6329 static void
6330f_getreginfo(typval_T *argvars, typval_T *rettv)
6331{
6332 char_u *strregname;
6333 int regname;
6334 char_u buf[NUMBUFLEN + 2];
6335 long reglen = 0;
6336 dict_T *dict;
6337 list_T *list;
6338
6339 if (argvars[0].v_type != VAR_UNKNOWN)
6340 {
6341 strregname = tv_get_string_chk(&argvars[0]);
6342 if (strregname == NULL)
6343 return;
6344 }
6345 else
6346 strregname = get_vim_var_str(VV_REG);
6347
6348 regname = (strregname == NULL ? '"' : *strregname);
6349 if (regname == 0 || regname == '@')
6350 regname = '"';
6351
6352 if (rettv_dict_alloc(rettv) == FAIL)
6353 return;
6354 dict = rettv->vval.v_dict;
6355
6356 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6357 if (list == NULL)
6358 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006359 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006360
6361 buf[0] = NUL;
6362 buf[1] = NUL;
6363 switch (get_reg_type(regname, &reglen))
6364 {
6365 case MLINE: buf[0] = 'V'; break;
6366 case MCHAR: buf[0] = 'v'; break;
6367 case MBLOCK:
6368 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6369 reglen + 1);
6370 break;
6371 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006372 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006373
6374 buf[0] = get_register_name(get_unname_register());
6375 buf[1] = NUL;
6376 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006377 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006378 else
6379 {
6380 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6381
6382 if (item != NULL)
6383 {
6384 item->di_tv.v_type = VAR_SPECIAL;
6385 item->di_tv.vval.v_number = regname == buf[0]
6386 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006387 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006388 }
6389 }
6390}
6391
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006392 static void
6393return_register(int regname, typval_T *rettv)
6394{
6395 char_u buf[2] = {0, 0};
6396
6397 buf[0] = (char_u)regname;
6398 rettv->v_type = VAR_STRING;
6399 rettv->vval.v_string = vim_strsave(buf);
6400}
6401
6402/*
6403 * "reg_executing()" function
6404 */
6405 static void
6406f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6407{
6408 return_register(reg_executing, rettv);
6409}
6410
6411/*
6412 * "reg_recording()" function
6413 */
6414 static void
6415f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6416{
6417 return_register(reg_recording, rettv);
6418}
6419
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421 * "rename({from}, {to})" function
6422 */
6423 static void
6424f_rename(typval_T *argvars, typval_T *rettv)
6425{
6426 char_u buf[NUMBUFLEN];
6427
6428 if (check_restricted() || check_secure())
6429 rettv->vval.v_number = -1;
6430 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006431 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6432 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433}
6434
6435/*
6436 * "repeat()" function
6437 */
6438 static void
6439f_repeat(typval_T *argvars, typval_T *rettv)
6440{
6441 char_u *p;
6442 int n;
6443 int slen;
6444 int len;
6445 char_u *r;
6446 int i;
6447
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006448 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449 if (argvars[0].v_type == VAR_LIST)
6450 {
6451 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6452 while (n-- > 0)
6453 if (list_extend(rettv->vval.v_list,
6454 argvars[0].vval.v_list, NULL) == FAIL)
6455 break;
6456 }
6457 else
6458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006459 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460 rettv->v_type = VAR_STRING;
6461 rettv->vval.v_string = NULL;
6462
6463 slen = (int)STRLEN(p);
6464 len = slen * n;
6465 if (len <= 0)
6466 return;
6467
6468 r = alloc(len + 1);
6469 if (r != NULL)
6470 {
6471 for (i = 0; i < n; i++)
6472 mch_memmove(r + i * slen, p, (size_t)slen);
6473 r[len] = NUL;
6474 }
6475
6476 rettv->vval.v_string = r;
6477 }
6478}
6479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006480#define SP_NOMOVE 0x01 // don't move cursor
6481#define SP_REPEAT 0x02 // repeat to find outer pair
6482#define SP_RETCOUNT 0x04 // return matchcount
6483#define SP_SETPCMARK 0x08 // set previous context mark
6484#define SP_START 0x10 // accept match at start position
6485#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6486#define SP_END 0x40 // leave cursor at end of match
6487#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489/*
6490 * Get flags for a search function.
6491 * Possibly sets "p_ws".
6492 * Returns BACKWARD, FORWARD or zero (for an error).
6493 */
6494 static int
6495get_search_arg(typval_T *varp, int *flagsp)
6496{
6497 int dir = FORWARD;
6498 char_u *flags;
6499 char_u nbuf[NUMBUFLEN];
6500 int mask;
6501
6502 if (varp->v_type != VAR_UNKNOWN)
6503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006504 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507 while (*flags != NUL)
6508 {
6509 switch (*flags)
6510 {
6511 case 'b': dir = BACKWARD; break;
6512 case 'w': p_ws = TRUE; break;
6513 case 'W': p_ws = FALSE; break;
6514 default: mask = 0;
6515 if (flagsp != NULL)
6516 switch (*flags)
6517 {
6518 case 'c': mask = SP_START; break;
6519 case 'e': mask = SP_END; break;
6520 case 'm': mask = SP_RETCOUNT; break;
6521 case 'n': mask = SP_NOMOVE; break;
6522 case 'p': mask = SP_SUBPAT; break;
6523 case 'r': mask = SP_REPEAT; break;
6524 case 's': mask = SP_SETPCMARK; break;
6525 case 'z': mask = SP_COLUMN; break;
6526 }
6527 if (mask == 0)
6528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006529 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 dir = 0;
6531 }
6532 else
6533 *flagsp |= mask;
6534 }
6535 if (dir == 0)
6536 break;
6537 ++flags;
6538 }
6539 }
6540 return dir;
6541}
6542
6543/*
6544 * Shared by search() and searchpos() functions.
6545 */
6546 static int
6547search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6548{
6549 int flags;
6550 char_u *pat;
6551 pos_T pos;
6552 pos_T save_cursor;
6553 int save_p_ws = p_ws;
6554 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006555 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006558 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559 long time_limit = 0;
6560#endif
6561 int options = SEARCH_KEEP;
6562 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006563 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006564 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006565 pos_T firstpos;
6566
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006567 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006568 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569 if (dir == 0)
6570 goto theend;
6571 flags = *flagsp;
6572 if (flags & SP_START)
6573 options |= SEARCH_START;
6574 if (flags & SP_END)
6575 options |= SEARCH_END;
6576 if (flags & SP_COLUMN)
6577 options |= SEARCH_COL;
6578
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006579 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006582 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583 if (lnum_stop < 0)
6584 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585 if (argvars[3].v_type != VAR_UNKNOWN)
6586 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006587#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006588 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589 if (time_limit < 0)
6590 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006592 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594 }
6595
6596#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006597 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 profile_setlimit(time_limit, &tm);
6599#endif
6600
6601 /*
6602 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6603 * Check to make sure only those flags are set.
6604 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6605 * flags cannot be set. Check for that condition also.
6606 */
6607 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6608 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6609 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006610 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 goto theend;
6612 }
6613
6614 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006615 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006616 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006617 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6618#ifdef FEAT_RELTIME
6619 sia.sa_tm = &tm;
6620#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006621
6622 // Repeat until {skip} returns FALSE.
6623 for (;;)
6624 {
6625 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006626 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006627 // finding the first match again means there is no match where {skip}
6628 // evaluates to zero.
6629 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6630 subpatnum = FAIL;
6631
Bram Moolenaara9c01042020-06-07 14:50:50 +02006632 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006633 // didn't find it or no skip argument
6634 break;
6635 firstpos = pos;
6636
Bram Moolenaara9c01042020-06-07 14:50:50 +02006637 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006638 {
6639 int do_skip;
6640 int err;
6641 pos_T save_pos = curwin->w_cursor;
6642
6643 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006644 err = FALSE;
6645 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006646 curwin->w_cursor = save_pos;
6647 if (err)
6648 {
6649 // Evaluating {skip} caused an error, break here.
6650 subpatnum = FAIL;
6651 break;
6652 }
6653 if (!do_skip)
6654 break;
6655 }
6656 }
6657
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658 if (subpatnum != FAIL)
6659 {
6660 if (flags & SP_SUBPAT)
6661 retval = subpatnum;
6662 else
6663 retval = pos.lnum;
6664 if (flags & SP_SETPCMARK)
6665 setpcmark();
6666 curwin->w_cursor = pos;
6667 if (match_pos != NULL)
6668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006669 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 match_pos->lnum = pos.lnum;
6671 match_pos->col = pos.col + 1;
6672 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006673 // "/$" will put the cursor after the end of the line, may need to
6674 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 check_cursor();
6676 }
6677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 if (flags & SP_NOMOVE)
6680 curwin->w_cursor = save_cursor;
6681 else
6682 curwin->w_set_curswant = TRUE;
6683theend:
6684 p_ws = save_p_ws;
6685
6686 return retval;
6687}
6688
6689#ifdef FEAT_FLOAT
6690
6691/*
6692 * round() is not in C90, use ceil() or floor() instead.
6693 */
6694 float_T
6695vim_round(float_T f)
6696{
6697 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6698}
6699
6700/*
6701 * "round({float})" function
6702 */
6703 static void
6704f_round(typval_T *argvars, typval_T *rettv)
6705{
6706 float_T f = 0.0;
6707
6708 rettv->v_type = VAR_FLOAT;
6709 if (get_float_arg(argvars, &f) == OK)
6710 rettv->vval.v_float = vim_round(f);
6711 else
6712 rettv->vval.v_float = 0.0;
6713}
6714#endif
6715
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006716#ifdef FEAT_RUBY
6717/*
6718 * "rubyeval()" function
6719 */
6720 static void
6721f_rubyeval(typval_T *argvars, typval_T *rettv)
6722{
6723 char_u *str;
6724 char_u buf[NUMBUFLEN];
6725
6726 str = tv_get_string_buf(&argvars[0], buf);
6727 do_rubyeval(str, rettv);
6728}
6729#endif
6730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731/*
6732 * "screenattr()" function
6733 */
6734 static void
6735f_screenattr(typval_T *argvars, typval_T *rettv)
6736{
6737 int row;
6738 int col;
6739 int c;
6740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006741 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6742 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 if (row < 0 || row >= screen_Rows
6744 || col < 0 || col >= screen_Columns)
6745 c = -1;
6746 else
6747 c = ScreenAttrs[LineOffset[row] + col];
6748 rettv->vval.v_number = c;
6749}
6750
6751/*
6752 * "screenchar()" function
6753 */
6754 static void
6755f_screenchar(typval_T *argvars, typval_T *rettv)
6756{
6757 int row;
6758 int col;
6759 int off;
6760 int c;
6761
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006762 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6763 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006764 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 c = -1;
6766 else
6767 {
6768 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769 if (enc_utf8 && ScreenLinesUC[off] != 0)
6770 c = ScreenLinesUC[off];
6771 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 c = ScreenLines[off];
6773 }
6774 rettv->vval.v_number = c;
6775}
6776
6777/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006778 * "screenchars()" function
6779 */
6780 static void
6781f_screenchars(typval_T *argvars, typval_T *rettv)
6782{
6783 int row;
6784 int col;
6785 int off;
6786 int c;
6787 int i;
6788
6789 if (rettv_list_alloc(rettv) == FAIL)
6790 return;
6791 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6792 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6793 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6794 return;
6795
6796 off = LineOffset[row] + col;
6797 if (enc_utf8 && ScreenLinesUC[off] != 0)
6798 c = ScreenLinesUC[off];
6799 else
6800 c = ScreenLines[off];
6801 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6802
6803 if (enc_utf8)
6804
6805 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6806 list_append_number(rettv->vval.v_list,
6807 (varnumber_T)ScreenLinesC[i][off]);
6808}
6809
6810/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 * "screencol()" function
6812 *
6813 * First column is 1 to be consistent with virtcol().
6814 */
6815 static void
6816f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6817{
6818 rettv->vval.v_number = screen_screencol() + 1;
6819}
6820
6821/*
6822 * "screenrow()" function
6823 */
6824 static void
6825f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6826{
6827 rettv->vval.v_number = screen_screenrow() + 1;
6828}
6829
6830/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006831 * "screenstring()" function
6832 */
6833 static void
6834f_screenstring(typval_T *argvars, typval_T *rettv)
6835{
6836 int row;
6837 int col;
6838 int off;
6839 int c;
6840 int i;
6841 char_u buf[MB_MAXBYTES + 1];
6842 int buflen = 0;
6843
6844 rettv->vval.v_string = NULL;
6845 rettv->v_type = VAR_STRING;
6846
6847 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6848 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6849 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6850 return;
6851
6852 off = LineOffset[row] + col;
6853 if (enc_utf8 && ScreenLinesUC[off] != 0)
6854 c = ScreenLinesUC[off];
6855 else
6856 c = ScreenLines[off];
6857 buflen += mb_char2bytes(c, buf);
6858
6859 if (enc_utf8)
6860 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6861 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6862
6863 buf[buflen] = NUL;
6864 rettv->vval.v_string = vim_strsave(buf);
6865}
6866
6867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 * "search()" function
6869 */
6870 static void
6871f_search(typval_T *argvars, typval_T *rettv)
6872{
6873 int flags = 0;
6874
6875 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6876}
6877
6878/*
6879 * "searchdecl()" function
6880 */
6881 static void
6882f_searchdecl(typval_T *argvars, typval_T *rettv)
6883{
Bram Moolenaar30788d32020-09-05 21:35:16 +02006884 int locally = TRUE;
6885 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 int error = FALSE;
6887 char_u *name;
6888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006889 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006891 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 if (argvars[1].v_type != VAR_UNKNOWN)
6893 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02006894 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02006896 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 }
6898 if (!error && name != NULL)
6899 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6900 locally, thisblock, SEARCH_KEEP) == FAIL;
6901}
6902
6903/*
6904 * Used by searchpair() and searchpairpos()
6905 */
6906 static int
6907searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6908{
6909 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006910 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 int save_p_ws = p_ws;
6912 int dir;
6913 int flags = 0;
6914 char_u nbuf1[NUMBUFLEN];
6915 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006916 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 long lnum_stop = 0;
6918 long time_limit = 0;
6919
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006920 // Get the three pattern arguments: start, middle, end. Will result in an
6921 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 spat = tv_get_string_chk(&argvars[0]);
6923 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6924 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006926 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 // Handle the optional fourth argument: flags
6929 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 if (dir == 0)
6931 goto theend;
6932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006933 // Don't accept SP_END or SP_SUBPAT.
6934 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 if ((flags & (SP_END | SP_SUBPAT)) != 0
6936 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006938 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 goto theend;
6940 }
6941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006942 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 if (flags & SP_REPEAT)
6944 p_ws = FALSE;
6945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 if (argvars[3].v_type == VAR_UNKNOWN
6948 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006949 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 else
6951 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006952 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006953 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006955 if (argvars[5].v_type != VAR_UNKNOWN)
6956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006957 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006960 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006962 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963#ifdef FEAT_RELTIME
6964 if (argvars[6].v_type != VAR_UNKNOWN)
6965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006966 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006967 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006969 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006971 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972 }
6973#endif
6974 }
6975 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976
6977 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6978 match_pos, lnum_stop, time_limit);
6979
6980theend:
6981 p_ws = save_p_ws;
6982
6983 return retval;
6984}
6985
6986/*
6987 * "searchpair()" function
6988 */
6989 static void
6990f_searchpair(typval_T *argvars, typval_T *rettv)
6991{
6992 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6993}
6994
6995/*
6996 * "searchpairpos()" function
6997 */
6998 static void
6999f_searchpairpos(typval_T *argvars, typval_T *rettv)
7000{
7001 pos_T match_pos;
7002 int lnum = 0;
7003 int col = 0;
7004
7005 if (rettv_list_alloc(rettv) == FAIL)
7006 return;
7007
7008 if (searchpair_cmn(argvars, &match_pos) > 0)
7009 {
7010 lnum = match_pos.lnum;
7011 col = match_pos.col;
7012 }
7013
7014 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7015 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7016}
7017
7018/*
7019 * Search for a start/middle/end thing.
7020 * Used by searchpair(), see its documentation for the details.
7021 * Returns 0 or -1 for no match,
7022 */
7023 long
7024do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007025 char_u *spat, // start pattern
7026 char_u *mpat, // middle pattern
7027 char_u *epat, // end pattern
7028 int dir, // BACKWARD or FORWARD
7029 typval_T *skip, // skip expression
7030 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007032 linenr_T lnum_stop, // stop at this line if not zero
7033 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034{
7035 char_u *save_cpo;
7036 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7037 long retval = 0;
7038 pos_T pos;
7039 pos_T firstpos;
7040 pos_T foundpos;
7041 pos_T save_cursor;
7042 pos_T save_pos;
7043 int n;
7044 int r;
7045 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007046 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 int err;
7048 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007049#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007051#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007053 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 save_cpo = p_cpo;
7055 p_cpo = empty_option;
7056
7057#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007058 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 profile_setlimit(time_limit, &tm);
7060#endif
7061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007062 // Make two search patterns: start/end (pat2, for in nested pairs) and
7063 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007064 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7065 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066 if (pat2 == NULL || pat3 == NULL)
7067 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007068 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 if (*mpat == NUL)
7070 STRCPY(pat3, pat2);
7071 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007072 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 spat, epat, mpat);
7074 if (flags & SP_START)
7075 options |= SEARCH_START;
7076
Bram Moolenaar48570482017-10-30 21:48:41 +01007077 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007078 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 save_cursor = curwin->w_cursor;
7081 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007082 CLEAR_POS(&firstpos);
7083 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 pat = pat3;
7085 for (;;)
7086 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007087 searchit_arg_T sia;
7088
Bram Moolenaara80faa82020-04-12 19:37:17 +02007089 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007090 sia.sa_stop_lnum = lnum_stop;
7091#ifdef FEAT_RELTIME
7092 sia.sa_tm = &tm;
7093#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007094 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007095 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007096 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007097 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007098 break;
7099
7100 if (firstpos.lnum == 0)
7101 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007102 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007104 // Found the same position again. Can happen with a pattern that
7105 // has "\zs" at the end and searching backwards. Advance one
7106 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 if (dir == BACKWARD)
7108 decl(&pos);
7109 else
7110 incl(&pos);
7111 }
7112 foundpos = pos;
7113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 options &= ~SEARCH_START;
7116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007117 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007118 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 {
7120 save_pos = curwin->w_cursor;
7121 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007122 err = FALSE;
7123 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 curwin->w_cursor = save_pos;
7125 if (err)
7126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 curwin->w_cursor = save_cursor;
7129 retval = -1;
7130 break;
7131 }
7132 if (r)
7133 continue;
7134 }
7135
7136 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007138 // Found end when searching backwards or start when searching
7139 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007141 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 }
7143 else
7144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007145 // Found end when searching forward or start when searching
7146 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007148 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 }
7150
7151 if (nest == 0)
7152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007153 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 if (flags & SP_RETCOUNT)
7155 ++retval;
7156 else
7157 retval = pos.lnum;
7158 if (flags & SP_SETPCMARK)
7159 setpcmark();
7160 curwin->w_cursor = pos;
7161 if (!(flags & SP_REPEAT))
7162 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007163 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 }
7165 }
7166
7167 if (match_pos != NULL)
7168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007169 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170 match_pos->lnum = curwin->w_cursor.lnum;
7171 match_pos->col = curwin->w_cursor.col + 1;
7172 }
7173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007174 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 if ((flags & SP_NOMOVE) || retval == 0)
7176 curwin->w_cursor = save_cursor;
7177
7178theend:
7179 vim_free(pat2);
7180 vim_free(pat3);
7181 if (p_cpo == empty_option)
7182 p_cpo = save_cpo;
7183 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007184 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 free_string_option(save_cpo);
7186
7187 return retval;
7188}
7189
7190/*
7191 * "searchpos()" function
7192 */
7193 static void
7194f_searchpos(typval_T *argvars, typval_T *rettv)
7195{
7196 pos_T match_pos;
7197 int lnum = 0;
7198 int col = 0;
7199 int n;
7200 int flags = 0;
7201
7202 if (rettv_list_alloc(rettv) == FAIL)
7203 return;
7204
7205 n = search_cmn(argvars, &match_pos, &flags);
7206 if (n > 0)
7207 {
7208 lnum = match_pos.lnum;
7209 col = match_pos.col;
7210 }
7211
7212 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7213 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7214 if (flags & SP_SUBPAT)
7215 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7216}
7217
7218 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7220{
7221 dict_T *d;
7222 dictitem_T *di;
7223 char_u *csearch;
7224
7225 if (argvars[0].v_type != VAR_DICT)
7226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007227 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 return;
7229 }
7230
7231 if ((d = argvars[0].vval.v_dict) != NULL)
7232 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007233 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007234 if (csearch != NULL)
7235 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236 if (enc_utf8)
7237 {
7238 int pcc[MAX_MCO];
7239 int c = utfc_ptr2char(csearch, pcc);
7240
7241 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7242 }
7243 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007245 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007246 }
7247
7248 di = dict_find(d, (char_u *)"forward", -1);
7249 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007250 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 ? FORWARD : BACKWARD);
7252
7253 di = dict_find(d, (char_u *)"until", -1);
7254 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007255 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256 }
7257}
7258
7259/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007260 * "setenv()" function
7261 */
7262 static void
7263f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7264{
7265 char_u namebuf[NUMBUFLEN];
7266 char_u valbuf[NUMBUFLEN];
7267 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7268
7269 if (argvars[1].v_type == VAR_SPECIAL
7270 && argvars[1].vval.v_number == VVAL_NULL)
7271 vim_unsetenv(name);
7272 else
7273 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7274}
7275
7276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 * "setfperm({fname}, {mode})" function
7278 */
7279 static void
7280f_setfperm(typval_T *argvars, typval_T *rettv)
7281{
7282 char_u *fname;
7283 char_u modebuf[NUMBUFLEN];
7284 char_u *mode_str;
7285 int i;
7286 int mask;
7287 int mode = 0;
7288
7289 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007290 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 if (fname == NULL)
7292 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007293 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 if (mode_str == NULL)
7295 return;
7296 if (STRLEN(mode_str) != 9)
7297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007298 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 return;
7300 }
7301
7302 mask = 1;
7303 for (i = 8; i >= 0; --i)
7304 {
7305 if (mode_str[i] != '-')
7306 mode |= mask;
7307 mask = mask << 1;
7308 }
7309 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7310}
7311
7312/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 * "setpos()" function
7314 */
7315 static void
7316f_setpos(typval_T *argvars, typval_T *rettv)
7317{
7318 pos_T pos;
7319 int fnum;
7320 char_u *name;
7321 colnr_T curswant = -1;
7322
7323 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007324 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 if (name != NULL)
7326 {
7327 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7328 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007329 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 pos.col = 0;
7331 if (name[0] == '.' && name[1] == NUL)
7332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007333 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007334 curwin->w_cursor = pos;
7335 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007337 curwin->w_curswant = curswant - 1;
7338 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007340 check_cursor();
7341 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342 }
7343 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7344 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007345 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 if (setmark_pos(name[1], &pos, fnum) == OK)
7347 rettv->vval.v_number = 0;
7348 }
7349 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007350 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 }
7352 }
7353}
7354
7355/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007356 * Translate a register type string to the yank type and block length
7357 */
7358 static int
7359get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7360{
7361 char_u *stropt = *pp;
7362 switch (*stropt)
7363 {
7364 case 'v': case 'c': // character-wise selection
7365 *yank_type = MCHAR;
7366 break;
7367 case 'V': case 'l': // line-wise selection
7368 *yank_type = MLINE;
7369 break;
7370 case 'b': case Ctrl_V: // block-wise selection
7371 *yank_type = MBLOCK;
7372 if (VIM_ISDIGIT(stropt[1]))
7373 {
7374 ++stropt;
7375 *block_len = getdigits(&stropt) - 1;
7376 --stropt;
7377 }
7378 break;
7379 default:
7380 return FAIL;
7381 }
7382 *pp = stropt;
7383 return OK;
7384}
7385
7386/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 * "setreg()" function
7388 */
7389 static void
7390f_setreg(typval_T *argvars, typval_T *rettv)
7391{
7392 int regname;
7393 char_u *strregname;
7394 char_u *stropt;
7395 char_u *strval;
7396 int append;
7397 char_u yank_type;
7398 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007399 typval_T *regcontents;
7400 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007402 pointreg = 0;
7403 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404 block_len = -1;
7405 yank_type = MAUTO;
7406 append = FALSE;
7407
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007409 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410
7411 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007412 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 regname = *strregname;
7414 if (regname == 0 || regname == '@')
7415 regname = '"';
7416
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007417 if (argvars[1].v_type == VAR_DICT)
7418 {
7419 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007420 dictitem_T *di;
7421
7422 if (d == NULL || d->dv_hashtab.ht_used == 0)
7423 {
7424 // Empty dict, clear the register (like setreg(0, []))
7425 char_u *lstval[2] = {NULL, NULL};
7426 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7427 return;
7428 }
7429
7430 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007431 if (di != NULL)
7432 regcontents = &di->di_tv;
7433
7434 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7435 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007436 {
7437 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7438
7439 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007440 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007441 semsg(_(e_invargval), "value");
7442 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007443 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007444 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007445
7446 if (regname == '"')
7447 {
7448 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7449 if (stropt != NULL)
7450 {
7451 pointreg = *stropt;
7452 regname = pointreg;
7453 }
7454 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007455 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007456 pointreg = regname;
7457 }
7458 else
7459 regcontents = &argvars[1];
7460
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 if (argvars[2].v_type != VAR_UNKNOWN)
7462 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007463 if (yank_type != MAUTO)
7464 {
7465 semsg(_(e_toomanyarg), "setreg");
7466 return;
7467 }
7468
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007469 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007470 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007471 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 for (; *stropt != NUL; ++stropt)
7473 switch (*stropt)
7474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007475 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 append = TRUE;
7477 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007478 default:
7479 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 }
7481 }
7482
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007483 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 {
7485 char_u **lstval;
7486 char_u **allocval;
7487 char_u buf[NUMBUFLEN];
7488 char_u **curval;
7489 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007490 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 listitem_T *li;
7492 int len;
7493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007494 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 len = ll == NULL ? 0 : ll->lv_len;
7496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007497 // First half: use for pointers to result lines; second half: use for
7498 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007499 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 if (lstval == NULL)
7501 return;
7502 curval = lstval;
7503 allocval = lstval + len + 2;
7504 curallocval = allocval;
7505
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007506 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007508 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007509 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007511 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 if (strval == NULL)
7513 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007514 if (strval == buf)
7515 {
7516 // Need to make a copy, next tv_get_string_buf_chk() will
7517 // overwrite the string.
7518 strval = vim_strsave(buf);
7519 if (strval == NULL)
7520 goto free_lstval;
7521 *curallocval++ = strval;
7522 }
7523 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 }
7526 *curval++ = NULL;
7527
7528 write_reg_contents_lst(regname, lstval, -1,
7529 append, yank_type, block_len);
7530free_lstval:
7531 while (curallocval > allocval)
7532 vim_free(*--curallocval);
7533 vim_free(lstval);
7534 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007535 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007537 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007538 if (strval == NULL)
7539 return;
7540 write_reg_contents_ex(regname, strval, -1,
7541 append, yank_type, block_len);
7542 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007543 if (pointreg != 0)
7544 get_yank_register(pointreg, TRUE);
7545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546 rettv->vval.v_number = 0;
7547}
7548
7549/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007550 * "settagstack()" function
7551 */
7552 static void
7553f_settagstack(typval_T *argvars, typval_T *rettv)
7554{
7555 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7556 win_T *wp;
7557 dict_T *d;
7558 int action = 'r';
7559
7560 rettv->vval.v_number = -1;
7561
7562 // first argument: window number or id
7563 wp = find_win_by_nr_or_id(&argvars[0]);
7564 if (wp == NULL)
7565 return;
7566
7567 // second argument: dict with items to set in the tag stack
7568 if (argvars[1].v_type != VAR_DICT)
7569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007570 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007571 return;
7572 }
7573 d = argvars[1].vval.v_dict;
7574 if (d == NULL)
7575 return;
7576
7577 // third argument: action - 'a' for append and 'r' for replace.
7578 // default is to replace the stack.
7579 if (argvars[2].v_type == VAR_UNKNOWN)
7580 action = 'r';
7581 else if (argvars[2].v_type == VAR_STRING)
7582 {
7583 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007584 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007585 if (actstr == NULL)
7586 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007587 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7588 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007589 action = *actstr;
7590 else
7591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007592 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007593 return;
7594 }
7595 }
7596 else
7597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007599 return;
7600 }
7601
7602 if (set_tagstack(wp, d, action) == OK)
7603 rettv->vval.v_number = 0;
7604}
7605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606#ifdef FEAT_CRYPT
7607/*
7608 * "sha256({string})" function
7609 */
7610 static void
7611f_sha256(typval_T *argvars, typval_T *rettv)
7612{
7613 char_u *p;
7614
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007615 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 rettv->vval.v_string = vim_strsave(
7617 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7618 rettv->v_type = VAR_STRING;
7619}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007620#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621
7622/*
7623 * "shellescape({string})" function
7624 */
7625 static void
7626f_shellescape(typval_T *argvars, typval_T *rettv)
7627{
Bram Moolenaar20615522017-06-05 18:46:26 +02007628 int do_special = non_zero_arg(&argvars[1]);
7629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007631 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632 rettv->v_type = VAR_STRING;
7633}
7634
7635/*
7636 * shiftwidth() function
7637 */
7638 static void
7639f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7640{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007641 rettv->vval.v_number = 0;
7642
7643 if (argvars[0].v_type != VAR_UNKNOWN)
7644 {
7645 long col;
7646
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007647 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007648 if (col < 0)
7649 return; // type error; errmsg already given
7650#ifdef FEAT_VARTABS
7651 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7652 return;
7653#endif
7654 }
7655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 rettv->vval.v_number = get_sw_value(curbuf);
7657}
7658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659#ifdef FEAT_FLOAT
7660/*
7661 * "sin()" function
7662 */
7663 static void
7664f_sin(typval_T *argvars, typval_T *rettv)
7665{
7666 float_T f = 0.0;
7667
7668 rettv->v_type = VAR_FLOAT;
7669 if (get_float_arg(argvars, &f) == OK)
7670 rettv->vval.v_float = sin(f);
7671 else
7672 rettv->vval.v_float = 0.0;
7673}
7674
7675/*
7676 * "sinh()" function
7677 */
7678 static void
7679f_sinh(typval_T *argvars, typval_T *rettv)
7680{
7681 float_T f = 0.0;
7682
7683 rettv->v_type = VAR_FLOAT;
7684 if (get_float_arg(argvars, &f) == OK)
7685 rettv->vval.v_float = sinh(f);
7686 else
7687 rettv->vval.v_float = 0.0;
7688}
7689#endif
7690
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691/*
7692 * "soundfold({word})" function
7693 */
7694 static void
7695f_soundfold(typval_T *argvars, typval_T *rettv)
7696{
7697 char_u *s;
7698
7699 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007700 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701#ifdef FEAT_SPELL
7702 rettv->vval.v_string = eval_soundfold(s);
7703#else
7704 rettv->vval.v_string = vim_strsave(s);
7705#endif
7706}
7707
7708/*
7709 * "spellbadword()" function
7710 */
7711 static void
7712f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7713{
7714 char_u *word = (char_u *)"";
7715 hlf_T attr = HLF_COUNT;
7716 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007717#ifdef FEAT_SPELL
7718 int wo_spell_save = curwin->w_p_spell;
7719
7720 if (!curwin->w_p_spell)
7721 {
7722 did_set_spelllang(curwin);
7723 curwin->w_p_spell = TRUE;
7724 }
7725
7726 if (*curwin->w_s->b_p_spl == NUL)
7727 {
7728 emsg(_(e_no_spell));
7729 curwin->w_p_spell = wo_spell_save;
7730 return;
7731 }
7732#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733
7734 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007735 {
7736#ifdef FEAT_SPELL
7737 curwin->w_p_spell = wo_spell_save;
7738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007739 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007740 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741
7742#ifdef FEAT_SPELL
7743 if (argvars[0].v_type == VAR_UNKNOWN)
7744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007745 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007746 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7747 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007748 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007750 curwin->w_set_curswant = TRUE;
7751 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007753 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007755 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 int capcol = -1;
7757
7758 if (str != NULL)
7759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007760 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 while (*str != NUL)
7762 {
7763 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7764 if (attr != HLF_COUNT)
7765 {
7766 word = str;
7767 break;
7768 }
7769 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007770 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007771 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 }
7773 }
7774 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007775 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776#endif
7777
7778 list_append_string(rettv->vval.v_list, word, len);
7779 list_append_string(rettv->vval.v_list, (char_u *)(
7780 attr == HLF_SPB ? "bad" :
7781 attr == HLF_SPR ? "rare" :
7782 attr == HLF_SPL ? "local" :
7783 attr == HLF_SPC ? "caps" :
7784 ""), -1);
7785}
7786
7787/*
7788 * "spellsuggest()" function
7789 */
7790 static void
7791f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7792{
7793#ifdef FEAT_SPELL
7794 char_u *str;
7795 int typeerr = FALSE;
7796 int maxcount;
7797 garray_T ga;
7798 int i;
7799 listitem_T *li;
7800 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007801 int wo_spell_save = curwin->w_p_spell;
7802
7803 if (!curwin->w_p_spell)
7804 {
7805 did_set_spelllang(curwin);
7806 curwin->w_p_spell = TRUE;
7807 }
7808
7809 if (*curwin->w_s->b_p_spl == NUL)
7810 {
7811 emsg(_(e_no_spell));
7812 curwin->w_p_spell = wo_spell_save;
7813 return;
7814 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815#endif
7816
7817 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007818 {
7819#ifdef FEAT_SPELL
7820 curwin->w_p_spell = wo_spell_save;
7821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007823 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824
7825#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007826 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007828 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 if (argvars[1].v_type != VAR_UNKNOWN)
7830 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007831 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 if (maxcount <= 0)
7833 return;
7834 if (argvars[2].v_type != VAR_UNKNOWN)
7835 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02007836 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 if (typeerr)
7838 return;
7839 }
7840 }
7841 else
7842 maxcount = 25;
7843
7844 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7845
7846 for (i = 0; i < ga.ga_len; ++i)
7847 {
7848 str = ((char_u **)ga.ga_data)[i];
7849
7850 li = listitem_alloc();
7851 if (li == NULL)
7852 vim_free(str);
7853 else
7854 {
7855 li->li_tv.v_type = VAR_STRING;
7856 li->li_tv.v_lock = 0;
7857 li->li_tv.vval.v_string = str;
7858 list_append(rettv->vval.v_list, li);
7859 }
7860 }
7861 ga_clear(&ga);
7862 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007863 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864#endif
7865}
7866
7867 static void
7868f_split(typval_T *argvars, typval_T *rettv)
7869{
7870 char_u *str;
7871 char_u *end;
7872 char_u *pat = NULL;
7873 regmatch_T regmatch;
7874 char_u patbuf[NUMBUFLEN];
7875 char_u *save_cpo;
7876 int match;
7877 colnr_T col = 0;
7878 int keepempty = FALSE;
7879 int typeerr = FALSE;
7880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007881 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 save_cpo = p_cpo;
7883 p_cpo = (char_u *)"";
7884
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007885 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 if (argvars[1].v_type != VAR_UNKNOWN)
7887 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007888 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 if (pat == NULL)
7890 typeerr = TRUE;
7891 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02007892 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 }
7894 if (pat == NULL || *pat == NUL)
7895 pat = (char_u *)"[\\x01- ]\\+";
7896
7897 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007898 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007900 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901
7902 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7903 if (regmatch.regprog != NULL)
7904 {
7905 regmatch.rm_ic = FALSE;
7906 while (*str != NUL || keepempty)
7907 {
7908 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007909 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 else
7911 match = vim_regexec_nl(&regmatch, str, col);
7912 if (match)
7913 end = regmatch.startp[0];
7914 else
7915 end = str + STRLEN(str);
7916 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7917 && *str != NUL && match && end < regmatch.endp[0]))
7918 {
7919 if (list_append_string(rettv->vval.v_list, str,
7920 (int)(end - str)) == FAIL)
7921 break;
7922 }
7923 if (!match)
7924 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007925 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 if (regmatch.endp[0] > str)
7927 col = 0;
7928 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007929 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 str = regmatch.endp[0];
7932 }
7933
7934 vim_regfree(regmatch.regprog);
7935 }
7936
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007937theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 p_cpo = save_cpo;
7939}
7940
7941#ifdef FEAT_FLOAT
7942/*
7943 * "sqrt()" function
7944 */
7945 static void
7946f_sqrt(typval_T *argvars, typval_T *rettv)
7947{
7948 float_T f = 0.0;
7949
7950 rettv->v_type = VAR_FLOAT;
7951 if (get_float_arg(argvars, &f) == OK)
7952 rettv->vval.v_float = sqrt(f);
7953 else
7954 rettv->vval.v_float = 0.0;
7955}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007956#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007958#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007959/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 * "str2float()" function
7961 */
7962 static void
7963f_str2float(typval_T *argvars, typval_T *rettv)
7964{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007965 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007966 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967
Bram Moolenaar08243d22017-01-10 16:12:29 +01007968 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 p = skipwhite(p + 1);
7970 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007971 if (isneg)
7972 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 rettv->v_type = VAR_FLOAT;
7974}
7975#endif
7976
7977/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007978 * "str2list()" function
7979 */
7980 static void
7981f_str2list(typval_T *argvars, typval_T *rettv)
7982{
7983 char_u *p;
7984 int utf8 = FALSE;
7985
7986 if (rettv_list_alloc(rettv) == FAIL)
7987 return;
7988
7989 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02007990 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02007991
7992 p = tv_get_string(&argvars[0]);
7993
7994 if (has_mbyte || utf8)
7995 {
7996 int (*ptr2len)(char_u *);
7997 int (*ptr2char)(char_u *);
7998
7999 if (utf8 || enc_utf8)
8000 {
8001 ptr2len = utf_ptr2len;
8002 ptr2char = utf_ptr2char;
8003 }
8004 else
8005 {
8006 ptr2len = mb_ptr2len;
8007 ptr2char = mb_ptr2char;
8008 }
8009
8010 for ( ; *p != NUL; p += (*ptr2len)(p))
8011 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8012 }
8013 else
8014 for ( ; *p != NUL; ++p)
8015 list_append_number(rettv->vval.v_list, *p);
8016}
8017
8018/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 * "str2nr()" function
8020 */
8021 static void
8022f_str2nr(typval_T *argvars, typval_T *rettv)
8023{
8024 int base = 10;
8025 char_u *p;
8026 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008027 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008028 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029
8030 if (argvars[1].v_type != VAR_UNKNOWN)
8031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008032 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 if (base != 2 && base != 8 && base != 10 && base != 16)
8034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008035 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 return;
8037 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008038 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008039 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 }
8041
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008042 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008043 isneg = (*p == '-');
8044 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 p = skipwhite(p + 1);
8046 switch (base)
8047 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008048 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008049 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008050 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008052 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8053 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008054 if (isneg)
8055 rettv->vval.v_number = -n;
8056 else
8057 rettv->vval.v_number = n;
8058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059}
8060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061/*
8062 * "strgetchar()" function
8063 */
8064 static void
8065f_strgetchar(typval_T *argvars, typval_T *rettv)
8066{
8067 char_u *str;
8068 int len;
8069 int error = FALSE;
8070 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008071 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072
8073 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008074 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 if (str == NULL)
8076 return;
8077 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008078 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 if (error)
8080 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081
Bram Moolenaar13505972019-01-24 15:04:48 +01008082 while (charidx >= 0 && byteidx < len)
8083 {
8084 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008086 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8087 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008089 --charidx;
8090 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092}
8093
8094/*
8095 * "stridx()" function
8096 */
8097 static void
8098f_stridx(typval_T *argvars, typval_T *rettv)
8099{
8100 char_u buf[NUMBUFLEN];
8101 char_u *needle;
8102 char_u *haystack;
8103 char_u *save_haystack;
8104 char_u *pos;
8105 int start_idx;
8106
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008107 needle = tv_get_string_chk(&argvars[1]);
8108 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 rettv->vval.v_number = -1;
8110 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008111 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112
8113 if (argvars[2].v_type != VAR_UNKNOWN)
8114 {
8115 int error = FALSE;
8116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008117 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (error || start_idx >= (int)STRLEN(haystack))
8119 return;
8120 if (start_idx >= 0)
8121 haystack += start_idx;
8122 }
8123
8124 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8125 if (pos != NULL)
8126 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8127}
8128
8129/*
8130 * "string()" function
8131 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008132 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133f_string(typval_T *argvars, typval_T *rettv)
8134{
8135 char_u *tofree;
8136 char_u numbuf[NUMBUFLEN];
8137
8138 rettv->v_type = VAR_STRING;
8139 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8140 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008141 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 if (rettv->vval.v_string != NULL && tofree == NULL)
8143 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8144}
8145
8146/*
8147 * "strlen()" function
8148 */
8149 static void
8150f_strlen(typval_T *argvars, typval_T *rettv)
8151{
8152 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008153 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154}
8155
8156/*
8157 * "strchars()" function
8158 */
8159 static void
8160f_strchars(typval_T *argvars, typval_T *rettv)
8161{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar3986b942020-09-06 16:09:04 +02008163 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 varnumber_T len = 0;
8165 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166
8167 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008168 skipcc = (int)tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008170 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171 else
8172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8174 while (*s != NUL)
8175 {
8176 func_mb_ptr2char_adv(&s);
8177 ++len;
8178 }
8179 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 }
8181}
8182
8183/*
8184 * "strdisplaywidth()" function
8185 */
8186 static void
8187f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8188{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008189 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 int col = 0;
8191
8192 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008193 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194
8195 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8196}
8197
8198/*
8199 * "strwidth()" function
8200 */
8201 static void
8202f_strwidth(typval_T *argvars, typval_T *rettv)
8203{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008204 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205
Bram Moolenaar13505972019-01-24 15:04:48 +01008206 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207}
8208
8209/*
8210 * "strcharpart()" function
8211 */
8212 static void
8213f_strcharpart(typval_T *argvars, typval_T *rettv)
8214{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 char_u *p;
8216 int nchar;
8217 int nbyte = 0;
8218 int charlen;
8219 int len = 0;
8220 int slen;
8221 int error = FALSE;
8222
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008223 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008224 slen = (int)STRLEN(p);
8225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008226 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227 if (!error)
8228 {
8229 if (nchar > 0)
8230 while (nchar > 0 && nbyte < slen)
8231 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008232 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 --nchar;
8234 }
8235 else
8236 nbyte = nchar;
8237 if (argvars[2].v_type != VAR_UNKNOWN)
8238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008239 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008240 while (charlen > 0 && nbyte + len < slen)
8241 {
8242 int off = nbyte + len;
8243
8244 if (off < 0)
8245 len += 1;
8246 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008247 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 --charlen;
8249 }
8250 }
8251 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008252 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 }
8254
8255 /*
8256 * Only return the overlap between the specified part and the actual
8257 * string.
8258 */
8259 if (nbyte < 0)
8260 {
8261 len += nbyte;
8262 nbyte = 0;
8263 }
8264 else if (nbyte > slen)
8265 nbyte = slen;
8266 if (len < 0)
8267 len = 0;
8268 else if (nbyte + len > slen)
8269 len = slen - nbyte;
8270
8271 rettv->v_type = VAR_STRING;
8272 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273}
8274
8275/*
8276 * "strpart()" function
8277 */
8278 static void
8279f_strpart(typval_T *argvars, typval_T *rettv)
8280{
8281 char_u *p;
8282 int n;
8283 int len;
8284 int slen;
8285 int error = FALSE;
8286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008287 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 slen = (int)STRLEN(p);
8289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008290 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 if (error)
8292 len = 0;
8293 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008294 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008296 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008298 // Only return the overlap between the specified part and the actual
8299 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 if (n < 0)
8301 {
8302 len += n;
8303 n = 0;
8304 }
8305 else if (n > slen)
8306 n = slen;
8307 if (len < 0)
8308 len = 0;
8309 else if (n + len > slen)
8310 len = slen - n;
8311
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008312 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8313 {
8314 int off;
8315
8316 // length in characters
8317 for (off = n; off < slen && len > 0; --len)
8318 off += mb_ptr2len(p + off);
8319 len = off - n;
8320 }
8321
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 rettv->v_type = VAR_STRING;
8323 rettv->vval.v_string = vim_strnsave(p + n, len);
8324}
8325
8326/*
8327 * "strridx()" function
8328 */
8329 static void
8330f_strridx(typval_T *argvars, typval_T *rettv)
8331{
8332 char_u buf[NUMBUFLEN];
8333 char_u *needle;
8334 char_u *haystack;
8335 char_u *rest;
8336 char_u *lastmatch = NULL;
8337 int haystack_len, end_idx;
8338
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 needle = tv_get_string_chk(&argvars[1]);
8340 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341
8342 rettv->vval.v_number = -1;
8343 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008344 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008345
8346 haystack_len = (int)STRLEN(haystack);
8347 if (argvars[2].v_type != VAR_UNKNOWN)
8348 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008349 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008350 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008352 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 }
8354 else
8355 end_idx = haystack_len;
8356
8357 if (*needle == NUL)
8358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008359 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 lastmatch = haystack + end_idx;
8361 }
8362 else
8363 {
8364 for (rest = haystack; *rest != '\0'; ++rest)
8365 {
8366 rest = (char_u *)strstr((char *)rest, (char *)needle);
8367 if (rest == NULL || rest > haystack + end_idx)
8368 break;
8369 lastmatch = rest;
8370 }
8371 }
8372
8373 if (lastmatch == NULL)
8374 rettv->vval.v_number = -1;
8375 else
8376 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8377}
8378
8379/*
8380 * "strtrans()" function
8381 */
8382 static void
8383f_strtrans(typval_T *argvars, typval_T *rettv)
8384{
8385 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008386 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387}
8388
8389/*
8390 * "submatch()" function
8391 */
8392 static void
8393f_submatch(typval_T *argvars, typval_T *rettv)
8394{
8395 int error = FALSE;
8396 int no;
8397 int retList = 0;
8398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 if (error)
8401 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008402 if (no < 0 || no >= NSUBEXP)
8403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008404 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008405 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02008408 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 if (error)
8410 return;
8411
8412 if (retList == 0)
8413 {
8414 rettv->v_type = VAR_STRING;
8415 rettv->vval.v_string = reg_submatch(no);
8416 }
8417 else
8418 {
8419 rettv->v_type = VAR_LIST;
8420 rettv->vval.v_list = reg_submatch_list(no);
8421 }
8422}
8423
8424/*
8425 * "substitute()" function
8426 */
8427 static void
8428f_substitute(typval_T *argvars, typval_T *rettv)
8429{
8430 char_u patbuf[NUMBUFLEN];
8431 char_u subbuf[NUMBUFLEN];
8432 char_u flagsbuf[NUMBUFLEN];
8433
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008434 char_u *str = tv_get_string_chk(&argvars[0]);
8435 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008436 char_u *sub = NULL;
8437 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008438 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008440 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8441 expr = &argvars[2];
8442 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008443 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008445 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008446 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8447 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008448 rettv->vval.v_string = NULL;
8449 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008450 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451}
8452
8453/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008454 * "swapinfo(swap_filename)" function
8455 */
8456 static void
8457f_swapinfo(typval_T *argvars, typval_T *rettv)
8458{
8459 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008460 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008461}
8462
8463/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008464 * "swapname(expr)" function
8465 */
8466 static void
8467f_swapname(typval_T *argvars, typval_T *rettv)
8468{
8469 buf_T *buf;
8470
8471 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008472 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008473 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8474 || buf->b_ml.ml_mfp->mf_fname == NULL)
8475 rettv->vval.v_string = NULL;
8476 else
8477 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8478}
8479
8480/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 * "synID(lnum, col, trans)" function
8482 */
8483 static void
8484f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8485{
8486 int id = 0;
8487#ifdef FEAT_SYN_HL
8488 linenr_T lnum;
8489 colnr_T col;
8490 int trans;
8491 int transerr = FALSE;
8492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008493 lnum = tv_get_lnum(argvars); // -1 on type error
8494 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02008495 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496
8497 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8498 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8499 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8500#endif
8501
8502 rettv->vval.v_number = id;
8503}
8504
8505/*
8506 * "synIDattr(id, what [, mode])" function
8507 */
8508 static void
8509f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8510{
8511 char_u *p = NULL;
8512#ifdef FEAT_SYN_HL
8513 int id;
8514 char_u *what;
8515 char_u *mode;
8516 char_u modebuf[NUMBUFLEN];
8517 int modec;
8518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008519 id = (int)tv_get_number(&argvars[0]);
8520 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 if (argvars[2].v_type != VAR_UNKNOWN)
8522 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008523 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 modec = TOLOWER_ASC(mode[0]);
8525 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008526 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008527 }
8528 else
8529 {
8530#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8531 if (USE_24BIT)
8532 modec = 'g';
8533 else
8534#endif
8535 if (t_colors > 1)
8536 modec = 'c';
8537 else
8538 modec = 't';
8539 }
8540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 switch (TOLOWER_ASC(what[0]))
8542 {
8543 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008544 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008546 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 p = highlight_has_attr(id, HL_BOLD, modec);
8548 break;
8549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 p = highlight_color(id, what, modec);
8552 break;
8553
8554 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008555 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008557 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 p = highlight_has_attr(id, HL_ITALIC, modec);
8559 break;
8560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008561 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008562 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 break;
8564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008565 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 p = highlight_has_attr(id, HL_INVERSE, modec);
8567 break;
8568
8569 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008570 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008572 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008573 else if (TOLOWER_ASC(what[1]) == 't' &&
8574 TOLOWER_ASC(what[2]) == 'r')
8575 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008576 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 p = highlight_has_attr(id, HL_STANDOUT, modec);
8578 break;
8579
8580 case 'u':
8581 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008582 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8584 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008585 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8587 break;
8588 }
8589
8590 if (p != NULL)
8591 p = vim_strsave(p);
8592#endif
8593 rettv->v_type = VAR_STRING;
8594 rettv->vval.v_string = p;
8595}
8596
8597/*
8598 * "synIDtrans(id)" function
8599 */
8600 static void
8601f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8602{
8603 int id;
8604
8605#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008606 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607
8608 if (id > 0)
8609 id = syn_get_final_id(id);
8610 else
8611#endif
8612 id = 0;
8613
8614 rettv->vval.v_number = id;
8615}
8616
8617/*
8618 * "synconcealed(lnum, col)" function
8619 */
8620 static void
8621f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8622{
8623#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8624 linenr_T lnum;
8625 colnr_T col;
8626 int syntax_flags = 0;
8627 int cchar;
8628 int matchid = 0;
8629 char_u str[NUMBUFLEN];
8630#endif
8631
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008632 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633
8634#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008635 lnum = tv_get_lnum(argvars); // -1 on type error
8636 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637
Bram Moolenaara80faa82020-04-12 19:37:17 +02008638 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639
8640 if (rettv_list_alloc(rettv) != FAIL)
8641 {
8642 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8643 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8644 && curwin->w_p_cole > 0)
8645 {
8646 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8647 syntax_flags = get_syntax_info(&matchid);
8648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008649 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8651 {
8652 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008653 if (cchar == NUL && curwin->w_p_cole == 1)
8654 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 if (cchar != NUL)
8656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 if (has_mbyte)
8658 (*mb_char2bytes)(cchar, str);
8659 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 str[0] = cchar;
8661 }
8662 }
8663 }
8664
8665 list_append_number(rettv->vval.v_list,
8666 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008667 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 list_append_string(rettv->vval.v_list, str, -1);
8669 list_append_number(rettv->vval.v_list, matchid);
8670 }
8671#endif
8672}
8673
8674/*
8675 * "synstack(lnum, col)" function
8676 */
8677 static void
8678f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8679{
8680#ifdef FEAT_SYN_HL
8681 linenr_T lnum;
8682 colnr_T col;
8683 int i;
8684 int id;
8685#endif
8686
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008687 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688
8689#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008690 lnum = tv_get_lnum(argvars); // -1 on type error
8691 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692
8693 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8694 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8695 && rettv_list_alloc(rettv) != FAIL)
8696 {
8697 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8698 for (i = 0; ; ++i)
8699 {
8700 id = syn_get_stack_item(i);
8701 if (id < 0)
8702 break;
8703 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8704 break;
8705 }
8706 }
8707#endif
8708}
8709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710/*
8711 * "tabpagebuflist()" function
8712 */
8713 static void
8714f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8715{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 tabpage_T *tp;
8717 win_T *wp = NULL;
8718
8719 if (argvars[0].v_type == VAR_UNKNOWN)
8720 wp = firstwin;
8721 else
8722 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 if (tp != NULL)
8725 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8726 }
8727 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8728 {
8729 for (; wp != NULL; wp = wp->w_next)
8730 if (list_append_number(rettv->vval.v_list,
8731 wp->w_buffer->b_fnum) == FAIL)
8732 break;
8733 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734}
8735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 * "tagfiles()" function
8738 */
8739 static void
8740f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8741{
8742 char_u *fname;
8743 tagname_T tn;
8744 int first;
8745
8746 if (rettv_list_alloc(rettv) == FAIL)
8747 return;
8748 fname = alloc(MAXPATHL);
8749 if (fname == NULL)
8750 return;
8751
8752 for (first = TRUE; ; first = FALSE)
8753 if (get_tagfname(&tn, first, fname) == FAIL
8754 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8755 break;
8756 tagname_free(&tn);
8757 vim_free(fname);
8758}
8759
8760/*
8761 * "taglist()" function
8762 */
8763 static void
8764f_taglist(typval_T *argvars, typval_T *rettv)
8765{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008766 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 char_u *tag_pattern;
8768
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008769 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770
8771 rettv->vval.v_number = FALSE;
8772 if (*tag_pattern == NUL)
8773 return;
8774
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008776 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008778 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779}
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781#ifdef FEAT_FLOAT
8782/*
8783 * "tan()" function
8784 */
8785 static void
8786f_tan(typval_T *argvars, typval_T *rettv)
8787{
8788 float_T f = 0.0;
8789
8790 rettv->v_type = VAR_FLOAT;
8791 if (get_float_arg(argvars, &f) == OK)
8792 rettv->vval.v_float = tan(f);
8793 else
8794 rettv->vval.v_float = 0.0;
8795}
8796
8797/*
8798 * "tanh()" function
8799 */
8800 static void
8801f_tanh(typval_T *argvars, typval_T *rettv)
8802{
8803 float_T f = 0.0;
8804
8805 rettv->v_type = VAR_FLOAT;
8806 if (get_float_arg(argvars, &f) == OK)
8807 rettv->vval.v_float = tanh(f);
8808 else
8809 rettv->vval.v_float = 0.0;
8810}
8811#endif
8812
8813/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814 * "tolower(string)" function
8815 */
8816 static void
8817f_tolower(typval_T *argvars, typval_T *rettv)
8818{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008820 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821}
8822
8823/*
8824 * "toupper(string)" function
8825 */
8826 static void
8827f_toupper(typval_T *argvars, typval_T *rettv)
8828{
8829 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831}
8832
8833/*
8834 * "tr(string, fromstr, tostr)" function
8835 */
8836 static void
8837f_tr(typval_T *argvars, typval_T *rettv)
8838{
8839 char_u *in_str;
8840 char_u *fromstr;
8841 char_u *tostr;
8842 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 int inlen;
8844 int fromlen;
8845 int tolen;
8846 int idx;
8847 char_u *cpstr;
8848 int cplen;
8849 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850 char_u buf[NUMBUFLEN];
8851 char_u buf2[NUMBUFLEN];
8852 garray_T ga;
8853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008854 in_str = tv_get_string(&argvars[0]);
8855 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8856 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008858 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 rettv->v_type = VAR_STRING;
8860 rettv->vval.v_string = NULL;
8861 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008862 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 ga_init2(&ga, (int)sizeof(char), 80);
8864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008866 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 if (STRLEN(fromstr) != STRLEN(tostr))
8868 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008870 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871 ga_clear(&ga);
8872 return;
8873 }
8874
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008875 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 while (*in_str != NUL)
8877 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 if (has_mbyte)
8879 {
8880 inlen = (*mb_ptr2len)(in_str);
8881 cpstr = in_str;
8882 cplen = inlen;
8883 idx = 0;
8884 for (p = fromstr; *p != NUL; p += fromlen)
8885 {
8886 fromlen = (*mb_ptr2len)(p);
8887 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8888 {
8889 for (p = tostr; *p != NUL; p += tolen)
8890 {
8891 tolen = (*mb_ptr2len)(p);
8892 if (idx-- == 0)
8893 {
8894 cplen = tolen;
8895 cpstr = p;
8896 break;
8897 }
8898 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008899 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900 goto error;
8901 break;
8902 }
8903 ++idx;
8904 }
8905
8906 if (first && cpstr == in_str)
8907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008908 // Check that fromstr and tostr have the same number of
8909 // (multi-byte) characters. Done only once when a character
8910 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 first = FALSE;
8912 for (p = tostr; *p != NUL; p += tolen)
8913 {
8914 tolen = (*mb_ptr2len)(p);
8915 --idx;
8916 }
8917 if (idx != 0)
8918 goto error;
8919 }
8920
8921 (void)ga_grow(&ga, cplen);
8922 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8923 ga.ga_len += cplen;
8924
8925 in_str += inlen;
8926 }
8927 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008929 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 p = vim_strchr(fromstr, *in_str);
8931 if (p != NULL)
8932 ga_append(&ga, tostr[p - fromstr]);
8933 else
8934 ga_append(&ga, *in_str);
8935 ++in_str;
8936 }
8937 }
8938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008939 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 (void)ga_grow(&ga, 1);
8941 ga_append(&ga, NUL);
8942
8943 rettv->vval.v_string = ga.ga_data;
8944}
8945
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008946/*
8947 * "trim({expr})" function
8948 */
8949 static void
8950f_trim(typval_T *argvars, typval_T *rettv)
8951{
8952 char_u buf1[NUMBUFLEN];
8953 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008954 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008955 char_u *mask = NULL;
8956 char_u *tail;
8957 char_u *prev;
8958 char_u *p;
8959 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008960 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008961
8962 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008963 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008964 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008965 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008966
8967 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008969 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008970
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008971 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008972 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008973 int error = 0;
8974
8975 // leading or trailing characters to trim
8976 dir = (int)tv_get_number_chk(&argvars[2], &error);
8977 if (error)
8978 return;
8979 if (dir < 0 || dir > 2)
8980 {
8981 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8982 return;
8983 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008984 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008985 }
8986
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008987 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008988 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008989 // Trim leading characters
8990 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008991 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008992 c1 = PTR2CHAR(head);
8993 if (mask == NULL)
8994 {
8995 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008996 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008997 }
8998 else
8999 {
9000 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9001 if (c1 == PTR2CHAR(p))
9002 break;
9003 if (*p == NUL)
9004 break;
9005 }
9006 MB_PTR_ADV(head);
9007 }
9008 }
9009
9010 tail = head + STRLEN(head);
9011 if (dir == 0 || dir == 2)
9012 {
9013 // Trim trailing characters
9014 for (; tail > head; tail = prev)
9015 {
9016 prev = tail;
9017 MB_PTR_BACK(head, prev);
9018 c1 = PTR2CHAR(prev);
9019 if (mask == NULL)
9020 {
9021 if (c1 > ' ' && c1 != 0xa0)
9022 break;
9023 }
9024 else
9025 {
9026 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9027 if (c1 == PTR2CHAR(p))
9028 break;
9029 if (*p == NUL)
9030 break;
9031 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009032 }
9033 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009034 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009035}
9036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037#ifdef FEAT_FLOAT
9038/*
9039 * "trunc({float})" function
9040 */
9041 static void
9042f_trunc(typval_T *argvars, typval_T *rettv)
9043{
9044 float_T f = 0.0;
9045
9046 rettv->v_type = VAR_FLOAT;
9047 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009048 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9050 else
9051 rettv->vval.v_float = 0.0;
9052}
9053#endif
9054
9055/*
9056 * "type(expr)" function
9057 */
9058 static void
9059f_type(typval_T *argvars, typval_T *rettv)
9060{
9061 int n = -1;
9062
9063 switch (argvars[0].v_type)
9064 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009065 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9066 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009068 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9069 case VAR_LIST: n = VAR_TYPE_LIST; break;
9070 case VAR_DICT: n = VAR_TYPE_DICT; break;
9071 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9072 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9073 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009074 case VAR_JOB: n = VAR_TYPE_JOB; break;
9075 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009076 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009078 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009079 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009080 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 n = -1;
9082 break;
9083 }
9084 rettv->vval.v_number = n;
9085}
9086
9087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 * "virtcol(string)" function
9089 */
9090 static void
9091f_virtcol(typval_T *argvars, typval_T *rettv)
9092{
9093 colnr_T vcol = 0;
9094 pos_T *fp;
9095 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009096 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097
9098 fp = var2fpos(&argvars[0], FALSE, &fnum);
9099 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9100 && fnum == curbuf->b_fnum)
9101 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009102 // Limit the column to a valid value, getvvcol() doesn't check.
9103 if (fp->col < 0)
9104 fp->col = 0;
9105 else
9106 {
9107 len = (int)STRLEN(ml_get(fp->lnum));
9108 if (fp->col > len)
9109 fp->col = len;
9110 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 getvvcol(curwin, fp, NULL, NULL, &vcol);
9112 ++vcol;
9113 }
9114
9115 rettv->vval.v_number = vcol;
9116}
9117
9118/*
9119 * "visualmode()" function
9120 */
9121 static void
9122f_visualmode(typval_T *argvars, typval_T *rettv)
9123{
9124 char_u str[2];
9125
9126 rettv->v_type = VAR_STRING;
9127 str[0] = curbuf->b_visual_mode_eval;
9128 str[1] = NUL;
9129 rettv->vval.v_string = vim_strsave(str);
9130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009131 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132 if (non_zero_arg(&argvars[0]))
9133 curbuf->b_visual_mode_eval = NUL;
9134}
9135
9136/*
9137 * "wildmenumode()" function
9138 */
9139 static void
9140f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9141{
9142#ifdef FEAT_WILDMENU
9143 if (wild_menu_showing)
9144 rettv->vval.v_number = 1;
9145#endif
9146}
9147
9148/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009149 * "windowsversion()" function
9150 */
9151 static void
9152f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9153{
9154 rettv->v_type = VAR_STRING;
9155 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9156}
9157
9158/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159 * "wordcount()" function
9160 */
9161 static void
9162f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9163{
9164 if (rettv_dict_alloc(rettv) == FAIL)
9165 return;
9166 cursor_pos_info(rettv->vval.v_dict);
9167}
9168
9169/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170 * "xor(expr, expr)" function
9171 */
9172 static void
9173f_xor(typval_T *argvars, typval_T *rettv)
9174{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009175 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9176 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177}
9178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009179#endif // FEAT_EVAL