blob: 62bbb8888d52d4ac1599beae0271d9f1a46cff8e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200385 static type_T *
386ret_remove(int argcount UNUSED, type_T **argtypes)
387{
388 if (argtypes[0]->tt_type == VAR_LIST
389 || argtypes[0]->tt_type == VAR_DICT)
390 return argtypes[0]->tt_member;
391 if (argtypes[0]->tt_type == VAR_BLOB)
392 return &t_number;
393 return &t_any;
394}
395
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200396 static type_T *
397ret_getreg(int argcount, type_T **argtypes UNUSED)
398{
399 // Assume that if the third argument is passed it's non-zero
400 if (argcount == 3)
401 return &t_list_string;
402 return &t_string;
403}
404
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200405 static type_T *
406ret_maparg(int argcount, type_T **argtypes UNUSED)
407{
408 // Assume that if the fourth argument is passed it's non-zero
409 if (argcount == 4)
410 return &t_dict_any;
411 return &t_string;
412}
413
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100414static type_T *ret_f_function(int argcount, type_T **argtypes);
415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416/*
417 * Array with names and number of arguments of all internal functions
418 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
419 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200420typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200422 char *f_name; // function name
423 char f_min_argc; // minimal number of arguments
424 char f_max_argc; // maximal number of arguments
425 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100426 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
427 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200429 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200430} funcentry_T;
431
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200432// values for f_argtype; zero means it cannot be used as a method
433#define FEARG_1 1 // base is the first argument
434#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200435#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200436#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200437#define FEARG_LAST 9 // base is the last argument
438
Bram Moolenaar15c47602020-03-26 22:16:48 +0100439#ifdef FEAT_FLOAT
440# define FLOAT_FUNC(name) name
441#else
442# define FLOAT_FUNC(name) NULL
443#endif
444#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
445# define MATH_FUNC(name) name
446#else
447# define MATH_FUNC(name) NULL
448#endif
449#ifdef FEAT_TIMERS
450# define TIMER_FUNC(name) name
451#else
452# define TIMER_FUNC(name) NULL
453#endif
454#ifdef FEAT_JOB_CHANNEL
455# define JOB_FUNC(name) name
456#else
457# define JOB_FUNC(name) NULL
458#endif
459#ifdef FEAT_PROP_POPUP
460# define PROP_FUNC(name) name
461#else
462# define PROP_FUNC(name) NULL
463#endif
464#ifdef FEAT_SIGNS
465# define SIGN_FUNC(name) name
466#else
467# define SIGN_FUNC(name) NULL
468#endif
469#ifdef FEAT_SOUND
470# define SOUND_FUNC(name) name
471#else
472# define SOUND_FUNC(name) NULL
473#endif
474#ifdef FEAT_TERMINAL
475# define TERM_FUNC(name) name
476#else
477# define TERM_FUNC(name) NULL
478#endif
479
Bram Moolenaarac92e252019-08-03 21:58:38 +0200480static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200481{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100482 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
483 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200484 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100485 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200486 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200487 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488 {"argc", 0, 1, 0, ret_number, f_argc},
489 {"argidx", 0, 0, 0, ret_number, f_argidx},
490 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200491 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100492 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100493 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
494 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200495 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100496 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar1d634542020-08-18 13:41:50 +0200497 {"assert_fails", 1, 4, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100498 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
499 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
500 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
501 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
502 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
503 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
504 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100505 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
506 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
507 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100508#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100509 f_balloon_gettext
510#else
511 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100513 },
514 {"balloon_show", 1, 1, FEARG_1, ret_void,
515#ifdef FEAT_BEVAL
516 f_balloon_show
517#else
518 NULL
519#endif
520 },
521 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
522#if defined(FEAT_BEVAL_TERM)
523 f_balloon_split
524#else
525 NULL
526#endif
527 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100528 {"browse", 4, 4, 0, ret_string, f_browse},
529 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
530 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
531 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
532 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
533 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
534 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
535 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
536 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
537 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
538 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
539 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
540 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
541 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
542 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
543 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
544 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
545 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100546 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
547 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
548 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
549 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
550 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
551 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
552 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
553 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
554 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
555 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
556 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
557 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
558 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
559 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
560 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
561 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
562 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
563 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
564 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100565 {"changenr", 0, 0, 0, ret_number, f_changenr},
566 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
Bram Moolenaar4e4473c2020-08-28 22:24:57 +0200567 {"charclass", 1, 1, FEARG_1, ret_number, f_charclass},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100568 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
569 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
570 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
571 {"col", 1, 1, FEARG_1, ret_number, f_col},
572 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
573 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
574 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
575 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
576 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200577 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100578 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
579 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580 {"count", 2, 4, FEARG_1, ret_number, f_count},
581 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
582 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100583 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100584#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100585 f_debugbreak
586#else
587 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200588#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100589 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200590 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100591 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
592 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
593 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
594 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
595 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
596 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
597 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
598 {"environ", 0, 0, 0, ret_dict_string, f_environ},
599 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
600 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
601 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
602 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
603 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
604 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
605 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100606 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100607 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
608 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200609 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100610 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
611 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
612 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
613 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200614 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100615 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
616 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200617 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100618 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
619 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
620 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100621 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
622 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
623 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
624 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
625 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
626 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
627 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
628 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200629 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100630 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
631 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
632 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200633 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100634 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
635 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
636 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
637 {"getchar", 0, 1, 0, ret_number, f_getchar},
638 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
639 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
640 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
641 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
642 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
643 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
644 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
645 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
646 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
647 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
648 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
649 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
650 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
651 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
652 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
653 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
654 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
655 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200656 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200657 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100658 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
659 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
660 {"getpid", 0, 0, 0, ret_number, f_getpid},
661 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200662 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200663 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200664 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100665 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
666 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
667 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
668 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
669 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
670 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
671 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
672 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
673 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
674 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
675 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
676 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
677 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100678 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100679 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
680 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
681 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
682 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
683 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
684 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
685 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
686 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
687 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
688 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
689 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
690 {"hostname", 0, 0, 0, ret_string, f_hostname},
691 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
692 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
693 {"index", 2, 4, FEARG_1, ret_number, f_index},
694 {"input", 1, 3, FEARG_1, ret_string, f_input},
695 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
696 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
697 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
698 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
699 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200700 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100701 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
702 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
703 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100704 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100705 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100706 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100707 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
709 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
710 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
711 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
712 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
713 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100714 {"join", 1, 2, FEARG_1, ret_string, f_join},
715 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
716 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
717 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
718 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200719 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100720 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
721 {"len", 1, 1, FEARG_1, ret_number, f_len},
722 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
723 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
724 {"line", 1, 2, FEARG_1, ret_number, f_line},
725 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
726 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
727 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
728 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
729 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
730 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
731 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100732 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
733 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
734 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200735#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100736 f_luaeval
737#else
738 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200739#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100740 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100741 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200742 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100743 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200744 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100745 {"match", 2, 4, FEARG_1, ret_any, f_match},
746 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
747 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
748 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
749 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
750 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
751 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
752 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
753 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
754 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100755 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100756#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 f_menu_info
758#else
759 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100761 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100762 {"min", 1, 1, FEARG_1, ret_any, f_min},
763 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
764 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100765 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200766#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 f_mzeval
768#else
769 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200770#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100771 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100772 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
773 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
774 {"or", 2, 2, FEARG_1, ret_number, f_or},
775 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100776 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 f_perleval
779#else
780 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200781#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100782 },
783 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
784 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200785 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100786 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
787 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
788 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
789 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
790 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
791 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
792 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
793 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
794 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
795 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200796 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100797 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
798 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
799 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
800 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
801 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
802 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
803 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
804 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100805 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
806 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100807 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
808 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
809 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
810 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
811 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
812 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
813 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
814 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
815 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
816 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
817 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
818 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
819 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100820 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
821 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100822 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100824 f_py3eval
825#else
826 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100828 },
829 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100831 f_pyeval
832#else
833 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100835 },
836 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100837#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100838 f_pyxeval
839#else
840 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100841#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100843 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
844 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200845 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
846 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100847 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200848 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100849 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
850 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
851 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100852 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100853 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
854 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
855 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
856 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
857 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
858 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200859 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200860 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100861 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200862 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100863 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200864 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100865 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
866 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100867#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100868 f_rubyeval
869#else
870 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100871#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100872 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100873 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
874 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
875 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
876 {"screencol", 0, 0, 0, ret_number, f_screencol},
877 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
878 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
879 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200880 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200881 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100882 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
883 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
884 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200885 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100886 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
887 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
888 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
889 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200890 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100891 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
892 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
893 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
894 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
895 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
896 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
897 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
898 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
899 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
900 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
901 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
902 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
903 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
904 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100905 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100907 f_sha256
908#else
909 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100911 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100912 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
913 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100914 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
915 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
916 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
917 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
918 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
919 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
920 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
921 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
922 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200923 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100924 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
925 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200926 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100927 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
928 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
929 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
930 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100931 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
932 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
933 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
934 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100935 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100936 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
937 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100938 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100939 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
940 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
941 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
942 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
943 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100944 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100946 f_strftime
947#else
948 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200949#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100950 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100951 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
952 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
953 {"string", 1, 1, FEARG_1, ret_string, f_string},
954 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200955 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100956 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100957#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100958 f_strptime
959#else
960 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100961#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100962 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100963 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
964 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
965 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
966 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
967 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
968 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
969 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
970 {"synID", 3, 3, 0, ret_number, f_synID},
971 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
972 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
973 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
974 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
975 {"system", 1, 2, FEARG_1, ret_string, f_system},
976 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
977 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
978 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
979 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
980 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
981 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100982 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
983 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100984 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100985 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
986 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
987 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
988 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
989 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100990#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100991 f_term_getansicolors
992#else
993 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200994#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100995 },
996 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
997 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
998 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
999 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1000 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1001 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1002 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1003 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1004 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1005 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1006 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1007 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1008 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001009#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001010 f_term_setansicolors
1011#else
1012 NULL
1013#endif
1014 },
1015 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1016 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1017 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1018 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1019 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1020 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001021 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001022 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1023 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1024 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1025 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1026 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1027 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1028 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1029 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001030 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001031 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001032 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001033 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001034 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001035 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001036 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1037 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1038 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1039 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001040 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001041#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001042 f_test_scrollbar
1043#else
1044 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001045#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001046 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001047 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1048 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1049 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1050 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001051 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001052 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1053 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1054 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1055 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1056 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001057 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1058 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1059 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001060 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001061 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001062 {"type", 1, 1, FEARG_1, ret_number, f_type},
1063 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1064 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1065 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1066 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1067 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1068 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1069 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1070 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1071 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1072 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1073 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1074 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1075 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1076 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1077 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1078 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1079 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1080 {"wincol", 0, 0, 0, ret_number, f_wincol},
1081 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1082 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1083 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1084 {"winline", 0, 0, 0, ret_number, f_winline},
1085 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1086 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1087 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1088 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1089 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1090 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1091 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1092 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001093};
1094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001095/*
1096 * Function given to ExpandGeneric() to obtain the list of internal
1097 * or user defined function names.
1098 */
1099 char_u *
1100get_function_name(expand_T *xp, int idx)
1101{
1102 static int intidx = -1;
1103 char_u *name;
1104
1105 if (idx == 0)
1106 intidx = -1;
1107 if (intidx < 0)
1108 {
1109 name = get_user_func_name(xp, idx);
1110 if (name != NULL)
1111 return name;
1112 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001113 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001114 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001115 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001117 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001118 STRCAT(IObuff, ")");
1119 return IObuff;
1120 }
1121
1122 return NULL;
1123}
1124
1125/*
1126 * Function given to ExpandGeneric() to obtain the list of internal or
1127 * user defined variable or function names.
1128 */
1129 char_u *
1130get_expr_name(expand_T *xp, int idx)
1131{
1132 static int intidx = -1;
1133 char_u *name;
1134
1135 if (idx == 0)
1136 intidx = -1;
1137 if (intidx < 0)
1138 {
1139 name = get_function_name(xp, idx);
1140 if (name != NULL)
1141 return name;
1142 }
1143 return get_user_var_name(xp, ++intidx);
1144}
1145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001146/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001147 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001148 * Return index, or -1 if not found or "implemented" is TRUE and the function
1149 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001150 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001151 static int
1152find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001153{
1154 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001155 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001156 int cmp;
1157 int x;
1158
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001159 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001160
1161 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001162 while (first <= last)
1163 {
1164 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001165 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001166 if (cmp < 0)
1167 last = x - 1;
1168 else if (cmp > 0)
1169 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001170 else if (implemented && global_functions[x].f_func == NULL)
1171 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001172 else
1173 return x;
1174 }
1175 return -1;
1176}
1177
Bram Moolenaar15c47602020-03-26 22:16:48 +01001178/*
1179 * Find internal function "name" in table "global_functions".
1180 * Return index, or -1 if not found or the function is not implemented.
1181 */
1182 int
1183find_internal_func(char_u *name)
1184{
1185 return find_internal_func_opt(name, TRUE);
1186}
1187
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001188 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001189has_internal_func(char_u *name)
1190{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001191 return find_internal_func_opt(name, TRUE) >= 0;
1192}
1193
1194 static int
1195has_internal_func_name(char_u *name)
1196{
1197 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001198}
1199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 char *
1201internal_func_name(int idx)
1202{
1203 return global_functions[idx].f_name;
1204}
1205
1206 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001207internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001209 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210}
1211
1212/*
1213 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001214 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1215 * first argument, 2 if method base is second argument, etc. 9 if method base
1216 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 */
1218 int
1219check_internal_func(int idx, int argcount)
1220{
1221 int res;
1222 char *name;
1223
1224 if (argcount < global_functions[idx].f_min_argc)
1225 res = FCERR_TOOFEW;
1226 else if (argcount > global_functions[idx].f_max_argc)
1227 res = FCERR_TOOMANY;
1228 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001229 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230
1231 name = internal_func_name(idx);
1232 if (res == FCERR_TOOMANY)
1233 semsg(_(e_toomanyarg), name);
1234 else
1235 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001236 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237}
1238
Bram Moolenaarac92e252019-08-03 21:58:38 +02001239 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001240call_internal_func(
1241 char_u *name,
1242 int argcount,
1243 typval_T *argvars,
1244 typval_T *rettv)
1245{
1246 int i;
1247
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001248 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001250 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001251 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001252 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001253 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001254 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001255 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001256 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001257 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001258}
1259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 void
1261call_internal_func_by_idx(
1262 int idx,
1263 typval_T *argvars,
1264 typval_T *rettv)
1265{
1266 global_functions[idx].f_func(argvars, rettv);
1267}
1268
Bram Moolenaarac92e252019-08-03 21:58:38 +02001269/*
1270 * Invoke a method for base->method().
1271 */
1272 int
1273call_internal_method(
1274 char_u *name,
1275 int argcount,
1276 typval_T *argvars,
1277 typval_T *rettv,
1278 typval_T *basetv)
1279{
1280 int i;
1281 int fi;
1282 typval_T argv[MAX_FUNC_ARGS + 1];
1283
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001284 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001285 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001286 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001287 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001288 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001289 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001290 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001291 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001292 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001293
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001294 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001295 {
1296 // base value goes last
1297 for (i = 0; i < argcount; ++i)
1298 argv[i] = argvars[i];
1299 argv[argcount] = *basetv;
1300 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001301 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001302 {
1303 // base value goes second
1304 argv[0] = argvars[0];
1305 argv[1] = *basetv;
1306 for (i = 1; i < argcount; ++i)
1307 argv[i + 1] = argvars[i];
1308 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001309 else if (global_functions[fi].f_argtype == FEARG_3)
1310 {
1311 // base value goes third
1312 argv[0] = argvars[0];
1313 argv[1] = argvars[1];
1314 argv[2] = *basetv;
1315 for (i = 2; i < argcount; ++i)
1316 argv[i + 1] = argvars[i];
1317 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001318 else if (global_functions[fi].f_argtype == FEARG_4)
1319 {
1320 // base value goes fourth
1321 argv[0] = argvars[0];
1322 argv[1] = argvars[1];
1323 argv[2] = argvars[2];
1324 argv[3] = *basetv;
1325 for (i = 3; i < argcount; ++i)
1326 argv[i + 1] = argvars[i];
1327 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001328 else
1329 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001330 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001331 argv[0] = *basetv;
1332 for (i = 0; i < argcount; ++i)
1333 argv[i + 1] = argvars[i];
1334 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001335 argv[argcount + 1].v_type = VAR_UNKNOWN;
1336
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001337 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001338 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339}
1340
1341/*
1342 * Return TRUE for a non-zero Number and a non-empty String.
1343 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001344 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345non_zero_arg(typval_T *argvars)
1346{
1347 return ((argvars[0].v_type == VAR_NUMBER
1348 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001349 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350 && argvars[0].vval.v_number == VVAL_TRUE)
1351 || (argvars[0].v_type == VAR_STRING
1352 && argvars[0].vval.v_string != NULL
1353 && *argvars[0].vval.v_string != NUL));
1354}
1355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357/*
1358 * Get the float value of "argvars[0]" into "f".
1359 * Returns FAIL when the argument is not a Number or Float.
1360 */
1361 static int
1362get_float_arg(typval_T *argvars, float_T *f)
1363{
1364 if (argvars[0].v_type == VAR_FLOAT)
1365 {
1366 *f = argvars[0].vval.v_float;
1367 return OK;
1368 }
1369 if (argvars[0].v_type == VAR_NUMBER)
1370 {
1371 *f = (float_T)argvars[0].vval.v_number;
1372 return OK;
1373 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001374 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 return FAIL;
1376}
1377
1378/*
1379 * "abs(expr)" function
1380 */
1381 static void
1382f_abs(typval_T *argvars, typval_T *rettv)
1383{
1384 if (argvars[0].v_type == VAR_FLOAT)
1385 {
1386 rettv->v_type = VAR_FLOAT;
1387 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1388 }
1389 else
1390 {
1391 varnumber_T n;
1392 int error = FALSE;
1393
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001394 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395 if (error)
1396 rettv->vval.v_number = -1;
1397 else if (n > 0)
1398 rettv->vval.v_number = n;
1399 else
1400 rettv->vval.v_number = -n;
1401 }
1402}
1403
1404/*
1405 * "acos()" function
1406 */
1407 static void
1408f_acos(typval_T *argvars, typval_T *rettv)
1409{
1410 float_T f = 0.0;
1411
1412 rettv->v_type = VAR_FLOAT;
1413 if (get_float_arg(argvars, &f) == OK)
1414 rettv->vval.v_float = acos(f);
1415 else
1416 rettv->vval.v_float = 0.0;
1417}
1418#endif
1419
1420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001421 * "and(expr, expr)" function
1422 */
1423 static void
1424f_and(typval_T *argvars, typval_T *rettv)
1425{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001426 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1427 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001428}
1429
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001430#ifdef FEAT_FLOAT
1431/*
1432 * "asin()" function
1433 */
1434 static void
1435f_asin(typval_T *argvars, typval_T *rettv)
1436{
1437 float_T f = 0.0;
1438
1439 rettv->v_type = VAR_FLOAT;
1440 if (get_float_arg(argvars, &f) == OK)
1441 rettv->vval.v_float = asin(f);
1442 else
1443 rettv->vval.v_float = 0.0;
1444}
1445
1446/*
1447 * "atan()" function
1448 */
1449 static void
1450f_atan(typval_T *argvars, typval_T *rettv)
1451{
1452 float_T f = 0.0;
1453
1454 rettv->v_type = VAR_FLOAT;
1455 if (get_float_arg(argvars, &f) == OK)
1456 rettv->vval.v_float = atan(f);
1457 else
1458 rettv->vval.v_float = 0.0;
1459}
1460
1461/*
1462 * "atan2()" function
1463 */
1464 static void
1465f_atan2(typval_T *argvars, typval_T *rettv)
1466{
1467 float_T fx = 0.0, fy = 0.0;
1468
1469 rettv->v_type = VAR_FLOAT;
1470 if (get_float_arg(argvars, &fx) == OK
1471 && get_float_arg(&argvars[1], &fy) == OK)
1472 rettv->vval.v_float = atan2(fx, fy);
1473 else
1474 rettv->vval.v_float = 0.0;
1475}
1476#endif
1477
1478/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001479 * "balloon_show()" function
1480 */
1481#ifdef FEAT_BEVAL
1482 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001483f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1484{
1485 rettv->v_type = VAR_STRING;
1486 if (balloonEval != NULL)
1487 {
1488 if (balloonEval->msg == NULL)
1489 rettv->vval.v_string = NULL;
1490 else
1491 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1492 }
1493}
1494
1495 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001496f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1497{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001498 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001499 {
1500 if (argvars[0].v_type == VAR_LIST
1501# ifdef FEAT_GUI
1502 && !gui.in_use
1503# endif
1504 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001505 {
1506 list_T *l = argvars[0].vval.v_list;
1507
1508 // empty list removes the balloon
1509 post_balloon(balloonEval, NULL,
1510 l == NULL || l->lv_len == 0 ? NULL : l);
1511 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001512 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001513 {
1514 char_u *mesg = tv_get_string_chk(&argvars[0]);
1515
1516 if (mesg != NULL)
1517 // empty string removes the balloon
1518 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1519 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001520 }
1521}
1522
Bram Moolenaar669a8282017-11-19 20:13:05 +01001523# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001524 static void
1525f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1526{
1527 if (rettv_list_alloc(rettv) == OK)
1528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001529 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001530
1531 if (msg != NULL)
1532 {
1533 pumitem_T *array;
1534 int size = split_message(msg, &array);
1535 int i;
1536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001537 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001538 for (i = 1; i < size - 1; ++i)
1539 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001540 while (size > 0)
1541 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001542 vim_free(array);
1543 }
1544 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001545}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001546# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001547#endif
1548
1549/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001550 * Get the buffer from "arg" and give an error and return NULL if it is not
1551 * valid.
1552 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001553 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001554get_buf_arg(typval_T *arg)
1555{
1556 buf_T *buf;
1557
1558 ++emsg_off;
1559 buf = tv_get_buf(arg, FALSE);
1560 --emsg_off;
1561 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001562 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001563 return buf;
1564}
1565
1566/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 * "byte2line(byte)" function
1568 */
1569 static void
1570f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1571{
1572#ifndef FEAT_BYTEOFF
1573 rettv->vval.v_number = -1;
1574#else
1575 long boff = 0;
1576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001578 if (boff < 0)
1579 rettv->vval.v_number = -1;
1580 else
1581 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1582 (linenr_T)0, &boff);
1583#endif
1584}
1585
1586 static void
1587byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1588{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001590 char_u *str;
1591 varnumber_T idx;
1592
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 str = tv_get_string_chk(&argvars[0]);
1594 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 rettv->vval.v_number = -1;
1596 if (str == NULL || idx < 0)
1597 return;
1598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599 t = str;
1600 for ( ; idx > 0; idx--)
1601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001602 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 return;
1604 if (enc_utf8 && comp)
1605 t += utf_ptr2len(t);
1606 else
1607 t += (*mb_ptr2len)(t);
1608 }
1609 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610}
1611
1612/*
1613 * "byteidx()" function
1614 */
1615 static void
1616f_byteidx(typval_T *argvars, typval_T *rettv)
1617{
1618 byteidx(argvars, rettv, FALSE);
1619}
1620
1621/*
1622 * "byteidxcomp()" function
1623 */
1624 static void
1625f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1626{
1627 byteidx(argvars, rettv, TRUE);
1628}
1629
1630/*
1631 * "call(func, arglist [, dict])" function
1632 */
1633 static void
1634f_call(typval_T *argvars, typval_T *rettv)
1635{
1636 char_u *func;
1637 partial_T *partial = NULL;
1638 dict_T *selfdict = NULL;
1639
1640 if (argvars[1].v_type != VAR_LIST)
1641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643 return;
1644 }
1645 if (argvars[1].vval.v_list == NULL)
1646 return;
1647
1648 if (argvars[0].v_type == VAR_FUNC)
1649 func = argvars[0].vval.v_string;
1650 else if (argvars[0].v_type == VAR_PARTIAL)
1651 {
1652 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001653 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 }
1655 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001656 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001658 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659
1660 if (argvars[2].v_type != VAR_UNKNOWN)
1661 {
1662 if (argvars[2].v_type != VAR_DICT)
1663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001664 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665 return;
1666 }
1667 selfdict = argvars[2].vval.v_dict;
1668 }
1669
1670 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1671}
1672
1673#ifdef FEAT_FLOAT
1674/*
1675 * "ceil({float})" function
1676 */
1677 static void
1678f_ceil(typval_T *argvars, typval_T *rettv)
1679{
1680 float_T f = 0.0;
1681
1682 rettv->v_type = VAR_FLOAT;
1683 if (get_float_arg(argvars, &f) == OK)
1684 rettv->vval.v_float = ceil(f);
1685 else
1686 rettv->vval.v_float = 0.0;
1687}
1688#endif
1689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690/*
1691 * "changenr()" function
1692 */
1693 static void
1694f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1695{
1696 rettv->vval.v_number = curbuf->b_u_seq_cur;
1697}
1698
1699/*
1700 * "char2nr(string)" function
1701 */
1702 static void
1703f_char2nr(typval_T *argvars, typval_T *rettv)
1704{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705 if (has_mbyte)
1706 {
1707 int utf8 = 0;
1708
1709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001710 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711
1712 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001713 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001715 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716 }
1717 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001718 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719}
1720
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001721 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001722get_optional_window(typval_T *argvars, int idx)
1723{
1724 win_T *win = curwin;
1725
1726 if (argvars[idx].v_type != VAR_UNKNOWN)
1727 {
1728 win = find_win_by_nr_or_id(&argvars[idx]);
1729 if (win == NULL)
1730 {
1731 emsg(_(e_invalwindow));
1732 return NULL;
1733 }
1734 }
1735 return win;
1736}
1737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001738/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001739 * "col(string)" function
1740 */
1741 static void
1742f_col(typval_T *argvars, typval_T *rettv)
1743{
1744 colnr_T col = 0;
1745 pos_T *fp;
1746 int fnum = curbuf->b_fnum;
1747
1748 fp = var2fpos(&argvars[0], FALSE, &fnum);
1749 if (fp != NULL && fnum == curbuf->b_fnum)
1750 {
1751 if (fp->col == MAXCOL)
1752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001753 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001754 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1755 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1756 else
1757 col = MAXCOL;
1758 }
1759 else
1760 {
1761 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001762 // col(".") when the cursor is on the NUL at the end of the line
1763 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001764 if (virtual_active() && fp == &curwin->w_cursor)
1765 {
1766 char_u *p = ml_get_cursor();
1767
1768 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1769 curwin->w_virtcol - curwin->w_cursor.coladd))
1770 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001771 int l;
1772
1773 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1774 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001775 }
1776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001777 }
1778 }
1779 rettv->vval.v_number = col;
1780}
1781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001782/*
1783 * "confirm(message, buttons[, default [, type]])" function
1784 */
1785 static void
1786f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1787{
1788#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1789 char_u *message;
1790 char_u *buttons = NULL;
1791 char_u buf[NUMBUFLEN];
1792 char_u buf2[NUMBUFLEN];
1793 int def = 1;
1794 int type = VIM_GENERIC;
1795 char_u *typestr;
1796 int error = FALSE;
1797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001798 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001799 if (message == NULL)
1800 error = TRUE;
1801 if (argvars[1].v_type != VAR_UNKNOWN)
1802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001803 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001804 if (buttons == NULL)
1805 error = TRUE;
1806 if (argvars[2].v_type != VAR_UNKNOWN)
1807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001808 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001809 if (argvars[3].v_type != VAR_UNKNOWN)
1810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001811 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001812 if (typestr == NULL)
1813 error = TRUE;
1814 else
1815 {
1816 switch (TOUPPER_ASC(*typestr))
1817 {
1818 case 'E': type = VIM_ERROR; break;
1819 case 'Q': type = VIM_QUESTION; break;
1820 case 'I': type = VIM_INFO; break;
1821 case 'W': type = VIM_WARNING; break;
1822 case 'G': type = VIM_GENERIC; break;
1823 }
1824 }
1825 }
1826 }
1827 }
1828
1829 if (buttons == NULL || *buttons == NUL)
1830 buttons = (char_u *)_("&Ok");
1831
1832 if (!error)
1833 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1834 def, NULL, FALSE);
1835#endif
1836}
1837
1838/*
1839 * "copy()" function
1840 */
1841 static void
1842f_copy(typval_T *argvars, typval_T *rettv)
1843{
1844 item_copy(&argvars[0], rettv, FALSE, 0);
1845}
1846
1847#ifdef FEAT_FLOAT
1848/*
1849 * "cos()" function
1850 */
1851 static void
1852f_cos(typval_T *argvars, typval_T *rettv)
1853{
1854 float_T f = 0.0;
1855
1856 rettv->v_type = VAR_FLOAT;
1857 if (get_float_arg(argvars, &f) == OK)
1858 rettv->vval.v_float = cos(f);
1859 else
1860 rettv->vval.v_float = 0.0;
1861}
1862
1863/*
1864 * "cosh()" function
1865 */
1866 static void
1867f_cosh(typval_T *argvars, typval_T *rettv)
1868{
1869 float_T f = 0.0;
1870
1871 rettv->v_type = VAR_FLOAT;
1872 if (get_float_arg(argvars, &f) == OK)
1873 rettv->vval.v_float = cosh(f);
1874 else
1875 rettv->vval.v_float = 0.0;
1876}
1877#endif
1878
1879/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 * "cursor(lnum, col)" function, or
1881 * "cursor(list)"
1882 *
1883 * Moves the cursor to the specified line and column.
1884 * Returns 0 when the position could be set, -1 otherwise.
1885 */
1886 static void
1887f_cursor(typval_T *argvars, typval_T *rettv)
1888{
1889 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 int set_curswant = TRUE;
1892
1893 rettv->vval.v_number = -1;
1894 if (argvars[1].v_type == VAR_UNKNOWN)
1895 {
1896 pos_T pos;
1897 colnr_T curswant = -1;
1898
1899 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001901 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001902 return;
1903 }
1904 line = pos.lnum;
1905 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 if (curswant >= 0)
1908 {
1909 curwin->w_curswant = curswant - 1;
1910 set_curswant = FALSE;
1911 }
1912 }
1913 else
1914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001915 line = tv_get_lnum(argvars);
1916 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001918 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001920 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 if (line > 0)
1923 curwin->w_cursor.lnum = line;
1924 if (col > 0)
1925 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 if (has_mbyte)
1932 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933
1934 curwin->w_set_curswant = set_curswant;
1935 rettv->vval.v_number = 0;
1936}
1937
Bram Moolenaar4f974752019-02-17 17:44:42 +01001938#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001939/*
1940 * "debugbreak()" function
1941 */
1942 static void
1943f_debugbreak(typval_T *argvars, typval_T *rettv)
1944{
1945 int pid;
1946
1947 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001948 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001949 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001950 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001951 else
1952 {
1953 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1954
1955 if (hProcess != NULL)
1956 {
1957 DebugBreakProcess(hProcess);
1958 CloseHandle(hProcess);
1959 rettv->vval.v_number = OK;
1960 }
1961 }
1962}
1963#endif
1964
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001965/*
1966 * "deepcopy()" function
1967 */
1968 static void
1969f_deepcopy(typval_T *argvars, typval_T *rettv)
1970{
1971 int noref = 0;
1972 int copyID;
1973
1974 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001975 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978 else
1979 {
1980 copyID = get_copyID();
1981 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1982 }
1983}
1984
1985/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001986 * "did_filetype()" function
1987 */
1988 static void
1989f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1990{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001991 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001992}
1993
1994/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001995 * "echoraw({expr})" function
1996 */
1997 static void
1998f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1999{
2000 char_u *str = tv_get_string_chk(&argvars[0]);
2001
2002 if (str != NULL && *str != NUL)
2003 {
2004 out_str(str);
2005 out_flush();
2006 }
2007}
2008
2009/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002010 * "empty({expr})" function
2011 */
2012 static void
2013f_empty(typval_T *argvars, typval_T *rettv)
2014{
2015 int n = FALSE;
2016
2017 switch (argvars[0].v_type)
2018 {
2019 case VAR_STRING:
2020 case VAR_FUNC:
2021 n = argvars[0].vval.v_string == NULL
2022 || *argvars[0].vval.v_string == NUL;
2023 break;
2024 case VAR_PARTIAL:
2025 n = FALSE;
2026 break;
2027 case VAR_NUMBER:
2028 n = argvars[0].vval.v_number == 0;
2029 break;
2030 case VAR_FLOAT:
2031#ifdef FEAT_FLOAT
2032 n = argvars[0].vval.v_float == 0.0;
2033 break;
2034#endif
2035 case VAR_LIST:
2036 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002037 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002038 break;
2039 case VAR_DICT:
2040 n = argvars[0].vval.v_dict == NULL
2041 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2042 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002043 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002044 case VAR_SPECIAL:
2045 n = argvars[0].vval.v_number != VVAL_TRUE;
2046 break;
2047
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002048 case VAR_BLOB:
2049 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002050 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2051 break;
2052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 case VAR_JOB:
2054#ifdef FEAT_JOB_CHANNEL
2055 n = argvars[0].vval.v_job == NULL
2056 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2057 break;
2058#endif
2059 case VAR_CHANNEL:
2060#ifdef FEAT_JOB_CHANNEL
2061 n = argvars[0].vval.v_channel == NULL
2062 || !channel_is_open(argvars[0].vval.v_channel);
2063 break;
2064#endif
2065 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002066 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002068 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 n = TRUE;
2070 break;
2071 }
2072
2073 rettv->vval.v_number = n;
2074}
2075
2076/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002077 * "environ()" function
2078 */
2079 static void
2080f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2081{
2082#if !defined(AMIGA)
2083 int i = 0;
2084 char_u *entry, *value;
2085# ifdef MSWIN
2086 extern wchar_t **_wenviron;
2087# else
2088 extern char **environ;
2089# endif
2090
2091 if (rettv_dict_alloc(rettv) != OK)
2092 return;
2093
2094# ifdef MSWIN
2095 if (*_wenviron == NULL)
2096 return;
2097# else
2098 if (*environ == NULL)
2099 return;
2100# endif
2101
2102 for (i = 0; ; ++i)
2103 {
2104# ifdef MSWIN
2105 short_u *p;
2106
2107 if ((p = (short_u *)_wenviron[i]) == NULL)
2108 return;
2109 entry = utf16_to_enc(p, NULL);
2110# else
2111 if ((entry = (char_u *)environ[i]) == NULL)
2112 return;
2113 entry = vim_strsave(entry);
2114# endif
2115 if (entry == NULL) // out of memory
2116 return;
2117 if ((value = vim_strchr(entry, '=')) == NULL)
2118 {
2119 vim_free(entry);
2120 continue;
2121 }
2122 *value++ = NUL;
2123 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2124 vim_free(entry);
2125 }
2126#endif
2127}
2128
2129/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130 * "escape({string}, {chars})" function
2131 */
2132 static void
2133f_escape(typval_T *argvars, typval_T *rettv)
2134{
2135 char_u buf[NUMBUFLEN];
2136
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002137 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2138 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 rettv->v_type = VAR_STRING;
2140}
2141
2142/*
2143 * "eval()" function
2144 */
2145 static void
2146f_eval(typval_T *argvars, typval_T *rettv)
2147{
2148 char_u *s, *p;
2149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002150 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151 if (s != NULL)
2152 s = skipwhite(s);
2153
2154 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002155 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 {
2157 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 need_clr_eos = FALSE;
2160 rettv->v_type = VAR_NUMBER;
2161 rettv->vval.v_number = 0;
2162 }
2163 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002164 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165}
2166
2167/*
2168 * "eventhandler()" function
2169 */
2170 static void
2171f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2172{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002173 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174}
2175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176static garray_T redir_execute_ga;
2177
2178/*
2179 * Append "value[value_len]" to the execute() output.
2180 */
2181 void
2182execute_redir_str(char_u *value, int value_len)
2183{
2184 int len;
2185
2186 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002187 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002189 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002190 if (ga_grow(&redir_execute_ga, len) == OK)
2191 {
2192 mch_memmove((char *)redir_execute_ga.ga_data
2193 + redir_execute_ga.ga_len, value, len);
2194 redir_execute_ga.ga_len += len;
2195 }
2196}
2197
2198/*
2199 * Get next line from a list.
2200 * Called by do_cmdline() to get the next line.
2201 * Returns allocated string, or NULL for end of function.
2202 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002203 static char_u *
2204get_list_line(
2205 int c UNUSED,
2206 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002207 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002208 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209{
2210 listitem_T **p = (listitem_T **)cookie;
2211 listitem_T *item = *p;
2212 char_u buf[NUMBUFLEN];
2213 char_u *s;
2214
2215 if (item == NULL)
2216 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 *p = item->li_next;
2219 return s == NULL ? NULL : vim_strsave(s);
2220}
2221
2222/*
2223 * "execute()" function
2224 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002225 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002226execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227{
2228 char_u *cmd = NULL;
2229 list_T *list = NULL;
2230 int save_msg_silent = msg_silent;
2231 int save_emsg_silent = emsg_silent;
2232 int save_emsg_noredir = emsg_noredir;
2233 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002234 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002236 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002237 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002238
2239 rettv->vval.v_string = NULL;
2240 rettv->v_type = VAR_STRING;
2241
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002242 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002244 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002245 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002246 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002247 return;
2248 ++list->lv_refcount;
2249 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002250 else if (argvars[arg_off].v_type == VAR_JOB
2251 || argvars[arg_off].v_type == VAR_CHANNEL)
2252 {
2253 emsg(_(e_inval_string));
2254 return;
2255 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002256 else
2257 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002258 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 if (cmd == NULL)
2260 return;
2261 }
2262
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002263 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 {
2265 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002266 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002267
2268 if (s == NULL)
2269 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002270 if (*s == NUL)
2271 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 if (STRNCMP(s, "silent", 6) == 0)
2273 ++msg_silent;
2274 if (STRCMP(s, "silent!") == 0)
2275 {
2276 emsg_silent = TRUE;
2277 emsg_noredir = TRUE;
2278 }
2279 }
2280 else
2281 ++msg_silent;
2282
2283 if (redir_execute)
2284 save_ga = redir_execute_ga;
2285 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2286 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002287 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002288 if (!echo_output)
2289 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002290
2291 if (cmd != NULL)
2292 do_cmdline_cmd(cmd);
2293 else
2294 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002295 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002297 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002298 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299 do_cmdline(NULL, get_list_line, (void *)&item,
2300 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2301 --list->lv_refcount;
2302 }
2303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002304 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002305 if (ga_grow(&redir_execute_ga, 1) == OK)
2306 {
2307 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2308 rettv->vval.v_string = redir_execute_ga.ga_data;
2309 }
2310 else
2311 {
2312 ga_clear(&redir_execute_ga);
2313 rettv->vval.v_string = NULL;
2314 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002315 msg_silent = save_msg_silent;
2316 emsg_silent = save_emsg_silent;
2317 emsg_noredir = save_emsg_noredir;
2318
2319 redir_execute = save_redir_execute;
2320 if (redir_execute)
2321 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002322 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002324 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002325 if (echo_output)
2326 // When not working silently: put it in column zero. A following
2327 // "echon" will overwrite the message, unavoidably.
2328 msg_col = 0;
2329 else
2330 // When working silently: Put it back where it was, since nothing
2331 // should have been written.
2332 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002333}
2334
2335/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002336 * "execute()" function
2337 */
2338 static void
2339f_execute(typval_T *argvars, typval_T *rettv)
2340{
2341 execute_common(argvars, rettv, 0);
2342}
2343
2344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 * "exists()" function
2346 */
2347 static void
2348f_exists(typval_T *argvars, typval_T *rettv)
2349{
2350 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002353 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002354 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002355 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002356 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357 if (mch_getenv(p + 1) != NULL)
2358 n = TRUE;
2359 else
2360 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002361 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002362 p = expand_env_save(p);
2363 if (p != NULL && *p != '$')
2364 n = TRUE;
2365 vim_free(p);
2366 }
2367 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002368 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002370 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002372 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002374 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002376 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002378 else if (*p == '?') // internal function only
2379 {
2380 n = has_internal_func_name(p + 1);
2381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 else if (*p == ':')
2383 {
2384 n = cmd_exists(p + 1);
2385 }
2386 else if (*p == '#')
2387 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002388 if (p[1] == '#')
2389 n = autocmd_supported(p + 2);
2390 else
2391 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002392 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002393 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002395 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 }
2397
2398 rettv->vval.v_number = n;
2399}
2400
2401#ifdef FEAT_FLOAT
2402/*
2403 * "exp()" function
2404 */
2405 static void
2406f_exp(typval_T *argvars, typval_T *rettv)
2407{
2408 float_T f = 0.0;
2409
2410 rettv->v_type = VAR_FLOAT;
2411 if (get_float_arg(argvars, &f) == OK)
2412 rettv->vval.v_float = exp(f);
2413 else
2414 rettv->vval.v_float = 0.0;
2415}
2416#endif
2417
2418/*
2419 * "expand()" function
2420 */
2421 static void
2422f_expand(typval_T *argvars, typval_T *rettv)
2423{
2424 char_u *s;
2425 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002426 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2428 expand_T xpc;
2429 int error = FALSE;
2430 char_u *result;
2431
2432 rettv->v_type = VAR_STRING;
2433 if (argvars[1].v_type != VAR_UNKNOWN
2434 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002435 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002436 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002437 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002439 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 if (*s == '%' || *s == '#' || *s == '<')
2441 {
2442 ++emsg_off;
2443 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2444 --emsg_off;
2445 if (rettv->v_type == VAR_LIST)
2446 {
2447 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2448 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002449 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 }
2451 else
2452 rettv->vval.v_string = result;
2453 }
2454 else
2455 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002456 // When the optional second argument is non-zero, don't remove matches
2457 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002459 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 options |= WILD_KEEP_ALL;
2461 if (!error)
2462 {
2463 ExpandInit(&xpc);
2464 xpc.xp_context = EXPAND_FILES;
2465 if (p_wic)
2466 options += WILD_ICASE;
2467 if (rettv->v_type == VAR_STRING)
2468 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2469 options, WILD_ALL);
2470 else if (rettv_list_alloc(rettv) != FAIL)
2471 {
2472 int i;
2473
2474 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2475 for (i = 0; i < xpc.xp_numfiles; i++)
2476 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2477 ExpandCleanup(&xpc);
2478 }
2479 }
2480 else
2481 rettv->vval.v_string = NULL;
2482 }
2483}
2484
2485/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002486 * "expandcmd()" function
2487 * Expand all the special characters in a command string.
2488 */
2489 static void
2490f_expandcmd(typval_T *argvars, typval_T *rettv)
2491{
2492 exarg_T eap;
2493 char_u *cmdstr;
2494 char *errormsg = NULL;
2495
2496 rettv->v_type = VAR_STRING;
2497 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2498
2499 memset(&eap, 0, sizeof(eap));
2500 eap.cmd = cmdstr;
2501 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002502 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002503 eap.usefilter = FALSE;
2504 eap.nextcmd = NULL;
2505 eap.cmdidx = CMD_USER;
2506
2507 expand_filename(&eap, &cmdstr, &errormsg);
2508 if (errormsg != NULL && *errormsg != NUL)
2509 emsg(errormsg);
2510
2511 rettv->vval.v_string = cmdstr;
2512}
2513
2514/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002515 * "feedkeys()" function
2516 */
2517 static void
2518f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2519{
2520 int remap = TRUE;
2521 int insert = FALSE;
2522 char_u *keys, *flags;
2523 char_u nbuf[NUMBUFLEN];
2524 int typed = FALSE;
2525 int execute = FALSE;
2526 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002527 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528 char_u *keys_esc;
2529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002530 // This is not allowed in the sandbox. If the commands would still be
2531 // executed in the sandbox it would be OK, but it probably happens later,
2532 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 if (check_secure())
2534 return;
2535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002536 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537
2538 if (argvars[1].v_type != VAR_UNKNOWN)
2539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 for ( ; *flags != NUL; ++flags)
2542 {
2543 switch (*flags)
2544 {
2545 case 'n': remap = FALSE; break;
2546 case 'm': remap = TRUE; break;
2547 case 't': typed = TRUE; break;
2548 case 'i': insert = TRUE; break;
2549 case 'x': execute = TRUE; break;
2550 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002551 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002552 }
2553 }
2554 }
2555
2556 if (*keys != NUL || execute)
2557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002558 // Need to escape K_SPECIAL and CSI before putting the string in the
2559 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002560 keys_esc = vim_strsave_escape_csi(keys);
2561 if (keys_esc != NULL)
2562 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002563 if (lowlevel)
2564 {
2565#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002566 int idx;
2567 int len = (int)STRLEN(keys);
2568
2569 for (idx = 0; idx < len; ++idx)
2570 {
2571 // if a CTRL-C was typed, set got_int, similar to what
2572 // happens in fill_input_buf()
2573 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2574 got_int = TRUE;
2575 add_to_input_buf(keys + idx, 1);
2576 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002577#else
2578 emsg(_("E980: lowlevel input not supported"));
2579#endif
2580 }
2581 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002582 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002583 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002584 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002585 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002586#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002587 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002588#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002589 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002590 typebuf_was_filled = TRUE;
2591 }
2592 vim_free(keys_esc);
2593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594 if (execute)
2595 {
2596 int save_msg_scroll = msg_scroll;
2597
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002598 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002599 msg_scroll = FALSE;
2600
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002601 if (!dangerous)
2602 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002603 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002604 if (!dangerous)
2605 --ex_normal_busy;
2606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 msg_scroll |= save_msg_scroll;
2608 }
2609 }
2610 }
2611}
2612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002613#ifdef FEAT_FLOAT
2614/*
2615 * "float2nr({float})" function
2616 */
2617 static void
2618f_float2nr(typval_T *argvars, typval_T *rettv)
2619{
2620 float_T f = 0.0;
2621
2622 if (get_float_arg(argvars, &f) == OK)
2623 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002624 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002625 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002626 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002627 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628 else
2629 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 }
2631}
2632
2633/*
2634 * "floor({float})" function
2635 */
2636 static void
2637f_floor(typval_T *argvars, typval_T *rettv)
2638{
2639 float_T f = 0.0;
2640
2641 rettv->v_type = VAR_FLOAT;
2642 if (get_float_arg(argvars, &f) == OK)
2643 rettv->vval.v_float = floor(f);
2644 else
2645 rettv->vval.v_float = 0.0;
2646}
2647
2648/*
2649 * "fmod()" function
2650 */
2651 static void
2652f_fmod(typval_T *argvars, typval_T *rettv)
2653{
2654 float_T fx = 0.0, fy = 0.0;
2655
2656 rettv->v_type = VAR_FLOAT;
2657 if (get_float_arg(argvars, &fx) == OK
2658 && get_float_arg(&argvars[1], &fy) == OK)
2659 rettv->vval.v_float = fmod(fx, fy);
2660 else
2661 rettv->vval.v_float = 0.0;
2662}
2663#endif
2664
2665/*
2666 * "fnameescape({string})" function
2667 */
2668 static void
2669f_fnameescape(typval_T *argvars, typval_T *rettv)
2670{
2671 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002672 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 rettv->v_type = VAR_STRING;
2674}
2675
2676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 * "foreground()" function
2678 */
2679 static void
2680f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2681{
2682#ifdef FEAT_GUI
2683 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002684 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002686 return;
2687 }
2688#endif
2689#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691#endif
2692}
2693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002695common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696{
2697 char_u *s;
2698 char_u *name;
2699 int use_string = FALSE;
2700 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002701 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002702 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703
2704 if (argvars[0].v_type == VAR_FUNC)
2705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002706 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 s = argvars[0].vval.v_string;
2708 }
2709 else if (argvars[0].v_type == VAR_PARTIAL
2710 && argvars[0].vval.v_partial != NULL)
2711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002712 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002714 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 }
2716 else
2717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002718 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002719 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002720 use_string = TRUE;
2721 }
2722
Bram Moolenaar843b8842016-08-21 14:36:15 +02002723 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002724 {
2725 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002726 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002727 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2728 if (*name != NUL)
2729 s = NULL;
2730 }
2731
Bram Moolenaar843b8842016-08-21 14:36:15 +02002732 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2733 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002734 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002736 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002737 ? find_func(trans_name, is_global, NULL) == NULL
2738 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002739 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002740 else
2741 {
2742 int dict_idx = 0;
2743 int arg_idx = 0;
2744 list_T *list = NULL;
2745
2746 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2747 {
2748 char sid_buf[25];
2749 int off = *s == 's' ? 2 : 5;
2750
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002751 // Expand s: and <SID> into <SNR>nr_, so that the function can
2752 // also be called from another script. Using trans_function_name()
2753 // would also work, but some plugins depend on the name being
2754 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002755 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002756 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 if (name != NULL)
2758 {
2759 STRCPY(name, sid_buf);
2760 STRCAT(name, s + off);
2761 }
2762 }
2763 else
2764 name = vim_strsave(s);
2765
2766 if (argvars[1].v_type != VAR_UNKNOWN)
2767 {
2768 if (argvars[2].v_type != VAR_UNKNOWN)
2769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002770 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 arg_idx = 1;
2772 dict_idx = 2;
2773 }
2774 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002775 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002776 dict_idx = 1;
2777 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002778 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 arg_idx = 1;
2780 if (dict_idx > 0)
2781 {
2782 if (argvars[dict_idx].v_type != VAR_DICT)
2783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002784 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002786 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 }
2788 if (argvars[dict_idx].vval.v_dict == NULL)
2789 dict_idx = 0;
2790 }
2791 if (arg_idx > 0)
2792 {
2793 if (argvars[arg_idx].v_type != VAR_LIST)
2794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002795 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002797 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 }
2799 list = argvars[arg_idx].vval.v_list;
2800 if (list == NULL || list->lv_len == 0)
2801 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002802 else if (list->lv_len > MAX_FUNC_ARGS)
2803 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002804 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002805 vim_free(name);
2806 goto theend;
2807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002808 }
2809 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002810 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002812 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815 if (pt == NULL)
2816 vim_free(name);
2817 else
2818 {
2819 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2820 {
2821 listitem_T *li;
2822 int i = 0;
2823 int arg_len = 0;
2824 int lv_len = 0;
2825
2826 if (arg_pt != NULL)
2827 arg_len = arg_pt->pt_argc;
2828 if (list != NULL)
2829 lv_len = list->lv_len;
2830 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002831 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832 if (pt->pt_argv == NULL)
2833 {
2834 vim_free(pt);
2835 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002836 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002838 for (i = 0; i < arg_len; i++)
2839 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2840 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002841 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002842 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002843 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002844 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002845 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 }
2847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002848 // For "function(dict.func, [], dict)" and "func" is a partial
2849 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 if (dict_idx > 0)
2851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002852 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002853 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2854 ++pt->pt_dict->dv_refcount;
2855 }
2856 else if (arg_pt != NULL)
2857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002858 // If the dict was bound automatically the result is also
2859 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002860 pt->pt_dict = arg_pt->pt_dict;
2861 pt->pt_auto = arg_pt->pt_auto;
2862 if (pt->pt_dict != NULL)
2863 ++pt->pt_dict->dv_refcount;
2864 }
2865
2866 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002867 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2868 {
2869 pt->pt_func = arg_pt->pt_func;
2870 func_ptr_ref(pt->pt_func);
2871 vim_free(name);
2872 }
2873 else if (is_funcref)
2874 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002875 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002876 func_ptr_ref(pt->pt_func);
2877 vim_free(name);
2878 }
2879 else
2880 {
2881 pt->pt_name = name;
2882 func_ref(name);
2883 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002884 }
2885 rettv->v_type = VAR_PARTIAL;
2886 rettv->vval.v_partial = pt;
2887 }
2888 else
2889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002890 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002891 rettv->v_type = VAR_FUNC;
2892 rettv->vval.v_string = name;
2893 func_ref(name);
2894 }
2895 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002896theend:
2897 vim_free(trans_name);
2898}
2899
2900/*
2901 * "funcref()" function
2902 */
2903 static void
2904f_funcref(typval_T *argvars, typval_T *rettv)
2905{
2906 common_function(argvars, rettv, TRUE);
2907}
2908
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002909 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002910ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002911{
2912 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2913 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002914 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002915}
2916
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002917/*
2918 * "function()" function
2919 */
2920 static void
2921f_function(typval_T *argvars, typval_T *rettv)
2922{
2923 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924}
2925
2926/*
2927 * "garbagecollect()" function
2928 */
2929 static void
2930f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2931{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002932 // This is postponed until we are back at the toplevel, because we may be
2933 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934 want_garbage_collect = TRUE;
2935
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002936 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 garbage_collect_at_exit = TRUE;
2938}
2939
2940/*
2941 * "get()" function
2942 */
2943 static void
2944f_get(typval_T *argvars, typval_T *rettv)
2945{
2946 listitem_T *li;
2947 list_T *l;
2948 dictitem_T *di;
2949 dict_T *d;
2950 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002951 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002953 if (argvars[0].v_type == VAR_BLOB)
2954 {
2955 int error = FALSE;
2956 int idx = tv_get_number_chk(&argvars[1], &error);
2957
2958 if (!error)
2959 {
2960 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002961 if (idx < 0)
2962 idx = blob_len(argvars[0].vval.v_blob) + idx;
2963 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2964 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002965 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002966 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002967 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002968 tv = rettv;
2969 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002970 }
2971 }
2972 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 {
2974 if ((l = argvars[0].vval.v_list) != NULL)
2975 {
2976 int error = FALSE;
2977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002978 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 if (!error && li != NULL)
2980 tv = &li->li_tv;
2981 }
2982 }
2983 else if (argvars[0].v_type == VAR_DICT)
2984 {
2985 if ((d = argvars[0].vval.v_dict) != NULL)
2986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002987 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002988 if (di != NULL)
2989 tv = &di->di_tv;
2990 }
2991 }
2992 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2993 {
2994 partial_T *pt;
2995 partial_T fref_pt;
2996
2997 if (argvars[0].v_type == VAR_PARTIAL)
2998 pt = argvars[0].vval.v_partial;
2999 else
3000 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003001 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 fref_pt.pt_name = argvars[0].vval.v_string;
3003 pt = &fref_pt;
3004 }
3005
3006 if (pt != NULL)
3007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003008 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003009 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010
3011 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3012 {
3013 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003014 n = partial_name(pt);
3015 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 rettv->vval.v_string = NULL;
3017 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003018 {
3019 rettv->vval.v_string = vim_strsave(n);
3020 if (rettv->v_type == VAR_FUNC)
3021 func_ref(rettv->vval.v_string);
3022 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 }
3024 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003025 {
3026 what_is_dict = TRUE;
3027 if (pt->pt_dict != NULL)
3028 rettv_dict_set(rettv, pt->pt_dict);
3029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 else if (STRCMP(what, "args") == 0)
3031 {
3032 rettv->v_type = VAR_LIST;
3033 if (rettv_list_alloc(rettv) == OK)
3034 {
3035 int i;
3036
3037 for (i = 0; i < pt->pt_argc; ++i)
3038 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3039 }
3040 }
3041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003042 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003043
3044 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3045 // third argument
3046 if (!what_is_dict)
3047 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048 }
3049 }
3050 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003051 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052
3053 if (tv == NULL)
3054 {
3055 if (argvars[2].v_type != VAR_UNKNOWN)
3056 copy_tv(&argvars[2], rettv);
3057 }
3058 else
3059 copy_tv(tv, rettv);
3060}
3061
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003062/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003063 * "getchangelist()" function
3064 */
3065 static void
3066f_getchangelist(typval_T *argvars, typval_T *rettv)
3067{
3068#ifdef FEAT_JUMPLIST
3069 buf_T *buf;
3070 int i;
3071 list_T *l;
3072 dict_T *d;
3073#endif
3074
3075 if (rettv_list_alloc(rettv) != OK)
3076 return;
3077
3078#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003079 if (argvars[0].v_type == VAR_UNKNOWN)
3080 buf = curbuf;
3081 else
3082 {
3083 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3084 ++emsg_off;
3085 buf = tv_get_buf(&argvars[0], FALSE);
3086 --emsg_off;
3087 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003088 if (buf == NULL)
3089 return;
3090
3091 l = list_alloc();
3092 if (l == NULL)
3093 return;
3094
3095 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3096 return;
3097 /*
3098 * The current window change list index tracks only the position in the
3099 * current buffer change list. For other buffers, use the change list
3100 * length as the current index.
3101 */
3102 list_append_number(rettv->vval.v_list,
3103 (varnumber_T)((buf == curwin->w_buffer)
3104 ? curwin->w_changelistidx : buf->b_changelistlen));
3105
3106 for (i = 0; i < buf->b_changelistlen; ++i)
3107 {
3108 if (buf->b_changelist[i].lnum == 0)
3109 continue;
3110 if ((d = dict_alloc()) == NULL)
3111 return;
3112 if (list_append_dict(l, d) == FAIL)
3113 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003114 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3115 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003116 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003117 }
3118#endif
3119}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120
3121/*
3122 * "getcharsearch()" function
3123 */
3124 static void
3125f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3126{
3127 if (rettv_dict_alloc(rettv) != FAIL)
3128 {
3129 dict_T *dict = rettv->vval.v_dict;
3130
Bram Moolenaare0be1672018-07-08 16:50:37 +02003131 dict_add_string(dict, "char", last_csearch());
3132 dict_add_number(dict, "forward", last_csearch_forward());
3133 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 }
3135}
3136
3137/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003138 * "getenv()" function
3139 */
3140 static void
3141f_getenv(typval_T *argvars, typval_T *rettv)
3142{
3143 int mustfree = FALSE;
3144 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3145
3146 if (p == NULL)
3147 {
3148 rettv->v_type = VAR_SPECIAL;
3149 rettv->vval.v_number = VVAL_NULL;
3150 return;
3151 }
3152 if (!mustfree)
3153 p = vim_strsave(p);
3154 rettv->vval.v_string = p;
3155 rettv->v_type = VAR_STRING;
3156}
3157
3158/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003159 * "getfontname()" function
3160 */
3161 static void
3162f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3163{
3164 rettv->v_type = VAR_STRING;
3165 rettv->vval.v_string = NULL;
3166#ifdef FEAT_GUI
3167 if (gui.in_use)
3168 {
3169 GuiFont font;
3170 char_u *name = NULL;
3171
3172 if (argvars[0].v_type == VAR_UNKNOWN)
3173 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003174 // Get the "Normal" font. Either the name saved by
3175 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003176 font = gui.norm_font;
3177 name = hl_get_font_name();
3178 }
3179 else
3180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003181 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003183 return;
3184 font = gui_mch_get_font(name, FALSE);
3185 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003186 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 }
3188 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3189 if (argvars[0].v_type != VAR_UNKNOWN)
3190 gui_mch_free_font(font);
3191 }
3192#endif
3193}
3194
3195/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003196 * "getjumplist()" function
3197 */
3198 static void
3199f_getjumplist(typval_T *argvars, typval_T *rettv)
3200{
3201#ifdef FEAT_JUMPLIST
3202 win_T *wp;
3203 int i;
3204 list_T *l;
3205 dict_T *d;
3206#endif
3207
3208 if (rettv_list_alloc(rettv) != OK)
3209 return;
3210
3211#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003212 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003213 if (wp == NULL)
3214 return;
3215
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003216 cleanup_jumplist(wp, TRUE);
3217
Bram Moolenaar4f505882018-02-10 21:06:32 +01003218 l = list_alloc();
3219 if (l == NULL)
3220 return;
3221
3222 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3223 return;
3224 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3225
3226 for (i = 0; i < wp->w_jumplistlen; ++i)
3227 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003228 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3229 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003230 if ((d = dict_alloc()) == NULL)
3231 return;
3232 if (list_append_dict(l, d) == FAIL)
3233 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003234 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3235 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003237 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003238 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003239 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003240 }
3241#endif
3242}
3243
3244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245 * "getpid()" function
3246 */
3247 static void
3248f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3249{
3250 rettv->vval.v_number = mch_get_pid();
3251}
3252
3253 static void
3254getpos_both(
3255 typval_T *argvars,
3256 typval_T *rettv,
3257 int getcurpos)
3258{
3259 pos_T *fp;
3260 list_T *l;
3261 int fnum = -1;
3262
3263 if (rettv_list_alloc(rettv) == OK)
3264 {
3265 l = rettv->vval.v_list;
3266 if (getcurpos)
3267 fp = &curwin->w_cursor;
3268 else
3269 fp = var2fpos(&argvars[0], TRUE, &fnum);
3270 if (fnum != -1)
3271 list_append_number(l, (varnumber_T)fnum);
3272 else
3273 list_append_number(l, (varnumber_T)0);
3274 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3275 : (varnumber_T)0);
3276 list_append_number(l, (fp != NULL)
3277 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3278 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003279 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 (varnumber_T)0);
3281 if (getcurpos)
3282 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003283 int save_set_curswant = curwin->w_set_curswant;
3284 colnr_T save_curswant = curwin->w_curswant;
3285 colnr_T save_virtcol = curwin->w_virtcol;
3286
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 update_curswant();
3288 list_append_number(l, curwin->w_curswant == MAXCOL ?
3289 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003290
3291 // Do not change "curswant", as it is unexpected that a get
3292 // function has a side effect.
3293 if (save_set_curswant)
3294 {
3295 curwin->w_set_curswant = save_set_curswant;
3296 curwin->w_curswant = save_curswant;
3297 curwin->w_virtcol = save_virtcol;
3298 curwin->w_valid &= ~VALID_VIRTCOL;
3299 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 }
3301 }
3302 else
3303 rettv->vval.v_number = FALSE;
3304}
3305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306/*
3307 * "getcurpos()" function
3308 */
3309 static void
3310f_getcurpos(typval_T *argvars, typval_T *rettv)
3311{
3312 getpos_both(argvars, rettv, TRUE);
3313}
3314
3315/*
3316 * "getpos(string)" function
3317 */
3318 static void
3319f_getpos(typval_T *argvars, typval_T *rettv)
3320{
3321 getpos_both(argvars, rettv, FALSE);
3322}
3323
3324/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 * "getreg()" function
3326 */
3327 static void
3328f_getreg(typval_T *argvars, typval_T *rettv)
3329{
3330 char_u *strregname;
3331 int regname;
3332 int arg2 = FALSE;
3333 int return_list = FALSE;
3334 int error = FALSE;
3335
3336 if (argvars[0].v_type != VAR_UNKNOWN)
3337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003338 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003339 error = strregname == NULL;
3340 if (argvars[1].v_type != VAR_UNKNOWN)
3341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003342 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003344 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 }
3346 }
3347 else
3348 strregname = get_vim_var_str(VV_REG);
3349
3350 if (error)
3351 return;
3352
3353 regname = (strregname == NULL ? '"' : *strregname);
3354 if (regname == 0)
3355 regname = '"';
3356
3357 if (return_list)
3358 {
3359 rettv->v_type = VAR_LIST;
3360 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3361 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3362 if (rettv->vval.v_list == NULL)
3363 (void)rettv_list_alloc(rettv);
3364 else
3365 ++rettv->vval.v_list->lv_refcount;
3366 }
3367 else
3368 {
3369 rettv->v_type = VAR_STRING;
3370 rettv->vval.v_string = get_reg_contents(regname,
3371 arg2 ? GREG_EXPR_SRC : 0);
3372 }
3373}
3374
3375/*
3376 * "getregtype()" function
3377 */
3378 static void
3379f_getregtype(typval_T *argvars, typval_T *rettv)
3380{
3381 char_u *strregname;
3382 int regname;
3383 char_u buf[NUMBUFLEN + 2];
3384 long reglen = 0;
3385
3386 if (argvars[0].v_type != VAR_UNKNOWN)
3387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003388 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 {
3391 rettv->v_type = VAR_STRING;
3392 rettv->vval.v_string = NULL;
3393 return;
3394 }
3395 }
3396 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003397 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398 strregname = get_vim_var_str(VV_REG);
3399
3400 regname = (strregname == NULL ? '"' : *strregname);
3401 if (regname == 0)
3402 regname = '"';
3403
3404 buf[0] = NUL;
3405 buf[1] = NUL;
3406 switch (get_reg_type(regname, &reglen))
3407 {
3408 case MLINE: buf[0] = 'V'; break;
3409 case MCHAR: buf[0] = 'v'; break;
3410 case MBLOCK:
3411 buf[0] = Ctrl_V;
3412 sprintf((char *)buf + 1, "%ld", reglen + 1);
3413 break;
3414 }
3415 rettv->v_type = VAR_STRING;
3416 rettv->vval.v_string = vim_strsave(buf);
3417}
3418
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003419/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003420 * "gettagstack()" function
3421 */
3422 static void
3423f_gettagstack(typval_T *argvars, typval_T *rettv)
3424{
3425 win_T *wp = curwin; // default is current window
3426
3427 if (rettv_dict_alloc(rettv) != OK)
3428 return;
3429
3430 if (argvars[0].v_type != VAR_UNKNOWN)
3431 {
3432 wp = find_win_by_nr_or_id(&argvars[0]);
3433 if (wp == NULL)
3434 return;
3435 }
3436
3437 get_tagstack(wp, rettv->vval.v_dict);
3438}
3439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003440// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441#include "version.h"
3442
3443/*
3444 * "has()" function
3445 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003446 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447f_has(typval_T *argvars, typval_T *rettv)
3448{
3449 int i;
3450 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003451 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003453 typedef struct {
3454 char *name;
3455 short present;
3456 } has_item_T;
3457 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003459 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003461 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003462#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003463 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003464#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003465 },
3466 {"arp",
3467#if defined(AMIGA) && defined(FEAT_ARP)
3468 1
3469#else
3470 0
3471#endif
3472 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003473 {"haiku",
3474#ifdef __HAIKU__
3475 1
3476#else
3477 0
3478#endif
3479 },
3480 {"bsd",
3481#if defined(BSD) && !defined(MACOS_X)
3482 1
3483#else
3484 0
3485#endif
3486 },
3487 {"hpux",
3488#ifdef hpux
3489 1
3490#else
3491 0
3492#endif
3493 },
3494 {"linux",
3495#ifdef __linux__
3496 1
3497#else
3498 0
3499#endif
3500 },
3501 {"mac", // Mac OS X (and, once, Mac OS Classic)
3502#ifdef MACOS_X
3503 1
3504#else
3505 0
3506#endif
3507 },
3508 {"osx", // Mac OS X
3509#ifdef MACOS_X
3510 1
3511#else
3512 0
3513#endif
3514 },
3515 {"macunix", // Mac OS X, with the darwin feature
3516#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3517 1
3518#else
3519 0
3520#endif
3521 },
3522 {"osxdarwin", // synonym for macunix
3523#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3524 1
3525#else
3526 0
3527#endif
3528 },
3529 {"qnx",
3530#ifdef __QNX__
3531 1
3532#else
3533 0
3534#endif
3535 },
3536 {"sun",
3537#ifdef SUN_SYSTEM
3538 1
3539#else
3540 0
3541#endif
3542 },
3543 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003545 1
3546#else
3547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003549 },
3550 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003552 1
3553#else
3554 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003556 },
3557 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003558#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003559 1
3560#else
3561 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003563 },
3564 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003565#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003566 1
3567#else
3568 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003570 },
3571 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003572#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003573 1
3574#else
3575 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 },
3578 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003580 1
3581#else
3582 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003584 },
3585 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003587 1
3588#else
3589 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003591 },
3592 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003594 1
3595#else
3596 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003598 },
3599 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003601 1
3602#else
3603 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003605 },
3606 {"autocmd", 1},
3607 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003608#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003609 1
3610#else
3611 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003612#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 },
3614 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003615#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003616 1
3617#else
3618 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003619#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003620 },
3621 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003622#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003623 1
3624#else
3625 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003627 },
3628 {"balloon_multiline",
3629#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3630 // MS-Windows requires runtime check, see below
3631 1
3632#else
3633 0
3634#endif
3635 },
3636 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003637#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003638 1
3639#else
3640 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003641#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003642 },
3643 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003645 1
3646#else
3647 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003649 },
3650 {"all_builtin_terms",
3651#if defined(ALL_BUILTIN_TCAPS)
3652 1
3653#else
3654 0
3655#endif
3656 },
3657 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003659 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003661 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003663 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003665 },
3666 {"byte_offset",
3667#ifdef FEAT_BYTEOFF
3668 1
3669#else
3670 0
3671#endif
3672 },
3673 {"channel",
3674#ifdef FEAT_JOB_CHANNEL
3675 1
3676#else
3677 0
3678#endif
3679 },
3680 {"cindent",
3681#ifdef FEAT_CINDENT
3682 1
3683#else
3684 0
3685#endif
3686 },
3687 {"clientserver",
3688#ifdef FEAT_CLIENTSERVER
3689 1
3690#else
3691 0
3692#endif
3693 },
3694 {"clipboard",
3695#ifdef FEAT_CLIPBOARD
3696 1
3697#else
3698 0
3699#endif
3700 },
3701 {"cmdline_compl", 1},
3702 {"cmdline_hist", 1},
3703 {"comments", 1},
3704 {"conceal",
3705#ifdef FEAT_CONCEAL
3706 1
3707#else
3708 0
3709#endif
3710 },
3711 {"cryptv",
3712#ifdef FEAT_CRYPT
3713 1
3714#else
3715 0
3716#endif
3717 },
3718 {"crypt-blowfish",
3719#ifdef FEAT_CRYPT
3720 1
3721#else
3722 0
3723#endif
3724 },
3725 {"crypt-blowfish2",
3726#ifdef FEAT_CRYPT
3727 1
3728#else
3729 0
3730#endif
3731 },
3732 {"cscope",
3733#ifdef FEAT_CSCOPE
3734 1
3735#else
3736 0
3737#endif
3738 },
3739 {"cursorbind", 1},
3740 {"cursorshape",
3741#ifdef CURSOR_SHAPE
3742 1
3743#else
3744 0
3745#endif
3746 },
3747 {"debug",
3748#ifdef DEBUG
3749 1
3750#else
3751 0
3752#endif
3753 },
3754 {"dialog_con",
3755#ifdef FEAT_CON_DIALOG
3756 1
3757#else
3758 0
3759#endif
3760 },
3761 {"dialog_gui",
3762#ifdef FEAT_GUI_DIALOG
3763 1
3764#else
3765 0
3766#endif
3767 },
3768 {"diff",
3769#ifdef FEAT_DIFF
3770 1
3771#else
3772 0
3773#endif
3774 },
3775 {"digraphs",
3776#ifdef FEAT_DIGRAPHS
3777 1
3778#else
3779 0
3780#endif
3781 },
3782 {"directx",
3783#ifdef FEAT_DIRECTX
3784 1
3785#else
3786 0
3787#endif
3788 },
3789 {"dnd",
3790#ifdef FEAT_DND
3791 1
3792#else
3793 0
3794#endif
3795 },
3796 {"emacs_tags",
3797#ifdef FEAT_EMACS_TAGS
3798 1
3799#else
3800 0
3801#endif
3802 },
3803 {"eval", 1}, // always present, of course!
3804 {"ex_extra", 1}, // graduated feature
3805 {"extra_search",
3806#ifdef FEAT_SEARCH_EXTRA
3807 1
3808#else
3809 0
3810#endif
3811 },
3812 {"file_in_path",
3813#ifdef FEAT_SEARCHPATH
3814 1
3815#else
3816 0
3817#endif
3818 },
3819 {"filterpipe",
3820#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3821 1
3822#else
3823 0
3824#endif
3825 },
3826 {"find_in_path",
3827#ifdef FEAT_FIND_ID
3828 1
3829#else
3830 0
3831#endif
3832 },
3833 {"float",
3834#ifdef FEAT_FLOAT
3835 1
3836#else
3837 0
3838#endif
3839 },
3840 {"folding",
3841#ifdef FEAT_FOLDING
3842 1
3843#else
3844 0
3845#endif
3846 },
3847 {"footer",
3848#ifdef FEAT_FOOTER
3849 1
3850#else
3851 0
3852#endif
3853 },
3854 {"fork",
3855#if !defined(USE_SYSTEM) && defined(UNIX)
3856 1
3857#else
3858 0
3859#endif
3860 },
3861 {"gettext",
3862#ifdef FEAT_GETTEXT
3863 1
3864#else
3865 0
3866#endif
3867 },
3868 {"gui",
3869#ifdef FEAT_GUI
3870 1
3871#else
3872 0
3873#endif
3874 },
3875 {"gui_neXtaw",
3876#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3877 1
3878#else
3879 0
3880#endif
3881 },
3882 {"gui_athena",
3883#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3884 1
3885#else
3886 0
3887#endif
3888 },
3889 {"gui_gtk",
3890#ifdef FEAT_GUI_GTK
3891 1
3892#else
3893 0
3894#endif
3895 },
3896 {"gui_gtk2",
3897#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3898 1
3899#else
3900 0
3901#endif
3902 },
3903 {"gui_gtk3",
3904#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3905 1
3906#else
3907 0
3908#endif
3909 },
3910 {"gui_gnome",
3911#ifdef FEAT_GUI_GNOME
3912 1
3913#else
3914 0
3915#endif
3916 },
3917 {"gui_haiku",
3918#ifdef FEAT_GUI_HAIKU
3919 1
3920#else
3921 0
3922#endif
3923 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003924 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003925 {"gui_motif",
3926#ifdef FEAT_GUI_MOTIF
3927 1
3928#else
3929 0
3930#endif
3931 },
3932 {"gui_photon",
3933#ifdef FEAT_GUI_PHOTON
3934 1
3935#else
3936 0
3937#endif
3938 },
3939 {"gui_win32",
3940#ifdef FEAT_GUI_MSWIN
3941 1
3942#else
3943 0
3944#endif
3945 },
3946 {"iconv",
3947#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3948 1
3949#else
3950 0
3951#endif
3952 },
3953 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003954 {"ipv6",
3955#ifdef FEAT_IPV6
3956 1
3957#else
3958 0
3959#endif
3960 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003961 {"job",
3962#ifdef FEAT_JOB_CHANNEL
3963 1
3964#else
3965 0
3966#endif
3967 },
3968 {"jumplist",
3969#ifdef FEAT_JUMPLIST
3970 1
3971#else
3972 0
3973#endif
3974 },
3975 {"keymap",
3976#ifdef FEAT_KEYMAP
3977 1
3978#else
3979 0
3980#endif
3981 },
3982 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3983 {"langmap",
3984#ifdef FEAT_LANGMAP
3985 1
3986#else
3987 0
3988#endif
3989 },
3990 {"libcall",
3991#ifdef FEAT_LIBCALL
3992 1
3993#else
3994 0
3995#endif
3996 },
3997 {"linebreak",
3998#ifdef FEAT_LINEBREAK
3999 1
4000#else
4001 0
4002#endif
4003 },
4004 {"lispindent",
4005#ifdef FEAT_LISP
4006 1
4007#else
4008 0
4009#endif
4010 },
4011 {"listcmds", 1},
4012 {"localmap", 1},
4013 {"lua",
4014#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4015 1
4016#else
4017 0
4018#endif
4019 },
4020 {"menu",
4021#ifdef FEAT_MENU
4022 1
4023#else
4024 0
4025#endif
4026 },
4027 {"mksession",
4028#ifdef FEAT_SESSION
4029 1
4030#else
4031 0
4032#endif
4033 },
4034 {"modify_fname", 1},
4035 {"mouse", 1},
4036 {"mouseshape",
4037#ifdef FEAT_MOUSESHAPE
4038 1
4039#else
4040 0
4041#endif
4042 },
4043 {"mouse_dec",
4044#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4045 1
4046#else
4047 0
4048#endif
4049 },
4050 {"mouse_gpm",
4051#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4052 1
4053#else
4054 0
4055#endif
4056 },
4057 {"mouse_jsbterm",
4058#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4059 1
4060#else
4061 0
4062#endif
4063 },
4064 {"mouse_netterm",
4065#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4066 1
4067#else
4068 0
4069#endif
4070 },
4071 {"mouse_pterm",
4072#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4073 1
4074#else
4075 0
4076#endif
4077 },
4078 {"mouse_sgr",
4079#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4080 1
4081#else
4082 0
4083#endif
4084 },
4085 {"mouse_sysmouse",
4086#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4087 1
4088#else
4089 0
4090#endif
4091 },
4092 {"mouse_urxvt",
4093#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4094 1
4095#else
4096 0
4097#endif
4098 },
4099 {"mouse_xterm",
4100#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4101 1
4102#else
4103 0
4104#endif
4105 },
4106 {"multi_byte", 1},
4107 {"multi_byte_ime",
4108#ifdef FEAT_MBYTE_IME
4109 1
4110#else
4111 0
4112#endif
4113 },
4114 {"multi_lang",
4115#ifdef FEAT_MULTI_LANG
4116 1
4117#else
4118 0
4119#endif
4120 },
4121 {"mzscheme",
4122#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4123 1
4124#else
4125 0
4126#endif
4127 },
4128 {"num64", 1},
4129 {"ole",
4130#ifdef FEAT_OLE
4131 1
4132#else
4133 0
4134#endif
4135 },
4136 {"packages",
4137#ifdef FEAT_EVAL
4138 1
4139#else
4140 0
4141#endif
4142 },
4143 {"path_extra",
4144#ifdef FEAT_PATH_EXTRA
4145 1
4146#else
4147 0
4148#endif
4149 },
4150 {"perl",
4151#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4152 1
4153#else
4154 0
4155#endif
4156 },
4157 {"persistent_undo",
4158#ifdef FEAT_PERSISTENT_UNDO
4159 1
4160#else
4161 0
4162#endif
4163 },
4164 {"python_compiled",
4165#if defined(FEAT_PYTHON)
4166 1
4167#else
4168 0
4169#endif
4170 },
4171 {"python_dynamic",
4172#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4173 1
4174#else
4175 0
4176#endif
4177 },
4178 {"python",
4179#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4180 1
4181#else
4182 0
4183#endif
4184 },
4185 {"pythonx",
4186#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4187 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4188 1
4189#else
4190 0
4191#endif
4192 },
4193 {"python3_compiled",
4194#if defined(FEAT_PYTHON3)
4195 1
4196#else
4197 0
4198#endif
4199 },
4200 {"python3_dynamic",
4201#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4202 1
4203#else
4204 0
4205#endif
4206 },
4207 {"python3",
4208#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4209 1
4210#else
4211 0
4212#endif
4213 },
4214 {"popupwin",
4215#ifdef FEAT_PROP_POPUP
4216 1
4217#else
4218 0
4219#endif
4220 },
4221 {"postscript",
4222#ifdef FEAT_POSTSCRIPT
4223 1
4224#else
4225 0
4226#endif
4227 },
4228 {"printer",
4229#ifdef FEAT_PRINTER
4230 1
4231#else
4232 0
4233#endif
4234 },
4235 {"profile",
4236#ifdef FEAT_PROFILE
4237 1
4238#else
4239 0
4240#endif
4241 },
4242 {"reltime",
4243#ifdef FEAT_RELTIME
4244 1
4245#else
4246 0
4247#endif
4248 },
4249 {"quickfix",
4250#ifdef FEAT_QUICKFIX
4251 1
4252#else
4253 0
4254#endif
4255 },
4256 {"rightleft",
4257#ifdef FEAT_RIGHTLEFT
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"ruby",
4264#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"scrollbind", 1},
4271 {"showcmd",
4272#ifdef FEAT_CMDL_INFO
4273 1
4274#else
4275 0
4276#endif
4277 },
4278 {"cmdline_info",
4279#ifdef FEAT_CMDL_INFO
4280 1
4281#else
4282 0
4283#endif
4284 },
4285 {"signs",
4286#ifdef FEAT_SIGNS
4287 1
4288#else
4289 0
4290#endif
4291 },
4292 {"smartindent",
4293#ifdef FEAT_SMARTINDENT
4294 1
4295#else
4296 0
4297#endif
4298 },
4299 {"startuptime",
4300#ifdef STARTUPTIME
4301 1
4302#else
4303 0
4304#endif
4305 },
4306 {"statusline",
4307#ifdef FEAT_STL_OPT
4308 1
4309#else
4310 0
4311#endif
4312 },
4313 {"netbeans_intg",
4314#ifdef FEAT_NETBEANS_INTG
4315 1
4316#else
4317 0
4318#endif
4319 },
4320 {"sound",
4321#ifdef FEAT_SOUND
4322 1
4323#else
4324 0
4325#endif
4326 },
4327 {"spell",
4328#ifdef FEAT_SPELL
4329 1
4330#else
4331 0
4332#endif
4333 },
4334 {"syntax",
4335#ifdef FEAT_SYN_HL
4336 1
4337#else
4338 0
4339#endif
4340 },
4341 {"system",
4342#if defined(USE_SYSTEM) || !defined(UNIX)
4343 1
4344#else
4345 0
4346#endif
4347 },
4348 {"tag_binary",
4349#ifdef FEAT_TAG_BINS
4350 1
4351#else
4352 0
4353#endif
4354 },
4355 {"tcl",
4356#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4357 1
4358#else
4359 0
4360#endif
4361 },
4362 {"termguicolors",
4363#ifdef FEAT_TERMGUICOLORS
4364 1
4365#else
4366 0
4367#endif
4368 },
4369 {"terminal",
4370#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4371 1
4372#else
4373 0
4374#endif
4375 },
4376 {"terminfo",
4377#ifdef TERMINFO
4378 1
4379#else
4380 0
4381#endif
4382 },
4383 {"termresponse",
4384#ifdef FEAT_TERMRESPONSE
4385 1
4386#else
4387 0
4388#endif
4389 },
4390 {"textobjects",
4391#ifdef FEAT_TEXTOBJ
4392 1
4393#else
4394 0
4395#endif
4396 },
4397 {"textprop",
4398#ifdef FEAT_PROP_POPUP
4399 1
4400#else
4401 0
4402#endif
4403 },
4404 {"tgetent",
4405#ifdef HAVE_TGETENT
4406 1
4407#else
4408 0
4409#endif
4410 },
4411 {"timers",
4412#ifdef FEAT_TIMERS
4413 1
4414#else
4415 0
4416#endif
4417 },
4418 {"title",
4419#ifdef FEAT_TITLE
4420 1
4421#else
4422 0
4423#endif
4424 },
4425 {"toolbar",
4426#ifdef FEAT_TOOLBAR
4427 1
4428#else
4429 0
4430#endif
4431 },
4432 {"unnamedplus",
4433#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4434 1
4435#else
4436 0
4437#endif
4438 },
4439 {"user-commands", 1}, // was accidentally included in 5.4
4440 {"user_commands", 1},
4441 {"vartabs",
4442#ifdef FEAT_VARTABS
4443 1
4444#else
4445 0
4446#endif
4447 },
4448 {"vertsplit", 1},
4449 {"viminfo",
4450#ifdef FEAT_VIMINFO
4451 1
4452#else
4453 0
4454#endif
4455 },
4456 {"vimscript-1", 1},
4457 {"vimscript-2", 1},
4458 {"vimscript-3", 1},
4459 {"vimscript-4", 1},
4460 {"virtualedit", 1},
4461 {"visual", 1},
4462 {"visualextra", 1},
4463 {"vreplace", 1},
4464 {"vtp",
4465#ifdef FEAT_VTP
4466 1
4467#else
4468 0
4469#endif
4470 },
4471 {"wildignore",
4472#ifdef FEAT_WILDIGN
4473 1
4474#else
4475 0
4476#endif
4477 },
4478 {"wildmenu",
4479#ifdef FEAT_WILDMENU
4480 1
4481#else
4482 0
4483#endif
4484 },
4485 {"windows", 1},
4486 {"winaltkeys",
4487#ifdef FEAT_WAK
4488 1
4489#else
4490 0
4491#endif
4492 },
4493 {"writebackup",
4494#ifdef FEAT_WRITEBACKUP
4495 1
4496#else
4497 0
4498#endif
4499 },
4500 {"xim",
4501#ifdef FEAT_XIM
4502 1
4503#else
4504 0
4505#endif
4506 },
4507 {"xfontset",
4508#ifdef FEAT_XFONTSET
4509 1
4510#else
4511 0
4512#endif
4513 },
4514 {"xpm",
4515#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4516 1
4517#else
4518 0
4519#endif
4520 },
4521 {"xpm_w32", // for backward compatibility
4522#ifdef FEAT_XPM_W32
4523 1
4524#else
4525 0
4526#endif
4527 },
4528 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 1
4531#else
4532 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004533#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004534 },
4535 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 1
4538#else
4539 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004541 },
4542 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 1
4545#else
4546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004548 },
4549 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 1
4552#else
4553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 },
4556 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 1
4559#else
4560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004562 },
4563 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564 };
4565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004566 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004567 for (i = 0; has_list[i].name != NULL; ++i)
4568 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 x = TRUE;
4571 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572 break;
4573 }
4574
Bram Moolenaar79296512020-03-22 16:17:14 +01004575 // features also in has_list[] but sometimes enabled at runtime
4576 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004578 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004579 {
4580 // intentionally empty
4581 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004582#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583 else if (STRICMP(name, "balloon_multiline") == 0)
4584 n = multiline_balloon_available();
4585#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004586#ifdef VIMDLL
4587 else if (STRICMP(name, "filterpipe") == 0)
4588 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589#endif
4590#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4591 else if (STRICMP(name, "iconv") == 0)
4592 n = iconv_enabled(FALSE);
4593#endif
4594#ifdef DYNAMIC_LUA
4595 else if (STRICMP(name, "lua") == 0)
4596 n = lua_enabled(FALSE);
4597#endif
4598#ifdef DYNAMIC_MZSCHEME
4599 else if (STRICMP(name, "mzscheme") == 0)
4600 n = mzscheme_enabled(FALSE);
4601#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004602#ifdef DYNAMIC_PERL
4603 else if (STRICMP(name, "perl") == 0)
4604 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004605#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#ifdef DYNAMIC_PYTHON
4607 else if (STRICMP(name, "python") == 0)
4608 n = python_enabled(FALSE);
4609#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004610#ifdef DYNAMIC_PYTHON3
4611 else if (STRICMP(name, "python3") == 0)
4612 n = python3_enabled(FALSE);
4613#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004614#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4615 else if (STRICMP(name, "pythonx") == 0)
4616 {
4617# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4618 if (p_pyx == 0)
4619 n = python3_enabled(FALSE) || python_enabled(FALSE);
4620 else if (p_pyx == 3)
4621 n = python3_enabled(FALSE);
4622 else if (p_pyx == 2)
4623 n = python_enabled(FALSE);
4624# elif defined(DYNAMIC_PYTHON)
4625 n = python_enabled(FALSE);
4626# elif defined(DYNAMIC_PYTHON3)
4627 n = python3_enabled(FALSE);
4628# endif
4629 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004631#ifdef DYNAMIC_RUBY
4632 else if (STRICMP(name, "ruby") == 0)
4633 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004635#ifdef DYNAMIC_TCL
4636 else if (STRICMP(name, "tcl") == 0)
4637 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004638#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004639#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004640 else if (STRICMP(name, "terminal") == 0)
4641 n = terminal_enabled();
4642#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004643 }
4644
Bram Moolenaar79296512020-03-22 16:17:14 +01004645 // features not in has_list[]
4646 if (x == FALSE)
4647 {
4648 if (STRNICMP(name, "patch", 5) == 0)
4649 {
4650 x = TRUE;
4651 if (name[5] == '-'
4652 && STRLEN(name) >= 11
4653 && vim_isdigit(name[6])
4654 && vim_isdigit(name[8])
4655 && vim_isdigit(name[10]))
4656 {
4657 int major = atoi((char *)name + 6);
4658 int minor = atoi((char *)name + 8);
4659
4660 // Expect "patch-9.9.01234".
4661 n = (major < VIM_VERSION_MAJOR
4662 || (major == VIM_VERSION_MAJOR
4663 && (minor < VIM_VERSION_MINOR
4664 || (minor == VIM_VERSION_MINOR
4665 && has_patch(atoi((char *)name + 10))))));
4666 }
4667 else
4668 n = has_patch(atoi((char *)name + 5));
4669 }
4670 else if (STRICMP(name, "vim_starting") == 0)
4671 {
4672 x = TRUE;
4673 n = (starting != 0);
4674 }
4675 else if (STRICMP(name, "ttyin") == 0)
4676 {
4677 x = TRUE;
4678 n = mch_input_isatty();
4679 }
4680 else if (STRICMP(name, "ttyout") == 0)
4681 {
4682 x = TRUE;
4683 n = stdout_isatty;
4684 }
4685 else if (STRICMP(name, "multi_byte_encoding") == 0)
4686 {
4687 x = TRUE;
4688 n = has_mbyte;
4689 }
4690 else if (STRICMP(name, "gui_running") == 0)
4691 {
4692 x = TRUE;
4693#ifdef FEAT_GUI
4694 n = (gui.in_use || gui.starting);
4695#endif
4696 }
4697 else if (STRICMP(name, "browse") == 0)
4698 {
4699 x = TRUE;
4700#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4701 n = gui.in_use; // gui_mch_browse() works when GUI is running
4702#endif
4703 }
4704 else if (STRICMP(name, "syntax_items") == 0)
4705 {
4706 x = TRUE;
4707#ifdef FEAT_SYN_HL
4708 n = syntax_present(curwin);
4709#endif
4710 }
4711 else if (STRICMP(name, "vcon") == 0)
4712 {
4713 x = TRUE;
4714#ifdef FEAT_VTP
4715 n = is_term_win32() && has_vtp_working();
4716#endif
4717 }
4718 else if (STRICMP(name, "netbeans_enabled") == 0)
4719 {
4720 x = TRUE;
4721#ifdef FEAT_NETBEANS_INTG
4722 n = netbeans_active();
4723#endif
4724 }
4725 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4726 {
4727 x = TRUE;
4728#ifdef FEAT_MOUSE_GPM
4729 n = gpm_enabled();
4730#endif
4731 }
4732 else if (STRICMP(name, "conpty") == 0)
4733 {
4734 x = TRUE;
4735#if defined(FEAT_TERMINAL) && defined(MSWIN)
4736 n = use_conpty();
4737#endif
4738 }
4739 else if (STRICMP(name, "clipboard_working") == 0)
4740 {
4741 x = TRUE;
4742#ifdef FEAT_CLIPBOARD
4743 n = clip_star.available;
4744#endif
4745 }
4746 }
4747
4748 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4749 // return whether feature could ever be enabled
4750 rettv->vval.v_number = x;
4751 else
4752 // return whether feature is enabled
4753 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004754}
4755
4756/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004757 * "haslocaldir()" function
4758 */
4759 static void
4760f_haslocaldir(typval_T *argvars, typval_T *rettv)
4761{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004762 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004763 win_T *wp = NULL;
4764
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004765 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4766
4767 // Check for window-local and tab-local directories
4768 if (wp != NULL && wp->w_localdir != NULL)
4769 rettv->vval.v_number = 1;
4770 else if (tp != NULL && tp->tp_localdir != NULL)
4771 rettv->vval.v_number = 2;
4772 else
4773 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004774}
4775
4776/*
4777 * "hasmapto()" function
4778 */
4779 static void
4780f_hasmapto(typval_T *argvars, typval_T *rettv)
4781{
4782 char_u *name;
4783 char_u *mode;
4784 char_u buf[NUMBUFLEN];
4785 int abbr = FALSE;
4786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004788 if (argvars[1].v_type == VAR_UNKNOWN)
4789 mode = (char_u *)"nvo";
4790 else
4791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004793 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004794 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795 }
4796
4797 if (map_to_exists(name, mode, abbr))
4798 rettv->vval.v_number = TRUE;
4799 else
4800 rettv->vval.v_number = FALSE;
4801}
4802
4803/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004804 * "highlightID(name)" function
4805 */
4806 static void
4807f_hlID(typval_T *argvars, typval_T *rettv)
4808{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004809 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004810}
4811
4812/*
4813 * "highlight_exists()" function
4814 */
4815 static void
4816f_hlexists(typval_T *argvars, typval_T *rettv)
4817{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004818 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819}
4820
4821/*
4822 * "hostname()" function
4823 */
4824 static void
4825f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4826{
4827 char_u hostname[256];
4828
4829 mch_get_host_name(hostname, 256);
4830 rettv->v_type = VAR_STRING;
4831 rettv->vval.v_string = vim_strsave(hostname);
4832}
4833
4834/*
4835 * iconv() function
4836 */
4837 static void
4838f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4839{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 char_u buf1[NUMBUFLEN];
4841 char_u buf2[NUMBUFLEN];
4842 char_u *from, *to, *str;
4843 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004844
4845 rettv->v_type = VAR_STRING;
4846 rettv->vval.v_string = NULL;
4847
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004848 str = tv_get_string(&argvars[0]);
4849 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4850 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 vimconv.vc_type = CONV_NONE;
4852 convert_setup(&vimconv, from, to);
4853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004854 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004855 if (vimconv.vc_type == CONV_NONE)
4856 rettv->vval.v_string = vim_strsave(str);
4857 else
4858 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4859
4860 convert_setup(&vimconv, NULL, NULL);
4861 vim_free(from);
4862 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863}
4864
4865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004866 * "index()" function
4867 */
4868 static void
4869f_index(typval_T *argvars, typval_T *rettv)
4870{
4871 list_T *l;
4872 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004873 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 long idx = 0;
4875 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004876 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877
4878 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004879 if (argvars[0].v_type == VAR_BLOB)
4880 {
4881 typval_T tv;
4882 int start = 0;
4883
4884 if (argvars[2].v_type != VAR_UNKNOWN)
4885 {
4886 start = tv_get_number_chk(&argvars[2], &error);
4887 if (error)
4888 return;
4889 }
4890 b = argvars[0].vval.v_blob;
4891 if (b == NULL)
4892 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004893 if (start < 0)
4894 {
4895 start = blob_len(b) + start;
4896 if (start < 0)
4897 start = 0;
4898 }
4899
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004900 for (idx = start; idx < blob_len(b); ++idx)
4901 {
4902 tv.v_type = VAR_NUMBER;
4903 tv.vval.v_number = blob_get(b, idx);
4904 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4905 {
4906 rettv->vval.v_number = idx;
4907 return;
4908 }
4909 }
4910 return;
4911 }
4912 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004914 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915 return;
4916 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004917
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 l = argvars[0].vval.v_list;
4919 if (l != NULL)
4920 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004921 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922 item = l->lv_first;
4923 if (argvars[2].v_type != VAR_UNKNOWN)
4924 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 // Start at specified item. Use the cached index that list_find()
4926 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004927 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004928 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004930 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 if (error)
4932 item = NULL;
4933 }
4934
4935 for ( ; item != NULL; item = item->li_next, ++idx)
4936 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4937 {
4938 rettv->vval.v_number = idx;
4939 break;
4940 }
4941 }
4942}
4943
4944static int inputsecret_flag = 0;
4945
4946/*
4947 * "input()" function
4948 * Also handles inputsecret() when inputsecret is set.
4949 */
4950 static void
4951f_input(typval_T *argvars, typval_T *rettv)
4952{
4953 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4954}
4955
4956/*
4957 * "inputdialog()" function
4958 */
4959 static void
4960f_inputdialog(typval_T *argvars, typval_T *rettv)
4961{
4962#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004963 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4965 {
4966 char_u *message;
4967 char_u buf[NUMBUFLEN];
4968 char_u *defstr = (char_u *)"";
4969
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004970 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004971 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004972 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004973 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4974 else
4975 IObuff[0] = NUL;
4976 if (message != NULL && defstr != NULL
4977 && do_dialog(VIM_QUESTION, NULL, message,
4978 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4979 rettv->vval.v_string = vim_strsave(IObuff);
4980 else
4981 {
4982 if (message != NULL && defstr != NULL
4983 && argvars[1].v_type != VAR_UNKNOWN
4984 && argvars[2].v_type != VAR_UNKNOWN)
4985 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004986 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004987 else
4988 rettv->vval.v_string = NULL;
4989 }
4990 rettv->v_type = VAR_STRING;
4991 }
4992 else
4993#endif
4994 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4995}
4996
4997/*
4998 * "inputlist()" function
4999 */
5000 static void
5001f_inputlist(typval_T *argvars, typval_T *rettv)
5002{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005003 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005004 listitem_T *li;
5005 int selected;
5006 int mouse_used;
5007
5008#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 // While starting up, there is no place to enter text. When running tests
5010 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005011 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005012 return;
5013#endif
5014 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005016 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 return;
5018 }
5019
5020 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 msg_row = Rows - 1; // for when 'cmdheight' > 1
5022 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 msg_scroll = TRUE;
5024 msg_clr_eos();
5025
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005026 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005027 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005028 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005030 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 msg_putchar('\n');
5032 }
5033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 selected = prompt_for_number(&mouse_used);
5036 if (mouse_used)
5037 selected -= lines_left;
5038
5039 rettv->vval.v_number = selected;
5040}
5041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5043
5044/*
5045 * "inputrestore()" function
5046 */
5047 static void
5048f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5049{
5050 if (ga_userinput.ga_len > 0)
5051 {
5052 --ga_userinput.ga_len;
5053 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5054 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005056 }
5057 else if (p_verbose > 1)
5058 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005059 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005061 }
5062}
5063
5064/*
5065 * "inputsave()" function
5066 */
5067 static void
5068f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5069{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005071 if (ga_grow(&ga_userinput, 1) == OK)
5072 {
5073 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5074 + ga_userinput.ga_len);
5075 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005076 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 }
5078 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005079 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005080}
5081
5082/*
5083 * "inputsecret()" function
5084 */
5085 static void
5086f_inputsecret(typval_T *argvars, typval_T *rettv)
5087{
5088 ++cmdline_star;
5089 ++inputsecret_flag;
5090 f_input(argvars, rettv);
5091 --cmdline_star;
5092 --inputsecret_flag;
5093}
5094
5095/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005096 * "interrupt()" function
5097 */
5098 static void
5099f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5100{
5101 got_int = TRUE;
5102}
5103
5104/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 * "invert(expr)" function
5106 */
5107 static void
5108f_invert(typval_T *argvars, typval_T *rettv)
5109{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005110 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111}
5112
5113/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 * "islocked()" function
5115 */
5116 static void
5117f_islocked(typval_T *argvars, typval_T *rettv)
5118{
5119 lval_T lv;
5120 char_u *end;
5121 dictitem_T *di;
5122
5123 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005124 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005125 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 if (end != NULL && lv.ll_name != NULL)
5127 {
5128 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005129 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005130 else
5131 {
5132 if (lv.ll_tv == NULL)
5133 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005134 di = find_var(lv.ll_name, NULL, TRUE);
5135 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // Consider a variable locked when:
5138 // 1. the variable itself is locked
5139 // 2. the value of the variable is locked.
5140 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005141 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5142 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 }
5144 }
5145 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005146 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005147 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005148 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5152 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005153 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5155 }
5156 }
5157
5158 clear_lval(&lv);
5159}
5160
5161#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5162/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005163 * "isinf()" function
5164 */
5165 static void
5166f_isinf(typval_T *argvars, typval_T *rettv)
5167{
5168 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5169 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5170}
5171
5172/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 * "isnan()" function
5174 */
5175 static void
5176f_isnan(typval_T *argvars, typval_T *rettv)
5177{
5178 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5179 && isnan(argvars[0].vval.v_float);
5180}
5181#endif
5182
5183/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005184 * "last_buffer_nr()" function.
5185 */
5186 static void
5187f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5188{
5189 int n = 0;
5190 buf_T *buf;
5191
Bram Moolenaar29323592016-07-24 22:04:11 +02005192 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005193 if (n < buf->b_fnum)
5194 n = buf->b_fnum;
5195
5196 rettv->vval.v_number = n;
5197}
5198
5199/*
5200 * "len()" function
5201 */
5202 static void
5203f_len(typval_T *argvars, typval_T *rettv)
5204{
5205 switch (argvars[0].v_type)
5206 {
5207 case VAR_STRING:
5208 case VAR_NUMBER:
5209 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005210 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005212 case VAR_BLOB:
5213 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5214 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 case VAR_LIST:
5216 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5217 break;
5218 case VAR_DICT:
5219 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5220 break;
5221 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005222 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005224 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225 case VAR_SPECIAL:
5226 case VAR_FLOAT:
5227 case VAR_FUNC:
5228 case VAR_PARTIAL:
5229 case VAR_JOB:
5230 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005231 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005232 break;
5233 }
5234}
5235
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005237libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005238{
5239#ifdef FEAT_LIBCALL
5240 char_u *string_in;
5241 char_u **string_result;
5242 int nr_result;
5243#endif
5244
5245 rettv->v_type = type;
5246 if (type != VAR_NUMBER)
5247 rettv->vval.v_string = NULL;
5248
5249 if (check_restricted() || check_secure())
5250 return;
5251
5252#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005253 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5255 {
5256 string_in = NULL;
5257 if (argvars[2].v_type == VAR_STRING)
5258 string_in = argvars[2].vval.v_string;
5259 if (type == VAR_NUMBER)
5260 string_result = NULL;
5261 else
5262 string_result = &rettv->vval.v_string;
5263 if (mch_libcall(argvars[0].vval.v_string,
5264 argvars[1].vval.v_string,
5265 string_in,
5266 argvars[2].vval.v_number,
5267 string_result,
5268 &nr_result) == OK
5269 && type == VAR_NUMBER)
5270 rettv->vval.v_number = nr_result;
5271 }
5272#endif
5273}
5274
5275/*
5276 * "libcall()" function
5277 */
5278 static void
5279f_libcall(typval_T *argvars, typval_T *rettv)
5280{
5281 libcall_common(argvars, rettv, VAR_STRING);
5282}
5283
5284/*
5285 * "libcallnr()" function
5286 */
5287 static void
5288f_libcallnr(typval_T *argvars, typval_T *rettv)
5289{
5290 libcall_common(argvars, rettv, VAR_NUMBER);
5291}
5292
5293/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005294 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 */
5296 static void
5297f_line(typval_T *argvars, typval_T *rettv)
5298{
5299 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005300 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005302 int id;
5303 tabpage_T *tp;
5304 win_T *wp;
5305 win_T *save_curwin;
5306 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005308 if (argvars[1].v_type != VAR_UNKNOWN)
5309 {
5310 // use window specified in the second argument
5311 id = (int)tv_get_number(&argvars[1]);
5312 wp = win_id2wp_tp(id, &tp);
5313 if (wp != NULL && tp != NULL)
5314 {
5315 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5316 == OK)
5317 {
5318 check_cursor();
5319 fp = var2fpos(&argvars[0], TRUE, &fnum);
5320 }
5321 restore_win_noblock(save_curwin, save_curtab, TRUE);
5322 }
5323 }
5324 else
5325 // use current window
5326 fp = var2fpos(&argvars[0], TRUE, &fnum);
5327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328 if (fp != NULL)
5329 lnum = fp->lnum;
5330 rettv->vval.v_number = lnum;
5331}
5332
5333/*
5334 * "line2byte(lnum)" function
5335 */
5336 static void
5337f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5338{
5339#ifndef FEAT_BYTEOFF
5340 rettv->vval.v_number = -1;
5341#else
5342 linenr_T lnum;
5343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005344 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5346 rettv->vval.v_number = -1;
5347 else
5348 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5349 if (rettv->vval.v_number >= 0)
5350 ++rettv->vval.v_number;
5351#endif
5352}
5353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354#ifdef FEAT_FLOAT
5355/*
5356 * "log()" function
5357 */
5358 static void
5359f_log(typval_T *argvars, typval_T *rettv)
5360{
5361 float_T f = 0.0;
5362
5363 rettv->v_type = VAR_FLOAT;
5364 if (get_float_arg(argvars, &f) == OK)
5365 rettv->vval.v_float = log(f);
5366 else
5367 rettv->vval.v_float = 0.0;
5368}
5369
5370/*
5371 * "log10()" function
5372 */
5373 static void
5374f_log10(typval_T *argvars, typval_T *rettv)
5375{
5376 float_T f = 0.0;
5377
5378 rettv->v_type = VAR_FLOAT;
5379 if (get_float_arg(argvars, &f) == OK)
5380 rettv->vval.v_float = log10(f);
5381 else
5382 rettv->vval.v_float = 0.0;
5383}
5384#endif
5385
5386#ifdef FEAT_LUA
5387/*
5388 * "luaeval()" function
5389 */
5390 static void
5391f_luaeval(typval_T *argvars, typval_T *rettv)
5392{
5393 char_u *str;
5394 char_u buf[NUMBUFLEN];
5395
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005396 if (check_restricted() || check_secure())
5397 return;
5398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005399 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005400 do_luaeval(str, argvars + 1, rettv);
5401}
5402#endif
5403
5404/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005405 * "maparg()" function
5406 */
5407 static void
5408f_maparg(typval_T *argvars, typval_T *rettv)
5409{
5410 get_maparg(argvars, rettv, TRUE);
5411}
5412
5413/*
5414 * "mapcheck()" function
5415 */
5416 static void
5417f_mapcheck(typval_T *argvars, typval_T *rettv)
5418{
5419 get_maparg(argvars, rettv, FALSE);
5420}
5421
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005422typedef enum
5423{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 MATCH_END, // matchend()
5425 MATCH_MATCH, // match()
5426 MATCH_STR, // matchstr()
5427 MATCH_LIST, // matchlist()
5428 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005429} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430
5431 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005432find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433{
5434 char_u *str = NULL;
5435 long len = 0;
5436 char_u *expr = NULL;
5437 char_u *pat;
5438 regmatch_T regmatch;
5439 char_u patbuf[NUMBUFLEN];
5440 char_u strbuf[NUMBUFLEN];
5441 char_u *save_cpo;
5442 long start = 0;
5443 long nth = 1;
5444 colnr_T startcol = 0;
5445 int match = 0;
5446 list_T *l = NULL;
5447 listitem_T *li = NULL;
5448 long idx = 0;
5449 char_u *tofree = NULL;
5450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452 save_cpo = p_cpo;
5453 p_cpo = (char_u *)"";
5454
5455 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005456 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 // type MATCH_LIST: return empty list when there are no matches.
5459 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 if (rettv_list_alloc(rettv) == FAIL)
5461 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005462 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005463 && (list_append_string(rettv->vval.v_list,
5464 (char_u *)"", 0) == FAIL
5465 || list_append_number(rettv->vval.v_list,
5466 (varnumber_T)-1) == FAIL
5467 || list_append_number(rettv->vval.v_list,
5468 (varnumber_T)-1) == FAIL
5469 || list_append_number(rettv->vval.v_list,
5470 (varnumber_T)-1) == FAIL))
5471 {
5472 list_free(rettv->vval.v_list);
5473 rettv->vval.v_list = NULL;
5474 goto theend;
5475 }
5476 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005477 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478 {
5479 rettv->v_type = VAR_STRING;
5480 rettv->vval.v_string = NULL;
5481 }
5482
5483 if (argvars[0].v_type == VAR_LIST)
5484 {
5485 if ((l = argvars[0].vval.v_list) == NULL)
5486 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005487 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 li = l->lv_first;
5489 }
5490 else
5491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 len = (long)STRLEN(str);
5494 }
5495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005496 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 if (pat == NULL)
5498 goto theend;
5499
5500 if (argvars[2].v_type != VAR_UNKNOWN)
5501 {
5502 int error = FALSE;
5503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005504 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005505 if (error)
5506 goto theend;
5507 if (l != NULL)
5508 {
5509 li = list_find(l, start);
5510 if (li == NULL)
5511 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005512 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005513 }
5514 else
5515 {
5516 if (start < 0)
5517 start = 0;
5518 if (start > len)
5519 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // When "count" argument is there ignore matches before "start",
5521 // otherwise skip part of the string. Differs when pattern is "^"
5522 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523 if (argvars[3].v_type != VAR_UNKNOWN)
5524 startcol = start;
5525 else
5526 {
5527 str += start;
5528 len -= start;
5529 }
5530 }
5531
5532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005533 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534 if (error)
5535 goto theend;
5536 }
5537
5538 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5539 if (regmatch.regprog != NULL)
5540 {
5541 regmatch.rm_ic = p_ic;
5542
5543 for (;;)
5544 {
5545 if (l != NULL)
5546 {
5547 if (li == NULL)
5548 {
5549 match = FALSE;
5550 break;
5551 }
5552 vim_free(tofree);
5553 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5554 if (str == NULL)
5555 break;
5556 }
5557
5558 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5559
5560 if (match && --nth <= 0)
5561 break;
5562 if (l == NULL && !match)
5563 break;
5564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005565 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005566 if (l != NULL)
5567 {
5568 li = li->li_next;
5569 ++idx;
5570 }
5571 else
5572 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573 startcol = (colnr_T)(regmatch.startp[0]
5574 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005575 if (startcol > (colnr_T)len
5576 || str + startcol <= regmatch.startp[0])
5577 {
5578 match = FALSE;
5579 break;
5580 }
5581 }
5582 }
5583
5584 if (match)
5585 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005586 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 {
5588 listitem_T *li1 = rettv->vval.v_list->lv_first;
5589 listitem_T *li2 = li1->li_next;
5590 listitem_T *li3 = li2->li_next;
5591 listitem_T *li4 = li3->li_next;
5592
5593 vim_free(li1->li_tv.vval.v_string);
5594 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005595 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 li3->li_tv.vval.v_number =
5597 (varnumber_T)(regmatch.startp[0] - expr);
5598 li4->li_tv.vval.v_number =
5599 (varnumber_T)(regmatch.endp[0] - expr);
5600 if (l != NULL)
5601 li2->li_tv.vval.v_number = (varnumber_T)idx;
5602 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005603 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 {
5605 int i;
5606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005607 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 for (i = 0; i < NSUBEXP; ++i)
5609 {
5610 if (regmatch.endp[i] == NULL)
5611 {
5612 if (list_append_string(rettv->vval.v_list,
5613 (char_u *)"", 0) == FAIL)
5614 break;
5615 }
5616 else if (list_append_string(rettv->vval.v_list,
5617 regmatch.startp[i],
5618 (int)(regmatch.endp[i] - regmatch.startp[i]))
5619 == FAIL)
5620 break;
5621 }
5622 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005623 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005625 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626 if (l != NULL)
5627 copy_tv(&li->li_tv, rettv);
5628 else
5629 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005630 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 }
5632 else if (l != NULL)
5633 rettv->vval.v_number = idx;
5634 else
5635 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005636 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 rettv->vval.v_number =
5638 (varnumber_T)(regmatch.startp[0] - str);
5639 else
5640 rettv->vval.v_number =
5641 (varnumber_T)(regmatch.endp[0] - str);
5642 rettv->vval.v_number += (varnumber_T)(str - expr);
5643 }
5644 }
5645 vim_regfree(regmatch.regprog);
5646 }
5647
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005648theend:
5649 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005650 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651 listitem_remove(rettv->vval.v_list,
5652 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 vim_free(tofree);
5654 p_cpo = save_cpo;
5655}
5656
5657/*
5658 * "match()" function
5659 */
5660 static void
5661f_match(typval_T *argvars, typval_T *rettv)
5662{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005663 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664}
5665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666/*
5667 * "matchend()" function
5668 */
5669 static void
5670f_matchend(typval_T *argvars, typval_T *rettv)
5671{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005672 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673}
5674
5675/*
5676 * "matchlist()" function
5677 */
5678 static void
5679f_matchlist(typval_T *argvars, typval_T *rettv)
5680{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005681 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682}
5683
5684/*
5685 * "matchstr()" function
5686 */
5687 static void
5688f_matchstr(typval_T *argvars, typval_T *rettv)
5689{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005690 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005691}
5692
5693/*
5694 * "matchstrpos()" function
5695 */
5696 static void
5697f_matchstrpos(typval_T *argvars, typval_T *rettv)
5698{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005699 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700}
5701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 static void
5703max_min(typval_T *argvars, typval_T *rettv, int domax)
5704{
5705 varnumber_T n = 0;
5706 varnumber_T i;
5707 int error = FALSE;
5708
5709 if (argvars[0].v_type == VAR_LIST)
5710 {
5711 list_T *l;
5712 listitem_T *li;
5713
5714 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005715 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005717 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005719 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5720 n = l->lv_u.nonmat.lv_start;
5721 else
5722 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5723 * l->lv_u.nonmat.lv_stride;
5724 }
5725 else
5726 {
5727 li = l->lv_first;
5728 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005729 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005730 n = tv_get_number_chk(&li->li_tv, &error);
5731 for (;;)
5732 {
5733 li = li->li_next;
5734 if (li == NULL)
5735 break;
5736 i = tv_get_number_chk(&li->li_tv, &error);
5737 if (domax ? i > n : i < n)
5738 n = i;
5739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740 }
5741 }
5742 }
5743 }
5744 else if (argvars[0].v_type == VAR_DICT)
5745 {
5746 dict_T *d;
5747 int first = TRUE;
5748 hashitem_T *hi;
5749 int todo;
5750
5751 d = argvars[0].vval.v_dict;
5752 if (d != NULL)
5753 {
5754 todo = (int)d->dv_hashtab.ht_used;
5755 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5756 {
5757 if (!HASHITEM_EMPTY(hi))
5758 {
5759 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005761 if (first)
5762 {
5763 n = i;
5764 first = FALSE;
5765 }
5766 else if (domax ? i > n : i < n)
5767 n = i;
5768 }
5769 }
5770 }
5771 }
5772 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005773 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005774 rettv->vval.v_number = error ? 0 : n;
5775}
5776
5777/*
5778 * "max()" function
5779 */
5780 static void
5781f_max(typval_T *argvars, typval_T *rettv)
5782{
5783 max_min(argvars, rettv, TRUE);
5784}
5785
5786/*
5787 * "min()" function
5788 */
5789 static void
5790f_min(typval_T *argvars, typval_T *rettv)
5791{
5792 max_min(argvars, rettv, FALSE);
5793}
5794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795#if defined(FEAT_MZSCHEME) || defined(PROTO)
5796/*
5797 * "mzeval()" function
5798 */
5799 static void
5800f_mzeval(typval_T *argvars, typval_T *rettv)
5801{
5802 char_u *str;
5803 char_u buf[NUMBUFLEN];
5804
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005805 if (check_restricted() || check_secure())
5806 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005807 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 do_mzeval(str, rettv);
5809}
5810
5811 void
5812mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5813{
5814 typval_T argvars[3];
5815
5816 argvars[0].v_type = VAR_STRING;
5817 argvars[0].vval.v_string = name;
5818 copy_tv(args, &argvars[1]);
5819 argvars[2].v_type = VAR_UNKNOWN;
5820 f_call(argvars, rettv);
5821 clear_tv(&argvars[1]);
5822}
5823#endif
5824
5825/*
5826 * "nextnonblank()" function
5827 */
5828 static void
5829f_nextnonblank(typval_T *argvars, typval_T *rettv)
5830{
5831 linenr_T lnum;
5832
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005833 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 {
5835 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5836 {
5837 lnum = 0;
5838 break;
5839 }
5840 if (*skipwhite(ml_get(lnum)) != NUL)
5841 break;
5842 }
5843 rettv->vval.v_number = lnum;
5844}
5845
5846/*
5847 * "nr2char()" function
5848 */
5849 static void
5850f_nr2char(typval_T *argvars, typval_T *rettv)
5851{
5852 char_u buf[NUMBUFLEN];
5853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 if (has_mbyte)
5855 {
5856 int utf8 = 0;
5857
5858 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005861 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 }
5865 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005867 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 buf[1] = NUL;
5869 }
5870 rettv->v_type = VAR_STRING;
5871 rettv->vval.v_string = vim_strsave(buf);
5872}
5873
5874/*
5875 * "or(expr, expr)" function
5876 */
5877 static void
5878f_or(typval_T *argvars, typval_T *rettv)
5879{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005880 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5881 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882}
5883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884#ifdef FEAT_PERL
5885/*
5886 * "perleval()" function
5887 */
5888 static void
5889f_perleval(typval_T *argvars, typval_T *rettv)
5890{
5891 char_u *str;
5892 char_u buf[NUMBUFLEN];
5893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005894 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895 do_perleval(str, rettv);
5896}
5897#endif
5898
5899#ifdef FEAT_FLOAT
5900/*
5901 * "pow()" function
5902 */
5903 static void
5904f_pow(typval_T *argvars, typval_T *rettv)
5905{
5906 float_T fx = 0.0, fy = 0.0;
5907
5908 rettv->v_type = VAR_FLOAT;
5909 if (get_float_arg(argvars, &fx) == OK
5910 && get_float_arg(&argvars[1], &fy) == OK)
5911 rettv->vval.v_float = pow(fx, fy);
5912 else
5913 rettv->vval.v_float = 0.0;
5914}
5915#endif
5916
5917/*
5918 * "prevnonblank()" function
5919 */
5920 static void
5921f_prevnonblank(typval_T *argvars, typval_T *rettv)
5922{
5923 linenr_T lnum;
5924
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005925 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005926 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5927 lnum = 0;
5928 else
5929 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5930 --lnum;
5931 rettv->vval.v_number = lnum;
5932}
5933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934// This dummy va_list is here because:
5935// - passing a NULL pointer doesn't work when va_list isn't a pointer
5936// - locally in the function results in a "used before set" warning
5937// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005938static va_list ap;
5939
5940/*
5941 * "printf()" function
5942 */
5943 static void
5944f_printf(typval_T *argvars, typval_T *rettv)
5945{
5946 char_u buf[NUMBUFLEN];
5947 int len;
5948 char_u *s;
5949 int saved_did_emsg = did_emsg;
5950 char *fmt;
5951
5952 rettv->v_type = VAR_STRING;
5953 rettv->vval.v_string = NULL;
5954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005955 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005957 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005958 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 if (!did_emsg)
5960 {
5961 s = alloc(len + 1);
5962 if (s != NULL)
5963 {
5964 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005965 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5966 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 }
5968 }
5969 did_emsg |= saved_did_emsg;
5970}
5971
5972/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005973 * "pum_getpos()" function
5974 */
5975 static void
5976f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5977{
5978 if (rettv_dict_alloc(rettv) != OK)
5979 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005980 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005981}
5982
5983/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 * "pumvisible()" function
5985 */
5986 static void
5987f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5988{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 if (pum_visible())
5990 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991}
5992
5993#ifdef FEAT_PYTHON3
5994/*
5995 * "py3eval()" function
5996 */
5997 static void
5998f_py3eval(typval_T *argvars, typval_T *rettv)
5999{
6000 char_u *str;
6001 char_u buf[NUMBUFLEN];
6002
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006003 if (check_restricted() || check_secure())
6004 return;
6005
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006006 if (p_pyx == 0)
6007 p_pyx = 3;
6008
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006009 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 do_py3eval(str, rettv);
6011}
6012#endif
6013
6014#ifdef FEAT_PYTHON
6015/*
6016 * "pyeval()" function
6017 */
6018 static void
6019f_pyeval(typval_T *argvars, typval_T *rettv)
6020{
6021 char_u *str;
6022 char_u buf[NUMBUFLEN];
6023
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006024 if (check_restricted() || check_secure())
6025 return;
6026
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006027 if (p_pyx == 0)
6028 p_pyx = 2;
6029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006030 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 do_pyeval(str, rettv);
6032}
6033#endif
6034
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006035#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6036/*
6037 * "pyxeval()" function
6038 */
6039 static void
6040f_pyxeval(typval_T *argvars, typval_T *rettv)
6041{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006042 if (check_restricted() || check_secure())
6043 return;
6044
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006045# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6046 init_pyxversion();
6047 if (p_pyx == 2)
6048 f_pyeval(argvars, rettv);
6049 else
6050 f_py3eval(argvars, rettv);
6051# elif defined(FEAT_PYTHON)
6052 f_pyeval(argvars, rettv);
6053# elif defined(FEAT_PYTHON3)
6054 f_py3eval(argvars, rettv);
6055# endif
6056}
6057#endif
6058
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006059static UINT32_T srand_seed_for_testing = 0;
6060static int srand_seed_for_testing_is_used = FALSE;
6061
6062 static void
6063f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6064{
6065 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006066 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006067 else
6068 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006069 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6070 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006071 }
6072}
6073
6074 static void
6075init_srand(UINT32_T *x)
6076{
6077#ifndef MSWIN
6078 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6079#endif
6080
6081 if (srand_seed_for_testing_is_used)
6082 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006083 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006084 return;
6085 }
6086#ifndef MSWIN
6087 if (dev_urandom_state != FAIL)
6088 {
6089 int fd = open("/dev/urandom", O_RDONLY);
6090 struct {
6091 union {
6092 UINT32_T number;
6093 char bytes[sizeof(UINT32_T)];
6094 } contents;
6095 } buf;
6096
6097 // Attempt reading /dev/urandom.
6098 if (fd == -1)
6099 dev_urandom_state = FAIL;
6100 else
6101 {
6102 buf.contents.number = 0;
6103 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6104 != sizeof(UINT32_T))
6105 dev_urandom_state = FAIL;
6106 else
6107 {
6108 dev_urandom_state = OK;
6109 *x = buf.contents.number;
6110 }
6111 close(fd);
6112 }
6113 }
6114 if (dev_urandom_state != OK)
6115 // Reading /dev/urandom doesn't work, fall back to time().
6116#endif
6117 *x = vim_time();
6118}
6119
6120#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6121#define SPLITMIX32(x, z) ( \
6122 z = (x += 0x9e3779b9), \
6123 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6124 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6125 z ^ (z >> 16) \
6126 )
6127#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6128 result = ROTL(y * 5, 7) * 9; \
6129 t = y << 9; \
6130 z ^= x; \
6131 w ^= y; \
6132 y ^= z, x ^= w; \
6133 z ^= t; \
6134 w = ROTL(w, 11);
6135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006136/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006137 * "rand()" function
6138 */
6139 static void
6140f_rand(typval_T *argvars, typval_T *rettv)
6141{
6142 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006143 static UINT32_T gx, gy, gz, gw;
6144 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006145 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006146 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006147
6148 if (argvars[0].v_type == VAR_UNKNOWN)
6149 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006150 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006151 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006152 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006153 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006154 init_srand(&x);
6155
6156 gx = SPLITMIX32(x, z);
6157 gy = SPLITMIX32(x, z);
6158 gz = SPLITMIX32(x, z);
6159 gw = SPLITMIX32(x, z);
6160 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006161 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006162
6163 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006164 }
6165 else if (argvars[0].v_type == VAR_LIST)
6166 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006167 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006168 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006169 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006170
6171 lx = list_find(l, 0L);
6172 ly = list_find(l, 1L);
6173 lz = list_find(l, 2L);
6174 lw = list_find(l, 3L);
6175 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6176 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6177 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6178 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6179 x = (UINT32_T)lx->li_tv.vval.v_number;
6180 y = (UINT32_T)ly->li_tv.vval.v_number;
6181 z = (UINT32_T)lz->li_tv.vval.v_number;
6182 w = (UINT32_T)lw->li_tv.vval.v_number;
6183
6184 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6185
6186 lx->li_tv.vval.v_number = (varnumber_T)x;
6187 ly->li_tv.vval.v_number = (varnumber_T)y;
6188 lz->li_tv.vval.v_number = (varnumber_T)z;
6189 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006190 }
6191 else
6192 goto theend;
6193
6194 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006195 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006196 return;
6197
6198theend:
6199 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006200 rettv->v_type = VAR_NUMBER;
6201 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006202}
6203
6204/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006205 * "srand()" function
6206 */
6207 static void
6208f_srand(typval_T *argvars, typval_T *rettv)
6209{
6210 UINT32_T x = 0, z;
6211
6212 if (rettv_list_alloc(rettv) == FAIL)
6213 return;
6214 if (argvars[0].v_type == VAR_UNKNOWN)
6215 {
6216 init_srand(&x);
6217 }
6218 else
6219 {
6220 int error = FALSE;
6221
6222 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6223 if (error)
6224 return;
6225 }
6226
6227 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6228 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6229 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6230 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6231}
6232
6233#undef ROTL
6234#undef SPLITMIX32
6235#undef SHUFFLE_XOSHIRO128STARSTAR
6236
6237/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 * "range()" function
6239 */
6240 static void
6241f_range(typval_T *argvars, typval_T *rettv)
6242{
6243 varnumber_T start;
6244 varnumber_T end;
6245 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 int error = FALSE;
6247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006248 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 if (argvars[1].v_type == VAR_UNKNOWN)
6250 {
6251 end = start - 1;
6252 start = 0;
6253 }
6254 else
6255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006258 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 }
6260
6261 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006264 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 list_T *list = rettv->vval.v_list;
6270
6271 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006272 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006273 // be called.
6274 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006275 list->lv_u.nonmat.lv_start = start;
6276 list->lv_u.nonmat.lv_end = end;
6277 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006278 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 }
6280}
6281
6282/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006283 * Materialize "list".
6284 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 */
6286 void
6287range_list_materialize(list_T *list)
6288{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006289 varnumber_T start = list->lv_u.nonmat.lv_start;
6290 varnumber_T end = list->lv_u.nonmat.lv_end;
6291 int stride = list->lv_u.nonmat.lv_stride;
6292 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006294 list->lv_first = NULL;
6295 list->lv_u.mat.lv_last = NULL;
6296 list->lv_len = 0;
6297 list->lv_u.mat.lv_idx_item = NULL;
6298 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6299 if (list_append_number(list, (varnumber_T)i) == FAIL)
6300 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301}
6302
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006303/*
6304 * "getreginfo()" function
6305 */
6306 static void
6307f_getreginfo(typval_T *argvars, typval_T *rettv)
6308{
6309 char_u *strregname;
6310 int regname;
6311 char_u buf[NUMBUFLEN + 2];
6312 long reglen = 0;
6313 dict_T *dict;
6314 list_T *list;
6315
6316 if (argvars[0].v_type != VAR_UNKNOWN)
6317 {
6318 strregname = tv_get_string_chk(&argvars[0]);
6319 if (strregname == NULL)
6320 return;
6321 }
6322 else
6323 strregname = get_vim_var_str(VV_REG);
6324
6325 regname = (strregname == NULL ? '"' : *strregname);
6326 if (regname == 0 || regname == '@')
6327 regname = '"';
6328
6329 if (rettv_dict_alloc(rettv) == FAIL)
6330 return;
6331 dict = rettv->vval.v_dict;
6332
6333 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6334 if (list == NULL)
6335 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006336 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006337
6338 buf[0] = NUL;
6339 buf[1] = NUL;
6340 switch (get_reg_type(regname, &reglen))
6341 {
6342 case MLINE: buf[0] = 'V'; break;
6343 case MCHAR: buf[0] = 'v'; break;
6344 case MBLOCK:
6345 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6346 reglen + 1);
6347 break;
6348 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006349 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006350
6351 buf[0] = get_register_name(get_unname_register());
6352 buf[1] = NUL;
6353 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006354 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006355 else
6356 {
6357 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6358
6359 if (item != NULL)
6360 {
6361 item->di_tv.v_type = VAR_SPECIAL;
6362 item->di_tv.vval.v_number = regname == buf[0]
6363 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006364 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006365 }
6366 }
6367}
6368
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006369 static void
6370return_register(int regname, typval_T *rettv)
6371{
6372 char_u buf[2] = {0, 0};
6373
6374 buf[0] = (char_u)regname;
6375 rettv->v_type = VAR_STRING;
6376 rettv->vval.v_string = vim_strsave(buf);
6377}
6378
6379/*
6380 * "reg_executing()" function
6381 */
6382 static void
6383f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6384{
6385 return_register(reg_executing, rettv);
6386}
6387
6388/*
6389 * "reg_recording()" function
6390 */
6391 static void
6392f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6393{
6394 return_register(reg_recording, rettv);
6395}
6396
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006397/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 * "rename({from}, {to})" function
6399 */
6400 static void
6401f_rename(typval_T *argvars, typval_T *rettv)
6402{
6403 char_u buf[NUMBUFLEN];
6404
6405 if (check_restricted() || check_secure())
6406 rettv->vval.v_number = -1;
6407 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006408 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6409 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410}
6411
6412/*
6413 * "repeat()" function
6414 */
6415 static void
6416f_repeat(typval_T *argvars, typval_T *rettv)
6417{
6418 char_u *p;
6419 int n;
6420 int slen;
6421 int len;
6422 char_u *r;
6423 int i;
6424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006425 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006426 if (argvars[0].v_type == VAR_LIST)
6427 {
6428 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6429 while (n-- > 0)
6430 if (list_extend(rettv->vval.v_list,
6431 argvars[0].vval.v_list, NULL) == FAIL)
6432 break;
6433 }
6434 else
6435 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006436 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 rettv->v_type = VAR_STRING;
6438 rettv->vval.v_string = NULL;
6439
6440 slen = (int)STRLEN(p);
6441 len = slen * n;
6442 if (len <= 0)
6443 return;
6444
6445 r = alloc(len + 1);
6446 if (r != NULL)
6447 {
6448 for (i = 0; i < n; i++)
6449 mch_memmove(r + i * slen, p, (size_t)slen);
6450 r[len] = NUL;
6451 }
6452
6453 rettv->vval.v_string = r;
6454 }
6455}
6456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457#define SP_NOMOVE 0x01 // don't move cursor
6458#define SP_REPEAT 0x02 // repeat to find outer pair
6459#define SP_RETCOUNT 0x04 // return matchcount
6460#define SP_SETPCMARK 0x08 // set previous context mark
6461#define SP_START 0x10 // accept match at start position
6462#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6463#define SP_END 0x40 // leave cursor at end of match
6464#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466/*
6467 * Get flags for a search function.
6468 * Possibly sets "p_ws".
6469 * Returns BACKWARD, FORWARD or zero (for an error).
6470 */
6471 static int
6472get_search_arg(typval_T *varp, int *flagsp)
6473{
6474 int dir = FORWARD;
6475 char_u *flags;
6476 char_u nbuf[NUMBUFLEN];
6477 int mask;
6478
6479 if (varp->v_type != VAR_UNKNOWN)
6480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006481 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006483 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484 while (*flags != NUL)
6485 {
6486 switch (*flags)
6487 {
6488 case 'b': dir = BACKWARD; break;
6489 case 'w': p_ws = TRUE; break;
6490 case 'W': p_ws = FALSE; break;
6491 default: mask = 0;
6492 if (flagsp != NULL)
6493 switch (*flags)
6494 {
6495 case 'c': mask = SP_START; break;
6496 case 'e': mask = SP_END; break;
6497 case 'm': mask = SP_RETCOUNT; break;
6498 case 'n': mask = SP_NOMOVE; break;
6499 case 'p': mask = SP_SUBPAT; break;
6500 case 'r': mask = SP_REPEAT; break;
6501 case 's': mask = SP_SETPCMARK; break;
6502 case 'z': mask = SP_COLUMN; break;
6503 }
6504 if (mask == 0)
6505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006506 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507 dir = 0;
6508 }
6509 else
6510 *flagsp |= mask;
6511 }
6512 if (dir == 0)
6513 break;
6514 ++flags;
6515 }
6516 }
6517 return dir;
6518}
6519
6520/*
6521 * Shared by search() and searchpos() functions.
6522 */
6523 static int
6524search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6525{
6526 int flags;
6527 char_u *pat;
6528 pos_T pos;
6529 pos_T save_cursor;
6530 int save_p_ws = p_ws;
6531 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006532 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006535 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 long time_limit = 0;
6537#endif
6538 int options = SEARCH_KEEP;
6539 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006540 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006541 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006542 pos_T firstpos;
6543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006544 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006545 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006546 if (dir == 0)
6547 goto theend;
6548 flags = *flagsp;
6549 if (flags & SP_START)
6550 options |= SEARCH_START;
6551 if (flags & SP_END)
6552 options |= SEARCH_END;
6553 if (flags & SP_COLUMN)
6554 options |= SEARCH_COL;
6555
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006556 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006559 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560 if (lnum_stop < 0)
6561 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 if (argvars[3].v_type != VAR_UNKNOWN)
6563 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006564#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006565 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566 if (time_limit < 0)
6567 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006569 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 }
6572
6573#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006574 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575 profile_setlimit(time_limit, &tm);
6576#endif
6577
6578 /*
6579 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6580 * Check to make sure only those flags are set.
6581 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6582 * flags cannot be set. Check for that condition also.
6583 */
6584 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6585 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006587 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 goto theend;
6589 }
6590
6591 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006592 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006593 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006594 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6595#ifdef FEAT_RELTIME
6596 sia.sa_tm = &tm;
6597#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006598
6599 // Repeat until {skip} returns FALSE.
6600 for (;;)
6601 {
6602 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006603 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006604 // finding the first match again means there is no match where {skip}
6605 // evaluates to zero.
6606 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6607 subpatnum = FAIL;
6608
Bram Moolenaara9c01042020-06-07 14:50:50 +02006609 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006610 // didn't find it or no skip argument
6611 break;
6612 firstpos = pos;
6613
Bram Moolenaara9c01042020-06-07 14:50:50 +02006614 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006615 {
6616 int do_skip;
6617 int err;
6618 pos_T save_pos = curwin->w_cursor;
6619
6620 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006621 err = FALSE;
6622 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006623 curwin->w_cursor = save_pos;
6624 if (err)
6625 {
6626 // Evaluating {skip} caused an error, break here.
6627 subpatnum = FAIL;
6628 break;
6629 }
6630 if (!do_skip)
6631 break;
6632 }
6633 }
6634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 if (subpatnum != FAIL)
6636 {
6637 if (flags & SP_SUBPAT)
6638 retval = subpatnum;
6639 else
6640 retval = pos.lnum;
6641 if (flags & SP_SETPCMARK)
6642 setpcmark();
6643 curwin->w_cursor = pos;
6644 if (match_pos != NULL)
6645 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 match_pos->lnum = pos.lnum;
6648 match_pos->col = pos.col + 1;
6649 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006650 // "/$" will put the cursor after the end of the line, may need to
6651 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 check_cursor();
6653 }
6654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006655 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656 if (flags & SP_NOMOVE)
6657 curwin->w_cursor = save_cursor;
6658 else
6659 curwin->w_set_curswant = TRUE;
6660theend:
6661 p_ws = save_p_ws;
6662
6663 return retval;
6664}
6665
6666#ifdef FEAT_FLOAT
6667
6668/*
6669 * round() is not in C90, use ceil() or floor() instead.
6670 */
6671 float_T
6672vim_round(float_T f)
6673{
6674 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6675}
6676
6677/*
6678 * "round({float})" function
6679 */
6680 static void
6681f_round(typval_T *argvars, typval_T *rettv)
6682{
6683 float_T f = 0.0;
6684
6685 rettv->v_type = VAR_FLOAT;
6686 if (get_float_arg(argvars, &f) == OK)
6687 rettv->vval.v_float = vim_round(f);
6688 else
6689 rettv->vval.v_float = 0.0;
6690}
6691#endif
6692
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006693#ifdef FEAT_RUBY
6694/*
6695 * "rubyeval()" function
6696 */
6697 static void
6698f_rubyeval(typval_T *argvars, typval_T *rettv)
6699{
6700 char_u *str;
6701 char_u buf[NUMBUFLEN];
6702
6703 str = tv_get_string_buf(&argvars[0], buf);
6704 do_rubyeval(str, rettv);
6705}
6706#endif
6707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708/*
6709 * "screenattr()" function
6710 */
6711 static void
6712f_screenattr(typval_T *argvars, typval_T *rettv)
6713{
6714 int row;
6715 int col;
6716 int c;
6717
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006718 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6719 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 if (row < 0 || row >= screen_Rows
6721 || col < 0 || col >= screen_Columns)
6722 c = -1;
6723 else
6724 c = ScreenAttrs[LineOffset[row] + col];
6725 rettv->vval.v_number = c;
6726}
6727
6728/*
6729 * "screenchar()" function
6730 */
6731 static void
6732f_screenchar(typval_T *argvars, typval_T *rettv)
6733{
6734 int row;
6735 int col;
6736 int off;
6737 int c;
6738
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006739 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6740 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006741 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 c = -1;
6743 else
6744 {
6745 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746 if (enc_utf8 && ScreenLinesUC[off] != 0)
6747 c = ScreenLinesUC[off];
6748 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749 c = ScreenLines[off];
6750 }
6751 rettv->vval.v_number = c;
6752}
6753
6754/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006755 * "screenchars()" function
6756 */
6757 static void
6758f_screenchars(typval_T *argvars, typval_T *rettv)
6759{
6760 int row;
6761 int col;
6762 int off;
6763 int c;
6764 int i;
6765
6766 if (rettv_list_alloc(rettv) == FAIL)
6767 return;
6768 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6769 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6770 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6771 return;
6772
6773 off = LineOffset[row] + col;
6774 if (enc_utf8 && ScreenLinesUC[off] != 0)
6775 c = ScreenLinesUC[off];
6776 else
6777 c = ScreenLines[off];
6778 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6779
6780 if (enc_utf8)
6781
6782 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6783 list_append_number(rettv->vval.v_list,
6784 (varnumber_T)ScreenLinesC[i][off]);
6785}
6786
6787/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788 * "screencol()" function
6789 *
6790 * First column is 1 to be consistent with virtcol().
6791 */
6792 static void
6793f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6794{
6795 rettv->vval.v_number = screen_screencol() + 1;
6796}
6797
6798/*
6799 * "screenrow()" function
6800 */
6801 static void
6802f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6803{
6804 rettv->vval.v_number = screen_screenrow() + 1;
6805}
6806
6807/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006808 * "screenstring()" function
6809 */
6810 static void
6811f_screenstring(typval_T *argvars, typval_T *rettv)
6812{
6813 int row;
6814 int col;
6815 int off;
6816 int c;
6817 int i;
6818 char_u buf[MB_MAXBYTES + 1];
6819 int buflen = 0;
6820
6821 rettv->vval.v_string = NULL;
6822 rettv->v_type = VAR_STRING;
6823
6824 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6825 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6826 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6827 return;
6828
6829 off = LineOffset[row] + col;
6830 if (enc_utf8 && ScreenLinesUC[off] != 0)
6831 c = ScreenLinesUC[off];
6832 else
6833 c = ScreenLines[off];
6834 buflen += mb_char2bytes(c, buf);
6835
6836 if (enc_utf8)
6837 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6838 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6839
6840 buf[buflen] = NUL;
6841 rettv->vval.v_string = vim_strsave(buf);
6842}
6843
6844/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845 * "search()" function
6846 */
6847 static void
6848f_search(typval_T *argvars, typval_T *rettv)
6849{
6850 int flags = 0;
6851
6852 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6853}
6854
6855/*
6856 * "searchdecl()" function
6857 */
6858 static void
6859f_searchdecl(typval_T *argvars, typval_T *rettv)
6860{
6861 int locally = 1;
6862 int thisblock = 0;
6863 int error = FALSE;
6864 char_u *name;
6865
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006866 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006868 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 if (argvars[1].v_type != VAR_UNKNOWN)
6870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006871 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006873 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 }
6875 if (!error && name != NULL)
6876 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6877 locally, thisblock, SEARCH_KEEP) == FAIL;
6878}
6879
6880/*
6881 * Used by searchpair() and searchpairpos()
6882 */
6883 static int
6884searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6885{
6886 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006887 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 int save_p_ws = p_ws;
6889 int dir;
6890 int flags = 0;
6891 char_u nbuf1[NUMBUFLEN];
6892 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006893 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 long lnum_stop = 0;
6895 long time_limit = 0;
6896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006897 // Get the three pattern arguments: start, middle, end. Will result in an
6898 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006899 spat = tv_get_string_chk(&argvars[0]);
6900 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6901 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006903 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006905 // Handle the optional fourth argument: flags
6906 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 if (dir == 0)
6908 goto theend;
6909
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006910 // Don't accept SP_END or SP_SUBPAT.
6911 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 if ((flags & (SP_END | SP_SUBPAT)) != 0
6913 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6914 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006915 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 goto theend;
6917 }
6918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (flags & SP_REPEAT)
6921 p_ws = FALSE;
6922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006923 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006924 if (argvars[3].v_type == VAR_UNKNOWN
6925 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006926 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927 else
6928 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006929 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006930 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 if (argvars[5].v_type != VAR_UNKNOWN)
6933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006934 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006937 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940#ifdef FEAT_RELTIME
6941 if (argvars[6].v_type != VAR_UNKNOWN)
6942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006943 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006946 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 }
6950#endif
6951 }
6952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953
6954 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6955 match_pos, lnum_stop, time_limit);
6956
6957theend:
6958 p_ws = save_p_ws;
6959
6960 return retval;
6961}
6962
6963/*
6964 * "searchpair()" function
6965 */
6966 static void
6967f_searchpair(typval_T *argvars, typval_T *rettv)
6968{
6969 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6970}
6971
6972/*
6973 * "searchpairpos()" function
6974 */
6975 static void
6976f_searchpairpos(typval_T *argvars, typval_T *rettv)
6977{
6978 pos_T match_pos;
6979 int lnum = 0;
6980 int col = 0;
6981
6982 if (rettv_list_alloc(rettv) == FAIL)
6983 return;
6984
6985 if (searchpair_cmn(argvars, &match_pos) > 0)
6986 {
6987 lnum = match_pos.lnum;
6988 col = match_pos.col;
6989 }
6990
6991 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6992 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6993}
6994
6995/*
6996 * Search for a start/middle/end thing.
6997 * Used by searchpair(), see its documentation for the details.
6998 * Returns 0 or -1 for no match,
6999 */
7000 long
7001do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007002 char_u *spat, // start pattern
7003 char_u *mpat, // middle pattern
7004 char_u *epat, // end pattern
7005 int dir, // BACKWARD or FORWARD
7006 typval_T *skip, // skip expression
7007 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007009 linenr_T lnum_stop, // stop at this line if not zero
7010 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011{
7012 char_u *save_cpo;
7013 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7014 long retval = 0;
7015 pos_T pos;
7016 pos_T firstpos;
7017 pos_T foundpos;
7018 pos_T save_cursor;
7019 pos_T save_pos;
7020 int n;
7021 int r;
7022 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007023 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 int err;
7025 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007026#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007028#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007030 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 save_cpo = p_cpo;
7032 p_cpo = empty_option;
7033
7034#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007035 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 profile_setlimit(time_limit, &tm);
7037#endif
7038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007039 // Make two search patterns: start/end (pat2, for in nested pairs) and
7040 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007041 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7042 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 if (pat2 == NULL || pat3 == NULL)
7044 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007045 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 if (*mpat == NUL)
7047 STRCPY(pat3, pat2);
7048 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007049 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 spat, epat, mpat);
7051 if (flags & SP_START)
7052 options |= SEARCH_START;
7053
Bram Moolenaar48570482017-10-30 21:48:41 +01007054 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007055 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 save_cursor = curwin->w_cursor;
7058 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007059 CLEAR_POS(&firstpos);
7060 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007061 pat = pat3;
7062 for (;;)
7063 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007064 searchit_arg_T sia;
7065
Bram Moolenaara80faa82020-04-12 19:37:17 +02007066 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007067 sia.sa_stop_lnum = lnum_stop;
7068#ifdef FEAT_RELTIME
7069 sia.sa_tm = &tm;
7070#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007071 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007072 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007073 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007074 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007075 break;
7076
7077 if (firstpos.lnum == 0)
7078 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007079 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007081 // Found the same position again. Can happen with a pattern that
7082 // has "\zs" at the end and searching backwards. Advance one
7083 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 if (dir == BACKWARD)
7085 decl(&pos);
7086 else
7087 incl(&pos);
7088 }
7089 foundpos = pos;
7090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007091 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 options &= ~SEARCH_START;
7093
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007094 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007095 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096 {
7097 save_pos = curwin->w_cursor;
7098 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007099 err = FALSE;
7100 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 curwin->w_cursor = save_pos;
7102 if (err)
7103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007104 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 curwin->w_cursor = save_cursor;
7106 retval = -1;
7107 break;
7108 }
7109 if (r)
7110 continue;
7111 }
7112
7113 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007115 // Found end when searching backwards or start when searching
7116 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007118 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 }
7120 else
7121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007122 // Found end when searching forward or start when searching
7123 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007125 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 }
7127
7128 if (nest == 0)
7129 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007130 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 if (flags & SP_RETCOUNT)
7132 ++retval;
7133 else
7134 retval = pos.lnum;
7135 if (flags & SP_SETPCMARK)
7136 setpcmark();
7137 curwin->w_cursor = pos;
7138 if (!(flags & SP_REPEAT))
7139 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007140 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007141 }
7142 }
7143
7144 if (match_pos != NULL)
7145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007146 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 match_pos->lnum = curwin->w_cursor.lnum;
7148 match_pos->col = curwin->w_cursor.col + 1;
7149 }
7150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007151 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007152 if ((flags & SP_NOMOVE) || retval == 0)
7153 curwin->w_cursor = save_cursor;
7154
7155theend:
7156 vim_free(pat2);
7157 vim_free(pat3);
7158 if (p_cpo == empty_option)
7159 p_cpo = save_cpo;
7160 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007161 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007162 free_string_option(save_cpo);
7163
7164 return retval;
7165}
7166
7167/*
7168 * "searchpos()" function
7169 */
7170 static void
7171f_searchpos(typval_T *argvars, typval_T *rettv)
7172{
7173 pos_T match_pos;
7174 int lnum = 0;
7175 int col = 0;
7176 int n;
7177 int flags = 0;
7178
7179 if (rettv_list_alloc(rettv) == FAIL)
7180 return;
7181
7182 n = search_cmn(argvars, &match_pos, &flags);
7183 if (n > 0)
7184 {
7185 lnum = match_pos.lnum;
7186 col = match_pos.col;
7187 }
7188
7189 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7190 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7191 if (flags & SP_SUBPAT)
7192 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7193}
7194
7195 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7197{
7198 dict_T *d;
7199 dictitem_T *di;
7200 char_u *csearch;
7201
7202 if (argvars[0].v_type != VAR_DICT)
7203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007204 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205 return;
7206 }
7207
7208 if ((d = argvars[0].vval.v_dict) != NULL)
7209 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007210 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 if (csearch != NULL)
7212 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 if (enc_utf8)
7214 {
7215 int pcc[MAX_MCO];
7216 int c = utfc_ptr2char(csearch, pcc);
7217
7218 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7219 }
7220 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007222 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223 }
7224
7225 di = dict_find(d, (char_u *)"forward", -1);
7226 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007227 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 ? FORWARD : BACKWARD);
7229
7230 di = dict_find(d, (char_u *)"until", -1);
7231 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007232 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 }
7234}
7235
7236/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007237 * "setenv()" function
7238 */
7239 static void
7240f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7241{
7242 char_u namebuf[NUMBUFLEN];
7243 char_u valbuf[NUMBUFLEN];
7244 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7245
7246 if (argvars[1].v_type == VAR_SPECIAL
7247 && argvars[1].vval.v_number == VVAL_NULL)
7248 vim_unsetenv(name);
7249 else
7250 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7251}
7252
7253/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 * "setfperm({fname}, {mode})" function
7255 */
7256 static void
7257f_setfperm(typval_T *argvars, typval_T *rettv)
7258{
7259 char_u *fname;
7260 char_u modebuf[NUMBUFLEN];
7261 char_u *mode_str;
7262 int i;
7263 int mask;
7264 int mode = 0;
7265
7266 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007267 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007268 if (fname == NULL)
7269 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007270 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 if (mode_str == NULL)
7272 return;
7273 if (STRLEN(mode_str) != 9)
7274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007275 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 return;
7277 }
7278
7279 mask = 1;
7280 for (i = 8; i >= 0; --i)
7281 {
7282 if (mode_str[i] != '-')
7283 mode |= mask;
7284 mask = mask << 1;
7285 }
7286 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7287}
7288
7289/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 * "setpos()" function
7291 */
7292 static void
7293f_setpos(typval_T *argvars, typval_T *rettv)
7294{
7295 pos_T pos;
7296 int fnum;
7297 char_u *name;
7298 colnr_T curswant = -1;
7299
7300 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007301 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 if (name != NULL)
7303 {
7304 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7305 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007306 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 pos.col = 0;
7308 if (name[0] == '.' && name[1] == NUL)
7309 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007310 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007311 curwin->w_cursor = pos;
7312 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007314 curwin->w_curswant = curswant - 1;
7315 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007316 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007317 check_cursor();
7318 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319 }
7320 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7321 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007322 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 if (setmark_pos(name[1], &pos, fnum) == OK)
7324 rettv->vval.v_number = 0;
7325 }
7326 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007327 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 }
7329 }
7330}
7331
7332/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007333 * Translate a register type string to the yank type and block length
7334 */
7335 static int
7336get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7337{
7338 char_u *stropt = *pp;
7339 switch (*stropt)
7340 {
7341 case 'v': case 'c': // character-wise selection
7342 *yank_type = MCHAR;
7343 break;
7344 case 'V': case 'l': // line-wise selection
7345 *yank_type = MLINE;
7346 break;
7347 case 'b': case Ctrl_V: // block-wise selection
7348 *yank_type = MBLOCK;
7349 if (VIM_ISDIGIT(stropt[1]))
7350 {
7351 ++stropt;
7352 *block_len = getdigits(&stropt) - 1;
7353 --stropt;
7354 }
7355 break;
7356 default:
7357 return FAIL;
7358 }
7359 *pp = stropt;
7360 return OK;
7361}
7362
7363/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 * "setreg()" function
7365 */
7366 static void
7367f_setreg(typval_T *argvars, typval_T *rettv)
7368{
7369 int regname;
7370 char_u *strregname;
7371 char_u *stropt;
7372 char_u *strval;
7373 int append;
7374 char_u yank_type;
7375 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007376 typval_T *regcontents;
7377 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007378
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007379 pointreg = 0;
7380 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 block_len = -1;
7382 yank_type = MAUTO;
7383 append = FALSE;
7384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007386 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387
7388 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007389 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 regname = *strregname;
7391 if (regname == 0 || regname == '@')
7392 regname = '"';
7393
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007394 if (argvars[1].v_type == VAR_DICT)
7395 {
7396 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007397 dictitem_T *di;
7398
7399 if (d == NULL || d->dv_hashtab.ht_used == 0)
7400 {
7401 // Empty dict, clear the register (like setreg(0, []))
7402 char_u *lstval[2] = {NULL, NULL};
7403 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7404 return;
7405 }
7406
7407 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007408 if (di != NULL)
7409 regcontents = &di->di_tv;
7410
7411 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7412 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007413 {
7414 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7415
7416 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007417 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007418 semsg(_(e_invargval), "value");
7419 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007420 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007421 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007422
7423 if (regname == '"')
7424 {
7425 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7426 if (stropt != NULL)
7427 {
7428 pointreg = *stropt;
7429 regname = pointreg;
7430 }
7431 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007432 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007433 pointreg = regname;
7434 }
7435 else
7436 regcontents = &argvars[1];
7437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 if (argvars[2].v_type != VAR_UNKNOWN)
7439 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007440 if (yank_type != MAUTO)
7441 {
7442 semsg(_(e_toomanyarg), "setreg");
7443 return;
7444 }
7445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007446 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007448 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 for (; *stropt != NUL; ++stropt)
7450 switch (*stropt)
7451 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007452 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 append = TRUE;
7454 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007455 default:
7456 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007457 }
7458 }
7459
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007460 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 {
7462 char_u **lstval;
7463 char_u **allocval;
7464 char_u buf[NUMBUFLEN];
7465 char_u **curval;
7466 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007467 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468 listitem_T *li;
7469 int len;
7470
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007471 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 len = ll == NULL ? 0 : ll->lv_len;
7473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007474 // First half: use for pointers to result lines; second half: use for
7475 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007476 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 if (lstval == NULL)
7478 return;
7479 curval = lstval;
7480 allocval = lstval + len + 2;
7481 curallocval = allocval;
7482
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007483 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007485 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007486 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007488 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 if (strval == NULL)
7490 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007491 if (strval == buf)
7492 {
7493 // Need to make a copy, next tv_get_string_buf_chk() will
7494 // overwrite the string.
7495 strval = vim_strsave(buf);
7496 if (strval == NULL)
7497 goto free_lstval;
7498 *curallocval++ = strval;
7499 }
7500 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 }
7503 *curval++ = NULL;
7504
7505 write_reg_contents_lst(regname, lstval, -1,
7506 append, yank_type, block_len);
7507free_lstval:
7508 while (curallocval > allocval)
7509 vim_free(*--curallocval);
7510 vim_free(lstval);
7511 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007512 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007514 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 if (strval == NULL)
7516 return;
7517 write_reg_contents_ex(regname, strval, -1,
7518 append, yank_type, block_len);
7519 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007520 if (pointreg != 0)
7521 get_yank_register(pointreg, TRUE);
7522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 rettv->vval.v_number = 0;
7524}
7525
7526/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007527 * "settagstack()" function
7528 */
7529 static void
7530f_settagstack(typval_T *argvars, typval_T *rettv)
7531{
7532 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7533 win_T *wp;
7534 dict_T *d;
7535 int action = 'r';
7536
7537 rettv->vval.v_number = -1;
7538
7539 // first argument: window number or id
7540 wp = find_win_by_nr_or_id(&argvars[0]);
7541 if (wp == NULL)
7542 return;
7543
7544 // second argument: dict with items to set in the tag stack
7545 if (argvars[1].v_type != VAR_DICT)
7546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007548 return;
7549 }
7550 d = argvars[1].vval.v_dict;
7551 if (d == NULL)
7552 return;
7553
7554 // third argument: action - 'a' for append and 'r' for replace.
7555 // default is to replace the stack.
7556 if (argvars[2].v_type == VAR_UNKNOWN)
7557 action = 'r';
7558 else if (argvars[2].v_type == VAR_STRING)
7559 {
7560 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007561 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007562 if (actstr == NULL)
7563 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007564 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7565 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007566 action = *actstr;
7567 else
7568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007569 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007570 return;
7571 }
7572 }
7573 else
7574 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007575 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007576 return;
7577 }
7578
7579 if (set_tagstack(wp, d, action) == OK)
7580 rettv->vval.v_number = 0;
7581}
7582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583#ifdef FEAT_CRYPT
7584/*
7585 * "sha256({string})" function
7586 */
7587 static void
7588f_sha256(typval_T *argvars, typval_T *rettv)
7589{
7590 char_u *p;
7591
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 rettv->vval.v_string = vim_strsave(
7594 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7595 rettv->v_type = VAR_STRING;
7596}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007597#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598
7599/*
7600 * "shellescape({string})" function
7601 */
7602 static void
7603f_shellescape(typval_T *argvars, typval_T *rettv)
7604{
Bram Moolenaar20615522017-06-05 18:46:26 +02007605 int do_special = non_zero_arg(&argvars[1]);
7606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007608 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 rettv->v_type = VAR_STRING;
7610}
7611
7612/*
7613 * shiftwidth() function
7614 */
7615 static void
7616f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7617{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007618 rettv->vval.v_number = 0;
7619
7620 if (argvars[0].v_type != VAR_UNKNOWN)
7621 {
7622 long col;
7623
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007624 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007625 if (col < 0)
7626 return; // type error; errmsg already given
7627#ifdef FEAT_VARTABS
7628 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7629 return;
7630#endif
7631 }
7632
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633 rettv->vval.v_number = get_sw_value(curbuf);
7634}
7635
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636#ifdef FEAT_FLOAT
7637/*
7638 * "sin()" function
7639 */
7640 static void
7641f_sin(typval_T *argvars, typval_T *rettv)
7642{
7643 float_T f = 0.0;
7644
7645 rettv->v_type = VAR_FLOAT;
7646 if (get_float_arg(argvars, &f) == OK)
7647 rettv->vval.v_float = sin(f);
7648 else
7649 rettv->vval.v_float = 0.0;
7650}
7651
7652/*
7653 * "sinh()" function
7654 */
7655 static void
7656f_sinh(typval_T *argvars, typval_T *rettv)
7657{
7658 float_T f = 0.0;
7659
7660 rettv->v_type = VAR_FLOAT;
7661 if (get_float_arg(argvars, &f) == OK)
7662 rettv->vval.v_float = sinh(f);
7663 else
7664 rettv->vval.v_float = 0.0;
7665}
7666#endif
7667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668/*
7669 * "soundfold({word})" function
7670 */
7671 static void
7672f_soundfold(typval_T *argvars, typval_T *rettv)
7673{
7674 char_u *s;
7675
7676 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007677 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678#ifdef FEAT_SPELL
7679 rettv->vval.v_string = eval_soundfold(s);
7680#else
7681 rettv->vval.v_string = vim_strsave(s);
7682#endif
7683}
7684
7685/*
7686 * "spellbadword()" function
7687 */
7688 static void
7689f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7690{
7691 char_u *word = (char_u *)"";
7692 hlf_T attr = HLF_COUNT;
7693 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007694#ifdef FEAT_SPELL
7695 int wo_spell_save = curwin->w_p_spell;
7696
7697 if (!curwin->w_p_spell)
7698 {
7699 did_set_spelllang(curwin);
7700 curwin->w_p_spell = TRUE;
7701 }
7702
7703 if (*curwin->w_s->b_p_spl == NUL)
7704 {
7705 emsg(_(e_no_spell));
7706 curwin->w_p_spell = wo_spell_save;
7707 return;
7708 }
7709#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710
7711 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007712 {
7713#ifdef FEAT_SPELL
7714 curwin->w_p_spell = wo_spell_save;
7715#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007717 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718
7719#ifdef FEAT_SPELL
7720 if (argvars[0].v_type == VAR_UNKNOWN)
7721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007722 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7724 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007725 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007727 curwin->w_set_curswant = TRUE;
7728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007730 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007732 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733 int capcol = -1;
7734
7735 if (str != NULL)
7736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007737 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 while (*str != NUL)
7739 {
7740 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7741 if (attr != HLF_COUNT)
7742 {
7743 word = str;
7744 break;
7745 }
7746 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007747 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007748 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749 }
7750 }
7751 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007752 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007753#endif
7754
7755 list_append_string(rettv->vval.v_list, word, len);
7756 list_append_string(rettv->vval.v_list, (char_u *)(
7757 attr == HLF_SPB ? "bad" :
7758 attr == HLF_SPR ? "rare" :
7759 attr == HLF_SPL ? "local" :
7760 attr == HLF_SPC ? "caps" :
7761 ""), -1);
7762}
7763
7764/*
7765 * "spellsuggest()" function
7766 */
7767 static void
7768f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7769{
7770#ifdef FEAT_SPELL
7771 char_u *str;
7772 int typeerr = FALSE;
7773 int maxcount;
7774 garray_T ga;
7775 int i;
7776 listitem_T *li;
7777 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007778 int wo_spell_save = curwin->w_p_spell;
7779
7780 if (!curwin->w_p_spell)
7781 {
7782 did_set_spelllang(curwin);
7783 curwin->w_p_spell = TRUE;
7784 }
7785
7786 if (*curwin->w_s->b_p_spl == NUL)
7787 {
7788 emsg(_(e_no_spell));
7789 curwin->w_p_spell = wo_spell_save;
7790 return;
7791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792#endif
7793
7794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007795 {
7796#ifdef FEAT_SPELL
7797 curwin->w_p_spell = wo_spell_save;
7798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801
7802#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007803 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007805 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 if (argvars[1].v_type != VAR_UNKNOWN)
7807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007808 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 if (maxcount <= 0)
7810 return;
7811 if (argvars[2].v_type != VAR_UNKNOWN)
7812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007813 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (typeerr)
7815 return;
7816 }
7817 }
7818 else
7819 maxcount = 25;
7820
7821 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7822
7823 for (i = 0; i < ga.ga_len; ++i)
7824 {
7825 str = ((char_u **)ga.ga_data)[i];
7826
7827 li = listitem_alloc();
7828 if (li == NULL)
7829 vim_free(str);
7830 else
7831 {
7832 li->li_tv.v_type = VAR_STRING;
7833 li->li_tv.v_lock = 0;
7834 li->li_tv.vval.v_string = str;
7835 list_append(rettv->vval.v_list, li);
7836 }
7837 }
7838 ga_clear(&ga);
7839 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007840 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841#endif
7842}
7843
7844 static void
7845f_split(typval_T *argvars, typval_T *rettv)
7846{
7847 char_u *str;
7848 char_u *end;
7849 char_u *pat = NULL;
7850 regmatch_T regmatch;
7851 char_u patbuf[NUMBUFLEN];
7852 char_u *save_cpo;
7853 int match;
7854 colnr_T col = 0;
7855 int keepempty = FALSE;
7856 int typeerr = FALSE;
7857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007858 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 save_cpo = p_cpo;
7860 p_cpo = (char_u *)"";
7861
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007862 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 if (argvars[1].v_type != VAR_UNKNOWN)
7864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007865 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 if (pat == NULL)
7867 typeerr = TRUE;
7868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007869 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870 }
7871 if (pat == NULL || *pat == NUL)
7872 pat = (char_u *)"[\\x01- ]\\+";
7873
7874 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007875 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007877 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878
7879 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7880 if (regmatch.regprog != NULL)
7881 {
7882 regmatch.rm_ic = FALSE;
7883 while (*str != NUL || keepempty)
7884 {
7885 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007886 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 else
7888 match = vim_regexec_nl(&regmatch, str, col);
7889 if (match)
7890 end = regmatch.startp[0];
7891 else
7892 end = str + STRLEN(str);
7893 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7894 && *str != NUL && match && end < regmatch.endp[0]))
7895 {
7896 if (list_append_string(rettv->vval.v_list, str,
7897 (int)(end - str)) == FAIL)
7898 break;
7899 }
7900 if (!match)
7901 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007902 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 if (regmatch.endp[0] > str)
7904 col = 0;
7905 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007906 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 str = regmatch.endp[0];
7909 }
7910
7911 vim_regfree(regmatch.regprog);
7912 }
7913
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007914theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 p_cpo = save_cpo;
7916}
7917
7918#ifdef FEAT_FLOAT
7919/*
7920 * "sqrt()" function
7921 */
7922 static void
7923f_sqrt(typval_T *argvars, typval_T *rettv)
7924{
7925 float_T f = 0.0;
7926
7927 rettv->v_type = VAR_FLOAT;
7928 if (get_float_arg(argvars, &f) == OK)
7929 rettv->vval.v_float = sqrt(f);
7930 else
7931 rettv->vval.v_float = 0.0;
7932}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007933#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007935#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007936/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937 * "str2float()" function
7938 */
7939 static void
7940f_str2float(typval_T *argvars, typval_T *rettv)
7941{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007942 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007943 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944
Bram Moolenaar08243d22017-01-10 16:12:29 +01007945 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 p = skipwhite(p + 1);
7947 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007948 if (isneg)
7949 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 rettv->v_type = VAR_FLOAT;
7951}
7952#endif
7953
7954/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007955 * "str2list()" function
7956 */
7957 static void
7958f_str2list(typval_T *argvars, typval_T *rettv)
7959{
7960 char_u *p;
7961 int utf8 = FALSE;
7962
7963 if (rettv_list_alloc(rettv) == FAIL)
7964 return;
7965
7966 if (argvars[1].v_type != VAR_UNKNOWN)
7967 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7968
7969 p = tv_get_string(&argvars[0]);
7970
7971 if (has_mbyte || utf8)
7972 {
7973 int (*ptr2len)(char_u *);
7974 int (*ptr2char)(char_u *);
7975
7976 if (utf8 || enc_utf8)
7977 {
7978 ptr2len = utf_ptr2len;
7979 ptr2char = utf_ptr2char;
7980 }
7981 else
7982 {
7983 ptr2len = mb_ptr2len;
7984 ptr2char = mb_ptr2char;
7985 }
7986
7987 for ( ; *p != NUL; p += (*ptr2len)(p))
7988 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7989 }
7990 else
7991 for ( ; *p != NUL; ++p)
7992 list_append_number(rettv->vval.v_list, *p);
7993}
7994
7995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 * "str2nr()" function
7997 */
7998 static void
7999f_str2nr(typval_T *argvars, typval_T *rettv)
8000{
8001 int base = 10;
8002 char_u *p;
8003 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008004 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008005 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006
8007 if (argvars[1].v_type != VAR_UNKNOWN)
8008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008009 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008010 if (base != 2 && base != 8 && base != 10 && base != 16)
8011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 return;
8014 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008015 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8016 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 }
8018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008019 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008020 isneg = (*p == '-');
8021 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 p = skipwhite(p + 1);
8023 switch (base)
8024 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008025 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008026 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008027 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008029 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8030 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008031 if (isneg)
8032 rettv->vval.v_number = -n;
8033 else
8034 rettv->vval.v_number = n;
8035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036}
8037
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038/*
8039 * "strgetchar()" function
8040 */
8041 static void
8042f_strgetchar(typval_T *argvars, typval_T *rettv)
8043{
8044 char_u *str;
8045 int len;
8046 int error = FALSE;
8047 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008048 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049
8050 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008051 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 if (str == NULL)
8053 return;
8054 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008055 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 if (error)
8057 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058
Bram Moolenaar13505972019-01-24 15:04:48 +01008059 while (charidx >= 0 && byteidx < len)
8060 {
8061 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008063 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8064 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008066 --charidx;
8067 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069}
8070
8071/*
8072 * "stridx()" function
8073 */
8074 static void
8075f_stridx(typval_T *argvars, typval_T *rettv)
8076{
8077 char_u buf[NUMBUFLEN];
8078 char_u *needle;
8079 char_u *haystack;
8080 char_u *save_haystack;
8081 char_u *pos;
8082 int start_idx;
8083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008084 needle = tv_get_string_chk(&argvars[1]);
8085 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 rettv->vval.v_number = -1;
8087 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008088 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089
8090 if (argvars[2].v_type != VAR_UNKNOWN)
8091 {
8092 int error = FALSE;
8093
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008094 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 if (error || start_idx >= (int)STRLEN(haystack))
8096 return;
8097 if (start_idx >= 0)
8098 haystack += start_idx;
8099 }
8100
8101 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8102 if (pos != NULL)
8103 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8104}
8105
8106/*
8107 * "string()" function
8108 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008109 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110f_string(typval_T *argvars, typval_T *rettv)
8111{
8112 char_u *tofree;
8113 char_u numbuf[NUMBUFLEN];
8114
8115 rettv->v_type = VAR_STRING;
8116 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8117 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008118 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 if (rettv->vval.v_string != NULL && tofree == NULL)
8120 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8121}
8122
8123/*
8124 * "strlen()" function
8125 */
8126 static void
8127f_strlen(typval_T *argvars, typval_T *rettv)
8128{
8129 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008130 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131}
8132
8133/*
8134 * "strchars()" function
8135 */
8136 static void
8137f_strchars(typval_T *argvars, typval_T *rettv)
8138{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008139 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 varnumber_T len = 0;
8142 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143
8144 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008145 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008147 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 else
8149 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8151 while (*s != NUL)
8152 {
8153 func_mb_ptr2char_adv(&s);
8154 ++len;
8155 }
8156 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 }
8158}
8159
8160/*
8161 * "strdisplaywidth()" function
8162 */
8163 static void
8164f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8165{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008166 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167 int col = 0;
8168
8169 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008170 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008171
8172 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8173}
8174
8175/*
8176 * "strwidth()" function
8177 */
8178 static void
8179f_strwidth(typval_T *argvars, typval_T *rettv)
8180{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008181 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008182
Bram Moolenaar13505972019-01-24 15:04:48 +01008183 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184}
8185
8186/*
8187 * "strcharpart()" function
8188 */
8189 static void
8190f_strcharpart(typval_T *argvars, typval_T *rettv)
8191{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 char_u *p;
8193 int nchar;
8194 int nbyte = 0;
8195 int charlen;
8196 int len = 0;
8197 int slen;
8198 int error = FALSE;
8199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008200 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 slen = (int)STRLEN(p);
8202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008203 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 if (!error)
8205 {
8206 if (nchar > 0)
8207 while (nchar > 0 && nbyte < slen)
8208 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008209 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210 --nchar;
8211 }
8212 else
8213 nbyte = nchar;
8214 if (argvars[2].v_type != VAR_UNKNOWN)
8215 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008216 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 while (charlen > 0 && nbyte + len < slen)
8218 {
8219 int off = nbyte + len;
8220
8221 if (off < 0)
8222 len += 1;
8223 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008224 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225 --charlen;
8226 }
8227 }
8228 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008229 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 }
8231
8232 /*
8233 * Only return the overlap between the specified part and the actual
8234 * string.
8235 */
8236 if (nbyte < 0)
8237 {
8238 len += nbyte;
8239 nbyte = 0;
8240 }
8241 else if (nbyte > slen)
8242 nbyte = slen;
8243 if (len < 0)
8244 len = 0;
8245 else if (nbyte + len > slen)
8246 len = slen - nbyte;
8247
8248 rettv->v_type = VAR_STRING;
8249 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250}
8251
8252/*
8253 * "strpart()" function
8254 */
8255 static void
8256f_strpart(typval_T *argvars, typval_T *rettv)
8257{
8258 char_u *p;
8259 int n;
8260 int len;
8261 int slen;
8262 int error = FALSE;
8263
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008264 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 slen = (int)STRLEN(p);
8266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008267 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 if (error)
8269 len = 0;
8270 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008271 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008273 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008275 // Only return the overlap between the specified part and the actual
8276 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 if (n < 0)
8278 {
8279 len += n;
8280 n = 0;
8281 }
8282 else if (n > slen)
8283 n = slen;
8284 if (len < 0)
8285 len = 0;
8286 else if (n + len > slen)
8287 len = slen - n;
8288
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008289 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8290 {
8291 int off;
8292
8293 // length in characters
8294 for (off = n; off < slen && len > 0; --len)
8295 off += mb_ptr2len(p + off);
8296 len = off - n;
8297 }
8298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 rettv->v_type = VAR_STRING;
8300 rettv->vval.v_string = vim_strnsave(p + n, len);
8301}
8302
8303/*
8304 * "strridx()" function
8305 */
8306 static void
8307f_strridx(typval_T *argvars, typval_T *rettv)
8308{
8309 char_u buf[NUMBUFLEN];
8310 char_u *needle;
8311 char_u *haystack;
8312 char_u *rest;
8313 char_u *lastmatch = NULL;
8314 int haystack_len, end_idx;
8315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008316 needle = tv_get_string_chk(&argvars[1]);
8317 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318
8319 rettv->vval.v_number = -1;
8320 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008321 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322
8323 haystack_len = (int)STRLEN(haystack);
8324 if (argvars[2].v_type != VAR_UNKNOWN)
8325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008326 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008327 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008329 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 }
8331 else
8332 end_idx = haystack_len;
8333
8334 if (*needle == NUL)
8335 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008336 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 lastmatch = haystack + end_idx;
8338 }
8339 else
8340 {
8341 for (rest = haystack; *rest != '\0'; ++rest)
8342 {
8343 rest = (char_u *)strstr((char *)rest, (char *)needle);
8344 if (rest == NULL || rest > haystack + end_idx)
8345 break;
8346 lastmatch = rest;
8347 }
8348 }
8349
8350 if (lastmatch == NULL)
8351 rettv->vval.v_number = -1;
8352 else
8353 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8354}
8355
8356/*
8357 * "strtrans()" function
8358 */
8359 static void
8360f_strtrans(typval_T *argvars, typval_T *rettv)
8361{
8362 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008363 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364}
8365
8366/*
8367 * "submatch()" function
8368 */
8369 static void
8370f_submatch(typval_T *argvars, typval_T *rettv)
8371{
8372 int error = FALSE;
8373 int no;
8374 int retList = 0;
8375
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 if (error)
8378 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008379 if (no < 0 || no >= NSUBEXP)
8380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008381 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008382 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008383 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008385 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 if (error)
8387 return;
8388
8389 if (retList == 0)
8390 {
8391 rettv->v_type = VAR_STRING;
8392 rettv->vval.v_string = reg_submatch(no);
8393 }
8394 else
8395 {
8396 rettv->v_type = VAR_LIST;
8397 rettv->vval.v_list = reg_submatch_list(no);
8398 }
8399}
8400
8401/*
8402 * "substitute()" function
8403 */
8404 static void
8405f_substitute(typval_T *argvars, typval_T *rettv)
8406{
8407 char_u patbuf[NUMBUFLEN];
8408 char_u subbuf[NUMBUFLEN];
8409 char_u flagsbuf[NUMBUFLEN];
8410
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008411 char_u *str = tv_get_string_chk(&argvars[0]);
8412 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008413 char_u *sub = NULL;
8414 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008415 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008417 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8418 expr = &argvars[2];
8419 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008420 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008421
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008423 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8424 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 rettv->vval.v_string = NULL;
8426 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008427 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008428}
8429
8430/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008431 * "swapinfo(swap_filename)" function
8432 */
8433 static void
8434f_swapinfo(typval_T *argvars, typval_T *rettv)
8435{
8436 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008438}
8439
8440/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008441 * "swapname(expr)" function
8442 */
8443 static void
8444f_swapname(typval_T *argvars, typval_T *rettv)
8445{
8446 buf_T *buf;
8447
8448 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008449 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008450 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8451 || buf->b_ml.ml_mfp->mf_fname == NULL)
8452 rettv->vval.v_string = NULL;
8453 else
8454 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8455}
8456
8457/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 * "synID(lnum, col, trans)" function
8459 */
8460 static void
8461f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8462{
8463 int id = 0;
8464#ifdef FEAT_SYN_HL
8465 linenr_T lnum;
8466 colnr_T col;
8467 int trans;
8468 int transerr = FALSE;
8469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008470 lnum = tv_get_lnum(argvars); // -1 on type error
8471 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008472 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473
8474 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8475 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8476 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8477#endif
8478
8479 rettv->vval.v_number = id;
8480}
8481
8482/*
8483 * "synIDattr(id, what [, mode])" function
8484 */
8485 static void
8486f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8487{
8488 char_u *p = NULL;
8489#ifdef FEAT_SYN_HL
8490 int id;
8491 char_u *what;
8492 char_u *mode;
8493 char_u modebuf[NUMBUFLEN];
8494 int modec;
8495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008496 id = (int)tv_get_number(&argvars[0]);
8497 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 if (argvars[2].v_type != VAR_UNKNOWN)
8499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008500 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 modec = TOLOWER_ASC(mode[0]);
8502 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008503 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
8505 else
8506 {
8507#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8508 if (USE_24BIT)
8509 modec = 'g';
8510 else
8511#endif
8512 if (t_colors > 1)
8513 modec = 'c';
8514 else
8515 modec = 't';
8516 }
8517
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 switch (TOLOWER_ASC(what[0]))
8519 {
8520 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008521 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008523 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 p = highlight_has_attr(id, HL_BOLD, modec);
8525 break;
8526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008527 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 p = highlight_color(id, what, modec);
8529 break;
8530
8531 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008532 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008534 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 p = highlight_has_attr(id, HL_ITALIC, modec);
8536 break;
8537
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008538 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008539 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 break;
8541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008542 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 p = highlight_has_attr(id, HL_INVERSE, modec);
8544 break;
8545
8546 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008547 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008549 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008550 else if (TOLOWER_ASC(what[1]) == 't' &&
8551 TOLOWER_ASC(what[2]) == 'r')
8552 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008553 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 p = highlight_has_attr(id, HL_STANDOUT, modec);
8555 break;
8556
8557 case 'u':
8558 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008559 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8561 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008562 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8564 break;
8565 }
8566
8567 if (p != NULL)
8568 p = vim_strsave(p);
8569#endif
8570 rettv->v_type = VAR_STRING;
8571 rettv->vval.v_string = p;
8572}
8573
8574/*
8575 * "synIDtrans(id)" function
8576 */
8577 static void
8578f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8579{
8580 int id;
8581
8582#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008583 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584
8585 if (id > 0)
8586 id = syn_get_final_id(id);
8587 else
8588#endif
8589 id = 0;
8590
8591 rettv->vval.v_number = id;
8592}
8593
8594/*
8595 * "synconcealed(lnum, col)" function
8596 */
8597 static void
8598f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8599{
8600#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8601 linenr_T lnum;
8602 colnr_T col;
8603 int syntax_flags = 0;
8604 int cchar;
8605 int matchid = 0;
8606 char_u str[NUMBUFLEN];
8607#endif
8608
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008609 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610
8611#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008612 lnum = tv_get_lnum(argvars); // -1 on type error
8613 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614
Bram Moolenaara80faa82020-04-12 19:37:17 +02008615 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616
8617 if (rettv_list_alloc(rettv) != FAIL)
8618 {
8619 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8620 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8621 && curwin->w_p_cole > 0)
8622 {
8623 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8624 syntax_flags = get_syntax_info(&matchid);
8625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008626 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8628 {
8629 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008630 if (cchar == NUL && curwin->w_p_cole == 1)
8631 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (cchar != NUL)
8633 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 if (has_mbyte)
8635 (*mb_char2bytes)(cchar, str);
8636 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 str[0] = cchar;
8638 }
8639 }
8640 }
8641
8642 list_append_number(rettv->vval.v_list,
8643 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008644 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 list_append_string(rettv->vval.v_list, str, -1);
8646 list_append_number(rettv->vval.v_list, matchid);
8647 }
8648#endif
8649}
8650
8651/*
8652 * "synstack(lnum, col)" function
8653 */
8654 static void
8655f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8656{
8657#ifdef FEAT_SYN_HL
8658 linenr_T lnum;
8659 colnr_T col;
8660 int i;
8661 int id;
8662#endif
8663
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008664 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665
8666#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008667 lnum = tv_get_lnum(argvars); // -1 on type error
8668 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669
8670 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8671 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8672 && rettv_list_alloc(rettv) != FAIL)
8673 {
8674 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8675 for (i = 0; ; ++i)
8676 {
8677 id = syn_get_stack_item(i);
8678 if (id < 0)
8679 break;
8680 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8681 break;
8682 }
8683 }
8684#endif
8685}
8686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687/*
8688 * "tabpagebuflist()" function
8689 */
8690 static void
8691f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8692{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 tabpage_T *tp;
8694 win_T *wp = NULL;
8695
8696 if (argvars[0].v_type == VAR_UNKNOWN)
8697 wp = firstwin;
8698 else
8699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008700 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 if (tp != NULL)
8702 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8703 }
8704 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8705 {
8706 for (; wp != NULL; wp = wp->w_next)
8707 if (list_append_number(rettv->vval.v_list,
8708 wp->w_buffer->b_fnum) == FAIL)
8709 break;
8710 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711}
8712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 * "tagfiles()" function
8715 */
8716 static void
8717f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8718{
8719 char_u *fname;
8720 tagname_T tn;
8721 int first;
8722
8723 if (rettv_list_alloc(rettv) == FAIL)
8724 return;
8725 fname = alloc(MAXPATHL);
8726 if (fname == NULL)
8727 return;
8728
8729 for (first = TRUE; ; first = FALSE)
8730 if (get_tagfname(&tn, first, fname) == FAIL
8731 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8732 break;
8733 tagname_free(&tn);
8734 vim_free(fname);
8735}
8736
8737/*
8738 * "taglist()" function
8739 */
8740 static void
8741f_taglist(typval_T *argvars, typval_T *rettv)
8742{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008743 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 char_u *tag_pattern;
8745
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008746 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747
8748 rettv->vval.v_number = FALSE;
8749 if (*tag_pattern == NUL)
8750 return;
8751
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008752 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008755 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756}
8757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758#ifdef FEAT_FLOAT
8759/*
8760 * "tan()" function
8761 */
8762 static void
8763f_tan(typval_T *argvars, typval_T *rettv)
8764{
8765 float_T f = 0.0;
8766
8767 rettv->v_type = VAR_FLOAT;
8768 if (get_float_arg(argvars, &f) == OK)
8769 rettv->vval.v_float = tan(f);
8770 else
8771 rettv->vval.v_float = 0.0;
8772}
8773
8774/*
8775 * "tanh()" function
8776 */
8777 static void
8778f_tanh(typval_T *argvars, typval_T *rettv)
8779{
8780 float_T f = 0.0;
8781
8782 rettv->v_type = VAR_FLOAT;
8783 if (get_float_arg(argvars, &f) == OK)
8784 rettv->vval.v_float = tanh(f);
8785 else
8786 rettv->vval.v_float = 0.0;
8787}
8788#endif
8789
8790/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 * "tolower(string)" function
8792 */
8793 static void
8794f_tolower(typval_T *argvars, typval_T *rettv)
8795{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008797 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798}
8799
8800/*
8801 * "toupper(string)" function
8802 */
8803 static void
8804f_toupper(typval_T *argvars, typval_T *rettv)
8805{
8806 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008807 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808}
8809
8810/*
8811 * "tr(string, fromstr, tostr)" function
8812 */
8813 static void
8814f_tr(typval_T *argvars, typval_T *rettv)
8815{
8816 char_u *in_str;
8817 char_u *fromstr;
8818 char_u *tostr;
8819 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820 int inlen;
8821 int fromlen;
8822 int tolen;
8823 int idx;
8824 char_u *cpstr;
8825 int cplen;
8826 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 char_u buf[NUMBUFLEN];
8828 char_u buf2[NUMBUFLEN];
8829 garray_T ga;
8830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008831 in_str = tv_get_string(&argvars[0]);
8832 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8833 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008835 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 rettv->v_type = VAR_STRING;
8837 rettv->vval.v_string = NULL;
8838 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008839 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 ga_init2(&ga, (int)sizeof(char), 80);
8841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008843 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 if (STRLEN(fromstr) != STRLEN(tostr))
8845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008847 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 ga_clear(&ga);
8849 return;
8850 }
8851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008852 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 while (*in_str != NUL)
8854 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 if (has_mbyte)
8856 {
8857 inlen = (*mb_ptr2len)(in_str);
8858 cpstr = in_str;
8859 cplen = inlen;
8860 idx = 0;
8861 for (p = fromstr; *p != NUL; p += fromlen)
8862 {
8863 fromlen = (*mb_ptr2len)(p);
8864 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8865 {
8866 for (p = tostr; *p != NUL; p += tolen)
8867 {
8868 tolen = (*mb_ptr2len)(p);
8869 if (idx-- == 0)
8870 {
8871 cplen = tolen;
8872 cpstr = p;
8873 break;
8874 }
8875 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008876 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 goto error;
8878 break;
8879 }
8880 ++idx;
8881 }
8882
8883 if (first && cpstr == in_str)
8884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008885 // Check that fromstr and tostr have the same number of
8886 // (multi-byte) characters. Done only once when a character
8887 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 first = FALSE;
8889 for (p = tostr; *p != NUL; p += tolen)
8890 {
8891 tolen = (*mb_ptr2len)(p);
8892 --idx;
8893 }
8894 if (idx != 0)
8895 goto error;
8896 }
8897
8898 (void)ga_grow(&ga, cplen);
8899 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8900 ga.ga_len += cplen;
8901
8902 in_str += inlen;
8903 }
8904 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008906 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 p = vim_strchr(fromstr, *in_str);
8908 if (p != NULL)
8909 ga_append(&ga, tostr[p - fromstr]);
8910 else
8911 ga_append(&ga, *in_str);
8912 ++in_str;
8913 }
8914 }
8915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008916 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 (void)ga_grow(&ga, 1);
8918 ga_append(&ga, NUL);
8919
8920 rettv->vval.v_string = ga.ga_data;
8921}
8922
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008923/*
8924 * "trim({expr})" function
8925 */
8926 static void
8927f_trim(typval_T *argvars, typval_T *rettv)
8928{
8929 char_u buf1[NUMBUFLEN];
8930 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008931 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008932 char_u *mask = NULL;
8933 char_u *tail;
8934 char_u *prev;
8935 char_u *p;
8936 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008937 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008938
8939 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008940 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008941 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008942 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008943
8944 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008946 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008947
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008948 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008949 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008950 int error = 0;
8951
8952 // leading or trailing characters to trim
8953 dir = (int)tv_get_number_chk(&argvars[2], &error);
8954 if (error)
8955 return;
8956 if (dir < 0 || dir > 2)
8957 {
8958 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8959 return;
8960 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008961 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008962 }
8963
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008964 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008965 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008966 // Trim leading characters
8967 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008968 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008969 c1 = PTR2CHAR(head);
8970 if (mask == NULL)
8971 {
8972 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008973 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008974 }
8975 else
8976 {
8977 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8978 if (c1 == PTR2CHAR(p))
8979 break;
8980 if (*p == NUL)
8981 break;
8982 }
8983 MB_PTR_ADV(head);
8984 }
8985 }
8986
8987 tail = head + STRLEN(head);
8988 if (dir == 0 || dir == 2)
8989 {
8990 // Trim trailing characters
8991 for (; tail > head; tail = prev)
8992 {
8993 prev = tail;
8994 MB_PTR_BACK(head, prev);
8995 c1 = PTR2CHAR(prev);
8996 if (mask == NULL)
8997 {
8998 if (c1 > ' ' && c1 != 0xa0)
8999 break;
9000 }
9001 else
9002 {
9003 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9004 if (c1 == PTR2CHAR(p))
9005 break;
9006 if (*p == NUL)
9007 break;
9008 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009009 }
9010 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009011 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009012}
9013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014#ifdef FEAT_FLOAT
9015/*
9016 * "trunc({float})" function
9017 */
9018 static void
9019f_trunc(typval_T *argvars, typval_T *rettv)
9020{
9021 float_T f = 0.0;
9022
9023 rettv->v_type = VAR_FLOAT;
9024 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009025 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9027 else
9028 rettv->vval.v_float = 0.0;
9029}
9030#endif
9031
9032/*
9033 * "type(expr)" function
9034 */
9035 static void
9036f_type(typval_T *argvars, typval_T *rettv)
9037{
9038 int n = -1;
9039
9040 switch (argvars[0].v_type)
9041 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009042 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9043 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009045 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9046 case VAR_LIST: n = VAR_TYPE_LIST; break;
9047 case VAR_DICT: n = VAR_TYPE_DICT; break;
9048 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9049 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9050 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009051 case VAR_JOB: n = VAR_TYPE_JOB; break;
9052 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009053 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009055 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009056 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009057 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 n = -1;
9059 break;
9060 }
9061 rettv->vval.v_number = n;
9062}
9063
9064/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 * "virtcol(string)" function
9066 */
9067 static void
9068f_virtcol(typval_T *argvars, typval_T *rettv)
9069{
9070 colnr_T vcol = 0;
9071 pos_T *fp;
9072 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009073 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009074
9075 fp = var2fpos(&argvars[0], FALSE, &fnum);
9076 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9077 && fnum == curbuf->b_fnum)
9078 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009079 // Limit the column to a valid value, getvvcol() doesn't check.
9080 if (fp->col < 0)
9081 fp->col = 0;
9082 else
9083 {
9084 len = (int)STRLEN(ml_get(fp->lnum));
9085 if (fp->col > len)
9086 fp->col = len;
9087 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009088 getvvcol(curwin, fp, NULL, NULL, &vcol);
9089 ++vcol;
9090 }
9091
9092 rettv->vval.v_number = vcol;
9093}
9094
9095/*
9096 * "visualmode()" function
9097 */
9098 static void
9099f_visualmode(typval_T *argvars, typval_T *rettv)
9100{
9101 char_u str[2];
9102
9103 rettv->v_type = VAR_STRING;
9104 str[0] = curbuf->b_visual_mode_eval;
9105 str[1] = NUL;
9106 rettv->vval.v_string = vim_strsave(str);
9107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009108 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (non_zero_arg(&argvars[0]))
9110 curbuf->b_visual_mode_eval = NUL;
9111}
9112
9113/*
9114 * "wildmenumode()" function
9115 */
9116 static void
9117f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9118{
9119#ifdef FEAT_WILDMENU
9120 if (wild_menu_showing)
9121 rettv->vval.v_number = 1;
9122#endif
9123}
9124
9125/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009126 * "windowsversion()" function
9127 */
9128 static void
9129f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9130{
9131 rettv->v_type = VAR_STRING;
9132 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9133}
9134
9135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136 * "wordcount()" function
9137 */
9138 static void
9139f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9140{
9141 if (rettv_dict_alloc(rettv) == FAIL)
9142 return;
9143 cursor_pos_info(rettv->vval.v_dict);
9144}
9145
9146/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 * "xor(expr, expr)" function
9148 */
9149 static void
9150f_xor(typval_T *argvars, typval_T *rettv)
9151{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009152 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9153 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154}
9155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009156#endif // FEAT_EVAL