blob: 5607ef9e5ba8a2b5490c527b40195d3d3f0a8f1d [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},
497 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
498 {"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},
718 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
719 {"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},
953 {"strpart", 2, 3, 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},
1049 {"test_void", 0, 0, 0, ret_any, 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 */
2201
2202 static char_u *
2203get_list_line(
2204 int c UNUSED,
2205 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002206 int indent UNUSED,
2207 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208{
2209 listitem_T **p = (listitem_T **)cookie;
2210 listitem_T *item = *p;
2211 char_u buf[NUMBUFLEN];
2212 char_u *s;
2213
2214 if (item == NULL)
2215 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002216 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 *p = item->li_next;
2218 return s == NULL ? NULL : vim_strsave(s);
2219}
2220
2221/*
2222 * "execute()" function
2223 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002224 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002225execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226{
2227 char_u *cmd = NULL;
2228 list_T *list = NULL;
2229 int save_msg_silent = msg_silent;
2230 int save_emsg_silent = emsg_silent;
2231 int save_emsg_noredir = emsg_noredir;
2232 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002233 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002234 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002235 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002236 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237
2238 rettv->vval.v_string = NULL;
2239 rettv->v_type = VAR_STRING;
2240
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002241 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002243 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002244 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 return;
2247 ++list->lv_refcount;
2248 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002249 else if (argvars[arg_off].v_type == VAR_JOB
2250 || argvars[arg_off].v_type == VAR_CHANNEL)
2251 {
2252 emsg(_(e_inval_string));
2253 return;
2254 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 else
2256 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002257 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 if (cmd == NULL)
2259 return;
2260 }
2261
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002262 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002263 {
2264 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002265 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266
2267 if (s == NULL)
2268 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002269 if (*s == NUL)
2270 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 if (STRNCMP(s, "silent", 6) == 0)
2272 ++msg_silent;
2273 if (STRCMP(s, "silent!") == 0)
2274 {
2275 emsg_silent = TRUE;
2276 emsg_noredir = TRUE;
2277 }
2278 }
2279 else
2280 ++msg_silent;
2281
2282 if (redir_execute)
2283 save_ga = redir_execute_ga;
2284 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2285 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002286 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002287 if (!echo_output)
2288 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289
2290 if (cmd != NULL)
2291 do_cmdline_cmd(cmd);
2292 else
2293 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002294 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002296 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002297 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298 do_cmdline(NULL, get_list_line, (void *)&item,
2299 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2300 --list->lv_refcount;
2301 }
2302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002303 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002304 if (ga_grow(&redir_execute_ga, 1) == OK)
2305 {
2306 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2307 rettv->vval.v_string = redir_execute_ga.ga_data;
2308 }
2309 else
2310 {
2311 ga_clear(&redir_execute_ga);
2312 rettv->vval.v_string = NULL;
2313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314 msg_silent = save_msg_silent;
2315 emsg_silent = save_emsg_silent;
2316 emsg_noredir = save_emsg_noredir;
2317
2318 redir_execute = save_redir_execute;
2319 if (redir_execute)
2320 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002321 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002322
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002323 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002324 if (echo_output)
2325 // When not working silently: put it in column zero. A following
2326 // "echon" will overwrite the message, unavoidably.
2327 msg_col = 0;
2328 else
2329 // When working silently: Put it back where it was, since nothing
2330 // should have been written.
2331 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002332}
2333
2334/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002335 * "execute()" function
2336 */
2337 static void
2338f_execute(typval_T *argvars, typval_T *rettv)
2339{
2340 execute_common(argvars, rettv, 0);
2341}
2342
2343/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002344 * "exists()" function
2345 */
2346 static void
2347f_exists(typval_T *argvars, typval_T *rettv)
2348{
2349 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002352 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002355 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 if (mch_getenv(p + 1) != NULL)
2357 n = TRUE;
2358 else
2359 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002360 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 p = expand_env_save(p);
2362 if (p != NULL && *p != '$')
2363 n = TRUE;
2364 vim_free(p);
2365 }
2366 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002367 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002368 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002369 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002371 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002373 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002375 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002377 else if (*p == '?') // internal function only
2378 {
2379 n = has_internal_func_name(p + 1);
2380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 else if (*p == ':')
2382 {
2383 n = cmd_exists(p + 1);
2384 }
2385 else if (*p == '#')
2386 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 if (p[1] == '#')
2388 n = autocmd_supported(p + 2);
2389 else
2390 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002392 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002394 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 }
2396
2397 rettv->vval.v_number = n;
2398}
2399
2400#ifdef FEAT_FLOAT
2401/*
2402 * "exp()" function
2403 */
2404 static void
2405f_exp(typval_T *argvars, typval_T *rettv)
2406{
2407 float_T f = 0.0;
2408
2409 rettv->v_type = VAR_FLOAT;
2410 if (get_float_arg(argvars, &f) == OK)
2411 rettv->vval.v_float = exp(f);
2412 else
2413 rettv->vval.v_float = 0.0;
2414}
2415#endif
2416
2417/*
2418 * "expand()" function
2419 */
2420 static void
2421f_expand(typval_T *argvars, typval_T *rettv)
2422{
2423 char_u *s;
2424 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002425 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2427 expand_T xpc;
2428 int error = FALSE;
2429 char_u *result;
2430
2431 rettv->v_type = VAR_STRING;
2432 if (argvars[1].v_type != VAR_UNKNOWN
2433 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002434 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002436 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002438 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 if (*s == '%' || *s == '#' || *s == '<')
2440 {
2441 ++emsg_off;
2442 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2443 --emsg_off;
2444 if (rettv->v_type == VAR_LIST)
2445 {
2446 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2447 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002448 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002449 }
2450 else
2451 rettv->vval.v_string = result;
2452 }
2453 else
2454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002455 // When the optional second argument is non-zero, don't remove matches
2456 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459 options |= WILD_KEEP_ALL;
2460 if (!error)
2461 {
2462 ExpandInit(&xpc);
2463 xpc.xp_context = EXPAND_FILES;
2464 if (p_wic)
2465 options += WILD_ICASE;
2466 if (rettv->v_type == VAR_STRING)
2467 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2468 options, WILD_ALL);
2469 else if (rettv_list_alloc(rettv) != FAIL)
2470 {
2471 int i;
2472
2473 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2474 for (i = 0; i < xpc.xp_numfiles; i++)
2475 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2476 ExpandCleanup(&xpc);
2477 }
2478 }
2479 else
2480 rettv->vval.v_string = NULL;
2481 }
2482}
2483
2484/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002485 * "expandcmd()" function
2486 * Expand all the special characters in a command string.
2487 */
2488 static void
2489f_expandcmd(typval_T *argvars, typval_T *rettv)
2490{
2491 exarg_T eap;
2492 char_u *cmdstr;
2493 char *errormsg = NULL;
2494
2495 rettv->v_type = VAR_STRING;
2496 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2497
2498 memset(&eap, 0, sizeof(eap));
2499 eap.cmd = cmdstr;
2500 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002501 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002502 eap.usefilter = FALSE;
2503 eap.nextcmd = NULL;
2504 eap.cmdidx = CMD_USER;
2505
2506 expand_filename(&eap, &cmdstr, &errormsg);
2507 if (errormsg != NULL && *errormsg != NUL)
2508 emsg(errormsg);
2509
2510 rettv->vval.v_string = cmdstr;
2511}
2512
2513/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 * "feedkeys()" function
2515 */
2516 static void
2517f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2518{
2519 int remap = TRUE;
2520 int insert = FALSE;
2521 char_u *keys, *flags;
2522 char_u nbuf[NUMBUFLEN];
2523 int typed = FALSE;
2524 int execute = FALSE;
2525 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002526 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527 char_u *keys_esc;
2528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002529 // This is not allowed in the sandbox. If the commands would still be
2530 // executed in the sandbox it would be OK, but it probably happens later,
2531 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 if (check_secure())
2533 return;
2534
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002535 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536
2537 if (argvars[1].v_type != VAR_UNKNOWN)
2538 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002539 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540 for ( ; *flags != NUL; ++flags)
2541 {
2542 switch (*flags)
2543 {
2544 case 'n': remap = FALSE; break;
2545 case 'm': remap = TRUE; break;
2546 case 't': typed = TRUE; break;
2547 case 'i': insert = TRUE; break;
2548 case 'x': execute = TRUE; break;
2549 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002550 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 }
2552 }
2553 }
2554
2555 if (*keys != NUL || execute)
2556 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002557 // Need to escape K_SPECIAL and CSI before putting the string in the
2558 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002559 keys_esc = vim_strsave_escape_csi(keys);
2560 if (keys_esc != NULL)
2561 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002562 if (lowlevel)
2563 {
2564#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002565 int idx;
2566 int len = (int)STRLEN(keys);
2567
2568 for (idx = 0; idx < len; ++idx)
2569 {
2570 // if a CTRL-C was typed, set got_int, similar to what
2571 // happens in fill_input_buf()
2572 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2573 got_int = TRUE;
2574 add_to_input_buf(keys + idx, 1);
2575 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002576#else
2577 emsg(_("E980: lowlevel input not supported"));
2578#endif
2579 }
2580 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002581 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002582 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002584 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002585#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002586 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002587#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002588 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002589 typebuf_was_filled = TRUE;
2590 }
2591 vim_free(keys_esc);
2592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593 if (execute)
2594 {
2595 int save_msg_scroll = msg_scroll;
2596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002597 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 msg_scroll = FALSE;
2599
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002600 if (!dangerous)
2601 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002602 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002603 if (!dangerous)
2604 --ex_normal_busy;
2605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 msg_scroll |= save_msg_scroll;
2607 }
2608 }
2609 }
2610}
2611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612#ifdef FEAT_FLOAT
2613/*
2614 * "float2nr({float})" function
2615 */
2616 static void
2617f_float2nr(typval_T *argvars, typval_T *rettv)
2618{
2619 float_T f = 0.0;
2620
2621 if (get_float_arg(argvars, &f) == OK)
2622 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002623 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002624 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002625 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002626 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 else
2628 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 }
2630}
2631
2632/*
2633 * "floor({float})" function
2634 */
2635 static void
2636f_floor(typval_T *argvars, typval_T *rettv)
2637{
2638 float_T f = 0.0;
2639
2640 rettv->v_type = VAR_FLOAT;
2641 if (get_float_arg(argvars, &f) == OK)
2642 rettv->vval.v_float = floor(f);
2643 else
2644 rettv->vval.v_float = 0.0;
2645}
2646
2647/*
2648 * "fmod()" function
2649 */
2650 static void
2651f_fmod(typval_T *argvars, typval_T *rettv)
2652{
2653 float_T fx = 0.0, fy = 0.0;
2654
2655 rettv->v_type = VAR_FLOAT;
2656 if (get_float_arg(argvars, &fx) == OK
2657 && get_float_arg(&argvars[1], &fy) == OK)
2658 rettv->vval.v_float = fmod(fx, fy);
2659 else
2660 rettv->vval.v_float = 0.0;
2661}
2662#endif
2663
2664/*
2665 * "fnameescape({string})" function
2666 */
2667 static void
2668f_fnameescape(typval_T *argvars, typval_T *rettv)
2669{
2670 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002671 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 rettv->v_type = VAR_STRING;
2673}
2674
2675/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676 * "foreground()" function
2677 */
2678 static void
2679f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2680{
2681#ifdef FEAT_GUI
2682 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002685 return;
2686 }
2687#endif
2688#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690#endif
2691}
2692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002694common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695{
2696 char_u *s;
2697 char_u *name;
2698 int use_string = FALSE;
2699 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002700 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002701 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702
2703 if (argvars[0].v_type == VAR_FUNC)
2704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002705 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 s = argvars[0].vval.v_string;
2707 }
2708 else if (argvars[0].v_type == VAR_PARTIAL
2709 && argvars[0].vval.v_partial != NULL)
2710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002711 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002713 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 }
2715 else
2716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002717 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002718 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 use_string = TRUE;
2720 }
2721
Bram Moolenaar843b8842016-08-21 14:36:15 +02002722 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002723 {
2724 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002725 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002726 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2727 if (*name != NUL)
2728 s = NULL;
2729 }
2730
Bram Moolenaar843b8842016-08-21 14:36:15 +02002731 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2732 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002733 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002735 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002736 ? find_func(trans_name, is_global, NULL) == NULL
2737 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002738 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 else
2740 {
2741 int dict_idx = 0;
2742 int arg_idx = 0;
2743 list_T *list = NULL;
2744
2745 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2746 {
2747 char sid_buf[25];
2748 int off = *s == 's' ? 2 : 5;
2749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002750 // Expand s: and <SID> into <SNR>nr_, so that the function can
2751 // also be called from another script. Using trans_function_name()
2752 // would also work, but some plugins depend on the name being
2753 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002754 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002755 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 if (name != NULL)
2757 {
2758 STRCPY(name, sid_buf);
2759 STRCAT(name, s + off);
2760 }
2761 }
2762 else
2763 name = vim_strsave(s);
2764
2765 if (argvars[1].v_type != VAR_UNKNOWN)
2766 {
2767 if (argvars[2].v_type != VAR_UNKNOWN)
2768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002769 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 arg_idx = 1;
2771 dict_idx = 2;
2772 }
2773 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 dict_idx = 1;
2776 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 arg_idx = 1;
2779 if (dict_idx > 0)
2780 {
2781 if (argvars[dict_idx].v_type != VAR_DICT)
2782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002785 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 }
2787 if (argvars[dict_idx].vval.v_dict == NULL)
2788 dict_idx = 0;
2789 }
2790 if (arg_idx > 0)
2791 {
2792 if (argvars[arg_idx].v_type != VAR_LIST)
2793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002794 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002796 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 }
2798 list = argvars[arg_idx].vval.v_list;
2799 if (list == NULL || list->lv_len == 0)
2800 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002801 else if (list->lv_len > MAX_FUNC_ARGS)
2802 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002803 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002804 vim_free(name);
2805 goto theend;
2806 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 }
2808 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002809 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002811 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002813 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 if (pt == NULL)
2815 vim_free(name);
2816 else
2817 {
2818 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2819 {
2820 listitem_T *li;
2821 int i = 0;
2822 int arg_len = 0;
2823 int lv_len = 0;
2824
2825 if (arg_pt != NULL)
2826 arg_len = arg_pt->pt_argc;
2827 if (list != NULL)
2828 lv_len = list->lv_len;
2829 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002830 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (pt->pt_argv == NULL)
2832 {
2833 vim_free(pt);
2834 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002835 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002837 for (i = 0; i < arg_len; i++)
2838 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2839 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002840 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002841 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002842 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002843 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002844 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002845 }
2846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002847 // For "function(dict.func, [], dict)" and "func" is a partial
2848 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002849 if (dict_idx > 0)
2850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2853 ++pt->pt_dict->dv_refcount;
2854 }
2855 else if (arg_pt != NULL)
2856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002857 // If the dict was bound automatically the result is also
2858 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859 pt->pt_dict = arg_pt->pt_dict;
2860 pt->pt_auto = arg_pt->pt_auto;
2861 if (pt->pt_dict != NULL)
2862 ++pt->pt_dict->dv_refcount;
2863 }
2864
2865 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002866 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2867 {
2868 pt->pt_func = arg_pt->pt_func;
2869 func_ptr_ref(pt->pt_func);
2870 vim_free(name);
2871 }
2872 else if (is_funcref)
2873 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002874 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875 func_ptr_ref(pt->pt_func);
2876 vim_free(name);
2877 }
2878 else
2879 {
2880 pt->pt_name = name;
2881 func_ref(name);
2882 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002883 }
2884 rettv->v_type = VAR_PARTIAL;
2885 rettv->vval.v_partial = pt;
2886 }
2887 else
2888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 rettv->v_type = VAR_FUNC;
2891 rettv->vval.v_string = name;
2892 func_ref(name);
2893 }
2894 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002895theend:
2896 vim_free(trans_name);
2897}
2898
2899/*
2900 * "funcref()" function
2901 */
2902 static void
2903f_funcref(typval_T *argvars, typval_T *rettv)
2904{
2905 common_function(argvars, rettv, TRUE);
2906}
2907
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002908 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002909ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002910{
2911 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2912 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002913 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002914}
2915
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002916/*
2917 * "function()" function
2918 */
2919 static void
2920f_function(typval_T *argvars, typval_T *rettv)
2921{
2922 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923}
2924
2925/*
2926 * "garbagecollect()" function
2927 */
2928 static void
2929f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2930{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002931 // This is postponed until we are back at the toplevel, because we may be
2932 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 want_garbage_collect = TRUE;
2934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002935 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 garbage_collect_at_exit = TRUE;
2937}
2938
2939/*
2940 * "get()" function
2941 */
2942 static void
2943f_get(typval_T *argvars, typval_T *rettv)
2944{
2945 listitem_T *li;
2946 list_T *l;
2947 dictitem_T *di;
2948 dict_T *d;
2949 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002950 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002952 if (argvars[0].v_type == VAR_BLOB)
2953 {
2954 int error = FALSE;
2955 int idx = tv_get_number_chk(&argvars[1], &error);
2956
2957 if (!error)
2958 {
2959 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002960 if (idx < 0)
2961 idx = blob_len(argvars[0].vval.v_blob) + idx;
2962 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2963 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002964 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002965 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002966 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002967 tv = rettv;
2968 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002969 }
2970 }
2971 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 {
2973 if ((l = argvars[0].vval.v_list) != NULL)
2974 {
2975 int error = FALSE;
2976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (!error && li != NULL)
2979 tv = &li->li_tv;
2980 }
2981 }
2982 else if (argvars[0].v_type == VAR_DICT)
2983 {
2984 if ((d = argvars[0].vval.v_dict) != NULL)
2985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002986 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 if (di != NULL)
2988 tv = &di->di_tv;
2989 }
2990 }
2991 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2992 {
2993 partial_T *pt;
2994 partial_T fref_pt;
2995
2996 if (argvars[0].v_type == VAR_PARTIAL)
2997 pt = argvars[0].vval.v_partial;
2998 else
2999 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003000 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 fref_pt.pt_name = argvars[0].vval.v_string;
3002 pt = &fref_pt;
3003 }
3004
3005 if (pt != NULL)
3006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003007 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003008 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009
3010 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3011 {
3012 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003013 n = partial_name(pt);
3014 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 rettv->vval.v_string = NULL;
3016 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003017 {
3018 rettv->vval.v_string = vim_strsave(n);
3019 if (rettv->v_type == VAR_FUNC)
3020 func_ref(rettv->vval.v_string);
3021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 }
3023 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003024 {
3025 what_is_dict = TRUE;
3026 if (pt->pt_dict != NULL)
3027 rettv_dict_set(rettv, pt->pt_dict);
3028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 else if (STRCMP(what, "args") == 0)
3030 {
3031 rettv->v_type = VAR_LIST;
3032 if (rettv_list_alloc(rettv) == OK)
3033 {
3034 int i;
3035
3036 for (i = 0; i < pt->pt_argc; ++i)
3037 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3038 }
3039 }
3040 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003041 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003042
3043 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3044 // third argument
3045 if (!what_is_dict)
3046 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 }
3048 }
3049 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003050 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051
3052 if (tv == NULL)
3053 {
3054 if (argvars[2].v_type != VAR_UNKNOWN)
3055 copy_tv(&argvars[2], rettv);
3056 }
3057 else
3058 copy_tv(tv, rettv);
3059}
3060
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003061/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003062 * "getchangelist()" function
3063 */
3064 static void
3065f_getchangelist(typval_T *argvars, typval_T *rettv)
3066{
3067#ifdef FEAT_JUMPLIST
3068 buf_T *buf;
3069 int i;
3070 list_T *l;
3071 dict_T *d;
3072#endif
3073
3074 if (rettv_list_alloc(rettv) != OK)
3075 return;
3076
3077#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003078 if (argvars[0].v_type == VAR_UNKNOWN)
3079 buf = curbuf;
3080 else
3081 {
3082 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3083 ++emsg_off;
3084 buf = tv_get_buf(&argvars[0], FALSE);
3085 --emsg_off;
3086 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003087 if (buf == NULL)
3088 return;
3089
3090 l = list_alloc();
3091 if (l == NULL)
3092 return;
3093
3094 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3095 return;
3096 /*
3097 * The current window change list index tracks only the position in the
3098 * current buffer change list. For other buffers, use the change list
3099 * length as the current index.
3100 */
3101 list_append_number(rettv->vval.v_list,
3102 (varnumber_T)((buf == curwin->w_buffer)
3103 ? curwin->w_changelistidx : buf->b_changelistlen));
3104
3105 for (i = 0; i < buf->b_changelistlen; ++i)
3106 {
3107 if (buf->b_changelist[i].lnum == 0)
3108 continue;
3109 if ((d = dict_alloc()) == NULL)
3110 return;
3111 if (list_append_dict(l, d) == FAIL)
3112 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003113 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3114 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003115 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003116 }
3117#endif
3118}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119
3120/*
3121 * "getcharsearch()" function
3122 */
3123 static void
3124f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3125{
3126 if (rettv_dict_alloc(rettv) != FAIL)
3127 {
3128 dict_T *dict = rettv->vval.v_dict;
3129
Bram Moolenaare0be1672018-07-08 16:50:37 +02003130 dict_add_string(dict, "char", last_csearch());
3131 dict_add_number(dict, "forward", last_csearch_forward());
3132 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 }
3134}
3135
3136/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003137 * "getenv()" function
3138 */
3139 static void
3140f_getenv(typval_T *argvars, typval_T *rettv)
3141{
3142 int mustfree = FALSE;
3143 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3144
3145 if (p == NULL)
3146 {
3147 rettv->v_type = VAR_SPECIAL;
3148 rettv->vval.v_number = VVAL_NULL;
3149 return;
3150 }
3151 if (!mustfree)
3152 p = vim_strsave(p);
3153 rettv->vval.v_string = p;
3154 rettv->v_type = VAR_STRING;
3155}
3156
3157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 * "getfontname()" function
3159 */
3160 static void
3161f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3162{
3163 rettv->v_type = VAR_STRING;
3164 rettv->vval.v_string = NULL;
3165#ifdef FEAT_GUI
3166 if (gui.in_use)
3167 {
3168 GuiFont font;
3169 char_u *name = NULL;
3170
3171 if (argvars[0].v_type == VAR_UNKNOWN)
3172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003173 // Get the "Normal" font. Either the name saved by
3174 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 font = gui.norm_font;
3176 name = hl_get_font_name();
3177 }
3178 else
3179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003180 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182 return;
3183 font = gui_mch_get_font(name, FALSE);
3184 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 }
3187 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3188 if (argvars[0].v_type != VAR_UNKNOWN)
3189 gui_mch_free_font(font);
3190 }
3191#endif
3192}
3193
3194/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003195 * "getjumplist()" function
3196 */
3197 static void
3198f_getjumplist(typval_T *argvars, typval_T *rettv)
3199{
3200#ifdef FEAT_JUMPLIST
3201 win_T *wp;
3202 int i;
3203 list_T *l;
3204 dict_T *d;
3205#endif
3206
3207 if (rettv_list_alloc(rettv) != OK)
3208 return;
3209
3210#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003211 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003212 if (wp == NULL)
3213 return;
3214
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003215 cleanup_jumplist(wp, TRUE);
3216
Bram Moolenaar4f505882018-02-10 21:06:32 +01003217 l = list_alloc();
3218 if (l == NULL)
3219 return;
3220
3221 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3222 return;
3223 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3224
3225 for (i = 0; i < wp->w_jumplistlen; ++i)
3226 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003227 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3228 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003229 if ((d = dict_alloc()) == NULL)
3230 return;
3231 if (list_append_dict(l, d) == FAIL)
3232 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003233 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3234 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003237 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003238 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003239 }
3240#endif
3241}
3242
3243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 * "getpid()" function
3245 */
3246 static void
3247f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3248{
3249 rettv->vval.v_number = mch_get_pid();
3250}
3251
3252 static void
3253getpos_both(
3254 typval_T *argvars,
3255 typval_T *rettv,
3256 int getcurpos)
3257{
3258 pos_T *fp;
3259 list_T *l;
3260 int fnum = -1;
3261
3262 if (rettv_list_alloc(rettv) == OK)
3263 {
3264 l = rettv->vval.v_list;
3265 if (getcurpos)
3266 fp = &curwin->w_cursor;
3267 else
3268 fp = var2fpos(&argvars[0], TRUE, &fnum);
3269 if (fnum != -1)
3270 list_append_number(l, (varnumber_T)fnum);
3271 else
3272 list_append_number(l, (varnumber_T)0);
3273 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3274 : (varnumber_T)0);
3275 list_append_number(l, (fp != NULL)
3276 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3277 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003278 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 (varnumber_T)0);
3280 if (getcurpos)
3281 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003282 int save_set_curswant = curwin->w_set_curswant;
3283 colnr_T save_curswant = curwin->w_curswant;
3284 colnr_T save_virtcol = curwin->w_virtcol;
3285
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 update_curswant();
3287 list_append_number(l, curwin->w_curswant == MAXCOL ?
3288 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003289
3290 // Do not change "curswant", as it is unexpected that a get
3291 // function has a side effect.
3292 if (save_set_curswant)
3293 {
3294 curwin->w_set_curswant = save_set_curswant;
3295 curwin->w_curswant = save_curswant;
3296 curwin->w_virtcol = save_virtcol;
3297 curwin->w_valid &= ~VALID_VIRTCOL;
3298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
3300 }
3301 else
3302 rettv->vval.v_number = FALSE;
3303}
3304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305/*
3306 * "getcurpos()" function
3307 */
3308 static void
3309f_getcurpos(typval_T *argvars, typval_T *rettv)
3310{
3311 getpos_both(argvars, rettv, TRUE);
3312}
3313
3314/*
3315 * "getpos(string)" function
3316 */
3317 static void
3318f_getpos(typval_T *argvars, typval_T *rettv)
3319{
3320 getpos_both(argvars, rettv, FALSE);
3321}
3322
3323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 * "getreg()" function
3325 */
3326 static void
3327f_getreg(typval_T *argvars, typval_T *rettv)
3328{
3329 char_u *strregname;
3330 int regname;
3331 int arg2 = FALSE;
3332 int return_list = FALSE;
3333 int error = FALSE;
3334
3335 if (argvars[0].v_type != VAR_UNKNOWN)
3336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 error = strregname == NULL;
3339 if (argvars[1].v_type != VAR_UNKNOWN)
3340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003341 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003343 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 }
3345 }
3346 else
3347 strregname = get_vim_var_str(VV_REG);
3348
3349 if (error)
3350 return;
3351
3352 regname = (strregname == NULL ? '"' : *strregname);
3353 if (regname == 0)
3354 regname = '"';
3355
3356 if (return_list)
3357 {
3358 rettv->v_type = VAR_LIST;
3359 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3360 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3361 if (rettv->vval.v_list == NULL)
3362 (void)rettv_list_alloc(rettv);
3363 else
3364 ++rettv->vval.v_list->lv_refcount;
3365 }
3366 else
3367 {
3368 rettv->v_type = VAR_STRING;
3369 rettv->vval.v_string = get_reg_contents(regname,
3370 arg2 ? GREG_EXPR_SRC : 0);
3371 }
3372}
3373
3374/*
3375 * "getregtype()" function
3376 */
3377 static void
3378f_getregtype(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *strregname;
3381 int regname;
3382 char_u buf[NUMBUFLEN + 2];
3383 long reglen = 0;
3384
3385 if (argvars[0].v_type != VAR_UNKNOWN)
3386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003387 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 {
3390 rettv->v_type = VAR_STRING;
3391 rettv->vval.v_string = NULL;
3392 return;
3393 }
3394 }
3395 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 strregname = get_vim_var_str(VV_REG);
3398
3399 regname = (strregname == NULL ? '"' : *strregname);
3400 if (regname == 0)
3401 regname = '"';
3402
3403 buf[0] = NUL;
3404 buf[1] = NUL;
3405 switch (get_reg_type(regname, &reglen))
3406 {
3407 case MLINE: buf[0] = 'V'; break;
3408 case MCHAR: buf[0] = 'v'; break;
3409 case MBLOCK:
3410 buf[0] = Ctrl_V;
3411 sprintf((char *)buf + 1, "%ld", reglen + 1);
3412 break;
3413 }
3414 rettv->v_type = VAR_STRING;
3415 rettv->vval.v_string = vim_strsave(buf);
3416}
3417
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003418/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003419 * "gettagstack()" function
3420 */
3421 static void
3422f_gettagstack(typval_T *argvars, typval_T *rettv)
3423{
3424 win_T *wp = curwin; // default is current window
3425
3426 if (rettv_dict_alloc(rettv) != OK)
3427 return;
3428
3429 if (argvars[0].v_type != VAR_UNKNOWN)
3430 {
3431 wp = find_win_by_nr_or_id(&argvars[0]);
3432 if (wp == NULL)
3433 return;
3434 }
3435
3436 get_tagstack(wp, rettv->vval.v_dict);
3437}
3438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003439// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440#include "version.h"
3441
3442/*
3443 * "has()" function
3444 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003445 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446f_has(typval_T *argvars, typval_T *rettv)
3447{
3448 int i;
3449 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003450 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003452 typedef struct {
3453 char *name;
3454 short present;
3455 } has_item_T;
3456 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003458 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003460 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003461#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003462 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003463#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003464 },
3465 {"arp",
3466#if defined(AMIGA) && defined(FEAT_ARP)
3467 1
3468#else
3469 0
3470#endif
3471 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003472 {"haiku",
3473#ifdef __HAIKU__
3474 1
3475#else
3476 0
3477#endif
3478 },
3479 {"bsd",
3480#if defined(BSD) && !defined(MACOS_X)
3481 1
3482#else
3483 0
3484#endif
3485 },
3486 {"hpux",
3487#ifdef hpux
3488 1
3489#else
3490 0
3491#endif
3492 },
3493 {"linux",
3494#ifdef __linux__
3495 1
3496#else
3497 0
3498#endif
3499 },
3500 {"mac", // Mac OS X (and, once, Mac OS Classic)
3501#ifdef MACOS_X
3502 1
3503#else
3504 0
3505#endif
3506 },
3507 {"osx", // Mac OS X
3508#ifdef MACOS_X
3509 1
3510#else
3511 0
3512#endif
3513 },
3514 {"macunix", // Mac OS X, with the darwin feature
3515#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3516 1
3517#else
3518 0
3519#endif
3520 },
3521 {"osxdarwin", // synonym for macunix
3522#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3523 1
3524#else
3525 0
3526#endif
3527 },
3528 {"qnx",
3529#ifdef __QNX__
3530 1
3531#else
3532 0
3533#endif
3534 },
3535 {"sun",
3536#ifdef SUN_SYSTEM
3537 1
3538#else
3539 0
3540#endif
3541 },
3542 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003544 1
3545#else
3546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003548 },
3549 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003551 1
3552#else
3553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003555 },
3556 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003557#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003558 1
3559#else
3560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003562 },
3563 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003564#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003565 1
3566#else
3567 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003569 },
3570 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003571#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003572 1
3573#else
3574 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003576 },
3577 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003579 1
3580#else
3581 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003583 },
3584 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003586 1
3587#else
3588 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003590 },
3591 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003592#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003593 1
3594#else
3595 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003596#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003597 },
3598 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003600 1
3601#else
3602 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003604 },
3605 {"autocmd", 1},
3606 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003607#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003608 1
3609#else
3610 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003611#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003612 },
3613 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003614#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003615 1
3616#else
3617 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003618#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003619 },
3620 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003621#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003622 1
3623#else
3624 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003626 },
3627 {"balloon_multiline",
3628#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3629 // MS-Windows requires runtime check, see below
3630 1
3631#else
3632 0
3633#endif
3634 },
3635 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003636#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003637 1
3638#else
3639 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003640#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003641 },
3642 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003644 1
3645#else
3646 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003648 },
3649 {"all_builtin_terms",
3650#if defined(ALL_BUILTIN_TCAPS)
3651 1
3652#else
3653 0
3654#endif
3655 },
3656 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003658 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003660 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003662 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003664 },
3665 {"byte_offset",
3666#ifdef FEAT_BYTEOFF
3667 1
3668#else
3669 0
3670#endif
3671 },
3672 {"channel",
3673#ifdef FEAT_JOB_CHANNEL
3674 1
3675#else
3676 0
3677#endif
3678 },
3679 {"cindent",
3680#ifdef FEAT_CINDENT
3681 1
3682#else
3683 0
3684#endif
3685 },
3686 {"clientserver",
3687#ifdef FEAT_CLIENTSERVER
3688 1
3689#else
3690 0
3691#endif
3692 },
3693 {"clipboard",
3694#ifdef FEAT_CLIPBOARD
3695 1
3696#else
3697 0
3698#endif
3699 },
3700 {"cmdline_compl", 1},
3701 {"cmdline_hist", 1},
3702 {"comments", 1},
3703 {"conceal",
3704#ifdef FEAT_CONCEAL
3705 1
3706#else
3707 0
3708#endif
3709 },
3710 {"cryptv",
3711#ifdef FEAT_CRYPT
3712 1
3713#else
3714 0
3715#endif
3716 },
3717 {"crypt-blowfish",
3718#ifdef FEAT_CRYPT
3719 1
3720#else
3721 0
3722#endif
3723 },
3724 {"crypt-blowfish2",
3725#ifdef FEAT_CRYPT
3726 1
3727#else
3728 0
3729#endif
3730 },
3731 {"cscope",
3732#ifdef FEAT_CSCOPE
3733 1
3734#else
3735 0
3736#endif
3737 },
3738 {"cursorbind", 1},
3739 {"cursorshape",
3740#ifdef CURSOR_SHAPE
3741 1
3742#else
3743 0
3744#endif
3745 },
3746 {"debug",
3747#ifdef DEBUG
3748 1
3749#else
3750 0
3751#endif
3752 },
3753 {"dialog_con",
3754#ifdef FEAT_CON_DIALOG
3755 1
3756#else
3757 0
3758#endif
3759 },
3760 {"dialog_gui",
3761#ifdef FEAT_GUI_DIALOG
3762 1
3763#else
3764 0
3765#endif
3766 },
3767 {"diff",
3768#ifdef FEAT_DIFF
3769 1
3770#else
3771 0
3772#endif
3773 },
3774 {"digraphs",
3775#ifdef FEAT_DIGRAPHS
3776 1
3777#else
3778 0
3779#endif
3780 },
3781 {"directx",
3782#ifdef FEAT_DIRECTX
3783 1
3784#else
3785 0
3786#endif
3787 },
3788 {"dnd",
3789#ifdef FEAT_DND
3790 1
3791#else
3792 0
3793#endif
3794 },
3795 {"emacs_tags",
3796#ifdef FEAT_EMACS_TAGS
3797 1
3798#else
3799 0
3800#endif
3801 },
3802 {"eval", 1}, // always present, of course!
3803 {"ex_extra", 1}, // graduated feature
3804 {"extra_search",
3805#ifdef FEAT_SEARCH_EXTRA
3806 1
3807#else
3808 0
3809#endif
3810 },
3811 {"file_in_path",
3812#ifdef FEAT_SEARCHPATH
3813 1
3814#else
3815 0
3816#endif
3817 },
3818 {"filterpipe",
3819#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3820 1
3821#else
3822 0
3823#endif
3824 },
3825 {"find_in_path",
3826#ifdef FEAT_FIND_ID
3827 1
3828#else
3829 0
3830#endif
3831 },
3832 {"float",
3833#ifdef FEAT_FLOAT
3834 1
3835#else
3836 0
3837#endif
3838 },
3839 {"folding",
3840#ifdef FEAT_FOLDING
3841 1
3842#else
3843 0
3844#endif
3845 },
3846 {"footer",
3847#ifdef FEAT_FOOTER
3848 1
3849#else
3850 0
3851#endif
3852 },
3853 {"fork",
3854#if !defined(USE_SYSTEM) && defined(UNIX)
3855 1
3856#else
3857 0
3858#endif
3859 },
3860 {"gettext",
3861#ifdef FEAT_GETTEXT
3862 1
3863#else
3864 0
3865#endif
3866 },
3867 {"gui",
3868#ifdef FEAT_GUI
3869 1
3870#else
3871 0
3872#endif
3873 },
3874 {"gui_neXtaw",
3875#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3876 1
3877#else
3878 0
3879#endif
3880 },
3881 {"gui_athena",
3882#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3883 1
3884#else
3885 0
3886#endif
3887 },
3888 {"gui_gtk",
3889#ifdef FEAT_GUI_GTK
3890 1
3891#else
3892 0
3893#endif
3894 },
3895 {"gui_gtk2",
3896#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3897 1
3898#else
3899 0
3900#endif
3901 },
3902 {"gui_gtk3",
3903#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3904 1
3905#else
3906 0
3907#endif
3908 },
3909 {"gui_gnome",
3910#ifdef FEAT_GUI_GNOME
3911 1
3912#else
3913 0
3914#endif
3915 },
3916 {"gui_haiku",
3917#ifdef FEAT_GUI_HAIKU
3918 1
3919#else
3920 0
3921#endif
3922 },
3923 {"gui_mac",
3924#ifdef FEAT_GUI_MAC
3925 1
3926#else
3927 0
3928#endif
3929 },
3930 {"gui_motif",
3931#ifdef FEAT_GUI_MOTIF
3932 1
3933#else
3934 0
3935#endif
3936 },
3937 {"gui_photon",
3938#ifdef FEAT_GUI_PHOTON
3939 1
3940#else
3941 0
3942#endif
3943 },
3944 {"gui_win32",
3945#ifdef FEAT_GUI_MSWIN
3946 1
3947#else
3948 0
3949#endif
3950 },
3951 {"iconv",
3952#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3953 1
3954#else
3955 0
3956#endif
3957 },
3958 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003959 {"ipv6",
3960#ifdef FEAT_IPV6
3961 1
3962#else
3963 0
3964#endif
3965 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003966 {"job",
3967#ifdef FEAT_JOB_CHANNEL
3968 1
3969#else
3970 0
3971#endif
3972 },
3973 {"jumplist",
3974#ifdef FEAT_JUMPLIST
3975 1
3976#else
3977 0
3978#endif
3979 },
3980 {"keymap",
3981#ifdef FEAT_KEYMAP
3982 1
3983#else
3984 0
3985#endif
3986 },
3987 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3988 {"langmap",
3989#ifdef FEAT_LANGMAP
3990 1
3991#else
3992 0
3993#endif
3994 },
3995 {"libcall",
3996#ifdef FEAT_LIBCALL
3997 1
3998#else
3999 0
4000#endif
4001 },
4002 {"linebreak",
4003#ifdef FEAT_LINEBREAK
4004 1
4005#else
4006 0
4007#endif
4008 },
4009 {"lispindent",
4010#ifdef FEAT_LISP
4011 1
4012#else
4013 0
4014#endif
4015 },
4016 {"listcmds", 1},
4017 {"localmap", 1},
4018 {"lua",
4019#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4020 1
4021#else
4022 0
4023#endif
4024 },
4025 {"menu",
4026#ifdef FEAT_MENU
4027 1
4028#else
4029 0
4030#endif
4031 },
4032 {"mksession",
4033#ifdef FEAT_SESSION
4034 1
4035#else
4036 0
4037#endif
4038 },
4039 {"modify_fname", 1},
4040 {"mouse", 1},
4041 {"mouseshape",
4042#ifdef FEAT_MOUSESHAPE
4043 1
4044#else
4045 0
4046#endif
4047 },
4048 {"mouse_dec",
4049#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4050 1
4051#else
4052 0
4053#endif
4054 },
4055 {"mouse_gpm",
4056#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4057 1
4058#else
4059 0
4060#endif
4061 },
4062 {"mouse_jsbterm",
4063#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4064 1
4065#else
4066 0
4067#endif
4068 },
4069 {"mouse_netterm",
4070#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4071 1
4072#else
4073 0
4074#endif
4075 },
4076 {"mouse_pterm",
4077#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4078 1
4079#else
4080 0
4081#endif
4082 },
4083 {"mouse_sgr",
4084#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4085 1
4086#else
4087 0
4088#endif
4089 },
4090 {"mouse_sysmouse",
4091#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4092 1
4093#else
4094 0
4095#endif
4096 },
4097 {"mouse_urxvt",
4098#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4099 1
4100#else
4101 0
4102#endif
4103 },
4104 {"mouse_xterm",
4105#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4106 1
4107#else
4108 0
4109#endif
4110 },
4111 {"multi_byte", 1},
4112 {"multi_byte_ime",
4113#ifdef FEAT_MBYTE_IME
4114 1
4115#else
4116 0
4117#endif
4118 },
4119 {"multi_lang",
4120#ifdef FEAT_MULTI_LANG
4121 1
4122#else
4123 0
4124#endif
4125 },
4126 {"mzscheme",
4127#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4128 1
4129#else
4130 0
4131#endif
4132 },
4133 {"num64", 1},
4134 {"ole",
4135#ifdef FEAT_OLE
4136 1
4137#else
4138 0
4139#endif
4140 },
4141 {"packages",
4142#ifdef FEAT_EVAL
4143 1
4144#else
4145 0
4146#endif
4147 },
4148 {"path_extra",
4149#ifdef FEAT_PATH_EXTRA
4150 1
4151#else
4152 0
4153#endif
4154 },
4155 {"perl",
4156#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4157 1
4158#else
4159 0
4160#endif
4161 },
4162 {"persistent_undo",
4163#ifdef FEAT_PERSISTENT_UNDO
4164 1
4165#else
4166 0
4167#endif
4168 },
4169 {"python_compiled",
4170#if defined(FEAT_PYTHON)
4171 1
4172#else
4173 0
4174#endif
4175 },
4176 {"python_dynamic",
4177#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4178 1
4179#else
4180 0
4181#endif
4182 },
4183 {"python",
4184#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4185 1
4186#else
4187 0
4188#endif
4189 },
4190 {"pythonx",
4191#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4192 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4193 1
4194#else
4195 0
4196#endif
4197 },
4198 {"python3_compiled",
4199#if defined(FEAT_PYTHON3)
4200 1
4201#else
4202 0
4203#endif
4204 },
4205 {"python3_dynamic",
4206#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4207 1
4208#else
4209 0
4210#endif
4211 },
4212 {"python3",
4213#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4214 1
4215#else
4216 0
4217#endif
4218 },
4219 {"popupwin",
4220#ifdef FEAT_PROP_POPUP
4221 1
4222#else
4223 0
4224#endif
4225 },
4226 {"postscript",
4227#ifdef FEAT_POSTSCRIPT
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"printer",
4234#ifdef FEAT_PRINTER
4235 1
4236#else
4237 0
4238#endif
4239 },
4240 {"profile",
4241#ifdef FEAT_PROFILE
4242 1
4243#else
4244 0
4245#endif
4246 },
4247 {"reltime",
4248#ifdef FEAT_RELTIME
4249 1
4250#else
4251 0
4252#endif
4253 },
4254 {"quickfix",
4255#ifdef FEAT_QUICKFIX
4256 1
4257#else
4258 0
4259#endif
4260 },
4261 {"rightleft",
4262#ifdef FEAT_RIGHTLEFT
4263 1
4264#else
4265 0
4266#endif
4267 },
4268 {"ruby",
4269#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4270 1
4271#else
4272 0
4273#endif
4274 },
4275 {"scrollbind", 1},
4276 {"showcmd",
4277#ifdef FEAT_CMDL_INFO
4278 1
4279#else
4280 0
4281#endif
4282 },
4283 {"cmdline_info",
4284#ifdef FEAT_CMDL_INFO
4285 1
4286#else
4287 0
4288#endif
4289 },
4290 {"signs",
4291#ifdef FEAT_SIGNS
4292 1
4293#else
4294 0
4295#endif
4296 },
4297 {"smartindent",
4298#ifdef FEAT_SMARTINDENT
4299 1
4300#else
4301 0
4302#endif
4303 },
4304 {"startuptime",
4305#ifdef STARTUPTIME
4306 1
4307#else
4308 0
4309#endif
4310 },
4311 {"statusline",
4312#ifdef FEAT_STL_OPT
4313 1
4314#else
4315 0
4316#endif
4317 },
4318 {"netbeans_intg",
4319#ifdef FEAT_NETBEANS_INTG
4320 1
4321#else
4322 0
4323#endif
4324 },
4325 {"sound",
4326#ifdef FEAT_SOUND
4327 1
4328#else
4329 0
4330#endif
4331 },
4332 {"spell",
4333#ifdef FEAT_SPELL
4334 1
4335#else
4336 0
4337#endif
4338 },
4339 {"syntax",
4340#ifdef FEAT_SYN_HL
4341 1
4342#else
4343 0
4344#endif
4345 },
4346 {"system",
4347#if defined(USE_SYSTEM) || !defined(UNIX)
4348 1
4349#else
4350 0
4351#endif
4352 },
4353 {"tag_binary",
4354#ifdef FEAT_TAG_BINS
4355 1
4356#else
4357 0
4358#endif
4359 },
4360 {"tcl",
4361#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4362 1
4363#else
4364 0
4365#endif
4366 },
4367 {"termguicolors",
4368#ifdef FEAT_TERMGUICOLORS
4369 1
4370#else
4371 0
4372#endif
4373 },
4374 {"terminal",
4375#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"terminfo",
4382#ifdef TERMINFO
4383 1
4384#else
4385 0
4386#endif
4387 },
4388 {"termresponse",
4389#ifdef FEAT_TERMRESPONSE
4390 1
4391#else
4392 0
4393#endif
4394 },
4395 {"textobjects",
4396#ifdef FEAT_TEXTOBJ
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"textprop",
4403#ifdef FEAT_PROP_POPUP
4404 1
4405#else
4406 0
4407#endif
4408 },
4409 {"tgetent",
4410#ifdef HAVE_TGETENT
4411 1
4412#else
4413 0
4414#endif
4415 },
4416 {"timers",
4417#ifdef FEAT_TIMERS
4418 1
4419#else
4420 0
4421#endif
4422 },
4423 {"title",
4424#ifdef FEAT_TITLE
4425 1
4426#else
4427 0
4428#endif
4429 },
4430 {"toolbar",
4431#ifdef FEAT_TOOLBAR
4432 1
4433#else
4434 0
4435#endif
4436 },
4437 {"unnamedplus",
4438#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4439 1
4440#else
4441 0
4442#endif
4443 },
4444 {"user-commands", 1}, // was accidentally included in 5.4
4445 {"user_commands", 1},
4446 {"vartabs",
4447#ifdef FEAT_VARTABS
4448 1
4449#else
4450 0
4451#endif
4452 },
4453 {"vertsplit", 1},
4454 {"viminfo",
4455#ifdef FEAT_VIMINFO
4456 1
4457#else
4458 0
4459#endif
4460 },
4461 {"vimscript-1", 1},
4462 {"vimscript-2", 1},
4463 {"vimscript-3", 1},
4464 {"vimscript-4", 1},
4465 {"virtualedit", 1},
4466 {"visual", 1},
4467 {"visualextra", 1},
4468 {"vreplace", 1},
4469 {"vtp",
4470#ifdef FEAT_VTP
4471 1
4472#else
4473 0
4474#endif
4475 },
4476 {"wildignore",
4477#ifdef FEAT_WILDIGN
4478 1
4479#else
4480 0
4481#endif
4482 },
4483 {"wildmenu",
4484#ifdef FEAT_WILDMENU
4485 1
4486#else
4487 0
4488#endif
4489 },
4490 {"windows", 1},
4491 {"winaltkeys",
4492#ifdef FEAT_WAK
4493 1
4494#else
4495 0
4496#endif
4497 },
4498 {"writebackup",
4499#ifdef FEAT_WRITEBACKUP
4500 1
4501#else
4502 0
4503#endif
4504 },
4505 {"xim",
4506#ifdef FEAT_XIM
4507 1
4508#else
4509 0
4510#endif
4511 },
4512 {"xfontset",
4513#ifdef FEAT_XFONTSET
4514 1
4515#else
4516 0
4517#endif
4518 },
4519 {"xpm",
4520#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"xpm_w32", // for backward compatibility
4527#ifdef FEAT_XPM_W32
4528 1
4529#else
4530 0
4531#endif
4532 },
4533 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004534#ifdef USE_XSMP
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 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541#ifdef USE_XSMP_INTERACT
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_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548#ifdef FEAT_XCLIPBOARD
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 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#ifdef FEAT_XTERM_SAVE
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 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 1
4564#else
4565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 },
4568 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569 };
4570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004571 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004572 for (i = 0; has_list[i].name != NULL; ++i)
4573 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004574 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004575 x = TRUE;
4576 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577 break;
4578 }
4579
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 // features also in has_list[] but sometimes enabled at runtime
4581 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004583 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004584 {
4585 // intentionally empty
4586 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004587#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588 else if (STRICMP(name, "balloon_multiline") == 0)
4589 n = multiline_balloon_available();
4590#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004591#ifdef VIMDLL
4592 else if (STRICMP(name, "filterpipe") == 0)
4593 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594#endif
4595#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4596 else if (STRICMP(name, "iconv") == 0)
4597 n = iconv_enabled(FALSE);
4598#endif
4599#ifdef DYNAMIC_LUA
4600 else if (STRICMP(name, "lua") == 0)
4601 n = lua_enabled(FALSE);
4602#endif
4603#ifdef DYNAMIC_MZSCHEME
4604 else if (STRICMP(name, "mzscheme") == 0)
4605 n = mzscheme_enabled(FALSE);
4606#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004607#ifdef DYNAMIC_PERL
4608 else if (STRICMP(name, "perl") == 0)
4609 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004610#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611#ifdef DYNAMIC_PYTHON
4612 else if (STRICMP(name, "python") == 0)
4613 n = python_enabled(FALSE);
4614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615#ifdef DYNAMIC_PYTHON3
4616 else if (STRICMP(name, "python3") == 0)
4617 n = python3_enabled(FALSE);
4618#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004619#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4620 else if (STRICMP(name, "pythonx") == 0)
4621 {
4622# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4623 if (p_pyx == 0)
4624 n = python3_enabled(FALSE) || python_enabled(FALSE);
4625 else if (p_pyx == 3)
4626 n = python3_enabled(FALSE);
4627 else if (p_pyx == 2)
4628 n = python_enabled(FALSE);
4629# elif defined(DYNAMIC_PYTHON)
4630 n = python_enabled(FALSE);
4631# elif defined(DYNAMIC_PYTHON3)
4632 n = python3_enabled(FALSE);
4633# endif
4634 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004635#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004636#ifdef DYNAMIC_RUBY
4637 else if (STRICMP(name, "ruby") == 0)
4638 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004639#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004640#ifdef DYNAMIC_TCL
4641 else if (STRICMP(name, "tcl") == 0)
4642 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004643#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004644#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004645 else if (STRICMP(name, "terminal") == 0)
4646 n = terminal_enabled();
4647#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004648 }
4649
Bram Moolenaar79296512020-03-22 16:17:14 +01004650 // features not in has_list[]
4651 if (x == FALSE)
4652 {
4653 if (STRNICMP(name, "patch", 5) == 0)
4654 {
4655 x = TRUE;
4656 if (name[5] == '-'
4657 && STRLEN(name) >= 11
4658 && vim_isdigit(name[6])
4659 && vim_isdigit(name[8])
4660 && vim_isdigit(name[10]))
4661 {
4662 int major = atoi((char *)name + 6);
4663 int minor = atoi((char *)name + 8);
4664
4665 // Expect "patch-9.9.01234".
4666 n = (major < VIM_VERSION_MAJOR
4667 || (major == VIM_VERSION_MAJOR
4668 && (minor < VIM_VERSION_MINOR
4669 || (minor == VIM_VERSION_MINOR
4670 && has_patch(atoi((char *)name + 10))))));
4671 }
4672 else
4673 n = has_patch(atoi((char *)name + 5));
4674 }
4675 else if (STRICMP(name, "vim_starting") == 0)
4676 {
4677 x = TRUE;
4678 n = (starting != 0);
4679 }
4680 else if (STRICMP(name, "ttyin") == 0)
4681 {
4682 x = TRUE;
4683 n = mch_input_isatty();
4684 }
4685 else if (STRICMP(name, "ttyout") == 0)
4686 {
4687 x = TRUE;
4688 n = stdout_isatty;
4689 }
4690 else if (STRICMP(name, "multi_byte_encoding") == 0)
4691 {
4692 x = TRUE;
4693 n = has_mbyte;
4694 }
4695 else if (STRICMP(name, "gui_running") == 0)
4696 {
4697 x = TRUE;
4698#ifdef FEAT_GUI
4699 n = (gui.in_use || gui.starting);
4700#endif
4701 }
4702 else if (STRICMP(name, "browse") == 0)
4703 {
4704 x = TRUE;
4705#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4706 n = gui.in_use; // gui_mch_browse() works when GUI is running
4707#endif
4708 }
4709 else if (STRICMP(name, "syntax_items") == 0)
4710 {
4711 x = TRUE;
4712#ifdef FEAT_SYN_HL
4713 n = syntax_present(curwin);
4714#endif
4715 }
4716 else if (STRICMP(name, "vcon") == 0)
4717 {
4718 x = TRUE;
4719#ifdef FEAT_VTP
4720 n = is_term_win32() && has_vtp_working();
4721#endif
4722 }
4723 else if (STRICMP(name, "netbeans_enabled") == 0)
4724 {
4725 x = TRUE;
4726#ifdef FEAT_NETBEANS_INTG
4727 n = netbeans_active();
4728#endif
4729 }
4730 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4731 {
4732 x = TRUE;
4733#ifdef FEAT_MOUSE_GPM
4734 n = gpm_enabled();
4735#endif
4736 }
4737 else if (STRICMP(name, "conpty") == 0)
4738 {
4739 x = TRUE;
4740#if defined(FEAT_TERMINAL) && defined(MSWIN)
4741 n = use_conpty();
4742#endif
4743 }
4744 else if (STRICMP(name, "clipboard_working") == 0)
4745 {
4746 x = TRUE;
4747#ifdef FEAT_CLIPBOARD
4748 n = clip_star.available;
4749#endif
4750 }
4751 }
4752
4753 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4754 // return whether feature could ever be enabled
4755 rettv->vval.v_number = x;
4756 else
4757 // return whether feature is enabled
4758 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759}
4760
4761/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 * "haslocaldir()" function
4763 */
4764 static void
4765f_haslocaldir(typval_T *argvars, typval_T *rettv)
4766{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004767 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768 win_T *wp = NULL;
4769
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004770 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4771
4772 // Check for window-local and tab-local directories
4773 if (wp != NULL && wp->w_localdir != NULL)
4774 rettv->vval.v_number = 1;
4775 else if (tp != NULL && tp->tp_localdir != NULL)
4776 rettv->vval.v_number = 2;
4777 else
4778 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779}
4780
4781/*
4782 * "hasmapto()" function
4783 */
4784 static void
4785f_hasmapto(typval_T *argvars, typval_T *rettv)
4786{
4787 char_u *name;
4788 char_u *mode;
4789 char_u buf[NUMBUFLEN];
4790 int abbr = FALSE;
4791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 if (argvars[1].v_type == VAR_UNKNOWN)
4794 mode = (char_u *)"nvo";
4795 else
4796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004797 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004798 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004799 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004800 }
4801
4802 if (map_to_exists(name, mode, abbr))
4803 rettv->vval.v_number = TRUE;
4804 else
4805 rettv->vval.v_number = FALSE;
4806}
4807
4808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004809 * "highlightID(name)" function
4810 */
4811 static void
4812f_hlID(typval_T *argvars, typval_T *rettv)
4813{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004814 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815}
4816
4817/*
4818 * "highlight_exists()" function
4819 */
4820 static void
4821f_hlexists(typval_T *argvars, typval_T *rettv)
4822{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824}
4825
4826/*
4827 * "hostname()" function
4828 */
4829 static void
4830f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4831{
4832 char_u hostname[256];
4833
4834 mch_get_host_name(hostname, 256);
4835 rettv->v_type = VAR_STRING;
4836 rettv->vval.v_string = vim_strsave(hostname);
4837}
4838
4839/*
4840 * iconv() function
4841 */
4842 static void
4843f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4844{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004845 char_u buf1[NUMBUFLEN];
4846 char_u buf2[NUMBUFLEN];
4847 char_u *from, *to, *str;
4848 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849
4850 rettv->v_type = VAR_STRING;
4851 rettv->vval.v_string = NULL;
4852
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004853 str = tv_get_string(&argvars[0]);
4854 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4855 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004856 vimconv.vc_type = CONV_NONE;
4857 convert_setup(&vimconv, from, to);
4858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004859 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004860 if (vimconv.vc_type == CONV_NONE)
4861 rettv->vval.v_string = vim_strsave(str);
4862 else
4863 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4864
4865 convert_setup(&vimconv, NULL, NULL);
4866 vim_free(from);
4867 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004868}
4869
4870/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004871 * "index()" function
4872 */
4873 static void
4874f_index(typval_T *argvars, typval_T *rettv)
4875{
4876 list_T *l;
4877 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004878 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879 long idx = 0;
4880 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004881 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882
4883 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004884 if (argvars[0].v_type == VAR_BLOB)
4885 {
4886 typval_T tv;
4887 int start = 0;
4888
4889 if (argvars[2].v_type != VAR_UNKNOWN)
4890 {
4891 start = tv_get_number_chk(&argvars[2], &error);
4892 if (error)
4893 return;
4894 }
4895 b = argvars[0].vval.v_blob;
4896 if (b == NULL)
4897 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004898 if (start < 0)
4899 {
4900 start = blob_len(b) + start;
4901 if (start < 0)
4902 start = 0;
4903 }
4904
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004905 for (idx = start; idx < blob_len(b); ++idx)
4906 {
4907 tv.v_type = VAR_NUMBER;
4908 tv.vval.v_number = blob_get(b, idx);
4909 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4910 {
4911 rettv->vval.v_number = idx;
4912 return;
4913 }
4914 }
4915 return;
4916 }
4917 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004919 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004920 return;
4921 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923 l = argvars[0].vval.v_list;
4924 if (l != NULL)
4925 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004926 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 item = l->lv_first;
4928 if (argvars[2].v_type != VAR_UNKNOWN)
4929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004930 // Start at specified item. Use the cached index that list_find()
4931 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004932 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004933 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004934 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004935 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 if (error)
4937 item = NULL;
4938 }
4939
4940 for ( ; item != NULL; item = item->li_next, ++idx)
4941 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4942 {
4943 rettv->vval.v_number = idx;
4944 break;
4945 }
4946 }
4947}
4948
4949static int inputsecret_flag = 0;
4950
4951/*
4952 * "input()" function
4953 * Also handles inputsecret() when inputsecret is set.
4954 */
4955 static void
4956f_input(typval_T *argvars, typval_T *rettv)
4957{
4958 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4959}
4960
4961/*
4962 * "inputdialog()" function
4963 */
4964 static void
4965f_inputdialog(typval_T *argvars, typval_T *rettv)
4966{
4967#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004968 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4970 {
4971 char_u *message;
4972 char_u buf[NUMBUFLEN];
4973 char_u *defstr = (char_u *)"";
4974
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004975 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004976 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004977 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4979 else
4980 IObuff[0] = NUL;
4981 if (message != NULL && defstr != NULL
4982 && do_dialog(VIM_QUESTION, NULL, message,
4983 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4984 rettv->vval.v_string = vim_strsave(IObuff);
4985 else
4986 {
4987 if (message != NULL && defstr != NULL
4988 && argvars[1].v_type != VAR_UNKNOWN
4989 && argvars[2].v_type != VAR_UNKNOWN)
4990 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004991 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 else
4993 rettv->vval.v_string = NULL;
4994 }
4995 rettv->v_type = VAR_STRING;
4996 }
4997 else
4998#endif
4999 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5000}
5001
5002/*
5003 * "inputlist()" function
5004 */
5005 static void
5006f_inputlist(typval_T *argvars, typval_T *rettv)
5007{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005008 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 listitem_T *li;
5010 int selected;
5011 int mouse_used;
5012
5013#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005014 // While starting up, there is no place to enter text. When running tests
5015 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005016 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 return;
5018#endif
5019 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005021 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 return;
5023 }
5024
5025 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005026 msg_row = Rows - 1; // for when 'cmdheight' > 1
5027 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028 msg_scroll = TRUE;
5029 msg_clr_eos();
5030
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005031 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005032 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005033 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005035 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005036 msg_putchar('\n');
5037 }
5038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040 selected = prompt_for_number(&mouse_used);
5041 if (mouse_used)
5042 selected -= lines_left;
5043
5044 rettv->vval.v_number = selected;
5045}
5046
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5048
5049/*
5050 * "inputrestore()" function
5051 */
5052 static void
5053f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5054{
5055 if (ga_userinput.ga_len > 0)
5056 {
5057 --ga_userinput.ga_len;
5058 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5059 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005061 }
5062 else if (p_verbose > 1)
5063 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005064 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005066 }
5067}
5068
5069/*
5070 * "inputsave()" function
5071 */
5072 static void
5073f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5074{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005076 if (ga_grow(&ga_userinput, 1) == OK)
5077 {
5078 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5079 + ga_userinput.ga_len);
5080 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005081 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005082 }
5083 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005084 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085}
5086
5087/*
5088 * "inputsecret()" function
5089 */
5090 static void
5091f_inputsecret(typval_T *argvars, typval_T *rettv)
5092{
5093 ++cmdline_star;
5094 ++inputsecret_flag;
5095 f_input(argvars, rettv);
5096 --cmdline_star;
5097 --inputsecret_flag;
5098}
5099
5100/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005101 * "interrupt()" function
5102 */
5103 static void
5104f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5105{
5106 got_int = TRUE;
5107}
5108
5109/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 * "invert(expr)" function
5111 */
5112 static void
5113f_invert(typval_T *argvars, typval_T *rettv)
5114{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005115 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116}
5117
5118/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005119 * "islocked()" function
5120 */
5121 static void
5122f_islocked(typval_T *argvars, typval_T *rettv)
5123{
5124 lval_T lv;
5125 char_u *end;
5126 dictitem_T *di;
5127
5128 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005129 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005130 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005131 if (end != NULL && lv.ll_name != NULL)
5132 {
5133 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005134 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 else
5136 {
5137 if (lv.ll_tv == NULL)
5138 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005139 di = find_var(lv.ll_name, NULL, TRUE);
5140 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005142 // Consider a variable locked when:
5143 // 1. the variable itself is locked
5144 // 2. the value of the variable is locked.
5145 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005146 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5147 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 }
5149 }
5150 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005151 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005153 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005156 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5157 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005158 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5160 }
5161 }
5162
5163 clear_lval(&lv);
5164}
5165
5166#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5167/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005168 * "isinf()" function
5169 */
5170 static void
5171f_isinf(typval_T *argvars, typval_T *rettv)
5172{
5173 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5174 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5175}
5176
5177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005178 * "isnan()" function
5179 */
5180 static void
5181f_isnan(typval_T *argvars, typval_T *rettv)
5182{
5183 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5184 && isnan(argvars[0].vval.v_float);
5185}
5186#endif
5187
5188/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 * "last_buffer_nr()" function.
5190 */
5191 static void
5192f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5193{
5194 int n = 0;
5195 buf_T *buf;
5196
Bram Moolenaar29323592016-07-24 22:04:11 +02005197 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005198 if (n < buf->b_fnum)
5199 n = buf->b_fnum;
5200
5201 rettv->vval.v_number = n;
5202}
5203
5204/*
5205 * "len()" function
5206 */
5207 static void
5208f_len(typval_T *argvars, typval_T *rettv)
5209{
5210 switch (argvars[0].v_type)
5211 {
5212 case VAR_STRING:
5213 case VAR_NUMBER:
5214 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005215 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005216 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005217 case VAR_BLOB:
5218 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5219 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005220 case VAR_LIST:
5221 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5222 break;
5223 case VAR_DICT:
5224 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5225 break;
5226 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005227 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005229 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 case VAR_SPECIAL:
5231 case VAR_FLOAT:
5232 case VAR_FUNC:
5233 case VAR_PARTIAL:
5234 case VAR_JOB:
5235 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005236 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 break;
5238 }
5239}
5240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005242libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243{
5244#ifdef FEAT_LIBCALL
5245 char_u *string_in;
5246 char_u **string_result;
5247 int nr_result;
5248#endif
5249
5250 rettv->v_type = type;
5251 if (type != VAR_NUMBER)
5252 rettv->vval.v_string = NULL;
5253
5254 if (check_restricted() || check_secure())
5255 return;
5256
5257#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005258 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5260 {
5261 string_in = NULL;
5262 if (argvars[2].v_type == VAR_STRING)
5263 string_in = argvars[2].vval.v_string;
5264 if (type == VAR_NUMBER)
5265 string_result = NULL;
5266 else
5267 string_result = &rettv->vval.v_string;
5268 if (mch_libcall(argvars[0].vval.v_string,
5269 argvars[1].vval.v_string,
5270 string_in,
5271 argvars[2].vval.v_number,
5272 string_result,
5273 &nr_result) == OK
5274 && type == VAR_NUMBER)
5275 rettv->vval.v_number = nr_result;
5276 }
5277#endif
5278}
5279
5280/*
5281 * "libcall()" function
5282 */
5283 static void
5284f_libcall(typval_T *argvars, typval_T *rettv)
5285{
5286 libcall_common(argvars, rettv, VAR_STRING);
5287}
5288
5289/*
5290 * "libcallnr()" function
5291 */
5292 static void
5293f_libcallnr(typval_T *argvars, typval_T *rettv)
5294{
5295 libcall_common(argvars, rettv, VAR_NUMBER);
5296}
5297
5298/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005299 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 */
5301 static void
5302f_line(typval_T *argvars, typval_T *rettv)
5303{
5304 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005305 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005307 int id;
5308 tabpage_T *tp;
5309 win_T *wp;
5310 win_T *save_curwin;
5311 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005313 if (argvars[1].v_type != VAR_UNKNOWN)
5314 {
5315 // use window specified in the second argument
5316 id = (int)tv_get_number(&argvars[1]);
5317 wp = win_id2wp_tp(id, &tp);
5318 if (wp != NULL && tp != NULL)
5319 {
5320 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5321 == OK)
5322 {
5323 check_cursor();
5324 fp = var2fpos(&argvars[0], TRUE, &fnum);
5325 }
5326 restore_win_noblock(save_curwin, save_curtab, TRUE);
5327 }
5328 }
5329 else
5330 // use current window
5331 fp = var2fpos(&argvars[0], TRUE, &fnum);
5332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333 if (fp != NULL)
5334 lnum = fp->lnum;
5335 rettv->vval.v_number = lnum;
5336}
5337
5338/*
5339 * "line2byte(lnum)" function
5340 */
5341 static void
5342f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5343{
5344#ifndef FEAT_BYTEOFF
5345 rettv->vval.v_number = -1;
5346#else
5347 linenr_T lnum;
5348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005349 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5351 rettv->vval.v_number = -1;
5352 else
5353 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5354 if (rettv->vval.v_number >= 0)
5355 ++rettv->vval.v_number;
5356#endif
5357}
5358
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359#ifdef FEAT_FLOAT
5360/*
5361 * "log()" function
5362 */
5363 static void
5364f_log(typval_T *argvars, typval_T *rettv)
5365{
5366 float_T f = 0.0;
5367
5368 rettv->v_type = VAR_FLOAT;
5369 if (get_float_arg(argvars, &f) == OK)
5370 rettv->vval.v_float = log(f);
5371 else
5372 rettv->vval.v_float = 0.0;
5373}
5374
5375/*
5376 * "log10()" function
5377 */
5378 static void
5379f_log10(typval_T *argvars, typval_T *rettv)
5380{
5381 float_T f = 0.0;
5382
5383 rettv->v_type = VAR_FLOAT;
5384 if (get_float_arg(argvars, &f) == OK)
5385 rettv->vval.v_float = log10(f);
5386 else
5387 rettv->vval.v_float = 0.0;
5388}
5389#endif
5390
5391#ifdef FEAT_LUA
5392/*
5393 * "luaeval()" function
5394 */
5395 static void
5396f_luaeval(typval_T *argvars, typval_T *rettv)
5397{
5398 char_u *str;
5399 char_u buf[NUMBUFLEN];
5400
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005401 if (check_restricted() || check_secure())
5402 return;
5403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005404 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005405 do_luaeval(str, argvars + 1, rettv);
5406}
5407#endif
5408
5409/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005410 * "maparg()" function
5411 */
5412 static void
5413f_maparg(typval_T *argvars, typval_T *rettv)
5414{
5415 get_maparg(argvars, rettv, TRUE);
5416}
5417
5418/*
5419 * "mapcheck()" function
5420 */
5421 static void
5422f_mapcheck(typval_T *argvars, typval_T *rettv)
5423{
5424 get_maparg(argvars, rettv, FALSE);
5425}
5426
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005427typedef enum
5428{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 MATCH_END, // matchend()
5430 MATCH_MATCH, // match()
5431 MATCH_STR, // matchstr()
5432 MATCH_LIST, // matchlist()
5433 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005434} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435
5436 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005437find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438{
5439 char_u *str = NULL;
5440 long len = 0;
5441 char_u *expr = NULL;
5442 char_u *pat;
5443 regmatch_T regmatch;
5444 char_u patbuf[NUMBUFLEN];
5445 char_u strbuf[NUMBUFLEN];
5446 char_u *save_cpo;
5447 long start = 0;
5448 long nth = 1;
5449 colnr_T startcol = 0;
5450 int match = 0;
5451 list_T *l = NULL;
5452 listitem_T *li = NULL;
5453 long idx = 0;
5454 char_u *tofree = NULL;
5455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005456 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 save_cpo = p_cpo;
5458 p_cpo = (char_u *)"";
5459
5460 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005461 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 // type MATCH_LIST: return empty list when there are no matches.
5464 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005465 if (rettv_list_alloc(rettv) == FAIL)
5466 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005467 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468 && (list_append_string(rettv->vval.v_list,
5469 (char_u *)"", 0) == FAIL
5470 || list_append_number(rettv->vval.v_list,
5471 (varnumber_T)-1) == FAIL
5472 || list_append_number(rettv->vval.v_list,
5473 (varnumber_T)-1) == FAIL
5474 || list_append_number(rettv->vval.v_list,
5475 (varnumber_T)-1) == FAIL))
5476 {
5477 list_free(rettv->vval.v_list);
5478 rettv->vval.v_list = NULL;
5479 goto theend;
5480 }
5481 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005482 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005483 {
5484 rettv->v_type = VAR_STRING;
5485 rettv->vval.v_string = NULL;
5486 }
5487
5488 if (argvars[0].v_type == VAR_LIST)
5489 {
5490 if ((l = argvars[0].vval.v_list) == NULL)
5491 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005492 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 li = l->lv_first;
5494 }
5495 else
5496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005497 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 len = (long)STRLEN(str);
5499 }
5500
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005501 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502 if (pat == NULL)
5503 goto theend;
5504
5505 if (argvars[2].v_type != VAR_UNKNOWN)
5506 {
5507 int error = FALSE;
5508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005509 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 if (error)
5511 goto theend;
5512 if (l != NULL)
5513 {
5514 li = list_find(l, start);
5515 if (li == NULL)
5516 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005517 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518 }
5519 else
5520 {
5521 if (start < 0)
5522 start = 0;
5523 if (start > len)
5524 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // When "count" argument is there ignore matches before "start",
5526 // otherwise skip part of the string. Differs when pattern is "^"
5527 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528 if (argvars[3].v_type != VAR_UNKNOWN)
5529 startcol = start;
5530 else
5531 {
5532 str += start;
5533 len -= start;
5534 }
5535 }
5536
5537 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005538 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539 if (error)
5540 goto theend;
5541 }
5542
5543 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5544 if (regmatch.regprog != NULL)
5545 {
5546 regmatch.rm_ic = p_ic;
5547
5548 for (;;)
5549 {
5550 if (l != NULL)
5551 {
5552 if (li == NULL)
5553 {
5554 match = FALSE;
5555 break;
5556 }
5557 vim_free(tofree);
5558 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5559 if (str == NULL)
5560 break;
5561 }
5562
5563 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5564
5565 if (match && --nth <= 0)
5566 break;
5567 if (l == NULL && !match)
5568 break;
5569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 if (l != NULL)
5572 {
5573 li = li->li_next;
5574 ++idx;
5575 }
5576 else
5577 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005578 startcol = (colnr_T)(regmatch.startp[0]
5579 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005580 if (startcol > (colnr_T)len
5581 || str + startcol <= regmatch.startp[0])
5582 {
5583 match = FALSE;
5584 break;
5585 }
5586 }
5587 }
5588
5589 if (match)
5590 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005591 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 {
5593 listitem_T *li1 = rettv->vval.v_list->lv_first;
5594 listitem_T *li2 = li1->li_next;
5595 listitem_T *li3 = li2->li_next;
5596 listitem_T *li4 = li3->li_next;
5597
5598 vim_free(li1->li_tv.vval.v_string);
5599 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005600 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005601 li3->li_tv.vval.v_number =
5602 (varnumber_T)(regmatch.startp[0] - expr);
5603 li4->li_tv.vval.v_number =
5604 (varnumber_T)(regmatch.endp[0] - expr);
5605 if (l != NULL)
5606 li2->li_tv.vval.v_number = (varnumber_T)idx;
5607 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005608 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 {
5610 int i;
5611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005612 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 for (i = 0; i < NSUBEXP; ++i)
5614 {
5615 if (regmatch.endp[i] == NULL)
5616 {
5617 if (list_append_string(rettv->vval.v_list,
5618 (char_u *)"", 0) == FAIL)
5619 break;
5620 }
5621 else if (list_append_string(rettv->vval.v_list,
5622 regmatch.startp[i],
5623 (int)(regmatch.endp[i] - regmatch.startp[i]))
5624 == FAIL)
5625 break;
5626 }
5627 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005628 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 if (l != NULL)
5632 copy_tv(&li->li_tv, rettv);
5633 else
5634 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005635 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 }
5637 else if (l != NULL)
5638 rettv->vval.v_number = idx;
5639 else
5640 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005641 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005642 rettv->vval.v_number =
5643 (varnumber_T)(regmatch.startp[0] - str);
5644 else
5645 rettv->vval.v_number =
5646 (varnumber_T)(regmatch.endp[0] - str);
5647 rettv->vval.v_number += (varnumber_T)(str - expr);
5648 }
5649 }
5650 vim_regfree(regmatch.regprog);
5651 }
5652
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005653theend:
5654 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005655 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 listitem_remove(rettv->vval.v_list,
5657 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658 vim_free(tofree);
5659 p_cpo = save_cpo;
5660}
5661
5662/*
5663 * "match()" function
5664 */
5665 static void
5666f_match(typval_T *argvars, typval_T *rettv)
5667{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005668 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669}
5670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671/*
5672 * "matchend()" function
5673 */
5674 static void
5675f_matchend(typval_T *argvars, typval_T *rettv)
5676{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005677 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678}
5679
5680/*
5681 * "matchlist()" function
5682 */
5683 static void
5684f_matchlist(typval_T *argvars, typval_T *rettv)
5685{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005686 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687}
5688
5689/*
5690 * "matchstr()" function
5691 */
5692 static void
5693f_matchstr(typval_T *argvars, typval_T *rettv)
5694{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005695 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696}
5697
5698/*
5699 * "matchstrpos()" function
5700 */
5701 static void
5702f_matchstrpos(typval_T *argvars, typval_T *rettv)
5703{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005704 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005705}
5706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005707 static void
5708max_min(typval_T *argvars, typval_T *rettv, int domax)
5709{
5710 varnumber_T n = 0;
5711 varnumber_T i;
5712 int error = FALSE;
5713
5714 if (argvars[0].v_type == VAR_LIST)
5715 {
5716 list_T *l;
5717 listitem_T *li;
5718
5719 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005720 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005721 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005722 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005724 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5725 n = l->lv_u.nonmat.lv_start;
5726 else
5727 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5728 * l->lv_u.nonmat.lv_stride;
5729 }
5730 else
5731 {
5732 li = l->lv_first;
5733 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005734 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005735 n = tv_get_number_chk(&li->li_tv, &error);
5736 for (;;)
5737 {
5738 li = li->li_next;
5739 if (li == NULL)
5740 break;
5741 i = tv_get_number_chk(&li->li_tv, &error);
5742 if (domax ? i > n : i < n)
5743 n = i;
5744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 }
5746 }
5747 }
5748 }
5749 else if (argvars[0].v_type == VAR_DICT)
5750 {
5751 dict_T *d;
5752 int first = TRUE;
5753 hashitem_T *hi;
5754 int todo;
5755
5756 d = argvars[0].vval.v_dict;
5757 if (d != NULL)
5758 {
5759 todo = (int)d->dv_hashtab.ht_used;
5760 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5761 {
5762 if (!HASHITEM_EMPTY(hi))
5763 {
5764 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005765 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766 if (first)
5767 {
5768 n = i;
5769 first = FALSE;
5770 }
5771 else if (domax ? i > n : i < n)
5772 n = i;
5773 }
5774 }
5775 }
5776 }
5777 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005778 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005779 rettv->vval.v_number = error ? 0 : n;
5780}
5781
5782/*
5783 * "max()" function
5784 */
5785 static void
5786f_max(typval_T *argvars, typval_T *rettv)
5787{
5788 max_min(argvars, rettv, TRUE);
5789}
5790
5791/*
5792 * "min()" function
5793 */
5794 static void
5795f_min(typval_T *argvars, typval_T *rettv)
5796{
5797 max_min(argvars, rettv, FALSE);
5798}
5799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800#if defined(FEAT_MZSCHEME) || defined(PROTO)
5801/*
5802 * "mzeval()" function
5803 */
5804 static void
5805f_mzeval(typval_T *argvars, typval_T *rettv)
5806{
5807 char_u *str;
5808 char_u buf[NUMBUFLEN];
5809
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005810 if (check_restricted() || check_secure())
5811 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005812 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005813 do_mzeval(str, rettv);
5814}
5815
5816 void
5817mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5818{
5819 typval_T argvars[3];
5820
5821 argvars[0].v_type = VAR_STRING;
5822 argvars[0].vval.v_string = name;
5823 copy_tv(args, &argvars[1]);
5824 argvars[2].v_type = VAR_UNKNOWN;
5825 f_call(argvars, rettv);
5826 clear_tv(&argvars[1]);
5827}
5828#endif
5829
5830/*
5831 * "nextnonblank()" function
5832 */
5833 static void
5834f_nextnonblank(typval_T *argvars, typval_T *rettv)
5835{
5836 linenr_T lnum;
5837
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005838 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005839 {
5840 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5841 {
5842 lnum = 0;
5843 break;
5844 }
5845 if (*skipwhite(ml_get(lnum)) != NUL)
5846 break;
5847 }
5848 rettv->vval.v_number = lnum;
5849}
5850
5851/*
5852 * "nr2char()" function
5853 */
5854 static void
5855f_nr2char(typval_T *argvars, typval_T *rettv)
5856{
5857 char_u buf[NUMBUFLEN];
5858
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 if (has_mbyte)
5860 {
5861 int utf8 = 0;
5862
5863 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005864 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005866 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005868 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 }
5870 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005872 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 buf[1] = NUL;
5874 }
5875 rettv->v_type = VAR_STRING;
5876 rettv->vval.v_string = vim_strsave(buf);
5877}
5878
5879/*
5880 * "or(expr, expr)" function
5881 */
5882 static void
5883f_or(typval_T *argvars, typval_T *rettv)
5884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5886 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005887}
5888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889#ifdef FEAT_PERL
5890/*
5891 * "perleval()" function
5892 */
5893 static void
5894f_perleval(typval_T *argvars, typval_T *rettv)
5895{
5896 char_u *str;
5897 char_u buf[NUMBUFLEN];
5898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005899 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 do_perleval(str, rettv);
5901}
5902#endif
5903
5904#ifdef FEAT_FLOAT
5905/*
5906 * "pow()" function
5907 */
5908 static void
5909f_pow(typval_T *argvars, typval_T *rettv)
5910{
5911 float_T fx = 0.0, fy = 0.0;
5912
5913 rettv->v_type = VAR_FLOAT;
5914 if (get_float_arg(argvars, &fx) == OK
5915 && get_float_arg(&argvars[1], &fy) == OK)
5916 rettv->vval.v_float = pow(fx, fy);
5917 else
5918 rettv->vval.v_float = 0.0;
5919}
5920#endif
5921
5922/*
5923 * "prevnonblank()" function
5924 */
5925 static void
5926f_prevnonblank(typval_T *argvars, typval_T *rettv)
5927{
5928 linenr_T lnum;
5929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005930 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5932 lnum = 0;
5933 else
5934 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5935 --lnum;
5936 rettv->vval.v_number = lnum;
5937}
5938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005939// This dummy va_list is here because:
5940// - passing a NULL pointer doesn't work when va_list isn't a pointer
5941// - locally in the function results in a "used before set" warning
5942// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943static va_list ap;
5944
5945/*
5946 * "printf()" function
5947 */
5948 static void
5949f_printf(typval_T *argvars, typval_T *rettv)
5950{
5951 char_u buf[NUMBUFLEN];
5952 int len;
5953 char_u *s;
5954 int saved_did_emsg = did_emsg;
5955 char *fmt;
5956
5957 rettv->v_type = VAR_STRING;
5958 rettv->vval.v_string = NULL;
5959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005960 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005962 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005963 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005964 if (!did_emsg)
5965 {
5966 s = alloc(len + 1);
5967 if (s != NULL)
5968 {
5969 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005970 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5971 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 }
5973 }
5974 did_emsg |= saved_did_emsg;
5975}
5976
5977/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005978 * "pum_getpos()" function
5979 */
5980 static void
5981f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5982{
5983 if (rettv_dict_alloc(rettv) != OK)
5984 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005985 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005986}
5987
5988/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 * "pumvisible()" function
5990 */
5991 static void
5992f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5993{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 if (pum_visible())
5995 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996}
5997
5998#ifdef FEAT_PYTHON3
5999/*
6000 * "py3eval()" function
6001 */
6002 static void
6003f_py3eval(typval_T *argvars, typval_T *rettv)
6004{
6005 char_u *str;
6006 char_u buf[NUMBUFLEN];
6007
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006008 if (check_restricted() || check_secure())
6009 return;
6010
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006011 if (p_pyx == 0)
6012 p_pyx = 3;
6013
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 do_py3eval(str, rettv);
6016}
6017#endif
6018
6019#ifdef FEAT_PYTHON
6020/*
6021 * "pyeval()" function
6022 */
6023 static void
6024f_pyeval(typval_T *argvars, typval_T *rettv)
6025{
6026 char_u *str;
6027 char_u buf[NUMBUFLEN];
6028
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006029 if (check_restricted() || check_secure())
6030 return;
6031
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006032 if (p_pyx == 0)
6033 p_pyx = 2;
6034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006035 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006036 do_pyeval(str, rettv);
6037}
6038#endif
6039
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006040#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6041/*
6042 * "pyxeval()" function
6043 */
6044 static void
6045f_pyxeval(typval_T *argvars, typval_T *rettv)
6046{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006047 if (check_restricted() || check_secure())
6048 return;
6049
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006050# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6051 init_pyxversion();
6052 if (p_pyx == 2)
6053 f_pyeval(argvars, rettv);
6054 else
6055 f_py3eval(argvars, rettv);
6056# elif defined(FEAT_PYTHON)
6057 f_pyeval(argvars, rettv);
6058# elif defined(FEAT_PYTHON3)
6059 f_py3eval(argvars, rettv);
6060# endif
6061}
6062#endif
6063
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006064static UINT32_T srand_seed_for_testing = 0;
6065static int srand_seed_for_testing_is_used = FALSE;
6066
6067 static void
6068f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6069{
6070 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006071 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006072 else
6073 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006074 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6075 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006076 }
6077}
6078
6079 static void
6080init_srand(UINT32_T *x)
6081{
6082#ifndef MSWIN
6083 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6084#endif
6085
6086 if (srand_seed_for_testing_is_used)
6087 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006088 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006089 return;
6090 }
6091#ifndef MSWIN
6092 if (dev_urandom_state != FAIL)
6093 {
6094 int fd = open("/dev/urandom", O_RDONLY);
6095 struct {
6096 union {
6097 UINT32_T number;
6098 char bytes[sizeof(UINT32_T)];
6099 } contents;
6100 } buf;
6101
6102 // Attempt reading /dev/urandom.
6103 if (fd == -1)
6104 dev_urandom_state = FAIL;
6105 else
6106 {
6107 buf.contents.number = 0;
6108 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6109 != sizeof(UINT32_T))
6110 dev_urandom_state = FAIL;
6111 else
6112 {
6113 dev_urandom_state = OK;
6114 *x = buf.contents.number;
6115 }
6116 close(fd);
6117 }
6118 }
6119 if (dev_urandom_state != OK)
6120 // Reading /dev/urandom doesn't work, fall back to time().
6121#endif
6122 *x = vim_time();
6123}
6124
6125#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6126#define SPLITMIX32(x, z) ( \
6127 z = (x += 0x9e3779b9), \
6128 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6129 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6130 z ^ (z >> 16) \
6131 )
6132#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6133 result = ROTL(y * 5, 7) * 9; \
6134 t = y << 9; \
6135 z ^= x; \
6136 w ^= y; \
6137 y ^= z, x ^= w; \
6138 z ^= t; \
6139 w = ROTL(w, 11);
6140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006142 * "rand()" function
6143 */
6144 static void
6145f_rand(typval_T *argvars, typval_T *rettv)
6146{
6147 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006148 static UINT32_T gx, gy, gz, gw;
6149 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006150 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006151 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006152
6153 if (argvars[0].v_type == VAR_UNKNOWN)
6154 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006155 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006156 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006157 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006158 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006159 init_srand(&x);
6160
6161 gx = SPLITMIX32(x, z);
6162 gy = SPLITMIX32(x, z);
6163 gz = SPLITMIX32(x, z);
6164 gw = SPLITMIX32(x, z);
6165 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006166 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006167
6168 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006169 }
6170 else if (argvars[0].v_type == VAR_LIST)
6171 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006172 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006173 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006174 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006175
6176 lx = list_find(l, 0L);
6177 ly = list_find(l, 1L);
6178 lz = list_find(l, 2L);
6179 lw = list_find(l, 3L);
6180 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6181 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6182 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6183 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6184 x = (UINT32_T)lx->li_tv.vval.v_number;
6185 y = (UINT32_T)ly->li_tv.vval.v_number;
6186 z = (UINT32_T)lz->li_tv.vval.v_number;
6187 w = (UINT32_T)lw->li_tv.vval.v_number;
6188
6189 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6190
6191 lx->li_tv.vval.v_number = (varnumber_T)x;
6192 ly->li_tv.vval.v_number = (varnumber_T)y;
6193 lz->li_tv.vval.v_number = (varnumber_T)z;
6194 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006195 }
6196 else
6197 goto theend;
6198
6199 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006200 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006201 return;
6202
6203theend:
6204 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006205 rettv->v_type = VAR_NUMBER;
6206 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006207}
6208
6209/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006210 * "srand()" function
6211 */
6212 static void
6213f_srand(typval_T *argvars, typval_T *rettv)
6214{
6215 UINT32_T x = 0, z;
6216
6217 if (rettv_list_alloc(rettv) == FAIL)
6218 return;
6219 if (argvars[0].v_type == VAR_UNKNOWN)
6220 {
6221 init_srand(&x);
6222 }
6223 else
6224 {
6225 int error = FALSE;
6226
6227 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6228 if (error)
6229 return;
6230 }
6231
6232 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6233 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6234 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6235 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6236}
6237
6238#undef ROTL
6239#undef SPLITMIX32
6240#undef SHUFFLE_XOSHIRO128STARSTAR
6241
6242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243 * "range()" function
6244 */
6245 static void
6246f_range(typval_T *argvars, typval_T *rettv)
6247{
6248 varnumber_T start;
6249 varnumber_T end;
6250 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006251 int error = FALSE;
6252
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 if (argvars[1].v_type == VAR_UNKNOWN)
6255 {
6256 end = start - 1;
6257 start = 0;
6258 }
6259 else
6260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006261 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006263 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 }
6265
6266 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006267 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006269 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006271 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006273 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006274 list_T *list = rettv->vval.v_list;
6275
6276 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006277 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 // be called.
6279 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006280 list->lv_u.nonmat.lv_start = start;
6281 list->lv_u.nonmat.lv_end = end;
6282 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006283 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 }
6285}
6286
6287/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006288 * Materialize "list".
6289 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290 */
6291 void
6292range_list_materialize(list_T *list)
6293{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006294 varnumber_T start = list->lv_u.nonmat.lv_start;
6295 varnumber_T end = list->lv_u.nonmat.lv_end;
6296 int stride = list->lv_u.nonmat.lv_stride;
6297 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006298
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006299 list->lv_first = NULL;
6300 list->lv_u.mat.lv_last = NULL;
6301 list->lv_len = 0;
6302 list->lv_u.mat.lv_idx_item = NULL;
6303 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6304 if (list_append_number(list, (varnumber_T)i) == FAIL)
6305 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306}
6307
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006308/*
6309 * "getreginfo()" function
6310 */
6311 static void
6312f_getreginfo(typval_T *argvars, typval_T *rettv)
6313{
6314 char_u *strregname;
6315 int regname;
6316 char_u buf[NUMBUFLEN + 2];
6317 long reglen = 0;
6318 dict_T *dict;
6319 list_T *list;
6320
6321 if (argvars[0].v_type != VAR_UNKNOWN)
6322 {
6323 strregname = tv_get_string_chk(&argvars[0]);
6324 if (strregname == NULL)
6325 return;
6326 }
6327 else
6328 strregname = get_vim_var_str(VV_REG);
6329
6330 regname = (strregname == NULL ? '"' : *strregname);
6331 if (regname == 0 || regname == '@')
6332 regname = '"';
6333
6334 if (rettv_dict_alloc(rettv) == FAIL)
6335 return;
6336 dict = rettv->vval.v_dict;
6337
6338 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6339 if (list == NULL)
6340 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006341 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006342
6343 buf[0] = NUL;
6344 buf[1] = NUL;
6345 switch (get_reg_type(regname, &reglen))
6346 {
6347 case MLINE: buf[0] = 'V'; break;
6348 case MCHAR: buf[0] = 'v'; break;
6349 case MBLOCK:
6350 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6351 reglen + 1);
6352 break;
6353 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006354 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006355
6356 buf[0] = get_register_name(get_unname_register());
6357 buf[1] = NUL;
6358 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006359 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006360 else
6361 {
6362 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6363
6364 if (item != NULL)
6365 {
6366 item->di_tv.v_type = VAR_SPECIAL;
6367 item->di_tv.vval.v_number = regname == buf[0]
6368 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006369 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006370 }
6371 }
6372}
6373
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006374 static void
6375return_register(int regname, typval_T *rettv)
6376{
6377 char_u buf[2] = {0, 0};
6378
6379 buf[0] = (char_u)regname;
6380 rettv->v_type = VAR_STRING;
6381 rettv->vval.v_string = vim_strsave(buf);
6382}
6383
6384/*
6385 * "reg_executing()" function
6386 */
6387 static void
6388f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6389{
6390 return_register(reg_executing, rettv);
6391}
6392
6393/*
6394 * "reg_recording()" function
6395 */
6396 static void
6397f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6398{
6399 return_register(reg_recording, rettv);
6400}
6401
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006402/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 * "rename({from}, {to})" function
6404 */
6405 static void
6406f_rename(typval_T *argvars, typval_T *rettv)
6407{
6408 char_u buf[NUMBUFLEN];
6409
6410 if (check_restricted() || check_secure())
6411 rettv->vval.v_number = -1;
6412 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006413 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6414 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415}
6416
6417/*
6418 * "repeat()" function
6419 */
6420 static void
6421f_repeat(typval_T *argvars, typval_T *rettv)
6422{
6423 char_u *p;
6424 int n;
6425 int slen;
6426 int len;
6427 char_u *r;
6428 int i;
6429
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006430 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 if (argvars[0].v_type == VAR_LIST)
6432 {
6433 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6434 while (n-- > 0)
6435 if (list_extend(rettv->vval.v_list,
6436 argvars[0].vval.v_list, NULL) == FAIL)
6437 break;
6438 }
6439 else
6440 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006441 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442 rettv->v_type = VAR_STRING;
6443 rettv->vval.v_string = NULL;
6444
6445 slen = (int)STRLEN(p);
6446 len = slen * n;
6447 if (len <= 0)
6448 return;
6449
6450 r = alloc(len + 1);
6451 if (r != NULL)
6452 {
6453 for (i = 0; i < n; i++)
6454 mch_memmove(r + i * slen, p, (size_t)slen);
6455 r[len] = NUL;
6456 }
6457
6458 rettv->vval.v_string = r;
6459 }
6460}
6461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462#define SP_NOMOVE 0x01 // don't move cursor
6463#define SP_REPEAT 0x02 // repeat to find outer pair
6464#define SP_RETCOUNT 0x04 // return matchcount
6465#define SP_SETPCMARK 0x08 // set previous context mark
6466#define SP_START 0x10 // accept match at start position
6467#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6468#define SP_END 0x40 // leave cursor at end of match
6469#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471/*
6472 * Get flags for a search function.
6473 * Possibly sets "p_ws".
6474 * Returns BACKWARD, FORWARD or zero (for an error).
6475 */
6476 static int
6477get_search_arg(typval_T *varp, int *flagsp)
6478{
6479 int dir = FORWARD;
6480 char_u *flags;
6481 char_u nbuf[NUMBUFLEN];
6482 int mask;
6483
6484 if (varp->v_type != VAR_UNKNOWN)
6485 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006486 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489 while (*flags != NUL)
6490 {
6491 switch (*flags)
6492 {
6493 case 'b': dir = BACKWARD; break;
6494 case 'w': p_ws = TRUE; break;
6495 case 'W': p_ws = FALSE; break;
6496 default: mask = 0;
6497 if (flagsp != NULL)
6498 switch (*flags)
6499 {
6500 case 'c': mask = SP_START; break;
6501 case 'e': mask = SP_END; break;
6502 case 'm': mask = SP_RETCOUNT; break;
6503 case 'n': mask = SP_NOMOVE; break;
6504 case 'p': mask = SP_SUBPAT; break;
6505 case 'r': mask = SP_REPEAT; break;
6506 case 's': mask = SP_SETPCMARK; break;
6507 case 'z': mask = SP_COLUMN; break;
6508 }
6509 if (mask == 0)
6510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006511 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 dir = 0;
6513 }
6514 else
6515 *flagsp |= mask;
6516 }
6517 if (dir == 0)
6518 break;
6519 ++flags;
6520 }
6521 }
6522 return dir;
6523}
6524
6525/*
6526 * Shared by search() and searchpos() functions.
6527 */
6528 static int
6529search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6530{
6531 int flags;
6532 char_u *pat;
6533 pos_T pos;
6534 pos_T save_cursor;
6535 int save_p_ws = p_ws;
6536 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006537 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006540 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541 long time_limit = 0;
6542#endif
6543 int options = SEARCH_KEEP;
6544 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006545 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006546 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006547 pos_T firstpos;
6548
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006549 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006550 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 if (dir == 0)
6552 goto theend;
6553 flags = *flagsp;
6554 if (flags & SP_START)
6555 options |= SEARCH_START;
6556 if (flags & SP_END)
6557 options |= SEARCH_END;
6558 if (flags & SP_COLUMN)
6559 options |= SEARCH_COL;
6560
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006561 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006564 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 if (lnum_stop < 0)
6566 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567 if (argvars[3].v_type != VAR_UNKNOWN)
6568 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006569#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006570 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 if (time_limit < 0)
6572 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006574 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 }
6577
6578#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580 profile_setlimit(time_limit, &tm);
6581#endif
6582
6583 /*
6584 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6585 * Check to make sure only those flags are set.
6586 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6587 * flags cannot be set. Check for that condition also.
6588 */
6589 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6590 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006592 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 goto theend;
6594 }
6595
6596 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006597 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006598 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006599 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6600#ifdef FEAT_RELTIME
6601 sia.sa_tm = &tm;
6602#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006603
6604 // Repeat until {skip} returns FALSE.
6605 for (;;)
6606 {
6607 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006608 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006609 // finding the first match again means there is no match where {skip}
6610 // evaluates to zero.
6611 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6612 subpatnum = FAIL;
6613
Bram Moolenaara9c01042020-06-07 14:50:50 +02006614 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006615 // didn't find it or no skip argument
6616 break;
6617 firstpos = pos;
6618
Bram Moolenaara9c01042020-06-07 14:50:50 +02006619 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006620 {
6621 int do_skip;
6622 int err;
6623 pos_T save_pos = curwin->w_cursor;
6624
6625 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006626 err = FALSE;
6627 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006628 curwin->w_cursor = save_pos;
6629 if (err)
6630 {
6631 // Evaluating {skip} caused an error, break here.
6632 subpatnum = FAIL;
6633 break;
6634 }
6635 if (!do_skip)
6636 break;
6637 }
6638 }
6639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 if (subpatnum != FAIL)
6641 {
6642 if (flags & SP_SUBPAT)
6643 retval = subpatnum;
6644 else
6645 retval = pos.lnum;
6646 if (flags & SP_SETPCMARK)
6647 setpcmark();
6648 curwin->w_cursor = pos;
6649 if (match_pos != NULL)
6650 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 match_pos->lnum = pos.lnum;
6653 match_pos->col = pos.col + 1;
6654 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 // "/$" will put the cursor after the end of the line, may need to
6656 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657 check_cursor();
6658 }
6659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006660 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661 if (flags & SP_NOMOVE)
6662 curwin->w_cursor = save_cursor;
6663 else
6664 curwin->w_set_curswant = TRUE;
6665theend:
6666 p_ws = save_p_ws;
6667
6668 return retval;
6669}
6670
6671#ifdef FEAT_FLOAT
6672
6673/*
6674 * round() is not in C90, use ceil() or floor() instead.
6675 */
6676 float_T
6677vim_round(float_T f)
6678{
6679 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6680}
6681
6682/*
6683 * "round({float})" function
6684 */
6685 static void
6686f_round(typval_T *argvars, typval_T *rettv)
6687{
6688 float_T f = 0.0;
6689
6690 rettv->v_type = VAR_FLOAT;
6691 if (get_float_arg(argvars, &f) == OK)
6692 rettv->vval.v_float = vim_round(f);
6693 else
6694 rettv->vval.v_float = 0.0;
6695}
6696#endif
6697
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006698#ifdef FEAT_RUBY
6699/*
6700 * "rubyeval()" function
6701 */
6702 static void
6703f_rubyeval(typval_T *argvars, typval_T *rettv)
6704{
6705 char_u *str;
6706 char_u buf[NUMBUFLEN];
6707
6708 str = tv_get_string_buf(&argvars[0], buf);
6709 do_rubyeval(str, rettv);
6710}
6711#endif
6712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713/*
6714 * "screenattr()" function
6715 */
6716 static void
6717f_screenattr(typval_T *argvars, typval_T *rettv)
6718{
6719 int row;
6720 int col;
6721 int c;
6722
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006723 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6724 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725 if (row < 0 || row >= screen_Rows
6726 || col < 0 || col >= screen_Columns)
6727 c = -1;
6728 else
6729 c = ScreenAttrs[LineOffset[row] + col];
6730 rettv->vval.v_number = c;
6731}
6732
6733/*
6734 * "screenchar()" function
6735 */
6736 static void
6737f_screenchar(typval_T *argvars, typval_T *rettv)
6738{
6739 int row;
6740 int col;
6741 int off;
6742 int c;
6743
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006744 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6745 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006746 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 c = -1;
6748 else
6749 {
6750 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751 if (enc_utf8 && ScreenLinesUC[off] != 0)
6752 c = ScreenLinesUC[off];
6753 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 c = ScreenLines[off];
6755 }
6756 rettv->vval.v_number = c;
6757}
6758
6759/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006760 * "screenchars()" function
6761 */
6762 static void
6763f_screenchars(typval_T *argvars, typval_T *rettv)
6764{
6765 int row;
6766 int col;
6767 int off;
6768 int c;
6769 int i;
6770
6771 if (rettv_list_alloc(rettv) == FAIL)
6772 return;
6773 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6774 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6775 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6776 return;
6777
6778 off = LineOffset[row] + col;
6779 if (enc_utf8 && ScreenLinesUC[off] != 0)
6780 c = ScreenLinesUC[off];
6781 else
6782 c = ScreenLines[off];
6783 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6784
6785 if (enc_utf8)
6786
6787 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6788 list_append_number(rettv->vval.v_list,
6789 (varnumber_T)ScreenLinesC[i][off]);
6790}
6791
6792/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793 * "screencol()" function
6794 *
6795 * First column is 1 to be consistent with virtcol().
6796 */
6797 static void
6798f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6799{
6800 rettv->vval.v_number = screen_screencol() + 1;
6801}
6802
6803/*
6804 * "screenrow()" function
6805 */
6806 static void
6807f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6808{
6809 rettv->vval.v_number = screen_screenrow() + 1;
6810}
6811
6812/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006813 * "screenstring()" function
6814 */
6815 static void
6816f_screenstring(typval_T *argvars, typval_T *rettv)
6817{
6818 int row;
6819 int col;
6820 int off;
6821 int c;
6822 int i;
6823 char_u buf[MB_MAXBYTES + 1];
6824 int buflen = 0;
6825
6826 rettv->vval.v_string = NULL;
6827 rettv->v_type = VAR_STRING;
6828
6829 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6830 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6831 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6832 return;
6833
6834 off = LineOffset[row] + col;
6835 if (enc_utf8 && ScreenLinesUC[off] != 0)
6836 c = ScreenLinesUC[off];
6837 else
6838 c = ScreenLines[off];
6839 buflen += mb_char2bytes(c, buf);
6840
6841 if (enc_utf8)
6842 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6843 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6844
6845 buf[buflen] = NUL;
6846 rettv->vval.v_string = vim_strsave(buf);
6847}
6848
6849/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850 * "search()" function
6851 */
6852 static void
6853f_search(typval_T *argvars, typval_T *rettv)
6854{
6855 int flags = 0;
6856
6857 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6858}
6859
6860/*
6861 * "searchdecl()" function
6862 */
6863 static void
6864f_searchdecl(typval_T *argvars, typval_T *rettv)
6865{
6866 int locally = 1;
6867 int thisblock = 0;
6868 int error = FALSE;
6869 char_u *name;
6870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006871 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 if (argvars[1].v_type != VAR_UNKNOWN)
6875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006878 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 }
6880 if (!error && name != NULL)
6881 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6882 locally, thisblock, SEARCH_KEEP) == FAIL;
6883}
6884
6885/*
6886 * Used by searchpair() and searchpairpos()
6887 */
6888 static int
6889searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6890{
6891 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006892 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 int save_p_ws = p_ws;
6894 int dir;
6895 int flags = 0;
6896 char_u nbuf1[NUMBUFLEN];
6897 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006898 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 long lnum_stop = 0;
6900 long time_limit = 0;
6901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006902 // Get the three pattern arguments: start, middle, end. Will result in an
6903 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006904 spat = tv_get_string_chk(&argvars[0]);
6905 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6906 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006910 // Handle the optional fourth argument: flags
6911 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 if (dir == 0)
6913 goto theend;
6914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 // Don't accept SP_END or SP_SUBPAT.
6916 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 if ((flags & (SP_END | SP_SUBPAT)) != 0
6918 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006920 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921 goto theend;
6922 }
6923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006924 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (flags & SP_REPEAT)
6926 p_ws = FALSE;
6927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006928 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 if (argvars[3].v_type == VAR_UNKNOWN
6930 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006931 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 else
6933 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006934 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006935 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 if (argvars[5].v_type != VAR_UNKNOWN)
6938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006942 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006944 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945#ifdef FEAT_RELTIME
6946 if (argvars[6].v_type != VAR_UNKNOWN)
6947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006948 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006951 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006953 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006954 }
6955#endif
6956 }
6957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958
6959 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6960 match_pos, lnum_stop, time_limit);
6961
6962theend:
6963 p_ws = save_p_ws;
6964
6965 return retval;
6966}
6967
6968/*
6969 * "searchpair()" function
6970 */
6971 static void
6972f_searchpair(typval_T *argvars, typval_T *rettv)
6973{
6974 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6975}
6976
6977/*
6978 * "searchpairpos()" function
6979 */
6980 static void
6981f_searchpairpos(typval_T *argvars, typval_T *rettv)
6982{
6983 pos_T match_pos;
6984 int lnum = 0;
6985 int col = 0;
6986
6987 if (rettv_list_alloc(rettv) == FAIL)
6988 return;
6989
6990 if (searchpair_cmn(argvars, &match_pos) > 0)
6991 {
6992 lnum = match_pos.lnum;
6993 col = match_pos.col;
6994 }
6995
6996 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6997 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6998}
6999
7000/*
7001 * Search for a start/middle/end thing.
7002 * Used by searchpair(), see its documentation for the details.
7003 * Returns 0 or -1 for no match,
7004 */
7005 long
7006do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007007 char_u *spat, // start pattern
7008 char_u *mpat, // middle pattern
7009 char_u *epat, // end pattern
7010 int dir, // BACKWARD or FORWARD
7011 typval_T *skip, // skip expression
7012 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007014 linenr_T lnum_stop, // stop at this line if not zero
7015 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016{
7017 char_u *save_cpo;
7018 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7019 long retval = 0;
7020 pos_T pos;
7021 pos_T firstpos;
7022 pos_T foundpos;
7023 pos_T save_cursor;
7024 pos_T save_pos;
7025 int n;
7026 int r;
7027 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007028 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007029 int err;
7030 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007031#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007033#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007035 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 save_cpo = p_cpo;
7037 p_cpo = empty_option;
7038
7039#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007040 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 profile_setlimit(time_limit, &tm);
7042#endif
7043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007044 // Make two search patterns: start/end (pat2, for in nested pairs) and
7045 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007046 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7047 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 if (pat2 == NULL || pat3 == NULL)
7049 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007050 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 if (*mpat == NUL)
7052 STRCPY(pat3, pat2);
7053 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007054 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 spat, epat, mpat);
7056 if (flags & SP_START)
7057 options |= SEARCH_START;
7058
Bram Moolenaar48570482017-10-30 21:48:41 +01007059 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007060 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 save_cursor = curwin->w_cursor;
7063 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007064 CLEAR_POS(&firstpos);
7065 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066 pat = pat3;
7067 for (;;)
7068 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007069 searchit_arg_T sia;
7070
Bram Moolenaara80faa82020-04-12 19:37:17 +02007071 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007072 sia.sa_stop_lnum = lnum_stop;
7073#ifdef FEAT_RELTIME
7074 sia.sa_tm = &tm;
7075#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007076 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007077 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007078 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 break;
7081
7082 if (firstpos.lnum == 0)
7083 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007084 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007086 // Found the same position again. Can happen with a pattern that
7087 // has "\zs" at the end and searching backwards. Advance one
7088 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007089 if (dir == BACKWARD)
7090 decl(&pos);
7091 else
7092 incl(&pos);
7093 }
7094 foundpos = pos;
7095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007096 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 options &= ~SEARCH_START;
7098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007099 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007100 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 {
7102 save_pos = curwin->w_cursor;
7103 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007104 err = FALSE;
7105 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 curwin->w_cursor = save_pos;
7107 if (err)
7108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 curwin->w_cursor = save_cursor;
7111 retval = -1;
7112 break;
7113 }
7114 if (r)
7115 continue;
7116 }
7117
7118 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007120 // Found end when searching backwards or start when searching
7121 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007123 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 }
7125 else
7126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 // Found end when searching forward or start when searching
7128 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007130 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 }
7132
7133 if (nest == 0)
7134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007135 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 if (flags & SP_RETCOUNT)
7137 ++retval;
7138 else
7139 retval = pos.lnum;
7140 if (flags & SP_SETPCMARK)
7141 setpcmark();
7142 curwin->w_cursor = pos;
7143 if (!(flags & SP_REPEAT))
7144 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007145 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 }
7147 }
7148
7149 if (match_pos != NULL)
7150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007151 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007152 match_pos->lnum = curwin->w_cursor.lnum;
7153 match_pos->col = curwin->w_cursor.col + 1;
7154 }
7155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007156 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 if ((flags & SP_NOMOVE) || retval == 0)
7158 curwin->w_cursor = save_cursor;
7159
7160theend:
7161 vim_free(pat2);
7162 vim_free(pat3);
7163 if (p_cpo == empty_option)
7164 p_cpo = save_cpo;
7165 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007166 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 free_string_option(save_cpo);
7168
7169 return retval;
7170}
7171
7172/*
7173 * "searchpos()" function
7174 */
7175 static void
7176f_searchpos(typval_T *argvars, typval_T *rettv)
7177{
7178 pos_T match_pos;
7179 int lnum = 0;
7180 int col = 0;
7181 int n;
7182 int flags = 0;
7183
7184 if (rettv_list_alloc(rettv) == FAIL)
7185 return;
7186
7187 n = search_cmn(argvars, &match_pos, &flags);
7188 if (n > 0)
7189 {
7190 lnum = match_pos.lnum;
7191 col = match_pos.col;
7192 }
7193
7194 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7195 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7196 if (flags & SP_SUBPAT)
7197 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7198}
7199
7200 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7202{
7203 dict_T *d;
7204 dictitem_T *di;
7205 char_u *csearch;
7206
7207 if (argvars[0].v_type != VAR_DICT)
7208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007209 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210 return;
7211 }
7212
7213 if ((d = argvars[0].vval.v_dict) != NULL)
7214 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007215 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216 if (csearch != NULL)
7217 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 if (enc_utf8)
7219 {
7220 int pcc[MAX_MCO];
7221 int c = utfc_ptr2char(csearch, pcc);
7222
7223 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7224 }
7225 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007227 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 }
7229
7230 di = dict_find(d, (char_u *)"forward", -1);
7231 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007232 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 ? FORWARD : BACKWARD);
7234
7235 di = dict_find(d, (char_u *)"until", -1);
7236 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007237 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 }
7239}
7240
7241/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007242 * "setenv()" function
7243 */
7244 static void
7245f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7246{
7247 char_u namebuf[NUMBUFLEN];
7248 char_u valbuf[NUMBUFLEN];
7249 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7250
7251 if (argvars[1].v_type == VAR_SPECIAL
7252 && argvars[1].vval.v_number == VVAL_NULL)
7253 vim_unsetenv(name);
7254 else
7255 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7256}
7257
7258/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007259 * "setfperm({fname}, {mode})" function
7260 */
7261 static void
7262f_setfperm(typval_T *argvars, typval_T *rettv)
7263{
7264 char_u *fname;
7265 char_u modebuf[NUMBUFLEN];
7266 char_u *mode_str;
7267 int i;
7268 int mask;
7269 int mode = 0;
7270
7271 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007272 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 if (fname == NULL)
7274 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007275 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 if (mode_str == NULL)
7277 return;
7278 if (STRLEN(mode_str) != 9)
7279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007280 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 return;
7282 }
7283
7284 mask = 1;
7285 for (i = 8; i >= 0; --i)
7286 {
7287 if (mode_str[i] != '-')
7288 mode |= mask;
7289 mask = mask << 1;
7290 }
7291 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7292}
7293
7294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 * "setpos()" function
7296 */
7297 static void
7298f_setpos(typval_T *argvars, typval_T *rettv)
7299{
7300 pos_T pos;
7301 int fnum;
7302 char_u *name;
7303 colnr_T curswant = -1;
7304
7305 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 if (name != NULL)
7308 {
7309 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7310 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007311 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007312 pos.col = 0;
7313 if (name[0] == '.' && name[1] == NUL)
7314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007315 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007316 curwin->w_cursor = pos;
7317 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007319 curwin->w_curswant = curswant - 1;
7320 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007322 check_cursor();
7323 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 }
7325 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007327 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 if (setmark_pos(name[1], &pos, fnum) == OK)
7329 rettv->vval.v_number = 0;
7330 }
7331 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007332 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333 }
7334 }
7335}
7336
7337/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007338 * Translate a register type string to the yank type and block length
7339 */
7340 static int
7341get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7342{
7343 char_u *stropt = *pp;
7344 switch (*stropt)
7345 {
7346 case 'v': case 'c': // character-wise selection
7347 *yank_type = MCHAR;
7348 break;
7349 case 'V': case 'l': // line-wise selection
7350 *yank_type = MLINE;
7351 break;
7352 case 'b': case Ctrl_V: // block-wise selection
7353 *yank_type = MBLOCK;
7354 if (VIM_ISDIGIT(stropt[1]))
7355 {
7356 ++stropt;
7357 *block_len = getdigits(&stropt) - 1;
7358 --stropt;
7359 }
7360 break;
7361 default:
7362 return FAIL;
7363 }
7364 *pp = stropt;
7365 return OK;
7366}
7367
7368/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 * "setreg()" function
7370 */
7371 static void
7372f_setreg(typval_T *argvars, typval_T *rettv)
7373{
7374 int regname;
7375 char_u *strregname;
7376 char_u *stropt;
7377 char_u *strval;
7378 int append;
7379 char_u yank_type;
7380 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007381 typval_T *regcontents;
7382 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007384 pointreg = 0;
7385 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 block_len = -1;
7387 yank_type = MAUTO;
7388 append = FALSE;
7389
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007391 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007392
7393 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007394 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 regname = *strregname;
7396 if (regname == 0 || regname == '@')
7397 regname = '"';
7398
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007399 if (argvars[1].v_type == VAR_DICT)
7400 {
7401 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007402 dictitem_T *di;
7403
7404 if (d == NULL || d->dv_hashtab.ht_used == 0)
7405 {
7406 // Empty dict, clear the register (like setreg(0, []))
7407 char_u *lstval[2] = {NULL, NULL};
7408 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7409 return;
7410 }
7411
7412 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007413 if (di != NULL)
7414 regcontents = &di->di_tv;
7415
7416 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7417 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007418 {
7419 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7420
7421 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007422 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007423 semsg(_(e_invargval), "value");
7424 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007425 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007426 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007427
7428 if (regname == '"')
7429 {
7430 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7431 if (stropt != NULL)
7432 {
7433 pointreg = *stropt;
7434 regname = pointreg;
7435 }
7436 }
7437 else if (dict_get_number(d, (char_u *)"isunnamed"))
7438 pointreg = regname;
7439 }
7440 else
7441 regcontents = &argvars[1];
7442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 if (argvars[2].v_type != VAR_UNKNOWN)
7444 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007445 if (yank_type != MAUTO)
7446 {
7447 semsg(_(e_toomanyarg), "setreg");
7448 return;
7449 }
7450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007451 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007453 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454 for (; *stropt != NUL; ++stropt)
7455 switch (*stropt)
7456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007457 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 append = TRUE;
7459 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007460 default:
7461 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462 }
7463 }
7464
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007465 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007466 {
7467 char_u **lstval;
7468 char_u **allocval;
7469 char_u buf[NUMBUFLEN];
7470 char_u **curval;
7471 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007472 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473 listitem_T *li;
7474 int len;
7475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007476 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 len = ll == NULL ? 0 : ll->lv_len;
7478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007479 // First half: use for pointers to result lines; second half: use for
7480 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007481 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 if (lstval == NULL)
7483 return;
7484 curval = lstval;
7485 allocval = lstval + len + 2;
7486 curallocval = allocval;
7487
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007488 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007490 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007491 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007492 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007493 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007494 if (strval == NULL)
7495 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007496 if (strval == buf)
7497 {
7498 // Need to make a copy, next tv_get_string_buf_chk() will
7499 // overwrite the string.
7500 strval = vim_strsave(buf);
7501 if (strval == NULL)
7502 goto free_lstval;
7503 *curallocval++ = strval;
7504 }
7505 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 }
7508 *curval++ = NULL;
7509
7510 write_reg_contents_lst(regname, lstval, -1,
7511 append, yank_type, block_len);
7512free_lstval:
7513 while (curallocval > allocval)
7514 vim_free(*--curallocval);
7515 vim_free(lstval);
7516 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007517 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007519 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 if (strval == NULL)
7521 return;
7522 write_reg_contents_ex(regname, strval, -1,
7523 append, yank_type, block_len);
7524 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007525 if (pointreg != 0)
7526 get_yank_register(pointreg, TRUE);
7527
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007528 rettv->vval.v_number = 0;
7529}
7530
7531/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007532 * "settagstack()" function
7533 */
7534 static void
7535f_settagstack(typval_T *argvars, typval_T *rettv)
7536{
7537 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7538 win_T *wp;
7539 dict_T *d;
7540 int action = 'r';
7541
7542 rettv->vval.v_number = -1;
7543
7544 // first argument: window number or id
7545 wp = find_win_by_nr_or_id(&argvars[0]);
7546 if (wp == NULL)
7547 return;
7548
7549 // second argument: dict with items to set in the tag stack
7550 if (argvars[1].v_type != VAR_DICT)
7551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007552 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007553 return;
7554 }
7555 d = argvars[1].vval.v_dict;
7556 if (d == NULL)
7557 return;
7558
7559 // third argument: action - 'a' for append and 'r' for replace.
7560 // default is to replace the stack.
7561 if (argvars[2].v_type == VAR_UNKNOWN)
7562 action = 'r';
7563 else if (argvars[2].v_type == VAR_STRING)
7564 {
7565 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007566 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007567 if (actstr == NULL)
7568 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007569 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7570 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007571 action = *actstr;
7572 else
7573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007575 return;
7576 }
7577 }
7578 else
7579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007580 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007581 return;
7582 }
7583
7584 if (set_tagstack(wp, d, action) == OK)
7585 rettv->vval.v_number = 0;
7586}
7587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007588#ifdef FEAT_CRYPT
7589/*
7590 * "sha256({string})" function
7591 */
7592 static void
7593f_sha256(typval_T *argvars, typval_T *rettv)
7594{
7595 char_u *p;
7596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 rettv->vval.v_string = vim_strsave(
7599 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7600 rettv->v_type = VAR_STRING;
7601}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007602#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603
7604/*
7605 * "shellescape({string})" function
7606 */
7607 static void
7608f_shellescape(typval_T *argvars, typval_T *rettv)
7609{
Bram Moolenaar20615522017-06-05 18:46:26 +02007610 int do_special = non_zero_arg(&argvars[1]);
7611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 rettv->v_type = VAR_STRING;
7615}
7616
7617/*
7618 * shiftwidth() function
7619 */
7620 static void
7621f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7622{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007623 rettv->vval.v_number = 0;
7624
7625 if (argvars[0].v_type != VAR_UNKNOWN)
7626 {
7627 long col;
7628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007630 if (col < 0)
7631 return; // type error; errmsg already given
7632#ifdef FEAT_VARTABS
7633 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7634 return;
7635#endif
7636 }
7637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 rettv->vval.v_number = get_sw_value(curbuf);
7639}
7640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641#ifdef FEAT_FLOAT
7642/*
7643 * "sin()" function
7644 */
7645 static void
7646f_sin(typval_T *argvars, typval_T *rettv)
7647{
7648 float_T f = 0.0;
7649
7650 rettv->v_type = VAR_FLOAT;
7651 if (get_float_arg(argvars, &f) == OK)
7652 rettv->vval.v_float = sin(f);
7653 else
7654 rettv->vval.v_float = 0.0;
7655}
7656
7657/*
7658 * "sinh()" function
7659 */
7660 static void
7661f_sinh(typval_T *argvars, typval_T *rettv)
7662{
7663 float_T f = 0.0;
7664
7665 rettv->v_type = VAR_FLOAT;
7666 if (get_float_arg(argvars, &f) == OK)
7667 rettv->vval.v_float = sinh(f);
7668 else
7669 rettv->vval.v_float = 0.0;
7670}
7671#endif
7672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673/*
7674 * "soundfold({word})" function
7675 */
7676 static void
7677f_soundfold(typval_T *argvars, typval_T *rettv)
7678{
7679 char_u *s;
7680
7681 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007682 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683#ifdef FEAT_SPELL
7684 rettv->vval.v_string = eval_soundfold(s);
7685#else
7686 rettv->vval.v_string = vim_strsave(s);
7687#endif
7688}
7689
7690/*
7691 * "spellbadword()" function
7692 */
7693 static void
7694f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7695{
7696 char_u *word = (char_u *)"";
7697 hlf_T attr = HLF_COUNT;
7698 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007699#ifdef FEAT_SPELL
7700 int wo_spell_save = curwin->w_p_spell;
7701
7702 if (!curwin->w_p_spell)
7703 {
7704 did_set_spelllang(curwin);
7705 curwin->w_p_spell = TRUE;
7706 }
7707
7708 if (*curwin->w_s->b_p_spl == NUL)
7709 {
7710 emsg(_(e_no_spell));
7711 curwin->w_p_spell = wo_spell_save;
7712 return;
7713 }
7714#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715
7716 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007717 {
7718#ifdef FEAT_SPELL
7719 curwin->w_p_spell = wo_spell_save;
7720#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723
7724#ifdef FEAT_SPELL
7725 if (argvars[0].v_type == VAR_UNKNOWN)
7726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007727 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7729 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007730 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007732 curwin->w_set_curswant = TRUE;
7733 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007734 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007735 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007736 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007737 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 int capcol = -1;
7739
7740 if (str != NULL)
7741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007742 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743 while (*str != NUL)
7744 {
7745 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7746 if (attr != HLF_COUNT)
7747 {
7748 word = str;
7749 break;
7750 }
7751 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007752 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007753 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 }
7755 }
7756 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007757 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758#endif
7759
7760 list_append_string(rettv->vval.v_list, word, len);
7761 list_append_string(rettv->vval.v_list, (char_u *)(
7762 attr == HLF_SPB ? "bad" :
7763 attr == HLF_SPR ? "rare" :
7764 attr == HLF_SPL ? "local" :
7765 attr == HLF_SPC ? "caps" :
7766 ""), -1);
7767}
7768
7769/*
7770 * "spellsuggest()" function
7771 */
7772 static void
7773f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7774{
7775#ifdef FEAT_SPELL
7776 char_u *str;
7777 int typeerr = FALSE;
7778 int maxcount;
7779 garray_T ga;
7780 int i;
7781 listitem_T *li;
7782 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007783 int wo_spell_save = curwin->w_p_spell;
7784
7785 if (!curwin->w_p_spell)
7786 {
7787 did_set_spelllang(curwin);
7788 curwin->w_p_spell = TRUE;
7789 }
7790
7791 if (*curwin->w_s->b_p_spl == NUL)
7792 {
7793 emsg(_(e_no_spell));
7794 curwin->w_p_spell = wo_spell_save;
7795 return;
7796 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797#endif
7798
7799 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007800 {
7801#ifdef FEAT_SPELL
7802 curwin->w_p_spell = wo_spell_save;
7803#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806
7807#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007808 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007810 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 if (argvars[1].v_type != VAR_UNKNOWN)
7812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007813 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (maxcount <= 0)
7815 return;
7816 if (argvars[2].v_type != VAR_UNKNOWN)
7817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007818 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 if (typeerr)
7820 return;
7821 }
7822 }
7823 else
7824 maxcount = 25;
7825
7826 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7827
7828 for (i = 0; i < ga.ga_len; ++i)
7829 {
7830 str = ((char_u **)ga.ga_data)[i];
7831
7832 li = listitem_alloc();
7833 if (li == NULL)
7834 vim_free(str);
7835 else
7836 {
7837 li->li_tv.v_type = VAR_STRING;
7838 li->li_tv.v_lock = 0;
7839 li->li_tv.vval.v_string = str;
7840 list_append(rettv->vval.v_list, li);
7841 }
7842 }
7843 ga_clear(&ga);
7844 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007845 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846#endif
7847}
7848
7849 static void
7850f_split(typval_T *argvars, typval_T *rettv)
7851{
7852 char_u *str;
7853 char_u *end;
7854 char_u *pat = NULL;
7855 regmatch_T regmatch;
7856 char_u patbuf[NUMBUFLEN];
7857 char_u *save_cpo;
7858 int match;
7859 colnr_T col = 0;
7860 int keepempty = FALSE;
7861 int typeerr = FALSE;
7862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007863 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 save_cpo = p_cpo;
7865 p_cpo = (char_u *)"";
7866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007867 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007868 if (argvars[1].v_type != VAR_UNKNOWN)
7869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007870 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007871 if (pat == NULL)
7872 typeerr = TRUE;
7873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007874 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 }
7876 if (pat == NULL || *pat == NUL)
7877 pat = (char_u *)"[\\x01- ]\\+";
7878
7879 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007880 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007882 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883
7884 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7885 if (regmatch.regprog != NULL)
7886 {
7887 regmatch.rm_ic = FALSE;
7888 while (*str != NUL || keepempty)
7889 {
7890 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007891 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 else
7893 match = vim_regexec_nl(&regmatch, str, col);
7894 if (match)
7895 end = regmatch.startp[0];
7896 else
7897 end = str + STRLEN(str);
7898 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7899 && *str != NUL && match && end < regmatch.endp[0]))
7900 {
7901 if (list_append_string(rettv->vval.v_list, str,
7902 (int)(end - str)) == FAIL)
7903 break;
7904 }
7905 if (!match)
7906 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007907 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 if (regmatch.endp[0] > str)
7909 col = 0;
7910 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007911 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 str = regmatch.endp[0];
7914 }
7915
7916 vim_regfree(regmatch.regprog);
7917 }
7918
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007919theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 p_cpo = save_cpo;
7921}
7922
7923#ifdef FEAT_FLOAT
7924/*
7925 * "sqrt()" function
7926 */
7927 static void
7928f_sqrt(typval_T *argvars, typval_T *rettv)
7929{
7930 float_T f = 0.0;
7931
7932 rettv->v_type = VAR_FLOAT;
7933 if (get_float_arg(argvars, &f) == OK)
7934 rettv->vval.v_float = sqrt(f);
7935 else
7936 rettv->vval.v_float = 0.0;
7937}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007938#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007940#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 * "str2float()" function
7943 */
7944 static void
7945f_str2float(typval_T *argvars, typval_T *rettv)
7946{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007947 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007948 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949
Bram Moolenaar08243d22017-01-10 16:12:29 +01007950 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007951 p = skipwhite(p + 1);
7952 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007953 if (isneg)
7954 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 rettv->v_type = VAR_FLOAT;
7956}
7957#endif
7958
7959/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007960 * "str2list()" function
7961 */
7962 static void
7963f_str2list(typval_T *argvars, typval_T *rettv)
7964{
7965 char_u *p;
7966 int utf8 = FALSE;
7967
7968 if (rettv_list_alloc(rettv) == FAIL)
7969 return;
7970
7971 if (argvars[1].v_type != VAR_UNKNOWN)
7972 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7973
7974 p = tv_get_string(&argvars[0]);
7975
7976 if (has_mbyte || utf8)
7977 {
7978 int (*ptr2len)(char_u *);
7979 int (*ptr2char)(char_u *);
7980
7981 if (utf8 || enc_utf8)
7982 {
7983 ptr2len = utf_ptr2len;
7984 ptr2char = utf_ptr2char;
7985 }
7986 else
7987 {
7988 ptr2len = mb_ptr2len;
7989 ptr2char = mb_ptr2char;
7990 }
7991
7992 for ( ; *p != NUL; p += (*ptr2len)(p))
7993 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7994 }
7995 else
7996 for ( ; *p != NUL; ++p)
7997 list_append_number(rettv->vval.v_list, *p);
7998}
7999
8000/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 * "str2nr()" function
8002 */
8003 static void
8004f_str2nr(typval_T *argvars, typval_T *rettv)
8005{
8006 int base = 10;
8007 char_u *p;
8008 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008009 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008010 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011
8012 if (argvars[1].v_type != VAR_UNKNOWN)
8013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008014 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 if (base != 2 && base != 8 && base != 10 && base != 16)
8016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008017 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 return;
8019 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008020 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8021 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 }
8023
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008024 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008025 isneg = (*p == '-');
8026 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 p = skipwhite(p + 1);
8028 switch (base)
8029 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008030 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008031 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008032 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008034 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8035 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008036 if (isneg)
8037 rettv->vval.v_number = -n;
8038 else
8039 rettv->vval.v_number = n;
8040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041}
8042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043/*
8044 * "strgetchar()" function
8045 */
8046 static void
8047f_strgetchar(typval_T *argvars, typval_T *rettv)
8048{
8049 char_u *str;
8050 int len;
8051 int error = FALSE;
8052 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008053 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054
8055 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008056 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 if (str == NULL)
8058 return;
8059 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008060 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (error)
8062 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008063
Bram Moolenaar13505972019-01-24 15:04:48 +01008064 while (charidx >= 0 && byteidx < len)
8065 {
8066 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008068 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8069 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008071 --charidx;
8072 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074}
8075
8076/*
8077 * "stridx()" function
8078 */
8079 static void
8080f_stridx(typval_T *argvars, typval_T *rettv)
8081{
8082 char_u buf[NUMBUFLEN];
8083 char_u *needle;
8084 char_u *haystack;
8085 char_u *save_haystack;
8086 char_u *pos;
8087 int start_idx;
8088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008089 needle = tv_get_string_chk(&argvars[1]);
8090 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 rettv->vval.v_number = -1;
8092 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008093 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094
8095 if (argvars[2].v_type != VAR_UNKNOWN)
8096 {
8097 int error = FALSE;
8098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008099 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 if (error || start_idx >= (int)STRLEN(haystack))
8101 return;
8102 if (start_idx >= 0)
8103 haystack += start_idx;
8104 }
8105
8106 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8107 if (pos != NULL)
8108 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8109}
8110
8111/*
8112 * "string()" function
8113 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008114 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115f_string(typval_T *argvars, typval_T *rettv)
8116{
8117 char_u *tofree;
8118 char_u numbuf[NUMBUFLEN];
8119
8120 rettv->v_type = VAR_STRING;
8121 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8122 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008123 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 if (rettv->vval.v_string != NULL && tofree == NULL)
8125 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8126}
8127
8128/*
8129 * "strlen()" function
8130 */
8131 static void
8132f_strlen(typval_T *argvars, typval_T *rettv)
8133{
8134 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008135 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136}
8137
8138/*
8139 * "strchars()" function
8140 */
8141 static void
8142f_strchars(typval_T *argvars, typval_T *rettv)
8143{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008144 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008145 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 varnumber_T len = 0;
8147 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148
8149 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008150 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008152 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 else
8154 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8156 while (*s != NUL)
8157 {
8158 func_mb_ptr2char_adv(&s);
8159 ++len;
8160 }
8161 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 }
8163}
8164
8165/*
8166 * "strdisplaywidth()" function
8167 */
8168 static void
8169f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8170{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008171 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 int col = 0;
8173
8174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008175 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176
8177 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8178}
8179
8180/*
8181 * "strwidth()" function
8182 */
8183 static void
8184f_strwidth(typval_T *argvars, typval_T *rettv)
8185{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008186 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187
Bram Moolenaar13505972019-01-24 15:04:48 +01008188 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189}
8190
8191/*
8192 * "strcharpart()" function
8193 */
8194 static void
8195f_strcharpart(typval_T *argvars, typval_T *rettv)
8196{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 char_u *p;
8198 int nchar;
8199 int nbyte = 0;
8200 int charlen;
8201 int len = 0;
8202 int slen;
8203 int error = FALSE;
8204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008205 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 slen = (int)STRLEN(p);
8207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008208 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (!error)
8210 {
8211 if (nchar > 0)
8212 while (nchar > 0 && nbyte < slen)
8213 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008214 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008215 --nchar;
8216 }
8217 else
8218 nbyte = nchar;
8219 if (argvars[2].v_type != VAR_UNKNOWN)
8220 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008221 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 while (charlen > 0 && nbyte + len < slen)
8223 {
8224 int off = nbyte + len;
8225
8226 if (off < 0)
8227 len += 1;
8228 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008229 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 --charlen;
8231 }
8232 }
8233 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008234 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235 }
8236
8237 /*
8238 * Only return the overlap between the specified part and the actual
8239 * string.
8240 */
8241 if (nbyte < 0)
8242 {
8243 len += nbyte;
8244 nbyte = 0;
8245 }
8246 else if (nbyte > slen)
8247 nbyte = slen;
8248 if (len < 0)
8249 len = 0;
8250 else if (nbyte + len > slen)
8251 len = slen - nbyte;
8252
8253 rettv->v_type = VAR_STRING;
8254 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255}
8256
8257/*
8258 * "strpart()" function
8259 */
8260 static void
8261f_strpart(typval_T *argvars, typval_T *rettv)
8262{
8263 char_u *p;
8264 int n;
8265 int len;
8266 int slen;
8267 int error = FALSE;
8268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008269 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 slen = (int)STRLEN(p);
8271
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008272 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 if (error)
8274 len = 0;
8275 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008276 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008278 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279
8280 /*
8281 * Only return the overlap between the specified part and the actual
8282 * string.
8283 */
8284 if (n < 0)
8285 {
8286 len += n;
8287 n = 0;
8288 }
8289 else if (n > slen)
8290 n = slen;
8291 if (len < 0)
8292 len = 0;
8293 else if (n + len > slen)
8294 len = slen - n;
8295
8296 rettv->v_type = VAR_STRING;
8297 rettv->vval.v_string = vim_strnsave(p + n, len);
8298}
8299
8300/*
8301 * "strridx()" function
8302 */
8303 static void
8304f_strridx(typval_T *argvars, typval_T *rettv)
8305{
8306 char_u buf[NUMBUFLEN];
8307 char_u *needle;
8308 char_u *haystack;
8309 char_u *rest;
8310 char_u *lastmatch = NULL;
8311 int haystack_len, end_idx;
8312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008313 needle = tv_get_string_chk(&argvars[1]);
8314 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315
8316 rettv->vval.v_number = -1;
8317 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008318 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319
8320 haystack_len = (int)STRLEN(haystack);
8321 if (argvars[2].v_type != VAR_UNKNOWN)
8322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008323 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008324 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008326 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 }
8328 else
8329 end_idx = haystack_len;
8330
8331 if (*needle == NUL)
8332 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008333 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 lastmatch = haystack + end_idx;
8335 }
8336 else
8337 {
8338 for (rest = haystack; *rest != '\0'; ++rest)
8339 {
8340 rest = (char_u *)strstr((char *)rest, (char *)needle);
8341 if (rest == NULL || rest > haystack + end_idx)
8342 break;
8343 lastmatch = rest;
8344 }
8345 }
8346
8347 if (lastmatch == NULL)
8348 rettv->vval.v_number = -1;
8349 else
8350 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8351}
8352
8353/*
8354 * "strtrans()" function
8355 */
8356 static void
8357f_strtrans(typval_T *argvars, typval_T *rettv)
8358{
8359 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008360 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361}
8362
8363/*
8364 * "submatch()" function
8365 */
8366 static void
8367f_submatch(typval_T *argvars, typval_T *rettv)
8368{
8369 int error = FALSE;
8370 int no;
8371 int retList = 0;
8372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008373 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 if (error)
8375 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008376 if (no < 0 || no >= NSUBEXP)
8377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008378 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008379 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008382 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 if (error)
8384 return;
8385
8386 if (retList == 0)
8387 {
8388 rettv->v_type = VAR_STRING;
8389 rettv->vval.v_string = reg_submatch(no);
8390 }
8391 else
8392 {
8393 rettv->v_type = VAR_LIST;
8394 rettv->vval.v_list = reg_submatch_list(no);
8395 }
8396}
8397
8398/*
8399 * "substitute()" function
8400 */
8401 static void
8402f_substitute(typval_T *argvars, typval_T *rettv)
8403{
8404 char_u patbuf[NUMBUFLEN];
8405 char_u subbuf[NUMBUFLEN];
8406 char_u flagsbuf[NUMBUFLEN];
8407
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 char_u *str = tv_get_string_chk(&argvars[0]);
8409 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008410 char_u *sub = NULL;
8411 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008414 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8415 expr = &argvars[2];
8416 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008417 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008420 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8421 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 rettv->vval.v_string = NULL;
8423 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008424 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425}
8426
8427/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008428 * "swapinfo(swap_filename)" function
8429 */
8430 static void
8431f_swapinfo(typval_T *argvars, typval_T *rettv)
8432{
8433 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008434 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008435}
8436
8437/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008438 * "swapname(expr)" function
8439 */
8440 static void
8441f_swapname(typval_T *argvars, typval_T *rettv)
8442{
8443 buf_T *buf;
8444
8445 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008446 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008447 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8448 || buf->b_ml.ml_mfp->mf_fname == NULL)
8449 rettv->vval.v_string = NULL;
8450 else
8451 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8452}
8453
8454/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455 * "synID(lnum, col, trans)" function
8456 */
8457 static void
8458f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8459{
8460 int id = 0;
8461#ifdef FEAT_SYN_HL
8462 linenr_T lnum;
8463 colnr_T col;
8464 int trans;
8465 int transerr = FALSE;
8466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008467 lnum = tv_get_lnum(argvars); // -1 on type error
8468 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008469 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470
8471 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8472 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8473 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8474#endif
8475
8476 rettv->vval.v_number = id;
8477}
8478
8479/*
8480 * "synIDattr(id, what [, mode])" function
8481 */
8482 static void
8483f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8484{
8485 char_u *p = NULL;
8486#ifdef FEAT_SYN_HL
8487 int id;
8488 char_u *what;
8489 char_u *mode;
8490 char_u modebuf[NUMBUFLEN];
8491 int modec;
8492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008493 id = (int)tv_get_number(&argvars[0]);
8494 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 if (argvars[2].v_type != VAR_UNKNOWN)
8496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008497 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 modec = TOLOWER_ASC(mode[0]);
8499 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008500 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 }
8502 else
8503 {
8504#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8505 if (USE_24BIT)
8506 modec = 'g';
8507 else
8508#endif
8509 if (t_colors > 1)
8510 modec = 'c';
8511 else
8512 modec = 't';
8513 }
8514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 switch (TOLOWER_ASC(what[0]))
8516 {
8517 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008520 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 p = highlight_has_attr(id, HL_BOLD, modec);
8522 break;
8523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008524 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525 p = highlight_color(id, what, modec);
8526 break;
8527
8528 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008529 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008531 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 p = highlight_has_attr(id, HL_ITALIC, modec);
8533 break;
8534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008535 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008536 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 break;
8538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008539 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 p = highlight_has_attr(id, HL_INVERSE, modec);
8541 break;
8542
8543 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008544 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008546 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008547 else if (TOLOWER_ASC(what[1]) == 't' &&
8548 TOLOWER_ASC(what[2]) == 'r')
8549 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 p = highlight_has_attr(id, HL_STANDOUT, modec);
8552 break;
8553
8554 case 'u':
8555 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008556 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8558 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008559 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8561 break;
8562 }
8563
8564 if (p != NULL)
8565 p = vim_strsave(p);
8566#endif
8567 rettv->v_type = VAR_STRING;
8568 rettv->vval.v_string = p;
8569}
8570
8571/*
8572 * "synIDtrans(id)" function
8573 */
8574 static void
8575f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8576{
8577 int id;
8578
8579#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581
8582 if (id > 0)
8583 id = syn_get_final_id(id);
8584 else
8585#endif
8586 id = 0;
8587
8588 rettv->vval.v_number = id;
8589}
8590
8591/*
8592 * "synconcealed(lnum, col)" function
8593 */
8594 static void
8595f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8596{
8597#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8598 linenr_T lnum;
8599 colnr_T col;
8600 int syntax_flags = 0;
8601 int cchar;
8602 int matchid = 0;
8603 char_u str[NUMBUFLEN];
8604#endif
8605
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008606 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607
8608#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008609 lnum = tv_get_lnum(argvars); // -1 on type error
8610 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611
Bram Moolenaara80faa82020-04-12 19:37:17 +02008612 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613
8614 if (rettv_list_alloc(rettv) != FAIL)
8615 {
8616 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8617 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8618 && curwin->w_p_cole > 0)
8619 {
8620 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8621 syntax_flags = get_syntax_info(&matchid);
8622
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008623 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8625 {
8626 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008627 if (cchar == NUL && curwin->w_p_cole == 1)
8628 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 if (cchar != NUL)
8630 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 if (has_mbyte)
8632 (*mb_char2bytes)(cchar, str);
8633 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 str[0] = cchar;
8635 }
8636 }
8637 }
8638
8639 list_append_number(rettv->vval.v_list,
8640 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008641 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 list_append_string(rettv->vval.v_list, str, -1);
8643 list_append_number(rettv->vval.v_list, matchid);
8644 }
8645#endif
8646}
8647
8648/*
8649 * "synstack(lnum, col)" function
8650 */
8651 static void
8652f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8653{
8654#ifdef FEAT_SYN_HL
8655 linenr_T lnum;
8656 colnr_T col;
8657 int i;
8658 int id;
8659#endif
8660
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008661 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662
8663#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008664 lnum = tv_get_lnum(argvars); // -1 on type error
8665 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666
8667 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8668 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8669 && rettv_list_alloc(rettv) != FAIL)
8670 {
8671 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8672 for (i = 0; ; ++i)
8673 {
8674 id = syn_get_stack_item(i);
8675 if (id < 0)
8676 break;
8677 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8678 break;
8679 }
8680 }
8681#endif
8682}
8683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684/*
8685 * "tabpagebuflist()" function
8686 */
8687 static void
8688f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8689{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 tabpage_T *tp;
8691 win_T *wp = NULL;
8692
8693 if (argvars[0].v_type == VAR_UNKNOWN)
8694 wp = firstwin;
8695 else
8696 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008697 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 if (tp != NULL)
8699 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8700 }
8701 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8702 {
8703 for (; wp != NULL; wp = wp->w_next)
8704 if (list_append_number(rettv->vval.v_list,
8705 wp->w_buffer->b_fnum) == FAIL)
8706 break;
8707 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708}
8709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 * "tagfiles()" function
8712 */
8713 static void
8714f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8715{
8716 char_u *fname;
8717 tagname_T tn;
8718 int first;
8719
8720 if (rettv_list_alloc(rettv) == FAIL)
8721 return;
8722 fname = alloc(MAXPATHL);
8723 if (fname == NULL)
8724 return;
8725
8726 for (first = TRUE; ; first = FALSE)
8727 if (get_tagfname(&tn, first, fname) == FAIL
8728 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8729 break;
8730 tagname_free(&tn);
8731 vim_free(fname);
8732}
8733
8734/*
8735 * "taglist()" function
8736 */
8737 static void
8738f_taglist(typval_T *argvars, typval_T *rettv)
8739{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008740 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 char_u *tag_pattern;
8742
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008743 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744
8745 rettv->vval.v_number = FALSE;
8746 if (*tag_pattern == NUL)
8747 return;
8748
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008749 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008752 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753}
8754
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755#ifdef FEAT_FLOAT
8756/*
8757 * "tan()" function
8758 */
8759 static void
8760f_tan(typval_T *argvars, typval_T *rettv)
8761{
8762 float_T f = 0.0;
8763
8764 rettv->v_type = VAR_FLOAT;
8765 if (get_float_arg(argvars, &f) == OK)
8766 rettv->vval.v_float = tan(f);
8767 else
8768 rettv->vval.v_float = 0.0;
8769}
8770
8771/*
8772 * "tanh()" function
8773 */
8774 static void
8775f_tanh(typval_T *argvars, typval_T *rettv)
8776{
8777 float_T f = 0.0;
8778
8779 rettv->v_type = VAR_FLOAT;
8780 if (get_float_arg(argvars, &f) == OK)
8781 rettv->vval.v_float = tanh(f);
8782 else
8783 rettv->vval.v_float = 0.0;
8784}
8785#endif
8786
8787/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788 * "tolower(string)" function
8789 */
8790 static void
8791f_tolower(typval_T *argvars, typval_T *rettv)
8792{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008794 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795}
8796
8797/*
8798 * "toupper(string)" function
8799 */
8800 static void
8801f_toupper(typval_T *argvars, typval_T *rettv)
8802{
8803 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008804 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805}
8806
8807/*
8808 * "tr(string, fromstr, tostr)" function
8809 */
8810 static void
8811f_tr(typval_T *argvars, typval_T *rettv)
8812{
8813 char_u *in_str;
8814 char_u *fromstr;
8815 char_u *tostr;
8816 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817 int inlen;
8818 int fromlen;
8819 int tolen;
8820 int idx;
8821 char_u *cpstr;
8822 int cplen;
8823 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 char_u buf[NUMBUFLEN];
8825 char_u buf2[NUMBUFLEN];
8826 garray_T ga;
8827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008828 in_str = tv_get_string(&argvars[0]);
8829 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8830 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008832 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 rettv->v_type = VAR_STRING;
8834 rettv->vval.v_string = NULL;
8835 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008836 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 ga_init2(&ga, (int)sizeof(char), 80);
8838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008840 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 if (STRLEN(fromstr) != STRLEN(tostr))
8842 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008844 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 ga_clear(&ga);
8846 return;
8847 }
8848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008849 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850 while (*in_str != NUL)
8851 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 if (has_mbyte)
8853 {
8854 inlen = (*mb_ptr2len)(in_str);
8855 cpstr = in_str;
8856 cplen = inlen;
8857 idx = 0;
8858 for (p = fromstr; *p != NUL; p += fromlen)
8859 {
8860 fromlen = (*mb_ptr2len)(p);
8861 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8862 {
8863 for (p = tostr; *p != NUL; p += tolen)
8864 {
8865 tolen = (*mb_ptr2len)(p);
8866 if (idx-- == 0)
8867 {
8868 cplen = tolen;
8869 cpstr = p;
8870 break;
8871 }
8872 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008873 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 goto error;
8875 break;
8876 }
8877 ++idx;
8878 }
8879
8880 if (first && cpstr == in_str)
8881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008882 // Check that fromstr and tostr have the same number of
8883 // (multi-byte) characters. Done only once when a character
8884 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 first = FALSE;
8886 for (p = tostr; *p != NUL; p += tolen)
8887 {
8888 tolen = (*mb_ptr2len)(p);
8889 --idx;
8890 }
8891 if (idx != 0)
8892 goto error;
8893 }
8894
8895 (void)ga_grow(&ga, cplen);
8896 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8897 ga.ga_len += cplen;
8898
8899 in_str += inlen;
8900 }
8901 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008903 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904 p = vim_strchr(fromstr, *in_str);
8905 if (p != NULL)
8906 ga_append(&ga, tostr[p - fromstr]);
8907 else
8908 ga_append(&ga, *in_str);
8909 ++in_str;
8910 }
8911 }
8912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008913 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 (void)ga_grow(&ga, 1);
8915 ga_append(&ga, NUL);
8916
8917 rettv->vval.v_string = ga.ga_data;
8918}
8919
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008920/*
8921 * "trim({expr})" function
8922 */
8923 static void
8924f_trim(typval_T *argvars, typval_T *rettv)
8925{
8926 char_u buf1[NUMBUFLEN];
8927 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008928 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008929 char_u *mask = NULL;
8930 char_u *tail;
8931 char_u *prev;
8932 char_u *p;
8933 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008934 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008935
8936 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008937 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008938 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008939 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008940
8941 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008943 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008944
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008946 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008947 int error = 0;
8948
8949 // leading or trailing characters to trim
8950 dir = (int)tv_get_number_chk(&argvars[2], &error);
8951 if (error)
8952 return;
8953 if (dir < 0 || dir > 2)
8954 {
8955 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8956 return;
8957 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008958 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008959 }
8960
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008961 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008962 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008963 // Trim leading characters
8964 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008965 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008966 c1 = PTR2CHAR(head);
8967 if (mask == NULL)
8968 {
8969 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008970 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008971 }
8972 else
8973 {
8974 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8975 if (c1 == PTR2CHAR(p))
8976 break;
8977 if (*p == NUL)
8978 break;
8979 }
8980 MB_PTR_ADV(head);
8981 }
8982 }
8983
8984 tail = head + STRLEN(head);
8985 if (dir == 0 || dir == 2)
8986 {
8987 // Trim trailing characters
8988 for (; tail > head; tail = prev)
8989 {
8990 prev = tail;
8991 MB_PTR_BACK(head, prev);
8992 c1 = PTR2CHAR(prev);
8993 if (mask == NULL)
8994 {
8995 if (c1 > ' ' && c1 != 0xa0)
8996 break;
8997 }
8998 else
8999 {
9000 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9001 if (c1 == PTR2CHAR(p))
9002 break;
9003 if (*p == NUL)
9004 break;
9005 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009006 }
9007 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009008 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009009}
9010
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011#ifdef FEAT_FLOAT
9012/*
9013 * "trunc({float})" function
9014 */
9015 static void
9016f_trunc(typval_T *argvars, typval_T *rettv)
9017{
9018 float_T f = 0.0;
9019
9020 rettv->v_type = VAR_FLOAT;
9021 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009022 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9024 else
9025 rettv->vval.v_float = 0.0;
9026}
9027#endif
9028
9029/*
9030 * "type(expr)" function
9031 */
9032 static void
9033f_type(typval_T *argvars, typval_T *rettv)
9034{
9035 int n = -1;
9036
9037 switch (argvars[0].v_type)
9038 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009039 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9040 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009042 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9043 case VAR_LIST: n = VAR_TYPE_LIST; break;
9044 case VAR_DICT: n = VAR_TYPE_DICT; break;
9045 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9046 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9047 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009048 case VAR_JOB: n = VAR_TYPE_JOB; break;
9049 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009050 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009052 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009053 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009054 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 n = -1;
9056 break;
9057 }
9058 rettv->vval.v_number = n;
9059}
9060
9061/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 * "virtcol(string)" function
9063 */
9064 static void
9065f_virtcol(typval_T *argvars, typval_T *rettv)
9066{
9067 colnr_T vcol = 0;
9068 pos_T *fp;
9069 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009070 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071
9072 fp = var2fpos(&argvars[0], FALSE, &fnum);
9073 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9074 && fnum == curbuf->b_fnum)
9075 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009076 // Limit the column to a valid value, getvvcol() doesn't check.
9077 if (fp->col < 0)
9078 fp->col = 0;
9079 else
9080 {
9081 len = (int)STRLEN(ml_get(fp->lnum));
9082 if (fp->col > len)
9083 fp->col = len;
9084 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085 getvvcol(curwin, fp, NULL, NULL, &vcol);
9086 ++vcol;
9087 }
9088
9089 rettv->vval.v_number = vcol;
9090}
9091
9092/*
9093 * "visualmode()" function
9094 */
9095 static void
9096f_visualmode(typval_T *argvars, typval_T *rettv)
9097{
9098 char_u str[2];
9099
9100 rettv->v_type = VAR_STRING;
9101 str[0] = curbuf->b_visual_mode_eval;
9102 str[1] = NUL;
9103 rettv->vval.v_string = vim_strsave(str);
9104
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009105 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 if (non_zero_arg(&argvars[0]))
9107 curbuf->b_visual_mode_eval = NUL;
9108}
9109
9110/*
9111 * "wildmenumode()" function
9112 */
9113 static void
9114f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9115{
9116#ifdef FEAT_WILDMENU
9117 if (wild_menu_showing)
9118 rettv->vval.v_number = 1;
9119#endif
9120}
9121
9122/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009123 * "windowsversion()" function
9124 */
9125 static void
9126f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9127{
9128 rettv->v_type = VAR_STRING;
9129 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9130}
9131
9132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 * "wordcount()" function
9134 */
9135 static void
9136f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9137{
9138 if (rettv_dict_alloc(rettv) == FAIL)
9139 return;
9140 cursor_pos_info(rettv->vval.v_dict);
9141}
9142
9143/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144 * "xor(expr, expr)" function
9145 */
9146 static void
9147f_xor(typval_T *argvars, typval_T *rettv)
9148{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009149 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9150 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009151}
9152
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009153#endif // FEAT_EVAL