blob: 270dcd2cd49b3afb3167bb4150e953816410cb23 [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 Moolenaar3d945cc2020-08-06 21:26:59 +0200396 static type_T *
397ret_getreg(int argcount, type_T **argtypes UNUSED)
398{
399 // Assume that if the third argument is passed it's non-zero
400 if (argcount == 3)
401 return &t_list_string;
402 return &t_string;
403}
404
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200405 static type_T *
406ret_maparg(int argcount, type_T **argtypes UNUSED)
407{
408 // Assume that if the fourth argument is passed it's non-zero
409 if (argcount == 4)
410 return &t_dict_any;
411 return &t_string;
412}
413
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100414static type_T *ret_f_function(int argcount, type_T **argtypes);
415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416/*
417 * Array with names and number of arguments of all internal functions
418 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
419 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200420typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200422 char *f_name; // function name
423 char f_min_argc; // minimal number of arguments
424 char f_max_argc; // maximal number of arguments
425 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100426 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
427 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200429 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200430} funcentry_T;
431
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200432// values for f_argtype; zero means it cannot be used as a method
433#define FEARG_1 1 // base is the first argument
434#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200435#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200436#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200437#define FEARG_LAST 9 // base is the last argument
438
Bram Moolenaar15c47602020-03-26 22:16:48 +0100439#ifdef FEAT_FLOAT
440# define FLOAT_FUNC(name) name
441#else
442# define FLOAT_FUNC(name) NULL
443#endif
444#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
445# define MATH_FUNC(name) name
446#else
447# define MATH_FUNC(name) NULL
448#endif
449#ifdef FEAT_TIMERS
450# define TIMER_FUNC(name) name
451#else
452# define TIMER_FUNC(name) NULL
453#endif
454#ifdef FEAT_JOB_CHANNEL
455# define JOB_FUNC(name) name
456#else
457# define JOB_FUNC(name) NULL
458#endif
459#ifdef FEAT_PROP_POPUP
460# define PROP_FUNC(name) name
461#else
462# define PROP_FUNC(name) NULL
463#endif
464#ifdef FEAT_SIGNS
465# define SIGN_FUNC(name) name
466#else
467# define SIGN_FUNC(name) NULL
468#endif
469#ifdef FEAT_SOUND
470# define SOUND_FUNC(name) name
471#else
472# define SOUND_FUNC(name) NULL
473#endif
474#ifdef FEAT_TERMINAL
475# define TERM_FUNC(name) name
476#else
477# define TERM_FUNC(name) NULL
478#endif
479
Bram Moolenaarac92e252019-08-03 21:58:38 +0200480static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200481{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100482 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
483 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200484 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100485 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200486 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200487 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488 {"argc", 0, 1, 0, ret_number, f_argc},
489 {"argidx", 0, 0, 0, ret_number, f_argidx},
490 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200491 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100492 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100493 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
494 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200495 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100496 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar1d634542020-08-18 13:41:50 +0200497 {"assert_fails", 1, 4, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100498 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
499 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
500 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
501 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
502 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
503 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
504 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100505 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
506 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
507 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100508#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100509 f_balloon_gettext
510#else
511 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100513 },
514 {"balloon_show", 1, 1, FEARG_1, ret_void,
515#ifdef FEAT_BEVAL
516 f_balloon_show
517#else
518 NULL
519#endif
520 },
521 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
522#if defined(FEAT_BEVAL_TERM)
523 f_balloon_split
524#else
525 NULL
526#endif
527 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100528 {"browse", 4, 4, 0, ret_string, f_browse},
529 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
530 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
531 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
532 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
533 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
534 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
535 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
536 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
537 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
538 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
539 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
540 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
541 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
542 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
543 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
544 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
545 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100546 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
547 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
548 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
549 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
550 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
551 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
552 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
553 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
554 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
555 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
556 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
557 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
558 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
559 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
560 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
561 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
562 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
563 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
564 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100565 {"changenr", 0, 0, 0, ret_number, f_changenr},
566 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
567 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
568 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
569 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
570 {"col", 1, 1, FEARG_1, ret_number, f_col},
571 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
572 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
573 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
574 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
575 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200576 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100577 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
578 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100579 {"count", 2, 4, FEARG_1, ret_number, f_count},
580 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
581 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100582 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100583#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100584 f_debugbreak
585#else
586 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200587#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100588 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200589 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100590 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
591 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
592 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
593 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
594 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
595 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
596 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
597 {"environ", 0, 0, 0, ret_dict_string, f_environ},
598 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
599 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
600 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
601 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
602 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
603 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
604 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100605 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100606 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
607 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200608 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100609 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
610 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
611 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
612 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200613 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100614 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
615 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200616 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100617 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
618 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
619 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100620 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
621 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
622 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
623 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
624 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
625 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
626 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
627 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200628 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100629 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
630 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
631 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200632 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100633 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
634 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
635 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
636 {"getchar", 0, 1, 0, ret_number, f_getchar},
637 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
638 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
639 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
640 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
641 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
642 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
643 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
644 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
645 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
646 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
647 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
648 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
649 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
650 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
651 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
652 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
653 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
654 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200655 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200656 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100657 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
658 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
659 {"getpid", 0, 0, 0, ret_number, f_getpid},
660 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200661 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200662 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200663 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100664 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
665 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
666 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
667 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
668 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
669 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
670 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
671 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
672 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
673 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
674 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
675 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
676 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100677 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100678 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
679 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
680 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
681 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
682 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
683 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
684 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
685 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
686 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
687 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
688 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
689 {"hostname", 0, 0, 0, ret_string, f_hostname},
690 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
691 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
692 {"index", 2, 4, FEARG_1, ret_number, f_index},
693 {"input", 1, 3, FEARG_1, ret_string, f_input},
694 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
695 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
696 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
697 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
698 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200699 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100700 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
701 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
702 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100703 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100704 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100705 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100706 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100707 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
708 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
709 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
710 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
711 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
712 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100713 {"join", 1, 2, FEARG_1, ret_string, f_join},
714 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
715 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
716 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
717 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200718 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100719 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
720 {"len", 1, 1, FEARG_1, ret_number, f_len},
721 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
722 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
723 {"line", 1, 2, FEARG_1, ret_number, f_line},
724 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
725 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
726 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
727 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
728 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
729 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
730 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100731 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
732 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
733 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200734#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100735 f_luaeval
736#else
737 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200738#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100739 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100740 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200741 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100742 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200743 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100744 {"match", 2, 4, FEARG_1, ret_any, f_match},
745 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
746 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
747 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
748 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
749 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
750 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
751 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
752 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
753 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100754 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100755#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_menu_info
757#else
758 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100761 {"min", 1, 1, FEARG_1, ret_any, f_min},
762 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
763 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766 f_mzeval
767#else
768 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200769#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100770 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100771 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
772 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
773 {"or", 2, 2, FEARG_1, ret_number, f_or},
774 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100775 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100777 f_perleval
778#else
779 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100781 },
782 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
783 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200784 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100785 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
786 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
787 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
788 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
789 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
790 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
791 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
792 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
793 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
794 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200795 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100796 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
797 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
798 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
799 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
800 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
801 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
802 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
803 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100804 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
805 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100806 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
807 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
808 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
809 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
810 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
811 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
812 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
813 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
814 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
815 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
816 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
817 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
818 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100819 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
820 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100821 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100823 f_py3eval
824#else
825 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100827 },
828 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100830 f_pyeval
831#else
832 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100834 },
835 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100836#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100837 f_pyxeval
838#else
839 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100840#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100841 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100842 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
843 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200844 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
845 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100846 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200847 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100848 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
849 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
850 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100851 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100852 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
853 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
854 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
855 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
856 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
857 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200858 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200859 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100860 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200861 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100862 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200863 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100864 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
865 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100866#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100867 f_rubyeval
868#else
869 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100870#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100871 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100872 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
873 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
874 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
875 {"screencol", 0, 0, 0, ret_number, f_screencol},
876 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
877 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
878 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200879 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200880 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100881 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
882 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
883 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200884 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100885 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
886 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
887 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
888 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
889 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
890 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
891 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
892 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
893 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
894 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
895 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
896 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
897 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
898 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
899 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
900 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
901 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
902 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100903 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100905 f_sha256
906#else
907 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100909 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100910 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
911 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100912 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
913 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
914 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
915 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
916 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
917 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
918 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
919 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
920 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200921 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100922 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
923 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200924 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100925 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
926 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
927 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
928 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100929 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
930 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
931 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
932 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100933 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100934 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
935 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100936 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100937 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
938 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
939 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
940 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
941 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100942 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100944 f_strftime
945#else
946 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100948 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100949 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
950 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
951 {"string", 1, 1, FEARG_1, ret_string, f_string},
952 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200953 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100955#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100956 f_strptime
957#else
958 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100959#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100960 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100961 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
962 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
963 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
964 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
965 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
966 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
967 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
968 {"synID", 3, 3, 0, ret_number, f_synID},
969 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
970 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
971 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
972 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
973 {"system", 1, 2, FEARG_1, ret_string, f_system},
974 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
975 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
976 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
977 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
978 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
979 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100980 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
981 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100982 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100983 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
984 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
985 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
986 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
987 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100988#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100989 f_term_getansicolors
990#else
991 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100993 },
994 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
995 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
996 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
997 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
998 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
999 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1000 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1001 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1002 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1003 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1004 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1005 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1006 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001007#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001008 f_term_setansicolors
1009#else
1010 NULL
1011#endif
1012 },
1013 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1014 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1015 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1016 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1017 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1018 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001019 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001020 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1021 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1022 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1023 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1024 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1025 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1026 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1027 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001028 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001029 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001030 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001031 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001032 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001033 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001034 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1035 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1036 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1037 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001038 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001039#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001040 f_test_scrollbar
1041#else
1042 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001043#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001044 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001045 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1046 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1047 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1048 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001049 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1051 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1052 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1053 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1054 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001055 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1056 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1057 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001058 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001059 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001060 {"type", 1, 1, FEARG_1, ret_number, f_type},
1061 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1062 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1063 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1064 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1065 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1066 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1067 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1068 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1069 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1070 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1071 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1072 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1073 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1074 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1075 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1076 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1077 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1078 {"wincol", 0, 0, 0, ret_number, f_wincol},
1079 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1080 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1081 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1082 {"winline", 0, 0, 0, ret_number, f_winline},
1083 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1084 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1085 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1086 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1087 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1088 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1089 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1090 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001091};
1092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001093/*
1094 * Function given to ExpandGeneric() to obtain the list of internal
1095 * or user defined function names.
1096 */
1097 char_u *
1098get_function_name(expand_T *xp, int idx)
1099{
1100 static int intidx = -1;
1101 char_u *name;
1102
1103 if (idx == 0)
1104 intidx = -1;
1105 if (intidx < 0)
1106 {
1107 name = get_user_func_name(xp, idx);
1108 if (name != NULL)
1109 return name;
1110 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001111 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001112 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001113 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001114 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001115 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116 STRCAT(IObuff, ")");
1117 return IObuff;
1118 }
1119
1120 return NULL;
1121}
1122
1123/*
1124 * Function given to ExpandGeneric() to obtain the list of internal or
1125 * user defined variable or function names.
1126 */
1127 char_u *
1128get_expr_name(expand_T *xp, int idx)
1129{
1130 static int intidx = -1;
1131 char_u *name;
1132
1133 if (idx == 0)
1134 intidx = -1;
1135 if (intidx < 0)
1136 {
1137 name = get_function_name(xp, idx);
1138 if (name != NULL)
1139 return name;
1140 }
1141 return get_user_var_name(xp, ++intidx);
1142}
1143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001144/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001145 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001146 * Return index, or -1 if not found or "implemented" is TRUE and the function
1147 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001149 static int
1150find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001151{
1152 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001153 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154 int cmp;
1155 int x;
1156
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001157 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001158
1159 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001160 while (first <= last)
1161 {
1162 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001163 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164 if (cmp < 0)
1165 last = x - 1;
1166 else if (cmp > 0)
1167 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001168 else if (implemented && global_functions[x].f_func == NULL)
1169 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001170 else
1171 return x;
1172 }
1173 return -1;
1174}
1175
Bram Moolenaar15c47602020-03-26 22:16:48 +01001176/*
1177 * Find internal function "name" in table "global_functions".
1178 * Return index, or -1 if not found or the function is not implemented.
1179 */
1180 int
1181find_internal_func(char_u *name)
1182{
1183 return find_internal_func_opt(name, TRUE);
1184}
1185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001186 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001187has_internal_func(char_u *name)
1188{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001189 return find_internal_func_opt(name, TRUE) >= 0;
1190}
1191
1192 static int
1193has_internal_func_name(char_u *name)
1194{
1195 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001196}
1197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 char *
1199internal_func_name(int idx)
1200{
1201 return global_functions[idx].f_name;
1202}
1203
1204 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001205internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001207 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208}
1209
1210/*
1211 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001212 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1213 * first argument, 2 if method base is second argument, etc. 9 if method base
1214 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 */
1216 int
1217check_internal_func(int idx, int argcount)
1218{
1219 int res;
1220 char *name;
1221
1222 if (argcount < global_functions[idx].f_min_argc)
1223 res = FCERR_TOOFEW;
1224 else if (argcount > global_functions[idx].f_max_argc)
1225 res = FCERR_TOOMANY;
1226 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001227 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229 name = internal_func_name(idx);
1230 if (res == FCERR_TOOMANY)
1231 semsg(_(e_toomanyarg), name);
1232 else
1233 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001234 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235}
1236
Bram Moolenaarac92e252019-08-03 21:58:38 +02001237 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238call_internal_func(
1239 char_u *name,
1240 int argcount,
1241 typval_T *argvars,
1242 typval_T *rettv)
1243{
1244 int i;
1245
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001246 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001248 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001249 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001250 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001251 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001252 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001254 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001255 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001256}
1257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 void
1259call_internal_func_by_idx(
1260 int idx,
1261 typval_T *argvars,
1262 typval_T *rettv)
1263{
1264 global_functions[idx].f_func(argvars, rettv);
1265}
1266
Bram Moolenaarac92e252019-08-03 21:58:38 +02001267/*
1268 * Invoke a method for base->method().
1269 */
1270 int
1271call_internal_method(
1272 char_u *name,
1273 int argcount,
1274 typval_T *argvars,
1275 typval_T *rettv,
1276 typval_T *basetv)
1277{
1278 int i;
1279 int fi;
1280 typval_T argv[MAX_FUNC_ARGS + 1];
1281
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001282 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001283 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001284 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001285 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001286 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001287 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001288 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001289 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001290 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001291
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001292 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001293 {
1294 // base value goes last
1295 for (i = 0; i < argcount; ++i)
1296 argv[i] = argvars[i];
1297 argv[argcount] = *basetv;
1298 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001299 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001300 {
1301 // base value goes second
1302 argv[0] = argvars[0];
1303 argv[1] = *basetv;
1304 for (i = 1; i < argcount; ++i)
1305 argv[i + 1] = argvars[i];
1306 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001307 else if (global_functions[fi].f_argtype == FEARG_3)
1308 {
1309 // base value goes third
1310 argv[0] = argvars[0];
1311 argv[1] = argvars[1];
1312 argv[2] = *basetv;
1313 for (i = 2; i < argcount; ++i)
1314 argv[i + 1] = argvars[i];
1315 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001316 else if (global_functions[fi].f_argtype == FEARG_4)
1317 {
1318 // base value goes fourth
1319 argv[0] = argvars[0];
1320 argv[1] = argvars[1];
1321 argv[2] = argvars[2];
1322 argv[3] = *basetv;
1323 for (i = 3; i < argcount; ++i)
1324 argv[i + 1] = argvars[i];
1325 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001326 else
1327 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001328 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001329 argv[0] = *basetv;
1330 for (i = 0; i < argcount; ++i)
1331 argv[i + 1] = argvars[i];
1332 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001333 argv[argcount + 1].v_type = VAR_UNKNOWN;
1334
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001335 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001336 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337}
1338
1339/*
1340 * Return TRUE for a non-zero Number and a non-empty String.
1341 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001342 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343non_zero_arg(typval_T *argvars)
1344{
1345 return ((argvars[0].v_type == VAR_NUMBER
1346 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001347 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348 && argvars[0].vval.v_number == VVAL_TRUE)
1349 || (argvars[0].v_type == VAR_STRING
1350 && argvars[0].vval.v_string != NULL
1351 && *argvars[0].vval.v_string != NUL));
1352}
1353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355/*
1356 * Get the float value of "argvars[0]" into "f".
1357 * Returns FAIL when the argument is not a Number or Float.
1358 */
1359 static int
1360get_float_arg(typval_T *argvars, float_T *f)
1361{
1362 if (argvars[0].v_type == VAR_FLOAT)
1363 {
1364 *f = argvars[0].vval.v_float;
1365 return OK;
1366 }
1367 if (argvars[0].v_type == VAR_NUMBER)
1368 {
1369 *f = (float_T)argvars[0].vval.v_number;
1370 return OK;
1371 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001372 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 return FAIL;
1374}
1375
1376/*
1377 * "abs(expr)" function
1378 */
1379 static void
1380f_abs(typval_T *argvars, typval_T *rettv)
1381{
1382 if (argvars[0].v_type == VAR_FLOAT)
1383 {
1384 rettv->v_type = VAR_FLOAT;
1385 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1386 }
1387 else
1388 {
1389 varnumber_T n;
1390 int error = FALSE;
1391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001393 if (error)
1394 rettv->vval.v_number = -1;
1395 else if (n > 0)
1396 rettv->vval.v_number = n;
1397 else
1398 rettv->vval.v_number = -n;
1399 }
1400}
1401
1402/*
1403 * "acos()" function
1404 */
1405 static void
1406f_acos(typval_T *argvars, typval_T *rettv)
1407{
1408 float_T f = 0.0;
1409
1410 rettv->v_type = VAR_FLOAT;
1411 if (get_float_arg(argvars, &f) == OK)
1412 rettv->vval.v_float = acos(f);
1413 else
1414 rettv->vval.v_float = 0.0;
1415}
1416#endif
1417
1418/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419 * "and(expr, expr)" function
1420 */
1421 static void
1422f_and(typval_T *argvars, typval_T *rettv)
1423{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1425 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001426}
1427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001428#ifdef FEAT_FLOAT
1429/*
1430 * "asin()" function
1431 */
1432 static void
1433f_asin(typval_T *argvars, typval_T *rettv)
1434{
1435 float_T f = 0.0;
1436
1437 rettv->v_type = VAR_FLOAT;
1438 if (get_float_arg(argvars, &f) == OK)
1439 rettv->vval.v_float = asin(f);
1440 else
1441 rettv->vval.v_float = 0.0;
1442}
1443
1444/*
1445 * "atan()" function
1446 */
1447 static void
1448f_atan(typval_T *argvars, typval_T *rettv)
1449{
1450 float_T f = 0.0;
1451
1452 rettv->v_type = VAR_FLOAT;
1453 if (get_float_arg(argvars, &f) == OK)
1454 rettv->vval.v_float = atan(f);
1455 else
1456 rettv->vval.v_float = 0.0;
1457}
1458
1459/*
1460 * "atan2()" function
1461 */
1462 static void
1463f_atan2(typval_T *argvars, typval_T *rettv)
1464{
1465 float_T fx = 0.0, fy = 0.0;
1466
1467 rettv->v_type = VAR_FLOAT;
1468 if (get_float_arg(argvars, &fx) == OK
1469 && get_float_arg(&argvars[1], &fy) == OK)
1470 rettv->vval.v_float = atan2(fx, fy);
1471 else
1472 rettv->vval.v_float = 0.0;
1473}
1474#endif
1475
1476/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001477 * "balloon_show()" function
1478 */
1479#ifdef FEAT_BEVAL
1480 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001481f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1482{
1483 rettv->v_type = VAR_STRING;
1484 if (balloonEval != NULL)
1485 {
1486 if (balloonEval->msg == NULL)
1487 rettv->vval.v_string = NULL;
1488 else
1489 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1490 }
1491}
1492
1493 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001494f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1495{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001496 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001497 {
1498 if (argvars[0].v_type == VAR_LIST
1499# ifdef FEAT_GUI
1500 && !gui.in_use
1501# endif
1502 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001503 {
1504 list_T *l = argvars[0].vval.v_list;
1505
1506 // empty list removes the balloon
1507 post_balloon(balloonEval, NULL,
1508 l == NULL || l->lv_len == 0 ? NULL : l);
1509 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001510 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001511 {
1512 char_u *mesg = tv_get_string_chk(&argvars[0]);
1513
1514 if (mesg != NULL)
1515 // empty string removes the balloon
1516 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1517 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001518 }
1519}
1520
Bram Moolenaar669a8282017-11-19 20:13:05 +01001521# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001522 static void
1523f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1524{
1525 if (rettv_list_alloc(rettv) == OK)
1526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001527 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001528
1529 if (msg != NULL)
1530 {
1531 pumitem_T *array;
1532 int size = split_message(msg, &array);
1533 int i;
1534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001535 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001536 for (i = 1; i < size - 1; ++i)
1537 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001538 while (size > 0)
1539 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001540 vim_free(array);
1541 }
1542 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001543}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001544# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001545#endif
1546
1547/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001548 * Get the buffer from "arg" and give an error and return NULL if it is not
1549 * valid.
1550 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001551 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001552get_buf_arg(typval_T *arg)
1553{
1554 buf_T *buf;
1555
1556 ++emsg_off;
1557 buf = tv_get_buf(arg, FALSE);
1558 --emsg_off;
1559 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001560 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001561 return buf;
1562}
1563
1564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565 * "byte2line(byte)" function
1566 */
1567 static void
1568f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1569{
1570#ifndef FEAT_BYTEOFF
1571 rettv->vval.v_number = -1;
1572#else
1573 long boff = 0;
1574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001575 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001576 if (boff < 0)
1577 rettv->vval.v_number = -1;
1578 else
1579 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1580 (linenr_T)0, &boff);
1581#endif
1582}
1583
1584 static void
1585byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1586{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588 char_u *str;
1589 varnumber_T idx;
1590
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 str = tv_get_string_chk(&argvars[0]);
1592 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 rettv->vval.v_number = -1;
1594 if (str == NULL || idx < 0)
1595 return;
1596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 t = str;
1598 for ( ; idx > 0; idx--)
1599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001600 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 return;
1602 if (enc_utf8 && comp)
1603 t += utf_ptr2len(t);
1604 else
1605 t += (*mb_ptr2len)(t);
1606 }
1607 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608}
1609
1610/*
1611 * "byteidx()" function
1612 */
1613 static void
1614f_byteidx(typval_T *argvars, typval_T *rettv)
1615{
1616 byteidx(argvars, rettv, FALSE);
1617}
1618
1619/*
1620 * "byteidxcomp()" function
1621 */
1622 static void
1623f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1624{
1625 byteidx(argvars, rettv, TRUE);
1626}
1627
1628/*
1629 * "call(func, arglist [, dict])" function
1630 */
1631 static void
1632f_call(typval_T *argvars, typval_T *rettv)
1633{
1634 char_u *func;
1635 partial_T *partial = NULL;
1636 dict_T *selfdict = NULL;
1637
1638 if (argvars[1].v_type != VAR_LIST)
1639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001640 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 return;
1642 }
1643 if (argvars[1].vval.v_list == NULL)
1644 return;
1645
1646 if (argvars[0].v_type == VAR_FUNC)
1647 func = argvars[0].vval.v_string;
1648 else if (argvars[0].v_type == VAR_PARTIAL)
1649 {
1650 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001651 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652 }
1653 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001654 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001656 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657
1658 if (argvars[2].v_type != VAR_UNKNOWN)
1659 {
1660 if (argvars[2].v_type != VAR_DICT)
1661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001662 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663 return;
1664 }
1665 selfdict = argvars[2].vval.v_dict;
1666 }
1667
1668 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1669}
1670
1671#ifdef FEAT_FLOAT
1672/*
1673 * "ceil({float})" function
1674 */
1675 static void
1676f_ceil(typval_T *argvars, typval_T *rettv)
1677{
1678 float_T f = 0.0;
1679
1680 rettv->v_type = VAR_FLOAT;
1681 if (get_float_arg(argvars, &f) == OK)
1682 rettv->vval.v_float = ceil(f);
1683 else
1684 rettv->vval.v_float = 0.0;
1685}
1686#endif
1687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688/*
1689 * "changenr()" function
1690 */
1691 static void
1692f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1693{
1694 rettv->vval.v_number = curbuf->b_u_seq_cur;
1695}
1696
1697/*
1698 * "char2nr(string)" function
1699 */
1700 static void
1701f_char2nr(typval_T *argvars, typval_T *rettv)
1702{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703 if (has_mbyte)
1704 {
1705 int utf8 = 0;
1706
1707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001708 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001709
1710 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001711 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001713 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 }
1715 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001716 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717}
1718
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001719 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001720get_optional_window(typval_T *argvars, int idx)
1721{
1722 win_T *win = curwin;
1723
1724 if (argvars[idx].v_type != VAR_UNKNOWN)
1725 {
1726 win = find_win_by_nr_or_id(&argvars[idx]);
1727 if (win == NULL)
1728 {
1729 emsg(_(e_invalwindow));
1730 return NULL;
1731 }
1732 }
1733 return win;
1734}
1735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737 * "col(string)" function
1738 */
1739 static void
1740f_col(typval_T *argvars, typval_T *rettv)
1741{
1742 colnr_T col = 0;
1743 pos_T *fp;
1744 int fnum = curbuf->b_fnum;
1745
1746 fp = var2fpos(&argvars[0], FALSE, &fnum);
1747 if (fp != NULL && fnum == curbuf->b_fnum)
1748 {
1749 if (fp->col == MAXCOL)
1750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001751 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001752 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1753 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1754 else
1755 col = MAXCOL;
1756 }
1757 else
1758 {
1759 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 // col(".") when the cursor is on the NUL at the end of the line
1761 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762 if (virtual_active() && fp == &curwin->w_cursor)
1763 {
1764 char_u *p = ml_get_cursor();
1765
1766 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1767 curwin->w_virtcol - curwin->w_cursor.coladd))
1768 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001769 int l;
1770
1771 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1772 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001773 }
1774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001775 }
1776 }
1777 rettv->vval.v_number = col;
1778}
1779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780/*
1781 * "confirm(message, buttons[, default [, type]])" function
1782 */
1783 static void
1784f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1785{
1786#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1787 char_u *message;
1788 char_u *buttons = NULL;
1789 char_u buf[NUMBUFLEN];
1790 char_u buf2[NUMBUFLEN];
1791 int def = 1;
1792 int type = VIM_GENERIC;
1793 char_u *typestr;
1794 int error = FALSE;
1795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001796 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001797 if (message == NULL)
1798 error = TRUE;
1799 if (argvars[1].v_type != VAR_UNKNOWN)
1800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001801 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802 if (buttons == NULL)
1803 error = TRUE;
1804 if (argvars[2].v_type != VAR_UNKNOWN)
1805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 if (argvars[3].v_type != VAR_UNKNOWN)
1808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001809 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001810 if (typestr == NULL)
1811 error = TRUE;
1812 else
1813 {
1814 switch (TOUPPER_ASC(*typestr))
1815 {
1816 case 'E': type = VIM_ERROR; break;
1817 case 'Q': type = VIM_QUESTION; break;
1818 case 'I': type = VIM_INFO; break;
1819 case 'W': type = VIM_WARNING; break;
1820 case 'G': type = VIM_GENERIC; break;
1821 }
1822 }
1823 }
1824 }
1825 }
1826
1827 if (buttons == NULL || *buttons == NUL)
1828 buttons = (char_u *)_("&Ok");
1829
1830 if (!error)
1831 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1832 def, NULL, FALSE);
1833#endif
1834}
1835
1836/*
1837 * "copy()" function
1838 */
1839 static void
1840f_copy(typval_T *argvars, typval_T *rettv)
1841{
1842 item_copy(&argvars[0], rettv, FALSE, 0);
1843}
1844
1845#ifdef FEAT_FLOAT
1846/*
1847 * "cos()" function
1848 */
1849 static void
1850f_cos(typval_T *argvars, typval_T *rettv)
1851{
1852 float_T f = 0.0;
1853
1854 rettv->v_type = VAR_FLOAT;
1855 if (get_float_arg(argvars, &f) == OK)
1856 rettv->vval.v_float = cos(f);
1857 else
1858 rettv->vval.v_float = 0.0;
1859}
1860
1861/*
1862 * "cosh()" function
1863 */
1864 static void
1865f_cosh(typval_T *argvars, typval_T *rettv)
1866{
1867 float_T f = 0.0;
1868
1869 rettv->v_type = VAR_FLOAT;
1870 if (get_float_arg(argvars, &f) == OK)
1871 rettv->vval.v_float = cosh(f);
1872 else
1873 rettv->vval.v_float = 0.0;
1874}
1875#endif
1876
1877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 * "cursor(lnum, col)" function, or
1879 * "cursor(list)"
1880 *
1881 * Moves the cursor to the specified line and column.
1882 * Returns 0 when the position could be set, -1 otherwise.
1883 */
1884 static void
1885f_cursor(typval_T *argvars, typval_T *rettv)
1886{
1887 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 int set_curswant = TRUE;
1890
1891 rettv->vval.v_number = -1;
1892 if (argvars[1].v_type == VAR_UNKNOWN)
1893 {
1894 pos_T pos;
1895 colnr_T curswant = -1;
1896
1897 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 return;
1901 }
1902 line = pos.lnum;
1903 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (curswant >= 0)
1906 {
1907 curwin->w_curswant = curswant - 1;
1908 set_curswant = FALSE;
1909 }
1910 }
1911 else
1912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001913 line = tv_get_lnum(argvars);
1914 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001918 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920 if (line > 0)
1921 curwin->w_cursor.lnum = line;
1922 if (col > 0)
1923 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 if (has_mbyte)
1930 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931
1932 curwin->w_set_curswant = set_curswant;
1933 rettv->vval.v_number = 0;
1934}
1935
Bram Moolenaar4f974752019-02-17 17:44:42 +01001936#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001937/*
1938 * "debugbreak()" function
1939 */
1940 static void
1941f_debugbreak(typval_T *argvars, typval_T *rettv)
1942{
1943 int pid;
1944
1945 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001946 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001947 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001948 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001949 else
1950 {
1951 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1952
1953 if (hProcess != NULL)
1954 {
1955 DebugBreakProcess(hProcess);
1956 CloseHandle(hProcess);
1957 rettv->vval.v_number = OK;
1958 }
1959 }
1960}
1961#endif
1962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001963/*
1964 * "deepcopy()" function
1965 */
1966 static void
1967f_deepcopy(typval_T *argvars, typval_T *rettv)
1968{
1969 int noref = 0;
1970 int copyID;
1971
1972 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 else
1977 {
1978 copyID = get_copyID();
1979 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1980 }
1981}
1982
1983/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001984 * "did_filetype()" function
1985 */
1986 static void
1987f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1988{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990}
1991
1992/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001993 * "echoraw({expr})" function
1994 */
1995 static void
1996f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1997{
1998 char_u *str = tv_get_string_chk(&argvars[0]);
1999
2000 if (str != NULL && *str != NUL)
2001 {
2002 out_str(str);
2003 out_flush();
2004 }
2005}
2006
2007/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 * "empty({expr})" function
2009 */
2010 static void
2011f_empty(typval_T *argvars, typval_T *rettv)
2012{
2013 int n = FALSE;
2014
2015 switch (argvars[0].v_type)
2016 {
2017 case VAR_STRING:
2018 case VAR_FUNC:
2019 n = argvars[0].vval.v_string == NULL
2020 || *argvars[0].vval.v_string == NUL;
2021 break;
2022 case VAR_PARTIAL:
2023 n = FALSE;
2024 break;
2025 case VAR_NUMBER:
2026 n = argvars[0].vval.v_number == 0;
2027 break;
2028 case VAR_FLOAT:
2029#ifdef FEAT_FLOAT
2030 n = argvars[0].vval.v_float == 0.0;
2031 break;
2032#endif
2033 case VAR_LIST:
2034 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002035 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 break;
2037 case VAR_DICT:
2038 n = argvars[0].vval.v_dict == NULL
2039 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2040 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002041 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 case VAR_SPECIAL:
2043 n = argvars[0].vval.v_number != VVAL_TRUE;
2044 break;
2045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002046 case VAR_BLOB:
2047 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002048 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2049 break;
2050
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 case VAR_JOB:
2052#ifdef FEAT_JOB_CHANNEL
2053 n = argvars[0].vval.v_job == NULL
2054 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2055 break;
2056#endif
2057 case VAR_CHANNEL:
2058#ifdef FEAT_JOB_CHANNEL
2059 n = argvars[0].vval.v_channel == NULL
2060 || !channel_is_open(argvars[0].vval.v_channel);
2061 break;
2062#endif
2063 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002064 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002066 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 n = TRUE;
2068 break;
2069 }
2070
2071 rettv->vval.v_number = n;
2072}
2073
2074/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002075 * "environ()" function
2076 */
2077 static void
2078f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2079{
2080#if !defined(AMIGA)
2081 int i = 0;
2082 char_u *entry, *value;
2083# ifdef MSWIN
2084 extern wchar_t **_wenviron;
2085# else
2086 extern char **environ;
2087# endif
2088
2089 if (rettv_dict_alloc(rettv) != OK)
2090 return;
2091
2092# ifdef MSWIN
2093 if (*_wenviron == NULL)
2094 return;
2095# else
2096 if (*environ == NULL)
2097 return;
2098# endif
2099
2100 for (i = 0; ; ++i)
2101 {
2102# ifdef MSWIN
2103 short_u *p;
2104
2105 if ((p = (short_u *)_wenviron[i]) == NULL)
2106 return;
2107 entry = utf16_to_enc(p, NULL);
2108# else
2109 if ((entry = (char_u *)environ[i]) == NULL)
2110 return;
2111 entry = vim_strsave(entry);
2112# endif
2113 if (entry == NULL) // out of memory
2114 return;
2115 if ((value = vim_strchr(entry, '=')) == NULL)
2116 {
2117 vim_free(entry);
2118 continue;
2119 }
2120 *value++ = NUL;
2121 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2122 vim_free(entry);
2123 }
2124#endif
2125}
2126
2127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 * "escape({string}, {chars})" function
2129 */
2130 static void
2131f_escape(typval_T *argvars, typval_T *rettv)
2132{
2133 char_u buf[NUMBUFLEN];
2134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002135 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2136 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002137 rettv->v_type = VAR_STRING;
2138}
2139
2140/*
2141 * "eval()" function
2142 */
2143 static void
2144f_eval(typval_T *argvars, typval_T *rettv)
2145{
2146 char_u *s, *p;
2147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 if (s != NULL)
2150 s = skipwhite(s);
2151
2152 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002153 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 {
2155 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 need_clr_eos = FALSE;
2158 rettv->v_type = VAR_NUMBER;
2159 rettv->vval.v_number = 0;
2160 }
2161 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002162 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163}
2164
2165/*
2166 * "eventhandler()" function
2167 */
2168 static void
2169f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2170{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002171 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172}
2173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174static garray_T redir_execute_ga;
2175
2176/*
2177 * Append "value[value_len]" to the execute() output.
2178 */
2179 void
2180execute_redir_str(char_u *value, int value_len)
2181{
2182 int len;
2183
2184 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002185 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002187 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188 if (ga_grow(&redir_execute_ga, len) == OK)
2189 {
2190 mch_memmove((char *)redir_execute_ga.ga_data
2191 + redir_execute_ga.ga_len, value, len);
2192 redir_execute_ga.ga_len += len;
2193 }
2194}
2195
2196/*
2197 * Get next line from a list.
2198 * Called by do_cmdline() to get the next line.
2199 * Returns allocated string, or NULL for end of function.
2200 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002201 static char_u *
2202get_list_line(
2203 int c UNUSED,
2204 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002205 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002206 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207{
2208 listitem_T **p = (listitem_T **)cookie;
2209 listitem_T *item = *p;
2210 char_u buf[NUMBUFLEN];
2211 char_u *s;
2212
2213 if (item == NULL)
2214 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002215 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 *p = item->li_next;
2217 return s == NULL ? NULL : vim_strsave(s);
2218}
2219
2220/*
2221 * "execute()" function
2222 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002223 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002224execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225{
2226 char_u *cmd = NULL;
2227 list_T *list = NULL;
2228 int save_msg_silent = msg_silent;
2229 int save_emsg_silent = emsg_silent;
2230 int save_emsg_noredir = emsg_noredir;
2231 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002232 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002234 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002235 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002236
2237 rettv->vval.v_string = NULL;
2238 rettv->v_type = VAR_STRING;
2239
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002240 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002241 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002242 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002243 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002244 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002245 return;
2246 ++list->lv_refcount;
2247 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002248 else if (argvars[arg_off].v_type == VAR_JOB
2249 || argvars[arg_off].v_type == VAR_CHANNEL)
2250 {
2251 emsg(_(e_inval_string));
2252 return;
2253 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002254 else
2255 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002256 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002257 if (cmd == NULL)
2258 return;
2259 }
2260
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002261 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002262 {
2263 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002264 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002265
2266 if (s == NULL)
2267 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002268 if (*s == NUL)
2269 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 if (STRNCMP(s, "silent", 6) == 0)
2271 ++msg_silent;
2272 if (STRCMP(s, "silent!") == 0)
2273 {
2274 emsg_silent = TRUE;
2275 emsg_noredir = TRUE;
2276 }
2277 }
2278 else
2279 ++msg_silent;
2280
2281 if (redir_execute)
2282 save_ga = redir_execute_ga;
2283 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2284 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002285 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002286 if (!echo_output)
2287 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288
2289 if (cmd != NULL)
2290 do_cmdline_cmd(cmd);
2291 else
2292 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002293 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002295 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002296 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002297 do_cmdline(NULL, get_list_line, (void *)&item,
2298 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2299 --list->lv_refcount;
2300 }
2301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002302 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002303 if (ga_grow(&redir_execute_ga, 1) == OK)
2304 {
2305 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2306 rettv->vval.v_string = redir_execute_ga.ga_data;
2307 }
2308 else
2309 {
2310 ga_clear(&redir_execute_ga);
2311 rettv->vval.v_string = NULL;
2312 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 msg_silent = save_msg_silent;
2314 emsg_silent = save_emsg_silent;
2315 emsg_noredir = save_emsg_noredir;
2316
2317 redir_execute = save_redir_execute;
2318 if (redir_execute)
2319 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002320 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002321
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002322 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002323 if (echo_output)
2324 // When not working silently: put it in column zero. A following
2325 // "echon" will overwrite the message, unavoidably.
2326 msg_col = 0;
2327 else
2328 // When working silently: Put it back where it was, since nothing
2329 // should have been written.
2330 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331}
2332
2333/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002334 * "execute()" function
2335 */
2336 static void
2337f_execute(typval_T *argvars, typval_T *rettv)
2338{
2339 execute_common(argvars, rettv, 0);
2340}
2341
2342/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002343 * "exists()" function
2344 */
2345 static void
2346f_exists(typval_T *argvars, typval_T *rettv)
2347{
2348 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002349 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002351 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002352 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355 if (mch_getenv(p + 1) != NULL)
2356 n = TRUE;
2357 else
2358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002359 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002360 p = expand_env_save(p);
2361 if (p != NULL && *p != '$')
2362 n = TRUE;
2363 vim_free(p);
2364 }
2365 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002366 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002368 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002370 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002372 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002374 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002376 else if (*p == '?') // internal function only
2377 {
2378 n = has_internal_func_name(p + 1);
2379 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 else if (*p == ':')
2381 {
2382 n = cmd_exists(p + 1);
2383 }
2384 else if (*p == '#')
2385 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 if (p[1] == '#')
2387 n = autocmd_supported(p + 2);
2388 else
2389 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002391 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002392 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002393 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 }
2395
2396 rettv->vval.v_number = n;
2397}
2398
2399#ifdef FEAT_FLOAT
2400/*
2401 * "exp()" function
2402 */
2403 static void
2404f_exp(typval_T *argvars, typval_T *rettv)
2405{
2406 float_T f = 0.0;
2407
2408 rettv->v_type = VAR_FLOAT;
2409 if (get_float_arg(argvars, &f) == OK)
2410 rettv->vval.v_float = exp(f);
2411 else
2412 rettv->vval.v_float = 0.0;
2413}
2414#endif
2415
2416/*
2417 * "expand()" function
2418 */
2419 static void
2420f_expand(typval_T *argvars, typval_T *rettv)
2421{
2422 char_u *s;
2423 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002424 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002425 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2426 expand_T xpc;
2427 int error = FALSE;
2428 char_u *result;
2429
2430 rettv->v_type = VAR_STRING;
2431 if (argvars[1].v_type != VAR_UNKNOWN
2432 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002433 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002434 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002435 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002437 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438 if (*s == '%' || *s == '#' || *s == '<')
2439 {
2440 ++emsg_off;
2441 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2442 --emsg_off;
2443 if (rettv->v_type == VAR_LIST)
2444 {
2445 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2446 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002447 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 }
2449 else
2450 rettv->vval.v_string = result;
2451 }
2452 else
2453 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002454 // When the optional second argument is non-zero, don't remove matches
2455 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002457 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 options |= WILD_KEEP_ALL;
2459 if (!error)
2460 {
2461 ExpandInit(&xpc);
2462 xpc.xp_context = EXPAND_FILES;
2463 if (p_wic)
2464 options += WILD_ICASE;
2465 if (rettv->v_type == VAR_STRING)
2466 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2467 options, WILD_ALL);
2468 else if (rettv_list_alloc(rettv) != FAIL)
2469 {
2470 int i;
2471
2472 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2473 for (i = 0; i < xpc.xp_numfiles; i++)
2474 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2475 ExpandCleanup(&xpc);
2476 }
2477 }
2478 else
2479 rettv->vval.v_string = NULL;
2480 }
2481}
2482
2483/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002484 * "expandcmd()" function
2485 * Expand all the special characters in a command string.
2486 */
2487 static void
2488f_expandcmd(typval_T *argvars, typval_T *rettv)
2489{
2490 exarg_T eap;
2491 char_u *cmdstr;
2492 char *errormsg = NULL;
2493
2494 rettv->v_type = VAR_STRING;
2495 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2496
2497 memset(&eap, 0, sizeof(eap));
2498 eap.cmd = cmdstr;
2499 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002500 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002501 eap.usefilter = FALSE;
2502 eap.nextcmd = NULL;
2503 eap.cmdidx = CMD_USER;
2504
2505 expand_filename(&eap, &cmdstr, &errormsg);
2506 if (errormsg != NULL && *errormsg != NUL)
2507 emsg(errormsg);
2508
2509 rettv->vval.v_string = cmdstr;
2510}
2511
2512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 * "feedkeys()" function
2514 */
2515 static void
2516f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2517{
2518 int remap = TRUE;
2519 int insert = FALSE;
2520 char_u *keys, *flags;
2521 char_u nbuf[NUMBUFLEN];
2522 int typed = FALSE;
2523 int execute = FALSE;
2524 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002525 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526 char_u *keys_esc;
2527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002528 // This is not allowed in the sandbox. If the commands would still be
2529 // executed in the sandbox it would be OK, but it probably happens later,
2530 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 if (check_secure())
2532 return;
2533
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002534 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535
2536 if (argvars[1].v_type != VAR_UNKNOWN)
2537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 for ( ; *flags != NUL; ++flags)
2540 {
2541 switch (*flags)
2542 {
2543 case 'n': remap = FALSE; break;
2544 case 'm': remap = TRUE; break;
2545 case 't': typed = TRUE; break;
2546 case 'i': insert = TRUE; break;
2547 case 'x': execute = TRUE; break;
2548 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002549 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 }
2551 }
2552 }
2553
2554 if (*keys != NUL || execute)
2555 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002556 // Need to escape K_SPECIAL and CSI before putting the string in the
2557 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002558 keys_esc = vim_strsave_escape_csi(keys);
2559 if (keys_esc != NULL)
2560 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002561 if (lowlevel)
2562 {
2563#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002564 int idx;
2565 int len = (int)STRLEN(keys);
2566
2567 for (idx = 0; idx < len; ++idx)
2568 {
2569 // if a CTRL-C was typed, set got_int, similar to what
2570 // happens in fill_input_buf()
2571 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2572 got_int = TRUE;
2573 add_to_input_buf(keys + idx, 1);
2574 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002575#else
2576 emsg(_("E980: lowlevel input not supported"));
2577#endif
2578 }
2579 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002580 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002581 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002583 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002584#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002585 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002586#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002587 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002588 typebuf_was_filled = TRUE;
2589 }
2590 vim_free(keys_esc);
2591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 if (execute)
2593 {
2594 int save_msg_scroll = msg_scroll;
2595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002596 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 msg_scroll = FALSE;
2598
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002599 if (!dangerous)
2600 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002601 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002602 if (!dangerous)
2603 --ex_normal_busy;
2604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002605 msg_scroll |= save_msg_scroll;
2606 }
2607 }
2608 }
2609}
2610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002611#ifdef FEAT_FLOAT
2612/*
2613 * "float2nr({float})" function
2614 */
2615 static void
2616f_float2nr(typval_T *argvars, typval_T *rettv)
2617{
2618 float_T f = 0.0;
2619
2620 if (get_float_arg(argvars, &f) == OK)
2621 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002622 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002623 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002624 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002625 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002626 else
2627 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628 }
2629}
2630
2631/*
2632 * "floor({float})" function
2633 */
2634 static void
2635f_floor(typval_T *argvars, typval_T *rettv)
2636{
2637 float_T f = 0.0;
2638
2639 rettv->v_type = VAR_FLOAT;
2640 if (get_float_arg(argvars, &f) == OK)
2641 rettv->vval.v_float = floor(f);
2642 else
2643 rettv->vval.v_float = 0.0;
2644}
2645
2646/*
2647 * "fmod()" function
2648 */
2649 static void
2650f_fmod(typval_T *argvars, typval_T *rettv)
2651{
2652 float_T fx = 0.0, fy = 0.0;
2653
2654 rettv->v_type = VAR_FLOAT;
2655 if (get_float_arg(argvars, &fx) == OK
2656 && get_float_arg(&argvars[1], &fy) == OK)
2657 rettv->vval.v_float = fmod(fx, fy);
2658 else
2659 rettv->vval.v_float = 0.0;
2660}
2661#endif
2662
2663/*
2664 * "fnameescape({string})" function
2665 */
2666 static void
2667f_fnameescape(typval_T *argvars, typval_T *rettv)
2668{
2669 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002670 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671 rettv->v_type = VAR_STRING;
2672}
2673
2674/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 * "foreground()" function
2676 */
2677 static void
2678f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2679{
2680#ifdef FEAT_GUI
2681 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002682 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002684 return;
2685 }
2686#endif
2687#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689#endif
2690}
2691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002693common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694{
2695 char_u *s;
2696 char_u *name;
2697 int use_string = FALSE;
2698 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002699 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002700 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701
2702 if (argvars[0].v_type == VAR_FUNC)
2703 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002704 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 s = argvars[0].vval.v_string;
2706 }
2707 else if (argvars[0].v_type == VAR_PARTIAL
2708 && argvars[0].vval.v_partial != NULL)
2709 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002710 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002712 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 }
2714 else
2715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002716 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002717 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 use_string = TRUE;
2719 }
2720
Bram Moolenaar843b8842016-08-21 14:36:15 +02002721 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002722 {
2723 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002724 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002725 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2726 if (*name != NUL)
2727 s = NULL;
2728 }
2729
Bram Moolenaar843b8842016-08-21 14:36:15 +02002730 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2731 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002732 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002733 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002734 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002735 ? find_func(trans_name, is_global, NULL) == NULL
2736 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002737 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002738 else
2739 {
2740 int dict_idx = 0;
2741 int arg_idx = 0;
2742 list_T *list = NULL;
2743
2744 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2745 {
2746 char sid_buf[25];
2747 int off = *s == 's' ? 2 : 5;
2748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // Expand s: and <SID> into <SNR>nr_, so that the function can
2750 // also be called from another script. Using trans_function_name()
2751 // would also work, but some plugins depend on the name being
2752 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002753 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002754 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 if (name != NULL)
2756 {
2757 STRCPY(name, sid_buf);
2758 STRCAT(name, s + off);
2759 }
2760 }
2761 else
2762 name = vim_strsave(s);
2763
2764 if (argvars[1].v_type != VAR_UNKNOWN)
2765 {
2766 if (argvars[2].v_type != VAR_UNKNOWN)
2767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002768 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 arg_idx = 1;
2770 dict_idx = 2;
2771 }
2772 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002773 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002774 dict_idx = 1;
2775 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002776 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 arg_idx = 1;
2778 if (dict_idx > 0)
2779 {
2780 if (argvars[dict_idx].v_type != VAR_DICT)
2781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002782 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002784 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 }
2786 if (argvars[dict_idx].vval.v_dict == NULL)
2787 dict_idx = 0;
2788 }
2789 if (arg_idx > 0)
2790 {
2791 if (argvars[arg_idx].v_type != VAR_LIST)
2792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002793 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002795 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 }
2797 list = argvars[arg_idx].vval.v_list;
2798 if (list == NULL || list->lv_len == 0)
2799 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002800 else if (list->lv_len > MAX_FUNC_ARGS)
2801 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002802 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002803 vim_free(name);
2804 goto theend;
2805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002806 }
2807 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002808 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002810 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002812 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813 if (pt == NULL)
2814 vim_free(name);
2815 else
2816 {
2817 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2818 {
2819 listitem_T *li;
2820 int i = 0;
2821 int arg_len = 0;
2822 int lv_len = 0;
2823
2824 if (arg_pt != NULL)
2825 arg_len = arg_pt->pt_argc;
2826 if (list != NULL)
2827 lv_len = list->lv_len;
2828 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002829 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002830 if (pt->pt_argv == NULL)
2831 {
2832 vim_free(pt);
2833 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002834 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002836 for (i = 0; i < arg_len; i++)
2837 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2838 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002839 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002840 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002841 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002842 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002843 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002844 }
2845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002846 // For "function(dict.func, [], dict)" and "func" is a partial
2847 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 if (dict_idx > 0)
2849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002850 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2852 ++pt->pt_dict->dv_refcount;
2853 }
2854 else if (arg_pt != NULL)
2855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002856 // If the dict was bound automatically the result is also
2857 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002858 pt->pt_dict = arg_pt->pt_dict;
2859 pt->pt_auto = arg_pt->pt_auto;
2860 if (pt->pt_dict != NULL)
2861 ++pt->pt_dict->dv_refcount;
2862 }
2863
2864 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002865 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2866 {
2867 pt->pt_func = arg_pt->pt_func;
2868 func_ptr_ref(pt->pt_func);
2869 vim_free(name);
2870 }
2871 else if (is_funcref)
2872 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002873 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002874 func_ptr_ref(pt->pt_func);
2875 vim_free(name);
2876 }
2877 else
2878 {
2879 pt->pt_name = name;
2880 func_ref(name);
2881 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002882 }
2883 rettv->v_type = VAR_PARTIAL;
2884 rettv->vval.v_partial = pt;
2885 }
2886 else
2887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 rettv->v_type = VAR_FUNC;
2890 rettv->vval.v_string = name;
2891 func_ref(name);
2892 }
2893 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002894theend:
2895 vim_free(trans_name);
2896}
2897
2898/*
2899 * "funcref()" function
2900 */
2901 static void
2902f_funcref(typval_T *argvars, typval_T *rettv)
2903{
2904 common_function(argvars, rettv, TRUE);
2905}
2906
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002907 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002908ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002909{
2910 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2911 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002912 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002913}
2914
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002915/*
2916 * "function()" function
2917 */
2918 static void
2919f_function(typval_T *argvars, typval_T *rettv)
2920{
2921 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922}
2923
2924/*
2925 * "garbagecollect()" function
2926 */
2927 static void
2928f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2929{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002930 // This is postponed until we are back at the toplevel, because we may be
2931 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 want_garbage_collect = TRUE;
2933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002934 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002935 garbage_collect_at_exit = TRUE;
2936}
2937
2938/*
2939 * "get()" function
2940 */
2941 static void
2942f_get(typval_T *argvars, typval_T *rettv)
2943{
2944 listitem_T *li;
2945 list_T *l;
2946 dictitem_T *di;
2947 dict_T *d;
2948 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002949 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002951 if (argvars[0].v_type == VAR_BLOB)
2952 {
2953 int error = FALSE;
2954 int idx = tv_get_number_chk(&argvars[1], &error);
2955
2956 if (!error)
2957 {
2958 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002959 if (idx < 0)
2960 idx = blob_len(argvars[0].vval.v_blob) + idx;
2961 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2962 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002963 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002964 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002965 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002966 tv = rettv;
2967 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002968 }
2969 }
2970 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 {
2972 if ((l = argvars[0].vval.v_list) != NULL)
2973 {
2974 int error = FALSE;
2975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977 if (!error && li != NULL)
2978 tv = &li->li_tv;
2979 }
2980 }
2981 else if (argvars[0].v_type == VAR_DICT)
2982 {
2983 if ((d = argvars[0].vval.v_dict) != NULL)
2984 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002985 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 if (di != NULL)
2987 tv = &di->di_tv;
2988 }
2989 }
2990 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2991 {
2992 partial_T *pt;
2993 partial_T fref_pt;
2994
2995 if (argvars[0].v_type == VAR_PARTIAL)
2996 pt = argvars[0].vval.v_partial;
2997 else
2998 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002999 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000 fref_pt.pt_name = argvars[0].vval.v_string;
3001 pt = &fref_pt;
3002 }
3003
3004 if (pt != NULL)
3005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003006 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003007 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003008
3009 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3010 {
3011 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003012 n = partial_name(pt);
3013 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 rettv->vval.v_string = NULL;
3015 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003016 {
3017 rettv->vval.v_string = vim_strsave(n);
3018 if (rettv->v_type == VAR_FUNC)
3019 func_ref(rettv->vval.v_string);
3020 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 }
3022 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003023 {
3024 what_is_dict = TRUE;
3025 if (pt->pt_dict != NULL)
3026 rettv_dict_set(rettv, pt->pt_dict);
3027 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 else if (STRCMP(what, "args") == 0)
3029 {
3030 rettv->v_type = VAR_LIST;
3031 if (rettv_list_alloc(rettv) == OK)
3032 {
3033 int i;
3034
3035 for (i = 0; i < pt->pt_argc; ++i)
3036 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3037 }
3038 }
3039 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003040 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003041
3042 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3043 // third argument
3044 if (!what_is_dict)
3045 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 }
3047 }
3048 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003049 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050
3051 if (tv == NULL)
3052 {
3053 if (argvars[2].v_type != VAR_UNKNOWN)
3054 copy_tv(&argvars[2], rettv);
3055 }
3056 else
3057 copy_tv(tv, rettv);
3058}
3059
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003060/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003061 * "getchangelist()" function
3062 */
3063 static void
3064f_getchangelist(typval_T *argvars, typval_T *rettv)
3065{
3066#ifdef FEAT_JUMPLIST
3067 buf_T *buf;
3068 int i;
3069 list_T *l;
3070 dict_T *d;
3071#endif
3072
3073 if (rettv_list_alloc(rettv) != OK)
3074 return;
3075
3076#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003077 if (argvars[0].v_type == VAR_UNKNOWN)
3078 buf = curbuf;
3079 else
3080 {
3081 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3082 ++emsg_off;
3083 buf = tv_get_buf(&argvars[0], FALSE);
3084 --emsg_off;
3085 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003086 if (buf == NULL)
3087 return;
3088
3089 l = list_alloc();
3090 if (l == NULL)
3091 return;
3092
3093 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3094 return;
3095 /*
3096 * The current window change list index tracks only the position in the
3097 * current buffer change list. For other buffers, use the change list
3098 * length as the current index.
3099 */
3100 list_append_number(rettv->vval.v_list,
3101 (varnumber_T)((buf == curwin->w_buffer)
3102 ? curwin->w_changelistidx : buf->b_changelistlen));
3103
3104 for (i = 0; i < buf->b_changelistlen; ++i)
3105 {
3106 if (buf->b_changelist[i].lnum == 0)
3107 continue;
3108 if ((d = dict_alloc()) == NULL)
3109 return;
3110 if (list_append_dict(l, d) == FAIL)
3111 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003112 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3113 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003114 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003115 }
3116#endif
3117}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118
3119/*
3120 * "getcharsearch()" function
3121 */
3122 static void
3123f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3124{
3125 if (rettv_dict_alloc(rettv) != FAIL)
3126 {
3127 dict_T *dict = rettv->vval.v_dict;
3128
Bram Moolenaare0be1672018-07-08 16:50:37 +02003129 dict_add_string(dict, "char", last_csearch());
3130 dict_add_number(dict, "forward", last_csearch_forward());
3131 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 }
3133}
3134
3135/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003136 * "getenv()" function
3137 */
3138 static void
3139f_getenv(typval_T *argvars, typval_T *rettv)
3140{
3141 int mustfree = FALSE;
3142 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3143
3144 if (p == NULL)
3145 {
3146 rettv->v_type = VAR_SPECIAL;
3147 rettv->vval.v_number = VVAL_NULL;
3148 return;
3149 }
3150 if (!mustfree)
3151 p = vim_strsave(p);
3152 rettv->vval.v_string = p;
3153 rettv->v_type = VAR_STRING;
3154}
3155
3156/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 * "getfontname()" function
3158 */
3159 static void
3160f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3161{
3162 rettv->v_type = VAR_STRING;
3163 rettv->vval.v_string = NULL;
3164#ifdef FEAT_GUI
3165 if (gui.in_use)
3166 {
3167 GuiFont font;
3168 char_u *name = NULL;
3169
3170 if (argvars[0].v_type == VAR_UNKNOWN)
3171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003172 // Get the "Normal" font. Either the name saved by
3173 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 font = gui.norm_font;
3175 name = hl_get_font_name();
3176 }
3177 else
3178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003179 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003180 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003181 return;
3182 font = gui_mch_get_font(name, FALSE);
3183 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 }
3186 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3187 if (argvars[0].v_type != VAR_UNKNOWN)
3188 gui_mch_free_font(font);
3189 }
3190#endif
3191}
3192
3193/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003194 * "getjumplist()" function
3195 */
3196 static void
3197f_getjumplist(typval_T *argvars, typval_T *rettv)
3198{
3199#ifdef FEAT_JUMPLIST
3200 win_T *wp;
3201 int i;
3202 list_T *l;
3203 dict_T *d;
3204#endif
3205
3206 if (rettv_list_alloc(rettv) != OK)
3207 return;
3208
3209#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003210 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003211 if (wp == NULL)
3212 return;
3213
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003214 cleanup_jumplist(wp, TRUE);
3215
Bram Moolenaar4f505882018-02-10 21:06:32 +01003216 l = list_alloc();
3217 if (l == NULL)
3218 return;
3219
3220 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3221 return;
3222 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3223
3224 for (i = 0; i < wp->w_jumplistlen; ++i)
3225 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003226 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3227 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003228 if ((d = dict_alloc()) == NULL)
3229 return;
3230 if (list_append_dict(l, d) == FAIL)
3231 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003232 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3233 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003234 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003236 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003237 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003238 }
3239#endif
3240}
3241
3242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243 * "getpid()" function
3244 */
3245 static void
3246f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3247{
3248 rettv->vval.v_number = mch_get_pid();
3249}
3250
3251 static void
3252getpos_both(
3253 typval_T *argvars,
3254 typval_T *rettv,
3255 int getcurpos)
3256{
3257 pos_T *fp;
3258 list_T *l;
3259 int fnum = -1;
3260
3261 if (rettv_list_alloc(rettv) == OK)
3262 {
3263 l = rettv->vval.v_list;
3264 if (getcurpos)
3265 fp = &curwin->w_cursor;
3266 else
3267 fp = var2fpos(&argvars[0], TRUE, &fnum);
3268 if (fnum != -1)
3269 list_append_number(l, (varnumber_T)fnum);
3270 else
3271 list_append_number(l, (varnumber_T)0);
3272 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3273 : (varnumber_T)0);
3274 list_append_number(l, (fp != NULL)
3275 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3276 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003277 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 (varnumber_T)0);
3279 if (getcurpos)
3280 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003281 int save_set_curswant = curwin->w_set_curswant;
3282 colnr_T save_curswant = curwin->w_curswant;
3283 colnr_T save_virtcol = curwin->w_virtcol;
3284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 update_curswant();
3286 list_append_number(l, curwin->w_curswant == MAXCOL ?
3287 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003288
3289 // Do not change "curswant", as it is unexpected that a get
3290 // function has a side effect.
3291 if (save_set_curswant)
3292 {
3293 curwin->w_set_curswant = save_set_curswant;
3294 curwin->w_curswant = save_curswant;
3295 curwin->w_virtcol = save_virtcol;
3296 curwin->w_valid &= ~VALID_VIRTCOL;
3297 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 }
3299 }
3300 else
3301 rettv->vval.v_number = FALSE;
3302}
3303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304/*
3305 * "getcurpos()" function
3306 */
3307 static void
3308f_getcurpos(typval_T *argvars, typval_T *rettv)
3309{
3310 getpos_both(argvars, rettv, TRUE);
3311}
3312
3313/*
3314 * "getpos(string)" function
3315 */
3316 static void
3317f_getpos(typval_T *argvars, typval_T *rettv)
3318{
3319 getpos_both(argvars, rettv, FALSE);
3320}
3321
3322/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 * "getreg()" function
3324 */
3325 static void
3326f_getreg(typval_T *argvars, typval_T *rettv)
3327{
3328 char_u *strregname;
3329 int regname;
3330 int arg2 = FALSE;
3331 int return_list = FALSE;
3332 int error = FALSE;
3333
3334 if (argvars[0].v_type != VAR_UNKNOWN)
3335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 error = strregname == NULL;
3338 if (argvars[1].v_type != VAR_UNKNOWN)
3339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003340 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003342 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 }
3344 }
3345 else
3346 strregname = get_vim_var_str(VV_REG);
3347
3348 if (error)
3349 return;
3350
3351 regname = (strregname == NULL ? '"' : *strregname);
3352 if (regname == 0)
3353 regname = '"';
3354
3355 if (return_list)
3356 {
3357 rettv->v_type = VAR_LIST;
3358 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3359 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3360 if (rettv->vval.v_list == NULL)
3361 (void)rettv_list_alloc(rettv);
3362 else
3363 ++rettv->vval.v_list->lv_refcount;
3364 }
3365 else
3366 {
3367 rettv->v_type = VAR_STRING;
3368 rettv->vval.v_string = get_reg_contents(regname,
3369 arg2 ? GREG_EXPR_SRC : 0);
3370 }
3371}
3372
3373/*
3374 * "getregtype()" function
3375 */
3376 static void
3377f_getregtype(typval_T *argvars, typval_T *rettv)
3378{
3379 char_u *strregname;
3380 int regname;
3381 char_u buf[NUMBUFLEN + 2];
3382 long reglen = 0;
3383
3384 if (argvars[0].v_type != VAR_UNKNOWN)
3385 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003386 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003387 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003388 {
3389 rettv->v_type = VAR_STRING;
3390 rettv->vval.v_string = NULL;
3391 return;
3392 }
3393 }
3394 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003396 strregname = get_vim_var_str(VV_REG);
3397
3398 regname = (strregname == NULL ? '"' : *strregname);
3399 if (regname == 0)
3400 regname = '"';
3401
3402 buf[0] = NUL;
3403 buf[1] = NUL;
3404 switch (get_reg_type(regname, &reglen))
3405 {
3406 case MLINE: buf[0] = 'V'; break;
3407 case MCHAR: buf[0] = 'v'; break;
3408 case MBLOCK:
3409 buf[0] = Ctrl_V;
3410 sprintf((char *)buf + 1, "%ld", reglen + 1);
3411 break;
3412 }
3413 rettv->v_type = VAR_STRING;
3414 rettv->vval.v_string = vim_strsave(buf);
3415}
3416
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003417/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003418 * "gettagstack()" function
3419 */
3420 static void
3421f_gettagstack(typval_T *argvars, typval_T *rettv)
3422{
3423 win_T *wp = curwin; // default is current window
3424
3425 if (rettv_dict_alloc(rettv) != OK)
3426 return;
3427
3428 if (argvars[0].v_type != VAR_UNKNOWN)
3429 {
3430 wp = find_win_by_nr_or_id(&argvars[0]);
3431 if (wp == NULL)
3432 return;
3433 }
3434
3435 get_tagstack(wp, rettv->vval.v_dict);
3436}
3437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439#include "version.h"
3440
3441/*
3442 * "has()" function
3443 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003444 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445f_has(typval_T *argvars, typval_T *rettv)
3446{
3447 int i;
3448 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003449 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003451 typedef struct {
3452 char *name;
3453 short present;
3454 } has_item_T;
3455 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003457 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003459 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003460#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003461 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003462#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003463 },
3464 {"arp",
3465#if defined(AMIGA) && defined(FEAT_ARP)
3466 1
3467#else
3468 0
3469#endif
3470 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003471 {"haiku",
3472#ifdef __HAIKU__
3473 1
3474#else
3475 0
3476#endif
3477 },
3478 {"bsd",
3479#if defined(BSD) && !defined(MACOS_X)
3480 1
3481#else
3482 0
3483#endif
3484 },
3485 {"hpux",
3486#ifdef hpux
3487 1
3488#else
3489 0
3490#endif
3491 },
3492 {"linux",
3493#ifdef __linux__
3494 1
3495#else
3496 0
3497#endif
3498 },
3499 {"mac", // Mac OS X (and, once, Mac OS Classic)
3500#ifdef MACOS_X
3501 1
3502#else
3503 0
3504#endif
3505 },
3506 {"osx", // Mac OS X
3507#ifdef MACOS_X
3508 1
3509#else
3510 0
3511#endif
3512 },
3513 {"macunix", // Mac OS X, with the darwin feature
3514#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3515 1
3516#else
3517 0
3518#endif
3519 },
3520 {"osxdarwin", // synonym for macunix
3521#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3522 1
3523#else
3524 0
3525#endif
3526 },
3527 {"qnx",
3528#ifdef __QNX__
3529 1
3530#else
3531 0
3532#endif
3533 },
3534 {"sun",
3535#ifdef SUN_SYSTEM
3536 1
3537#else
3538 0
3539#endif
3540 },
3541 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003543 1
3544#else
3545 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003547 },
3548 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003550 1
3551#else
3552 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003554 },
3555 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003556#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003557 1
3558#else
3559 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003560#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003561 },
3562 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003563#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003564 1
3565#else
3566 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003568 },
3569 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003570#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003571 1
3572#else
3573 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003575 },
3576 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003578 1
3579#else
3580 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003582 },
3583 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003585 1
3586#else
3587 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003589 },
3590 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003592 1
3593#else
3594 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003596 },
3597 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 1
3600#else
3601 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003603 },
3604 {"autocmd", 1},
3605 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003606#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003607 1
3608#else
3609 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003610#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003611 },
3612 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003613#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003614 1
3615#else
3616 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003617#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003618 },
3619 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003620#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003621 1
3622#else
3623 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003625 },
3626 {"balloon_multiline",
3627#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3628 // MS-Windows requires runtime check, see below
3629 1
3630#else
3631 0
3632#endif
3633 },
3634 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003635#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003636 1
3637#else
3638 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003639#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003640 },
3641 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003643 1
3644#else
3645 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003647 },
3648 {"all_builtin_terms",
3649#if defined(ALL_BUILTIN_TCAPS)
3650 1
3651#else
3652 0
3653#endif
3654 },
3655 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003657 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003659 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003661 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003663 },
3664 {"byte_offset",
3665#ifdef FEAT_BYTEOFF
3666 1
3667#else
3668 0
3669#endif
3670 },
3671 {"channel",
3672#ifdef FEAT_JOB_CHANNEL
3673 1
3674#else
3675 0
3676#endif
3677 },
3678 {"cindent",
3679#ifdef FEAT_CINDENT
3680 1
3681#else
3682 0
3683#endif
3684 },
3685 {"clientserver",
3686#ifdef FEAT_CLIENTSERVER
3687 1
3688#else
3689 0
3690#endif
3691 },
3692 {"clipboard",
3693#ifdef FEAT_CLIPBOARD
3694 1
3695#else
3696 0
3697#endif
3698 },
3699 {"cmdline_compl", 1},
3700 {"cmdline_hist", 1},
3701 {"comments", 1},
3702 {"conceal",
3703#ifdef FEAT_CONCEAL
3704 1
3705#else
3706 0
3707#endif
3708 },
3709 {"cryptv",
3710#ifdef FEAT_CRYPT
3711 1
3712#else
3713 0
3714#endif
3715 },
3716 {"crypt-blowfish",
3717#ifdef FEAT_CRYPT
3718 1
3719#else
3720 0
3721#endif
3722 },
3723 {"crypt-blowfish2",
3724#ifdef FEAT_CRYPT
3725 1
3726#else
3727 0
3728#endif
3729 },
3730 {"cscope",
3731#ifdef FEAT_CSCOPE
3732 1
3733#else
3734 0
3735#endif
3736 },
3737 {"cursorbind", 1},
3738 {"cursorshape",
3739#ifdef CURSOR_SHAPE
3740 1
3741#else
3742 0
3743#endif
3744 },
3745 {"debug",
3746#ifdef DEBUG
3747 1
3748#else
3749 0
3750#endif
3751 },
3752 {"dialog_con",
3753#ifdef FEAT_CON_DIALOG
3754 1
3755#else
3756 0
3757#endif
3758 },
3759 {"dialog_gui",
3760#ifdef FEAT_GUI_DIALOG
3761 1
3762#else
3763 0
3764#endif
3765 },
3766 {"diff",
3767#ifdef FEAT_DIFF
3768 1
3769#else
3770 0
3771#endif
3772 },
3773 {"digraphs",
3774#ifdef FEAT_DIGRAPHS
3775 1
3776#else
3777 0
3778#endif
3779 },
3780 {"directx",
3781#ifdef FEAT_DIRECTX
3782 1
3783#else
3784 0
3785#endif
3786 },
3787 {"dnd",
3788#ifdef FEAT_DND
3789 1
3790#else
3791 0
3792#endif
3793 },
3794 {"emacs_tags",
3795#ifdef FEAT_EMACS_TAGS
3796 1
3797#else
3798 0
3799#endif
3800 },
3801 {"eval", 1}, // always present, of course!
3802 {"ex_extra", 1}, // graduated feature
3803 {"extra_search",
3804#ifdef FEAT_SEARCH_EXTRA
3805 1
3806#else
3807 0
3808#endif
3809 },
3810 {"file_in_path",
3811#ifdef FEAT_SEARCHPATH
3812 1
3813#else
3814 0
3815#endif
3816 },
3817 {"filterpipe",
3818#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3819 1
3820#else
3821 0
3822#endif
3823 },
3824 {"find_in_path",
3825#ifdef FEAT_FIND_ID
3826 1
3827#else
3828 0
3829#endif
3830 },
3831 {"float",
3832#ifdef FEAT_FLOAT
3833 1
3834#else
3835 0
3836#endif
3837 },
3838 {"folding",
3839#ifdef FEAT_FOLDING
3840 1
3841#else
3842 0
3843#endif
3844 },
3845 {"footer",
3846#ifdef FEAT_FOOTER
3847 1
3848#else
3849 0
3850#endif
3851 },
3852 {"fork",
3853#if !defined(USE_SYSTEM) && defined(UNIX)
3854 1
3855#else
3856 0
3857#endif
3858 },
3859 {"gettext",
3860#ifdef FEAT_GETTEXT
3861 1
3862#else
3863 0
3864#endif
3865 },
3866 {"gui",
3867#ifdef FEAT_GUI
3868 1
3869#else
3870 0
3871#endif
3872 },
3873 {"gui_neXtaw",
3874#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3875 1
3876#else
3877 0
3878#endif
3879 },
3880 {"gui_athena",
3881#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3882 1
3883#else
3884 0
3885#endif
3886 },
3887 {"gui_gtk",
3888#ifdef FEAT_GUI_GTK
3889 1
3890#else
3891 0
3892#endif
3893 },
3894 {"gui_gtk2",
3895#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3896 1
3897#else
3898 0
3899#endif
3900 },
3901 {"gui_gtk3",
3902#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3903 1
3904#else
3905 0
3906#endif
3907 },
3908 {"gui_gnome",
3909#ifdef FEAT_GUI_GNOME
3910 1
3911#else
3912 0
3913#endif
3914 },
3915 {"gui_haiku",
3916#ifdef FEAT_GUI_HAIKU
3917 1
3918#else
3919 0
3920#endif
3921 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003922 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003923 {"gui_motif",
3924#ifdef FEAT_GUI_MOTIF
3925 1
3926#else
3927 0
3928#endif
3929 },
3930 {"gui_photon",
3931#ifdef FEAT_GUI_PHOTON
3932 1
3933#else
3934 0
3935#endif
3936 },
3937 {"gui_win32",
3938#ifdef FEAT_GUI_MSWIN
3939 1
3940#else
3941 0
3942#endif
3943 },
3944 {"iconv",
3945#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3946 1
3947#else
3948 0
3949#endif
3950 },
3951 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003952 {"ipv6",
3953#ifdef FEAT_IPV6
3954 1
3955#else
3956 0
3957#endif
3958 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003959 {"job",
3960#ifdef FEAT_JOB_CHANNEL
3961 1
3962#else
3963 0
3964#endif
3965 },
3966 {"jumplist",
3967#ifdef FEAT_JUMPLIST
3968 1
3969#else
3970 0
3971#endif
3972 },
3973 {"keymap",
3974#ifdef FEAT_KEYMAP
3975 1
3976#else
3977 0
3978#endif
3979 },
3980 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3981 {"langmap",
3982#ifdef FEAT_LANGMAP
3983 1
3984#else
3985 0
3986#endif
3987 },
3988 {"libcall",
3989#ifdef FEAT_LIBCALL
3990 1
3991#else
3992 0
3993#endif
3994 },
3995 {"linebreak",
3996#ifdef FEAT_LINEBREAK
3997 1
3998#else
3999 0
4000#endif
4001 },
4002 {"lispindent",
4003#ifdef FEAT_LISP
4004 1
4005#else
4006 0
4007#endif
4008 },
4009 {"listcmds", 1},
4010 {"localmap", 1},
4011 {"lua",
4012#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4013 1
4014#else
4015 0
4016#endif
4017 },
4018 {"menu",
4019#ifdef FEAT_MENU
4020 1
4021#else
4022 0
4023#endif
4024 },
4025 {"mksession",
4026#ifdef FEAT_SESSION
4027 1
4028#else
4029 0
4030#endif
4031 },
4032 {"modify_fname", 1},
4033 {"mouse", 1},
4034 {"mouseshape",
4035#ifdef FEAT_MOUSESHAPE
4036 1
4037#else
4038 0
4039#endif
4040 },
4041 {"mouse_dec",
4042#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4043 1
4044#else
4045 0
4046#endif
4047 },
4048 {"mouse_gpm",
4049#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4050 1
4051#else
4052 0
4053#endif
4054 },
4055 {"mouse_jsbterm",
4056#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4057 1
4058#else
4059 0
4060#endif
4061 },
4062 {"mouse_netterm",
4063#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4064 1
4065#else
4066 0
4067#endif
4068 },
4069 {"mouse_pterm",
4070#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4071 1
4072#else
4073 0
4074#endif
4075 },
4076 {"mouse_sgr",
4077#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4078 1
4079#else
4080 0
4081#endif
4082 },
4083 {"mouse_sysmouse",
4084#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4085 1
4086#else
4087 0
4088#endif
4089 },
4090 {"mouse_urxvt",
4091#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4092 1
4093#else
4094 0
4095#endif
4096 },
4097 {"mouse_xterm",
4098#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4099 1
4100#else
4101 0
4102#endif
4103 },
4104 {"multi_byte", 1},
4105 {"multi_byte_ime",
4106#ifdef FEAT_MBYTE_IME
4107 1
4108#else
4109 0
4110#endif
4111 },
4112 {"multi_lang",
4113#ifdef FEAT_MULTI_LANG
4114 1
4115#else
4116 0
4117#endif
4118 },
4119 {"mzscheme",
4120#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4121 1
4122#else
4123 0
4124#endif
4125 },
4126 {"num64", 1},
4127 {"ole",
4128#ifdef FEAT_OLE
4129 1
4130#else
4131 0
4132#endif
4133 },
4134 {"packages",
4135#ifdef FEAT_EVAL
4136 1
4137#else
4138 0
4139#endif
4140 },
4141 {"path_extra",
4142#ifdef FEAT_PATH_EXTRA
4143 1
4144#else
4145 0
4146#endif
4147 },
4148 {"perl",
4149#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4150 1
4151#else
4152 0
4153#endif
4154 },
4155 {"persistent_undo",
4156#ifdef FEAT_PERSISTENT_UNDO
4157 1
4158#else
4159 0
4160#endif
4161 },
4162 {"python_compiled",
4163#if defined(FEAT_PYTHON)
4164 1
4165#else
4166 0
4167#endif
4168 },
4169 {"python_dynamic",
4170#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4171 1
4172#else
4173 0
4174#endif
4175 },
4176 {"python",
4177#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4178 1
4179#else
4180 0
4181#endif
4182 },
4183 {"pythonx",
4184#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4185 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4186 1
4187#else
4188 0
4189#endif
4190 },
4191 {"python3_compiled",
4192#if defined(FEAT_PYTHON3)
4193 1
4194#else
4195 0
4196#endif
4197 },
4198 {"python3_dynamic",
4199#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4200 1
4201#else
4202 0
4203#endif
4204 },
4205 {"python3",
4206#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4207 1
4208#else
4209 0
4210#endif
4211 },
4212 {"popupwin",
4213#ifdef FEAT_PROP_POPUP
4214 1
4215#else
4216 0
4217#endif
4218 },
4219 {"postscript",
4220#ifdef FEAT_POSTSCRIPT
4221 1
4222#else
4223 0
4224#endif
4225 },
4226 {"printer",
4227#ifdef FEAT_PRINTER
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"profile",
4234#ifdef FEAT_PROFILE
4235 1
4236#else
4237 0
4238#endif
4239 },
4240 {"reltime",
4241#ifdef FEAT_RELTIME
4242 1
4243#else
4244 0
4245#endif
4246 },
4247 {"quickfix",
4248#ifdef FEAT_QUICKFIX
4249 1
4250#else
4251 0
4252#endif
4253 },
4254 {"rightleft",
4255#ifdef FEAT_RIGHTLEFT
4256 1
4257#else
4258 0
4259#endif
4260 },
4261 {"ruby",
4262#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4263 1
4264#else
4265 0
4266#endif
4267 },
4268 {"scrollbind", 1},
4269 {"showcmd",
4270#ifdef FEAT_CMDL_INFO
4271 1
4272#else
4273 0
4274#endif
4275 },
4276 {"cmdline_info",
4277#ifdef FEAT_CMDL_INFO
4278 1
4279#else
4280 0
4281#endif
4282 },
4283 {"signs",
4284#ifdef FEAT_SIGNS
4285 1
4286#else
4287 0
4288#endif
4289 },
4290 {"smartindent",
4291#ifdef FEAT_SMARTINDENT
4292 1
4293#else
4294 0
4295#endif
4296 },
4297 {"startuptime",
4298#ifdef STARTUPTIME
4299 1
4300#else
4301 0
4302#endif
4303 },
4304 {"statusline",
4305#ifdef FEAT_STL_OPT
4306 1
4307#else
4308 0
4309#endif
4310 },
4311 {"netbeans_intg",
4312#ifdef FEAT_NETBEANS_INTG
4313 1
4314#else
4315 0
4316#endif
4317 },
4318 {"sound",
4319#ifdef FEAT_SOUND
4320 1
4321#else
4322 0
4323#endif
4324 },
4325 {"spell",
4326#ifdef FEAT_SPELL
4327 1
4328#else
4329 0
4330#endif
4331 },
4332 {"syntax",
4333#ifdef FEAT_SYN_HL
4334 1
4335#else
4336 0
4337#endif
4338 },
4339 {"system",
4340#if defined(USE_SYSTEM) || !defined(UNIX)
4341 1
4342#else
4343 0
4344#endif
4345 },
4346 {"tag_binary",
4347#ifdef FEAT_TAG_BINS
4348 1
4349#else
4350 0
4351#endif
4352 },
4353 {"tcl",
4354#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4355 1
4356#else
4357 0
4358#endif
4359 },
4360 {"termguicolors",
4361#ifdef FEAT_TERMGUICOLORS
4362 1
4363#else
4364 0
4365#endif
4366 },
4367 {"terminal",
4368#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4369 1
4370#else
4371 0
4372#endif
4373 },
4374 {"terminfo",
4375#ifdef TERMINFO
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"termresponse",
4382#ifdef FEAT_TERMRESPONSE
4383 1
4384#else
4385 0
4386#endif
4387 },
4388 {"textobjects",
4389#ifdef FEAT_TEXTOBJ
4390 1
4391#else
4392 0
4393#endif
4394 },
4395 {"textprop",
4396#ifdef FEAT_PROP_POPUP
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"tgetent",
4403#ifdef HAVE_TGETENT
4404 1
4405#else
4406 0
4407#endif
4408 },
4409 {"timers",
4410#ifdef FEAT_TIMERS
4411 1
4412#else
4413 0
4414#endif
4415 },
4416 {"title",
4417#ifdef FEAT_TITLE
4418 1
4419#else
4420 0
4421#endif
4422 },
4423 {"toolbar",
4424#ifdef FEAT_TOOLBAR
4425 1
4426#else
4427 0
4428#endif
4429 },
4430 {"unnamedplus",
4431#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4432 1
4433#else
4434 0
4435#endif
4436 },
4437 {"user-commands", 1}, // was accidentally included in 5.4
4438 {"user_commands", 1},
4439 {"vartabs",
4440#ifdef FEAT_VARTABS
4441 1
4442#else
4443 0
4444#endif
4445 },
4446 {"vertsplit", 1},
4447 {"viminfo",
4448#ifdef FEAT_VIMINFO
4449 1
4450#else
4451 0
4452#endif
4453 },
4454 {"vimscript-1", 1},
4455 {"vimscript-2", 1},
4456 {"vimscript-3", 1},
4457 {"vimscript-4", 1},
4458 {"virtualedit", 1},
4459 {"visual", 1},
4460 {"visualextra", 1},
4461 {"vreplace", 1},
4462 {"vtp",
4463#ifdef FEAT_VTP
4464 1
4465#else
4466 0
4467#endif
4468 },
4469 {"wildignore",
4470#ifdef FEAT_WILDIGN
4471 1
4472#else
4473 0
4474#endif
4475 },
4476 {"wildmenu",
4477#ifdef FEAT_WILDMENU
4478 1
4479#else
4480 0
4481#endif
4482 },
4483 {"windows", 1},
4484 {"winaltkeys",
4485#ifdef FEAT_WAK
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"writebackup",
4492#ifdef FEAT_WRITEBACKUP
4493 1
4494#else
4495 0
4496#endif
4497 },
4498 {"xim",
4499#ifdef FEAT_XIM
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"xfontset",
4506#ifdef FEAT_XFONTSET
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"xpm",
4513#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4514 1
4515#else
4516 0
4517#endif
4518 },
4519 {"xpm_w32", // for backward compatibility
4520#ifdef FEAT_XPM_W32
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004528 1
4529#else
4530 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004532 },
4533 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004535 1
4536#else
4537 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004539 },
4540 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004542 1
4543#else
4544 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004546 },
4547 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004549 1
4550#else
4551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004553 },
4554 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 1
4557#else
4558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 },
4561 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562 };
4563
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004564 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004565 for (i = 0; has_list[i].name != NULL; ++i)
4566 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004567 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004568 x = TRUE;
4569 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570 break;
4571 }
4572
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 // features also in has_list[] but sometimes enabled at runtime
4574 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004575 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004576 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004577 {
4578 // intentionally empty
4579 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004580#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581 else if (STRICMP(name, "balloon_multiline") == 0)
4582 n = multiline_balloon_available();
4583#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004584#ifdef VIMDLL
4585 else if (STRICMP(name, "filterpipe") == 0)
4586 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587#endif
4588#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4589 else if (STRICMP(name, "iconv") == 0)
4590 n = iconv_enabled(FALSE);
4591#endif
4592#ifdef DYNAMIC_LUA
4593 else if (STRICMP(name, "lua") == 0)
4594 n = lua_enabled(FALSE);
4595#endif
4596#ifdef DYNAMIC_MZSCHEME
4597 else if (STRICMP(name, "mzscheme") == 0)
4598 n = mzscheme_enabled(FALSE);
4599#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004600#ifdef DYNAMIC_PERL
4601 else if (STRICMP(name, "perl") == 0)
4602 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604#ifdef DYNAMIC_PYTHON
4605 else if (STRICMP(name, "python") == 0)
4606 n = python_enabled(FALSE);
4607#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608#ifdef DYNAMIC_PYTHON3
4609 else if (STRICMP(name, "python3") == 0)
4610 n = python3_enabled(FALSE);
4611#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004612#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4613 else if (STRICMP(name, "pythonx") == 0)
4614 {
4615# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4616 if (p_pyx == 0)
4617 n = python3_enabled(FALSE) || python_enabled(FALSE);
4618 else if (p_pyx == 3)
4619 n = python3_enabled(FALSE);
4620 else if (p_pyx == 2)
4621 n = python_enabled(FALSE);
4622# elif defined(DYNAMIC_PYTHON)
4623 n = python_enabled(FALSE);
4624# elif defined(DYNAMIC_PYTHON3)
4625 n = python3_enabled(FALSE);
4626# endif
4627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004629#ifdef DYNAMIC_RUBY
4630 else if (STRICMP(name, "ruby") == 0)
4631 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004633#ifdef DYNAMIC_TCL
4634 else if (STRICMP(name, "tcl") == 0)
4635 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004636#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004637#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004638 else if (STRICMP(name, "terminal") == 0)
4639 n = terminal_enabled();
4640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004641 }
4642
Bram Moolenaar79296512020-03-22 16:17:14 +01004643 // features not in has_list[]
4644 if (x == FALSE)
4645 {
4646 if (STRNICMP(name, "patch", 5) == 0)
4647 {
4648 x = TRUE;
4649 if (name[5] == '-'
4650 && STRLEN(name) >= 11
4651 && vim_isdigit(name[6])
4652 && vim_isdigit(name[8])
4653 && vim_isdigit(name[10]))
4654 {
4655 int major = atoi((char *)name + 6);
4656 int minor = atoi((char *)name + 8);
4657
4658 // Expect "patch-9.9.01234".
4659 n = (major < VIM_VERSION_MAJOR
4660 || (major == VIM_VERSION_MAJOR
4661 && (minor < VIM_VERSION_MINOR
4662 || (minor == VIM_VERSION_MINOR
4663 && has_patch(atoi((char *)name + 10))))));
4664 }
4665 else
4666 n = has_patch(atoi((char *)name + 5));
4667 }
4668 else if (STRICMP(name, "vim_starting") == 0)
4669 {
4670 x = TRUE;
4671 n = (starting != 0);
4672 }
4673 else if (STRICMP(name, "ttyin") == 0)
4674 {
4675 x = TRUE;
4676 n = mch_input_isatty();
4677 }
4678 else if (STRICMP(name, "ttyout") == 0)
4679 {
4680 x = TRUE;
4681 n = stdout_isatty;
4682 }
4683 else if (STRICMP(name, "multi_byte_encoding") == 0)
4684 {
4685 x = TRUE;
4686 n = has_mbyte;
4687 }
4688 else if (STRICMP(name, "gui_running") == 0)
4689 {
4690 x = TRUE;
4691#ifdef FEAT_GUI
4692 n = (gui.in_use || gui.starting);
4693#endif
4694 }
4695 else if (STRICMP(name, "browse") == 0)
4696 {
4697 x = TRUE;
4698#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4699 n = gui.in_use; // gui_mch_browse() works when GUI is running
4700#endif
4701 }
4702 else if (STRICMP(name, "syntax_items") == 0)
4703 {
4704 x = TRUE;
4705#ifdef FEAT_SYN_HL
4706 n = syntax_present(curwin);
4707#endif
4708 }
4709 else if (STRICMP(name, "vcon") == 0)
4710 {
4711 x = TRUE;
4712#ifdef FEAT_VTP
4713 n = is_term_win32() && has_vtp_working();
4714#endif
4715 }
4716 else if (STRICMP(name, "netbeans_enabled") == 0)
4717 {
4718 x = TRUE;
4719#ifdef FEAT_NETBEANS_INTG
4720 n = netbeans_active();
4721#endif
4722 }
4723 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4724 {
4725 x = TRUE;
4726#ifdef FEAT_MOUSE_GPM
4727 n = gpm_enabled();
4728#endif
4729 }
4730 else if (STRICMP(name, "conpty") == 0)
4731 {
4732 x = TRUE;
4733#if defined(FEAT_TERMINAL) && defined(MSWIN)
4734 n = use_conpty();
4735#endif
4736 }
4737 else if (STRICMP(name, "clipboard_working") == 0)
4738 {
4739 x = TRUE;
4740#ifdef FEAT_CLIPBOARD
4741 n = clip_star.available;
4742#endif
4743 }
4744 }
4745
4746 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4747 // return whether feature could ever be enabled
4748 rettv->vval.v_number = x;
4749 else
4750 // return whether feature is enabled
4751 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004752}
4753
4754/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004755 * "haslocaldir()" function
4756 */
4757 static void
4758f_haslocaldir(typval_T *argvars, typval_T *rettv)
4759{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004760 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 win_T *wp = NULL;
4762
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004763 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4764
4765 // Check for window-local and tab-local directories
4766 if (wp != NULL && wp->w_localdir != NULL)
4767 rettv->vval.v_number = 1;
4768 else if (tp != NULL && tp->tp_localdir != NULL)
4769 rettv->vval.v_number = 2;
4770 else
4771 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004772}
4773
4774/*
4775 * "hasmapto()" function
4776 */
4777 static void
4778f_hasmapto(typval_T *argvars, typval_T *rettv)
4779{
4780 char_u *name;
4781 char_u *mode;
4782 char_u buf[NUMBUFLEN];
4783 int abbr = FALSE;
4784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004786 if (argvars[1].v_type == VAR_UNKNOWN)
4787 mode = (char_u *)"nvo";
4788 else
4789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004790 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 }
4794
4795 if (map_to_exists(name, mode, abbr))
4796 rettv->vval.v_number = TRUE;
4797 else
4798 rettv->vval.v_number = FALSE;
4799}
4800
4801/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004802 * "highlightID(name)" function
4803 */
4804 static void
4805f_hlID(typval_T *argvars, typval_T *rettv)
4806{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004807 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004808}
4809
4810/*
4811 * "highlight_exists()" function
4812 */
4813 static void
4814f_hlexists(typval_T *argvars, typval_T *rettv)
4815{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817}
4818
4819/*
4820 * "hostname()" function
4821 */
4822 static void
4823f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4824{
4825 char_u hostname[256];
4826
4827 mch_get_host_name(hostname, 256);
4828 rettv->v_type = VAR_STRING;
4829 rettv->vval.v_string = vim_strsave(hostname);
4830}
4831
4832/*
4833 * iconv() function
4834 */
4835 static void
4836f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4837{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838 char_u buf1[NUMBUFLEN];
4839 char_u buf2[NUMBUFLEN];
4840 char_u *from, *to, *str;
4841 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004842
4843 rettv->v_type = VAR_STRING;
4844 rettv->vval.v_string = NULL;
4845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 str = tv_get_string(&argvars[0]);
4847 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4848 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 vimconv.vc_type = CONV_NONE;
4850 convert_setup(&vimconv, from, to);
4851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004852 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004853 if (vimconv.vc_type == CONV_NONE)
4854 rettv->vval.v_string = vim_strsave(str);
4855 else
4856 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4857
4858 convert_setup(&vimconv, NULL, NULL);
4859 vim_free(from);
4860 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004861}
4862
4863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004864 * "index()" function
4865 */
4866 static void
4867f_index(typval_T *argvars, typval_T *rettv)
4868{
4869 list_T *l;
4870 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004871 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 long idx = 0;
4873 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004874 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875
4876 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004877 if (argvars[0].v_type == VAR_BLOB)
4878 {
4879 typval_T tv;
4880 int start = 0;
4881
4882 if (argvars[2].v_type != VAR_UNKNOWN)
4883 {
4884 start = tv_get_number_chk(&argvars[2], &error);
4885 if (error)
4886 return;
4887 }
4888 b = argvars[0].vval.v_blob;
4889 if (b == NULL)
4890 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004891 if (start < 0)
4892 {
4893 start = blob_len(b) + start;
4894 if (start < 0)
4895 start = 0;
4896 }
4897
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004898 for (idx = start; idx < blob_len(b); ++idx)
4899 {
4900 tv.v_type = VAR_NUMBER;
4901 tv.vval.v_number = blob_get(b, idx);
4902 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4903 {
4904 rettv->vval.v_number = idx;
4905 return;
4906 }
4907 }
4908 return;
4909 }
4910 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004912 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 return;
4914 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004915
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004916 l = argvars[0].vval.v_list;
4917 if (l != NULL)
4918 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004919 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004920 item = l->lv_first;
4921 if (argvars[2].v_type != VAR_UNKNOWN)
4922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 // Start at specified item. Use the cached index that list_find()
4924 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004925 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004926 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004928 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929 if (error)
4930 item = NULL;
4931 }
4932
4933 for ( ; item != NULL; item = item->li_next, ++idx)
4934 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4935 {
4936 rettv->vval.v_number = idx;
4937 break;
4938 }
4939 }
4940}
4941
4942static int inputsecret_flag = 0;
4943
4944/*
4945 * "input()" function
4946 * Also handles inputsecret() when inputsecret is set.
4947 */
4948 static void
4949f_input(typval_T *argvars, typval_T *rettv)
4950{
4951 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4952}
4953
4954/*
4955 * "inputdialog()" function
4956 */
4957 static void
4958f_inputdialog(typval_T *argvars, typval_T *rettv)
4959{
4960#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004961 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4963 {
4964 char_u *message;
4965 char_u buf[NUMBUFLEN];
4966 char_u *defstr = (char_u *)"";
4967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004968 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004970 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4972 else
4973 IObuff[0] = NUL;
4974 if (message != NULL && defstr != NULL
4975 && do_dialog(VIM_QUESTION, NULL, message,
4976 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4977 rettv->vval.v_string = vim_strsave(IObuff);
4978 else
4979 {
4980 if (message != NULL && defstr != NULL
4981 && argvars[1].v_type != VAR_UNKNOWN
4982 && argvars[2].v_type != VAR_UNKNOWN)
4983 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004984 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004985 else
4986 rettv->vval.v_string = NULL;
4987 }
4988 rettv->v_type = VAR_STRING;
4989 }
4990 else
4991#endif
4992 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4993}
4994
4995/*
4996 * "inputlist()" function
4997 */
4998 static void
4999f_inputlist(typval_T *argvars, typval_T *rettv)
5000{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005001 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002 listitem_T *li;
5003 int selected;
5004 int mouse_used;
5005
5006#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // While starting up, there is no place to enter text. When running tests
5008 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005009 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010 return;
5011#endif
5012 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5013 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005014 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 return;
5016 }
5017
5018 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 msg_row = Rows - 1; // for when 'cmdheight' > 1
5020 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005021 msg_scroll = TRUE;
5022 msg_clr_eos();
5023
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005024 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005025 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005026 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005028 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029 msg_putchar('\n');
5030 }
5031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 selected = prompt_for_number(&mouse_used);
5034 if (mouse_used)
5035 selected -= lines_left;
5036
5037 rettv->vval.v_number = selected;
5038}
5039
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5041
5042/*
5043 * "inputrestore()" function
5044 */
5045 static void
5046f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5047{
5048 if (ga_userinput.ga_len > 0)
5049 {
5050 --ga_userinput.ga_len;
5051 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5052 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005053 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005054 }
5055 else if (p_verbose > 1)
5056 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005057 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005058 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005059 }
5060}
5061
5062/*
5063 * "inputsave()" function
5064 */
5065 static void
5066f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5067{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005068 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069 if (ga_grow(&ga_userinput, 1) == OK)
5070 {
5071 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5072 + ga_userinput.ga_len);
5073 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005074 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 }
5076 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005077 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005078}
5079
5080/*
5081 * "inputsecret()" function
5082 */
5083 static void
5084f_inputsecret(typval_T *argvars, typval_T *rettv)
5085{
5086 ++cmdline_star;
5087 ++inputsecret_flag;
5088 f_input(argvars, rettv);
5089 --cmdline_star;
5090 --inputsecret_flag;
5091}
5092
5093/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005094 * "interrupt()" function
5095 */
5096 static void
5097f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5098{
5099 got_int = TRUE;
5100}
5101
5102/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 * "invert(expr)" function
5104 */
5105 static void
5106f_invert(typval_T *argvars, typval_T *rettv)
5107{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005108 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109}
5110
5111/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 * "islocked()" function
5113 */
5114 static void
5115f_islocked(typval_T *argvars, typval_T *rettv)
5116{
5117 lval_T lv;
5118 char_u *end;
5119 dictitem_T *di;
5120
5121 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005122 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005123 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 if (end != NULL && lv.ll_name != NULL)
5125 {
5126 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005127 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 else
5129 {
5130 if (lv.ll_tv == NULL)
5131 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005132 di = find_var(lv.ll_name, NULL, TRUE);
5133 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // Consider a variable locked when:
5136 // 1. the variable itself is locked
5137 // 2. the value of the variable is locked.
5138 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005139 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5140 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141 }
5142 }
5143 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005144 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005146 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005147 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005148 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5150 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5153 }
5154 }
5155
5156 clear_lval(&lv);
5157}
5158
5159#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5160/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005161 * "isinf()" function
5162 */
5163 static void
5164f_isinf(typval_T *argvars, typval_T *rettv)
5165{
5166 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5167 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5168}
5169
5170/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 * "isnan()" function
5172 */
5173 static void
5174f_isnan(typval_T *argvars, typval_T *rettv)
5175{
5176 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5177 && isnan(argvars[0].vval.v_float);
5178}
5179#endif
5180
5181/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005182 * "last_buffer_nr()" function.
5183 */
5184 static void
5185f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5186{
5187 int n = 0;
5188 buf_T *buf;
5189
Bram Moolenaar29323592016-07-24 22:04:11 +02005190 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005191 if (n < buf->b_fnum)
5192 n = buf->b_fnum;
5193
5194 rettv->vval.v_number = n;
5195}
5196
5197/*
5198 * "len()" function
5199 */
5200 static void
5201f_len(typval_T *argvars, typval_T *rettv)
5202{
5203 switch (argvars[0].v_type)
5204 {
5205 case VAR_STRING:
5206 case VAR_NUMBER:
5207 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005208 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005209 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005210 case VAR_BLOB:
5211 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5212 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005213 case VAR_LIST:
5214 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5215 break;
5216 case VAR_DICT:
5217 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5218 break;
5219 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005220 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005222 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 case VAR_SPECIAL:
5224 case VAR_FLOAT:
5225 case VAR_FUNC:
5226 case VAR_PARTIAL:
5227 case VAR_JOB:
5228 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005229 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 break;
5231 }
5232}
5233
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005235libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236{
5237#ifdef FEAT_LIBCALL
5238 char_u *string_in;
5239 char_u **string_result;
5240 int nr_result;
5241#endif
5242
5243 rettv->v_type = type;
5244 if (type != VAR_NUMBER)
5245 rettv->vval.v_string = NULL;
5246
5247 if (check_restricted() || check_secure())
5248 return;
5249
5250#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005251 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5253 {
5254 string_in = NULL;
5255 if (argvars[2].v_type == VAR_STRING)
5256 string_in = argvars[2].vval.v_string;
5257 if (type == VAR_NUMBER)
5258 string_result = NULL;
5259 else
5260 string_result = &rettv->vval.v_string;
5261 if (mch_libcall(argvars[0].vval.v_string,
5262 argvars[1].vval.v_string,
5263 string_in,
5264 argvars[2].vval.v_number,
5265 string_result,
5266 &nr_result) == OK
5267 && type == VAR_NUMBER)
5268 rettv->vval.v_number = nr_result;
5269 }
5270#endif
5271}
5272
5273/*
5274 * "libcall()" function
5275 */
5276 static void
5277f_libcall(typval_T *argvars, typval_T *rettv)
5278{
5279 libcall_common(argvars, rettv, VAR_STRING);
5280}
5281
5282/*
5283 * "libcallnr()" function
5284 */
5285 static void
5286f_libcallnr(typval_T *argvars, typval_T *rettv)
5287{
5288 libcall_common(argvars, rettv, VAR_NUMBER);
5289}
5290
5291/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005292 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 */
5294 static void
5295f_line(typval_T *argvars, typval_T *rettv)
5296{
5297 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005298 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005299 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005300 int id;
5301 tabpage_T *tp;
5302 win_T *wp;
5303 win_T *save_curwin;
5304 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005306 if (argvars[1].v_type != VAR_UNKNOWN)
5307 {
5308 // use window specified in the second argument
5309 id = (int)tv_get_number(&argvars[1]);
5310 wp = win_id2wp_tp(id, &tp);
5311 if (wp != NULL && tp != NULL)
5312 {
5313 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5314 == OK)
5315 {
5316 check_cursor();
5317 fp = var2fpos(&argvars[0], TRUE, &fnum);
5318 }
5319 restore_win_noblock(save_curwin, save_curtab, TRUE);
5320 }
5321 }
5322 else
5323 // use current window
5324 fp = var2fpos(&argvars[0], TRUE, &fnum);
5325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326 if (fp != NULL)
5327 lnum = fp->lnum;
5328 rettv->vval.v_number = lnum;
5329}
5330
5331/*
5332 * "line2byte(lnum)" function
5333 */
5334 static void
5335f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5336{
5337#ifndef FEAT_BYTEOFF
5338 rettv->vval.v_number = -1;
5339#else
5340 linenr_T lnum;
5341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005342 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5344 rettv->vval.v_number = -1;
5345 else
5346 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5347 if (rettv->vval.v_number >= 0)
5348 ++rettv->vval.v_number;
5349#endif
5350}
5351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352#ifdef FEAT_FLOAT
5353/*
5354 * "log()" function
5355 */
5356 static void
5357f_log(typval_T *argvars, typval_T *rettv)
5358{
5359 float_T f = 0.0;
5360
5361 rettv->v_type = VAR_FLOAT;
5362 if (get_float_arg(argvars, &f) == OK)
5363 rettv->vval.v_float = log(f);
5364 else
5365 rettv->vval.v_float = 0.0;
5366}
5367
5368/*
5369 * "log10()" function
5370 */
5371 static void
5372f_log10(typval_T *argvars, typval_T *rettv)
5373{
5374 float_T f = 0.0;
5375
5376 rettv->v_type = VAR_FLOAT;
5377 if (get_float_arg(argvars, &f) == OK)
5378 rettv->vval.v_float = log10(f);
5379 else
5380 rettv->vval.v_float = 0.0;
5381}
5382#endif
5383
5384#ifdef FEAT_LUA
5385/*
5386 * "luaeval()" function
5387 */
5388 static void
5389f_luaeval(typval_T *argvars, typval_T *rettv)
5390{
5391 char_u *str;
5392 char_u buf[NUMBUFLEN];
5393
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005394 if (check_restricted() || check_secure())
5395 return;
5396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005397 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005398 do_luaeval(str, argvars + 1, rettv);
5399}
5400#endif
5401
5402/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005403 * "maparg()" function
5404 */
5405 static void
5406f_maparg(typval_T *argvars, typval_T *rettv)
5407{
5408 get_maparg(argvars, rettv, TRUE);
5409}
5410
5411/*
5412 * "mapcheck()" function
5413 */
5414 static void
5415f_mapcheck(typval_T *argvars, typval_T *rettv)
5416{
5417 get_maparg(argvars, rettv, FALSE);
5418}
5419
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005420typedef enum
5421{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 MATCH_END, // matchend()
5423 MATCH_MATCH, // match()
5424 MATCH_STR, // matchstr()
5425 MATCH_LIST, // matchlist()
5426 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005427} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428
5429 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005430find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431{
5432 char_u *str = NULL;
5433 long len = 0;
5434 char_u *expr = NULL;
5435 char_u *pat;
5436 regmatch_T regmatch;
5437 char_u patbuf[NUMBUFLEN];
5438 char_u strbuf[NUMBUFLEN];
5439 char_u *save_cpo;
5440 long start = 0;
5441 long nth = 1;
5442 colnr_T startcol = 0;
5443 int match = 0;
5444 list_T *l = NULL;
5445 listitem_T *li = NULL;
5446 long idx = 0;
5447 char_u *tofree = NULL;
5448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005449 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005450 save_cpo = p_cpo;
5451 p_cpo = (char_u *)"";
5452
5453 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005454 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // type MATCH_LIST: return empty list when there are no matches.
5457 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005458 if (rettv_list_alloc(rettv) == FAIL)
5459 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005460 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461 && (list_append_string(rettv->vval.v_list,
5462 (char_u *)"", 0) == FAIL
5463 || list_append_number(rettv->vval.v_list,
5464 (varnumber_T)-1) == FAIL
5465 || list_append_number(rettv->vval.v_list,
5466 (varnumber_T)-1) == FAIL
5467 || list_append_number(rettv->vval.v_list,
5468 (varnumber_T)-1) == FAIL))
5469 {
5470 list_free(rettv->vval.v_list);
5471 rettv->vval.v_list = NULL;
5472 goto theend;
5473 }
5474 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005475 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 {
5477 rettv->v_type = VAR_STRING;
5478 rettv->vval.v_string = NULL;
5479 }
5480
5481 if (argvars[0].v_type == VAR_LIST)
5482 {
5483 if ((l = argvars[0].vval.v_list) == NULL)
5484 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005485 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 li = l->lv_first;
5487 }
5488 else
5489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005490 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491 len = (long)STRLEN(str);
5492 }
5493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005494 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 if (pat == NULL)
5496 goto theend;
5497
5498 if (argvars[2].v_type != VAR_UNKNOWN)
5499 {
5500 int error = FALSE;
5501
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005502 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005503 if (error)
5504 goto theend;
5505 if (l != NULL)
5506 {
5507 li = list_find(l, start);
5508 if (li == NULL)
5509 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005510 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 }
5512 else
5513 {
5514 if (start < 0)
5515 start = 0;
5516 if (start > len)
5517 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005518 // When "count" argument is there ignore matches before "start",
5519 // otherwise skip part of the string. Differs when pattern is "^"
5520 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521 if (argvars[3].v_type != VAR_UNKNOWN)
5522 startcol = start;
5523 else
5524 {
5525 str += start;
5526 len -= start;
5527 }
5528 }
5529
5530 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005531 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005532 if (error)
5533 goto theend;
5534 }
5535
5536 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5537 if (regmatch.regprog != NULL)
5538 {
5539 regmatch.rm_ic = p_ic;
5540
5541 for (;;)
5542 {
5543 if (l != NULL)
5544 {
5545 if (li == NULL)
5546 {
5547 match = FALSE;
5548 break;
5549 }
5550 vim_free(tofree);
5551 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5552 if (str == NULL)
5553 break;
5554 }
5555
5556 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5557
5558 if (match && --nth <= 0)
5559 break;
5560 if (l == NULL && !match)
5561 break;
5562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 if (l != NULL)
5565 {
5566 li = li->li_next;
5567 ++idx;
5568 }
5569 else
5570 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 startcol = (colnr_T)(regmatch.startp[0]
5572 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573 if (startcol > (colnr_T)len
5574 || str + startcol <= regmatch.startp[0])
5575 {
5576 match = FALSE;
5577 break;
5578 }
5579 }
5580 }
5581
5582 if (match)
5583 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005584 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 {
5586 listitem_T *li1 = rettv->vval.v_list->lv_first;
5587 listitem_T *li2 = li1->li_next;
5588 listitem_T *li3 = li2->li_next;
5589 listitem_T *li4 = li3->li_next;
5590
5591 vim_free(li1->li_tv.vval.v_string);
5592 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005593 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594 li3->li_tv.vval.v_number =
5595 (varnumber_T)(regmatch.startp[0] - expr);
5596 li4->li_tv.vval.v_number =
5597 (varnumber_T)(regmatch.endp[0] - expr);
5598 if (l != NULL)
5599 li2->li_tv.vval.v_number = (varnumber_T)idx;
5600 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005601 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602 {
5603 int i;
5604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005606 for (i = 0; i < NSUBEXP; ++i)
5607 {
5608 if (regmatch.endp[i] == NULL)
5609 {
5610 if (list_append_string(rettv->vval.v_list,
5611 (char_u *)"", 0) == FAIL)
5612 break;
5613 }
5614 else if (list_append_string(rettv->vval.v_list,
5615 regmatch.startp[i],
5616 (int)(regmatch.endp[i] - regmatch.startp[i]))
5617 == FAIL)
5618 break;
5619 }
5620 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005621 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005623 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 if (l != NULL)
5625 copy_tv(&li->li_tv, rettv);
5626 else
5627 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005628 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 }
5630 else if (l != NULL)
5631 rettv->vval.v_number = idx;
5632 else
5633 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005634 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635 rettv->vval.v_number =
5636 (varnumber_T)(regmatch.startp[0] - str);
5637 else
5638 rettv->vval.v_number =
5639 (varnumber_T)(regmatch.endp[0] - str);
5640 rettv->vval.v_number += (varnumber_T)(str - expr);
5641 }
5642 }
5643 vim_regfree(regmatch.regprog);
5644 }
5645
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005646theend:
5647 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 listitem_remove(rettv->vval.v_list,
5650 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651 vim_free(tofree);
5652 p_cpo = save_cpo;
5653}
5654
5655/*
5656 * "match()" function
5657 */
5658 static void
5659f_match(typval_T *argvars, typval_T *rettv)
5660{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005661 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662}
5663
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664/*
5665 * "matchend()" function
5666 */
5667 static void
5668f_matchend(typval_T *argvars, typval_T *rettv)
5669{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005670 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671}
5672
5673/*
5674 * "matchlist()" function
5675 */
5676 static void
5677f_matchlist(typval_T *argvars, typval_T *rettv)
5678{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005679 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680}
5681
5682/*
5683 * "matchstr()" function
5684 */
5685 static void
5686f_matchstr(typval_T *argvars, typval_T *rettv)
5687{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005688 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689}
5690
5691/*
5692 * "matchstrpos()" function
5693 */
5694 static void
5695f_matchstrpos(typval_T *argvars, typval_T *rettv)
5696{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005697 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005698}
5699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700 static void
5701max_min(typval_T *argvars, typval_T *rettv, int domax)
5702{
5703 varnumber_T n = 0;
5704 varnumber_T i;
5705 int error = FALSE;
5706
5707 if (argvars[0].v_type == VAR_LIST)
5708 {
5709 list_T *l;
5710 listitem_T *li;
5711
5712 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005713 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005715 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005717 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5718 n = l->lv_u.nonmat.lv_start;
5719 else
5720 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5721 * l->lv_u.nonmat.lv_stride;
5722 }
5723 else
5724 {
5725 li = l->lv_first;
5726 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005727 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005728 n = tv_get_number_chk(&li->li_tv, &error);
5729 for (;;)
5730 {
5731 li = li->li_next;
5732 if (li == NULL)
5733 break;
5734 i = tv_get_number_chk(&li->li_tv, &error);
5735 if (domax ? i > n : i < n)
5736 n = i;
5737 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 }
5739 }
5740 }
5741 }
5742 else if (argvars[0].v_type == VAR_DICT)
5743 {
5744 dict_T *d;
5745 int first = TRUE;
5746 hashitem_T *hi;
5747 int todo;
5748
5749 d = argvars[0].vval.v_dict;
5750 if (d != NULL)
5751 {
5752 todo = (int)d->dv_hashtab.ht_used;
5753 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5754 {
5755 if (!HASHITEM_EMPTY(hi))
5756 {
5757 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005758 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005759 if (first)
5760 {
5761 n = i;
5762 first = FALSE;
5763 }
5764 else if (domax ? i > n : i < n)
5765 n = i;
5766 }
5767 }
5768 }
5769 }
5770 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005771 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005772 rettv->vval.v_number = error ? 0 : n;
5773}
5774
5775/*
5776 * "max()" function
5777 */
5778 static void
5779f_max(typval_T *argvars, typval_T *rettv)
5780{
5781 max_min(argvars, rettv, TRUE);
5782}
5783
5784/*
5785 * "min()" function
5786 */
5787 static void
5788f_min(typval_T *argvars, typval_T *rettv)
5789{
5790 max_min(argvars, rettv, FALSE);
5791}
5792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793#if defined(FEAT_MZSCHEME) || defined(PROTO)
5794/*
5795 * "mzeval()" function
5796 */
5797 static void
5798f_mzeval(typval_T *argvars, typval_T *rettv)
5799{
5800 char_u *str;
5801 char_u buf[NUMBUFLEN];
5802
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005803 if (check_restricted() || check_secure())
5804 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 do_mzeval(str, rettv);
5807}
5808
5809 void
5810mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5811{
5812 typval_T argvars[3];
5813
5814 argvars[0].v_type = VAR_STRING;
5815 argvars[0].vval.v_string = name;
5816 copy_tv(args, &argvars[1]);
5817 argvars[2].v_type = VAR_UNKNOWN;
5818 f_call(argvars, rettv);
5819 clear_tv(&argvars[1]);
5820}
5821#endif
5822
5823/*
5824 * "nextnonblank()" function
5825 */
5826 static void
5827f_nextnonblank(typval_T *argvars, typval_T *rettv)
5828{
5829 linenr_T lnum;
5830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 {
5833 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5834 {
5835 lnum = 0;
5836 break;
5837 }
5838 if (*skipwhite(ml_get(lnum)) != NUL)
5839 break;
5840 }
5841 rettv->vval.v_number = lnum;
5842}
5843
5844/*
5845 * "nr2char()" function
5846 */
5847 static void
5848f_nr2char(typval_T *argvars, typval_T *rettv)
5849{
5850 char_u buf[NUMBUFLEN];
5851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 if (has_mbyte)
5853 {
5854 int utf8 = 0;
5855
5856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005859 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862 }
5863 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 buf[1] = NUL;
5867 }
5868 rettv->v_type = VAR_STRING;
5869 rettv->vval.v_string = vim_strsave(buf);
5870}
5871
5872/*
5873 * "or(expr, expr)" function
5874 */
5875 static void
5876f_or(typval_T *argvars, typval_T *rettv)
5877{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005878 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5879 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880}
5881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882#ifdef FEAT_PERL
5883/*
5884 * "perleval()" function
5885 */
5886 static void
5887f_perleval(typval_T *argvars, typval_T *rettv)
5888{
5889 char_u *str;
5890 char_u buf[NUMBUFLEN];
5891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005892 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005893 do_perleval(str, rettv);
5894}
5895#endif
5896
5897#ifdef FEAT_FLOAT
5898/*
5899 * "pow()" function
5900 */
5901 static void
5902f_pow(typval_T *argvars, typval_T *rettv)
5903{
5904 float_T fx = 0.0, fy = 0.0;
5905
5906 rettv->v_type = VAR_FLOAT;
5907 if (get_float_arg(argvars, &fx) == OK
5908 && get_float_arg(&argvars[1], &fy) == OK)
5909 rettv->vval.v_float = pow(fx, fy);
5910 else
5911 rettv->vval.v_float = 0.0;
5912}
5913#endif
5914
5915/*
5916 * "prevnonblank()" function
5917 */
5918 static void
5919f_prevnonblank(typval_T *argvars, typval_T *rettv)
5920{
5921 linenr_T lnum;
5922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005923 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5925 lnum = 0;
5926 else
5927 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5928 --lnum;
5929 rettv->vval.v_number = lnum;
5930}
5931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932// This dummy va_list is here because:
5933// - passing a NULL pointer doesn't work when va_list isn't a pointer
5934// - locally in the function results in a "used before set" warning
5935// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936static va_list ap;
5937
5938/*
5939 * "printf()" function
5940 */
5941 static void
5942f_printf(typval_T *argvars, typval_T *rettv)
5943{
5944 char_u buf[NUMBUFLEN];
5945 int len;
5946 char_u *s;
5947 int saved_did_emsg = did_emsg;
5948 char *fmt;
5949
5950 rettv->v_type = VAR_STRING;
5951 rettv->vval.v_string = NULL;
5952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005955 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005956 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957 if (!did_emsg)
5958 {
5959 s = alloc(len + 1);
5960 if (s != NULL)
5961 {
5962 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005963 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5964 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 }
5966 }
5967 did_emsg |= saved_did_emsg;
5968}
5969
5970/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005971 * "pum_getpos()" function
5972 */
5973 static void
5974f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5975{
5976 if (rettv_dict_alloc(rettv) != OK)
5977 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005978 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005979}
5980
5981/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 * "pumvisible()" function
5983 */
5984 static void
5985f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5986{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987 if (pum_visible())
5988 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989}
5990
5991#ifdef FEAT_PYTHON3
5992/*
5993 * "py3eval()" function
5994 */
5995 static void
5996f_py3eval(typval_T *argvars, typval_T *rettv)
5997{
5998 char_u *str;
5999 char_u buf[NUMBUFLEN];
6000
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006001 if (check_restricted() || check_secure())
6002 return;
6003
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006004 if (p_pyx == 0)
6005 p_pyx = 3;
6006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 do_py3eval(str, rettv);
6009}
6010#endif
6011
6012#ifdef FEAT_PYTHON
6013/*
6014 * "pyeval()" function
6015 */
6016 static void
6017f_pyeval(typval_T *argvars, typval_T *rettv)
6018{
6019 char_u *str;
6020 char_u buf[NUMBUFLEN];
6021
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006022 if (check_restricted() || check_secure())
6023 return;
6024
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006025 if (p_pyx == 0)
6026 p_pyx = 2;
6027
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006028 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 do_pyeval(str, rettv);
6030}
6031#endif
6032
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006033#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6034/*
6035 * "pyxeval()" function
6036 */
6037 static void
6038f_pyxeval(typval_T *argvars, typval_T *rettv)
6039{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006040 if (check_restricted() || check_secure())
6041 return;
6042
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006043# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6044 init_pyxversion();
6045 if (p_pyx == 2)
6046 f_pyeval(argvars, rettv);
6047 else
6048 f_py3eval(argvars, rettv);
6049# elif defined(FEAT_PYTHON)
6050 f_pyeval(argvars, rettv);
6051# elif defined(FEAT_PYTHON3)
6052 f_py3eval(argvars, rettv);
6053# endif
6054}
6055#endif
6056
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006057static UINT32_T srand_seed_for_testing = 0;
6058static int srand_seed_for_testing_is_used = FALSE;
6059
6060 static void
6061f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6062{
6063 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006064 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006065 else
6066 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006067 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6068 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006069 }
6070}
6071
6072 static void
6073init_srand(UINT32_T *x)
6074{
6075#ifndef MSWIN
6076 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6077#endif
6078
6079 if (srand_seed_for_testing_is_used)
6080 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006081 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006082 return;
6083 }
6084#ifndef MSWIN
6085 if (dev_urandom_state != FAIL)
6086 {
6087 int fd = open("/dev/urandom", O_RDONLY);
6088 struct {
6089 union {
6090 UINT32_T number;
6091 char bytes[sizeof(UINT32_T)];
6092 } contents;
6093 } buf;
6094
6095 // Attempt reading /dev/urandom.
6096 if (fd == -1)
6097 dev_urandom_state = FAIL;
6098 else
6099 {
6100 buf.contents.number = 0;
6101 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6102 != sizeof(UINT32_T))
6103 dev_urandom_state = FAIL;
6104 else
6105 {
6106 dev_urandom_state = OK;
6107 *x = buf.contents.number;
6108 }
6109 close(fd);
6110 }
6111 }
6112 if (dev_urandom_state != OK)
6113 // Reading /dev/urandom doesn't work, fall back to time().
6114#endif
6115 *x = vim_time();
6116}
6117
6118#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6119#define SPLITMIX32(x, z) ( \
6120 z = (x += 0x9e3779b9), \
6121 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6122 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6123 z ^ (z >> 16) \
6124 )
6125#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6126 result = ROTL(y * 5, 7) * 9; \
6127 t = y << 9; \
6128 z ^= x; \
6129 w ^= y; \
6130 y ^= z, x ^= w; \
6131 z ^= t; \
6132 w = ROTL(w, 11);
6133
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006134/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006135 * "rand()" function
6136 */
6137 static void
6138f_rand(typval_T *argvars, typval_T *rettv)
6139{
6140 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006141 static UINT32_T gx, gy, gz, gw;
6142 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006143 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006144 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006145
6146 if (argvars[0].v_type == VAR_UNKNOWN)
6147 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006148 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006149 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006150 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006151 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006152 init_srand(&x);
6153
6154 gx = SPLITMIX32(x, z);
6155 gy = SPLITMIX32(x, z);
6156 gz = SPLITMIX32(x, z);
6157 gw = SPLITMIX32(x, z);
6158 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006159 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006160
6161 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006162 }
6163 else if (argvars[0].v_type == VAR_LIST)
6164 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006165 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006166 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006167 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006168
6169 lx = list_find(l, 0L);
6170 ly = list_find(l, 1L);
6171 lz = list_find(l, 2L);
6172 lw = list_find(l, 3L);
6173 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6174 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6175 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6176 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6177 x = (UINT32_T)lx->li_tv.vval.v_number;
6178 y = (UINT32_T)ly->li_tv.vval.v_number;
6179 z = (UINT32_T)lz->li_tv.vval.v_number;
6180 w = (UINT32_T)lw->li_tv.vval.v_number;
6181
6182 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6183
6184 lx->li_tv.vval.v_number = (varnumber_T)x;
6185 ly->li_tv.vval.v_number = (varnumber_T)y;
6186 lz->li_tv.vval.v_number = (varnumber_T)z;
6187 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006188 }
6189 else
6190 goto theend;
6191
6192 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006193 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006194 return;
6195
6196theend:
6197 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006198 rettv->v_type = VAR_NUMBER;
6199 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006200}
6201
6202/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006203 * "srand()" function
6204 */
6205 static void
6206f_srand(typval_T *argvars, typval_T *rettv)
6207{
6208 UINT32_T x = 0, z;
6209
6210 if (rettv_list_alloc(rettv) == FAIL)
6211 return;
6212 if (argvars[0].v_type == VAR_UNKNOWN)
6213 {
6214 init_srand(&x);
6215 }
6216 else
6217 {
6218 int error = FALSE;
6219
6220 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6221 if (error)
6222 return;
6223 }
6224
6225 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6226 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6227 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6228 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6229}
6230
6231#undef ROTL
6232#undef SPLITMIX32
6233#undef SHUFFLE_XOSHIRO128STARSTAR
6234
6235/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 * "range()" function
6237 */
6238 static void
6239f_range(typval_T *argvars, typval_T *rettv)
6240{
6241 varnumber_T start;
6242 varnumber_T end;
6243 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 int error = FALSE;
6245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006246 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 if (argvars[1].v_type == VAR_UNKNOWN)
6248 {
6249 end = start - 1;
6250 start = 0;
6251 }
6252 else
6253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006254 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 }
6258
6259 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006260 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006262 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 list_T *list = rettv->vval.v_list;
6268
6269 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006270 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006271 // be called.
6272 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006273 list->lv_u.nonmat.lv_start = start;
6274 list->lv_u.nonmat.lv_end = end;
6275 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006276 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 }
6278}
6279
6280/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006281 * Materialize "list".
6282 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006283 */
6284 void
6285range_list_materialize(list_T *list)
6286{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006287 varnumber_T start = list->lv_u.nonmat.lv_start;
6288 varnumber_T end = list->lv_u.nonmat.lv_end;
6289 int stride = list->lv_u.nonmat.lv_stride;
6290 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006292 list->lv_first = NULL;
6293 list->lv_u.mat.lv_last = NULL;
6294 list->lv_len = 0;
6295 list->lv_u.mat.lv_idx_item = NULL;
6296 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6297 if (list_append_number(list, (varnumber_T)i) == FAIL)
6298 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299}
6300
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006301/*
6302 * "getreginfo()" function
6303 */
6304 static void
6305f_getreginfo(typval_T *argvars, typval_T *rettv)
6306{
6307 char_u *strregname;
6308 int regname;
6309 char_u buf[NUMBUFLEN + 2];
6310 long reglen = 0;
6311 dict_T *dict;
6312 list_T *list;
6313
6314 if (argvars[0].v_type != VAR_UNKNOWN)
6315 {
6316 strregname = tv_get_string_chk(&argvars[0]);
6317 if (strregname == NULL)
6318 return;
6319 }
6320 else
6321 strregname = get_vim_var_str(VV_REG);
6322
6323 regname = (strregname == NULL ? '"' : *strregname);
6324 if (regname == 0 || regname == '@')
6325 regname = '"';
6326
6327 if (rettv_dict_alloc(rettv) == FAIL)
6328 return;
6329 dict = rettv->vval.v_dict;
6330
6331 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6332 if (list == NULL)
6333 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006334 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006335
6336 buf[0] = NUL;
6337 buf[1] = NUL;
6338 switch (get_reg_type(regname, &reglen))
6339 {
6340 case MLINE: buf[0] = 'V'; break;
6341 case MCHAR: buf[0] = 'v'; break;
6342 case MBLOCK:
6343 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6344 reglen + 1);
6345 break;
6346 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006347 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006348
6349 buf[0] = get_register_name(get_unname_register());
6350 buf[1] = NUL;
6351 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006352 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006353 else
6354 {
6355 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6356
6357 if (item != NULL)
6358 {
6359 item->di_tv.v_type = VAR_SPECIAL;
6360 item->di_tv.vval.v_number = regname == buf[0]
6361 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006362 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006363 }
6364 }
6365}
6366
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006367 static void
6368return_register(int regname, typval_T *rettv)
6369{
6370 char_u buf[2] = {0, 0};
6371
6372 buf[0] = (char_u)regname;
6373 rettv->v_type = VAR_STRING;
6374 rettv->vval.v_string = vim_strsave(buf);
6375}
6376
6377/*
6378 * "reg_executing()" function
6379 */
6380 static void
6381f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6382{
6383 return_register(reg_executing, rettv);
6384}
6385
6386/*
6387 * "reg_recording()" function
6388 */
6389 static void
6390f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6391{
6392 return_register(reg_recording, rettv);
6393}
6394
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006395/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 * "rename({from}, {to})" function
6397 */
6398 static void
6399f_rename(typval_T *argvars, typval_T *rettv)
6400{
6401 char_u buf[NUMBUFLEN];
6402
6403 if (check_restricted() || check_secure())
6404 rettv->vval.v_number = -1;
6405 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006406 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6407 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408}
6409
6410/*
6411 * "repeat()" function
6412 */
6413 static void
6414f_repeat(typval_T *argvars, typval_T *rettv)
6415{
6416 char_u *p;
6417 int n;
6418 int slen;
6419 int len;
6420 char_u *r;
6421 int i;
6422
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006423 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 if (argvars[0].v_type == VAR_LIST)
6425 {
6426 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6427 while (n-- > 0)
6428 if (list_extend(rettv->vval.v_list,
6429 argvars[0].vval.v_list, NULL) == FAIL)
6430 break;
6431 }
6432 else
6433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006434 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 rettv->v_type = VAR_STRING;
6436 rettv->vval.v_string = NULL;
6437
6438 slen = (int)STRLEN(p);
6439 len = slen * n;
6440 if (len <= 0)
6441 return;
6442
6443 r = alloc(len + 1);
6444 if (r != NULL)
6445 {
6446 for (i = 0; i < n; i++)
6447 mch_memmove(r + i * slen, p, (size_t)slen);
6448 r[len] = NUL;
6449 }
6450
6451 rettv->vval.v_string = r;
6452 }
6453}
6454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006455#define SP_NOMOVE 0x01 // don't move cursor
6456#define SP_REPEAT 0x02 // repeat to find outer pair
6457#define SP_RETCOUNT 0x04 // return matchcount
6458#define SP_SETPCMARK 0x08 // set previous context mark
6459#define SP_START 0x10 // accept match at start position
6460#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6461#define SP_END 0x40 // leave cursor at end of match
6462#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006463
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464/*
6465 * Get flags for a search function.
6466 * Possibly sets "p_ws".
6467 * Returns BACKWARD, FORWARD or zero (for an error).
6468 */
6469 static int
6470get_search_arg(typval_T *varp, int *flagsp)
6471{
6472 int dir = FORWARD;
6473 char_u *flags;
6474 char_u nbuf[NUMBUFLEN];
6475 int mask;
6476
6477 if (varp->v_type != VAR_UNKNOWN)
6478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006479 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 while (*flags != NUL)
6483 {
6484 switch (*flags)
6485 {
6486 case 'b': dir = BACKWARD; break;
6487 case 'w': p_ws = TRUE; break;
6488 case 'W': p_ws = FALSE; break;
6489 default: mask = 0;
6490 if (flagsp != NULL)
6491 switch (*flags)
6492 {
6493 case 'c': mask = SP_START; break;
6494 case 'e': mask = SP_END; break;
6495 case 'm': mask = SP_RETCOUNT; break;
6496 case 'n': mask = SP_NOMOVE; break;
6497 case 'p': mask = SP_SUBPAT; break;
6498 case 'r': mask = SP_REPEAT; break;
6499 case 's': mask = SP_SETPCMARK; break;
6500 case 'z': mask = SP_COLUMN; break;
6501 }
6502 if (mask == 0)
6503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006504 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 dir = 0;
6506 }
6507 else
6508 *flagsp |= mask;
6509 }
6510 if (dir == 0)
6511 break;
6512 ++flags;
6513 }
6514 }
6515 return dir;
6516}
6517
6518/*
6519 * Shared by search() and searchpos() functions.
6520 */
6521 static int
6522search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6523{
6524 int flags;
6525 char_u *pat;
6526 pos_T pos;
6527 pos_T save_cursor;
6528 int save_p_ws = p_ws;
6529 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006530 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006533 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 long time_limit = 0;
6535#endif
6536 int options = SEARCH_KEEP;
6537 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006538 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006539 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006540 pos_T firstpos;
6541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006542 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 if (dir == 0)
6545 goto theend;
6546 flags = *flagsp;
6547 if (flags & SP_START)
6548 options |= SEARCH_START;
6549 if (flags & SP_END)
6550 options |= SEARCH_END;
6551 if (flags & SP_COLUMN)
6552 options |= SEARCH_COL;
6553
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006554 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006557 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558 if (lnum_stop < 0)
6559 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560 if (argvars[3].v_type != VAR_UNKNOWN)
6561 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006562#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006563 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 if (time_limit < 0)
6565 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006567 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006568 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569 }
6570
6571#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006572 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 profile_setlimit(time_limit, &tm);
6574#endif
6575
6576 /*
6577 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6578 * Check to make sure only those flags are set.
6579 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6580 * flags cannot be set. Check for that condition also.
6581 */
6582 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6583 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006585 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 goto theend;
6587 }
6588
6589 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006590 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006591 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006592 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6593#ifdef FEAT_RELTIME
6594 sia.sa_tm = &tm;
6595#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006596
6597 // Repeat until {skip} returns FALSE.
6598 for (;;)
6599 {
6600 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006601 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006602 // finding the first match again means there is no match where {skip}
6603 // evaluates to zero.
6604 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6605 subpatnum = FAIL;
6606
Bram Moolenaara9c01042020-06-07 14:50:50 +02006607 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006608 // didn't find it or no skip argument
6609 break;
6610 firstpos = pos;
6611
Bram Moolenaara9c01042020-06-07 14:50:50 +02006612 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006613 {
6614 int do_skip;
6615 int err;
6616 pos_T save_pos = curwin->w_cursor;
6617
6618 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006619 err = FALSE;
6620 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006621 curwin->w_cursor = save_pos;
6622 if (err)
6623 {
6624 // Evaluating {skip} caused an error, break here.
6625 subpatnum = FAIL;
6626 break;
6627 }
6628 if (!do_skip)
6629 break;
6630 }
6631 }
6632
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 if (subpatnum != FAIL)
6634 {
6635 if (flags & SP_SUBPAT)
6636 retval = subpatnum;
6637 else
6638 retval = pos.lnum;
6639 if (flags & SP_SETPCMARK)
6640 setpcmark();
6641 curwin->w_cursor = pos;
6642 if (match_pos != NULL)
6643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006644 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 match_pos->lnum = pos.lnum;
6646 match_pos->col = pos.col + 1;
6647 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006648 // "/$" will put the cursor after the end of the line, may need to
6649 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006650 check_cursor();
6651 }
6652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006653 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006654 if (flags & SP_NOMOVE)
6655 curwin->w_cursor = save_cursor;
6656 else
6657 curwin->w_set_curswant = TRUE;
6658theend:
6659 p_ws = save_p_ws;
6660
6661 return retval;
6662}
6663
6664#ifdef FEAT_FLOAT
6665
6666/*
6667 * round() is not in C90, use ceil() or floor() instead.
6668 */
6669 float_T
6670vim_round(float_T f)
6671{
6672 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6673}
6674
6675/*
6676 * "round({float})" function
6677 */
6678 static void
6679f_round(typval_T *argvars, typval_T *rettv)
6680{
6681 float_T f = 0.0;
6682
6683 rettv->v_type = VAR_FLOAT;
6684 if (get_float_arg(argvars, &f) == OK)
6685 rettv->vval.v_float = vim_round(f);
6686 else
6687 rettv->vval.v_float = 0.0;
6688}
6689#endif
6690
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006691#ifdef FEAT_RUBY
6692/*
6693 * "rubyeval()" function
6694 */
6695 static void
6696f_rubyeval(typval_T *argvars, typval_T *rettv)
6697{
6698 char_u *str;
6699 char_u buf[NUMBUFLEN];
6700
6701 str = tv_get_string_buf(&argvars[0], buf);
6702 do_rubyeval(str, rettv);
6703}
6704#endif
6705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706/*
6707 * "screenattr()" function
6708 */
6709 static void
6710f_screenattr(typval_T *argvars, typval_T *rettv)
6711{
6712 int row;
6713 int col;
6714 int c;
6715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006716 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6717 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718 if (row < 0 || row >= screen_Rows
6719 || col < 0 || col >= screen_Columns)
6720 c = -1;
6721 else
6722 c = ScreenAttrs[LineOffset[row] + col];
6723 rettv->vval.v_number = c;
6724}
6725
6726/*
6727 * "screenchar()" function
6728 */
6729 static void
6730f_screenchar(typval_T *argvars, typval_T *rettv)
6731{
6732 int row;
6733 int col;
6734 int off;
6735 int c;
6736
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006737 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6738 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006739 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 c = -1;
6741 else
6742 {
6743 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744 if (enc_utf8 && ScreenLinesUC[off] != 0)
6745 c = ScreenLinesUC[off];
6746 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 c = ScreenLines[off];
6748 }
6749 rettv->vval.v_number = c;
6750}
6751
6752/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006753 * "screenchars()" function
6754 */
6755 static void
6756f_screenchars(typval_T *argvars, typval_T *rettv)
6757{
6758 int row;
6759 int col;
6760 int off;
6761 int c;
6762 int i;
6763
6764 if (rettv_list_alloc(rettv) == FAIL)
6765 return;
6766 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6767 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6768 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6769 return;
6770
6771 off = LineOffset[row] + col;
6772 if (enc_utf8 && ScreenLinesUC[off] != 0)
6773 c = ScreenLinesUC[off];
6774 else
6775 c = ScreenLines[off];
6776 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6777
6778 if (enc_utf8)
6779
6780 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6781 list_append_number(rettv->vval.v_list,
6782 (varnumber_T)ScreenLinesC[i][off]);
6783}
6784
6785/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 * "screencol()" function
6787 *
6788 * First column is 1 to be consistent with virtcol().
6789 */
6790 static void
6791f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6792{
6793 rettv->vval.v_number = screen_screencol() + 1;
6794}
6795
6796/*
6797 * "screenrow()" function
6798 */
6799 static void
6800f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6801{
6802 rettv->vval.v_number = screen_screenrow() + 1;
6803}
6804
6805/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006806 * "screenstring()" function
6807 */
6808 static void
6809f_screenstring(typval_T *argvars, typval_T *rettv)
6810{
6811 int row;
6812 int col;
6813 int off;
6814 int c;
6815 int i;
6816 char_u buf[MB_MAXBYTES + 1];
6817 int buflen = 0;
6818
6819 rettv->vval.v_string = NULL;
6820 rettv->v_type = VAR_STRING;
6821
6822 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6823 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6824 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6825 return;
6826
6827 off = LineOffset[row] + col;
6828 if (enc_utf8 && ScreenLinesUC[off] != 0)
6829 c = ScreenLinesUC[off];
6830 else
6831 c = ScreenLines[off];
6832 buflen += mb_char2bytes(c, buf);
6833
6834 if (enc_utf8)
6835 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6836 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6837
6838 buf[buflen] = NUL;
6839 rettv->vval.v_string = vim_strsave(buf);
6840}
6841
6842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843 * "search()" function
6844 */
6845 static void
6846f_search(typval_T *argvars, typval_T *rettv)
6847{
6848 int flags = 0;
6849
6850 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6851}
6852
6853/*
6854 * "searchdecl()" function
6855 */
6856 static void
6857f_searchdecl(typval_T *argvars, typval_T *rettv)
6858{
6859 int locally = 1;
6860 int thisblock = 0;
6861 int error = FALSE;
6862 char_u *name;
6863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006864 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006866 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 if (argvars[1].v_type != VAR_UNKNOWN)
6868 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006869 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 }
6873 if (!error && name != NULL)
6874 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6875 locally, thisblock, SEARCH_KEEP) == FAIL;
6876}
6877
6878/*
6879 * Used by searchpair() and searchpairpos()
6880 */
6881 static int
6882searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6883{
6884 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006885 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 int save_p_ws = p_ws;
6887 int dir;
6888 int flags = 0;
6889 char_u nbuf1[NUMBUFLEN];
6890 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006891 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 long lnum_stop = 0;
6893 long time_limit = 0;
6894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006895 // Get the three pattern arguments: start, middle, end. Will result in an
6896 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006897 spat = tv_get_string_chk(&argvars[0]);
6898 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6899 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006901 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006903 // Handle the optional fourth argument: flags
6904 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 if (dir == 0)
6906 goto theend;
6907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 // Don't accept SP_END or SP_SUBPAT.
6909 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 if ((flags & (SP_END | SP_SUBPAT)) != 0
6911 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006913 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914 goto theend;
6915 }
6916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006917 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918 if (flags & SP_REPEAT)
6919 p_ws = FALSE;
6920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006921 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006922 if (argvars[3].v_type == VAR_UNKNOWN
6923 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006924 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 else
6926 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006927 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006928 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006929
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 if (argvars[5].v_type != VAR_UNKNOWN)
6931 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006932 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006935 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006937 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938#ifdef FEAT_RELTIME
6939 if (argvars[6].v_type != VAR_UNKNOWN)
6940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006941 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006944 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 }
6948#endif
6949 }
6950 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951
6952 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6953 match_pos, lnum_stop, time_limit);
6954
6955theend:
6956 p_ws = save_p_ws;
6957
6958 return retval;
6959}
6960
6961/*
6962 * "searchpair()" function
6963 */
6964 static void
6965f_searchpair(typval_T *argvars, typval_T *rettv)
6966{
6967 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6968}
6969
6970/*
6971 * "searchpairpos()" function
6972 */
6973 static void
6974f_searchpairpos(typval_T *argvars, typval_T *rettv)
6975{
6976 pos_T match_pos;
6977 int lnum = 0;
6978 int col = 0;
6979
6980 if (rettv_list_alloc(rettv) == FAIL)
6981 return;
6982
6983 if (searchpair_cmn(argvars, &match_pos) > 0)
6984 {
6985 lnum = match_pos.lnum;
6986 col = match_pos.col;
6987 }
6988
6989 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6990 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6991}
6992
6993/*
6994 * Search for a start/middle/end thing.
6995 * Used by searchpair(), see its documentation for the details.
6996 * Returns 0 or -1 for no match,
6997 */
6998 long
6999do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007000 char_u *spat, // start pattern
7001 char_u *mpat, // middle pattern
7002 char_u *epat, // end pattern
7003 int dir, // BACKWARD or FORWARD
7004 typval_T *skip, // skip expression
7005 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007007 linenr_T lnum_stop, // stop at this line if not zero
7008 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009{
7010 char_u *save_cpo;
7011 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7012 long retval = 0;
7013 pos_T pos;
7014 pos_T firstpos;
7015 pos_T foundpos;
7016 pos_T save_cursor;
7017 pos_T save_pos;
7018 int n;
7019 int r;
7020 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007021 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 int err;
7023 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007024#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007025 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007026#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007028 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007029 save_cpo = p_cpo;
7030 p_cpo = empty_option;
7031
7032#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007033 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 profile_setlimit(time_limit, &tm);
7035#endif
7036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007037 // Make two search patterns: start/end (pat2, for in nested pairs) and
7038 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007039 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7040 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 if (pat2 == NULL || pat3 == NULL)
7042 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007043 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 if (*mpat == NUL)
7045 STRCPY(pat3, pat2);
7046 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007047 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 spat, epat, mpat);
7049 if (flags & SP_START)
7050 options |= SEARCH_START;
7051
Bram Moolenaar48570482017-10-30 21:48:41 +01007052 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007053 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 save_cursor = curwin->w_cursor;
7056 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007057 CLEAR_POS(&firstpos);
7058 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 pat = pat3;
7060 for (;;)
7061 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007062 searchit_arg_T sia;
7063
Bram Moolenaara80faa82020-04-12 19:37:17 +02007064 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007065 sia.sa_stop_lnum = lnum_stop;
7066#ifdef FEAT_RELTIME
7067 sia.sa_tm = &tm;
7068#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007069 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007070 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007071 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007072 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 break;
7074
7075 if (firstpos.lnum == 0)
7076 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007077 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // Found the same position again. Can happen with a pattern that
7080 // has "\zs" at the end and searching backwards. Advance one
7081 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007082 if (dir == BACKWARD)
7083 decl(&pos);
7084 else
7085 incl(&pos);
7086 }
7087 foundpos = pos;
7088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007089 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 options &= ~SEARCH_START;
7091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007092 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007093 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 {
7095 save_pos = curwin->w_cursor;
7096 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007097 err = FALSE;
7098 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 curwin->w_cursor = save_pos;
7100 if (err)
7101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007102 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 curwin->w_cursor = save_cursor;
7104 retval = -1;
7105 break;
7106 }
7107 if (r)
7108 continue;
7109 }
7110
7111 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7112 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007113 // Found end when searching backwards or start when searching
7114 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007116 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117 }
7118 else
7119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007120 // Found end when searching forward or start when searching
7121 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 }
7125
7126 if (nest == 0)
7127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007128 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (flags & SP_RETCOUNT)
7130 ++retval;
7131 else
7132 retval = pos.lnum;
7133 if (flags & SP_SETPCMARK)
7134 setpcmark();
7135 curwin->w_cursor = pos;
7136 if (!(flags & SP_REPEAT))
7137 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007138 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 }
7140 }
7141
7142 if (match_pos != NULL)
7143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007144 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 match_pos->lnum = curwin->w_cursor.lnum;
7146 match_pos->col = curwin->w_cursor.col + 1;
7147 }
7148
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007149 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 if ((flags & SP_NOMOVE) || retval == 0)
7151 curwin->w_cursor = save_cursor;
7152
7153theend:
7154 vim_free(pat2);
7155 vim_free(pat3);
7156 if (p_cpo == empty_option)
7157 p_cpo = save_cpo;
7158 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007159 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160 free_string_option(save_cpo);
7161
7162 return retval;
7163}
7164
7165/*
7166 * "searchpos()" function
7167 */
7168 static void
7169f_searchpos(typval_T *argvars, typval_T *rettv)
7170{
7171 pos_T match_pos;
7172 int lnum = 0;
7173 int col = 0;
7174 int n;
7175 int flags = 0;
7176
7177 if (rettv_list_alloc(rettv) == FAIL)
7178 return;
7179
7180 n = search_cmn(argvars, &match_pos, &flags);
7181 if (n > 0)
7182 {
7183 lnum = match_pos.lnum;
7184 col = match_pos.col;
7185 }
7186
7187 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7188 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7189 if (flags & SP_SUBPAT)
7190 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7191}
7192
7193 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7195{
7196 dict_T *d;
7197 dictitem_T *di;
7198 char_u *csearch;
7199
7200 if (argvars[0].v_type != VAR_DICT)
7201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007202 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203 return;
7204 }
7205
7206 if ((d = argvars[0].vval.v_dict) != NULL)
7207 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007208 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 if (csearch != NULL)
7210 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 if (enc_utf8)
7212 {
7213 int pcc[MAX_MCO];
7214 int c = utfc_ptr2char(csearch, pcc);
7215
7216 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7217 }
7218 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007220 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221 }
7222
7223 di = dict_find(d, (char_u *)"forward", -1);
7224 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007225 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226 ? FORWARD : BACKWARD);
7227
7228 di = dict_find(d, (char_u *)"until", -1);
7229 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007230 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 }
7232}
7233
7234/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007235 * "setenv()" function
7236 */
7237 static void
7238f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7239{
7240 char_u namebuf[NUMBUFLEN];
7241 char_u valbuf[NUMBUFLEN];
7242 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7243
7244 if (argvars[1].v_type == VAR_SPECIAL
7245 && argvars[1].vval.v_number == VVAL_NULL)
7246 vim_unsetenv(name);
7247 else
7248 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7249}
7250
7251/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 * "setfperm({fname}, {mode})" function
7253 */
7254 static void
7255f_setfperm(typval_T *argvars, typval_T *rettv)
7256{
7257 char_u *fname;
7258 char_u modebuf[NUMBUFLEN];
7259 char_u *mode_str;
7260 int i;
7261 int mask;
7262 int mode = 0;
7263
7264 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007265 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007266 if (fname == NULL)
7267 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007268 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007269 if (mode_str == NULL)
7270 return;
7271 if (STRLEN(mode_str) != 9)
7272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007273 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 return;
7275 }
7276
7277 mask = 1;
7278 for (i = 8; i >= 0; --i)
7279 {
7280 if (mode_str[i] != '-')
7281 mode |= mask;
7282 mask = mask << 1;
7283 }
7284 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7285}
7286
7287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 * "setpos()" function
7289 */
7290 static void
7291f_setpos(typval_T *argvars, typval_T *rettv)
7292{
7293 pos_T pos;
7294 int fnum;
7295 char_u *name;
7296 colnr_T curswant = -1;
7297
7298 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 if (name != NULL)
7301 {
7302 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7303 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007304 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305 pos.col = 0;
7306 if (name[0] == '.' && name[1] == NUL)
7307 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007308 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007309 curwin->w_cursor = pos;
7310 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007311 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007312 curwin->w_curswant = curswant - 1;
7313 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007314 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007315 check_cursor();
7316 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 }
7318 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007320 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 if (setmark_pos(name[1], &pos, fnum) == OK)
7322 rettv->vval.v_number = 0;
7323 }
7324 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007325 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 }
7327 }
7328}
7329
7330/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007331 * Translate a register type string to the yank type and block length
7332 */
7333 static int
7334get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7335{
7336 char_u *stropt = *pp;
7337 switch (*stropt)
7338 {
7339 case 'v': case 'c': // character-wise selection
7340 *yank_type = MCHAR;
7341 break;
7342 case 'V': case 'l': // line-wise selection
7343 *yank_type = MLINE;
7344 break;
7345 case 'b': case Ctrl_V: // block-wise selection
7346 *yank_type = MBLOCK;
7347 if (VIM_ISDIGIT(stropt[1]))
7348 {
7349 ++stropt;
7350 *block_len = getdigits(&stropt) - 1;
7351 --stropt;
7352 }
7353 break;
7354 default:
7355 return FAIL;
7356 }
7357 *pp = stropt;
7358 return OK;
7359}
7360
7361/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 * "setreg()" function
7363 */
7364 static void
7365f_setreg(typval_T *argvars, typval_T *rettv)
7366{
7367 int regname;
7368 char_u *strregname;
7369 char_u *stropt;
7370 char_u *strval;
7371 int append;
7372 char_u yank_type;
7373 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007374 typval_T *regcontents;
7375 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007377 pointreg = 0;
7378 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007379 block_len = -1;
7380 yank_type = MAUTO;
7381 append = FALSE;
7382
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007383 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007384 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385
7386 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007387 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 regname = *strregname;
7389 if (regname == 0 || regname == '@')
7390 regname = '"';
7391
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007392 if (argvars[1].v_type == VAR_DICT)
7393 {
7394 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007395 dictitem_T *di;
7396
7397 if (d == NULL || d->dv_hashtab.ht_used == 0)
7398 {
7399 // Empty dict, clear the register (like setreg(0, []))
7400 char_u *lstval[2] = {NULL, NULL};
7401 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7402 return;
7403 }
7404
7405 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007406 if (di != NULL)
7407 regcontents = &di->di_tv;
7408
7409 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7410 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007411 {
7412 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7413
7414 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007415 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007416 semsg(_(e_invargval), "value");
7417 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007418 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007419 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007420
7421 if (regname == '"')
7422 {
7423 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7424 if (stropt != NULL)
7425 {
7426 pointreg = *stropt;
7427 regname = pointreg;
7428 }
7429 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007430 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007431 pointreg = regname;
7432 }
7433 else
7434 regcontents = &argvars[1];
7435
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 if (argvars[2].v_type != VAR_UNKNOWN)
7437 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007438 if (yank_type != MAUTO)
7439 {
7440 semsg(_(e_toomanyarg), "setreg");
7441 return;
7442 }
7443
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007444 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007446 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 for (; *stropt != NUL; ++stropt)
7448 switch (*stropt)
7449 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007450 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 append = TRUE;
7452 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007453 default:
7454 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 }
7456 }
7457
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007458 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007459 {
7460 char_u **lstval;
7461 char_u **allocval;
7462 char_u buf[NUMBUFLEN];
7463 char_u **curval;
7464 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007465 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007466 listitem_T *li;
7467 int len;
7468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007469 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007470 len = ll == NULL ? 0 : ll->lv_len;
7471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007472 // First half: use for pointers to result lines; second half: use for
7473 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007474 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 if (lstval == NULL)
7476 return;
7477 curval = lstval;
7478 allocval = lstval + len + 2;
7479 curallocval = allocval;
7480
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007481 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007483 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007484 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007486 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 if (strval == NULL)
7488 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007489 if (strval == buf)
7490 {
7491 // Need to make a copy, next tv_get_string_buf_chk() will
7492 // overwrite the string.
7493 strval = vim_strsave(buf);
7494 if (strval == NULL)
7495 goto free_lstval;
7496 *curallocval++ = strval;
7497 }
7498 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 }
7501 *curval++ = NULL;
7502
7503 write_reg_contents_lst(regname, lstval, -1,
7504 append, yank_type, block_len);
7505free_lstval:
7506 while (curallocval > allocval)
7507 vim_free(*--curallocval);
7508 vim_free(lstval);
7509 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007510 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007512 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 if (strval == NULL)
7514 return;
7515 write_reg_contents_ex(regname, strval, -1,
7516 append, yank_type, block_len);
7517 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007518 if (pointreg != 0)
7519 get_yank_register(pointreg, TRUE);
7520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 rettv->vval.v_number = 0;
7522}
7523
7524/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007525 * "settagstack()" function
7526 */
7527 static void
7528f_settagstack(typval_T *argvars, typval_T *rettv)
7529{
7530 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7531 win_T *wp;
7532 dict_T *d;
7533 int action = 'r';
7534
7535 rettv->vval.v_number = -1;
7536
7537 // first argument: window number or id
7538 wp = find_win_by_nr_or_id(&argvars[0]);
7539 if (wp == NULL)
7540 return;
7541
7542 // second argument: dict with items to set in the tag stack
7543 if (argvars[1].v_type != VAR_DICT)
7544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007546 return;
7547 }
7548 d = argvars[1].vval.v_dict;
7549 if (d == NULL)
7550 return;
7551
7552 // third argument: action - 'a' for append and 'r' for replace.
7553 // default is to replace the stack.
7554 if (argvars[2].v_type == VAR_UNKNOWN)
7555 action = 'r';
7556 else if (argvars[2].v_type == VAR_STRING)
7557 {
7558 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007559 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007560 if (actstr == NULL)
7561 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007562 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7563 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007564 action = *actstr;
7565 else
7566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007567 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007568 return;
7569 }
7570 }
7571 else
7572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007573 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007574 return;
7575 }
7576
7577 if (set_tagstack(wp, d, action) == OK)
7578 rettv->vval.v_number = 0;
7579}
7580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581#ifdef FEAT_CRYPT
7582/*
7583 * "sha256({string})" function
7584 */
7585 static void
7586f_sha256(typval_T *argvars, typval_T *rettv)
7587{
7588 char_u *p;
7589
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007590 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591 rettv->vval.v_string = vim_strsave(
7592 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7593 rettv->v_type = VAR_STRING;
7594}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007595#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596
7597/*
7598 * "shellescape({string})" function
7599 */
7600 static void
7601f_shellescape(typval_T *argvars, typval_T *rettv)
7602{
Bram Moolenaar20615522017-06-05 18:46:26 +02007603 int do_special = non_zero_arg(&argvars[1]);
7604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007606 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 rettv->v_type = VAR_STRING;
7608}
7609
7610/*
7611 * shiftwidth() function
7612 */
7613 static void
7614f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7615{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007616 rettv->vval.v_number = 0;
7617
7618 if (argvars[0].v_type != VAR_UNKNOWN)
7619 {
7620 long col;
7621
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007622 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007623 if (col < 0)
7624 return; // type error; errmsg already given
7625#ifdef FEAT_VARTABS
7626 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7627 return;
7628#endif
7629 }
7630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 rettv->vval.v_number = get_sw_value(curbuf);
7632}
7633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634#ifdef FEAT_FLOAT
7635/*
7636 * "sin()" function
7637 */
7638 static void
7639f_sin(typval_T *argvars, typval_T *rettv)
7640{
7641 float_T f = 0.0;
7642
7643 rettv->v_type = VAR_FLOAT;
7644 if (get_float_arg(argvars, &f) == OK)
7645 rettv->vval.v_float = sin(f);
7646 else
7647 rettv->vval.v_float = 0.0;
7648}
7649
7650/*
7651 * "sinh()" function
7652 */
7653 static void
7654f_sinh(typval_T *argvars, typval_T *rettv)
7655{
7656 float_T f = 0.0;
7657
7658 rettv->v_type = VAR_FLOAT;
7659 if (get_float_arg(argvars, &f) == OK)
7660 rettv->vval.v_float = sinh(f);
7661 else
7662 rettv->vval.v_float = 0.0;
7663}
7664#endif
7665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666/*
7667 * "soundfold({word})" function
7668 */
7669 static void
7670f_soundfold(typval_T *argvars, typval_T *rettv)
7671{
7672 char_u *s;
7673
7674 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007675 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676#ifdef FEAT_SPELL
7677 rettv->vval.v_string = eval_soundfold(s);
7678#else
7679 rettv->vval.v_string = vim_strsave(s);
7680#endif
7681}
7682
7683/*
7684 * "spellbadword()" function
7685 */
7686 static void
7687f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7688{
7689 char_u *word = (char_u *)"";
7690 hlf_T attr = HLF_COUNT;
7691 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007692#ifdef FEAT_SPELL
7693 int wo_spell_save = curwin->w_p_spell;
7694
7695 if (!curwin->w_p_spell)
7696 {
7697 did_set_spelllang(curwin);
7698 curwin->w_p_spell = TRUE;
7699 }
7700
7701 if (*curwin->w_s->b_p_spl == NUL)
7702 {
7703 emsg(_(e_no_spell));
7704 curwin->w_p_spell = wo_spell_save;
7705 return;
7706 }
7707#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708
7709 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007710 {
7711#ifdef FEAT_SPELL
7712 curwin->w_p_spell = wo_spell_save;
7713#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716
7717#ifdef FEAT_SPELL
7718 if (argvars[0].v_type == VAR_UNKNOWN)
7719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007720 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7722 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007723 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007725 curwin->w_set_curswant = TRUE;
7726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007728 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007730 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 int capcol = -1;
7732
7733 if (str != NULL)
7734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007735 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 while (*str != NUL)
7737 {
7738 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7739 if (attr != HLF_COUNT)
7740 {
7741 word = str;
7742 break;
7743 }
7744 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007745 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007746 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747 }
7748 }
7749 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007750 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751#endif
7752
7753 list_append_string(rettv->vval.v_list, word, len);
7754 list_append_string(rettv->vval.v_list, (char_u *)(
7755 attr == HLF_SPB ? "bad" :
7756 attr == HLF_SPR ? "rare" :
7757 attr == HLF_SPL ? "local" :
7758 attr == HLF_SPC ? "caps" :
7759 ""), -1);
7760}
7761
7762/*
7763 * "spellsuggest()" function
7764 */
7765 static void
7766f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7767{
7768#ifdef FEAT_SPELL
7769 char_u *str;
7770 int typeerr = FALSE;
7771 int maxcount;
7772 garray_T ga;
7773 int i;
7774 listitem_T *li;
7775 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007776 int wo_spell_save = curwin->w_p_spell;
7777
7778 if (!curwin->w_p_spell)
7779 {
7780 did_set_spelllang(curwin);
7781 curwin->w_p_spell = TRUE;
7782 }
7783
7784 if (*curwin->w_s->b_p_spl == NUL)
7785 {
7786 emsg(_(e_no_spell));
7787 curwin->w_p_spell = wo_spell_save;
7788 return;
7789 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790#endif
7791
7792 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007793 {
7794#ifdef FEAT_SPELL
7795 curwin->w_p_spell = wo_spell_save;
7796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007798 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799
7800#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007801 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 if (argvars[1].v_type != VAR_UNKNOWN)
7805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007806 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 if (maxcount <= 0)
7808 return;
7809 if (argvars[2].v_type != VAR_UNKNOWN)
7810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007811 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812 if (typeerr)
7813 return;
7814 }
7815 }
7816 else
7817 maxcount = 25;
7818
7819 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7820
7821 for (i = 0; i < ga.ga_len; ++i)
7822 {
7823 str = ((char_u **)ga.ga_data)[i];
7824
7825 li = listitem_alloc();
7826 if (li == NULL)
7827 vim_free(str);
7828 else
7829 {
7830 li->li_tv.v_type = VAR_STRING;
7831 li->li_tv.v_lock = 0;
7832 li->li_tv.vval.v_string = str;
7833 list_append(rettv->vval.v_list, li);
7834 }
7835 }
7836 ga_clear(&ga);
7837 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007838 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839#endif
7840}
7841
7842 static void
7843f_split(typval_T *argvars, typval_T *rettv)
7844{
7845 char_u *str;
7846 char_u *end;
7847 char_u *pat = NULL;
7848 regmatch_T regmatch;
7849 char_u patbuf[NUMBUFLEN];
7850 char_u *save_cpo;
7851 int match;
7852 colnr_T col = 0;
7853 int keepempty = FALSE;
7854 int typeerr = FALSE;
7855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007856 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 save_cpo = p_cpo;
7858 p_cpo = (char_u *)"";
7859
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007860 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 if (argvars[1].v_type != VAR_UNKNOWN)
7862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007863 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 if (pat == NULL)
7865 typeerr = TRUE;
7866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007867 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007868 }
7869 if (pat == NULL || *pat == NUL)
7870 pat = (char_u *)"[\\x01- ]\\+";
7871
7872 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007873 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007875 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876
7877 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7878 if (regmatch.regprog != NULL)
7879 {
7880 regmatch.rm_ic = FALSE;
7881 while (*str != NUL || keepempty)
7882 {
7883 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007884 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 else
7886 match = vim_regexec_nl(&regmatch, str, col);
7887 if (match)
7888 end = regmatch.startp[0];
7889 else
7890 end = str + STRLEN(str);
7891 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7892 && *str != NUL && match && end < regmatch.endp[0]))
7893 {
7894 if (list_append_string(rettv->vval.v_list, str,
7895 (int)(end - str)) == FAIL)
7896 break;
7897 }
7898 if (!match)
7899 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007900 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 if (regmatch.endp[0] > str)
7902 col = 0;
7903 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007904 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 str = regmatch.endp[0];
7907 }
7908
7909 vim_regfree(regmatch.regprog);
7910 }
7911
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007912theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 p_cpo = save_cpo;
7914}
7915
7916#ifdef FEAT_FLOAT
7917/*
7918 * "sqrt()" function
7919 */
7920 static void
7921f_sqrt(typval_T *argvars, typval_T *rettv)
7922{
7923 float_T f = 0.0;
7924
7925 rettv->v_type = VAR_FLOAT;
7926 if (get_float_arg(argvars, &f) == OK)
7927 rettv->vval.v_float = sqrt(f);
7928 else
7929 rettv->vval.v_float = 0.0;
7930}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007931#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007933#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007934/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935 * "str2float()" function
7936 */
7937 static void
7938f_str2float(typval_T *argvars, typval_T *rettv)
7939{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007940 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007941 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942
Bram Moolenaar08243d22017-01-10 16:12:29 +01007943 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944 p = skipwhite(p + 1);
7945 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007946 if (isneg)
7947 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 rettv->v_type = VAR_FLOAT;
7949}
7950#endif
7951
7952/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007953 * "str2list()" function
7954 */
7955 static void
7956f_str2list(typval_T *argvars, typval_T *rettv)
7957{
7958 char_u *p;
7959 int utf8 = FALSE;
7960
7961 if (rettv_list_alloc(rettv) == FAIL)
7962 return;
7963
7964 if (argvars[1].v_type != VAR_UNKNOWN)
7965 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7966
7967 p = tv_get_string(&argvars[0]);
7968
7969 if (has_mbyte || utf8)
7970 {
7971 int (*ptr2len)(char_u *);
7972 int (*ptr2char)(char_u *);
7973
7974 if (utf8 || enc_utf8)
7975 {
7976 ptr2len = utf_ptr2len;
7977 ptr2char = utf_ptr2char;
7978 }
7979 else
7980 {
7981 ptr2len = mb_ptr2len;
7982 ptr2char = mb_ptr2char;
7983 }
7984
7985 for ( ; *p != NUL; p += (*ptr2len)(p))
7986 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7987 }
7988 else
7989 for ( ; *p != NUL; ++p)
7990 list_append_number(rettv->vval.v_list, *p);
7991}
7992
7993/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 * "str2nr()" function
7995 */
7996 static void
7997f_str2nr(typval_T *argvars, typval_T *rettv)
7998{
7999 int base = 10;
8000 char_u *p;
8001 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008002 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008003 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004
8005 if (argvars[1].v_type != VAR_UNKNOWN)
8006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008007 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (base != 2 && base != 8 && base != 10 && base != 16)
8009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008010 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 return;
8012 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008013 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8014 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 }
8016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008017 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008018 isneg = (*p == '-');
8019 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 p = skipwhite(p + 1);
8021 switch (base)
8022 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008023 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008024 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008025 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008027 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8028 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008029 if (isneg)
8030 rettv->vval.v_number = -n;
8031 else
8032 rettv->vval.v_number = n;
8033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034}
8035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036/*
8037 * "strgetchar()" function
8038 */
8039 static void
8040f_strgetchar(typval_T *argvars, typval_T *rettv)
8041{
8042 char_u *str;
8043 int len;
8044 int error = FALSE;
8045 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008046 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008047
8048 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008049 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 if (str == NULL)
8051 return;
8052 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008053 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 if (error)
8055 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056
Bram Moolenaar13505972019-01-24 15:04:48 +01008057 while (charidx >= 0 && byteidx < len)
8058 {
8059 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008061 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8062 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008064 --charidx;
8065 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067}
8068
8069/*
8070 * "stridx()" function
8071 */
8072 static void
8073f_stridx(typval_T *argvars, typval_T *rettv)
8074{
8075 char_u buf[NUMBUFLEN];
8076 char_u *needle;
8077 char_u *haystack;
8078 char_u *save_haystack;
8079 char_u *pos;
8080 int start_idx;
8081
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008082 needle = tv_get_string_chk(&argvars[1]);
8083 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 rettv->vval.v_number = -1;
8085 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008086 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087
8088 if (argvars[2].v_type != VAR_UNKNOWN)
8089 {
8090 int error = FALSE;
8091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008092 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 if (error || start_idx >= (int)STRLEN(haystack))
8094 return;
8095 if (start_idx >= 0)
8096 haystack += start_idx;
8097 }
8098
8099 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8100 if (pos != NULL)
8101 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8102}
8103
8104/*
8105 * "string()" function
8106 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008107 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108f_string(typval_T *argvars, typval_T *rettv)
8109{
8110 char_u *tofree;
8111 char_u numbuf[NUMBUFLEN];
8112
8113 rettv->v_type = VAR_STRING;
8114 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8115 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008116 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 if (rettv->vval.v_string != NULL && tofree == NULL)
8118 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8119}
8120
8121/*
8122 * "strlen()" function
8123 */
8124 static void
8125f_strlen(typval_T *argvars, typval_T *rettv)
8126{
8127 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008128 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129}
8130
8131/*
8132 * "strchars()" function
8133 */
8134 static void
8135f_strchars(typval_T *argvars, typval_T *rettv)
8136{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008137 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 varnumber_T len = 0;
8140 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141
8142 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008143 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008145 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 else
8147 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8149 while (*s != NUL)
8150 {
8151 func_mb_ptr2char_adv(&s);
8152 ++len;
8153 }
8154 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 }
8156}
8157
8158/*
8159 * "strdisplaywidth()" function
8160 */
8161 static void
8162f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8163{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008164 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 int col = 0;
8166
8167 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008168 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169
8170 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8171}
8172
8173/*
8174 * "strwidth()" function
8175 */
8176 static void
8177f_strwidth(typval_T *argvars, typval_T *rettv)
8178{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008179 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180
Bram Moolenaar13505972019-01-24 15:04:48 +01008181 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182}
8183
8184/*
8185 * "strcharpart()" function
8186 */
8187 static void
8188f_strcharpart(typval_T *argvars, typval_T *rettv)
8189{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190 char_u *p;
8191 int nchar;
8192 int nbyte = 0;
8193 int charlen;
8194 int len = 0;
8195 int slen;
8196 int error = FALSE;
8197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008198 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 slen = (int)STRLEN(p);
8200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008201 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 if (!error)
8203 {
8204 if (nchar > 0)
8205 while (nchar > 0 && nbyte < slen)
8206 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008207 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208 --nchar;
8209 }
8210 else
8211 nbyte = nchar;
8212 if (argvars[2].v_type != VAR_UNKNOWN)
8213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008214 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 while (charlen > 0 && nbyte + len < slen)
8216 {
8217 int off = nbyte + len;
8218
8219 if (off < 0)
8220 len += 1;
8221 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008222 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 --charlen;
8224 }
8225 }
8226 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008227 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228 }
8229
8230 /*
8231 * Only return the overlap between the specified part and the actual
8232 * string.
8233 */
8234 if (nbyte < 0)
8235 {
8236 len += nbyte;
8237 nbyte = 0;
8238 }
8239 else if (nbyte > slen)
8240 nbyte = slen;
8241 if (len < 0)
8242 len = 0;
8243 else if (nbyte + len > slen)
8244 len = slen - nbyte;
8245
8246 rettv->v_type = VAR_STRING;
8247 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248}
8249
8250/*
8251 * "strpart()" function
8252 */
8253 static void
8254f_strpart(typval_T *argvars, typval_T *rettv)
8255{
8256 char_u *p;
8257 int n;
8258 int len;
8259 int slen;
8260 int error = FALSE;
8261
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008262 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 slen = (int)STRLEN(p);
8264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008265 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 if (error)
8267 len = 0;
8268 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008269 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008271 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008273 // Only return the overlap between the specified part and the actual
8274 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 if (n < 0)
8276 {
8277 len += n;
8278 n = 0;
8279 }
8280 else if (n > slen)
8281 n = slen;
8282 if (len < 0)
8283 len = 0;
8284 else if (n + len > slen)
8285 len = slen - n;
8286
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008287 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8288 {
8289 int off;
8290
8291 // length in characters
8292 for (off = n; off < slen && len > 0; --len)
8293 off += mb_ptr2len(p + off);
8294 len = off - n;
8295 }
8296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 rettv->v_type = VAR_STRING;
8298 rettv->vval.v_string = vim_strnsave(p + n, len);
8299}
8300
8301/*
8302 * "strridx()" function
8303 */
8304 static void
8305f_strridx(typval_T *argvars, typval_T *rettv)
8306{
8307 char_u buf[NUMBUFLEN];
8308 char_u *needle;
8309 char_u *haystack;
8310 char_u *rest;
8311 char_u *lastmatch = NULL;
8312 int haystack_len, end_idx;
8313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 needle = tv_get_string_chk(&argvars[1]);
8315 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316
8317 rettv->vval.v_number = -1;
8318 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008319 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320
8321 haystack_len = (int)STRLEN(haystack);
8322 if (argvars[2].v_type != VAR_UNKNOWN)
8323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008324 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008327 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 }
8329 else
8330 end_idx = haystack_len;
8331
8332 if (*needle == NUL)
8333 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008334 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335 lastmatch = haystack + end_idx;
8336 }
8337 else
8338 {
8339 for (rest = haystack; *rest != '\0'; ++rest)
8340 {
8341 rest = (char_u *)strstr((char *)rest, (char *)needle);
8342 if (rest == NULL || rest > haystack + end_idx)
8343 break;
8344 lastmatch = rest;
8345 }
8346 }
8347
8348 if (lastmatch == NULL)
8349 rettv->vval.v_number = -1;
8350 else
8351 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8352}
8353
8354/*
8355 * "strtrans()" function
8356 */
8357 static void
8358f_strtrans(typval_T *argvars, typval_T *rettv)
8359{
8360 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362}
8363
8364/*
8365 * "submatch()" function
8366 */
8367 static void
8368f_submatch(typval_T *argvars, typval_T *rettv)
8369{
8370 int error = FALSE;
8371 int no;
8372 int retList = 0;
8373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (error)
8376 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008377 if (no < 0 || no >= NSUBEXP)
8378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008379 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008380 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008383 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 if (error)
8385 return;
8386
8387 if (retList == 0)
8388 {
8389 rettv->v_type = VAR_STRING;
8390 rettv->vval.v_string = reg_submatch(no);
8391 }
8392 else
8393 {
8394 rettv->v_type = VAR_LIST;
8395 rettv->vval.v_list = reg_submatch_list(no);
8396 }
8397}
8398
8399/*
8400 * "substitute()" function
8401 */
8402 static void
8403f_substitute(typval_T *argvars, typval_T *rettv)
8404{
8405 char_u patbuf[NUMBUFLEN];
8406 char_u subbuf[NUMBUFLEN];
8407 char_u flagsbuf[NUMBUFLEN];
8408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008409 char_u *str = tv_get_string_chk(&argvars[0]);
8410 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008411 char_u *sub = NULL;
8412 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008413 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008415 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8416 expr = &argvars[2];
8417 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008418 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008421 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8422 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423 rettv->vval.v_string = NULL;
8424 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008425 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426}
8427
8428/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008429 * "swapinfo(swap_filename)" function
8430 */
8431 static void
8432f_swapinfo(typval_T *argvars, typval_T *rettv)
8433{
8434 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008435 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008436}
8437
8438/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008439 * "swapname(expr)" function
8440 */
8441 static void
8442f_swapname(typval_T *argvars, typval_T *rettv)
8443{
8444 buf_T *buf;
8445
8446 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008447 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008448 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8449 || buf->b_ml.ml_mfp->mf_fname == NULL)
8450 rettv->vval.v_string = NULL;
8451 else
8452 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8453}
8454
8455/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008456 * "synID(lnum, col, trans)" function
8457 */
8458 static void
8459f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8460{
8461 int id = 0;
8462#ifdef FEAT_SYN_HL
8463 linenr_T lnum;
8464 colnr_T col;
8465 int trans;
8466 int transerr = FALSE;
8467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008468 lnum = tv_get_lnum(argvars); // -1 on type error
8469 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008470 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008471
8472 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8473 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8474 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8475#endif
8476
8477 rettv->vval.v_number = id;
8478}
8479
8480/*
8481 * "synIDattr(id, what [, mode])" function
8482 */
8483 static void
8484f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8485{
8486 char_u *p = NULL;
8487#ifdef FEAT_SYN_HL
8488 int id;
8489 char_u *what;
8490 char_u *mode;
8491 char_u modebuf[NUMBUFLEN];
8492 int modec;
8493
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008494 id = (int)tv_get_number(&argvars[0]);
8495 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 if (argvars[2].v_type != VAR_UNKNOWN)
8497 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008498 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 modec = TOLOWER_ASC(mode[0]);
8500 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008501 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 }
8503 else
8504 {
8505#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8506 if (USE_24BIT)
8507 modec = 'g';
8508 else
8509#endif
8510 if (t_colors > 1)
8511 modec = 'c';
8512 else
8513 modec = 't';
8514 }
8515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 switch (TOLOWER_ASC(what[0]))
8517 {
8518 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008519 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 p = highlight_has_attr(id, HL_BOLD, modec);
8523 break;
8524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008525 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 p = highlight_color(id, what, modec);
8527 break;
8528
8529 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008530 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008532 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 p = highlight_has_attr(id, HL_ITALIC, modec);
8534 break;
8535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008536 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008537 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538 break;
8539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008540 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 p = highlight_has_attr(id, HL_INVERSE, modec);
8542 break;
8543
8544 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008545 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008547 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008548 else if (TOLOWER_ASC(what[1]) == 't' &&
8549 TOLOWER_ASC(what[2]) == 'r')
8550 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008551 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552 p = highlight_has_attr(id, HL_STANDOUT, modec);
8553 break;
8554
8555 case 'u':
8556 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008557 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8559 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008560 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8562 break;
8563 }
8564
8565 if (p != NULL)
8566 p = vim_strsave(p);
8567#endif
8568 rettv->v_type = VAR_STRING;
8569 rettv->vval.v_string = p;
8570}
8571
8572/*
8573 * "synIDtrans(id)" function
8574 */
8575 static void
8576f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8577{
8578 int id;
8579
8580#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008581 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582
8583 if (id > 0)
8584 id = syn_get_final_id(id);
8585 else
8586#endif
8587 id = 0;
8588
8589 rettv->vval.v_number = id;
8590}
8591
8592/*
8593 * "synconcealed(lnum, col)" function
8594 */
8595 static void
8596f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8597{
8598#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8599 linenr_T lnum;
8600 colnr_T col;
8601 int syntax_flags = 0;
8602 int cchar;
8603 int matchid = 0;
8604 char_u str[NUMBUFLEN];
8605#endif
8606
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008607 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608
8609#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008610 lnum = tv_get_lnum(argvars); // -1 on type error
8611 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612
Bram Moolenaara80faa82020-04-12 19:37:17 +02008613 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614
8615 if (rettv_list_alloc(rettv) != FAIL)
8616 {
8617 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8618 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8619 && curwin->w_p_cole > 0)
8620 {
8621 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8622 syntax_flags = get_syntax_info(&matchid);
8623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008624 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8626 {
8627 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008628 if (cchar == NUL && curwin->w_p_cole == 1)
8629 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 if (cchar != NUL)
8631 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (has_mbyte)
8633 (*mb_char2bytes)(cchar, str);
8634 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 str[0] = cchar;
8636 }
8637 }
8638 }
8639
8640 list_append_number(rettv->vval.v_list,
8641 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008642 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 list_append_string(rettv->vval.v_list, str, -1);
8644 list_append_number(rettv->vval.v_list, matchid);
8645 }
8646#endif
8647}
8648
8649/*
8650 * "synstack(lnum, col)" function
8651 */
8652 static void
8653f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8654{
8655#ifdef FEAT_SYN_HL
8656 linenr_T lnum;
8657 colnr_T col;
8658 int i;
8659 int id;
8660#endif
8661
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008662 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663
8664#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008665 lnum = tv_get_lnum(argvars); // -1 on type error
8666 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667
8668 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8669 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8670 && rettv_list_alloc(rettv) != FAIL)
8671 {
8672 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8673 for (i = 0; ; ++i)
8674 {
8675 id = syn_get_stack_item(i);
8676 if (id < 0)
8677 break;
8678 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8679 break;
8680 }
8681 }
8682#endif
8683}
8684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685/*
8686 * "tabpagebuflist()" function
8687 */
8688 static void
8689f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8690{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691 tabpage_T *tp;
8692 win_T *wp = NULL;
8693
8694 if (argvars[0].v_type == VAR_UNKNOWN)
8695 wp = firstwin;
8696 else
8697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 if (tp != NULL)
8700 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8701 }
8702 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8703 {
8704 for (; wp != NULL; wp = wp->w_next)
8705 if (list_append_number(rettv->vval.v_list,
8706 wp->w_buffer->b_fnum) == FAIL)
8707 break;
8708 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709}
8710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 * "tagfiles()" function
8713 */
8714 static void
8715f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8716{
8717 char_u *fname;
8718 tagname_T tn;
8719 int first;
8720
8721 if (rettv_list_alloc(rettv) == FAIL)
8722 return;
8723 fname = alloc(MAXPATHL);
8724 if (fname == NULL)
8725 return;
8726
8727 for (first = TRUE; ; first = FALSE)
8728 if (get_tagfname(&tn, first, fname) == FAIL
8729 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8730 break;
8731 tagname_free(&tn);
8732 vim_free(fname);
8733}
8734
8735/*
8736 * "taglist()" function
8737 */
8738 static void
8739f_taglist(typval_T *argvars, typval_T *rettv)
8740{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008741 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 char_u *tag_pattern;
8743
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008744 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745
8746 rettv->vval.v_number = FALSE;
8747 if (*tag_pattern == NUL)
8748 return;
8749
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008750 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008753 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754}
8755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756#ifdef FEAT_FLOAT
8757/*
8758 * "tan()" function
8759 */
8760 static void
8761f_tan(typval_T *argvars, typval_T *rettv)
8762{
8763 float_T f = 0.0;
8764
8765 rettv->v_type = VAR_FLOAT;
8766 if (get_float_arg(argvars, &f) == OK)
8767 rettv->vval.v_float = tan(f);
8768 else
8769 rettv->vval.v_float = 0.0;
8770}
8771
8772/*
8773 * "tanh()" function
8774 */
8775 static void
8776f_tanh(typval_T *argvars, typval_T *rettv)
8777{
8778 float_T f = 0.0;
8779
8780 rettv->v_type = VAR_FLOAT;
8781 if (get_float_arg(argvars, &f) == OK)
8782 rettv->vval.v_float = tanh(f);
8783 else
8784 rettv->vval.v_float = 0.0;
8785}
8786#endif
8787
8788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 * "tolower(string)" function
8790 */
8791 static void
8792f_tolower(typval_T *argvars, typval_T *rettv)
8793{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008795 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796}
8797
8798/*
8799 * "toupper(string)" function
8800 */
8801 static void
8802f_toupper(typval_T *argvars, typval_T *rettv)
8803{
8804 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806}
8807
8808/*
8809 * "tr(string, fromstr, tostr)" function
8810 */
8811 static void
8812f_tr(typval_T *argvars, typval_T *rettv)
8813{
8814 char_u *in_str;
8815 char_u *fromstr;
8816 char_u *tostr;
8817 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 int inlen;
8819 int fromlen;
8820 int tolen;
8821 int idx;
8822 char_u *cpstr;
8823 int cplen;
8824 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 char_u buf[NUMBUFLEN];
8826 char_u buf2[NUMBUFLEN];
8827 garray_T ga;
8828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008829 in_str = tv_get_string(&argvars[0]);
8830 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8831 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008833 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 rettv->v_type = VAR_STRING;
8835 rettv->vval.v_string = NULL;
8836 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008837 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 ga_init2(&ga, (int)sizeof(char), 80);
8839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008841 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (STRLEN(fromstr) != STRLEN(tostr))
8843 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008845 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 ga_clear(&ga);
8847 return;
8848 }
8849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008850 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 while (*in_str != NUL)
8852 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 if (has_mbyte)
8854 {
8855 inlen = (*mb_ptr2len)(in_str);
8856 cpstr = in_str;
8857 cplen = inlen;
8858 idx = 0;
8859 for (p = fromstr; *p != NUL; p += fromlen)
8860 {
8861 fromlen = (*mb_ptr2len)(p);
8862 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8863 {
8864 for (p = tostr; *p != NUL; p += tolen)
8865 {
8866 tolen = (*mb_ptr2len)(p);
8867 if (idx-- == 0)
8868 {
8869 cplen = tolen;
8870 cpstr = p;
8871 break;
8872 }
8873 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008874 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 goto error;
8876 break;
8877 }
8878 ++idx;
8879 }
8880
8881 if (first && cpstr == in_str)
8882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008883 // Check that fromstr and tostr have the same number of
8884 // (multi-byte) characters. Done only once when a character
8885 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 first = FALSE;
8887 for (p = tostr; *p != NUL; p += tolen)
8888 {
8889 tolen = (*mb_ptr2len)(p);
8890 --idx;
8891 }
8892 if (idx != 0)
8893 goto error;
8894 }
8895
8896 (void)ga_grow(&ga, cplen);
8897 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8898 ga.ga_len += cplen;
8899
8900 in_str += inlen;
8901 }
8902 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008904 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 p = vim_strchr(fromstr, *in_str);
8906 if (p != NULL)
8907 ga_append(&ga, tostr[p - fromstr]);
8908 else
8909 ga_append(&ga, *in_str);
8910 ++in_str;
8911 }
8912 }
8913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008914 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 (void)ga_grow(&ga, 1);
8916 ga_append(&ga, NUL);
8917
8918 rettv->vval.v_string = ga.ga_data;
8919}
8920
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008921/*
8922 * "trim({expr})" function
8923 */
8924 static void
8925f_trim(typval_T *argvars, typval_T *rettv)
8926{
8927 char_u buf1[NUMBUFLEN];
8928 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008930 char_u *mask = NULL;
8931 char_u *tail;
8932 char_u *prev;
8933 char_u *p;
8934 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008935 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008936
8937 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008938 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008939 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008940 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008941
8942 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008943 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008944 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008945
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008946 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008947 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008948 int error = 0;
8949
8950 // leading or trailing characters to trim
8951 dir = (int)tv_get_number_chk(&argvars[2], &error);
8952 if (error)
8953 return;
8954 if (dir < 0 || dir > 2)
8955 {
8956 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8957 return;
8958 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008959 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008960 }
8961
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008962 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008963 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008964 // Trim leading characters
8965 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008966 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008967 c1 = PTR2CHAR(head);
8968 if (mask == NULL)
8969 {
8970 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008971 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008972 }
8973 else
8974 {
8975 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8976 if (c1 == PTR2CHAR(p))
8977 break;
8978 if (*p == NUL)
8979 break;
8980 }
8981 MB_PTR_ADV(head);
8982 }
8983 }
8984
8985 tail = head + STRLEN(head);
8986 if (dir == 0 || dir == 2)
8987 {
8988 // Trim trailing characters
8989 for (; tail > head; tail = prev)
8990 {
8991 prev = tail;
8992 MB_PTR_BACK(head, prev);
8993 c1 = PTR2CHAR(prev);
8994 if (mask == NULL)
8995 {
8996 if (c1 > ' ' && c1 != 0xa0)
8997 break;
8998 }
8999 else
9000 {
9001 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9002 if (c1 == PTR2CHAR(p))
9003 break;
9004 if (*p == NUL)
9005 break;
9006 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009007 }
9008 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009009 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009010}
9011
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012#ifdef FEAT_FLOAT
9013/*
9014 * "trunc({float})" function
9015 */
9016 static void
9017f_trunc(typval_T *argvars, typval_T *rettv)
9018{
9019 float_T f = 0.0;
9020
9021 rettv->v_type = VAR_FLOAT;
9022 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009023 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9025 else
9026 rettv->vval.v_float = 0.0;
9027}
9028#endif
9029
9030/*
9031 * "type(expr)" function
9032 */
9033 static void
9034f_type(typval_T *argvars, typval_T *rettv)
9035{
9036 int n = -1;
9037
9038 switch (argvars[0].v_type)
9039 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009040 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9041 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009043 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9044 case VAR_LIST: n = VAR_TYPE_LIST; break;
9045 case VAR_DICT: n = VAR_TYPE_DICT; break;
9046 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9047 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9048 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009049 case VAR_JOB: n = VAR_TYPE_JOB; break;
9050 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009051 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009052 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009053 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009054 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009055 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 n = -1;
9057 break;
9058 }
9059 rettv->vval.v_number = n;
9060}
9061
9062/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 * "virtcol(string)" function
9064 */
9065 static void
9066f_virtcol(typval_T *argvars, typval_T *rettv)
9067{
9068 colnr_T vcol = 0;
9069 pos_T *fp;
9070 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009071 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072
9073 fp = var2fpos(&argvars[0], FALSE, &fnum);
9074 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9075 && fnum == curbuf->b_fnum)
9076 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009077 // Limit the column to a valid value, getvvcol() doesn't check.
9078 if (fp->col < 0)
9079 fp->col = 0;
9080 else
9081 {
9082 len = (int)STRLEN(ml_get(fp->lnum));
9083 if (fp->col > len)
9084 fp->col = len;
9085 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 getvvcol(curwin, fp, NULL, NULL, &vcol);
9087 ++vcol;
9088 }
9089
9090 rettv->vval.v_number = vcol;
9091}
9092
9093/*
9094 * "visualmode()" function
9095 */
9096 static void
9097f_visualmode(typval_T *argvars, typval_T *rettv)
9098{
9099 char_u str[2];
9100
9101 rettv->v_type = VAR_STRING;
9102 str[0] = curbuf->b_visual_mode_eval;
9103 str[1] = NUL;
9104 rettv->vval.v_string = vim_strsave(str);
9105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009106 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 if (non_zero_arg(&argvars[0]))
9108 curbuf->b_visual_mode_eval = NUL;
9109}
9110
9111/*
9112 * "wildmenumode()" function
9113 */
9114 static void
9115f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9116{
9117#ifdef FEAT_WILDMENU
9118 if (wild_menu_showing)
9119 rettv->vval.v_number = 1;
9120#endif
9121}
9122
9123/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009124 * "windowsversion()" function
9125 */
9126 static void
9127f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9128{
9129 rettv->v_type = VAR_STRING;
9130 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9131}
9132
9133/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009134 * "wordcount()" function
9135 */
9136 static void
9137f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9138{
9139 if (rettv_dict_alloc(rettv) == FAIL)
9140 return;
9141 cursor_pos_info(rettv->vval.v_dict);
9142}
9143
9144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 * "xor(expr, expr)" function
9146 */
9147 static void
9148f_xor(typval_T *argvars, typval_T *rettv)
9149{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009150 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9151 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009152}
9153
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009154#endif // FEAT_EVAL