blob: c07b1ab77b3265826fcd6a526b32c059a5750acf [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 Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200385 static type_T *
386ret_remove(int argcount UNUSED, type_T **argtypes)
387{
388 if (argtypes[0]->tt_type == VAR_LIST
389 || argtypes[0]->tt_type == VAR_DICT)
390 return argtypes[0]->tt_member;
391 if (argtypes[0]->tt_type == VAR_BLOB)
392 return &t_number;
393 return &t_any;
394}
395
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100396static type_T *ret_f_function(int argcount, type_T **argtypes);
397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200398/*
399 * Array with names and number of arguments of all internal functions
400 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
401 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200402typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200404 char *f_name; // function name
405 char f_min_argc; // minimal number of arguments
406 char f_max_argc; // maximal number of arguments
407 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100408 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
409 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200411 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200412} funcentry_T;
413
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200414// values for f_argtype; zero means it cannot be used as a method
415#define FEARG_1 1 // base is the first argument
416#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200417#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200418#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200419#define FEARG_LAST 9 // base is the last argument
420
Bram Moolenaar15c47602020-03-26 22:16:48 +0100421#ifdef FEAT_FLOAT
422# define FLOAT_FUNC(name) name
423#else
424# define FLOAT_FUNC(name) NULL
425#endif
426#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
427# define MATH_FUNC(name) name
428#else
429# define MATH_FUNC(name) NULL
430#endif
431#ifdef FEAT_TIMERS
432# define TIMER_FUNC(name) name
433#else
434# define TIMER_FUNC(name) NULL
435#endif
436#ifdef FEAT_JOB_CHANNEL
437# define JOB_FUNC(name) name
438#else
439# define JOB_FUNC(name) NULL
440#endif
441#ifdef FEAT_PROP_POPUP
442# define PROP_FUNC(name) name
443#else
444# define PROP_FUNC(name) NULL
445#endif
446#ifdef FEAT_SIGNS
447# define SIGN_FUNC(name) name
448#else
449# define SIGN_FUNC(name) NULL
450#endif
451#ifdef FEAT_SOUND
452# define SOUND_FUNC(name) name
453#else
454# define SOUND_FUNC(name) NULL
455#endif
456#ifdef FEAT_TERMINAL
457# define TERM_FUNC(name) name
458#else
459# define TERM_FUNC(name) NULL
460#endif
461
Bram Moolenaarac92e252019-08-03 21:58:38 +0200462static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100464 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
465 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200466 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100467 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200468 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200469 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100470 {"argc", 0, 1, 0, ret_number, f_argc},
471 {"argidx", 0, 0, 0, ret_number, f_argidx},
472 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200473 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100474 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100475 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
476 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200477 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100478 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
479 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
480 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
481 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
482 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
483 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
484 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
485 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
486 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100487 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
488 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
489 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100490#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100491 f_balloon_gettext
492#else
493 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100494#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100495 },
496 {"balloon_show", 1, 1, FEARG_1, ret_void,
497#ifdef FEAT_BEVAL
498 f_balloon_show
499#else
500 NULL
501#endif
502 },
503 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
504#if defined(FEAT_BEVAL_TERM)
505 f_balloon_split
506#else
507 NULL
508#endif
509 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100510 {"browse", 4, 4, 0, ret_string, f_browse},
511 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
512 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
513 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
514 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
515 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
516 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
517 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
518 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
519 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
520 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
521 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
522 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
523 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
524 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
525 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
526 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
527 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100528 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
529 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
530 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
531 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
532 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
533 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
534 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
535 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
536 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
537 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
538 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
539 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
540 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
541 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
542 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
543 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
544 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
545 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
546 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100547 {"changenr", 0, 0, 0, ret_number, f_changenr},
548 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
549 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
550 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
551 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
552 {"col", 1, 1, FEARG_1, ret_number, f_col},
553 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
554 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
555 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
556 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
557 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200558 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100559 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
560 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100561 {"count", 2, 4, FEARG_1, ret_number, f_count},
562 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
563 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100564 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100565#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100566 f_debugbreak
567#else
568 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200569#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100570 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200571 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100572 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
573 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
574 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
575 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
576 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
577 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
578 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
579 {"environ", 0, 0, 0, ret_dict_string, f_environ},
580 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
581 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
582 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
583 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
584 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
585 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
586 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100587 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100588 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
589 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200590 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100591 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
592 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
593 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
594 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200595 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100596 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
597 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200598 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100599 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
600 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
601 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100602 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
603 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
604 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
605 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
606 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
607 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
608 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
609 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200610 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
612 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
613 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200614 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100615 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
616 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
617 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
618 {"getchar", 0, 1, 0, ret_number, f_getchar},
619 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
620 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
621 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
622 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
623 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
624 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
625 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
626 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
627 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
628 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
629 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
630 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
631 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
632 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
633 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
634 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
635 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
636 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200637 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200638 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100639 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
640 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
641 {"getpid", 0, 0, 0, ret_number, f_getpid},
642 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200643 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100644 {"getreg", 0, 3, FEARG_1, ret_string, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200645 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100646 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
647 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
648 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
649 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
650 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
651 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
652 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
653 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
654 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
655 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
656 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
657 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
658 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100659 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100660 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
661 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
662 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
663 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
664 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
665 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
666 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
667 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
668 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
669 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
670 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
671 {"hostname", 0, 0, 0, ret_string, f_hostname},
672 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
673 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
674 {"index", 2, 4, FEARG_1, ret_number, f_index},
675 {"input", 1, 3, FEARG_1, ret_string, f_input},
676 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
677 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
678 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
679 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
680 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200681 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100682 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
683 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
684 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100685 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100686 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100687 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100688 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100689 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
690 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
691 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
692 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
693 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
694 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100695 {"join", 1, 2, FEARG_1, ret_string, f_join},
696 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
697 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
698 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
699 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
700 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
701 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
702 {"len", 1, 1, FEARG_1, ret_number, f_len},
703 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
704 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
705 {"line", 1, 2, FEARG_1, ret_number, f_line},
706 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
707 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
708 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
709 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
710 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
711 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
712 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100713 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
714 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
715 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200716#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100717 f_luaeval
718#else
719 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200720#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100721 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100722 {"map", 2, 2, FEARG_1, ret_any, f_map},
723 {"maparg", 1, 4, FEARG_1, ret_string, f_maparg},
724 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200725 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100726 {"match", 2, 4, FEARG_1, ret_any, f_match},
727 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
728 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
729 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
730 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
731 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
732 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
733 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
734 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
735 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100736 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100737#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100738 f_menu_info
739#else
740 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100741#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100742 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100743 {"min", 1, 1, FEARG_1, ret_any, f_min},
744 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
745 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100746 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100748 f_mzeval
749#else
750 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200751#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100752 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100753 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
754 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
755 {"or", 2, 2, FEARG_1, ret_number, f_or},
756 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_perleval
760#else
761 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
764 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
765 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200766 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
768 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
769 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
770 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
771 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
772 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
773 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
774 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
775 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
776 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200777 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
779 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
780 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
781 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
782 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
783 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
784 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
785 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100786 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
787 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100788 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
789 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
790 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
791 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
792 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
793 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
794 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
795 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
796 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
797 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
798 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
799 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
800 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100801 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
802 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100803 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100805 f_py3eval
806#else
807 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100809 },
810 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100812 f_pyeval
813#else
814 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100816 },
817 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100819 f_pyxeval
820#else
821 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100822#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100823 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100824 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
825 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200826 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
827 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100828 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200829 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100830 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
831 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
832 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100833 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100834 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
835 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
836 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
837 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
838 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
839 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200840 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200841 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100842 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200843 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100844 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200845 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
847 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100848#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100849 f_rubyeval
850#else
851 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100852#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100853 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100854 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
855 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
856 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
857 {"screencol", 0, 0, 0, ret_number, f_screencol},
858 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
859 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
860 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200861 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200862 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100863 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
864 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
865 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200866 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100867 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
868 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
869 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
870 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
871 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
872 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
873 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
874 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
875 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
876 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
877 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
878 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
879 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
880 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
881 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
882 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
883 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
884 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100885 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100887 f_sha256
888#else
889 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100891 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100892 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
893 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
895 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
896 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
897 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
898 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
899 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
900 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
901 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
902 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200903 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100904 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
905 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200906 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100907 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
908 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
909 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
910 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100911 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
912 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
913 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
914 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100916 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
917 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100918 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100919 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
920 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
921 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
922 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
923 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100924 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100926 f_strftime
927#else
928 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100930 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100931 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
932 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
933 {"string", 1, 1, FEARG_1, ret_string, f_string},
934 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
935 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100936 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100937#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100938 f_strptime
939#else
940 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100941#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100942 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100943 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
944 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
945 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
946 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
947 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
948 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
949 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
950 {"synID", 3, 3, 0, ret_number, f_synID},
951 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
952 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
953 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
954 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
955 {"system", 1, 2, FEARG_1, ret_string, f_system},
956 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
957 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
958 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
959 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
960 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
961 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100962 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
963 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100964 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100965 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
966 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
967 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
968 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
969 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100970#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100971 f_term_getansicolors
972#else
973 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100975 },
976 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
977 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
978 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
979 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
980 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
981 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
982 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
983 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
984 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
985 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
986 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
987 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
988 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100989#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100990 f_term_setansicolors
991#else
992 NULL
993#endif
994 },
995 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
996 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
997 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
998 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
999 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1000 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001001 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001002 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1003 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1004 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1005 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1006 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1007 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1008 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1009 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001010 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001011 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001012 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001013 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001014 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001015 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001016 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1017 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1018 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1019 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001020 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001021#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001022 f_test_scrollbar
1023#else
1024 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001025#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001026 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001027 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1028 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1029 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1030 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
1031 {"test_void", 0, 0, 0, ret_any, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001032 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1033 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1034 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1035 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1036 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001037 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1038 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1039 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001040 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001041 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001042 {"type", 1, 1, FEARG_1, ret_number, f_type},
1043 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1044 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1045 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1046 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1047 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1048 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1049 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1050 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1051 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1052 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1053 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1054 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1055 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1056 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1057 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1058 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1059 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1060 {"wincol", 0, 0, 0, ret_number, f_wincol},
1061 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1062 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1063 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1064 {"winline", 0, 0, 0, ret_number, f_winline},
1065 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1066 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1067 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1068 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1069 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1070 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1071 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1072 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001073};
1074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001075/*
1076 * Function given to ExpandGeneric() to obtain the list of internal
1077 * or user defined function names.
1078 */
1079 char_u *
1080get_function_name(expand_T *xp, int idx)
1081{
1082 static int intidx = -1;
1083 char_u *name;
1084
1085 if (idx == 0)
1086 intidx = -1;
1087 if (intidx < 0)
1088 {
1089 name = get_user_func_name(xp, idx);
1090 if (name != NULL)
1091 return name;
1092 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001093 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001094 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001095 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001096 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001097 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001098 STRCAT(IObuff, ")");
1099 return IObuff;
1100 }
1101
1102 return NULL;
1103}
1104
1105/*
1106 * Function given to ExpandGeneric() to obtain the list of internal or
1107 * user defined variable or function names.
1108 */
1109 char_u *
1110get_expr_name(expand_T *xp, int idx)
1111{
1112 static int intidx = -1;
1113 char_u *name;
1114
1115 if (idx == 0)
1116 intidx = -1;
1117 if (intidx < 0)
1118 {
1119 name = get_function_name(xp, idx);
1120 if (name != NULL)
1121 return name;
1122 }
1123 return get_user_var_name(xp, ++intidx);
1124}
1125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001127 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001128 * Return index, or -1 if not found or "implemented" is TRUE and the function
1129 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001130 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001131 static int
1132find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001133{
1134 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001135 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136 int cmp;
1137 int x;
1138
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001139 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001140
1141 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001142 while (first <= last)
1143 {
1144 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001145 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001146 if (cmp < 0)
1147 last = x - 1;
1148 else if (cmp > 0)
1149 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001150 else if (implemented && global_functions[x].f_func == NULL)
1151 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001152 else
1153 return x;
1154 }
1155 return -1;
1156}
1157
Bram Moolenaar15c47602020-03-26 22:16:48 +01001158/*
1159 * Find internal function "name" in table "global_functions".
1160 * Return index, or -1 if not found or the function is not implemented.
1161 */
1162 int
1163find_internal_func(char_u *name)
1164{
1165 return find_internal_func_opt(name, TRUE);
1166}
1167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001169has_internal_func(char_u *name)
1170{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001171 return find_internal_func_opt(name, TRUE) >= 0;
1172}
1173
1174 static int
1175has_internal_func_name(char_u *name)
1176{
1177 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001178}
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 char *
1181internal_func_name(int idx)
1182{
1183 return global_functions[idx].f_name;
1184}
1185
1186 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001187internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001189 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190}
1191
1192/*
1193 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001194 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1195 * first argument, 2 if method base is second argument, etc. 9 if method base
1196 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 */
1198 int
1199check_internal_func(int idx, int argcount)
1200{
1201 int res;
1202 char *name;
1203
1204 if (argcount < global_functions[idx].f_min_argc)
1205 res = FCERR_TOOFEW;
1206 else if (argcount > global_functions[idx].f_max_argc)
1207 res = FCERR_TOOMANY;
1208 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001209 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
1211 name = internal_func_name(idx);
1212 if (res == FCERR_TOOMANY)
1213 semsg(_(e_toomanyarg), name);
1214 else
1215 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001216 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217}
1218
Bram Moolenaarac92e252019-08-03 21:58:38 +02001219 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001220call_internal_func(
1221 char_u *name,
1222 int argcount,
1223 typval_T *argvars,
1224 typval_T *rettv)
1225{
1226 int i;
1227
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001228 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001230 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001231 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001232 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001233 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001234 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001235 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001236 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001237 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001238}
1239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 void
1241call_internal_func_by_idx(
1242 int idx,
1243 typval_T *argvars,
1244 typval_T *rettv)
1245{
1246 global_functions[idx].f_func(argvars, rettv);
1247}
1248
Bram Moolenaarac92e252019-08-03 21:58:38 +02001249/*
1250 * Invoke a method for base->method().
1251 */
1252 int
1253call_internal_method(
1254 char_u *name,
1255 int argcount,
1256 typval_T *argvars,
1257 typval_T *rettv,
1258 typval_T *basetv)
1259{
1260 int i;
1261 int fi;
1262 typval_T argv[MAX_FUNC_ARGS + 1];
1263
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001264 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001265 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001266 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001267 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001268 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001269 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001270 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001271 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001272 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001273
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001274 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001275 {
1276 // base value goes last
1277 for (i = 0; i < argcount; ++i)
1278 argv[i] = argvars[i];
1279 argv[argcount] = *basetv;
1280 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001281 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001282 {
1283 // base value goes second
1284 argv[0] = argvars[0];
1285 argv[1] = *basetv;
1286 for (i = 1; i < argcount; ++i)
1287 argv[i + 1] = argvars[i];
1288 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001289 else if (global_functions[fi].f_argtype == FEARG_3)
1290 {
1291 // base value goes third
1292 argv[0] = argvars[0];
1293 argv[1] = argvars[1];
1294 argv[2] = *basetv;
1295 for (i = 2; i < argcount; ++i)
1296 argv[i + 1] = argvars[i];
1297 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001298 else if (global_functions[fi].f_argtype == FEARG_4)
1299 {
1300 // base value goes fourth
1301 argv[0] = argvars[0];
1302 argv[1] = argvars[1];
1303 argv[2] = argvars[2];
1304 argv[3] = *basetv;
1305 for (i = 3; i < argcount; ++i)
1306 argv[i + 1] = argvars[i];
1307 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001308 else
1309 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001310 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001311 argv[0] = *basetv;
1312 for (i = 0; i < argcount; ++i)
1313 argv[i + 1] = argvars[i];
1314 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001315 argv[argcount + 1].v_type = VAR_UNKNOWN;
1316
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001317 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001318 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319}
1320
1321/*
1322 * Return TRUE for a non-zero Number and a non-empty String.
1323 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001324 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001325non_zero_arg(typval_T *argvars)
1326{
1327 return ((argvars[0].v_type == VAR_NUMBER
1328 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001329 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 && argvars[0].vval.v_number == VVAL_TRUE)
1331 || (argvars[0].v_type == VAR_STRING
1332 && argvars[0].vval.v_string != NULL
1333 && *argvars[0].vval.v_string != NUL));
1334}
1335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001336#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337/*
1338 * Get the float value of "argvars[0]" into "f".
1339 * Returns FAIL when the argument is not a Number or Float.
1340 */
1341 static int
1342get_float_arg(typval_T *argvars, float_T *f)
1343{
1344 if (argvars[0].v_type == VAR_FLOAT)
1345 {
1346 *f = argvars[0].vval.v_float;
1347 return OK;
1348 }
1349 if (argvars[0].v_type == VAR_NUMBER)
1350 {
1351 *f = (float_T)argvars[0].vval.v_number;
1352 return OK;
1353 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001354 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 return FAIL;
1356}
1357
1358/*
1359 * "abs(expr)" function
1360 */
1361 static void
1362f_abs(typval_T *argvars, typval_T *rettv)
1363{
1364 if (argvars[0].v_type == VAR_FLOAT)
1365 {
1366 rettv->v_type = VAR_FLOAT;
1367 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1368 }
1369 else
1370 {
1371 varnumber_T n;
1372 int error = FALSE;
1373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001374 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 if (error)
1376 rettv->vval.v_number = -1;
1377 else if (n > 0)
1378 rettv->vval.v_number = n;
1379 else
1380 rettv->vval.v_number = -n;
1381 }
1382}
1383
1384/*
1385 * "acos()" function
1386 */
1387 static void
1388f_acos(typval_T *argvars, typval_T *rettv)
1389{
1390 float_T f = 0.0;
1391
1392 rettv->v_type = VAR_FLOAT;
1393 if (get_float_arg(argvars, &f) == OK)
1394 rettv->vval.v_float = acos(f);
1395 else
1396 rettv->vval.v_float = 0.0;
1397}
1398#endif
1399
1400/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 * "and(expr, expr)" function
1402 */
1403 static void
1404f_and(typval_T *argvars, typval_T *rettv)
1405{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001406 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1407 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001408}
1409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001410#ifdef FEAT_FLOAT
1411/*
1412 * "asin()" function
1413 */
1414 static void
1415f_asin(typval_T *argvars, typval_T *rettv)
1416{
1417 float_T f = 0.0;
1418
1419 rettv->v_type = VAR_FLOAT;
1420 if (get_float_arg(argvars, &f) == OK)
1421 rettv->vval.v_float = asin(f);
1422 else
1423 rettv->vval.v_float = 0.0;
1424}
1425
1426/*
1427 * "atan()" function
1428 */
1429 static void
1430f_atan(typval_T *argvars, typval_T *rettv)
1431{
1432 float_T f = 0.0;
1433
1434 rettv->v_type = VAR_FLOAT;
1435 if (get_float_arg(argvars, &f) == OK)
1436 rettv->vval.v_float = atan(f);
1437 else
1438 rettv->vval.v_float = 0.0;
1439}
1440
1441/*
1442 * "atan2()" function
1443 */
1444 static void
1445f_atan2(typval_T *argvars, typval_T *rettv)
1446{
1447 float_T fx = 0.0, fy = 0.0;
1448
1449 rettv->v_type = VAR_FLOAT;
1450 if (get_float_arg(argvars, &fx) == OK
1451 && get_float_arg(&argvars[1], &fy) == OK)
1452 rettv->vval.v_float = atan2(fx, fy);
1453 else
1454 rettv->vval.v_float = 0.0;
1455}
1456#endif
1457
1458/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001459 * "balloon_show()" function
1460 */
1461#ifdef FEAT_BEVAL
1462 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001463f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1464{
1465 rettv->v_type = VAR_STRING;
1466 if (balloonEval != NULL)
1467 {
1468 if (balloonEval->msg == NULL)
1469 rettv->vval.v_string = NULL;
1470 else
1471 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1472 }
1473}
1474
1475 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001476f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1477{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001478 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001479 {
1480 if (argvars[0].v_type == VAR_LIST
1481# ifdef FEAT_GUI
1482 && !gui.in_use
1483# endif
1484 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001485 {
1486 list_T *l = argvars[0].vval.v_list;
1487
1488 // empty list removes the balloon
1489 post_balloon(balloonEval, NULL,
1490 l == NULL || l->lv_len == 0 ? NULL : l);
1491 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001492 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001493 {
1494 char_u *mesg = tv_get_string_chk(&argvars[0]);
1495
1496 if (mesg != NULL)
1497 // empty string removes the balloon
1498 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1499 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001500 }
1501}
1502
Bram Moolenaar669a8282017-11-19 20:13:05 +01001503# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001504 static void
1505f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1506{
1507 if (rettv_list_alloc(rettv) == OK)
1508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001510
1511 if (msg != NULL)
1512 {
1513 pumitem_T *array;
1514 int size = split_message(msg, &array);
1515 int i;
1516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001517 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001518 for (i = 1; i < size - 1; ++i)
1519 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001520 while (size > 0)
1521 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001522 vim_free(array);
1523 }
1524 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001525}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001526# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001527#endif
1528
1529/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001530 * Get the buffer from "arg" and give an error and return NULL if it is not
1531 * valid.
1532 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001533 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001534get_buf_arg(typval_T *arg)
1535{
1536 buf_T *buf;
1537
1538 ++emsg_off;
1539 buf = tv_get_buf(arg, FALSE);
1540 --emsg_off;
1541 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001542 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001543 return buf;
1544}
1545
1546/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001547 * "byte2line(byte)" function
1548 */
1549 static void
1550f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1551{
1552#ifndef FEAT_BYTEOFF
1553 rettv->vval.v_number = -1;
1554#else
1555 long boff = 0;
1556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001557 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558 if (boff < 0)
1559 rettv->vval.v_number = -1;
1560 else
1561 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1562 (linenr_T)0, &boff);
1563#endif
1564}
1565
1566 static void
1567byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1568{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 char_u *str;
1571 varnumber_T idx;
1572
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001573 str = tv_get_string_chk(&argvars[0]);
1574 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 rettv->vval.v_number = -1;
1576 if (str == NULL || idx < 0)
1577 return;
1578
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 t = str;
1580 for ( ; idx > 0; idx--)
1581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001582 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 return;
1584 if (enc_utf8 && comp)
1585 t += utf_ptr2len(t);
1586 else
1587 t += (*mb_ptr2len)(t);
1588 }
1589 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590}
1591
1592/*
1593 * "byteidx()" function
1594 */
1595 static void
1596f_byteidx(typval_T *argvars, typval_T *rettv)
1597{
1598 byteidx(argvars, rettv, FALSE);
1599}
1600
1601/*
1602 * "byteidxcomp()" function
1603 */
1604 static void
1605f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1606{
1607 byteidx(argvars, rettv, TRUE);
1608}
1609
1610/*
1611 * "call(func, arglist [, dict])" function
1612 */
1613 static void
1614f_call(typval_T *argvars, typval_T *rettv)
1615{
1616 char_u *func;
1617 partial_T *partial = NULL;
1618 dict_T *selfdict = NULL;
1619
1620 if (argvars[1].v_type != VAR_LIST)
1621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001622 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623 return;
1624 }
1625 if (argvars[1].vval.v_list == NULL)
1626 return;
1627
1628 if (argvars[0].v_type == VAR_FUNC)
1629 func = argvars[0].vval.v_string;
1630 else if (argvars[0].v_type == VAR_PARTIAL)
1631 {
1632 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001633 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634 }
1635 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001636 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001638 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639
1640 if (argvars[2].v_type != VAR_UNKNOWN)
1641 {
1642 if (argvars[2].v_type != VAR_DICT)
1643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001644 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 return;
1646 }
1647 selfdict = argvars[2].vval.v_dict;
1648 }
1649
1650 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1651}
1652
1653#ifdef FEAT_FLOAT
1654/*
1655 * "ceil({float})" function
1656 */
1657 static void
1658f_ceil(typval_T *argvars, typval_T *rettv)
1659{
1660 float_T f = 0.0;
1661
1662 rettv->v_type = VAR_FLOAT;
1663 if (get_float_arg(argvars, &f) == OK)
1664 rettv->vval.v_float = ceil(f);
1665 else
1666 rettv->vval.v_float = 0.0;
1667}
1668#endif
1669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670/*
1671 * "changenr()" function
1672 */
1673 static void
1674f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1675{
1676 rettv->vval.v_number = curbuf->b_u_seq_cur;
1677}
1678
1679/*
1680 * "char2nr(string)" function
1681 */
1682 static void
1683f_char2nr(typval_T *argvars, typval_T *rettv)
1684{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 if (has_mbyte)
1686 {
1687 int utf8 = 0;
1688
1689 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001690 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691
1692 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001693 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001695 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696 }
1697 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001698 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699}
1700
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001701 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001702get_optional_window(typval_T *argvars, int idx)
1703{
1704 win_T *win = curwin;
1705
1706 if (argvars[idx].v_type != VAR_UNKNOWN)
1707 {
1708 win = find_win_by_nr_or_id(&argvars[idx]);
1709 if (win == NULL)
1710 {
1711 emsg(_(e_invalwindow));
1712 return NULL;
1713 }
1714 }
1715 return win;
1716}
1717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719 * "col(string)" function
1720 */
1721 static void
1722f_col(typval_T *argvars, typval_T *rettv)
1723{
1724 colnr_T col = 0;
1725 pos_T *fp;
1726 int fnum = curbuf->b_fnum;
1727
1728 fp = var2fpos(&argvars[0], FALSE, &fnum);
1729 if (fp != NULL && fnum == curbuf->b_fnum)
1730 {
1731 if (fp->col == MAXCOL)
1732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001733 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001734 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1735 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1736 else
1737 col = MAXCOL;
1738 }
1739 else
1740 {
1741 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 // col(".") when the cursor is on the NUL at the end of the line
1743 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744 if (virtual_active() && fp == &curwin->w_cursor)
1745 {
1746 char_u *p = ml_get_cursor();
1747
1748 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1749 curwin->w_virtcol - curwin->w_cursor.coladd))
1750 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001751 int l;
1752
1753 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1754 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001755 }
1756 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001757 }
1758 }
1759 rettv->vval.v_number = col;
1760}
1761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762/*
1763 * "confirm(message, buttons[, default [, type]])" function
1764 */
1765 static void
1766f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1767{
1768#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1769 char_u *message;
1770 char_u *buttons = NULL;
1771 char_u buf[NUMBUFLEN];
1772 char_u buf2[NUMBUFLEN];
1773 int def = 1;
1774 int type = VIM_GENERIC;
1775 char_u *typestr;
1776 int error = FALSE;
1777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001778 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 if (message == NULL)
1780 error = TRUE;
1781 if (argvars[1].v_type != VAR_UNKNOWN)
1782 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001783 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001784 if (buttons == NULL)
1785 error = TRUE;
1786 if (argvars[2].v_type != VAR_UNKNOWN)
1787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001788 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001789 if (argvars[3].v_type != VAR_UNKNOWN)
1790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001791 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001792 if (typestr == NULL)
1793 error = TRUE;
1794 else
1795 {
1796 switch (TOUPPER_ASC(*typestr))
1797 {
1798 case 'E': type = VIM_ERROR; break;
1799 case 'Q': type = VIM_QUESTION; break;
1800 case 'I': type = VIM_INFO; break;
1801 case 'W': type = VIM_WARNING; break;
1802 case 'G': type = VIM_GENERIC; break;
1803 }
1804 }
1805 }
1806 }
1807 }
1808
1809 if (buttons == NULL || *buttons == NUL)
1810 buttons = (char_u *)_("&Ok");
1811
1812 if (!error)
1813 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1814 def, NULL, FALSE);
1815#endif
1816}
1817
1818/*
1819 * "copy()" function
1820 */
1821 static void
1822f_copy(typval_T *argvars, typval_T *rettv)
1823{
1824 item_copy(&argvars[0], rettv, FALSE, 0);
1825}
1826
1827#ifdef FEAT_FLOAT
1828/*
1829 * "cos()" function
1830 */
1831 static void
1832f_cos(typval_T *argvars, typval_T *rettv)
1833{
1834 float_T f = 0.0;
1835
1836 rettv->v_type = VAR_FLOAT;
1837 if (get_float_arg(argvars, &f) == OK)
1838 rettv->vval.v_float = cos(f);
1839 else
1840 rettv->vval.v_float = 0.0;
1841}
1842
1843/*
1844 * "cosh()" function
1845 */
1846 static void
1847f_cosh(typval_T *argvars, typval_T *rettv)
1848{
1849 float_T f = 0.0;
1850
1851 rettv->v_type = VAR_FLOAT;
1852 if (get_float_arg(argvars, &f) == OK)
1853 rettv->vval.v_float = cosh(f);
1854 else
1855 rettv->vval.v_float = 0.0;
1856}
1857#endif
1858
1859/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001860 * "cursor(lnum, col)" function, or
1861 * "cursor(list)"
1862 *
1863 * Moves the cursor to the specified line and column.
1864 * Returns 0 when the position could be set, -1 otherwise.
1865 */
1866 static void
1867f_cursor(typval_T *argvars, typval_T *rettv)
1868{
1869 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001870 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001871 int set_curswant = TRUE;
1872
1873 rettv->vval.v_number = -1;
1874 if (argvars[1].v_type == VAR_UNKNOWN)
1875 {
1876 pos_T pos;
1877 colnr_T curswant = -1;
1878
1879 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001882 return;
1883 }
1884 line = pos.lnum;
1885 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887 if (curswant >= 0)
1888 {
1889 curwin->w_curswant = curswant - 1;
1890 set_curswant = FALSE;
1891 }
1892 }
1893 else
1894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001895 line = tv_get_lnum(argvars);
1896 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001898 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001900 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001902 if (line > 0)
1903 curwin->w_cursor.lnum = line;
1904 if (col > 0)
1905 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 if (has_mbyte)
1912 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913
1914 curwin->w_set_curswant = set_curswant;
1915 rettv->vval.v_number = 0;
1916}
1917
Bram Moolenaar4f974752019-02-17 17:44:42 +01001918#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001919/*
1920 * "debugbreak()" function
1921 */
1922 static void
1923f_debugbreak(typval_T *argvars, typval_T *rettv)
1924{
1925 int pid;
1926
1927 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001928 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001929 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001930 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001931 else
1932 {
1933 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1934
1935 if (hProcess != NULL)
1936 {
1937 DebugBreakProcess(hProcess);
1938 CloseHandle(hProcess);
1939 rettv->vval.v_number = OK;
1940 }
1941 }
1942}
1943#endif
1944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945/*
1946 * "deepcopy()" function
1947 */
1948 static void
1949f_deepcopy(typval_T *argvars, typval_T *rettv)
1950{
1951 int noref = 0;
1952 int copyID;
1953
1954 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001955 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001956 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001957 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001958 else
1959 {
1960 copyID = get_copyID();
1961 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1962 }
1963}
1964
1965/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001966 * "did_filetype()" function
1967 */
1968 static void
1969f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1970{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001971 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001972}
1973
1974/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001975 * "echoraw({expr})" function
1976 */
1977 static void
1978f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1979{
1980 char_u *str = tv_get_string_chk(&argvars[0]);
1981
1982 if (str != NULL && *str != NUL)
1983 {
1984 out_str(str);
1985 out_flush();
1986 }
1987}
1988
1989/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990 * "empty({expr})" function
1991 */
1992 static void
1993f_empty(typval_T *argvars, typval_T *rettv)
1994{
1995 int n = FALSE;
1996
1997 switch (argvars[0].v_type)
1998 {
1999 case VAR_STRING:
2000 case VAR_FUNC:
2001 n = argvars[0].vval.v_string == NULL
2002 || *argvars[0].vval.v_string == NUL;
2003 break;
2004 case VAR_PARTIAL:
2005 n = FALSE;
2006 break;
2007 case VAR_NUMBER:
2008 n = argvars[0].vval.v_number == 0;
2009 break;
2010 case VAR_FLOAT:
2011#ifdef FEAT_FLOAT
2012 n = argvars[0].vval.v_float == 0.0;
2013 break;
2014#endif
2015 case VAR_LIST:
2016 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002017 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 break;
2019 case VAR_DICT:
2020 n = argvars[0].vval.v_dict == NULL
2021 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2022 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002023 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002024 case VAR_SPECIAL:
2025 n = argvars[0].vval.v_number != VVAL_TRUE;
2026 break;
2027
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002028 case VAR_BLOB:
2029 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002030 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2031 break;
2032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002033 case VAR_JOB:
2034#ifdef FEAT_JOB_CHANNEL
2035 n = argvars[0].vval.v_job == NULL
2036 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2037 break;
2038#endif
2039 case VAR_CHANNEL:
2040#ifdef FEAT_JOB_CHANNEL
2041 n = argvars[0].vval.v_channel == NULL
2042 || !channel_is_open(argvars[0].vval.v_channel);
2043 break;
2044#endif
2045 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002046 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002048 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 n = TRUE;
2050 break;
2051 }
2052
2053 rettv->vval.v_number = n;
2054}
2055
2056/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002057 * "environ()" function
2058 */
2059 static void
2060f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2061{
2062#if !defined(AMIGA)
2063 int i = 0;
2064 char_u *entry, *value;
2065# ifdef MSWIN
2066 extern wchar_t **_wenviron;
2067# else
2068 extern char **environ;
2069# endif
2070
2071 if (rettv_dict_alloc(rettv) != OK)
2072 return;
2073
2074# ifdef MSWIN
2075 if (*_wenviron == NULL)
2076 return;
2077# else
2078 if (*environ == NULL)
2079 return;
2080# endif
2081
2082 for (i = 0; ; ++i)
2083 {
2084# ifdef MSWIN
2085 short_u *p;
2086
2087 if ((p = (short_u *)_wenviron[i]) == NULL)
2088 return;
2089 entry = utf16_to_enc(p, NULL);
2090# else
2091 if ((entry = (char_u *)environ[i]) == NULL)
2092 return;
2093 entry = vim_strsave(entry);
2094# endif
2095 if (entry == NULL) // out of memory
2096 return;
2097 if ((value = vim_strchr(entry, '=')) == NULL)
2098 {
2099 vim_free(entry);
2100 continue;
2101 }
2102 *value++ = NUL;
2103 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2104 vim_free(entry);
2105 }
2106#endif
2107}
2108
2109/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 * "escape({string}, {chars})" function
2111 */
2112 static void
2113f_escape(typval_T *argvars, typval_T *rettv)
2114{
2115 char_u buf[NUMBUFLEN];
2116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002117 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2118 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002119 rettv->v_type = VAR_STRING;
2120}
2121
2122/*
2123 * "eval()" function
2124 */
2125 static void
2126f_eval(typval_T *argvars, typval_T *rettv)
2127{
2128 char_u *s, *p;
2129
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002130 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131 if (s != NULL)
2132 s = skipwhite(s);
2133
2134 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 {
2137 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 need_clr_eos = FALSE;
2140 rettv->v_type = VAR_NUMBER;
2141 rettv->vval.v_number = 0;
2142 }
2143 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002144 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145}
2146
2147/*
2148 * "eventhandler()" function
2149 */
2150 static void
2151f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2152{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002153 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154}
2155
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156static garray_T redir_execute_ga;
2157
2158/*
2159 * Append "value[value_len]" to the execute() output.
2160 */
2161 void
2162execute_redir_str(char_u *value, int value_len)
2163{
2164 int len;
2165
2166 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002167 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002169 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 if (ga_grow(&redir_execute_ga, len) == OK)
2171 {
2172 mch_memmove((char *)redir_execute_ga.ga_data
2173 + redir_execute_ga.ga_len, value, len);
2174 redir_execute_ga.ga_len += len;
2175 }
2176}
2177
2178/*
2179 * Get next line from a list.
2180 * Called by do_cmdline() to get the next line.
2181 * Returns allocated string, or NULL for end of function.
2182 */
2183
2184 static char_u *
2185get_list_line(
2186 int c UNUSED,
2187 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002188 int indent UNUSED,
2189 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002190{
2191 listitem_T **p = (listitem_T **)cookie;
2192 listitem_T *item = *p;
2193 char_u buf[NUMBUFLEN];
2194 char_u *s;
2195
2196 if (item == NULL)
2197 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002198 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002199 *p = item->li_next;
2200 return s == NULL ? NULL : vim_strsave(s);
2201}
2202
2203/*
2204 * "execute()" function
2205 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002206 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002207execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208{
2209 char_u *cmd = NULL;
2210 list_T *list = NULL;
2211 int save_msg_silent = msg_silent;
2212 int save_emsg_silent = emsg_silent;
2213 int save_emsg_noredir = emsg_noredir;
2214 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002215 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002217 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002218 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219
2220 rettv->vval.v_string = NULL;
2221 rettv->v_type = VAR_STRING;
2222
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002223 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002225 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002226 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002227 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 return;
2229 ++list->lv_refcount;
2230 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002231 else if (argvars[arg_off].v_type == VAR_JOB
2232 || argvars[arg_off].v_type == VAR_CHANNEL)
2233 {
2234 emsg(_(e_inval_string));
2235 return;
2236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237 else
2238 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002239 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240 if (cmd == NULL)
2241 return;
2242 }
2243
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002244 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002245 {
2246 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002247 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002248
2249 if (s == NULL)
2250 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002251 if (*s == NUL)
2252 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002253 if (STRNCMP(s, "silent", 6) == 0)
2254 ++msg_silent;
2255 if (STRCMP(s, "silent!") == 0)
2256 {
2257 emsg_silent = TRUE;
2258 emsg_noredir = TRUE;
2259 }
2260 }
2261 else
2262 ++msg_silent;
2263
2264 if (redir_execute)
2265 save_ga = redir_execute_ga;
2266 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2267 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002268 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002269 if (!echo_output)
2270 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271
2272 if (cmd != NULL)
2273 do_cmdline_cmd(cmd);
2274 else
2275 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002276 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002277
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002278 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002279 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 do_cmdline(NULL, get_list_line, (void *)&item,
2281 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2282 --list->lv_refcount;
2283 }
2284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002285 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002286 if (ga_grow(&redir_execute_ga, 1) == OK)
2287 {
2288 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2289 rettv->vval.v_string = redir_execute_ga.ga_data;
2290 }
2291 else
2292 {
2293 ga_clear(&redir_execute_ga);
2294 rettv->vval.v_string = NULL;
2295 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 msg_silent = save_msg_silent;
2297 emsg_silent = save_emsg_silent;
2298 emsg_noredir = save_emsg_noredir;
2299
2300 redir_execute = save_redir_execute;
2301 if (redir_execute)
2302 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002303 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002305 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002306 if (echo_output)
2307 // When not working silently: put it in column zero. A following
2308 // "echon" will overwrite the message, unavoidably.
2309 msg_col = 0;
2310 else
2311 // When working silently: Put it back where it was, since nothing
2312 // should have been written.
2313 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314}
2315
2316/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002317 * "execute()" function
2318 */
2319 static void
2320f_execute(typval_T *argvars, typval_T *rettv)
2321{
2322 execute_common(argvars, rettv, 0);
2323}
2324
2325/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 * "exists()" function
2327 */
2328 static void
2329f_exists(typval_T *argvars, typval_T *rettv)
2330{
2331 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002332 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002333
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002334 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002335 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002337 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002338 if (mch_getenv(p + 1) != NULL)
2339 n = TRUE;
2340 else
2341 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002342 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002343 p = expand_env_save(p);
2344 if (p != NULL && *p != '$')
2345 n = TRUE;
2346 vim_free(p);
2347 }
2348 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002349 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002351 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002355 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002357 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002359 else if (*p == '?') // internal function only
2360 {
2361 n = has_internal_func_name(p + 1);
2362 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 else if (*p == ':')
2364 {
2365 n = cmd_exists(p + 1);
2366 }
2367 else if (*p == '#')
2368 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369 if (p[1] == '#')
2370 n = autocmd_supported(p + 2);
2371 else
2372 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002374 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002376 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 }
2378
2379 rettv->vval.v_number = n;
2380}
2381
2382#ifdef FEAT_FLOAT
2383/*
2384 * "exp()" function
2385 */
2386 static void
2387f_exp(typval_T *argvars, typval_T *rettv)
2388{
2389 float_T f = 0.0;
2390
2391 rettv->v_type = VAR_FLOAT;
2392 if (get_float_arg(argvars, &f) == OK)
2393 rettv->vval.v_float = exp(f);
2394 else
2395 rettv->vval.v_float = 0.0;
2396}
2397#endif
2398
2399/*
2400 * "expand()" function
2401 */
2402 static void
2403f_expand(typval_T *argvars, typval_T *rettv)
2404{
2405 char_u *s;
2406 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002407 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2409 expand_T xpc;
2410 int error = FALSE;
2411 char_u *result;
2412
2413 rettv->v_type = VAR_STRING;
2414 if (argvars[1].v_type != VAR_UNKNOWN
2415 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002416 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002418 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002420 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002421 if (*s == '%' || *s == '#' || *s == '<')
2422 {
2423 ++emsg_off;
2424 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2425 --emsg_off;
2426 if (rettv->v_type == VAR_LIST)
2427 {
2428 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2429 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002430 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431 }
2432 else
2433 rettv->vval.v_string = result;
2434 }
2435 else
2436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002437 // When the optional second argument is non-zero, don't remove matches
2438 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002440 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002441 options |= WILD_KEEP_ALL;
2442 if (!error)
2443 {
2444 ExpandInit(&xpc);
2445 xpc.xp_context = EXPAND_FILES;
2446 if (p_wic)
2447 options += WILD_ICASE;
2448 if (rettv->v_type == VAR_STRING)
2449 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2450 options, WILD_ALL);
2451 else if (rettv_list_alloc(rettv) != FAIL)
2452 {
2453 int i;
2454
2455 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2456 for (i = 0; i < xpc.xp_numfiles; i++)
2457 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2458 ExpandCleanup(&xpc);
2459 }
2460 }
2461 else
2462 rettv->vval.v_string = NULL;
2463 }
2464}
2465
2466/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002467 * "expandcmd()" function
2468 * Expand all the special characters in a command string.
2469 */
2470 static void
2471f_expandcmd(typval_T *argvars, typval_T *rettv)
2472{
2473 exarg_T eap;
2474 char_u *cmdstr;
2475 char *errormsg = NULL;
2476
2477 rettv->v_type = VAR_STRING;
2478 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2479
2480 memset(&eap, 0, sizeof(eap));
2481 eap.cmd = cmdstr;
2482 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002483 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002484 eap.usefilter = FALSE;
2485 eap.nextcmd = NULL;
2486 eap.cmdidx = CMD_USER;
2487
2488 expand_filename(&eap, &cmdstr, &errormsg);
2489 if (errormsg != NULL && *errormsg != NUL)
2490 emsg(errormsg);
2491
2492 rettv->vval.v_string = cmdstr;
2493}
2494
2495/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002496 * "feedkeys()" function
2497 */
2498 static void
2499f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2500{
2501 int remap = TRUE;
2502 int insert = FALSE;
2503 char_u *keys, *flags;
2504 char_u nbuf[NUMBUFLEN];
2505 int typed = FALSE;
2506 int execute = FALSE;
2507 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002508 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509 char_u *keys_esc;
2510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002511 // This is not allowed in the sandbox. If the commands would still be
2512 // executed in the sandbox it would be OK, but it probably happens later,
2513 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 if (check_secure())
2515 return;
2516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002517 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518
2519 if (argvars[1].v_type != VAR_UNKNOWN)
2520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002521 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 for ( ; *flags != NUL; ++flags)
2523 {
2524 switch (*flags)
2525 {
2526 case 'n': remap = FALSE; break;
2527 case 'm': remap = TRUE; break;
2528 case 't': typed = TRUE; break;
2529 case 'i': insert = TRUE; break;
2530 case 'x': execute = TRUE; break;
2531 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002532 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 }
2534 }
2535 }
2536
2537 if (*keys != NUL || execute)
2538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002539 // Need to escape K_SPECIAL and CSI before putting the string in the
2540 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 keys_esc = vim_strsave_escape_csi(keys);
2542 if (keys_esc != NULL)
2543 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002544 if (lowlevel)
2545 {
2546#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002547 int idx;
2548 int len = (int)STRLEN(keys);
2549
2550 for (idx = 0; idx < len; ++idx)
2551 {
2552 // if a CTRL-C was typed, set got_int, similar to what
2553 // happens in fill_input_buf()
2554 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2555 got_int = TRUE;
2556 add_to_input_buf(keys + idx, 1);
2557 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002558#else
2559 emsg(_("E980: lowlevel input not supported"));
2560#endif
2561 }
2562 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002563 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002564 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002566 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002567#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002568 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002569#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002570 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002571 typebuf_was_filled = TRUE;
2572 }
2573 vim_free(keys_esc);
2574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002575 if (execute)
2576 {
2577 int save_msg_scroll = msg_scroll;
2578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002579 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 msg_scroll = FALSE;
2581
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002582 if (!dangerous)
2583 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002584 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002585 if (!dangerous)
2586 --ex_normal_busy;
2587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 msg_scroll |= save_msg_scroll;
2589 }
2590 }
2591 }
2592}
2593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594#ifdef FEAT_FLOAT
2595/*
2596 * "float2nr({float})" function
2597 */
2598 static void
2599f_float2nr(typval_T *argvars, typval_T *rettv)
2600{
2601 float_T f = 0.0;
2602
2603 if (get_float_arg(argvars, &f) == OK)
2604 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002605 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002606 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002607 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002608 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609 else
2610 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611 }
2612}
2613
2614/*
2615 * "floor({float})" function
2616 */
2617 static void
2618f_floor(typval_T *argvars, typval_T *rettv)
2619{
2620 float_T f = 0.0;
2621
2622 rettv->v_type = VAR_FLOAT;
2623 if (get_float_arg(argvars, &f) == OK)
2624 rettv->vval.v_float = floor(f);
2625 else
2626 rettv->vval.v_float = 0.0;
2627}
2628
2629/*
2630 * "fmod()" function
2631 */
2632 static void
2633f_fmod(typval_T *argvars, typval_T *rettv)
2634{
2635 float_T fx = 0.0, fy = 0.0;
2636
2637 rettv->v_type = VAR_FLOAT;
2638 if (get_float_arg(argvars, &fx) == OK
2639 && get_float_arg(&argvars[1], &fy) == OK)
2640 rettv->vval.v_float = fmod(fx, fy);
2641 else
2642 rettv->vval.v_float = 0.0;
2643}
2644#endif
2645
2646/*
2647 * "fnameescape({string})" function
2648 */
2649 static void
2650f_fnameescape(typval_T *argvars, typval_T *rettv)
2651{
2652 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002653 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002654 rettv->v_type = VAR_STRING;
2655}
2656
2657/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658 * "foreground()" function
2659 */
2660 static void
2661f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2662{
2663#ifdef FEAT_GUI
2664 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002665 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002667 return;
2668 }
2669#endif
2670#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672#endif
2673}
2674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002676common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677{
2678 char_u *s;
2679 char_u *name;
2680 int use_string = FALSE;
2681 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002682 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002683 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684
2685 if (argvars[0].v_type == VAR_FUNC)
2686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002687 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 s = argvars[0].vval.v_string;
2689 }
2690 else if (argvars[0].v_type == VAR_PARTIAL
2691 && argvars[0].vval.v_partial != NULL)
2692 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002693 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002695 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696 }
2697 else
2698 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002699 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002700 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 use_string = TRUE;
2702 }
2703
Bram Moolenaar843b8842016-08-21 14:36:15 +02002704 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002705 {
2706 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002707 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002708 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2709 if (*name != NUL)
2710 s = NULL;
2711 }
2712
Bram Moolenaar843b8842016-08-21 14:36:15 +02002713 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2714 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002715 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002716 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002717 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002718 ? find_func(trans_name, is_global, NULL) == NULL
2719 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002720 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 else
2722 {
2723 int dict_idx = 0;
2724 int arg_idx = 0;
2725 list_T *list = NULL;
2726
2727 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2728 {
2729 char sid_buf[25];
2730 int off = *s == 's' ? 2 : 5;
2731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002732 // Expand s: and <SID> into <SNR>nr_, so that the function can
2733 // also be called from another script. Using trans_function_name()
2734 // would also work, but some plugins depend on the name being
2735 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002736 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002737 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002738 if (name != NULL)
2739 {
2740 STRCPY(name, sid_buf);
2741 STRCAT(name, s + off);
2742 }
2743 }
2744 else
2745 name = vim_strsave(s);
2746
2747 if (argvars[1].v_type != VAR_UNKNOWN)
2748 {
2749 if (argvars[2].v_type != VAR_UNKNOWN)
2750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002752 arg_idx = 1;
2753 dict_idx = 2;
2754 }
2755 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002756 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 dict_idx = 1;
2758 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002760 arg_idx = 1;
2761 if (dict_idx > 0)
2762 {
2763 if (argvars[dict_idx].v_type != VAR_DICT)
2764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002765 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002767 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 }
2769 if (argvars[dict_idx].vval.v_dict == NULL)
2770 dict_idx = 0;
2771 }
2772 if (arg_idx > 0)
2773 {
2774 if (argvars[arg_idx].v_type != VAR_LIST)
2775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002776 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002778 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 }
2780 list = argvars[arg_idx].vval.v_list;
2781 if (list == NULL || list->lv_len == 0)
2782 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002783 else if (list->lv_len > MAX_FUNC_ARGS)
2784 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002785 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002786 vim_free(name);
2787 goto theend;
2788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 }
2790 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002791 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002793 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002795 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 if (pt == NULL)
2797 vim_free(name);
2798 else
2799 {
2800 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2801 {
2802 listitem_T *li;
2803 int i = 0;
2804 int arg_len = 0;
2805 int lv_len = 0;
2806
2807 if (arg_pt != NULL)
2808 arg_len = arg_pt->pt_argc;
2809 if (list != NULL)
2810 lv_len = list->lv_len;
2811 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002812 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813 if (pt->pt_argv == NULL)
2814 {
2815 vim_free(pt);
2816 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002817 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002819 for (i = 0; i < arg_len; i++)
2820 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2821 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002822 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002823 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002824 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002825 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827 }
2828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002829 // For "function(dict.func, [], dict)" and "func" is a partial
2830 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (dict_idx > 0)
2832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002833 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002834 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2835 ++pt->pt_dict->dv_refcount;
2836 }
2837 else if (arg_pt != NULL)
2838 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 // If the dict was bound automatically the result is also
2840 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002841 pt->pt_dict = arg_pt->pt_dict;
2842 pt->pt_auto = arg_pt->pt_auto;
2843 if (pt->pt_dict != NULL)
2844 ++pt->pt_dict->dv_refcount;
2845 }
2846
2847 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002848 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2849 {
2850 pt->pt_func = arg_pt->pt_func;
2851 func_ptr_ref(pt->pt_func);
2852 vim_free(name);
2853 }
2854 else if (is_funcref)
2855 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002856 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002857 func_ptr_ref(pt->pt_func);
2858 vim_free(name);
2859 }
2860 else
2861 {
2862 pt->pt_name = name;
2863 func_ref(name);
2864 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002865 }
2866 rettv->v_type = VAR_PARTIAL;
2867 rettv->vval.v_partial = pt;
2868 }
2869 else
2870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002871 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872 rettv->v_type = VAR_FUNC;
2873 rettv->vval.v_string = name;
2874 func_ref(name);
2875 }
2876 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002877theend:
2878 vim_free(trans_name);
2879}
2880
2881/*
2882 * "funcref()" function
2883 */
2884 static void
2885f_funcref(typval_T *argvars, typval_T *rettv)
2886{
2887 common_function(argvars, rettv, TRUE);
2888}
2889
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002890 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002891ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002892{
2893 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2894 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002895 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002896}
2897
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002898/*
2899 * "function()" function
2900 */
2901 static void
2902f_function(typval_T *argvars, typval_T *rettv)
2903{
2904 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905}
2906
2907/*
2908 * "garbagecollect()" function
2909 */
2910 static void
2911f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2912{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002913 // This is postponed until we are back at the toplevel, because we may be
2914 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002915 want_garbage_collect = TRUE;
2916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002917 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002918 garbage_collect_at_exit = TRUE;
2919}
2920
2921/*
2922 * "get()" function
2923 */
2924 static void
2925f_get(typval_T *argvars, typval_T *rettv)
2926{
2927 listitem_T *li;
2928 list_T *l;
2929 dictitem_T *di;
2930 dict_T *d;
2931 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002932 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002934 if (argvars[0].v_type == VAR_BLOB)
2935 {
2936 int error = FALSE;
2937 int idx = tv_get_number_chk(&argvars[1], &error);
2938
2939 if (!error)
2940 {
2941 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002942 if (idx < 0)
2943 idx = blob_len(argvars[0].vval.v_blob) + idx;
2944 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2945 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002947 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002948 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002949 tv = rettv;
2950 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 }
2952 }
2953 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 {
2955 if ((l = argvars[0].vval.v_list) != NULL)
2956 {
2957 int error = FALSE;
2958
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002959 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 if (!error && li != NULL)
2961 tv = &li->li_tv;
2962 }
2963 }
2964 else if (argvars[0].v_type == VAR_DICT)
2965 {
2966 if ((d = argvars[0].vval.v_dict) != NULL)
2967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002968 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 if (di != NULL)
2970 tv = &di->di_tv;
2971 }
2972 }
2973 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2974 {
2975 partial_T *pt;
2976 partial_T fref_pt;
2977
2978 if (argvars[0].v_type == VAR_PARTIAL)
2979 pt = argvars[0].vval.v_partial;
2980 else
2981 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002982 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 fref_pt.pt_name = argvars[0].vval.v_string;
2984 pt = &fref_pt;
2985 }
2986
2987 if (pt != NULL)
2988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002989 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002990 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991
2992 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
2993 {
2994 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002995 n = partial_name(pt);
2996 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 rettv->vval.v_string = NULL;
2998 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002999 {
3000 rettv->vval.v_string = vim_strsave(n);
3001 if (rettv->v_type == VAR_FUNC)
3002 func_ref(rettv->vval.v_string);
3003 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 }
3005 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003006 {
3007 what_is_dict = TRUE;
3008 if (pt->pt_dict != NULL)
3009 rettv_dict_set(rettv, pt->pt_dict);
3010 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 else if (STRCMP(what, "args") == 0)
3012 {
3013 rettv->v_type = VAR_LIST;
3014 if (rettv_list_alloc(rettv) == OK)
3015 {
3016 int i;
3017
3018 for (i = 0; i < pt->pt_argc; ++i)
3019 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3020 }
3021 }
3022 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003024
3025 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3026 // third argument
3027 if (!what_is_dict)
3028 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 }
3030 }
3031 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003032 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 if (tv == NULL)
3035 {
3036 if (argvars[2].v_type != VAR_UNKNOWN)
3037 copy_tv(&argvars[2], rettv);
3038 }
3039 else
3040 copy_tv(tv, rettv);
3041}
3042
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003043/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003044 * "getchangelist()" function
3045 */
3046 static void
3047f_getchangelist(typval_T *argvars, typval_T *rettv)
3048{
3049#ifdef FEAT_JUMPLIST
3050 buf_T *buf;
3051 int i;
3052 list_T *l;
3053 dict_T *d;
3054#endif
3055
3056 if (rettv_list_alloc(rettv) != OK)
3057 return;
3058
3059#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003060 if (argvars[0].v_type == VAR_UNKNOWN)
3061 buf = curbuf;
3062 else
3063 {
3064 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3065 ++emsg_off;
3066 buf = tv_get_buf(&argvars[0], FALSE);
3067 --emsg_off;
3068 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003069 if (buf == NULL)
3070 return;
3071
3072 l = list_alloc();
3073 if (l == NULL)
3074 return;
3075
3076 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3077 return;
3078 /*
3079 * The current window change list index tracks only the position in the
3080 * current buffer change list. For other buffers, use the change list
3081 * length as the current index.
3082 */
3083 list_append_number(rettv->vval.v_list,
3084 (varnumber_T)((buf == curwin->w_buffer)
3085 ? curwin->w_changelistidx : buf->b_changelistlen));
3086
3087 for (i = 0; i < buf->b_changelistlen; ++i)
3088 {
3089 if (buf->b_changelist[i].lnum == 0)
3090 continue;
3091 if ((d = dict_alloc()) == NULL)
3092 return;
3093 if (list_append_dict(l, d) == FAIL)
3094 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003095 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3096 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003097 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003098 }
3099#endif
3100}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101
3102/*
3103 * "getcharsearch()" function
3104 */
3105 static void
3106f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3107{
3108 if (rettv_dict_alloc(rettv) != FAIL)
3109 {
3110 dict_T *dict = rettv->vval.v_dict;
3111
Bram Moolenaare0be1672018-07-08 16:50:37 +02003112 dict_add_string(dict, "char", last_csearch());
3113 dict_add_number(dict, "forward", last_csearch_forward());
3114 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 }
3116}
3117
3118/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003119 * "getenv()" function
3120 */
3121 static void
3122f_getenv(typval_T *argvars, typval_T *rettv)
3123{
3124 int mustfree = FALSE;
3125 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3126
3127 if (p == NULL)
3128 {
3129 rettv->v_type = VAR_SPECIAL;
3130 rettv->vval.v_number = VVAL_NULL;
3131 return;
3132 }
3133 if (!mustfree)
3134 p = vim_strsave(p);
3135 rettv->vval.v_string = p;
3136 rettv->v_type = VAR_STRING;
3137}
3138
3139/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140 * "getfontname()" function
3141 */
3142 static void
3143f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3144{
3145 rettv->v_type = VAR_STRING;
3146 rettv->vval.v_string = NULL;
3147#ifdef FEAT_GUI
3148 if (gui.in_use)
3149 {
3150 GuiFont font;
3151 char_u *name = NULL;
3152
3153 if (argvars[0].v_type == VAR_UNKNOWN)
3154 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003155 // Get the "Normal" font. Either the name saved by
3156 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 font = gui.norm_font;
3158 name = hl_get_font_name();
3159 }
3160 else
3161 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003162 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003163 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 return;
3165 font = gui_mch_get_font(name, FALSE);
3166 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003167 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168 }
3169 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3170 if (argvars[0].v_type != VAR_UNKNOWN)
3171 gui_mch_free_font(font);
3172 }
3173#endif
3174}
3175
3176/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003177 * "getjumplist()" function
3178 */
3179 static void
3180f_getjumplist(typval_T *argvars, typval_T *rettv)
3181{
3182#ifdef FEAT_JUMPLIST
3183 win_T *wp;
3184 int i;
3185 list_T *l;
3186 dict_T *d;
3187#endif
3188
3189 if (rettv_list_alloc(rettv) != OK)
3190 return;
3191
3192#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003193 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003194 if (wp == NULL)
3195 return;
3196
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003197 cleanup_jumplist(wp, TRUE);
3198
Bram Moolenaar4f505882018-02-10 21:06:32 +01003199 l = list_alloc();
3200 if (l == NULL)
3201 return;
3202
3203 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3204 return;
3205 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3206
3207 for (i = 0; i < wp->w_jumplistlen; ++i)
3208 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003209 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3210 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003211 if ((d = dict_alloc()) == NULL)
3212 return;
3213 if (list_append_dict(l, d) == FAIL)
3214 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003215 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3216 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003217 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003218 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003219 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003220 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003221 }
3222#endif
3223}
3224
3225/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226 * "getpid()" function
3227 */
3228 static void
3229f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3230{
3231 rettv->vval.v_number = mch_get_pid();
3232}
3233
3234 static void
3235getpos_both(
3236 typval_T *argvars,
3237 typval_T *rettv,
3238 int getcurpos)
3239{
3240 pos_T *fp;
3241 list_T *l;
3242 int fnum = -1;
3243
3244 if (rettv_list_alloc(rettv) == OK)
3245 {
3246 l = rettv->vval.v_list;
3247 if (getcurpos)
3248 fp = &curwin->w_cursor;
3249 else
3250 fp = var2fpos(&argvars[0], TRUE, &fnum);
3251 if (fnum != -1)
3252 list_append_number(l, (varnumber_T)fnum);
3253 else
3254 list_append_number(l, (varnumber_T)0);
3255 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3256 : (varnumber_T)0);
3257 list_append_number(l, (fp != NULL)
3258 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3259 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003260 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 (varnumber_T)0);
3262 if (getcurpos)
3263 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003264 int save_set_curswant = curwin->w_set_curswant;
3265 colnr_T save_curswant = curwin->w_curswant;
3266 colnr_T save_virtcol = curwin->w_virtcol;
3267
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 update_curswant();
3269 list_append_number(l, curwin->w_curswant == MAXCOL ?
3270 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003271
3272 // Do not change "curswant", as it is unexpected that a get
3273 // function has a side effect.
3274 if (save_set_curswant)
3275 {
3276 curwin->w_set_curswant = save_set_curswant;
3277 curwin->w_curswant = save_curswant;
3278 curwin->w_virtcol = save_virtcol;
3279 curwin->w_valid &= ~VALID_VIRTCOL;
3280 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 }
3282 }
3283 else
3284 rettv->vval.v_number = FALSE;
3285}
3286
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287/*
3288 * "getcurpos()" function
3289 */
3290 static void
3291f_getcurpos(typval_T *argvars, typval_T *rettv)
3292{
3293 getpos_both(argvars, rettv, TRUE);
3294}
3295
3296/*
3297 * "getpos(string)" function
3298 */
3299 static void
3300f_getpos(typval_T *argvars, typval_T *rettv)
3301{
3302 getpos_both(argvars, rettv, FALSE);
3303}
3304
3305/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 * "getreg()" function
3307 */
3308 static void
3309f_getreg(typval_T *argvars, typval_T *rettv)
3310{
3311 char_u *strregname;
3312 int regname;
3313 int arg2 = FALSE;
3314 int return_list = FALSE;
3315 int error = FALSE;
3316
3317 if (argvars[0].v_type != VAR_UNKNOWN)
3318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003319 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003320 error = strregname == NULL;
3321 if (argvars[1].v_type != VAR_UNKNOWN)
3322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003323 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003325 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 }
3327 }
3328 else
3329 strregname = get_vim_var_str(VV_REG);
3330
3331 if (error)
3332 return;
3333
3334 regname = (strregname == NULL ? '"' : *strregname);
3335 if (regname == 0)
3336 regname = '"';
3337
3338 if (return_list)
3339 {
3340 rettv->v_type = VAR_LIST;
3341 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3342 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3343 if (rettv->vval.v_list == NULL)
3344 (void)rettv_list_alloc(rettv);
3345 else
3346 ++rettv->vval.v_list->lv_refcount;
3347 }
3348 else
3349 {
3350 rettv->v_type = VAR_STRING;
3351 rettv->vval.v_string = get_reg_contents(regname,
3352 arg2 ? GREG_EXPR_SRC : 0);
3353 }
3354}
3355
3356/*
3357 * "getregtype()" function
3358 */
3359 static void
3360f_getregtype(typval_T *argvars, typval_T *rettv)
3361{
3362 char_u *strregname;
3363 int regname;
3364 char_u buf[NUMBUFLEN + 2];
3365 long reglen = 0;
3366
3367 if (argvars[0].v_type != VAR_UNKNOWN)
3368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003369 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003370 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 {
3372 rettv->v_type = VAR_STRING;
3373 rettv->vval.v_string = NULL;
3374 return;
3375 }
3376 }
3377 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003378 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003379 strregname = get_vim_var_str(VV_REG);
3380
3381 regname = (strregname == NULL ? '"' : *strregname);
3382 if (regname == 0)
3383 regname = '"';
3384
3385 buf[0] = NUL;
3386 buf[1] = NUL;
3387 switch (get_reg_type(regname, &reglen))
3388 {
3389 case MLINE: buf[0] = 'V'; break;
3390 case MCHAR: buf[0] = 'v'; break;
3391 case MBLOCK:
3392 buf[0] = Ctrl_V;
3393 sprintf((char *)buf + 1, "%ld", reglen + 1);
3394 break;
3395 }
3396 rettv->v_type = VAR_STRING;
3397 rettv->vval.v_string = vim_strsave(buf);
3398}
3399
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003400/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003401 * "gettagstack()" function
3402 */
3403 static void
3404f_gettagstack(typval_T *argvars, typval_T *rettv)
3405{
3406 win_T *wp = curwin; // default is current window
3407
3408 if (rettv_dict_alloc(rettv) != OK)
3409 return;
3410
3411 if (argvars[0].v_type != VAR_UNKNOWN)
3412 {
3413 wp = find_win_by_nr_or_id(&argvars[0]);
3414 if (wp == NULL)
3415 return;
3416 }
3417
3418 get_tagstack(wp, rettv->vval.v_dict);
3419}
3420
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003421// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003422#include "version.h"
3423
3424/*
3425 * "has()" function
3426 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003427 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428f_has(typval_T *argvars, typval_T *rettv)
3429{
3430 int i;
3431 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003432 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003434 typedef struct {
3435 char *name;
3436 short present;
3437 } has_item_T;
3438 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003440 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003442 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003443#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003444 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003445#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003446 },
3447 {"arp",
3448#if defined(AMIGA) && defined(FEAT_ARP)
3449 1
3450#else
3451 0
3452#endif
3453 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003454 {"haiku",
3455#ifdef __HAIKU__
3456 1
3457#else
3458 0
3459#endif
3460 },
3461 {"bsd",
3462#if defined(BSD) && !defined(MACOS_X)
3463 1
3464#else
3465 0
3466#endif
3467 },
3468 {"hpux",
3469#ifdef hpux
3470 1
3471#else
3472 0
3473#endif
3474 },
3475 {"linux",
3476#ifdef __linux__
3477 1
3478#else
3479 0
3480#endif
3481 },
3482 {"mac", // Mac OS X (and, once, Mac OS Classic)
3483#ifdef MACOS_X
3484 1
3485#else
3486 0
3487#endif
3488 },
3489 {"osx", // Mac OS X
3490#ifdef MACOS_X
3491 1
3492#else
3493 0
3494#endif
3495 },
3496 {"macunix", // Mac OS X, with the darwin feature
3497#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3498 1
3499#else
3500 0
3501#endif
3502 },
3503 {"osxdarwin", // synonym for macunix
3504#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3505 1
3506#else
3507 0
3508#endif
3509 },
3510 {"qnx",
3511#ifdef __QNX__
3512 1
3513#else
3514 0
3515#endif
3516 },
3517 {"sun",
3518#ifdef SUN_SYSTEM
3519 1
3520#else
3521 0
3522#endif
3523 },
3524 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003526 1
3527#else
3528 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003530 },
3531 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003533 1
3534#else
3535 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003537 },
3538 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003539#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003540 1
3541#else
3542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003544 },
3545 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003546#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003547 1
3548#else
3549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003551 },
3552 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003553#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003554 1
3555#else
3556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003558 },
3559 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003560#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003561 1
3562#else
3563 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003565 },
3566 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567#ifndef CASE_INSENSITIVE_FILENAME
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 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574#ifdef HAVE_ACL
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 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581#ifdef FEAT_ARABIC
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 {"autocmd", 1},
3588 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003589#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003590 1
3591#else
3592 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003594 },
3595 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003596#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003597 1
3598#else
3599 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003600#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003601 },
3602 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003603#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003604 1
3605#else
3606 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003608 },
3609 {"balloon_multiline",
3610#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3611 // MS-Windows requires runtime check, see below
3612 1
3613#else
3614 0
3615#endif
3616 },
3617 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003618#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003619 1
3620#else
3621 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003622#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003623 },
3624 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003626 1
3627#else
3628 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003630 },
3631 {"all_builtin_terms",
3632#if defined(ALL_BUILTIN_TCAPS)
3633 1
3634#else
3635 0
3636#endif
3637 },
3638 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003640 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003642 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003644 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003646 },
3647 {"byte_offset",
3648#ifdef FEAT_BYTEOFF
3649 1
3650#else
3651 0
3652#endif
3653 },
3654 {"channel",
3655#ifdef FEAT_JOB_CHANNEL
3656 1
3657#else
3658 0
3659#endif
3660 },
3661 {"cindent",
3662#ifdef FEAT_CINDENT
3663 1
3664#else
3665 0
3666#endif
3667 },
3668 {"clientserver",
3669#ifdef FEAT_CLIENTSERVER
3670 1
3671#else
3672 0
3673#endif
3674 },
3675 {"clipboard",
3676#ifdef FEAT_CLIPBOARD
3677 1
3678#else
3679 0
3680#endif
3681 },
3682 {"cmdline_compl", 1},
3683 {"cmdline_hist", 1},
3684 {"comments", 1},
3685 {"conceal",
3686#ifdef FEAT_CONCEAL
3687 1
3688#else
3689 0
3690#endif
3691 },
3692 {"cryptv",
3693#ifdef FEAT_CRYPT
3694 1
3695#else
3696 0
3697#endif
3698 },
3699 {"crypt-blowfish",
3700#ifdef FEAT_CRYPT
3701 1
3702#else
3703 0
3704#endif
3705 },
3706 {"crypt-blowfish2",
3707#ifdef FEAT_CRYPT
3708 1
3709#else
3710 0
3711#endif
3712 },
3713 {"cscope",
3714#ifdef FEAT_CSCOPE
3715 1
3716#else
3717 0
3718#endif
3719 },
3720 {"cursorbind", 1},
3721 {"cursorshape",
3722#ifdef CURSOR_SHAPE
3723 1
3724#else
3725 0
3726#endif
3727 },
3728 {"debug",
3729#ifdef DEBUG
3730 1
3731#else
3732 0
3733#endif
3734 },
3735 {"dialog_con",
3736#ifdef FEAT_CON_DIALOG
3737 1
3738#else
3739 0
3740#endif
3741 },
3742 {"dialog_gui",
3743#ifdef FEAT_GUI_DIALOG
3744 1
3745#else
3746 0
3747#endif
3748 },
3749 {"diff",
3750#ifdef FEAT_DIFF
3751 1
3752#else
3753 0
3754#endif
3755 },
3756 {"digraphs",
3757#ifdef FEAT_DIGRAPHS
3758 1
3759#else
3760 0
3761#endif
3762 },
3763 {"directx",
3764#ifdef FEAT_DIRECTX
3765 1
3766#else
3767 0
3768#endif
3769 },
3770 {"dnd",
3771#ifdef FEAT_DND
3772 1
3773#else
3774 0
3775#endif
3776 },
3777 {"emacs_tags",
3778#ifdef FEAT_EMACS_TAGS
3779 1
3780#else
3781 0
3782#endif
3783 },
3784 {"eval", 1}, // always present, of course!
3785 {"ex_extra", 1}, // graduated feature
3786 {"extra_search",
3787#ifdef FEAT_SEARCH_EXTRA
3788 1
3789#else
3790 0
3791#endif
3792 },
3793 {"file_in_path",
3794#ifdef FEAT_SEARCHPATH
3795 1
3796#else
3797 0
3798#endif
3799 },
3800 {"filterpipe",
3801#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3802 1
3803#else
3804 0
3805#endif
3806 },
3807 {"find_in_path",
3808#ifdef FEAT_FIND_ID
3809 1
3810#else
3811 0
3812#endif
3813 },
3814 {"float",
3815#ifdef FEAT_FLOAT
3816 1
3817#else
3818 0
3819#endif
3820 },
3821 {"folding",
3822#ifdef FEAT_FOLDING
3823 1
3824#else
3825 0
3826#endif
3827 },
3828 {"footer",
3829#ifdef FEAT_FOOTER
3830 1
3831#else
3832 0
3833#endif
3834 },
3835 {"fork",
3836#if !defined(USE_SYSTEM) && defined(UNIX)
3837 1
3838#else
3839 0
3840#endif
3841 },
3842 {"gettext",
3843#ifdef FEAT_GETTEXT
3844 1
3845#else
3846 0
3847#endif
3848 },
3849 {"gui",
3850#ifdef FEAT_GUI
3851 1
3852#else
3853 0
3854#endif
3855 },
3856 {"gui_neXtaw",
3857#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3858 1
3859#else
3860 0
3861#endif
3862 },
3863 {"gui_athena",
3864#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3865 1
3866#else
3867 0
3868#endif
3869 },
3870 {"gui_gtk",
3871#ifdef FEAT_GUI_GTK
3872 1
3873#else
3874 0
3875#endif
3876 },
3877 {"gui_gtk2",
3878#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3879 1
3880#else
3881 0
3882#endif
3883 },
3884 {"gui_gtk3",
3885#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3886 1
3887#else
3888 0
3889#endif
3890 },
3891 {"gui_gnome",
3892#ifdef FEAT_GUI_GNOME
3893 1
3894#else
3895 0
3896#endif
3897 },
3898 {"gui_haiku",
3899#ifdef FEAT_GUI_HAIKU
3900 1
3901#else
3902 0
3903#endif
3904 },
3905 {"gui_mac",
3906#ifdef FEAT_GUI_MAC
3907 1
3908#else
3909 0
3910#endif
3911 },
3912 {"gui_motif",
3913#ifdef FEAT_GUI_MOTIF
3914 1
3915#else
3916 0
3917#endif
3918 },
3919 {"gui_photon",
3920#ifdef FEAT_GUI_PHOTON
3921 1
3922#else
3923 0
3924#endif
3925 },
3926 {"gui_win32",
3927#ifdef FEAT_GUI_MSWIN
3928 1
3929#else
3930 0
3931#endif
3932 },
3933 {"iconv",
3934#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3935 1
3936#else
3937 0
3938#endif
3939 },
3940 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003941 {"ipv6",
3942#ifdef FEAT_IPV6
3943 1
3944#else
3945 0
3946#endif
3947 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003948 {"job",
3949#ifdef FEAT_JOB_CHANNEL
3950 1
3951#else
3952 0
3953#endif
3954 },
3955 {"jumplist",
3956#ifdef FEAT_JUMPLIST
3957 1
3958#else
3959 0
3960#endif
3961 },
3962 {"keymap",
3963#ifdef FEAT_KEYMAP
3964 1
3965#else
3966 0
3967#endif
3968 },
3969 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3970 {"langmap",
3971#ifdef FEAT_LANGMAP
3972 1
3973#else
3974 0
3975#endif
3976 },
3977 {"libcall",
3978#ifdef FEAT_LIBCALL
3979 1
3980#else
3981 0
3982#endif
3983 },
3984 {"linebreak",
3985#ifdef FEAT_LINEBREAK
3986 1
3987#else
3988 0
3989#endif
3990 },
3991 {"lispindent",
3992#ifdef FEAT_LISP
3993 1
3994#else
3995 0
3996#endif
3997 },
3998 {"listcmds", 1},
3999 {"localmap", 1},
4000 {"lua",
4001#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4002 1
4003#else
4004 0
4005#endif
4006 },
4007 {"menu",
4008#ifdef FEAT_MENU
4009 1
4010#else
4011 0
4012#endif
4013 },
4014 {"mksession",
4015#ifdef FEAT_SESSION
4016 1
4017#else
4018 0
4019#endif
4020 },
4021 {"modify_fname", 1},
4022 {"mouse", 1},
4023 {"mouseshape",
4024#ifdef FEAT_MOUSESHAPE
4025 1
4026#else
4027 0
4028#endif
4029 },
4030 {"mouse_dec",
4031#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4032 1
4033#else
4034 0
4035#endif
4036 },
4037 {"mouse_gpm",
4038#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4039 1
4040#else
4041 0
4042#endif
4043 },
4044 {"mouse_jsbterm",
4045#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4046 1
4047#else
4048 0
4049#endif
4050 },
4051 {"mouse_netterm",
4052#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4053 1
4054#else
4055 0
4056#endif
4057 },
4058 {"mouse_pterm",
4059#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4060 1
4061#else
4062 0
4063#endif
4064 },
4065 {"mouse_sgr",
4066#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4067 1
4068#else
4069 0
4070#endif
4071 },
4072 {"mouse_sysmouse",
4073#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4074 1
4075#else
4076 0
4077#endif
4078 },
4079 {"mouse_urxvt",
4080#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4081 1
4082#else
4083 0
4084#endif
4085 },
4086 {"mouse_xterm",
4087#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4088 1
4089#else
4090 0
4091#endif
4092 },
4093 {"multi_byte", 1},
4094 {"multi_byte_ime",
4095#ifdef FEAT_MBYTE_IME
4096 1
4097#else
4098 0
4099#endif
4100 },
4101 {"multi_lang",
4102#ifdef FEAT_MULTI_LANG
4103 1
4104#else
4105 0
4106#endif
4107 },
4108 {"mzscheme",
4109#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4110 1
4111#else
4112 0
4113#endif
4114 },
4115 {"num64", 1},
4116 {"ole",
4117#ifdef FEAT_OLE
4118 1
4119#else
4120 0
4121#endif
4122 },
4123 {"packages",
4124#ifdef FEAT_EVAL
4125 1
4126#else
4127 0
4128#endif
4129 },
4130 {"path_extra",
4131#ifdef FEAT_PATH_EXTRA
4132 1
4133#else
4134 0
4135#endif
4136 },
4137 {"perl",
4138#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4139 1
4140#else
4141 0
4142#endif
4143 },
4144 {"persistent_undo",
4145#ifdef FEAT_PERSISTENT_UNDO
4146 1
4147#else
4148 0
4149#endif
4150 },
4151 {"python_compiled",
4152#if defined(FEAT_PYTHON)
4153 1
4154#else
4155 0
4156#endif
4157 },
4158 {"python_dynamic",
4159#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4160 1
4161#else
4162 0
4163#endif
4164 },
4165 {"python",
4166#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4167 1
4168#else
4169 0
4170#endif
4171 },
4172 {"pythonx",
4173#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4174 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4175 1
4176#else
4177 0
4178#endif
4179 },
4180 {"python3_compiled",
4181#if defined(FEAT_PYTHON3)
4182 1
4183#else
4184 0
4185#endif
4186 },
4187 {"python3_dynamic",
4188#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4189 1
4190#else
4191 0
4192#endif
4193 },
4194 {"python3",
4195#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4196 1
4197#else
4198 0
4199#endif
4200 },
4201 {"popupwin",
4202#ifdef FEAT_PROP_POPUP
4203 1
4204#else
4205 0
4206#endif
4207 },
4208 {"postscript",
4209#ifdef FEAT_POSTSCRIPT
4210 1
4211#else
4212 0
4213#endif
4214 },
4215 {"printer",
4216#ifdef FEAT_PRINTER
4217 1
4218#else
4219 0
4220#endif
4221 },
4222 {"profile",
4223#ifdef FEAT_PROFILE
4224 1
4225#else
4226 0
4227#endif
4228 },
4229 {"reltime",
4230#ifdef FEAT_RELTIME
4231 1
4232#else
4233 0
4234#endif
4235 },
4236 {"quickfix",
4237#ifdef FEAT_QUICKFIX
4238 1
4239#else
4240 0
4241#endif
4242 },
4243 {"rightleft",
4244#ifdef FEAT_RIGHTLEFT
4245 1
4246#else
4247 0
4248#endif
4249 },
4250 {"ruby",
4251#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4252 1
4253#else
4254 0
4255#endif
4256 },
4257 {"scrollbind", 1},
4258 {"showcmd",
4259#ifdef FEAT_CMDL_INFO
4260 1
4261#else
4262 0
4263#endif
4264 },
4265 {"cmdline_info",
4266#ifdef FEAT_CMDL_INFO
4267 1
4268#else
4269 0
4270#endif
4271 },
4272 {"signs",
4273#ifdef FEAT_SIGNS
4274 1
4275#else
4276 0
4277#endif
4278 },
4279 {"smartindent",
4280#ifdef FEAT_SMARTINDENT
4281 1
4282#else
4283 0
4284#endif
4285 },
4286 {"startuptime",
4287#ifdef STARTUPTIME
4288 1
4289#else
4290 0
4291#endif
4292 },
4293 {"statusline",
4294#ifdef FEAT_STL_OPT
4295 1
4296#else
4297 0
4298#endif
4299 },
4300 {"netbeans_intg",
4301#ifdef FEAT_NETBEANS_INTG
4302 1
4303#else
4304 0
4305#endif
4306 },
4307 {"sound",
4308#ifdef FEAT_SOUND
4309 1
4310#else
4311 0
4312#endif
4313 },
4314 {"spell",
4315#ifdef FEAT_SPELL
4316 1
4317#else
4318 0
4319#endif
4320 },
4321 {"syntax",
4322#ifdef FEAT_SYN_HL
4323 1
4324#else
4325 0
4326#endif
4327 },
4328 {"system",
4329#if defined(USE_SYSTEM) || !defined(UNIX)
4330 1
4331#else
4332 0
4333#endif
4334 },
4335 {"tag_binary",
4336#ifdef FEAT_TAG_BINS
4337 1
4338#else
4339 0
4340#endif
4341 },
4342 {"tcl",
4343#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4344 1
4345#else
4346 0
4347#endif
4348 },
4349 {"termguicolors",
4350#ifdef FEAT_TERMGUICOLORS
4351 1
4352#else
4353 0
4354#endif
4355 },
4356 {"terminal",
4357#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4358 1
4359#else
4360 0
4361#endif
4362 },
4363 {"terminfo",
4364#ifdef TERMINFO
4365 1
4366#else
4367 0
4368#endif
4369 },
4370 {"termresponse",
4371#ifdef FEAT_TERMRESPONSE
4372 1
4373#else
4374 0
4375#endif
4376 },
4377 {"textobjects",
4378#ifdef FEAT_TEXTOBJ
4379 1
4380#else
4381 0
4382#endif
4383 },
4384 {"textprop",
4385#ifdef FEAT_PROP_POPUP
4386 1
4387#else
4388 0
4389#endif
4390 },
4391 {"tgetent",
4392#ifdef HAVE_TGETENT
4393 1
4394#else
4395 0
4396#endif
4397 },
4398 {"timers",
4399#ifdef FEAT_TIMERS
4400 1
4401#else
4402 0
4403#endif
4404 },
4405 {"title",
4406#ifdef FEAT_TITLE
4407 1
4408#else
4409 0
4410#endif
4411 },
4412 {"toolbar",
4413#ifdef FEAT_TOOLBAR
4414 1
4415#else
4416 0
4417#endif
4418 },
4419 {"unnamedplus",
4420#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4421 1
4422#else
4423 0
4424#endif
4425 },
4426 {"user-commands", 1}, // was accidentally included in 5.4
4427 {"user_commands", 1},
4428 {"vartabs",
4429#ifdef FEAT_VARTABS
4430 1
4431#else
4432 0
4433#endif
4434 },
4435 {"vertsplit", 1},
4436 {"viminfo",
4437#ifdef FEAT_VIMINFO
4438 1
4439#else
4440 0
4441#endif
4442 },
4443 {"vimscript-1", 1},
4444 {"vimscript-2", 1},
4445 {"vimscript-3", 1},
4446 {"vimscript-4", 1},
4447 {"virtualedit", 1},
4448 {"visual", 1},
4449 {"visualextra", 1},
4450 {"vreplace", 1},
4451 {"vtp",
4452#ifdef FEAT_VTP
4453 1
4454#else
4455 0
4456#endif
4457 },
4458 {"wildignore",
4459#ifdef FEAT_WILDIGN
4460 1
4461#else
4462 0
4463#endif
4464 },
4465 {"wildmenu",
4466#ifdef FEAT_WILDMENU
4467 1
4468#else
4469 0
4470#endif
4471 },
4472 {"windows", 1},
4473 {"winaltkeys",
4474#ifdef FEAT_WAK
4475 1
4476#else
4477 0
4478#endif
4479 },
4480 {"writebackup",
4481#ifdef FEAT_WRITEBACKUP
4482 1
4483#else
4484 0
4485#endif
4486 },
4487 {"xim",
4488#ifdef FEAT_XIM
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"xfontset",
4495#ifdef FEAT_XFONTSET
4496 1
4497#else
4498 0
4499#endif
4500 },
4501 {"xpm",
4502#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4503 1
4504#else
4505 0
4506#endif
4507 },
4508 {"xpm_w32", // for backward compatibility
4509#ifdef FEAT_XPM_W32
4510 1
4511#else
4512 0
4513#endif
4514 },
4515 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004517 1
4518#else
4519 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004521 },
4522 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004524 1
4525#else
4526 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 },
4529 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004531 1
4532#else
4533 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 },
4536 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004537#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004538 1
4539#else
4540 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 },
4543 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004545 1
4546#else
4547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 },
4550 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551 };
4552
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004553 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 for (i = 0; has_list[i].name != NULL; ++i)
4555 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004557 x = TRUE;
4558 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559 break;
4560 }
4561
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 // features also in has_list[] but sometimes enabled at runtime
4563 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004566 {
4567 // intentionally empty
4568 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004569#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570 else if (STRICMP(name, "balloon_multiline") == 0)
4571 n = multiline_balloon_available();
4572#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004573#ifdef VIMDLL
4574 else if (STRICMP(name, "filterpipe") == 0)
4575 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#endif
4577#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4578 else if (STRICMP(name, "iconv") == 0)
4579 n = iconv_enabled(FALSE);
4580#endif
4581#ifdef DYNAMIC_LUA
4582 else if (STRICMP(name, "lua") == 0)
4583 n = lua_enabled(FALSE);
4584#endif
4585#ifdef DYNAMIC_MZSCHEME
4586 else if (STRICMP(name, "mzscheme") == 0)
4587 n = mzscheme_enabled(FALSE);
4588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004589#ifdef DYNAMIC_PERL
4590 else if (STRICMP(name, "perl") == 0)
4591 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#ifdef DYNAMIC_PYTHON
4594 else if (STRICMP(name, "python") == 0)
4595 n = python_enabled(FALSE);
4596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597#ifdef DYNAMIC_PYTHON3
4598 else if (STRICMP(name, "python3") == 0)
4599 n = python3_enabled(FALSE);
4600#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004601#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4602 else if (STRICMP(name, "pythonx") == 0)
4603 {
4604# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4605 if (p_pyx == 0)
4606 n = python3_enabled(FALSE) || python_enabled(FALSE);
4607 else if (p_pyx == 3)
4608 n = python3_enabled(FALSE);
4609 else if (p_pyx == 2)
4610 n = python_enabled(FALSE);
4611# elif defined(DYNAMIC_PYTHON)
4612 n = python_enabled(FALSE);
4613# elif defined(DYNAMIC_PYTHON3)
4614 n = python3_enabled(FALSE);
4615# endif
4616 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004618#ifdef DYNAMIC_RUBY
4619 else if (STRICMP(name, "ruby") == 0)
4620 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004622#ifdef DYNAMIC_TCL
4623 else if (STRICMP(name, "tcl") == 0)
4624 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004625#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004626#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004627 else if (STRICMP(name, "terminal") == 0)
4628 n = terminal_enabled();
4629#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630 }
4631
Bram Moolenaar79296512020-03-22 16:17:14 +01004632 // features not in has_list[]
4633 if (x == FALSE)
4634 {
4635 if (STRNICMP(name, "patch", 5) == 0)
4636 {
4637 x = TRUE;
4638 if (name[5] == '-'
4639 && STRLEN(name) >= 11
4640 && vim_isdigit(name[6])
4641 && vim_isdigit(name[8])
4642 && vim_isdigit(name[10]))
4643 {
4644 int major = atoi((char *)name + 6);
4645 int minor = atoi((char *)name + 8);
4646
4647 // Expect "patch-9.9.01234".
4648 n = (major < VIM_VERSION_MAJOR
4649 || (major == VIM_VERSION_MAJOR
4650 && (minor < VIM_VERSION_MINOR
4651 || (minor == VIM_VERSION_MINOR
4652 && has_patch(atoi((char *)name + 10))))));
4653 }
4654 else
4655 n = has_patch(atoi((char *)name + 5));
4656 }
4657 else if (STRICMP(name, "vim_starting") == 0)
4658 {
4659 x = TRUE;
4660 n = (starting != 0);
4661 }
4662 else if (STRICMP(name, "ttyin") == 0)
4663 {
4664 x = TRUE;
4665 n = mch_input_isatty();
4666 }
4667 else if (STRICMP(name, "ttyout") == 0)
4668 {
4669 x = TRUE;
4670 n = stdout_isatty;
4671 }
4672 else if (STRICMP(name, "multi_byte_encoding") == 0)
4673 {
4674 x = TRUE;
4675 n = has_mbyte;
4676 }
4677 else if (STRICMP(name, "gui_running") == 0)
4678 {
4679 x = TRUE;
4680#ifdef FEAT_GUI
4681 n = (gui.in_use || gui.starting);
4682#endif
4683 }
4684 else if (STRICMP(name, "browse") == 0)
4685 {
4686 x = TRUE;
4687#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4688 n = gui.in_use; // gui_mch_browse() works when GUI is running
4689#endif
4690 }
4691 else if (STRICMP(name, "syntax_items") == 0)
4692 {
4693 x = TRUE;
4694#ifdef FEAT_SYN_HL
4695 n = syntax_present(curwin);
4696#endif
4697 }
4698 else if (STRICMP(name, "vcon") == 0)
4699 {
4700 x = TRUE;
4701#ifdef FEAT_VTP
4702 n = is_term_win32() && has_vtp_working();
4703#endif
4704 }
4705 else if (STRICMP(name, "netbeans_enabled") == 0)
4706 {
4707 x = TRUE;
4708#ifdef FEAT_NETBEANS_INTG
4709 n = netbeans_active();
4710#endif
4711 }
4712 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4713 {
4714 x = TRUE;
4715#ifdef FEAT_MOUSE_GPM
4716 n = gpm_enabled();
4717#endif
4718 }
4719 else if (STRICMP(name, "conpty") == 0)
4720 {
4721 x = TRUE;
4722#if defined(FEAT_TERMINAL) && defined(MSWIN)
4723 n = use_conpty();
4724#endif
4725 }
4726 else if (STRICMP(name, "clipboard_working") == 0)
4727 {
4728 x = TRUE;
4729#ifdef FEAT_CLIPBOARD
4730 n = clip_star.available;
4731#endif
4732 }
4733 }
4734
4735 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4736 // return whether feature could ever be enabled
4737 rettv->vval.v_number = x;
4738 else
4739 // return whether feature is enabled
4740 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741}
4742
4743/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004744 * "haslocaldir()" function
4745 */
4746 static void
4747f_haslocaldir(typval_T *argvars, typval_T *rettv)
4748{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004749 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750 win_T *wp = NULL;
4751
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004752 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4753
4754 // Check for window-local and tab-local directories
4755 if (wp != NULL && wp->w_localdir != NULL)
4756 rettv->vval.v_number = 1;
4757 else if (tp != NULL && tp->tp_localdir != NULL)
4758 rettv->vval.v_number = 2;
4759 else
4760 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761}
4762
4763/*
4764 * "hasmapto()" function
4765 */
4766 static void
4767f_hasmapto(typval_T *argvars, typval_T *rettv)
4768{
4769 char_u *name;
4770 char_u *mode;
4771 char_u buf[NUMBUFLEN];
4772 int abbr = FALSE;
4773
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004774 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004775 if (argvars[1].v_type == VAR_UNKNOWN)
4776 mode = (char_u *)"nvo";
4777 else
4778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004779 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782 }
4783
4784 if (map_to_exists(name, mode, abbr))
4785 rettv->vval.v_number = TRUE;
4786 else
4787 rettv->vval.v_number = FALSE;
4788}
4789
4790/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 * "highlightID(name)" function
4792 */
4793 static void
4794f_hlID(typval_T *argvars, typval_T *rettv)
4795{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004796 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797}
4798
4799/*
4800 * "highlight_exists()" function
4801 */
4802 static void
4803f_hlexists(typval_T *argvars, typval_T *rettv)
4804{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004806}
4807
4808/*
4809 * "hostname()" function
4810 */
4811 static void
4812f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4813{
4814 char_u hostname[256];
4815
4816 mch_get_host_name(hostname, 256);
4817 rettv->v_type = VAR_STRING;
4818 rettv->vval.v_string = vim_strsave(hostname);
4819}
4820
4821/*
4822 * iconv() function
4823 */
4824 static void
4825f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4826{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004827 char_u buf1[NUMBUFLEN];
4828 char_u buf2[NUMBUFLEN];
4829 char_u *from, *to, *str;
4830 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004831
4832 rettv->v_type = VAR_STRING;
4833 rettv->vval.v_string = NULL;
4834
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004835 str = tv_get_string(&argvars[0]);
4836 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4837 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838 vimconv.vc_type = CONV_NONE;
4839 convert_setup(&vimconv, from, to);
4840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004841 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842 if (vimconv.vc_type == CONV_NONE)
4843 rettv->vval.v_string = vim_strsave(str);
4844 else
4845 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4846
4847 convert_setup(&vimconv, NULL, NULL);
4848 vim_free(from);
4849 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850}
4851
4852/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 * "index()" function
4854 */
4855 static void
4856f_index(typval_T *argvars, typval_T *rettv)
4857{
4858 list_T *l;
4859 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004860 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861 long idx = 0;
4862 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004863 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004864
4865 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004866 if (argvars[0].v_type == VAR_BLOB)
4867 {
4868 typval_T tv;
4869 int start = 0;
4870
4871 if (argvars[2].v_type != VAR_UNKNOWN)
4872 {
4873 start = tv_get_number_chk(&argvars[2], &error);
4874 if (error)
4875 return;
4876 }
4877 b = argvars[0].vval.v_blob;
4878 if (b == NULL)
4879 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004880 if (start < 0)
4881 {
4882 start = blob_len(b) + start;
4883 if (start < 0)
4884 start = 0;
4885 }
4886
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004887 for (idx = start; idx < blob_len(b); ++idx)
4888 {
4889 tv.v_type = VAR_NUMBER;
4890 tv.vval.v_number = blob_get(b, idx);
4891 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4892 {
4893 rettv->vval.v_number = idx;
4894 return;
4895 }
4896 }
4897 return;
4898 }
4899 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004901 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 return;
4903 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004904
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004905 l = argvars[0].vval.v_list;
4906 if (l != NULL)
4907 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004908 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909 item = l->lv_first;
4910 if (argvars[2].v_type != VAR_UNKNOWN)
4911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 // Start at specified item. Use the cached index that list_find()
4913 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004914 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004915 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004916 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004917 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 if (error)
4919 item = NULL;
4920 }
4921
4922 for ( ; item != NULL; item = item->li_next, ++idx)
4923 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4924 {
4925 rettv->vval.v_number = idx;
4926 break;
4927 }
4928 }
4929}
4930
4931static int inputsecret_flag = 0;
4932
4933/*
4934 * "input()" function
4935 * Also handles inputsecret() when inputsecret is set.
4936 */
4937 static void
4938f_input(typval_T *argvars, typval_T *rettv)
4939{
4940 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4941}
4942
4943/*
4944 * "inputdialog()" function
4945 */
4946 static void
4947f_inputdialog(typval_T *argvars, typval_T *rettv)
4948{
4949#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4952 {
4953 char_u *message;
4954 char_u buf[NUMBUFLEN];
4955 char_u *defstr = (char_u *)"";
4956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004957 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004959 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4961 else
4962 IObuff[0] = NUL;
4963 if (message != NULL && defstr != NULL
4964 && do_dialog(VIM_QUESTION, NULL, message,
4965 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4966 rettv->vval.v_string = vim_strsave(IObuff);
4967 else
4968 {
4969 if (message != NULL && defstr != NULL
4970 && argvars[1].v_type != VAR_UNKNOWN
4971 && argvars[2].v_type != VAR_UNKNOWN)
4972 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004973 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004974 else
4975 rettv->vval.v_string = NULL;
4976 }
4977 rettv->v_type = VAR_STRING;
4978 }
4979 else
4980#endif
4981 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4982}
4983
4984/*
4985 * "inputlist()" function
4986 */
4987 static void
4988f_inputlist(typval_T *argvars, typval_T *rettv)
4989{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004990 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 listitem_T *li;
4992 int selected;
4993 int mouse_used;
4994
4995#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 // While starting up, there is no place to enter text. When running tests
4997 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02004998 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999 return;
5000#endif
5001 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005003 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005004 return;
5005 }
5006
5007 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 msg_row = Rows - 1; // for when 'cmdheight' > 1
5009 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010 msg_scroll = TRUE;
5011 msg_clr_eos();
5012
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005013 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005014 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005015 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005017 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 msg_putchar('\n');
5019 }
5020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 selected = prompt_for_number(&mouse_used);
5023 if (mouse_used)
5024 selected -= lines_left;
5025
5026 rettv->vval.v_number = selected;
5027}
5028
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5030
5031/*
5032 * "inputrestore()" function
5033 */
5034 static void
5035f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5036{
5037 if (ga_userinput.ga_len > 0)
5038 {
5039 --ga_userinput.ga_len;
5040 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5041 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 }
5044 else if (p_verbose > 1)
5045 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005046 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005048 }
5049}
5050
5051/*
5052 * "inputsave()" function
5053 */
5054 static void
5055f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5056{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005057 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005058 if (ga_grow(&ga_userinput, 1) == OK)
5059 {
5060 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5061 + ga_userinput.ga_len);
5062 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005063 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005064 }
5065 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005067}
5068
5069/*
5070 * "inputsecret()" function
5071 */
5072 static void
5073f_inputsecret(typval_T *argvars, typval_T *rettv)
5074{
5075 ++cmdline_star;
5076 ++inputsecret_flag;
5077 f_input(argvars, rettv);
5078 --cmdline_star;
5079 --inputsecret_flag;
5080}
5081
5082/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005083 * "interrupt()" function
5084 */
5085 static void
5086f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5087{
5088 got_int = TRUE;
5089}
5090
5091/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 * "invert(expr)" function
5093 */
5094 static void
5095f_invert(typval_T *argvars, typval_T *rettv)
5096{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005097 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098}
5099
5100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 * "islocked()" function
5102 */
5103 static void
5104f_islocked(typval_T *argvars, typval_T *rettv)
5105{
5106 lval_T lv;
5107 char_u *end;
5108 dictitem_T *di;
5109
5110 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005111 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005112 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113 if (end != NULL && lv.ll_name != NULL)
5114 {
5115 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005116 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117 else
5118 {
5119 if (lv.ll_tv == NULL)
5120 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005121 di = find_var(lv.ll_name, NULL, TRUE);
5122 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 // Consider a variable locked when:
5125 // 1. the variable itself is locked
5126 // 2. the value of the variable is locked.
5127 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005128 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5129 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130 }
5131 }
5132 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005135 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005138 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5139 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005140 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5142 }
5143 }
5144
5145 clear_lval(&lv);
5146}
5147
5148#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5149/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005150 * "isinf()" function
5151 */
5152 static void
5153f_isinf(typval_T *argvars, typval_T *rettv)
5154{
5155 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5156 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5157}
5158
5159/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 * "isnan()" function
5161 */
5162 static void
5163f_isnan(typval_T *argvars, typval_T *rettv)
5164{
5165 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5166 && isnan(argvars[0].vval.v_float);
5167}
5168#endif
5169
5170/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 * "last_buffer_nr()" function.
5172 */
5173 static void
5174f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5175{
5176 int n = 0;
5177 buf_T *buf;
5178
Bram Moolenaar29323592016-07-24 22:04:11 +02005179 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005180 if (n < buf->b_fnum)
5181 n = buf->b_fnum;
5182
5183 rettv->vval.v_number = n;
5184}
5185
5186/*
5187 * "len()" function
5188 */
5189 static void
5190f_len(typval_T *argvars, typval_T *rettv)
5191{
5192 switch (argvars[0].v_type)
5193 {
5194 case VAR_STRING:
5195 case VAR_NUMBER:
5196 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005197 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005199 case VAR_BLOB:
5200 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5201 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 case VAR_LIST:
5203 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5204 break;
5205 case VAR_DICT:
5206 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5207 break;
5208 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005209 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005211 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212 case VAR_SPECIAL:
5213 case VAR_FLOAT:
5214 case VAR_FUNC:
5215 case VAR_PARTIAL:
5216 case VAR_JOB:
5217 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005218 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219 break;
5220 }
5221}
5222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005224libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225{
5226#ifdef FEAT_LIBCALL
5227 char_u *string_in;
5228 char_u **string_result;
5229 int nr_result;
5230#endif
5231
5232 rettv->v_type = type;
5233 if (type != VAR_NUMBER)
5234 rettv->vval.v_string = NULL;
5235
5236 if (check_restricted() || check_secure())
5237 return;
5238
5239#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5242 {
5243 string_in = NULL;
5244 if (argvars[2].v_type == VAR_STRING)
5245 string_in = argvars[2].vval.v_string;
5246 if (type == VAR_NUMBER)
5247 string_result = NULL;
5248 else
5249 string_result = &rettv->vval.v_string;
5250 if (mch_libcall(argvars[0].vval.v_string,
5251 argvars[1].vval.v_string,
5252 string_in,
5253 argvars[2].vval.v_number,
5254 string_result,
5255 &nr_result) == OK
5256 && type == VAR_NUMBER)
5257 rettv->vval.v_number = nr_result;
5258 }
5259#endif
5260}
5261
5262/*
5263 * "libcall()" function
5264 */
5265 static void
5266f_libcall(typval_T *argvars, typval_T *rettv)
5267{
5268 libcall_common(argvars, rettv, VAR_STRING);
5269}
5270
5271/*
5272 * "libcallnr()" function
5273 */
5274 static void
5275f_libcallnr(typval_T *argvars, typval_T *rettv)
5276{
5277 libcall_common(argvars, rettv, VAR_NUMBER);
5278}
5279
5280/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005281 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 */
5283 static void
5284f_line(typval_T *argvars, typval_T *rettv)
5285{
5286 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005287 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005289 int id;
5290 tabpage_T *tp;
5291 win_T *wp;
5292 win_T *save_curwin;
5293 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005295 if (argvars[1].v_type != VAR_UNKNOWN)
5296 {
5297 // use window specified in the second argument
5298 id = (int)tv_get_number(&argvars[1]);
5299 wp = win_id2wp_tp(id, &tp);
5300 if (wp != NULL && tp != NULL)
5301 {
5302 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5303 == OK)
5304 {
5305 check_cursor();
5306 fp = var2fpos(&argvars[0], TRUE, &fnum);
5307 }
5308 restore_win_noblock(save_curwin, save_curtab, TRUE);
5309 }
5310 }
5311 else
5312 // use current window
5313 fp = var2fpos(&argvars[0], TRUE, &fnum);
5314
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005315 if (fp != NULL)
5316 lnum = fp->lnum;
5317 rettv->vval.v_number = lnum;
5318}
5319
5320/*
5321 * "line2byte(lnum)" function
5322 */
5323 static void
5324f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5325{
5326#ifndef FEAT_BYTEOFF
5327 rettv->vval.v_number = -1;
5328#else
5329 linenr_T lnum;
5330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005331 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5333 rettv->vval.v_number = -1;
5334 else
5335 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5336 if (rettv->vval.v_number >= 0)
5337 ++rettv->vval.v_number;
5338#endif
5339}
5340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341#ifdef FEAT_FLOAT
5342/*
5343 * "log()" function
5344 */
5345 static void
5346f_log(typval_T *argvars, typval_T *rettv)
5347{
5348 float_T f = 0.0;
5349
5350 rettv->v_type = VAR_FLOAT;
5351 if (get_float_arg(argvars, &f) == OK)
5352 rettv->vval.v_float = log(f);
5353 else
5354 rettv->vval.v_float = 0.0;
5355}
5356
5357/*
5358 * "log10()" function
5359 */
5360 static void
5361f_log10(typval_T *argvars, typval_T *rettv)
5362{
5363 float_T f = 0.0;
5364
5365 rettv->v_type = VAR_FLOAT;
5366 if (get_float_arg(argvars, &f) == OK)
5367 rettv->vval.v_float = log10(f);
5368 else
5369 rettv->vval.v_float = 0.0;
5370}
5371#endif
5372
5373#ifdef FEAT_LUA
5374/*
5375 * "luaeval()" function
5376 */
5377 static void
5378f_luaeval(typval_T *argvars, typval_T *rettv)
5379{
5380 char_u *str;
5381 char_u buf[NUMBUFLEN];
5382
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005383 if (check_restricted() || check_secure())
5384 return;
5385
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005386 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005387 do_luaeval(str, argvars + 1, rettv);
5388}
5389#endif
5390
5391/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005392 * "maparg()" function
5393 */
5394 static void
5395f_maparg(typval_T *argvars, typval_T *rettv)
5396{
5397 get_maparg(argvars, rettv, TRUE);
5398}
5399
5400/*
5401 * "mapcheck()" function
5402 */
5403 static void
5404f_mapcheck(typval_T *argvars, typval_T *rettv)
5405{
5406 get_maparg(argvars, rettv, FALSE);
5407}
5408
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005409typedef enum
5410{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005411 MATCH_END, // matchend()
5412 MATCH_MATCH, // match()
5413 MATCH_STR, // matchstr()
5414 MATCH_LIST, // matchlist()
5415 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005416} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417
5418 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005419find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420{
5421 char_u *str = NULL;
5422 long len = 0;
5423 char_u *expr = NULL;
5424 char_u *pat;
5425 regmatch_T regmatch;
5426 char_u patbuf[NUMBUFLEN];
5427 char_u strbuf[NUMBUFLEN];
5428 char_u *save_cpo;
5429 long start = 0;
5430 long nth = 1;
5431 colnr_T startcol = 0;
5432 int match = 0;
5433 list_T *l = NULL;
5434 listitem_T *li = NULL;
5435 long idx = 0;
5436 char_u *tofree = NULL;
5437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005438 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005439 save_cpo = p_cpo;
5440 p_cpo = (char_u *)"";
5441
5442 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005443 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005444 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005445 // type MATCH_LIST: return empty list when there are no matches.
5446 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447 if (rettv_list_alloc(rettv) == FAIL)
5448 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005449 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005450 && (list_append_string(rettv->vval.v_list,
5451 (char_u *)"", 0) == FAIL
5452 || list_append_number(rettv->vval.v_list,
5453 (varnumber_T)-1) == FAIL
5454 || list_append_number(rettv->vval.v_list,
5455 (varnumber_T)-1) == FAIL
5456 || list_append_number(rettv->vval.v_list,
5457 (varnumber_T)-1) == FAIL))
5458 {
5459 list_free(rettv->vval.v_list);
5460 rettv->vval.v_list = NULL;
5461 goto theend;
5462 }
5463 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005464 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005465 {
5466 rettv->v_type = VAR_STRING;
5467 rettv->vval.v_string = NULL;
5468 }
5469
5470 if (argvars[0].v_type == VAR_LIST)
5471 {
5472 if ((l = argvars[0].vval.v_list) == NULL)
5473 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005474 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475 li = l->lv_first;
5476 }
5477 else
5478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005479 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 len = (long)STRLEN(str);
5481 }
5482
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005483 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 if (pat == NULL)
5485 goto theend;
5486
5487 if (argvars[2].v_type != VAR_UNKNOWN)
5488 {
5489 int error = FALSE;
5490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005491 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 if (error)
5493 goto theend;
5494 if (l != NULL)
5495 {
5496 li = list_find(l, start);
5497 if (li == NULL)
5498 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005499 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 }
5501 else
5502 {
5503 if (start < 0)
5504 start = 0;
5505 if (start > len)
5506 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005507 // When "count" argument is there ignore matches before "start",
5508 // otherwise skip part of the string. Differs when pattern is "^"
5509 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 if (argvars[3].v_type != VAR_UNKNOWN)
5511 startcol = start;
5512 else
5513 {
5514 str += start;
5515 len -= start;
5516 }
5517 }
5518
5519 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005520 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521 if (error)
5522 goto theend;
5523 }
5524
5525 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5526 if (regmatch.regprog != NULL)
5527 {
5528 regmatch.rm_ic = p_ic;
5529
5530 for (;;)
5531 {
5532 if (l != NULL)
5533 {
5534 if (li == NULL)
5535 {
5536 match = FALSE;
5537 break;
5538 }
5539 vim_free(tofree);
5540 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5541 if (str == NULL)
5542 break;
5543 }
5544
5545 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5546
5547 if (match && --nth <= 0)
5548 break;
5549 if (l == NULL && !match)
5550 break;
5551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005552 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 if (l != NULL)
5554 {
5555 li = li->li_next;
5556 ++idx;
5557 }
5558 else
5559 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 startcol = (colnr_T)(regmatch.startp[0]
5561 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562 if (startcol > (colnr_T)len
5563 || str + startcol <= regmatch.startp[0])
5564 {
5565 match = FALSE;
5566 break;
5567 }
5568 }
5569 }
5570
5571 if (match)
5572 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005573 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 {
5575 listitem_T *li1 = rettv->vval.v_list->lv_first;
5576 listitem_T *li2 = li1->li_next;
5577 listitem_T *li3 = li2->li_next;
5578 listitem_T *li4 = li3->li_next;
5579
5580 vim_free(li1->li_tv.vval.v_string);
5581 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005582 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 li3->li_tv.vval.v_number =
5584 (varnumber_T)(regmatch.startp[0] - expr);
5585 li4->li_tv.vval.v_number =
5586 (varnumber_T)(regmatch.endp[0] - expr);
5587 if (l != NULL)
5588 li2->li_tv.vval.v_number = (varnumber_T)idx;
5589 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005590 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005591 {
5592 int i;
5593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005594 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595 for (i = 0; i < NSUBEXP; ++i)
5596 {
5597 if (regmatch.endp[i] == NULL)
5598 {
5599 if (list_append_string(rettv->vval.v_list,
5600 (char_u *)"", 0) == FAIL)
5601 break;
5602 }
5603 else if (list_append_string(rettv->vval.v_list,
5604 regmatch.startp[i],
5605 (int)(regmatch.endp[i] - regmatch.startp[i]))
5606 == FAIL)
5607 break;
5608 }
5609 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005610 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005612 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 if (l != NULL)
5614 copy_tv(&li->li_tv, rettv);
5615 else
5616 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005617 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005618 }
5619 else if (l != NULL)
5620 rettv->vval.v_number = idx;
5621 else
5622 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005623 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 rettv->vval.v_number =
5625 (varnumber_T)(regmatch.startp[0] - str);
5626 else
5627 rettv->vval.v_number =
5628 (varnumber_T)(regmatch.endp[0] - str);
5629 rettv->vval.v_number += (varnumber_T)(str - expr);
5630 }
5631 }
5632 vim_regfree(regmatch.regprog);
5633 }
5634
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005635theend:
5636 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638 listitem_remove(rettv->vval.v_list,
5639 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005640 vim_free(tofree);
5641 p_cpo = save_cpo;
5642}
5643
5644/*
5645 * "match()" function
5646 */
5647 static void
5648f_match(typval_T *argvars, typval_T *rettv)
5649{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005650 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651}
5652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653/*
5654 * "matchend()" function
5655 */
5656 static void
5657f_matchend(typval_T *argvars, typval_T *rettv)
5658{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005659 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660}
5661
5662/*
5663 * "matchlist()" function
5664 */
5665 static void
5666f_matchlist(typval_T *argvars, typval_T *rettv)
5667{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005668 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669}
5670
5671/*
5672 * "matchstr()" function
5673 */
5674 static void
5675f_matchstr(typval_T *argvars, typval_T *rettv)
5676{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005677 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678}
5679
5680/*
5681 * "matchstrpos()" function
5682 */
5683 static void
5684f_matchstrpos(typval_T *argvars, typval_T *rettv)
5685{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005686 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687}
5688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689 static void
5690max_min(typval_T *argvars, typval_T *rettv, int domax)
5691{
5692 varnumber_T n = 0;
5693 varnumber_T i;
5694 int error = FALSE;
5695
5696 if (argvars[0].v_type == VAR_LIST)
5697 {
5698 list_T *l;
5699 listitem_T *li;
5700
5701 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005702 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005704 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005706 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5707 n = l->lv_u.nonmat.lv_start;
5708 else
5709 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5710 * l->lv_u.nonmat.lv_stride;
5711 }
5712 else
5713 {
5714 li = l->lv_first;
5715 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005717 n = tv_get_number_chk(&li->li_tv, &error);
5718 for (;;)
5719 {
5720 li = li->li_next;
5721 if (li == NULL)
5722 break;
5723 i = tv_get_number_chk(&li->li_tv, &error);
5724 if (domax ? i > n : i < n)
5725 n = i;
5726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005727 }
5728 }
5729 }
5730 }
5731 else if (argvars[0].v_type == VAR_DICT)
5732 {
5733 dict_T *d;
5734 int first = TRUE;
5735 hashitem_T *hi;
5736 int todo;
5737
5738 d = argvars[0].vval.v_dict;
5739 if (d != NULL)
5740 {
5741 todo = (int)d->dv_hashtab.ht_used;
5742 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5743 {
5744 if (!HASHITEM_EMPTY(hi))
5745 {
5746 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005747 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005748 if (first)
5749 {
5750 n = i;
5751 first = FALSE;
5752 }
5753 else if (domax ? i > n : i < n)
5754 n = i;
5755 }
5756 }
5757 }
5758 }
5759 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005760 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005761 rettv->vval.v_number = error ? 0 : n;
5762}
5763
5764/*
5765 * "max()" function
5766 */
5767 static void
5768f_max(typval_T *argvars, typval_T *rettv)
5769{
5770 max_min(argvars, rettv, TRUE);
5771}
5772
5773/*
5774 * "min()" function
5775 */
5776 static void
5777f_min(typval_T *argvars, typval_T *rettv)
5778{
5779 max_min(argvars, rettv, FALSE);
5780}
5781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782#if defined(FEAT_MZSCHEME) || defined(PROTO)
5783/*
5784 * "mzeval()" function
5785 */
5786 static void
5787f_mzeval(typval_T *argvars, typval_T *rettv)
5788{
5789 char_u *str;
5790 char_u buf[NUMBUFLEN];
5791
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005792 if (check_restricted() || check_secure())
5793 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005794 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 do_mzeval(str, rettv);
5796}
5797
5798 void
5799mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5800{
5801 typval_T argvars[3];
5802
5803 argvars[0].v_type = VAR_STRING;
5804 argvars[0].vval.v_string = name;
5805 copy_tv(args, &argvars[1]);
5806 argvars[2].v_type = VAR_UNKNOWN;
5807 f_call(argvars, rettv);
5808 clear_tv(&argvars[1]);
5809}
5810#endif
5811
5812/*
5813 * "nextnonblank()" function
5814 */
5815 static void
5816f_nextnonblank(typval_T *argvars, typval_T *rettv)
5817{
5818 linenr_T lnum;
5819
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005820 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 {
5822 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5823 {
5824 lnum = 0;
5825 break;
5826 }
5827 if (*skipwhite(ml_get(lnum)) != NUL)
5828 break;
5829 }
5830 rettv->vval.v_number = lnum;
5831}
5832
5833/*
5834 * "nr2char()" function
5835 */
5836 static void
5837f_nr2char(typval_T *argvars, typval_T *rettv)
5838{
5839 char_u buf[NUMBUFLEN];
5840
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005841 if (has_mbyte)
5842 {
5843 int utf8 = 0;
5844
5845 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005846 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005848 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 }
5852 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005854 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855 buf[1] = NUL;
5856 }
5857 rettv->v_type = VAR_STRING;
5858 rettv->vval.v_string = vim_strsave(buf);
5859}
5860
5861/*
5862 * "or(expr, expr)" function
5863 */
5864 static void
5865f_or(typval_T *argvars, typval_T *rettv)
5866{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005867 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5868 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869}
5870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871#ifdef FEAT_PERL
5872/*
5873 * "perleval()" function
5874 */
5875 static void
5876f_perleval(typval_T *argvars, typval_T *rettv)
5877{
5878 char_u *str;
5879 char_u buf[NUMBUFLEN];
5880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 do_perleval(str, rettv);
5883}
5884#endif
5885
5886#ifdef FEAT_FLOAT
5887/*
5888 * "pow()" function
5889 */
5890 static void
5891f_pow(typval_T *argvars, typval_T *rettv)
5892{
5893 float_T fx = 0.0, fy = 0.0;
5894
5895 rettv->v_type = VAR_FLOAT;
5896 if (get_float_arg(argvars, &fx) == OK
5897 && get_float_arg(&argvars[1], &fy) == OK)
5898 rettv->vval.v_float = pow(fx, fy);
5899 else
5900 rettv->vval.v_float = 0.0;
5901}
5902#endif
5903
5904/*
5905 * "prevnonblank()" function
5906 */
5907 static void
5908f_prevnonblank(typval_T *argvars, typval_T *rettv)
5909{
5910 linenr_T lnum;
5911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005912 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5914 lnum = 0;
5915 else
5916 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5917 --lnum;
5918 rettv->vval.v_number = lnum;
5919}
5920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921// This dummy va_list is here because:
5922// - passing a NULL pointer doesn't work when va_list isn't a pointer
5923// - locally in the function results in a "used before set" warning
5924// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925static va_list ap;
5926
5927/*
5928 * "printf()" function
5929 */
5930 static void
5931f_printf(typval_T *argvars, typval_T *rettv)
5932{
5933 char_u buf[NUMBUFLEN];
5934 int len;
5935 char_u *s;
5936 int saved_did_emsg = did_emsg;
5937 char *fmt;
5938
5939 rettv->v_type = VAR_STRING;
5940 rettv->vval.v_string = NULL;
5941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005942 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005945 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946 if (!did_emsg)
5947 {
5948 s = alloc(len + 1);
5949 if (s != NULL)
5950 {
5951 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005952 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5953 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 }
5955 }
5956 did_emsg |= saved_did_emsg;
5957}
5958
5959/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005960 * "pum_getpos()" function
5961 */
5962 static void
5963f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5964{
5965 if (rettv_dict_alloc(rettv) != OK)
5966 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005967 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005968}
5969
5970/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971 * "pumvisible()" function
5972 */
5973 static void
5974f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5975{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976 if (pum_visible())
5977 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978}
5979
5980#ifdef FEAT_PYTHON3
5981/*
5982 * "py3eval()" function
5983 */
5984 static void
5985f_py3eval(typval_T *argvars, typval_T *rettv)
5986{
5987 char_u *str;
5988 char_u buf[NUMBUFLEN];
5989
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005990 if (check_restricted() || check_secure())
5991 return;
5992
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005993 if (p_pyx == 0)
5994 p_pyx = 3;
5995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005996 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005997 do_py3eval(str, rettv);
5998}
5999#endif
6000
6001#ifdef FEAT_PYTHON
6002/*
6003 * "pyeval()" function
6004 */
6005 static void
6006f_pyeval(typval_T *argvars, typval_T *rettv)
6007{
6008 char_u *str;
6009 char_u buf[NUMBUFLEN];
6010
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006011 if (check_restricted() || check_secure())
6012 return;
6013
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006014 if (p_pyx == 0)
6015 p_pyx = 2;
6016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006017 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 do_pyeval(str, rettv);
6019}
6020#endif
6021
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006022#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6023/*
6024 * "pyxeval()" function
6025 */
6026 static void
6027f_pyxeval(typval_T *argvars, typval_T *rettv)
6028{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006029 if (check_restricted() || check_secure())
6030 return;
6031
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006032# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6033 init_pyxversion();
6034 if (p_pyx == 2)
6035 f_pyeval(argvars, rettv);
6036 else
6037 f_py3eval(argvars, rettv);
6038# elif defined(FEAT_PYTHON)
6039 f_pyeval(argvars, rettv);
6040# elif defined(FEAT_PYTHON3)
6041 f_py3eval(argvars, rettv);
6042# endif
6043}
6044#endif
6045
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006046static UINT32_T srand_seed_for_testing = 0;
6047static int srand_seed_for_testing_is_used = FALSE;
6048
6049 static void
6050f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6051{
6052 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006053 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006054 else
6055 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006056 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6057 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006058 }
6059}
6060
6061 static void
6062init_srand(UINT32_T *x)
6063{
6064#ifndef MSWIN
6065 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6066#endif
6067
6068 if (srand_seed_for_testing_is_used)
6069 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006070 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006071 return;
6072 }
6073#ifndef MSWIN
6074 if (dev_urandom_state != FAIL)
6075 {
6076 int fd = open("/dev/urandom", O_RDONLY);
6077 struct {
6078 union {
6079 UINT32_T number;
6080 char bytes[sizeof(UINT32_T)];
6081 } contents;
6082 } buf;
6083
6084 // Attempt reading /dev/urandom.
6085 if (fd == -1)
6086 dev_urandom_state = FAIL;
6087 else
6088 {
6089 buf.contents.number = 0;
6090 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6091 != sizeof(UINT32_T))
6092 dev_urandom_state = FAIL;
6093 else
6094 {
6095 dev_urandom_state = OK;
6096 *x = buf.contents.number;
6097 }
6098 close(fd);
6099 }
6100 }
6101 if (dev_urandom_state != OK)
6102 // Reading /dev/urandom doesn't work, fall back to time().
6103#endif
6104 *x = vim_time();
6105}
6106
6107#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6108#define SPLITMIX32(x, z) ( \
6109 z = (x += 0x9e3779b9), \
6110 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6111 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6112 z ^ (z >> 16) \
6113 )
6114#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6115 result = ROTL(y * 5, 7) * 9; \
6116 t = y << 9; \
6117 z ^= x; \
6118 w ^= y; \
6119 y ^= z, x ^= w; \
6120 z ^= t; \
6121 w = ROTL(w, 11);
6122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006124 * "rand()" function
6125 */
6126 static void
6127f_rand(typval_T *argvars, typval_T *rettv)
6128{
6129 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006130 static UINT32_T gx, gy, gz, gw;
6131 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006132 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006133 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006134
6135 if (argvars[0].v_type == VAR_UNKNOWN)
6136 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006137 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006138 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006139 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006140 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006141 init_srand(&x);
6142
6143 gx = SPLITMIX32(x, z);
6144 gy = SPLITMIX32(x, z);
6145 gz = SPLITMIX32(x, z);
6146 gw = SPLITMIX32(x, z);
6147 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006148 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006149
6150 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006151 }
6152 else if (argvars[0].v_type == VAR_LIST)
6153 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006154 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006155 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006156 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006157
6158 lx = list_find(l, 0L);
6159 ly = list_find(l, 1L);
6160 lz = list_find(l, 2L);
6161 lw = list_find(l, 3L);
6162 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6163 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6164 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6165 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6166 x = (UINT32_T)lx->li_tv.vval.v_number;
6167 y = (UINT32_T)ly->li_tv.vval.v_number;
6168 z = (UINT32_T)lz->li_tv.vval.v_number;
6169 w = (UINT32_T)lw->li_tv.vval.v_number;
6170
6171 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6172
6173 lx->li_tv.vval.v_number = (varnumber_T)x;
6174 ly->li_tv.vval.v_number = (varnumber_T)y;
6175 lz->li_tv.vval.v_number = (varnumber_T)z;
6176 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006177 }
6178 else
6179 goto theend;
6180
6181 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006182 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006183 return;
6184
6185theend:
6186 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006187 rettv->v_type = VAR_NUMBER;
6188 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006189}
6190
6191/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006192 * "srand()" function
6193 */
6194 static void
6195f_srand(typval_T *argvars, typval_T *rettv)
6196{
6197 UINT32_T x = 0, z;
6198
6199 if (rettv_list_alloc(rettv) == FAIL)
6200 return;
6201 if (argvars[0].v_type == VAR_UNKNOWN)
6202 {
6203 init_srand(&x);
6204 }
6205 else
6206 {
6207 int error = FALSE;
6208
6209 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6210 if (error)
6211 return;
6212 }
6213
6214 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6215 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6216 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6217 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6218}
6219
6220#undef ROTL
6221#undef SPLITMIX32
6222#undef SHUFFLE_XOSHIRO128STARSTAR
6223
6224/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006225 * "range()" function
6226 */
6227 static void
6228f_range(typval_T *argvars, typval_T *rettv)
6229{
6230 varnumber_T start;
6231 varnumber_T end;
6232 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 int error = FALSE;
6234
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006235 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 if (argvars[1].v_type == VAR_UNKNOWN)
6237 {
6238 end = start - 1;
6239 start = 0;
6240 }
6241 else
6242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006243 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006245 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 }
6247
6248 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006251 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006253 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256 list_T *list = rettv->vval.v_list;
6257
6258 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006259 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 // be called.
6261 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006262 list->lv_u.nonmat.lv_start = start;
6263 list->lv_u.nonmat.lv_end = end;
6264 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006265 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 }
6267}
6268
6269/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006270 * Materialize "list".
6271 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 */
6273 void
6274range_list_materialize(list_T *list)
6275{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006276 varnumber_T start = list->lv_u.nonmat.lv_start;
6277 varnumber_T end = list->lv_u.nonmat.lv_end;
6278 int stride = list->lv_u.nonmat.lv_stride;
6279 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006281 list->lv_first = NULL;
6282 list->lv_u.mat.lv_last = NULL;
6283 list->lv_len = 0;
6284 list->lv_u.mat.lv_idx_item = NULL;
6285 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6286 if (list_append_number(list, (varnumber_T)i) == FAIL)
6287 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288}
6289
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006290/*
6291 * "getreginfo()" function
6292 */
6293 static void
6294f_getreginfo(typval_T *argvars, typval_T *rettv)
6295{
6296 char_u *strregname;
6297 int regname;
6298 char_u buf[NUMBUFLEN + 2];
6299 long reglen = 0;
6300 dict_T *dict;
6301 list_T *list;
6302
6303 if (argvars[0].v_type != VAR_UNKNOWN)
6304 {
6305 strregname = tv_get_string_chk(&argvars[0]);
6306 if (strregname == NULL)
6307 return;
6308 }
6309 else
6310 strregname = get_vim_var_str(VV_REG);
6311
6312 regname = (strregname == NULL ? '"' : *strregname);
6313 if (regname == 0 || regname == '@')
6314 regname = '"';
6315
6316 if (rettv_dict_alloc(rettv) == FAIL)
6317 return;
6318 dict = rettv->vval.v_dict;
6319
6320 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6321 if (list == NULL)
6322 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006323 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006324
6325 buf[0] = NUL;
6326 buf[1] = NUL;
6327 switch (get_reg_type(regname, &reglen))
6328 {
6329 case MLINE: buf[0] = 'V'; break;
6330 case MCHAR: buf[0] = 'v'; break;
6331 case MBLOCK:
6332 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6333 reglen + 1);
6334 break;
6335 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006336 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006337
6338 buf[0] = get_register_name(get_unname_register());
6339 buf[1] = NUL;
6340 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006341 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006342 else
6343 {
6344 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6345
6346 if (item != NULL)
6347 {
6348 item->di_tv.v_type = VAR_SPECIAL;
6349 item->di_tv.vval.v_number = regname == buf[0]
6350 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006351 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006352 }
6353 }
6354}
6355
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006356 static void
6357return_register(int regname, typval_T *rettv)
6358{
6359 char_u buf[2] = {0, 0};
6360
6361 buf[0] = (char_u)regname;
6362 rettv->v_type = VAR_STRING;
6363 rettv->vval.v_string = vim_strsave(buf);
6364}
6365
6366/*
6367 * "reg_executing()" function
6368 */
6369 static void
6370f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6371{
6372 return_register(reg_executing, rettv);
6373}
6374
6375/*
6376 * "reg_recording()" function
6377 */
6378 static void
6379f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6380{
6381 return_register(reg_recording, rettv);
6382}
6383
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 * "rename({from}, {to})" function
6386 */
6387 static void
6388f_rename(typval_T *argvars, typval_T *rettv)
6389{
6390 char_u buf[NUMBUFLEN];
6391
6392 if (check_restricted() || check_secure())
6393 rettv->vval.v_number = -1;
6394 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006395 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6396 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397}
6398
6399/*
6400 * "repeat()" function
6401 */
6402 static void
6403f_repeat(typval_T *argvars, typval_T *rettv)
6404{
6405 char_u *p;
6406 int n;
6407 int slen;
6408 int len;
6409 char_u *r;
6410 int i;
6411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006412 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 if (argvars[0].v_type == VAR_LIST)
6414 {
6415 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6416 while (n-- > 0)
6417 if (list_extend(rettv->vval.v_list,
6418 argvars[0].vval.v_list, NULL) == FAIL)
6419 break;
6420 }
6421 else
6422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006423 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 rettv->v_type = VAR_STRING;
6425 rettv->vval.v_string = NULL;
6426
6427 slen = (int)STRLEN(p);
6428 len = slen * n;
6429 if (len <= 0)
6430 return;
6431
6432 r = alloc(len + 1);
6433 if (r != NULL)
6434 {
6435 for (i = 0; i < n; i++)
6436 mch_memmove(r + i * slen, p, (size_t)slen);
6437 r[len] = NUL;
6438 }
6439
6440 rettv->vval.v_string = r;
6441 }
6442}
6443
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006444#define SP_NOMOVE 0x01 // don't move cursor
6445#define SP_REPEAT 0x02 // repeat to find outer pair
6446#define SP_RETCOUNT 0x04 // return matchcount
6447#define SP_SETPCMARK 0x08 // set previous context mark
6448#define SP_START 0x10 // accept match at start position
6449#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6450#define SP_END 0x40 // leave cursor at end of match
6451#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006452
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006453/*
6454 * Get flags for a search function.
6455 * Possibly sets "p_ws".
6456 * Returns BACKWARD, FORWARD or zero (for an error).
6457 */
6458 static int
6459get_search_arg(typval_T *varp, int *flagsp)
6460{
6461 int dir = FORWARD;
6462 char_u *flags;
6463 char_u nbuf[NUMBUFLEN];
6464 int mask;
6465
6466 if (varp->v_type != VAR_UNKNOWN)
6467 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006468 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006470 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471 while (*flags != NUL)
6472 {
6473 switch (*flags)
6474 {
6475 case 'b': dir = BACKWARD; break;
6476 case 'w': p_ws = TRUE; break;
6477 case 'W': p_ws = FALSE; break;
6478 default: mask = 0;
6479 if (flagsp != NULL)
6480 switch (*flags)
6481 {
6482 case 'c': mask = SP_START; break;
6483 case 'e': mask = SP_END; break;
6484 case 'm': mask = SP_RETCOUNT; break;
6485 case 'n': mask = SP_NOMOVE; break;
6486 case 'p': mask = SP_SUBPAT; break;
6487 case 'r': mask = SP_REPEAT; break;
6488 case 's': mask = SP_SETPCMARK; break;
6489 case 'z': mask = SP_COLUMN; break;
6490 }
6491 if (mask == 0)
6492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006493 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 dir = 0;
6495 }
6496 else
6497 *flagsp |= mask;
6498 }
6499 if (dir == 0)
6500 break;
6501 ++flags;
6502 }
6503 }
6504 return dir;
6505}
6506
6507/*
6508 * Shared by search() and searchpos() functions.
6509 */
6510 static int
6511search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6512{
6513 int flags;
6514 char_u *pat;
6515 pos_T pos;
6516 pos_T save_cursor;
6517 int save_p_ws = p_ws;
6518 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006519 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006522 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006523 long time_limit = 0;
6524#endif
6525 int options = SEARCH_KEEP;
6526 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006527 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006528 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006529 pos_T firstpos;
6530
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006531 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 if (dir == 0)
6534 goto theend;
6535 flags = *flagsp;
6536 if (flags & SP_START)
6537 options |= SEARCH_START;
6538 if (flags & SP_END)
6539 options |= SEARCH_END;
6540 if (flags & SP_COLUMN)
6541 options |= SEARCH_COL;
6542
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006543 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006546 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547 if (lnum_stop < 0)
6548 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549 if (argvars[3].v_type != VAR_UNKNOWN)
6550 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006551#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006552 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553 if (time_limit < 0)
6554 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006556 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006557 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558 }
6559
6560#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006561 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 profile_setlimit(time_limit, &tm);
6563#endif
6564
6565 /*
6566 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6567 * Check to make sure only those flags are set.
6568 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6569 * flags cannot be set. Check for that condition also.
6570 */
6571 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6572 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006574 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 goto theend;
6576 }
6577
6578 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006579 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006580 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006581 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6582#ifdef FEAT_RELTIME
6583 sia.sa_tm = &tm;
6584#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006585
6586 // Repeat until {skip} returns FALSE.
6587 for (;;)
6588 {
6589 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006590 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006591 // finding the first match again means there is no match where {skip}
6592 // evaluates to zero.
6593 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6594 subpatnum = FAIL;
6595
Bram Moolenaara9c01042020-06-07 14:50:50 +02006596 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006597 // didn't find it or no skip argument
6598 break;
6599 firstpos = pos;
6600
Bram Moolenaara9c01042020-06-07 14:50:50 +02006601 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006602 {
6603 int do_skip;
6604 int err;
6605 pos_T save_pos = curwin->w_cursor;
6606
6607 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006608 err = FALSE;
6609 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006610 curwin->w_cursor = save_pos;
6611 if (err)
6612 {
6613 // Evaluating {skip} caused an error, break here.
6614 subpatnum = FAIL;
6615 break;
6616 }
6617 if (!do_skip)
6618 break;
6619 }
6620 }
6621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 if (subpatnum != FAIL)
6623 {
6624 if (flags & SP_SUBPAT)
6625 retval = subpatnum;
6626 else
6627 retval = pos.lnum;
6628 if (flags & SP_SETPCMARK)
6629 setpcmark();
6630 curwin->w_cursor = pos;
6631 if (match_pos != NULL)
6632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006633 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 match_pos->lnum = pos.lnum;
6635 match_pos->col = pos.col + 1;
6636 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006637 // "/$" will put the cursor after the end of the line, may need to
6638 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 check_cursor();
6640 }
6641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 if (flags & SP_NOMOVE)
6644 curwin->w_cursor = save_cursor;
6645 else
6646 curwin->w_set_curswant = TRUE;
6647theend:
6648 p_ws = save_p_ws;
6649
6650 return retval;
6651}
6652
6653#ifdef FEAT_FLOAT
6654
6655/*
6656 * round() is not in C90, use ceil() or floor() instead.
6657 */
6658 float_T
6659vim_round(float_T f)
6660{
6661 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6662}
6663
6664/*
6665 * "round({float})" function
6666 */
6667 static void
6668f_round(typval_T *argvars, typval_T *rettv)
6669{
6670 float_T f = 0.0;
6671
6672 rettv->v_type = VAR_FLOAT;
6673 if (get_float_arg(argvars, &f) == OK)
6674 rettv->vval.v_float = vim_round(f);
6675 else
6676 rettv->vval.v_float = 0.0;
6677}
6678#endif
6679
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006680#ifdef FEAT_RUBY
6681/*
6682 * "rubyeval()" function
6683 */
6684 static void
6685f_rubyeval(typval_T *argvars, typval_T *rettv)
6686{
6687 char_u *str;
6688 char_u buf[NUMBUFLEN];
6689
6690 str = tv_get_string_buf(&argvars[0], buf);
6691 do_rubyeval(str, rettv);
6692}
6693#endif
6694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695/*
6696 * "screenattr()" function
6697 */
6698 static void
6699f_screenattr(typval_T *argvars, typval_T *rettv)
6700{
6701 int row;
6702 int col;
6703 int c;
6704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006705 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6706 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707 if (row < 0 || row >= screen_Rows
6708 || col < 0 || col >= screen_Columns)
6709 c = -1;
6710 else
6711 c = ScreenAttrs[LineOffset[row] + col];
6712 rettv->vval.v_number = c;
6713}
6714
6715/*
6716 * "screenchar()" function
6717 */
6718 static void
6719f_screenchar(typval_T *argvars, typval_T *rettv)
6720{
6721 int row;
6722 int col;
6723 int off;
6724 int c;
6725
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006726 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6727 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006728 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 c = -1;
6730 else
6731 {
6732 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006733 if (enc_utf8 && ScreenLinesUC[off] != 0)
6734 c = ScreenLinesUC[off];
6735 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 c = ScreenLines[off];
6737 }
6738 rettv->vval.v_number = c;
6739}
6740
6741/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006742 * "screenchars()" function
6743 */
6744 static void
6745f_screenchars(typval_T *argvars, typval_T *rettv)
6746{
6747 int row;
6748 int col;
6749 int off;
6750 int c;
6751 int i;
6752
6753 if (rettv_list_alloc(rettv) == FAIL)
6754 return;
6755 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6756 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6757 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6758 return;
6759
6760 off = LineOffset[row] + col;
6761 if (enc_utf8 && ScreenLinesUC[off] != 0)
6762 c = ScreenLinesUC[off];
6763 else
6764 c = ScreenLines[off];
6765 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6766
6767 if (enc_utf8)
6768
6769 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6770 list_append_number(rettv->vval.v_list,
6771 (varnumber_T)ScreenLinesC[i][off]);
6772}
6773
6774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775 * "screencol()" function
6776 *
6777 * First column is 1 to be consistent with virtcol().
6778 */
6779 static void
6780f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6781{
6782 rettv->vval.v_number = screen_screencol() + 1;
6783}
6784
6785/*
6786 * "screenrow()" function
6787 */
6788 static void
6789f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6790{
6791 rettv->vval.v_number = screen_screenrow() + 1;
6792}
6793
6794/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006795 * "screenstring()" function
6796 */
6797 static void
6798f_screenstring(typval_T *argvars, typval_T *rettv)
6799{
6800 int row;
6801 int col;
6802 int off;
6803 int c;
6804 int i;
6805 char_u buf[MB_MAXBYTES + 1];
6806 int buflen = 0;
6807
6808 rettv->vval.v_string = NULL;
6809 rettv->v_type = VAR_STRING;
6810
6811 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6812 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6813 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6814 return;
6815
6816 off = LineOffset[row] + col;
6817 if (enc_utf8 && ScreenLinesUC[off] != 0)
6818 c = ScreenLinesUC[off];
6819 else
6820 c = ScreenLines[off];
6821 buflen += mb_char2bytes(c, buf);
6822
6823 if (enc_utf8)
6824 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6825 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6826
6827 buf[buflen] = NUL;
6828 rettv->vval.v_string = vim_strsave(buf);
6829}
6830
6831/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006832 * "search()" function
6833 */
6834 static void
6835f_search(typval_T *argvars, typval_T *rettv)
6836{
6837 int flags = 0;
6838
6839 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6840}
6841
6842/*
6843 * "searchdecl()" function
6844 */
6845 static void
6846f_searchdecl(typval_T *argvars, typval_T *rettv)
6847{
6848 int locally = 1;
6849 int thisblock = 0;
6850 int error = FALSE;
6851 char_u *name;
6852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006853 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006855 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006856 if (argvars[1].v_type != VAR_UNKNOWN)
6857 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006858 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006860 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861 }
6862 if (!error && name != NULL)
6863 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6864 locally, thisblock, SEARCH_KEEP) == FAIL;
6865}
6866
6867/*
6868 * Used by searchpair() and searchpairpos()
6869 */
6870 static int
6871searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6872{
6873 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006874 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 int save_p_ws = p_ws;
6876 int dir;
6877 int flags = 0;
6878 char_u nbuf1[NUMBUFLEN];
6879 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006880 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881 long lnum_stop = 0;
6882 long time_limit = 0;
6883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006884 // Get the three pattern arguments: start, middle, end. Will result in an
6885 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006886 spat = tv_get_string_chk(&argvars[0]);
6887 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6888 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006890 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006892 // Handle the optional fourth argument: flags
6893 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 if (dir == 0)
6895 goto theend;
6896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006897 // Don't accept SP_END or SP_SUBPAT.
6898 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 if ((flags & (SP_END | SP_SUBPAT)) != 0
6900 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006902 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 goto theend;
6904 }
6905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006906 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 if (flags & SP_REPEAT)
6908 p_ws = FALSE;
6909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006910 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 if (argvars[3].v_type == VAR_UNKNOWN
6912 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006913 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 else
6915 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006916 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006917 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006918
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 if (argvars[5].v_type != VAR_UNKNOWN)
6920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006921 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006922 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006924 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006926 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927#ifdef FEAT_RELTIME
6928 if (argvars[6].v_type != VAR_UNKNOWN)
6929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006930 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006933 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 }
6937#endif
6938 }
6939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940
6941 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6942 match_pos, lnum_stop, time_limit);
6943
6944theend:
6945 p_ws = save_p_ws;
6946
6947 return retval;
6948}
6949
6950/*
6951 * "searchpair()" function
6952 */
6953 static void
6954f_searchpair(typval_T *argvars, typval_T *rettv)
6955{
6956 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6957}
6958
6959/*
6960 * "searchpairpos()" function
6961 */
6962 static void
6963f_searchpairpos(typval_T *argvars, typval_T *rettv)
6964{
6965 pos_T match_pos;
6966 int lnum = 0;
6967 int col = 0;
6968
6969 if (rettv_list_alloc(rettv) == FAIL)
6970 return;
6971
6972 if (searchpair_cmn(argvars, &match_pos) > 0)
6973 {
6974 lnum = match_pos.lnum;
6975 col = match_pos.col;
6976 }
6977
6978 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6979 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6980}
6981
6982/*
6983 * Search for a start/middle/end thing.
6984 * Used by searchpair(), see its documentation for the details.
6985 * Returns 0 or -1 for no match,
6986 */
6987 long
6988do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006989 char_u *spat, // start pattern
6990 char_u *mpat, // middle pattern
6991 char_u *epat, // end pattern
6992 int dir, // BACKWARD or FORWARD
6993 typval_T *skip, // skip expression
6994 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006995 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 linenr_T lnum_stop, // stop at this line if not zero
6997 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998{
6999 char_u *save_cpo;
7000 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7001 long retval = 0;
7002 pos_T pos;
7003 pos_T firstpos;
7004 pos_T foundpos;
7005 pos_T save_cursor;
7006 pos_T save_pos;
7007 int n;
7008 int r;
7009 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007010 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011 int err;
7012 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007013#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007015#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007017 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007018 save_cpo = p_cpo;
7019 p_cpo = empty_option;
7020
7021#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 profile_setlimit(time_limit, &tm);
7024#endif
7025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // Make two search patterns: start/end (pat2, for in nested pairs) and
7027 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007028 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7029 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 if (pat2 == NULL || pat3 == NULL)
7031 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007032 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033 if (*mpat == NUL)
7034 STRCPY(pat3, pat2);
7035 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007036 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037 spat, epat, mpat);
7038 if (flags & SP_START)
7039 options |= SEARCH_START;
7040
Bram Moolenaar48570482017-10-30 21:48:41 +01007041 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007042 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007043
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 save_cursor = curwin->w_cursor;
7045 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007046 CLEAR_POS(&firstpos);
7047 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 pat = pat3;
7049 for (;;)
7050 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007051 searchit_arg_T sia;
7052
Bram Moolenaara80faa82020-04-12 19:37:17 +02007053 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007054 sia.sa_stop_lnum = lnum_stop;
7055#ifdef FEAT_RELTIME
7056 sia.sa_tm = &tm;
7057#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007058 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007059 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007060 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007061 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 break;
7063
7064 if (firstpos.lnum == 0)
7065 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007066 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007068 // Found the same position again. Can happen with a pattern that
7069 // has "\zs" at the end and searching backwards. Advance one
7070 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 if (dir == BACKWARD)
7072 decl(&pos);
7073 else
7074 incl(&pos);
7075 }
7076 foundpos = pos;
7077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007078 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 options &= ~SEARCH_START;
7080
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007081 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007082 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 {
7084 save_pos = curwin->w_cursor;
7085 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007086 err = FALSE;
7087 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088 curwin->w_cursor = save_pos;
7089 if (err)
7090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007091 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 curwin->w_cursor = save_cursor;
7093 retval = -1;
7094 break;
7095 }
7096 if (r)
7097 continue;
7098 }
7099
7100 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 // Found end when searching backwards or start when searching
7103 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007105 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 }
7107 else
7108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // Found end when searching forward or start when searching
7110 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007112 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 }
7114
7115 if (nest == 0)
7116 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007117 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 if (flags & SP_RETCOUNT)
7119 ++retval;
7120 else
7121 retval = pos.lnum;
7122 if (flags & SP_SETPCMARK)
7123 setpcmark();
7124 curwin->w_cursor = pos;
7125 if (!(flags & SP_REPEAT))
7126 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007128 }
7129 }
7130
7131 if (match_pos != NULL)
7132 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007133 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134 match_pos->lnum = curwin->w_cursor.lnum;
7135 match_pos->col = curwin->w_cursor.col + 1;
7136 }
7137
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007138 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 if ((flags & SP_NOMOVE) || retval == 0)
7140 curwin->w_cursor = save_cursor;
7141
7142theend:
7143 vim_free(pat2);
7144 vim_free(pat3);
7145 if (p_cpo == empty_option)
7146 p_cpo = save_cpo;
7147 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007148 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 free_string_option(save_cpo);
7150
7151 return retval;
7152}
7153
7154/*
7155 * "searchpos()" function
7156 */
7157 static void
7158f_searchpos(typval_T *argvars, typval_T *rettv)
7159{
7160 pos_T match_pos;
7161 int lnum = 0;
7162 int col = 0;
7163 int n;
7164 int flags = 0;
7165
7166 if (rettv_list_alloc(rettv) == FAIL)
7167 return;
7168
7169 n = search_cmn(argvars, &match_pos, &flags);
7170 if (n > 0)
7171 {
7172 lnum = match_pos.lnum;
7173 col = match_pos.col;
7174 }
7175
7176 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7177 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7178 if (flags & SP_SUBPAT)
7179 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7180}
7181
7182 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007183f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7184{
7185 dict_T *d;
7186 dictitem_T *di;
7187 char_u *csearch;
7188
7189 if (argvars[0].v_type != VAR_DICT)
7190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007191 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 return;
7193 }
7194
7195 if ((d = argvars[0].vval.v_dict) != NULL)
7196 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007197 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 if (csearch != NULL)
7199 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200 if (enc_utf8)
7201 {
7202 int pcc[MAX_MCO];
7203 int c = utfc_ptr2char(csearch, pcc);
7204
7205 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7206 }
7207 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007209 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210 }
7211
7212 di = dict_find(d, (char_u *)"forward", -1);
7213 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007214 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215 ? FORWARD : BACKWARD);
7216
7217 di = dict_find(d, (char_u *)"until", -1);
7218 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007219 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 }
7221}
7222
7223/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007224 * "setenv()" function
7225 */
7226 static void
7227f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7228{
7229 char_u namebuf[NUMBUFLEN];
7230 char_u valbuf[NUMBUFLEN];
7231 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7232
7233 if (argvars[1].v_type == VAR_SPECIAL
7234 && argvars[1].vval.v_number == VVAL_NULL)
7235 vim_unsetenv(name);
7236 else
7237 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7238}
7239
7240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 * "setfperm({fname}, {mode})" function
7242 */
7243 static void
7244f_setfperm(typval_T *argvars, typval_T *rettv)
7245{
7246 char_u *fname;
7247 char_u modebuf[NUMBUFLEN];
7248 char_u *mode_str;
7249 int i;
7250 int mask;
7251 int mode = 0;
7252
7253 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007254 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255 if (fname == NULL)
7256 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007257 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258 if (mode_str == NULL)
7259 return;
7260 if (STRLEN(mode_str) != 9)
7261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007262 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263 return;
7264 }
7265
7266 mask = 1;
7267 for (i = 8; i >= 0; --i)
7268 {
7269 if (mode_str[i] != '-')
7270 mode |= mask;
7271 mask = mask << 1;
7272 }
7273 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7274}
7275
7276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 * "setpos()" function
7278 */
7279 static void
7280f_setpos(typval_T *argvars, typval_T *rettv)
7281{
7282 pos_T pos;
7283 int fnum;
7284 char_u *name;
7285 colnr_T curswant = -1;
7286
7287 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007288 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 if (name != NULL)
7290 {
7291 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7292 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007293 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 pos.col = 0;
7295 if (name[0] == '.' && name[1] == NUL)
7296 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007297 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007298 curwin->w_cursor = pos;
7299 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007301 curwin->w_curswant = curswant - 1;
7302 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007304 check_cursor();
7305 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 }
7307 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007309 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 if (setmark_pos(name[1], &pos, fnum) == OK)
7311 rettv->vval.v_number = 0;
7312 }
7313 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 }
7316 }
7317}
7318
7319/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007320 * Translate a register type string to the yank type and block length
7321 */
7322 static int
7323get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7324{
7325 char_u *stropt = *pp;
7326 switch (*stropt)
7327 {
7328 case 'v': case 'c': // character-wise selection
7329 *yank_type = MCHAR;
7330 break;
7331 case 'V': case 'l': // line-wise selection
7332 *yank_type = MLINE;
7333 break;
7334 case 'b': case Ctrl_V: // block-wise selection
7335 *yank_type = MBLOCK;
7336 if (VIM_ISDIGIT(stropt[1]))
7337 {
7338 ++stropt;
7339 *block_len = getdigits(&stropt) - 1;
7340 --stropt;
7341 }
7342 break;
7343 default:
7344 return FAIL;
7345 }
7346 *pp = stropt;
7347 return OK;
7348}
7349
7350/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 * "setreg()" function
7352 */
7353 static void
7354f_setreg(typval_T *argvars, typval_T *rettv)
7355{
7356 int regname;
7357 char_u *strregname;
7358 char_u *stropt;
7359 char_u *strval;
7360 int append;
7361 char_u yank_type;
7362 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007363 typval_T *regcontents;
7364 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007366 pointreg = 0;
7367 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368 block_len = -1;
7369 yank_type = MAUTO;
7370 append = FALSE;
7371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007372 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007373 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007374
7375 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007376 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 regname = *strregname;
7378 if (regname == 0 || regname == '@')
7379 regname = '"';
7380
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007381 if (argvars[1].v_type == VAR_DICT)
7382 {
7383 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007384 dictitem_T *di;
7385
7386 if (d == NULL || d->dv_hashtab.ht_used == 0)
7387 {
7388 // Empty dict, clear the register (like setreg(0, []))
7389 char_u *lstval[2] = {NULL, NULL};
7390 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7391 return;
7392 }
7393
7394 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007395 if (di != NULL)
7396 regcontents = &di->di_tv;
7397
7398 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7399 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007400 {
7401 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7402
7403 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007404 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007405 semsg(_(e_invargval), "value");
7406 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007407 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007408 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007409
7410 if (regname == '"')
7411 {
7412 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7413 if (stropt != NULL)
7414 {
7415 pointreg = *stropt;
7416 regname = pointreg;
7417 }
7418 }
7419 else if (dict_get_number(d, (char_u *)"isunnamed"))
7420 pointreg = regname;
7421 }
7422 else
7423 regcontents = &argvars[1];
7424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007425 if (argvars[2].v_type != VAR_UNKNOWN)
7426 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007427 if (yank_type != MAUTO)
7428 {
7429 semsg(_(e_toomanyarg), "setreg");
7430 return;
7431 }
7432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007433 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007435 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 for (; *stropt != NUL; ++stropt)
7437 switch (*stropt)
7438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007439 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440 append = TRUE;
7441 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007442 default:
7443 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 }
7445 }
7446
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007447 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448 {
7449 char_u **lstval;
7450 char_u **allocval;
7451 char_u buf[NUMBUFLEN];
7452 char_u **curval;
7453 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007454 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 listitem_T *li;
7456 int len;
7457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007458 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007459 len = ll == NULL ? 0 : ll->lv_len;
7460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007461 // First half: use for pointers to result lines; second half: use for
7462 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007463 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 if (lstval == NULL)
7465 return;
7466 curval = lstval;
7467 allocval = lstval + len + 2;
7468 curallocval = allocval;
7469
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007470 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007472 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007473 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007475 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 if (strval == NULL)
7477 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007478 if (strval == buf)
7479 {
7480 // Need to make a copy, next tv_get_string_buf_chk() will
7481 // overwrite the string.
7482 strval = vim_strsave(buf);
7483 if (strval == NULL)
7484 goto free_lstval;
7485 *curallocval++ = strval;
7486 }
7487 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 }
7490 *curval++ = NULL;
7491
7492 write_reg_contents_lst(regname, lstval, -1,
7493 append, yank_type, block_len);
7494free_lstval:
7495 while (curallocval > allocval)
7496 vim_free(*--curallocval);
7497 vim_free(lstval);
7498 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007499 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007501 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 if (strval == NULL)
7503 return;
7504 write_reg_contents_ex(regname, strval, -1,
7505 append, yank_type, block_len);
7506 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007507 if (pointreg != 0)
7508 get_yank_register(pointreg, TRUE);
7509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 rettv->vval.v_number = 0;
7511}
7512
7513/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007514 * "settagstack()" function
7515 */
7516 static void
7517f_settagstack(typval_T *argvars, typval_T *rettv)
7518{
7519 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7520 win_T *wp;
7521 dict_T *d;
7522 int action = 'r';
7523
7524 rettv->vval.v_number = -1;
7525
7526 // first argument: window number or id
7527 wp = find_win_by_nr_or_id(&argvars[0]);
7528 if (wp == NULL)
7529 return;
7530
7531 // second argument: dict with items to set in the tag stack
7532 if (argvars[1].v_type != VAR_DICT)
7533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007535 return;
7536 }
7537 d = argvars[1].vval.v_dict;
7538 if (d == NULL)
7539 return;
7540
7541 // third argument: action - 'a' for append and 'r' for replace.
7542 // default is to replace the stack.
7543 if (argvars[2].v_type == VAR_UNKNOWN)
7544 action = 'r';
7545 else if (argvars[2].v_type == VAR_STRING)
7546 {
7547 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007548 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007549 if (actstr == NULL)
7550 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007551 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7552 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007553 action = *actstr;
7554 else
7555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007556 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007557 return;
7558 }
7559 }
7560 else
7561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007562 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007563 return;
7564 }
7565
7566 if (set_tagstack(wp, d, action) == OK)
7567 rettv->vval.v_number = 0;
7568}
7569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570#ifdef FEAT_CRYPT
7571/*
7572 * "sha256({string})" function
7573 */
7574 static void
7575f_sha256(typval_T *argvars, typval_T *rettv)
7576{
7577 char_u *p;
7578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007579 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580 rettv->vval.v_string = vim_strsave(
7581 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7582 rettv->v_type = VAR_STRING;
7583}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007584#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007585
7586/*
7587 * "shellescape({string})" function
7588 */
7589 static void
7590f_shellescape(typval_T *argvars, typval_T *rettv)
7591{
Bram Moolenaar20615522017-06-05 18:46:26 +02007592 int do_special = non_zero_arg(&argvars[1]);
7593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007595 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 rettv->v_type = VAR_STRING;
7597}
7598
7599/*
7600 * shiftwidth() function
7601 */
7602 static void
7603f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7604{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007605 rettv->vval.v_number = 0;
7606
7607 if (argvars[0].v_type != VAR_UNKNOWN)
7608 {
7609 long col;
7610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007612 if (col < 0)
7613 return; // type error; errmsg already given
7614#ifdef FEAT_VARTABS
7615 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7616 return;
7617#endif
7618 }
7619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620 rettv->vval.v_number = get_sw_value(curbuf);
7621}
7622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623#ifdef FEAT_FLOAT
7624/*
7625 * "sin()" function
7626 */
7627 static void
7628f_sin(typval_T *argvars, typval_T *rettv)
7629{
7630 float_T f = 0.0;
7631
7632 rettv->v_type = VAR_FLOAT;
7633 if (get_float_arg(argvars, &f) == OK)
7634 rettv->vval.v_float = sin(f);
7635 else
7636 rettv->vval.v_float = 0.0;
7637}
7638
7639/*
7640 * "sinh()" function
7641 */
7642 static void
7643f_sinh(typval_T *argvars, typval_T *rettv)
7644{
7645 float_T f = 0.0;
7646
7647 rettv->v_type = VAR_FLOAT;
7648 if (get_float_arg(argvars, &f) == OK)
7649 rettv->vval.v_float = sinh(f);
7650 else
7651 rettv->vval.v_float = 0.0;
7652}
7653#endif
7654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655/*
7656 * "soundfold({word})" function
7657 */
7658 static void
7659f_soundfold(typval_T *argvars, typval_T *rettv)
7660{
7661 char_u *s;
7662
7663 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665#ifdef FEAT_SPELL
7666 rettv->vval.v_string = eval_soundfold(s);
7667#else
7668 rettv->vval.v_string = vim_strsave(s);
7669#endif
7670}
7671
7672/*
7673 * "spellbadword()" function
7674 */
7675 static void
7676f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7677{
7678 char_u *word = (char_u *)"";
7679 hlf_T attr = HLF_COUNT;
7680 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007681#ifdef FEAT_SPELL
7682 int wo_spell_save = curwin->w_p_spell;
7683
7684 if (!curwin->w_p_spell)
7685 {
7686 did_set_spelllang(curwin);
7687 curwin->w_p_spell = TRUE;
7688 }
7689
7690 if (*curwin->w_s->b_p_spl == NUL)
7691 {
7692 emsg(_(e_no_spell));
7693 curwin->w_p_spell = wo_spell_save;
7694 return;
7695 }
7696#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697
7698 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007699 {
7700#ifdef FEAT_SPELL
7701 curwin->w_p_spell = wo_spell_save;
7702#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007704 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705
7706#ifdef FEAT_SPELL
7707 if (argvars[0].v_type == VAR_UNKNOWN)
7708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007709 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7711 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007712 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007714 curwin->w_set_curswant = TRUE;
7715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007717 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007719 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720 int capcol = -1;
7721
7722 if (str != NULL)
7723 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007724 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 while (*str != NUL)
7726 {
7727 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7728 if (attr != HLF_COUNT)
7729 {
7730 word = str;
7731 break;
7732 }
7733 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007734 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007735 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 }
7737 }
7738 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007739 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007740#endif
7741
7742 list_append_string(rettv->vval.v_list, word, len);
7743 list_append_string(rettv->vval.v_list, (char_u *)(
7744 attr == HLF_SPB ? "bad" :
7745 attr == HLF_SPR ? "rare" :
7746 attr == HLF_SPL ? "local" :
7747 attr == HLF_SPC ? "caps" :
7748 ""), -1);
7749}
7750
7751/*
7752 * "spellsuggest()" function
7753 */
7754 static void
7755f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7756{
7757#ifdef FEAT_SPELL
7758 char_u *str;
7759 int typeerr = FALSE;
7760 int maxcount;
7761 garray_T ga;
7762 int i;
7763 listitem_T *li;
7764 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007765 int wo_spell_save = curwin->w_p_spell;
7766
7767 if (!curwin->w_p_spell)
7768 {
7769 did_set_spelllang(curwin);
7770 curwin->w_p_spell = TRUE;
7771 }
7772
7773 if (*curwin->w_s->b_p_spl == NUL)
7774 {
7775 emsg(_(e_no_spell));
7776 curwin->w_p_spell = wo_spell_save;
7777 return;
7778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779#endif
7780
7781 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007782 {
7783#ifdef FEAT_SPELL
7784 curwin->w_p_spell = wo_spell_save;
7785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007787 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007788
7789#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007790 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007792 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793 if (argvars[1].v_type != VAR_UNKNOWN)
7794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007795 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796 if (maxcount <= 0)
7797 return;
7798 if (argvars[2].v_type != VAR_UNKNOWN)
7799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007800 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801 if (typeerr)
7802 return;
7803 }
7804 }
7805 else
7806 maxcount = 25;
7807
7808 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7809
7810 for (i = 0; i < ga.ga_len; ++i)
7811 {
7812 str = ((char_u **)ga.ga_data)[i];
7813
7814 li = listitem_alloc();
7815 if (li == NULL)
7816 vim_free(str);
7817 else
7818 {
7819 li->li_tv.v_type = VAR_STRING;
7820 li->li_tv.v_lock = 0;
7821 li->li_tv.vval.v_string = str;
7822 list_append(rettv->vval.v_list, li);
7823 }
7824 }
7825 ga_clear(&ga);
7826 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007827 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828#endif
7829}
7830
7831 static void
7832f_split(typval_T *argvars, typval_T *rettv)
7833{
7834 char_u *str;
7835 char_u *end;
7836 char_u *pat = NULL;
7837 regmatch_T regmatch;
7838 char_u patbuf[NUMBUFLEN];
7839 char_u *save_cpo;
7840 int match;
7841 colnr_T col = 0;
7842 int keepempty = FALSE;
7843 int typeerr = FALSE;
7844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007845 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 save_cpo = p_cpo;
7847 p_cpo = (char_u *)"";
7848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007849 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 if (argvars[1].v_type != VAR_UNKNOWN)
7851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007852 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 if (pat == NULL)
7854 typeerr = TRUE;
7855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007856 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 }
7858 if (pat == NULL || *pat == NUL)
7859 pat = (char_u *)"[\\x01- ]\\+";
7860
7861 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007862 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007864 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865
7866 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7867 if (regmatch.regprog != NULL)
7868 {
7869 regmatch.rm_ic = FALSE;
7870 while (*str != NUL || keepempty)
7871 {
7872 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007873 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 else
7875 match = vim_regexec_nl(&regmatch, str, col);
7876 if (match)
7877 end = regmatch.startp[0];
7878 else
7879 end = str + STRLEN(str);
7880 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7881 && *str != NUL && match && end < regmatch.endp[0]))
7882 {
7883 if (list_append_string(rettv->vval.v_list, str,
7884 (int)(end - str)) == FAIL)
7885 break;
7886 }
7887 if (!match)
7888 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007889 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 if (regmatch.endp[0] > str)
7891 col = 0;
7892 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007893 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 str = regmatch.endp[0];
7896 }
7897
7898 vim_regfree(regmatch.regprog);
7899 }
7900
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007901theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 p_cpo = save_cpo;
7903}
7904
7905#ifdef FEAT_FLOAT
7906/*
7907 * "sqrt()" function
7908 */
7909 static void
7910f_sqrt(typval_T *argvars, typval_T *rettv)
7911{
7912 float_T f = 0.0;
7913
7914 rettv->v_type = VAR_FLOAT;
7915 if (get_float_arg(argvars, &f) == OK)
7916 rettv->vval.v_float = sqrt(f);
7917 else
7918 rettv->vval.v_float = 0.0;
7919}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007920#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007922#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007923/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007924 * "str2float()" function
7925 */
7926 static void
7927f_str2float(typval_T *argvars, typval_T *rettv)
7928{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007930 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931
Bram Moolenaar08243d22017-01-10 16:12:29 +01007932 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 p = skipwhite(p + 1);
7934 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007935 if (isneg)
7936 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937 rettv->v_type = VAR_FLOAT;
7938}
7939#endif
7940
7941/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007942 * "str2list()" function
7943 */
7944 static void
7945f_str2list(typval_T *argvars, typval_T *rettv)
7946{
7947 char_u *p;
7948 int utf8 = FALSE;
7949
7950 if (rettv_list_alloc(rettv) == FAIL)
7951 return;
7952
7953 if (argvars[1].v_type != VAR_UNKNOWN)
7954 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7955
7956 p = tv_get_string(&argvars[0]);
7957
7958 if (has_mbyte || utf8)
7959 {
7960 int (*ptr2len)(char_u *);
7961 int (*ptr2char)(char_u *);
7962
7963 if (utf8 || enc_utf8)
7964 {
7965 ptr2len = utf_ptr2len;
7966 ptr2char = utf_ptr2char;
7967 }
7968 else
7969 {
7970 ptr2len = mb_ptr2len;
7971 ptr2char = mb_ptr2char;
7972 }
7973
7974 for ( ; *p != NUL; p += (*ptr2len)(p))
7975 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7976 }
7977 else
7978 for ( ; *p != NUL; ++p)
7979 list_append_number(rettv->vval.v_list, *p);
7980}
7981
7982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 * "str2nr()" function
7984 */
7985 static void
7986f_str2nr(typval_T *argvars, typval_T *rettv)
7987{
7988 int base = 10;
7989 char_u *p;
7990 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007991 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01007992 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993
7994 if (argvars[1].v_type != VAR_UNKNOWN)
7995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007996 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 if (base != 2 && base != 8 && base != 10 && base != 16)
7998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007999 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 return;
8001 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008002 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8003 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 }
8005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008006 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008007 isneg = (*p == '-');
8008 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 p = skipwhite(p + 1);
8010 switch (base)
8011 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008012 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008013 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008014 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008016 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8017 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008018 if (isneg)
8019 rettv->vval.v_number = -n;
8020 else
8021 rettv->vval.v_number = n;
8022
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023}
8024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008025/*
8026 * "strgetchar()" function
8027 */
8028 static void
8029f_strgetchar(typval_T *argvars, typval_T *rettv)
8030{
8031 char_u *str;
8032 int len;
8033 int error = FALSE;
8034 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008035 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036
8037 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008038 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 if (str == NULL)
8040 return;
8041 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008042 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 if (error)
8044 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045
Bram Moolenaar13505972019-01-24 15:04:48 +01008046 while (charidx >= 0 && byteidx < len)
8047 {
8048 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008050 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8051 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008053 --charidx;
8054 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056}
8057
8058/*
8059 * "stridx()" function
8060 */
8061 static void
8062f_stridx(typval_T *argvars, typval_T *rettv)
8063{
8064 char_u buf[NUMBUFLEN];
8065 char_u *needle;
8066 char_u *haystack;
8067 char_u *save_haystack;
8068 char_u *pos;
8069 int start_idx;
8070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008071 needle = tv_get_string_chk(&argvars[1]);
8072 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 rettv->vval.v_number = -1;
8074 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008075 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076
8077 if (argvars[2].v_type != VAR_UNKNOWN)
8078 {
8079 int error = FALSE;
8080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008081 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 if (error || start_idx >= (int)STRLEN(haystack))
8083 return;
8084 if (start_idx >= 0)
8085 haystack += start_idx;
8086 }
8087
8088 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8089 if (pos != NULL)
8090 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8091}
8092
8093/*
8094 * "string()" function
8095 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008096 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097f_string(typval_T *argvars, typval_T *rettv)
8098{
8099 char_u *tofree;
8100 char_u numbuf[NUMBUFLEN];
8101
8102 rettv->v_type = VAR_STRING;
8103 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8104 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008105 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008106 if (rettv->vval.v_string != NULL && tofree == NULL)
8107 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8108}
8109
8110/*
8111 * "strlen()" function
8112 */
8113 static void
8114f_strlen(typval_T *argvars, typval_T *rettv)
8115{
8116 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008117 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118}
8119
8120/*
8121 * "strchars()" function
8122 */
8123 static void
8124f_strchars(typval_T *argvars, typval_T *rettv)
8125{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008128 varnumber_T len = 0;
8129 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130
8131 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008132 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008134 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 else
8136 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8138 while (*s != NUL)
8139 {
8140 func_mb_ptr2char_adv(&s);
8141 ++len;
8142 }
8143 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 }
8145}
8146
8147/*
8148 * "strdisplaywidth()" function
8149 */
8150 static void
8151f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8152{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008153 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 int col = 0;
8155
8156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008157 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158
8159 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8160}
8161
8162/*
8163 * "strwidth()" function
8164 */
8165 static void
8166f_strwidth(typval_T *argvars, typval_T *rettv)
8167{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008168 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169
Bram Moolenaar13505972019-01-24 15:04:48 +01008170 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171}
8172
8173/*
8174 * "strcharpart()" function
8175 */
8176 static void
8177f_strcharpart(typval_T *argvars, typval_T *rettv)
8178{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 char_u *p;
8180 int nchar;
8181 int nbyte = 0;
8182 int charlen;
8183 int len = 0;
8184 int slen;
8185 int error = FALSE;
8186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008187 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 slen = (int)STRLEN(p);
8189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008190 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 if (!error)
8192 {
8193 if (nchar > 0)
8194 while (nchar > 0 && nbyte < slen)
8195 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008196 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 --nchar;
8198 }
8199 else
8200 nbyte = nchar;
8201 if (argvars[2].v_type != VAR_UNKNOWN)
8202 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008203 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 while (charlen > 0 && nbyte + len < slen)
8205 {
8206 int off = nbyte + len;
8207
8208 if (off < 0)
8209 len += 1;
8210 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008211 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 --charlen;
8213 }
8214 }
8215 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008216 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 }
8218
8219 /*
8220 * Only return the overlap between the specified part and the actual
8221 * string.
8222 */
8223 if (nbyte < 0)
8224 {
8225 len += nbyte;
8226 nbyte = 0;
8227 }
8228 else if (nbyte > slen)
8229 nbyte = slen;
8230 if (len < 0)
8231 len = 0;
8232 else if (nbyte + len > slen)
8233 len = slen - nbyte;
8234
8235 rettv->v_type = VAR_STRING;
8236 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237}
8238
8239/*
8240 * "strpart()" function
8241 */
8242 static void
8243f_strpart(typval_T *argvars, typval_T *rettv)
8244{
8245 char_u *p;
8246 int n;
8247 int len;
8248 int slen;
8249 int error = FALSE;
8250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008251 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 slen = (int)STRLEN(p);
8253
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008254 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255 if (error)
8256 len = 0;
8257 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008258 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008260 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261
8262 /*
8263 * Only return the overlap between the specified part and the actual
8264 * string.
8265 */
8266 if (n < 0)
8267 {
8268 len += n;
8269 n = 0;
8270 }
8271 else if (n > slen)
8272 n = slen;
8273 if (len < 0)
8274 len = 0;
8275 else if (n + len > slen)
8276 len = slen - n;
8277
8278 rettv->v_type = VAR_STRING;
8279 rettv->vval.v_string = vim_strnsave(p + n, len);
8280}
8281
8282/*
8283 * "strridx()" function
8284 */
8285 static void
8286f_strridx(typval_T *argvars, typval_T *rettv)
8287{
8288 char_u buf[NUMBUFLEN];
8289 char_u *needle;
8290 char_u *haystack;
8291 char_u *rest;
8292 char_u *lastmatch = NULL;
8293 int haystack_len, end_idx;
8294
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008295 needle = tv_get_string_chk(&argvars[1]);
8296 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297
8298 rettv->vval.v_number = -1;
8299 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008300 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301
8302 haystack_len = (int)STRLEN(haystack);
8303 if (argvars[2].v_type != VAR_UNKNOWN)
8304 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008305 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008306 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008308 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 }
8310 else
8311 end_idx = haystack_len;
8312
8313 if (*needle == NUL)
8314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008315 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 lastmatch = haystack + end_idx;
8317 }
8318 else
8319 {
8320 for (rest = haystack; *rest != '\0'; ++rest)
8321 {
8322 rest = (char_u *)strstr((char *)rest, (char *)needle);
8323 if (rest == NULL || rest > haystack + end_idx)
8324 break;
8325 lastmatch = rest;
8326 }
8327 }
8328
8329 if (lastmatch == NULL)
8330 rettv->vval.v_number = -1;
8331 else
8332 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8333}
8334
8335/*
8336 * "strtrans()" function
8337 */
8338 static void
8339f_strtrans(typval_T *argvars, typval_T *rettv)
8340{
8341 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008342 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343}
8344
8345/*
8346 * "submatch()" function
8347 */
8348 static void
8349f_submatch(typval_T *argvars, typval_T *rettv)
8350{
8351 int error = FALSE;
8352 int no;
8353 int retList = 0;
8354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008355 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 if (error)
8357 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008358 if (no < 0 || no >= NSUBEXP)
8359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008360 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008361 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008362 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 if (error)
8366 return;
8367
8368 if (retList == 0)
8369 {
8370 rettv->v_type = VAR_STRING;
8371 rettv->vval.v_string = reg_submatch(no);
8372 }
8373 else
8374 {
8375 rettv->v_type = VAR_LIST;
8376 rettv->vval.v_list = reg_submatch_list(no);
8377 }
8378}
8379
8380/*
8381 * "substitute()" function
8382 */
8383 static void
8384f_substitute(typval_T *argvars, typval_T *rettv)
8385{
8386 char_u patbuf[NUMBUFLEN];
8387 char_u subbuf[NUMBUFLEN];
8388 char_u flagsbuf[NUMBUFLEN];
8389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008390 char_u *str = tv_get_string_chk(&argvars[0]);
8391 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008392 char_u *sub = NULL;
8393 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008394 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008396 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8397 expr = &argvars[2];
8398 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008400
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008402 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8403 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 rettv->vval.v_string = NULL;
8405 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008406 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407}
8408
8409/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008410 * "swapinfo(swap_filename)" function
8411 */
8412 static void
8413f_swapinfo(typval_T *argvars, typval_T *rettv)
8414{
8415 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008417}
8418
8419/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008420 * "swapname(expr)" function
8421 */
8422 static void
8423f_swapname(typval_T *argvars, typval_T *rettv)
8424{
8425 buf_T *buf;
8426
8427 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008428 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008429 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8430 || buf->b_ml.ml_mfp->mf_fname == NULL)
8431 rettv->vval.v_string = NULL;
8432 else
8433 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8434}
8435
8436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 * "synID(lnum, col, trans)" function
8438 */
8439 static void
8440f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8441{
8442 int id = 0;
8443#ifdef FEAT_SYN_HL
8444 linenr_T lnum;
8445 colnr_T col;
8446 int trans;
8447 int transerr = FALSE;
8448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008449 lnum = tv_get_lnum(argvars); // -1 on type error
8450 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008451 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452
8453 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8454 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8455 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8456#endif
8457
8458 rettv->vval.v_number = id;
8459}
8460
8461/*
8462 * "synIDattr(id, what [, mode])" function
8463 */
8464 static void
8465f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8466{
8467 char_u *p = NULL;
8468#ifdef FEAT_SYN_HL
8469 int id;
8470 char_u *what;
8471 char_u *mode;
8472 char_u modebuf[NUMBUFLEN];
8473 int modec;
8474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008475 id = (int)tv_get_number(&argvars[0]);
8476 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 if (argvars[2].v_type != VAR_UNKNOWN)
8478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008479 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 modec = TOLOWER_ASC(mode[0]);
8481 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008482 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008483 }
8484 else
8485 {
8486#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8487 if (USE_24BIT)
8488 modec = 'g';
8489 else
8490#endif
8491 if (t_colors > 1)
8492 modec = 'c';
8493 else
8494 modec = 't';
8495 }
8496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 switch (TOLOWER_ASC(what[0]))
8498 {
8499 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008500 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008502 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503 p = highlight_has_attr(id, HL_BOLD, modec);
8504 break;
8505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008506 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 p = highlight_color(id, what, modec);
8508 break;
8509
8510 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008511 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008513 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 p = highlight_has_attr(id, HL_ITALIC, modec);
8515 break;
8516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008517 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008518 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 break;
8520
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 p = highlight_has_attr(id, HL_INVERSE, modec);
8523 break;
8524
8525 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008526 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008527 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008528 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008529 else if (TOLOWER_ASC(what[1]) == 't' &&
8530 TOLOWER_ASC(what[2]) == 'r')
8531 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008532 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 p = highlight_has_attr(id, HL_STANDOUT, modec);
8534 break;
8535
8536 case 'u':
8537 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008538 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8540 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008541 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8543 break;
8544 }
8545
8546 if (p != NULL)
8547 p = vim_strsave(p);
8548#endif
8549 rettv->v_type = VAR_STRING;
8550 rettv->vval.v_string = p;
8551}
8552
8553/*
8554 * "synIDtrans(id)" function
8555 */
8556 static void
8557f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8558{
8559 int id;
8560
8561#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008562 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563
8564 if (id > 0)
8565 id = syn_get_final_id(id);
8566 else
8567#endif
8568 id = 0;
8569
8570 rettv->vval.v_number = id;
8571}
8572
8573/*
8574 * "synconcealed(lnum, col)" function
8575 */
8576 static void
8577f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8578{
8579#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8580 linenr_T lnum;
8581 colnr_T col;
8582 int syntax_flags = 0;
8583 int cchar;
8584 int matchid = 0;
8585 char_u str[NUMBUFLEN];
8586#endif
8587
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008588 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589
8590#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008591 lnum = tv_get_lnum(argvars); // -1 on type error
8592 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593
Bram Moolenaara80faa82020-04-12 19:37:17 +02008594 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595
8596 if (rettv_list_alloc(rettv) != FAIL)
8597 {
8598 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8599 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8600 && curwin->w_p_cole > 0)
8601 {
8602 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8603 syntax_flags = get_syntax_info(&matchid);
8604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008605 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8607 {
8608 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008609 if (cchar == NUL && curwin->w_p_cole == 1)
8610 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 if (cchar != NUL)
8612 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 if (has_mbyte)
8614 (*mb_char2bytes)(cchar, str);
8615 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 str[0] = cchar;
8617 }
8618 }
8619 }
8620
8621 list_append_number(rettv->vval.v_list,
8622 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008623 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 list_append_string(rettv->vval.v_list, str, -1);
8625 list_append_number(rettv->vval.v_list, matchid);
8626 }
8627#endif
8628}
8629
8630/*
8631 * "synstack(lnum, col)" function
8632 */
8633 static void
8634f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8635{
8636#ifdef FEAT_SYN_HL
8637 linenr_T lnum;
8638 colnr_T col;
8639 int i;
8640 int id;
8641#endif
8642
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008643 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644
8645#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008646 lnum = tv_get_lnum(argvars); // -1 on type error
8647 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648
8649 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8650 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8651 && rettv_list_alloc(rettv) != FAIL)
8652 {
8653 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8654 for (i = 0; ; ++i)
8655 {
8656 id = syn_get_stack_item(i);
8657 if (id < 0)
8658 break;
8659 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8660 break;
8661 }
8662 }
8663#endif
8664}
8665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666/*
8667 * "tabpagebuflist()" function
8668 */
8669 static void
8670f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8671{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 tabpage_T *tp;
8673 win_T *wp = NULL;
8674
8675 if (argvars[0].v_type == VAR_UNKNOWN)
8676 wp = firstwin;
8677 else
8678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008679 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 if (tp != NULL)
8681 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8682 }
8683 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8684 {
8685 for (; wp != NULL; wp = wp->w_next)
8686 if (list_append_number(rettv->vval.v_list,
8687 wp->w_buffer->b_fnum) == FAIL)
8688 break;
8689 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690}
8691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 * "tagfiles()" function
8694 */
8695 static void
8696f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8697{
8698 char_u *fname;
8699 tagname_T tn;
8700 int first;
8701
8702 if (rettv_list_alloc(rettv) == FAIL)
8703 return;
8704 fname = alloc(MAXPATHL);
8705 if (fname == NULL)
8706 return;
8707
8708 for (first = TRUE; ; first = FALSE)
8709 if (get_tagfname(&tn, first, fname) == FAIL
8710 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8711 break;
8712 tagname_free(&tn);
8713 vim_free(fname);
8714}
8715
8716/*
8717 * "taglist()" function
8718 */
8719 static void
8720f_taglist(typval_T *argvars, typval_T *rettv)
8721{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008722 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 char_u *tag_pattern;
8724
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726
8727 rettv->vval.v_number = FALSE;
8728 if (*tag_pattern == NUL)
8729 return;
8730
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008731 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008732 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008734 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735}
8736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737#ifdef FEAT_FLOAT
8738/*
8739 * "tan()" function
8740 */
8741 static void
8742f_tan(typval_T *argvars, typval_T *rettv)
8743{
8744 float_T f = 0.0;
8745
8746 rettv->v_type = VAR_FLOAT;
8747 if (get_float_arg(argvars, &f) == OK)
8748 rettv->vval.v_float = tan(f);
8749 else
8750 rettv->vval.v_float = 0.0;
8751}
8752
8753/*
8754 * "tanh()" function
8755 */
8756 static void
8757f_tanh(typval_T *argvars, typval_T *rettv)
8758{
8759 float_T f = 0.0;
8760
8761 rettv->v_type = VAR_FLOAT;
8762 if (get_float_arg(argvars, &f) == OK)
8763 rettv->vval.v_float = tanh(f);
8764 else
8765 rettv->vval.v_float = 0.0;
8766}
8767#endif
8768
8769/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 * "tolower(string)" function
8771 */
8772 static void
8773f_tolower(typval_T *argvars, typval_T *rettv)
8774{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008776 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777}
8778
8779/*
8780 * "toupper(string)" function
8781 */
8782 static void
8783f_toupper(typval_T *argvars, typval_T *rettv)
8784{
8785 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008786 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787}
8788
8789/*
8790 * "tr(string, fromstr, tostr)" function
8791 */
8792 static void
8793f_tr(typval_T *argvars, typval_T *rettv)
8794{
8795 char_u *in_str;
8796 char_u *fromstr;
8797 char_u *tostr;
8798 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 int inlen;
8800 int fromlen;
8801 int tolen;
8802 int idx;
8803 char_u *cpstr;
8804 int cplen;
8805 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 char_u buf[NUMBUFLEN];
8807 char_u buf2[NUMBUFLEN];
8808 garray_T ga;
8809
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008810 in_str = tv_get_string(&argvars[0]);
8811 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8812 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008814 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 rettv->v_type = VAR_STRING;
8816 rettv->vval.v_string = NULL;
8817 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008818 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 ga_init2(&ga, (int)sizeof(char), 80);
8820
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008822 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 if (STRLEN(fromstr) != STRLEN(tostr))
8824 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008826 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 ga_clear(&ga);
8828 return;
8829 }
8830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008831 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 while (*in_str != NUL)
8833 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 if (has_mbyte)
8835 {
8836 inlen = (*mb_ptr2len)(in_str);
8837 cpstr = in_str;
8838 cplen = inlen;
8839 idx = 0;
8840 for (p = fromstr; *p != NUL; p += fromlen)
8841 {
8842 fromlen = (*mb_ptr2len)(p);
8843 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8844 {
8845 for (p = tostr; *p != NUL; p += tolen)
8846 {
8847 tolen = (*mb_ptr2len)(p);
8848 if (idx-- == 0)
8849 {
8850 cplen = tolen;
8851 cpstr = p;
8852 break;
8853 }
8854 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008855 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 goto error;
8857 break;
8858 }
8859 ++idx;
8860 }
8861
8862 if (first && cpstr == in_str)
8863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008864 // Check that fromstr and tostr have the same number of
8865 // (multi-byte) characters. Done only once when a character
8866 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 first = FALSE;
8868 for (p = tostr; *p != NUL; p += tolen)
8869 {
8870 tolen = (*mb_ptr2len)(p);
8871 --idx;
8872 }
8873 if (idx != 0)
8874 goto error;
8875 }
8876
8877 (void)ga_grow(&ga, cplen);
8878 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8879 ga.ga_len += cplen;
8880
8881 in_str += inlen;
8882 }
8883 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008885 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 p = vim_strchr(fromstr, *in_str);
8887 if (p != NULL)
8888 ga_append(&ga, tostr[p - fromstr]);
8889 else
8890 ga_append(&ga, *in_str);
8891 ++in_str;
8892 }
8893 }
8894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008895 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 (void)ga_grow(&ga, 1);
8897 ga_append(&ga, NUL);
8898
8899 rettv->vval.v_string = ga.ga_data;
8900}
8901
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008902/*
8903 * "trim({expr})" function
8904 */
8905 static void
8906f_trim(typval_T *argvars, typval_T *rettv)
8907{
8908 char_u buf1[NUMBUFLEN];
8909 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008910 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008911 char_u *mask = NULL;
8912 char_u *tail;
8913 char_u *prev;
8914 char_u *p;
8915 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008916 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008917
8918 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008919 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008920 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008921 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008922
8923 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008925 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008926
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008927 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008928 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008929 int error = 0;
8930
8931 // leading or trailing characters to trim
8932 dir = (int)tv_get_number_chk(&argvars[2], &error);
8933 if (error)
8934 return;
8935 if (dir < 0 || dir > 2)
8936 {
8937 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8938 return;
8939 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008940 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008941 }
8942
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008943 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008944 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008945 // Trim leading characters
8946 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008947 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008948 c1 = PTR2CHAR(head);
8949 if (mask == NULL)
8950 {
8951 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008952 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008953 }
8954 else
8955 {
8956 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8957 if (c1 == PTR2CHAR(p))
8958 break;
8959 if (*p == NUL)
8960 break;
8961 }
8962 MB_PTR_ADV(head);
8963 }
8964 }
8965
8966 tail = head + STRLEN(head);
8967 if (dir == 0 || dir == 2)
8968 {
8969 // Trim trailing characters
8970 for (; tail > head; tail = prev)
8971 {
8972 prev = tail;
8973 MB_PTR_BACK(head, prev);
8974 c1 = PTR2CHAR(prev);
8975 if (mask == NULL)
8976 {
8977 if (c1 > ' ' && c1 != 0xa0)
8978 break;
8979 }
8980 else
8981 {
8982 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8983 if (c1 == PTR2CHAR(p))
8984 break;
8985 if (*p == NUL)
8986 break;
8987 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008988 }
8989 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02008990 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008991}
8992
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993#ifdef FEAT_FLOAT
8994/*
8995 * "trunc({float})" function
8996 */
8997 static void
8998f_trunc(typval_T *argvars, typval_T *rettv)
8999{
9000 float_T f = 0.0;
9001
9002 rettv->v_type = VAR_FLOAT;
9003 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009004 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9006 else
9007 rettv->vval.v_float = 0.0;
9008}
9009#endif
9010
9011/*
9012 * "type(expr)" function
9013 */
9014 static void
9015f_type(typval_T *argvars, typval_T *rettv)
9016{
9017 int n = -1;
9018
9019 switch (argvars[0].v_type)
9020 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009021 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9022 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009024 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9025 case VAR_LIST: n = VAR_TYPE_LIST; break;
9026 case VAR_DICT: n = VAR_TYPE_DICT; break;
9027 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9028 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9029 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009030 case VAR_JOB: n = VAR_TYPE_JOB; break;
9031 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009032 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009034 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009035 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009036 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 n = -1;
9038 break;
9039 }
9040 rettv->vval.v_number = n;
9041}
9042
9043/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 * "virtcol(string)" function
9045 */
9046 static void
9047f_virtcol(typval_T *argvars, typval_T *rettv)
9048{
9049 colnr_T vcol = 0;
9050 pos_T *fp;
9051 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009052 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053
9054 fp = var2fpos(&argvars[0], FALSE, &fnum);
9055 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9056 && fnum == curbuf->b_fnum)
9057 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009058 // Limit the column to a valid value, getvvcol() doesn't check.
9059 if (fp->col < 0)
9060 fp->col = 0;
9061 else
9062 {
9063 len = (int)STRLEN(ml_get(fp->lnum));
9064 if (fp->col > len)
9065 fp->col = len;
9066 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 getvvcol(curwin, fp, NULL, NULL, &vcol);
9068 ++vcol;
9069 }
9070
9071 rettv->vval.v_number = vcol;
9072}
9073
9074/*
9075 * "visualmode()" function
9076 */
9077 static void
9078f_visualmode(typval_T *argvars, typval_T *rettv)
9079{
9080 char_u str[2];
9081
9082 rettv->v_type = VAR_STRING;
9083 str[0] = curbuf->b_visual_mode_eval;
9084 str[1] = NUL;
9085 rettv->vval.v_string = vim_strsave(str);
9086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009087 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 if (non_zero_arg(&argvars[0]))
9089 curbuf->b_visual_mode_eval = NUL;
9090}
9091
9092/*
9093 * "wildmenumode()" function
9094 */
9095 static void
9096f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9097{
9098#ifdef FEAT_WILDMENU
9099 if (wild_menu_showing)
9100 rettv->vval.v_number = 1;
9101#endif
9102}
9103
9104/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009105 * "windowsversion()" function
9106 */
9107 static void
9108f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9109{
9110 rettv->v_type = VAR_STRING;
9111 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9112}
9113
9114/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 * "wordcount()" function
9116 */
9117 static void
9118f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9119{
9120 if (rettv_dict_alloc(rettv) == FAIL)
9121 return;
9122 cursor_pos_info(rettv->vval.v_dict);
9123}
9124
9125/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 * "xor(expr, expr)" function
9127 */
9128 static void
9129f_xor(typval_T *argvars, typval_T *rettv)
9130{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009131 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9132 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133}
9134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009135#endif // FEAT_EVAL