blob: c759afcd2030d0f363a3ffb4de67780f123ce726 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200385 static type_T *
386ret_remove(int argcount UNUSED, type_T **argtypes)
387{
388 if (argtypes[0]->tt_type == VAR_LIST
389 || argtypes[0]->tt_type == VAR_DICT)
390 return argtypes[0]->tt_member;
391 if (argtypes[0]->tt_type == VAR_BLOB)
392 return &t_number;
393 return &t_any;
394}
395
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200396 static type_T *
397ret_getreg(int argcount, type_T **argtypes UNUSED)
398{
399 // Assume that if the third argument is passed it's non-zero
400 if (argcount == 3)
401 return &t_list_string;
402 return &t_string;
403}
404
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200405 static type_T *
406ret_maparg(int argcount, type_T **argtypes UNUSED)
407{
408 // Assume that if the fourth argument is passed it's non-zero
409 if (argcount == 4)
410 return &t_dict_any;
411 return &t_string;
412}
413
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100414static type_T *ret_f_function(int argcount, type_T **argtypes);
415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416/*
417 * Array with names and number of arguments of all internal functions
418 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
419 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200420typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200422 char *f_name; // function name
423 char f_min_argc; // minimal number of arguments
424 char f_max_argc; // maximal number of arguments
425 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100426 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
427 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200429 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200430} funcentry_T;
431
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200432// values for f_argtype; zero means it cannot be used as a method
433#define FEARG_1 1 // base is the first argument
434#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200435#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200436#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200437#define FEARG_LAST 9 // base is the last argument
438
Bram Moolenaar15c47602020-03-26 22:16:48 +0100439#ifdef FEAT_FLOAT
440# define FLOAT_FUNC(name) name
441#else
442# define FLOAT_FUNC(name) NULL
443#endif
444#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
445# define MATH_FUNC(name) name
446#else
447# define MATH_FUNC(name) NULL
448#endif
449#ifdef FEAT_TIMERS
450# define TIMER_FUNC(name) name
451#else
452# define TIMER_FUNC(name) NULL
453#endif
454#ifdef FEAT_JOB_CHANNEL
455# define JOB_FUNC(name) name
456#else
457# define JOB_FUNC(name) NULL
458#endif
459#ifdef FEAT_PROP_POPUP
460# define PROP_FUNC(name) name
461#else
462# define PROP_FUNC(name) NULL
463#endif
464#ifdef FEAT_SIGNS
465# define SIGN_FUNC(name) name
466#else
467# define SIGN_FUNC(name) NULL
468#endif
469#ifdef FEAT_SOUND
470# define SOUND_FUNC(name) name
471#else
472# define SOUND_FUNC(name) NULL
473#endif
474#ifdef FEAT_TERMINAL
475# define TERM_FUNC(name) name
476#else
477# define TERM_FUNC(name) NULL
478#endif
479
Bram Moolenaarac92e252019-08-03 21:58:38 +0200480static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200481{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100482 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
483 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200484 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100485 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200486 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200487 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488 {"argc", 0, 1, 0, ret_number, f_argc},
489 {"argidx", 0, 0, 0, ret_number, f_argidx},
490 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200491 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100492 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100493 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
494 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200495 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100496 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar1d634542020-08-18 13:41:50 +0200497 {"assert_fails", 1, 4, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100498 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
499 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
500 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
501 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
502 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
503 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
504 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100505 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
506 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
507 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100508#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100509 f_balloon_gettext
510#else
511 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100513 },
514 {"balloon_show", 1, 1, FEARG_1, ret_void,
515#ifdef FEAT_BEVAL
516 f_balloon_show
517#else
518 NULL
519#endif
520 },
521 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
522#if defined(FEAT_BEVAL_TERM)
523 f_balloon_split
524#else
525 NULL
526#endif
527 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100528 {"browse", 4, 4, 0, ret_string, f_browse},
529 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
530 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
531 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
532 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
533 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
534 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
535 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
536 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
537 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
538 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
539 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
540 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
541 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
542 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
543 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
544 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
545 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100546 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
547 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
548 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
549 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
550 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
551 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
552 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
553 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
554 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
555 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
556 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
557 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
558 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
559 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
560 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
561 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
562 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
563 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
564 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100565 {"changenr", 0, 0, 0, ret_number, f_changenr},
566 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
567 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
568 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
569 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
570 {"col", 1, 1, FEARG_1, ret_number, f_col},
571 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
572 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
573 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
574 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
575 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200576 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100577 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
578 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100579 {"count", 2, 4, FEARG_1, ret_number, f_count},
580 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
581 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100582 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100583#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100584 f_debugbreak
585#else
586 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200587#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100588 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200589 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100590 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
591 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
592 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
593 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
594 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
595 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
596 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
597 {"environ", 0, 0, 0, ret_dict_string, f_environ},
598 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
599 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
600 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
601 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
602 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
603 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
604 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100605 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100606 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
607 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200608 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100609 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
610 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
611 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
612 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200613 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100614 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
615 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200616 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100617 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
618 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
619 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100620 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
621 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
622 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
623 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
624 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
625 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
626 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
627 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200628 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100629 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
630 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
631 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200632 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100633 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
634 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
635 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
636 {"getchar", 0, 1, 0, ret_number, f_getchar},
637 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
638 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
639 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
640 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
641 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
642 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
643 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
644 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
645 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
646 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
647 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
648 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
649 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
650 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
651 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
652 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
653 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
654 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200655 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200656 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100657 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
658 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
659 {"getpid", 0, 0, 0, ret_number, f_getpid},
660 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200661 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200662 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200663 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100664 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
665 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
666 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
667 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
668 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
669 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
670 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
671 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
672 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
673 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
674 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
675 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
676 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100677 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100678 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
679 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
680 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
681 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
682 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
683 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
684 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
685 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
686 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
687 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
688 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
689 {"hostname", 0, 0, 0, ret_string, f_hostname},
690 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
691 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
692 {"index", 2, 4, FEARG_1, ret_number, f_index},
693 {"input", 1, 3, FEARG_1, ret_string, f_input},
694 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
695 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
696 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
697 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
698 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200699 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100700 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
701 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
702 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100703 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100704 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100705 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100706 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100707 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
708 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
709 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
710 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
711 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
712 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100713 {"join", 1, 2, FEARG_1, ret_string, f_join},
714 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
715 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
716 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
717 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200718 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100719 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
720 {"len", 1, 1, FEARG_1, ret_number, f_len},
721 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
722 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
723 {"line", 1, 2, FEARG_1, ret_number, f_line},
724 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
725 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
726 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
727 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
728 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
729 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
730 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100731 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
732 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
733 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200734#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100735 f_luaeval
736#else
737 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200738#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100739 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100740 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200741 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100742 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200743 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100744 {"match", 2, 4, FEARG_1, ret_any, f_match},
745 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
746 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
747 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
748 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
749 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
750 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
751 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
752 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
753 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100754 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100755#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 f_menu_info
757#else
758 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100759#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100760 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100761 {"min", 1, 1, FEARG_1, ret_any, f_min},
762 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
763 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100764 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766 f_mzeval
767#else
768 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200769#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100770 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100771 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
772 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
773 {"or", 2, 2, FEARG_1, ret_number, f_or},
774 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100775 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100777 f_perleval
778#else
779 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200780#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100781 },
782 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
783 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200784 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100785 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
786 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
787 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
788 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
789 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
790 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
791 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
792 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
793 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
794 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200795 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100796 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
797 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
798 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
799 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
800 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
801 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
802 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
803 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100804 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
805 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100806 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
807 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
808 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
809 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
810 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
811 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
812 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
813 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
814 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
815 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
816 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
817 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
818 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100819 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
820 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100821 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200822#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100823 f_py3eval
824#else
825 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100827 },
828 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100830 f_pyeval
831#else
832 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200833#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100834 },
835 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100836#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100837 f_pyxeval
838#else
839 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100840#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100841 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100842 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
843 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200844 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
845 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100846 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200847 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100848 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
849 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
850 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100851 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100852 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
853 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
854 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
855 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
856 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
857 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200858 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200859 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100860 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200861 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100862 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200863 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100864 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
865 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100866#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100867 f_rubyeval
868#else
869 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100870#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100871 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100872 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
873 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
874 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
875 {"screencol", 0, 0, 0, ret_number, f_screencol},
876 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
877 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
878 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200879 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200880 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100881 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
882 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
883 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200884 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100885 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
886 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
887 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
888 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
889 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
890 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
891 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
892 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
893 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
894 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
895 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
896 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
897 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
898 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
899 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
900 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
901 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
902 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100903 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100905 f_sha256
906#else
907 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100909 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100910 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
911 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100912 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
913 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
914 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
915 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
916 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
917 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
918 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
919 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
920 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200921 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100922 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
923 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200924 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100925 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
926 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
927 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
928 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100929 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
930 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
931 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
932 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100933 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100934 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
935 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100936 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100937 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
938 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
939 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
940 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
941 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100942 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100944 f_strftime
945#else
946 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100948 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100949 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
950 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
951 {"string", 1, 1, FEARG_1, ret_string, f_string},
952 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
953 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100954 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100955#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100956 f_strptime
957#else
958 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100959#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100960 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100961 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
962 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
963 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
964 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
965 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
966 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
967 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
968 {"synID", 3, 3, 0, ret_number, f_synID},
969 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
970 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
971 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
972 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
973 {"system", 1, 2, FEARG_1, ret_string, f_system},
974 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
975 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
976 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
977 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
978 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
979 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100980 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
981 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100982 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100983 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
984 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
985 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
986 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
987 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100988#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100989 f_term_getansicolors
990#else
991 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100993 },
994 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
995 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
996 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
997 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
998 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
999 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1000 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1001 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1002 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1003 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1004 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1005 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1006 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001007#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001008 f_term_setansicolors
1009#else
1010 NULL
1011#endif
1012 },
1013 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1014 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1015 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1016 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1017 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1018 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001019 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001020 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1021 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1022 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1023 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1024 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1025 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1026 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1027 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001028 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001029 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001030 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001031 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001032 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001033 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001034 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1035 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1036 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1037 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001038 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001039#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001040 f_test_scrollbar
1041#else
1042 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001043#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001044 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001045 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1046 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1047 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1048 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001049 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1051 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1052 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1053 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1054 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001055 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1056 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1057 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001058 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001059 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001060 {"type", 1, 1, FEARG_1, ret_number, f_type},
1061 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1062 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1063 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1064 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1065 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1066 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1067 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1068 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1069 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1070 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1071 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1072 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1073 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1074 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1075 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1076 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1077 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1078 {"wincol", 0, 0, 0, ret_number, f_wincol},
1079 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1080 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1081 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1082 {"winline", 0, 0, 0, ret_number, f_winline},
1083 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1084 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1085 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1086 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1087 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1088 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1089 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1090 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001091};
1092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001093/*
1094 * Function given to ExpandGeneric() to obtain the list of internal
1095 * or user defined function names.
1096 */
1097 char_u *
1098get_function_name(expand_T *xp, int idx)
1099{
1100 static int intidx = -1;
1101 char_u *name;
1102
1103 if (idx == 0)
1104 intidx = -1;
1105 if (intidx < 0)
1106 {
1107 name = get_user_func_name(xp, idx);
1108 if (name != NULL)
1109 return name;
1110 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001111 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001112 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001113 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001114 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001115 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116 STRCAT(IObuff, ")");
1117 return IObuff;
1118 }
1119
1120 return NULL;
1121}
1122
1123/*
1124 * Function given to ExpandGeneric() to obtain the list of internal or
1125 * user defined variable or function names.
1126 */
1127 char_u *
1128get_expr_name(expand_T *xp, int idx)
1129{
1130 static int intidx = -1;
1131 char_u *name;
1132
1133 if (idx == 0)
1134 intidx = -1;
1135 if (intidx < 0)
1136 {
1137 name = get_function_name(xp, idx);
1138 if (name != NULL)
1139 return name;
1140 }
1141 return get_user_var_name(xp, ++intidx);
1142}
1143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001144/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001145 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001146 * Return index, or -1 if not found or "implemented" is TRUE and the function
1147 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001149 static int
1150find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001151{
1152 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001153 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154 int cmp;
1155 int x;
1156
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001157 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001158
1159 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001160 while (first <= last)
1161 {
1162 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001163 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164 if (cmp < 0)
1165 last = x - 1;
1166 else if (cmp > 0)
1167 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001168 else if (implemented && global_functions[x].f_func == NULL)
1169 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001170 else
1171 return x;
1172 }
1173 return -1;
1174}
1175
Bram Moolenaar15c47602020-03-26 22:16:48 +01001176/*
1177 * Find internal function "name" in table "global_functions".
1178 * Return index, or -1 if not found or the function is not implemented.
1179 */
1180 int
1181find_internal_func(char_u *name)
1182{
1183 return find_internal_func_opt(name, TRUE);
1184}
1185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001186 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001187has_internal_func(char_u *name)
1188{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001189 return find_internal_func_opt(name, TRUE) >= 0;
1190}
1191
1192 static int
1193has_internal_func_name(char_u *name)
1194{
1195 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001196}
1197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 char *
1199internal_func_name(int idx)
1200{
1201 return global_functions[idx].f_name;
1202}
1203
1204 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001205internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001207 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208}
1209
1210/*
1211 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001212 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1213 * first argument, 2 if method base is second argument, etc. 9 if method base
1214 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 */
1216 int
1217check_internal_func(int idx, int argcount)
1218{
1219 int res;
1220 char *name;
1221
1222 if (argcount < global_functions[idx].f_min_argc)
1223 res = FCERR_TOOFEW;
1224 else if (argcount > global_functions[idx].f_max_argc)
1225 res = FCERR_TOOMANY;
1226 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001227 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229 name = internal_func_name(idx);
1230 if (res == FCERR_TOOMANY)
1231 semsg(_(e_toomanyarg), name);
1232 else
1233 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001234 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235}
1236
Bram Moolenaarac92e252019-08-03 21:58:38 +02001237 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238call_internal_func(
1239 char_u *name,
1240 int argcount,
1241 typval_T *argvars,
1242 typval_T *rettv)
1243{
1244 int i;
1245
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001246 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001248 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001249 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001250 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001251 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001252 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001254 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001255 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001256}
1257
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 void
1259call_internal_func_by_idx(
1260 int idx,
1261 typval_T *argvars,
1262 typval_T *rettv)
1263{
1264 global_functions[idx].f_func(argvars, rettv);
1265}
1266
Bram Moolenaarac92e252019-08-03 21:58:38 +02001267/*
1268 * Invoke a method for base->method().
1269 */
1270 int
1271call_internal_method(
1272 char_u *name,
1273 int argcount,
1274 typval_T *argvars,
1275 typval_T *rettv,
1276 typval_T *basetv)
1277{
1278 int i;
1279 int fi;
1280 typval_T argv[MAX_FUNC_ARGS + 1];
1281
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001282 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001283 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001284 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001285 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001286 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001287 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001288 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001289 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001290 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001291
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001292 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001293 {
1294 // base value goes last
1295 for (i = 0; i < argcount; ++i)
1296 argv[i] = argvars[i];
1297 argv[argcount] = *basetv;
1298 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001299 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001300 {
1301 // base value goes second
1302 argv[0] = argvars[0];
1303 argv[1] = *basetv;
1304 for (i = 1; i < argcount; ++i)
1305 argv[i + 1] = argvars[i];
1306 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001307 else if (global_functions[fi].f_argtype == FEARG_3)
1308 {
1309 // base value goes third
1310 argv[0] = argvars[0];
1311 argv[1] = argvars[1];
1312 argv[2] = *basetv;
1313 for (i = 2; i < argcount; ++i)
1314 argv[i + 1] = argvars[i];
1315 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001316 else if (global_functions[fi].f_argtype == FEARG_4)
1317 {
1318 // base value goes fourth
1319 argv[0] = argvars[0];
1320 argv[1] = argvars[1];
1321 argv[2] = argvars[2];
1322 argv[3] = *basetv;
1323 for (i = 3; i < argcount; ++i)
1324 argv[i + 1] = argvars[i];
1325 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001326 else
1327 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001328 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001329 argv[0] = *basetv;
1330 for (i = 0; i < argcount; ++i)
1331 argv[i + 1] = argvars[i];
1332 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001333 argv[argcount + 1].v_type = VAR_UNKNOWN;
1334
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001335 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001336 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337}
1338
1339/*
1340 * Return TRUE for a non-zero Number and a non-empty String.
1341 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001342 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343non_zero_arg(typval_T *argvars)
1344{
1345 return ((argvars[0].v_type == VAR_NUMBER
1346 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001347 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348 && argvars[0].vval.v_number == VVAL_TRUE)
1349 || (argvars[0].v_type == VAR_STRING
1350 && argvars[0].vval.v_string != NULL
1351 && *argvars[0].vval.v_string != NUL));
1352}
1353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355/*
1356 * Get the float value of "argvars[0]" into "f".
1357 * Returns FAIL when the argument is not a Number or Float.
1358 */
1359 static int
1360get_float_arg(typval_T *argvars, float_T *f)
1361{
1362 if (argvars[0].v_type == VAR_FLOAT)
1363 {
1364 *f = argvars[0].vval.v_float;
1365 return OK;
1366 }
1367 if (argvars[0].v_type == VAR_NUMBER)
1368 {
1369 *f = (float_T)argvars[0].vval.v_number;
1370 return OK;
1371 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001372 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 return FAIL;
1374}
1375
1376/*
1377 * "abs(expr)" function
1378 */
1379 static void
1380f_abs(typval_T *argvars, typval_T *rettv)
1381{
1382 if (argvars[0].v_type == VAR_FLOAT)
1383 {
1384 rettv->v_type = VAR_FLOAT;
1385 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1386 }
1387 else
1388 {
1389 varnumber_T n;
1390 int error = FALSE;
1391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001393 if (error)
1394 rettv->vval.v_number = -1;
1395 else if (n > 0)
1396 rettv->vval.v_number = n;
1397 else
1398 rettv->vval.v_number = -n;
1399 }
1400}
1401
1402/*
1403 * "acos()" function
1404 */
1405 static void
1406f_acos(typval_T *argvars, typval_T *rettv)
1407{
1408 float_T f = 0.0;
1409
1410 rettv->v_type = VAR_FLOAT;
1411 if (get_float_arg(argvars, &f) == OK)
1412 rettv->vval.v_float = acos(f);
1413 else
1414 rettv->vval.v_float = 0.0;
1415}
1416#endif
1417
1418/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419 * "and(expr, expr)" function
1420 */
1421 static void
1422f_and(typval_T *argvars, typval_T *rettv)
1423{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1425 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001426}
1427
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001428#ifdef FEAT_FLOAT
1429/*
1430 * "asin()" function
1431 */
1432 static void
1433f_asin(typval_T *argvars, typval_T *rettv)
1434{
1435 float_T f = 0.0;
1436
1437 rettv->v_type = VAR_FLOAT;
1438 if (get_float_arg(argvars, &f) == OK)
1439 rettv->vval.v_float = asin(f);
1440 else
1441 rettv->vval.v_float = 0.0;
1442}
1443
1444/*
1445 * "atan()" function
1446 */
1447 static void
1448f_atan(typval_T *argvars, typval_T *rettv)
1449{
1450 float_T f = 0.0;
1451
1452 rettv->v_type = VAR_FLOAT;
1453 if (get_float_arg(argvars, &f) == OK)
1454 rettv->vval.v_float = atan(f);
1455 else
1456 rettv->vval.v_float = 0.0;
1457}
1458
1459/*
1460 * "atan2()" function
1461 */
1462 static void
1463f_atan2(typval_T *argvars, typval_T *rettv)
1464{
1465 float_T fx = 0.0, fy = 0.0;
1466
1467 rettv->v_type = VAR_FLOAT;
1468 if (get_float_arg(argvars, &fx) == OK
1469 && get_float_arg(&argvars[1], &fy) == OK)
1470 rettv->vval.v_float = atan2(fx, fy);
1471 else
1472 rettv->vval.v_float = 0.0;
1473}
1474#endif
1475
1476/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001477 * "balloon_show()" function
1478 */
1479#ifdef FEAT_BEVAL
1480 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001481f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1482{
1483 rettv->v_type = VAR_STRING;
1484 if (balloonEval != NULL)
1485 {
1486 if (balloonEval->msg == NULL)
1487 rettv->vval.v_string = NULL;
1488 else
1489 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1490 }
1491}
1492
1493 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001494f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1495{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001496 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001497 {
1498 if (argvars[0].v_type == VAR_LIST
1499# ifdef FEAT_GUI
1500 && !gui.in_use
1501# endif
1502 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001503 {
1504 list_T *l = argvars[0].vval.v_list;
1505
1506 // empty list removes the balloon
1507 post_balloon(balloonEval, NULL,
1508 l == NULL || l->lv_len == 0 ? NULL : l);
1509 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001510 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001511 {
1512 char_u *mesg = tv_get_string_chk(&argvars[0]);
1513
1514 if (mesg != NULL)
1515 // empty string removes the balloon
1516 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1517 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001518 }
1519}
1520
Bram Moolenaar669a8282017-11-19 20:13:05 +01001521# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001522 static void
1523f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1524{
1525 if (rettv_list_alloc(rettv) == OK)
1526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001527 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001528
1529 if (msg != NULL)
1530 {
1531 pumitem_T *array;
1532 int size = split_message(msg, &array);
1533 int i;
1534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001535 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001536 for (i = 1; i < size - 1; ++i)
1537 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001538 while (size > 0)
1539 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001540 vim_free(array);
1541 }
1542 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001543}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001544# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001545#endif
1546
1547/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001548 * Get the buffer from "arg" and give an error and return NULL if it is not
1549 * valid.
1550 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001551 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001552get_buf_arg(typval_T *arg)
1553{
1554 buf_T *buf;
1555
1556 ++emsg_off;
1557 buf = tv_get_buf(arg, FALSE);
1558 --emsg_off;
1559 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001560 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001561 return buf;
1562}
1563
1564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565 * "byte2line(byte)" function
1566 */
1567 static void
1568f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1569{
1570#ifndef FEAT_BYTEOFF
1571 rettv->vval.v_number = -1;
1572#else
1573 long boff = 0;
1574
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001575 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001576 if (boff < 0)
1577 rettv->vval.v_number = -1;
1578 else
1579 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1580 (linenr_T)0, &boff);
1581#endif
1582}
1583
1584 static void
1585byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1586{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588 char_u *str;
1589 varnumber_T idx;
1590
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 str = tv_get_string_chk(&argvars[0]);
1592 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 rettv->vval.v_number = -1;
1594 if (str == NULL || idx < 0)
1595 return;
1596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 t = str;
1598 for ( ; idx > 0; idx--)
1599 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001600 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 return;
1602 if (enc_utf8 && comp)
1603 t += utf_ptr2len(t);
1604 else
1605 t += (*mb_ptr2len)(t);
1606 }
1607 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608}
1609
1610/*
1611 * "byteidx()" function
1612 */
1613 static void
1614f_byteidx(typval_T *argvars, typval_T *rettv)
1615{
1616 byteidx(argvars, rettv, FALSE);
1617}
1618
1619/*
1620 * "byteidxcomp()" function
1621 */
1622 static void
1623f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1624{
1625 byteidx(argvars, rettv, TRUE);
1626}
1627
1628/*
1629 * "call(func, arglist [, dict])" function
1630 */
1631 static void
1632f_call(typval_T *argvars, typval_T *rettv)
1633{
1634 char_u *func;
1635 partial_T *partial = NULL;
1636 dict_T *selfdict = NULL;
1637
1638 if (argvars[1].v_type != VAR_LIST)
1639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001640 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 return;
1642 }
1643 if (argvars[1].vval.v_list == NULL)
1644 return;
1645
1646 if (argvars[0].v_type == VAR_FUNC)
1647 func = argvars[0].vval.v_string;
1648 else if (argvars[0].v_type == VAR_PARTIAL)
1649 {
1650 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001651 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652 }
1653 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001654 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001656 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657
1658 if (argvars[2].v_type != VAR_UNKNOWN)
1659 {
1660 if (argvars[2].v_type != VAR_DICT)
1661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001662 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663 return;
1664 }
1665 selfdict = argvars[2].vval.v_dict;
1666 }
1667
1668 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1669}
1670
1671#ifdef FEAT_FLOAT
1672/*
1673 * "ceil({float})" function
1674 */
1675 static void
1676f_ceil(typval_T *argvars, typval_T *rettv)
1677{
1678 float_T f = 0.0;
1679
1680 rettv->v_type = VAR_FLOAT;
1681 if (get_float_arg(argvars, &f) == OK)
1682 rettv->vval.v_float = ceil(f);
1683 else
1684 rettv->vval.v_float = 0.0;
1685}
1686#endif
1687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688/*
1689 * "changenr()" function
1690 */
1691 static void
1692f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1693{
1694 rettv->vval.v_number = curbuf->b_u_seq_cur;
1695}
1696
1697/*
1698 * "char2nr(string)" function
1699 */
1700 static void
1701f_char2nr(typval_T *argvars, typval_T *rettv)
1702{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703 if (has_mbyte)
1704 {
1705 int utf8 = 0;
1706
1707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001708 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001709
1710 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001711 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001713 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 }
1715 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001716 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717}
1718
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001719 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001720get_optional_window(typval_T *argvars, int idx)
1721{
1722 win_T *win = curwin;
1723
1724 if (argvars[idx].v_type != VAR_UNKNOWN)
1725 {
1726 win = find_win_by_nr_or_id(&argvars[idx]);
1727 if (win == NULL)
1728 {
1729 emsg(_(e_invalwindow));
1730 return NULL;
1731 }
1732 }
1733 return win;
1734}
1735
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737 * "col(string)" function
1738 */
1739 static void
1740f_col(typval_T *argvars, typval_T *rettv)
1741{
1742 colnr_T col = 0;
1743 pos_T *fp;
1744 int fnum = curbuf->b_fnum;
1745
1746 fp = var2fpos(&argvars[0], FALSE, &fnum);
1747 if (fp != NULL && fnum == curbuf->b_fnum)
1748 {
1749 if (fp->col == MAXCOL)
1750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001751 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001752 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1753 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1754 else
1755 col = MAXCOL;
1756 }
1757 else
1758 {
1759 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001760 // col(".") when the cursor is on the NUL at the end of the line
1761 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001762 if (virtual_active() && fp == &curwin->w_cursor)
1763 {
1764 char_u *p = ml_get_cursor();
1765
1766 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1767 curwin->w_virtcol - curwin->w_cursor.coladd))
1768 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001769 int l;
1770
1771 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1772 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001773 }
1774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001775 }
1776 }
1777 rettv->vval.v_number = col;
1778}
1779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001780/*
1781 * "confirm(message, buttons[, default [, type]])" function
1782 */
1783 static void
1784f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1785{
1786#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1787 char_u *message;
1788 char_u *buttons = NULL;
1789 char_u buf[NUMBUFLEN];
1790 char_u buf2[NUMBUFLEN];
1791 int def = 1;
1792 int type = VIM_GENERIC;
1793 char_u *typestr;
1794 int error = FALSE;
1795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001796 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001797 if (message == NULL)
1798 error = TRUE;
1799 if (argvars[1].v_type != VAR_UNKNOWN)
1800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001801 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001802 if (buttons == NULL)
1803 error = TRUE;
1804 if (argvars[2].v_type != VAR_UNKNOWN)
1805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001806 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001807 if (argvars[3].v_type != VAR_UNKNOWN)
1808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001809 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001810 if (typestr == NULL)
1811 error = TRUE;
1812 else
1813 {
1814 switch (TOUPPER_ASC(*typestr))
1815 {
1816 case 'E': type = VIM_ERROR; break;
1817 case 'Q': type = VIM_QUESTION; break;
1818 case 'I': type = VIM_INFO; break;
1819 case 'W': type = VIM_WARNING; break;
1820 case 'G': type = VIM_GENERIC; break;
1821 }
1822 }
1823 }
1824 }
1825 }
1826
1827 if (buttons == NULL || *buttons == NUL)
1828 buttons = (char_u *)_("&Ok");
1829
1830 if (!error)
1831 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1832 def, NULL, FALSE);
1833#endif
1834}
1835
1836/*
1837 * "copy()" function
1838 */
1839 static void
1840f_copy(typval_T *argvars, typval_T *rettv)
1841{
1842 item_copy(&argvars[0], rettv, FALSE, 0);
1843}
1844
1845#ifdef FEAT_FLOAT
1846/*
1847 * "cos()" function
1848 */
1849 static void
1850f_cos(typval_T *argvars, typval_T *rettv)
1851{
1852 float_T f = 0.0;
1853
1854 rettv->v_type = VAR_FLOAT;
1855 if (get_float_arg(argvars, &f) == OK)
1856 rettv->vval.v_float = cos(f);
1857 else
1858 rettv->vval.v_float = 0.0;
1859}
1860
1861/*
1862 * "cosh()" function
1863 */
1864 static void
1865f_cosh(typval_T *argvars, typval_T *rettv)
1866{
1867 float_T f = 0.0;
1868
1869 rettv->v_type = VAR_FLOAT;
1870 if (get_float_arg(argvars, &f) == OK)
1871 rettv->vval.v_float = cosh(f);
1872 else
1873 rettv->vval.v_float = 0.0;
1874}
1875#endif
1876
1877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 * "cursor(lnum, col)" function, or
1879 * "cursor(list)"
1880 *
1881 * Moves the cursor to the specified line and column.
1882 * Returns 0 when the position could be set, -1 otherwise.
1883 */
1884 static void
1885f_cursor(typval_T *argvars, typval_T *rettv)
1886{
1887 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 int set_curswant = TRUE;
1890
1891 rettv->vval.v_number = -1;
1892 if (argvars[1].v_type == VAR_UNKNOWN)
1893 {
1894 pos_T pos;
1895 colnr_T curswant = -1;
1896
1897 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 return;
1901 }
1902 line = pos.lnum;
1903 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (curswant >= 0)
1906 {
1907 curwin->w_curswant = curswant - 1;
1908 set_curswant = FALSE;
1909 }
1910 }
1911 else
1912 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001913 line = tv_get_lnum(argvars);
1914 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001918 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920 if (line > 0)
1921 curwin->w_cursor.lnum = line;
1922 if (col > 0)
1923 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001928 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 if (has_mbyte)
1930 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931
1932 curwin->w_set_curswant = set_curswant;
1933 rettv->vval.v_number = 0;
1934}
1935
Bram Moolenaar4f974752019-02-17 17:44:42 +01001936#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001937/*
1938 * "debugbreak()" function
1939 */
1940 static void
1941f_debugbreak(typval_T *argvars, typval_T *rettv)
1942{
1943 int pid;
1944
1945 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001946 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001947 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001948 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001949 else
1950 {
1951 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1952
1953 if (hProcess != NULL)
1954 {
1955 DebugBreakProcess(hProcess);
1956 CloseHandle(hProcess);
1957 rettv->vval.v_number = OK;
1958 }
1959 }
1960}
1961#endif
1962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001963/*
1964 * "deepcopy()" function
1965 */
1966 static void
1967f_deepcopy(typval_T *argvars, typval_T *rettv)
1968{
1969 int noref = 0;
1970 int copyID;
1971
1972 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001976 else
1977 {
1978 copyID = get_copyID();
1979 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1980 }
1981}
1982
1983/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001984 * "did_filetype()" function
1985 */
1986 static void
1987f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1988{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001989 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001990}
1991
1992/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001993 * "echoraw({expr})" function
1994 */
1995 static void
1996f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1997{
1998 char_u *str = tv_get_string_chk(&argvars[0]);
1999
2000 if (str != NULL && *str != NUL)
2001 {
2002 out_str(str);
2003 out_flush();
2004 }
2005}
2006
2007/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002008 * "empty({expr})" function
2009 */
2010 static void
2011f_empty(typval_T *argvars, typval_T *rettv)
2012{
2013 int n = FALSE;
2014
2015 switch (argvars[0].v_type)
2016 {
2017 case VAR_STRING:
2018 case VAR_FUNC:
2019 n = argvars[0].vval.v_string == NULL
2020 || *argvars[0].vval.v_string == NUL;
2021 break;
2022 case VAR_PARTIAL:
2023 n = FALSE;
2024 break;
2025 case VAR_NUMBER:
2026 n = argvars[0].vval.v_number == 0;
2027 break;
2028 case VAR_FLOAT:
2029#ifdef FEAT_FLOAT
2030 n = argvars[0].vval.v_float == 0.0;
2031 break;
2032#endif
2033 case VAR_LIST:
2034 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002035 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002036 break;
2037 case VAR_DICT:
2038 n = argvars[0].vval.v_dict == NULL
2039 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2040 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002041 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 case VAR_SPECIAL:
2043 n = argvars[0].vval.v_number != VVAL_TRUE;
2044 break;
2045
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002046 case VAR_BLOB:
2047 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002048 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2049 break;
2050
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 case VAR_JOB:
2052#ifdef FEAT_JOB_CHANNEL
2053 n = argvars[0].vval.v_job == NULL
2054 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2055 break;
2056#endif
2057 case VAR_CHANNEL:
2058#ifdef FEAT_JOB_CHANNEL
2059 n = argvars[0].vval.v_channel == NULL
2060 || !channel_is_open(argvars[0].vval.v_channel);
2061 break;
2062#endif
2063 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002064 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002066 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 n = TRUE;
2068 break;
2069 }
2070
2071 rettv->vval.v_number = n;
2072}
2073
2074/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002075 * "environ()" function
2076 */
2077 static void
2078f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2079{
2080#if !defined(AMIGA)
2081 int i = 0;
2082 char_u *entry, *value;
2083# ifdef MSWIN
2084 extern wchar_t **_wenviron;
2085# else
2086 extern char **environ;
2087# endif
2088
2089 if (rettv_dict_alloc(rettv) != OK)
2090 return;
2091
2092# ifdef MSWIN
2093 if (*_wenviron == NULL)
2094 return;
2095# else
2096 if (*environ == NULL)
2097 return;
2098# endif
2099
2100 for (i = 0; ; ++i)
2101 {
2102# ifdef MSWIN
2103 short_u *p;
2104
2105 if ((p = (short_u *)_wenviron[i]) == NULL)
2106 return;
2107 entry = utf16_to_enc(p, NULL);
2108# else
2109 if ((entry = (char_u *)environ[i]) == NULL)
2110 return;
2111 entry = vim_strsave(entry);
2112# endif
2113 if (entry == NULL) // out of memory
2114 return;
2115 if ((value = vim_strchr(entry, '=')) == NULL)
2116 {
2117 vim_free(entry);
2118 continue;
2119 }
2120 *value++ = NUL;
2121 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2122 vim_free(entry);
2123 }
2124#endif
2125}
2126
2127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 * "escape({string}, {chars})" function
2129 */
2130 static void
2131f_escape(typval_T *argvars, typval_T *rettv)
2132{
2133 char_u buf[NUMBUFLEN];
2134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002135 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2136 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002137 rettv->v_type = VAR_STRING;
2138}
2139
2140/*
2141 * "eval()" function
2142 */
2143 static void
2144f_eval(typval_T *argvars, typval_T *rettv)
2145{
2146 char_u *s, *p;
2147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 if (s != NULL)
2150 s = skipwhite(s);
2151
2152 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002153 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 {
2155 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 need_clr_eos = FALSE;
2158 rettv->v_type = VAR_NUMBER;
2159 rettv->vval.v_number = 0;
2160 }
2161 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002162 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163}
2164
2165/*
2166 * "eventhandler()" function
2167 */
2168 static void
2169f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2170{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002171 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172}
2173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174static garray_T redir_execute_ga;
2175
2176/*
2177 * Append "value[value_len]" to the execute() output.
2178 */
2179 void
2180execute_redir_str(char_u *value, int value_len)
2181{
2182 int len;
2183
2184 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002185 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002187 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188 if (ga_grow(&redir_execute_ga, len) == OK)
2189 {
2190 mch_memmove((char *)redir_execute_ga.ga_data
2191 + redir_execute_ga.ga_len, value, len);
2192 redir_execute_ga.ga_len += len;
2193 }
2194}
2195
2196/*
2197 * Get next line from a list.
2198 * Called by do_cmdline() to get the next line.
2199 * Returns allocated string, or NULL for end of function.
2200 */
2201
2202 static char_u *
2203get_list_line(
2204 int c UNUSED,
2205 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002206 int indent UNUSED,
2207 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208{
2209 listitem_T **p = (listitem_T **)cookie;
2210 listitem_T *item = *p;
2211 char_u buf[NUMBUFLEN];
2212 char_u *s;
2213
2214 if (item == NULL)
2215 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002216 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 *p = item->li_next;
2218 return s == NULL ? NULL : vim_strsave(s);
2219}
2220
2221/*
2222 * "execute()" function
2223 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002224 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002225execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226{
2227 char_u *cmd = NULL;
2228 list_T *list = NULL;
2229 int save_msg_silent = msg_silent;
2230 int save_emsg_silent = emsg_silent;
2231 int save_emsg_noredir = emsg_noredir;
2232 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002233 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002234 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002235 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002236 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237
2238 rettv->vval.v_string = NULL;
2239 rettv->v_type = VAR_STRING;
2240
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002241 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002242 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002243 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002244 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 return;
2247 ++list->lv_refcount;
2248 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002249 else if (argvars[arg_off].v_type == VAR_JOB
2250 || argvars[arg_off].v_type == VAR_CHANNEL)
2251 {
2252 emsg(_(e_inval_string));
2253 return;
2254 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002255 else
2256 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002257 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 if (cmd == NULL)
2259 return;
2260 }
2261
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002262 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002263 {
2264 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002265 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266
2267 if (s == NULL)
2268 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002269 if (*s == NUL)
2270 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 if (STRNCMP(s, "silent", 6) == 0)
2272 ++msg_silent;
2273 if (STRCMP(s, "silent!") == 0)
2274 {
2275 emsg_silent = TRUE;
2276 emsg_noredir = TRUE;
2277 }
2278 }
2279 else
2280 ++msg_silent;
2281
2282 if (redir_execute)
2283 save_ga = redir_execute_ga;
2284 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2285 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002286 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002287 if (!echo_output)
2288 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289
2290 if (cmd != NULL)
2291 do_cmdline_cmd(cmd);
2292 else
2293 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002294 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002296 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002297 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298 do_cmdline(NULL, get_list_line, (void *)&item,
2299 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2300 --list->lv_refcount;
2301 }
2302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002303 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002304 if (ga_grow(&redir_execute_ga, 1) == OK)
2305 {
2306 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2307 rettv->vval.v_string = redir_execute_ga.ga_data;
2308 }
2309 else
2310 {
2311 ga_clear(&redir_execute_ga);
2312 rettv->vval.v_string = NULL;
2313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314 msg_silent = save_msg_silent;
2315 emsg_silent = save_emsg_silent;
2316 emsg_noredir = save_emsg_noredir;
2317
2318 redir_execute = save_redir_execute;
2319 if (redir_execute)
2320 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002321 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002322
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002323 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002324 if (echo_output)
2325 // When not working silently: put it in column zero. A following
2326 // "echon" will overwrite the message, unavoidably.
2327 msg_col = 0;
2328 else
2329 // When working silently: Put it back where it was, since nothing
2330 // should have been written.
2331 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002332}
2333
2334/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002335 * "execute()" function
2336 */
2337 static void
2338f_execute(typval_T *argvars, typval_T *rettv)
2339{
2340 execute_common(argvars, rettv, 0);
2341}
2342
2343/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002344 * "exists()" function
2345 */
2346 static void
2347f_exists(typval_T *argvars, typval_T *rettv)
2348{
2349 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002352 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002355 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 if (mch_getenv(p + 1) != NULL)
2357 n = TRUE;
2358 else
2359 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002360 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 p = expand_env_save(p);
2362 if (p != NULL && *p != '$')
2363 n = TRUE;
2364 vim_free(p);
2365 }
2366 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002367 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002368 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002369 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002371 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002373 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002375 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002377 else if (*p == '?') // internal function only
2378 {
2379 n = has_internal_func_name(p + 1);
2380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 else if (*p == ':')
2382 {
2383 n = cmd_exists(p + 1);
2384 }
2385 else if (*p == '#')
2386 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 if (p[1] == '#')
2388 n = autocmd_supported(p + 2);
2389 else
2390 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002391 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002392 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002394 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 }
2396
2397 rettv->vval.v_number = n;
2398}
2399
2400#ifdef FEAT_FLOAT
2401/*
2402 * "exp()" function
2403 */
2404 static void
2405f_exp(typval_T *argvars, typval_T *rettv)
2406{
2407 float_T f = 0.0;
2408
2409 rettv->v_type = VAR_FLOAT;
2410 if (get_float_arg(argvars, &f) == OK)
2411 rettv->vval.v_float = exp(f);
2412 else
2413 rettv->vval.v_float = 0.0;
2414}
2415#endif
2416
2417/*
2418 * "expand()" function
2419 */
2420 static void
2421f_expand(typval_T *argvars, typval_T *rettv)
2422{
2423 char_u *s;
2424 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002425 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2427 expand_T xpc;
2428 int error = FALSE;
2429 char_u *result;
2430
2431 rettv->v_type = VAR_STRING;
2432 if (argvars[1].v_type != VAR_UNKNOWN
2433 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002434 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002436 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002438 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 if (*s == '%' || *s == '#' || *s == '<')
2440 {
2441 ++emsg_off;
2442 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2443 --emsg_off;
2444 if (rettv->v_type == VAR_LIST)
2445 {
2446 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2447 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002448 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002449 }
2450 else
2451 rettv->vval.v_string = result;
2452 }
2453 else
2454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002455 // When the optional second argument is non-zero, don't remove matches
2456 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459 options |= WILD_KEEP_ALL;
2460 if (!error)
2461 {
2462 ExpandInit(&xpc);
2463 xpc.xp_context = EXPAND_FILES;
2464 if (p_wic)
2465 options += WILD_ICASE;
2466 if (rettv->v_type == VAR_STRING)
2467 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2468 options, WILD_ALL);
2469 else if (rettv_list_alloc(rettv) != FAIL)
2470 {
2471 int i;
2472
2473 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2474 for (i = 0; i < xpc.xp_numfiles; i++)
2475 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2476 ExpandCleanup(&xpc);
2477 }
2478 }
2479 else
2480 rettv->vval.v_string = NULL;
2481 }
2482}
2483
2484/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002485 * "expandcmd()" function
2486 * Expand all the special characters in a command string.
2487 */
2488 static void
2489f_expandcmd(typval_T *argvars, typval_T *rettv)
2490{
2491 exarg_T eap;
2492 char_u *cmdstr;
2493 char *errormsg = NULL;
2494
2495 rettv->v_type = VAR_STRING;
2496 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2497
2498 memset(&eap, 0, sizeof(eap));
2499 eap.cmd = cmdstr;
2500 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002501 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002502 eap.usefilter = FALSE;
2503 eap.nextcmd = NULL;
2504 eap.cmdidx = CMD_USER;
2505
2506 expand_filename(&eap, &cmdstr, &errormsg);
2507 if (errormsg != NULL && *errormsg != NUL)
2508 emsg(errormsg);
2509
2510 rettv->vval.v_string = cmdstr;
2511}
2512
2513/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002514 * "feedkeys()" function
2515 */
2516 static void
2517f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2518{
2519 int remap = TRUE;
2520 int insert = FALSE;
2521 char_u *keys, *flags;
2522 char_u nbuf[NUMBUFLEN];
2523 int typed = FALSE;
2524 int execute = FALSE;
2525 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002526 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527 char_u *keys_esc;
2528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002529 // This is not allowed in the sandbox. If the commands would still be
2530 // executed in the sandbox it would be OK, but it probably happens later,
2531 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 if (check_secure())
2533 return;
2534
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002535 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536
2537 if (argvars[1].v_type != VAR_UNKNOWN)
2538 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002539 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540 for ( ; *flags != NUL; ++flags)
2541 {
2542 switch (*flags)
2543 {
2544 case 'n': remap = FALSE; break;
2545 case 'm': remap = TRUE; break;
2546 case 't': typed = TRUE; break;
2547 case 'i': insert = TRUE; break;
2548 case 'x': execute = TRUE; break;
2549 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002550 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 }
2552 }
2553 }
2554
2555 if (*keys != NUL || execute)
2556 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002557 // Need to escape K_SPECIAL and CSI before putting the string in the
2558 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002559 keys_esc = vim_strsave_escape_csi(keys);
2560 if (keys_esc != NULL)
2561 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002562 if (lowlevel)
2563 {
2564#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002565 int idx;
2566 int len = (int)STRLEN(keys);
2567
2568 for (idx = 0; idx < len; ++idx)
2569 {
2570 // if a CTRL-C was typed, set got_int, similar to what
2571 // happens in fill_input_buf()
2572 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2573 got_int = TRUE;
2574 add_to_input_buf(keys + idx, 1);
2575 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002576#else
2577 emsg(_("E980: lowlevel input not supported"));
2578#endif
2579 }
2580 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002581 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002582 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002584 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002585#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002586 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002587#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002588 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002589 typebuf_was_filled = TRUE;
2590 }
2591 vim_free(keys_esc);
2592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593 if (execute)
2594 {
2595 int save_msg_scroll = msg_scroll;
2596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002597 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 msg_scroll = FALSE;
2599
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002600 if (!dangerous)
2601 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002602 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002603 if (!dangerous)
2604 --ex_normal_busy;
2605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002606 msg_scroll |= save_msg_scroll;
2607 }
2608 }
2609 }
2610}
2611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612#ifdef FEAT_FLOAT
2613/*
2614 * "float2nr({float})" function
2615 */
2616 static void
2617f_float2nr(typval_T *argvars, typval_T *rettv)
2618{
2619 float_T f = 0.0;
2620
2621 if (get_float_arg(argvars, &f) == OK)
2622 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002623 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002624 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002625 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002626 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 else
2628 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 }
2630}
2631
2632/*
2633 * "floor({float})" function
2634 */
2635 static void
2636f_floor(typval_T *argvars, typval_T *rettv)
2637{
2638 float_T f = 0.0;
2639
2640 rettv->v_type = VAR_FLOAT;
2641 if (get_float_arg(argvars, &f) == OK)
2642 rettv->vval.v_float = floor(f);
2643 else
2644 rettv->vval.v_float = 0.0;
2645}
2646
2647/*
2648 * "fmod()" function
2649 */
2650 static void
2651f_fmod(typval_T *argvars, typval_T *rettv)
2652{
2653 float_T fx = 0.0, fy = 0.0;
2654
2655 rettv->v_type = VAR_FLOAT;
2656 if (get_float_arg(argvars, &fx) == OK
2657 && get_float_arg(&argvars[1], &fy) == OK)
2658 rettv->vval.v_float = fmod(fx, fy);
2659 else
2660 rettv->vval.v_float = 0.0;
2661}
2662#endif
2663
2664/*
2665 * "fnameescape({string})" function
2666 */
2667 static void
2668f_fnameescape(typval_T *argvars, typval_T *rettv)
2669{
2670 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002671 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 rettv->v_type = VAR_STRING;
2673}
2674
2675/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676 * "foreground()" function
2677 */
2678 static void
2679f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2680{
2681#ifdef FEAT_GUI
2682 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002685 return;
2686 }
2687#endif
2688#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002689 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690#endif
2691}
2692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002694common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695{
2696 char_u *s;
2697 char_u *name;
2698 int use_string = FALSE;
2699 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002700 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002701 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702
2703 if (argvars[0].v_type == VAR_FUNC)
2704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002705 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 s = argvars[0].vval.v_string;
2707 }
2708 else if (argvars[0].v_type == VAR_PARTIAL
2709 && argvars[0].vval.v_partial != NULL)
2710 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002711 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002713 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 }
2715 else
2716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002717 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002718 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 use_string = TRUE;
2720 }
2721
Bram Moolenaar843b8842016-08-21 14:36:15 +02002722 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002723 {
2724 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002725 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002726 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2727 if (*name != NUL)
2728 s = NULL;
2729 }
2730
Bram Moolenaar843b8842016-08-21 14:36:15 +02002731 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2732 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002733 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002734 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002735 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002736 ? find_func(trans_name, is_global, NULL) == NULL
2737 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002738 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 else
2740 {
2741 int dict_idx = 0;
2742 int arg_idx = 0;
2743 list_T *list = NULL;
2744
2745 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2746 {
2747 char sid_buf[25];
2748 int off = *s == 's' ? 2 : 5;
2749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002750 // Expand s: and <SID> into <SNR>nr_, so that the function can
2751 // also be called from another script. Using trans_function_name()
2752 // would also work, but some plugins depend on the name being
2753 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002754 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002755 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 if (name != NULL)
2757 {
2758 STRCPY(name, sid_buf);
2759 STRCAT(name, s + off);
2760 }
2761 }
2762 else
2763 name = vim_strsave(s);
2764
2765 if (argvars[1].v_type != VAR_UNKNOWN)
2766 {
2767 if (argvars[2].v_type != VAR_UNKNOWN)
2768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002769 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 arg_idx = 1;
2771 dict_idx = 2;
2772 }
2773 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002774 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 dict_idx = 1;
2776 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 arg_idx = 1;
2779 if (dict_idx > 0)
2780 {
2781 if (argvars[dict_idx].v_type != VAR_DICT)
2782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002785 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 }
2787 if (argvars[dict_idx].vval.v_dict == NULL)
2788 dict_idx = 0;
2789 }
2790 if (arg_idx > 0)
2791 {
2792 if (argvars[arg_idx].v_type != VAR_LIST)
2793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002794 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002796 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 }
2798 list = argvars[arg_idx].vval.v_list;
2799 if (list == NULL || list->lv_len == 0)
2800 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002801 else if (list->lv_len > MAX_FUNC_ARGS)
2802 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002803 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002804 vim_free(name);
2805 goto theend;
2806 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 }
2808 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002809 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002811 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002813 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 if (pt == NULL)
2815 vim_free(name);
2816 else
2817 {
2818 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2819 {
2820 listitem_T *li;
2821 int i = 0;
2822 int arg_len = 0;
2823 int lv_len = 0;
2824
2825 if (arg_pt != NULL)
2826 arg_len = arg_pt->pt_argc;
2827 if (list != NULL)
2828 lv_len = list->lv_len;
2829 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002830 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 if (pt->pt_argv == NULL)
2832 {
2833 vim_free(pt);
2834 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002835 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002837 for (i = 0; i < arg_len; i++)
2838 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2839 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002840 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002841 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002842 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002843 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002844 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002845 }
2846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002847 // For "function(dict.func, [], dict)" and "func" is a partial
2848 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002849 if (dict_idx > 0)
2850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2853 ++pt->pt_dict->dv_refcount;
2854 }
2855 else if (arg_pt != NULL)
2856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002857 // If the dict was bound automatically the result is also
2858 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002859 pt->pt_dict = arg_pt->pt_dict;
2860 pt->pt_auto = arg_pt->pt_auto;
2861 if (pt->pt_dict != NULL)
2862 ++pt->pt_dict->dv_refcount;
2863 }
2864
2865 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002866 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2867 {
2868 pt->pt_func = arg_pt->pt_func;
2869 func_ptr_ref(pt->pt_func);
2870 vim_free(name);
2871 }
2872 else if (is_funcref)
2873 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002874 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875 func_ptr_ref(pt->pt_func);
2876 vim_free(name);
2877 }
2878 else
2879 {
2880 pt->pt_name = name;
2881 func_ref(name);
2882 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002883 }
2884 rettv->v_type = VAR_PARTIAL;
2885 rettv->vval.v_partial = pt;
2886 }
2887 else
2888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 rettv->v_type = VAR_FUNC;
2891 rettv->vval.v_string = name;
2892 func_ref(name);
2893 }
2894 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002895theend:
2896 vim_free(trans_name);
2897}
2898
2899/*
2900 * "funcref()" function
2901 */
2902 static void
2903f_funcref(typval_T *argvars, typval_T *rettv)
2904{
2905 common_function(argvars, rettv, TRUE);
2906}
2907
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002908 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002909ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002910{
2911 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2912 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002913 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002914}
2915
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002916/*
2917 * "function()" function
2918 */
2919 static void
2920f_function(typval_T *argvars, typval_T *rettv)
2921{
2922 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923}
2924
2925/*
2926 * "garbagecollect()" function
2927 */
2928 static void
2929f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2930{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002931 // This is postponed until we are back at the toplevel, because we may be
2932 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 want_garbage_collect = TRUE;
2934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002935 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 garbage_collect_at_exit = TRUE;
2937}
2938
2939/*
2940 * "get()" function
2941 */
2942 static void
2943f_get(typval_T *argvars, typval_T *rettv)
2944{
2945 listitem_T *li;
2946 list_T *l;
2947 dictitem_T *di;
2948 dict_T *d;
2949 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002950 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002952 if (argvars[0].v_type == VAR_BLOB)
2953 {
2954 int error = FALSE;
2955 int idx = tv_get_number_chk(&argvars[1], &error);
2956
2957 if (!error)
2958 {
2959 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002960 if (idx < 0)
2961 idx = blob_len(argvars[0].vval.v_blob) + idx;
2962 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2963 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002964 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002965 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002966 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002967 tv = rettv;
2968 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002969 }
2970 }
2971 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 {
2973 if ((l = argvars[0].vval.v_list) != NULL)
2974 {
2975 int error = FALSE;
2976
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (!error && li != NULL)
2979 tv = &li->li_tv;
2980 }
2981 }
2982 else if (argvars[0].v_type == VAR_DICT)
2983 {
2984 if ((d = argvars[0].vval.v_dict) != NULL)
2985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002986 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 if (di != NULL)
2988 tv = &di->di_tv;
2989 }
2990 }
2991 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2992 {
2993 partial_T *pt;
2994 partial_T fref_pt;
2995
2996 if (argvars[0].v_type == VAR_PARTIAL)
2997 pt = argvars[0].vval.v_partial;
2998 else
2999 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003000 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 fref_pt.pt_name = argvars[0].vval.v_string;
3002 pt = &fref_pt;
3003 }
3004
3005 if (pt != NULL)
3006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003007 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003008 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009
3010 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3011 {
3012 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003013 n = partial_name(pt);
3014 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 rettv->vval.v_string = NULL;
3016 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003017 {
3018 rettv->vval.v_string = vim_strsave(n);
3019 if (rettv->v_type == VAR_FUNC)
3020 func_ref(rettv->vval.v_string);
3021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 }
3023 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003024 {
3025 what_is_dict = TRUE;
3026 if (pt->pt_dict != NULL)
3027 rettv_dict_set(rettv, pt->pt_dict);
3028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 else if (STRCMP(what, "args") == 0)
3030 {
3031 rettv->v_type = VAR_LIST;
3032 if (rettv_list_alloc(rettv) == OK)
3033 {
3034 int i;
3035
3036 for (i = 0; i < pt->pt_argc; ++i)
3037 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3038 }
3039 }
3040 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003041 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003042
3043 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3044 // third argument
3045 if (!what_is_dict)
3046 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 }
3048 }
3049 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003050 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051
3052 if (tv == NULL)
3053 {
3054 if (argvars[2].v_type != VAR_UNKNOWN)
3055 copy_tv(&argvars[2], rettv);
3056 }
3057 else
3058 copy_tv(tv, rettv);
3059}
3060
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003061/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003062 * "getchangelist()" function
3063 */
3064 static void
3065f_getchangelist(typval_T *argvars, typval_T *rettv)
3066{
3067#ifdef FEAT_JUMPLIST
3068 buf_T *buf;
3069 int i;
3070 list_T *l;
3071 dict_T *d;
3072#endif
3073
3074 if (rettv_list_alloc(rettv) != OK)
3075 return;
3076
3077#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003078 if (argvars[0].v_type == VAR_UNKNOWN)
3079 buf = curbuf;
3080 else
3081 {
3082 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3083 ++emsg_off;
3084 buf = tv_get_buf(&argvars[0], FALSE);
3085 --emsg_off;
3086 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003087 if (buf == NULL)
3088 return;
3089
3090 l = list_alloc();
3091 if (l == NULL)
3092 return;
3093
3094 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3095 return;
3096 /*
3097 * The current window change list index tracks only the position in the
3098 * current buffer change list. For other buffers, use the change list
3099 * length as the current index.
3100 */
3101 list_append_number(rettv->vval.v_list,
3102 (varnumber_T)((buf == curwin->w_buffer)
3103 ? curwin->w_changelistidx : buf->b_changelistlen));
3104
3105 for (i = 0; i < buf->b_changelistlen; ++i)
3106 {
3107 if (buf->b_changelist[i].lnum == 0)
3108 continue;
3109 if ((d = dict_alloc()) == NULL)
3110 return;
3111 if (list_append_dict(l, d) == FAIL)
3112 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003113 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3114 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003115 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003116 }
3117#endif
3118}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119
3120/*
3121 * "getcharsearch()" function
3122 */
3123 static void
3124f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3125{
3126 if (rettv_dict_alloc(rettv) != FAIL)
3127 {
3128 dict_T *dict = rettv->vval.v_dict;
3129
Bram Moolenaare0be1672018-07-08 16:50:37 +02003130 dict_add_string(dict, "char", last_csearch());
3131 dict_add_number(dict, "forward", last_csearch_forward());
3132 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 }
3134}
3135
3136/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003137 * "getenv()" function
3138 */
3139 static void
3140f_getenv(typval_T *argvars, typval_T *rettv)
3141{
3142 int mustfree = FALSE;
3143 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3144
3145 if (p == NULL)
3146 {
3147 rettv->v_type = VAR_SPECIAL;
3148 rettv->vval.v_number = VVAL_NULL;
3149 return;
3150 }
3151 if (!mustfree)
3152 p = vim_strsave(p);
3153 rettv->vval.v_string = p;
3154 rettv->v_type = VAR_STRING;
3155}
3156
3157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 * "getfontname()" function
3159 */
3160 static void
3161f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3162{
3163 rettv->v_type = VAR_STRING;
3164 rettv->vval.v_string = NULL;
3165#ifdef FEAT_GUI
3166 if (gui.in_use)
3167 {
3168 GuiFont font;
3169 char_u *name = NULL;
3170
3171 if (argvars[0].v_type == VAR_UNKNOWN)
3172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003173 // Get the "Normal" font. Either the name saved by
3174 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003175 font = gui.norm_font;
3176 name = hl_get_font_name();
3177 }
3178 else
3179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003180 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003181 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182 return;
3183 font = gui_mch_get_font(name, FALSE);
3184 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 }
3187 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3188 if (argvars[0].v_type != VAR_UNKNOWN)
3189 gui_mch_free_font(font);
3190 }
3191#endif
3192}
3193
3194/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003195 * "getjumplist()" function
3196 */
3197 static void
3198f_getjumplist(typval_T *argvars, typval_T *rettv)
3199{
3200#ifdef FEAT_JUMPLIST
3201 win_T *wp;
3202 int i;
3203 list_T *l;
3204 dict_T *d;
3205#endif
3206
3207 if (rettv_list_alloc(rettv) != OK)
3208 return;
3209
3210#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003211 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003212 if (wp == NULL)
3213 return;
3214
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003215 cleanup_jumplist(wp, TRUE);
3216
Bram Moolenaar4f505882018-02-10 21:06:32 +01003217 l = list_alloc();
3218 if (l == NULL)
3219 return;
3220
3221 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3222 return;
3223 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3224
3225 for (i = 0; i < wp->w_jumplistlen; ++i)
3226 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003227 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3228 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003229 if ((d = dict_alloc()) == NULL)
3230 return;
3231 if (list_append_dict(l, d) == FAIL)
3232 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003233 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3234 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003235 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003237 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003238 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003239 }
3240#endif
3241}
3242
3243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 * "getpid()" function
3245 */
3246 static void
3247f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3248{
3249 rettv->vval.v_number = mch_get_pid();
3250}
3251
3252 static void
3253getpos_both(
3254 typval_T *argvars,
3255 typval_T *rettv,
3256 int getcurpos)
3257{
3258 pos_T *fp;
3259 list_T *l;
3260 int fnum = -1;
3261
3262 if (rettv_list_alloc(rettv) == OK)
3263 {
3264 l = rettv->vval.v_list;
3265 if (getcurpos)
3266 fp = &curwin->w_cursor;
3267 else
3268 fp = var2fpos(&argvars[0], TRUE, &fnum);
3269 if (fnum != -1)
3270 list_append_number(l, (varnumber_T)fnum);
3271 else
3272 list_append_number(l, (varnumber_T)0);
3273 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3274 : (varnumber_T)0);
3275 list_append_number(l, (fp != NULL)
3276 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3277 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003278 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 (varnumber_T)0);
3280 if (getcurpos)
3281 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003282 int save_set_curswant = curwin->w_set_curswant;
3283 colnr_T save_curswant = curwin->w_curswant;
3284 colnr_T save_virtcol = curwin->w_virtcol;
3285
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 update_curswant();
3287 list_append_number(l, curwin->w_curswant == MAXCOL ?
3288 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003289
3290 // Do not change "curswant", as it is unexpected that a get
3291 // function has a side effect.
3292 if (save_set_curswant)
3293 {
3294 curwin->w_set_curswant = save_set_curswant;
3295 curwin->w_curswant = save_curswant;
3296 curwin->w_virtcol = save_virtcol;
3297 curwin->w_valid &= ~VALID_VIRTCOL;
3298 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 }
3300 }
3301 else
3302 rettv->vval.v_number = FALSE;
3303}
3304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305/*
3306 * "getcurpos()" function
3307 */
3308 static void
3309f_getcurpos(typval_T *argvars, typval_T *rettv)
3310{
3311 getpos_both(argvars, rettv, TRUE);
3312}
3313
3314/*
3315 * "getpos(string)" function
3316 */
3317 static void
3318f_getpos(typval_T *argvars, typval_T *rettv)
3319{
3320 getpos_both(argvars, rettv, FALSE);
3321}
3322
3323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 * "getreg()" function
3325 */
3326 static void
3327f_getreg(typval_T *argvars, typval_T *rettv)
3328{
3329 char_u *strregname;
3330 int regname;
3331 int arg2 = FALSE;
3332 int return_list = FALSE;
3333 int error = FALSE;
3334
3335 if (argvars[0].v_type != VAR_UNKNOWN)
3336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 error = strregname == NULL;
3339 if (argvars[1].v_type != VAR_UNKNOWN)
3340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003341 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003343 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 }
3345 }
3346 else
3347 strregname = get_vim_var_str(VV_REG);
3348
3349 if (error)
3350 return;
3351
3352 regname = (strregname == NULL ? '"' : *strregname);
3353 if (regname == 0)
3354 regname = '"';
3355
3356 if (return_list)
3357 {
3358 rettv->v_type = VAR_LIST;
3359 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3360 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3361 if (rettv->vval.v_list == NULL)
3362 (void)rettv_list_alloc(rettv);
3363 else
3364 ++rettv->vval.v_list->lv_refcount;
3365 }
3366 else
3367 {
3368 rettv->v_type = VAR_STRING;
3369 rettv->vval.v_string = get_reg_contents(regname,
3370 arg2 ? GREG_EXPR_SRC : 0);
3371 }
3372}
3373
3374/*
3375 * "getregtype()" function
3376 */
3377 static void
3378f_getregtype(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *strregname;
3381 int regname;
3382 char_u buf[NUMBUFLEN + 2];
3383 long reglen = 0;
3384
3385 if (argvars[0].v_type != VAR_UNKNOWN)
3386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003387 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003388 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 {
3390 rettv->v_type = VAR_STRING;
3391 rettv->vval.v_string = NULL;
3392 return;
3393 }
3394 }
3395 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 strregname = get_vim_var_str(VV_REG);
3398
3399 regname = (strregname == NULL ? '"' : *strregname);
3400 if (regname == 0)
3401 regname = '"';
3402
3403 buf[0] = NUL;
3404 buf[1] = NUL;
3405 switch (get_reg_type(regname, &reglen))
3406 {
3407 case MLINE: buf[0] = 'V'; break;
3408 case MCHAR: buf[0] = 'v'; break;
3409 case MBLOCK:
3410 buf[0] = Ctrl_V;
3411 sprintf((char *)buf + 1, "%ld", reglen + 1);
3412 break;
3413 }
3414 rettv->v_type = VAR_STRING;
3415 rettv->vval.v_string = vim_strsave(buf);
3416}
3417
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003418/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003419 * "gettagstack()" function
3420 */
3421 static void
3422f_gettagstack(typval_T *argvars, typval_T *rettv)
3423{
3424 win_T *wp = curwin; // default is current window
3425
3426 if (rettv_dict_alloc(rettv) != OK)
3427 return;
3428
3429 if (argvars[0].v_type != VAR_UNKNOWN)
3430 {
3431 wp = find_win_by_nr_or_id(&argvars[0]);
3432 if (wp == NULL)
3433 return;
3434 }
3435
3436 get_tagstack(wp, rettv->vval.v_dict);
3437}
3438
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003439// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440#include "version.h"
3441
3442/*
3443 * "has()" function
3444 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003445 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446f_has(typval_T *argvars, typval_T *rettv)
3447{
3448 int i;
3449 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003450 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003452 typedef struct {
3453 char *name;
3454 short present;
3455 } has_item_T;
3456 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003458 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003460 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003461#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003462 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003463#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003464 },
3465 {"arp",
3466#if defined(AMIGA) && defined(FEAT_ARP)
3467 1
3468#else
3469 0
3470#endif
3471 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003472 {"haiku",
3473#ifdef __HAIKU__
3474 1
3475#else
3476 0
3477#endif
3478 },
3479 {"bsd",
3480#if defined(BSD) && !defined(MACOS_X)
3481 1
3482#else
3483 0
3484#endif
3485 },
3486 {"hpux",
3487#ifdef hpux
3488 1
3489#else
3490 0
3491#endif
3492 },
3493 {"linux",
3494#ifdef __linux__
3495 1
3496#else
3497 0
3498#endif
3499 },
3500 {"mac", // Mac OS X (and, once, Mac OS Classic)
3501#ifdef MACOS_X
3502 1
3503#else
3504 0
3505#endif
3506 },
3507 {"osx", // Mac OS X
3508#ifdef MACOS_X
3509 1
3510#else
3511 0
3512#endif
3513 },
3514 {"macunix", // Mac OS X, with the darwin feature
3515#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3516 1
3517#else
3518 0
3519#endif
3520 },
3521 {"osxdarwin", // synonym for macunix
3522#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3523 1
3524#else
3525 0
3526#endif
3527 },
3528 {"qnx",
3529#ifdef __QNX__
3530 1
3531#else
3532 0
3533#endif
3534 },
3535 {"sun",
3536#ifdef SUN_SYSTEM
3537 1
3538#else
3539 0
3540#endif
3541 },
3542 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003544 1
3545#else
3546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003548 },
3549 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003551 1
3552#else
3553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003555 },
3556 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003557#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003558 1
3559#else
3560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003562 },
3563 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003564#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003565 1
3566#else
3567 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003569 },
3570 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003571#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003572 1
3573#else
3574 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003576 },
3577 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003579 1
3580#else
3581 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003583 },
3584 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003586 1
3587#else
3588 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003589#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003590 },
3591 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003592#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003593 1
3594#else
3595 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003596#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003597 },
3598 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003600 1
3601#else
3602 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003604 },
3605 {"autocmd", 1},
3606 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003607#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003608 1
3609#else
3610 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003611#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003612 },
3613 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003614#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003615 1
3616#else
3617 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003618#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003619 },
3620 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003621#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003622 1
3623#else
3624 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003626 },
3627 {"balloon_multiline",
3628#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3629 // MS-Windows requires runtime check, see below
3630 1
3631#else
3632 0
3633#endif
3634 },
3635 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003636#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003637 1
3638#else
3639 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003640#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003641 },
3642 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003644 1
3645#else
3646 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003648 },
3649 {"all_builtin_terms",
3650#if defined(ALL_BUILTIN_TCAPS)
3651 1
3652#else
3653 0
3654#endif
3655 },
3656 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003658 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003660 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003662 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003664 },
3665 {"byte_offset",
3666#ifdef FEAT_BYTEOFF
3667 1
3668#else
3669 0
3670#endif
3671 },
3672 {"channel",
3673#ifdef FEAT_JOB_CHANNEL
3674 1
3675#else
3676 0
3677#endif
3678 },
3679 {"cindent",
3680#ifdef FEAT_CINDENT
3681 1
3682#else
3683 0
3684#endif
3685 },
3686 {"clientserver",
3687#ifdef FEAT_CLIENTSERVER
3688 1
3689#else
3690 0
3691#endif
3692 },
3693 {"clipboard",
3694#ifdef FEAT_CLIPBOARD
3695 1
3696#else
3697 0
3698#endif
3699 },
3700 {"cmdline_compl", 1},
3701 {"cmdline_hist", 1},
3702 {"comments", 1},
3703 {"conceal",
3704#ifdef FEAT_CONCEAL
3705 1
3706#else
3707 0
3708#endif
3709 },
3710 {"cryptv",
3711#ifdef FEAT_CRYPT
3712 1
3713#else
3714 0
3715#endif
3716 },
3717 {"crypt-blowfish",
3718#ifdef FEAT_CRYPT
3719 1
3720#else
3721 0
3722#endif
3723 },
3724 {"crypt-blowfish2",
3725#ifdef FEAT_CRYPT
3726 1
3727#else
3728 0
3729#endif
3730 },
3731 {"cscope",
3732#ifdef FEAT_CSCOPE
3733 1
3734#else
3735 0
3736#endif
3737 },
3738 {"cursorbind", 1},
3739 {"cursorshape",
3740#ifdef CURSOR_SHAPE
3741 1
3742#else
3743 0
3744#endif
3745 },
3746 {"debug",
3747#ifdef DEBUG
3748 1
3749#else
3750 0
3751#endif
3752 },
3753 {"dialog_con",
3754#ifdef FEAT_CON_DIALOG
3755 1
3756#else
3757 0
3758#endif
3759 },
3760 {"dialog_gui",
3761#ifdef FEAT_GUI_DIALOG
3762 1
3763#else
3764 0
3765#endif
3766 },
3767 {"diff",
3768#ifdef FEAT_DIFF
3769 1
3770#else
3771 0
3772#endif
3773 },
3774 {"digraphs",
3775#ifdef FEAT_DIGRAPHS
3776 1
3777#else
3778 0
3779#endif
3780 },
3781 {"directx",
3782#ifdef FEAT_DIRECTX
3783 1
3784#else
3785 0
3786#endif
3787 },
3788 {"dnd",
3789#ifdef FEAT_DND
3790 1
3791#else
3792 0
3793#endif
3794 },
3795 {"emacs_tags",
3796#ifdef FEAT_EMACS_TAGS
3797 1
3798#else
3799 0
3800#endif
3801 },
3802 {"eval", 1}, // always present, of course!
3803 {"ex_extra", 1}, // graduated feature
3804 {"extra_search",
3805#ifdef FEAT_SEARCH_EXTRA
3806 1
3807#else
3808 0
3809#endif
3810 },
3811 {"file_in_path",
3812#ifdef FEAT_SEARCHPATH
3813 1
3814#else
3815 0
3816#endif
3817 },
3818 {"filterpipe",
3819#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3820 1
3821#else
3822 0
3823#endif
3824 },
3825 {"find_in_path",
3826#ifdef FEAT_FIND_ID
3827 1
3828#else
3829 0
3830#endif
3831 },
3832 {"float",
3833#ifdef FEAT_FLOAT
3834 1
3835#else
3836 0
3837#endif
3838 },
3839 {"folding",
3840#ifdef FEAT_FOLDING
3841 1
3842#else
3843 0
3844#endif
3845 },
3846 {"footer",
3847#ifdef FEAT_FOOTER
3848 1
3849#else
3850 0
3851#endif
3852 },
3853 {"fork",
3854#if !defined(USE_SYSTEM) && defined(UNIX)
3855 1
3856#else
3857 0
3858#endif
3859 },
3860 {"gettext",
3861#ifdef FEAT_GETTEXT
3862 1
3863#else
3864 0
3865#endif
3866 },
3867 {"gui",
3868#ifdef FEAT_GUI
3869 1
3870#else
3871 0
3872#endif
3873 },
3874 {"gui_neXtaw",
3875#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3876 1
3877#else
3878 0
3879#endif
3880 },
3881 {"gui_athena",
3882#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3883 1
3884#else
3885 0
3886#endif
3887 },
3888 {"gui_gtk",
3889#ifdef FEAT_GUI_GTK
3890 1
3891#else
3892 0
3893#endif
3894 },
3895 {"gui_gtk2",
3896#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3897 1
3898#else
3899 0
3900#endif
3901 },
3902 {"gui_gtk3",
3903#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3904 1
3905#else
3906 0
3907#endif
3908 },
3909 {"gui_gnome",
3910#ifdef FEAT_GUI_GNOME
3911 1
3912#else
3913 0
3914#endif
3915 },
3916 {"gui_haiku",
3917#ifdef FEAT_GUI_HAIKU
3918 1
3919#else
3920 0
3921#endif
3922 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003923 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003924 {"gui_motif",
3925#ifdef FEAT_GUI_MOTIF
3926 1
3927#else
3928 0
3929#endif
3930 },
3931 {"gui_photon",
3932#ifdef FEAT_GUI_PHOTON
3933 1
3934#else
3935 0
3936#endif
3937 },
3938 {"gui_win32",
3939#ifdef FEAT_GUI_MSWIN
3940 1
3941#else
3942 0
3943#endif
3944 },
3945 {"iconv",
3946#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3947 1
3948#else
3949 0
3950#endif
3951 },
3952 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003953 {"ipv6",
3954#ifdef FEAT_IPV6
3955 1
3956#else
3957 0
3958#endif
3959 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003960 {"job",
3961#ifdef FEAT_JOB_CHANNEL
3962 1
3963#else
3964 0
3965#endif
3966 },
3967 {"jumplist",
3968#ifdef FEAT_JUMPLIST
3969 1
3970#else
3971 0
3972#endif
3973 },
3974 {"keymap",
3975#ifdef FEAT_KEYMAP
3976 1
3977#else
3978 0
3979#endif
3980 },
3981 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3982 {"langmap",
3983#ifdef FEAT_LANGMAP
3984 1
3985#else
3986 0
3987#endif
3988 },
3989 {"libcall",
3990#ifdef FEAT_LIBCALL
3991 1
3992#else
3993 0
3994#endif
3995 },
3996 {"linebreak",
3997#ifdef FEAT_LINEBREAK
3998 1
3999#else
4000 0
4001#endif
4002 },
4003 {"lispindent",
4004#ifdef FEAT_LISP
4005 1
4006#else
4007 0
4008#endif
4009 },
4010 {"listcmds", 1},
4011 {"localmap", 1},
4012 {"lua",
4013#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4014 1
4015#else
4016 0
4017#endif
4018 },
4019 {"menu",
4020#ifdef FEAT_MENU
4021 1
4022#else
4023 0
4024#endif
4025 },
4026 {"mksession",
4027#ifdef FEAT_SESSION
4028 1
4029#else
4030 0
4031#endif
4032 },
4033 {"modify_fname", 1},
4034 {"mouse", 1},
4035 {"mouseshape",
4036#ifdef FEAT_MOUSESHAPE
4037 1
4038#else
4039 0
4040#endif
4041 },
4042 {"mouse_dec",
4043#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4044 1
4045#else
4046 0
4047#endif
4048 },
4049 {"mouse_gpm",
4050#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4051 1
4052#else
4053 0
4054#endif
4055 },
4056 {"mouse_jsbterm",
4057#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4058 1
4059#else
4060 0
4061#endif
4062 },
4063 {"mouse_netterm",
4064#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4065 1
4066#else
4067 0
4068#endif
4069 },
4070 {"mouse_pterm",
4071#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4072 1
4073#else
4074 0
4075#endif
4076 },
4077 {"mouse_sgr",
4078#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4079 1
4080#else
4081 0
4082#endif
4083 },
4084 {"mouse_sysmouse",
4085#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4086 1
4087#else
4088 0
4089#endif
4090 },
4091 {"mouse_urxvt",
4092#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4093 1
4094#else
4095 0
4096#endif
4097 },
4098 {"mouse_xterm",
4099#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4100 1
4101#else
4102 0
4103#endif
4104 },
4105 {"multi_byte", 1},
4106 {"multi_byte_ime",
4107#ifdef FEAT_MBYTE_IME
4108 1
4109#else
4110 0
4111#endif
4112 },
4113 {"multi_lang",
4114#ifdef FEAT_MULTI_LANG
4115 1
4116#else
4117 0
4118#endif
4119 },
4120 {"mzscheme",
4121#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4122 1
4123#else
4124 0
4125#endif
4126 },
4127 {"num64", 1},
4128 {"ole",
4129#ifdef FEAT_OLE
4130 1
4131#else
4132 0
4133#endif
4134 },
4135 {"packages",
4136#ifdef FEAT_EVAL
4137 1
4138#else
4139 0
4140#endif
4141 },
4142 {"path_extra",
4143#ifdef FEAT_PATH_EXTRA
4144 1
4145#else
4146 0
4147#endif
4148 },
4149 {"perl",
4150#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4151 1
4152#else
4153 0
4154#endif
4155 },
4156 {"persistent_undo",
4157#ifdef FEAT_PERSISTENT_UNDO
4158 1
4159#else
4160 0
4161#endif
4162 },
4163 {"python_compiled",
4164#if defined(FEAT_PYTHON)
4165 1
4166#else
4167 0
4168#endif
4169 },
4170 {"python_dynamic",
4171#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4172 1
4173#else
4174 0
4175#endif
4176 },
4177 {"python",
4178#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4179 1
4180#else
4181 0
4182#endif
4183 },
4184 {"pythonx",
4185#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4186 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4187 1
4188#else
4189 0
4190#endif
4191 },
4192 {"python3_compiled",
4193#if defined(FEAT_PYTHON3)
4194 1
4195#else
4196 0
4197#endif
4198 },
4199 {"python3_dynamic",
4200#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4201 1
4202#else
4203 0
4204#endif
4205 },
4206 {"python3",
4207#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4208 1
4209#else
4210 0
4211#endif
4212 },
4213 {"popupwin",
4214#ifdef FEAT_PROP_POPUP
4215 1
4216#else
4217 0
4218#endif
4219 },
4220 {"postscript",
4221#ifdef FEAT_POSTSCRIPT
4222 1
4223#else
4224 0
4225#endif
4226 },
4227 {"printer",
4228#ifdef FEAT_PRINTER
4229 1
4230#else
4231 0
4232#endif
4233 },
4234 {"profile",
4235#ifdef FEAT_PROFILE
4236 1
4237#else
4238 0
4239#endif
4240 },
4241 {"reltime",
4242#ifdef FEAT_RELTIME
4243 1
4244#else
4245 0
4246#endif
4247 },
4248 {"quickfix",
4249#ifdef FEAT_QUICKFIX
4250 1
4251#else
4252 0
4253#endif
4254 },
4255 {"rightleft",
4256#ifdef FEAT_RIGHTLEFT
4257 1
4258#else
4259 0
4260#endif
4261 },
4262 {"ruby",
4263#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4264 1
4265#else
4266 0
4267#endif
4268 },
4269 {"scrollbind", 1},
4270 {"showcmd",
4271#ifdef FEAT_CMDL_INFO
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"cmdline_info",
4278#ifdef FEAT_CMDL_INFO
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"signs",
4285#ifdef FEAT_SIGNS
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"smartindent",
4292#ifdef FEAT_SMARTINDENT
4293 1
4294#else
4295 0
4296#endif
4297 },
4298 {"startuptime",
4299#ifdef STARTUPTIME
4300 1
4301#else
4302 0
4303#endif
4304 },
4305 {"statusline",
4306#ifdef FEAT_STL_OPT
4307 1
4308#else
4309 0
4310#endif
4311 },
4312 {"netbeans_intg",
4313#ifdef FEAT_NETBEANS_INTG
4314 1
4315#else
4316 0
4317#endif
4318 },
4319 {"sound",
4320#ifdef FEAT_SOUND
4321 1
4322#else
4323 0
4324#endif
4325 },
4326 {"spell",
4327#ifdef FEAT_SPELL
4328 1
4329#else
4330 0
4331#endif
4332 },
4333 {"syntax",
4334#ifdef FEAT_SYN_HL
4335 1
4336#else
4337 0
4338#endif
4339 },
4340 {"system",
4341#if defined(USE_SYSTEM) || !defined(UNIX)
4342 1
4343#else
4344 0
4345#endif
4346 },
4347 {"tag_binary",
4348#ifdef FEAT_TAG_BINS
4349 1
4350#else
4351 0
4352#endif
4353 },
4354 {"tcl",
4355#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4356 1
4357#else
4358 0
4359#endif
4360 },
4361 {"termguicolors",
4362#ifdef FEAT_TERMGUICOLORS
4363 1
4364#else
4365 0
4366#endif
4367 },
4368 {"terminal",
4369#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4370 1
4371#else
4372 0
4373#endif
4374 },
4375 {"terminfo",
4376#ifdef TERMINFO
4377 1
4378#else
4379 0
4380#endif
4381 },
4382 {"termresponse",
4383#ifdef FEAT_TERMRESPONSE
4384 1
4385#else
4386 0
4387#endif
4388 },
4389 {"textobjects",
4390#ifdef FEAT_TEXTOBJ
4391 1
4392#else
4393 0
4394#endif
4395 },
4396 {"textprop",
4397#ifdef FEAT_PROP_POPUP
4398 1
4399#else
4400 0
4401#endif
4402 },
4403 {"tgetent",
4404#ifdef HAVE_TGETENT
4405 1
4406#else
4407 0
4408#endif
4409 },
4410 {"timers",
4411#ifdef FEAT_TIMERS
4412 1
4413#else
4414 0
4415#endif
4416 },
4417 {"title",
4418#ifdef FEAT_TITLE
4419 1
4420#else
4421 0
4422#endif
4423 },
4424 {"toolbar",
4425#ifdef FEAT_TOOLBAR
4426 1
4427#else
4428 0
4429#endif
4430 },
4431 {"unnamedplus",
4432#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4433 1
4434#else
4435 0
4436#endif
4437 },
4438 {"user-commands", 1}, // was accidentally included in 5.4
4439 {"user_commands", 1},
4440 {"vartabs",
4441#ifdef FEAT_VARTABS
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"vertsplit", 1},
4448 {"viminfo",
4449#ifdef FEAT_VIMINFO
4450 1
4451#else
4452 0
4453#endif
4454 },
4455 {"vimscript-1", 1},
4456 {"vimscript-2", 1},
4457 {"vimscript-3", 1},
4458 {"vimscript-4", 1},
4459 {"virtualedit", 1},
4460 {"visual", 1},
4461 {"visualextra", 1},
4462 {"vreplace", 1},
4463 {"vtp",
4464#ifdef FEAT_VTP
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"wildignore",
4471#ifdef FEAT_WILDIGN
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"wildmenu",
4478#ifdef FEAT_WILDMENU
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"windows", 1},
4485 {"winaltkeys",
4486#ifdef FEAT_WAK
4487 1
4488#else
4489 0
4490#endif
4491 },
4492 {"writebackup",
4493#ifdef FEAT_WRITEBACKUP
4494 1
4495#else
4496 0
4497#endif
4498 },
4499 {"xim",
4500#ifdef FEAT_XIM
4501 1
4502#else
4503 0
4504#endif
4505 },
4506 {"xfontset",
4507#ifdef FEAT_XFONTSET
4508 1
4509#else
4510 0
4511#endif
4512 },
4513 {"xpm",
4514#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4515 1
4516#else
4517 0
4518#endif
4519 },
4520 {"xpm_w32", // for backward compatibility
4521#ifdef FEAT_XPM_W32
4522 1
4523#else
4524 0
4525#endif
4526 },
4527 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004529 1
4530#else
4531 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 },
4534 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004536 1
4537#else
4538 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004540 },
4541 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004543 1
4544#else
4545 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 },
4548 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004550 1
4551#else
4552 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 },
4555 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004557 1
4558#else
4559 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004561 },
4562 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004563 };
4564
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004565 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 for (i = 0; has_list[i].name != NULL; ++i)
4567 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004569 x = TRUE;
4570 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571 break;
4572 }
4573
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 // features also in has_list[] but sometimes enabled at runtime
4575 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004578 {
4579 // intentionally empty
4580 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004581#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582 else if (STRICMP(name, "balloon_multiline") == 0)
4583 n = multiline_balloon_available();
4584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004585#ifdef VIMDLL
4586 else if (STRICMP(name, "filterpipe") == 0)
4587 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588#endif
4589#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4590 else if (STRICMP(name, "iconv") == 0)
4591 n = iconv_enabled(FALSE);
4592#endif
4593#ifdef DYNAMIC_LUA
4594 else if (STRICMP(name, "lua") == 0)
4595 n = lua_enabled(FALSE);
4596#endif
4597#ifdef DYNAMIC_MZSCHEME
4598 else if (STRICMP(name, "mzscheme") == 0)
4599 n = mzscheme_enabled(FALSE);
4600#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004601#ifdef DYNAMIC_PERL
4602 else if (STRICMP(name, "perl") == 0)
4603 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004605#ifdef DYNAMIC_PYTHON
4606 else if (STRICMP(name, "python") == 0)
4607 n = python_enabled(FALSE);
4608#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609#ifdef DYNAMIC_PYTHON3
4610 else if (STRICMP(name, "python3") == 0)
4611 n = python3_enabled(FALSE);
4612#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004613#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4614 else if (STRICMP(name, "pythonx") == 0)
4615 {
4616# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4617 if (p_pyx == 0)
4618 n = python3_enabled(FALSE) || python_enabled(FALSE);
4619 else if (p_pyx == 3)
4620 n = python3_enabled(FALSE);
4621 else if (p_pyx == 2)
4622 n = python_enabled(FALSE);
4623# elif defined(DYNAMIC_PYTHON)
4624 n = python_enabled(FALSE);
4625# elif defined(DYNAMIC_PYTHON3)
4626 n = python3_enabled(FALSE);
4627# endif
4628 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004630#ifdef DYNAMIC_RUBY
4631 else if (STRICMP(name, "ruby") == 0)
4632 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004634#ifdef DYNAMIC_TCL
4635 else if (STRICMP(name, "tcl") == 0)
4636 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004637#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004638#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004639 else if (STRICMP(name, "terminal") == 0)
4640 n = terminal_enabled();
4641#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004642 }
4643
Bram Moolenaar79296512020-03-22 16:17:14 +01004644 // features not in has_list[]
4645 if (x == FALSE)
4646 {
4647 if (STRNICMP(name, "patch", 5) == 0)
4648 {
4649 x = TRUE;
4650 if (name[5] == '-'
4651 && STRLEN(name) >= 11
4652 && vim_isdigit(name[6])
4653 && vim_isdigit(name[8])
4654 && vim_isdigit(name[10]))
4655 {
4656 int major = atoi((char *)name + 6);
4657 int minor = atoi((char *)name + 8);
4658
4659 // Expect "patch-9.9.01234".
4660 n = (major < VIM_VERSION_MAJOR
4661 || (major == VIM_VERSION_MAJOR
4662 && (minor < VIM_VERSION_MINOR
4663 || (minor == VIM_VERSION_MINOR
4664 && has_patch(atoi((char *)name + 10))))));
4665 }
4666 else
4667 n = has_patch(atoi((char *)name + 5));
4668 }
4669 else if (STRICMP(name, "vim_starting") == 0)
4670 {
4671 x = TRUE;
4672 n = (starting != 0);
4673 }
4674 else if (STRICMP(name, "ttyin") == 0)
4675 {
4676 x = TRUE;
4677 n = mch_input_isatty();
4678 }
4679 else if (STRICMP(name, "ttyout") == 0)
4680 {
4681 x = TRUE;
4682 n = stdout_isatty;
4683 }
4684 else if (STRICMP(name, "multi_byte_encoding") == 0)
4685 {
4686 x = TRUE;
4687 n = has_mbyte;
4688 }
4689 else if (STRICMP(name, "gui_running") == 0)
4690 {
4691 x = TRUE;
4692#ifdef FEAT_GUI
4693 n = (gui.in_use || gui.starting);
4694#endif
4695 }
4696 else if (STRICMP(name, "browse") == 0)
4697 {
4698 x = TRUE;
4699#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4700 n = gui.in_use; // gui_mch_browse() works when GUI is running
4701#endif
4702 }
4703 else if (STRICMP(name, "syntax_items") == 0)
4704 {
4705 x = TRUE;
4706#ifdef FEAT_SYN_HL
4707 n = syntax_present(curwin);
4708#endif
4709 }
4710 else if (STRICMP(name, "vcon") == 0)
4711 {
4712 x = TRUE;
4713#ifdef FEAT_VTP
4714 n = is_term_win32() && has_vtp_working();
4715#endif
4716 }
4717 else if (STRICMP(name, "netbeans_enabled") == 0)
4718 {
4719 x = TRUE;
4720#ifdef FEAT_NETBEANS_INTG
4721 n = netbeans_active();
4722#endif
4723 }
4724 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4725 {
4726 x = TRUE;
4727#ifdef FEAT_MOUSE_GPM
4728 n = gpm_enabled();
4729#endif
4730 }
4731 else if (STRICMP(name, "conpty") == 0)
4732 {
4733 x = TRUE;
4734#if defined(FEAT_TERMINAL) && defined(MSWIN)
4735 n = use_conpty();
4736#endif
4737 }
4738 else if (STRICMP(name, "clipboard_working") == 0)
4739 {
4740 x = TRUE;
4741#ifdef FEAT_CLIPBOARD
4742 n = clip_star.available;
4743#endif
4744 }
4745 }
4746
4747 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4748 // return whether feature could ever be enabled
4749 rettv->vval.v_number = x;
4750 else
4751 // return whether feature is enabled
4752 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004753}
4754
4755/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004756 * "haslocaldir()" function
4757 */
4758 static void
4759f_haslocaldir(typval_T *argvars, typval_T *rettv)
4760{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004761 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004762 win_T *wp = NULL;
4763
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004764 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4765
4766 // Check for window-local and tab-local directories
4767 if (wp != NULL && wp->w_localdir != NULL)
4768 rettv->vval.v_number = 1;
4769 else if (tp != NULL && tp->tp_localdir != NULL)
4770 rettv->vval.v_number = 2;
4771 else
4772 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773}
4774
4775/*
4776 * "hasmapto()" function
4777 */
4778 static void
4779f_hasmapto(typval_T *argvars, typval_T *rettv)
4780{
4781 char_u *name;
4782 char_u *mode;
4783 char_u buf[NUMBUFLEN];
4784 int abbr = FALSE;
4785
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004786 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004787 if (argvars[1].v_type == VAR_UNKNOWN)
4788 mode = (char_u *)"nvo";
4789 else
4790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004793 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004794 }
4795
4796 if (map_to_exists(name, mode, abbr))
4797 rettv->vval.v_number = TRUE;
4798 else
4799 rettv->vval.v_number = FALSE;
4800}
4801
4802/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004803 * "highlightID(name)" function
4804 */
4805 static void
4806f_hlID(typval_T *argvars, typval_T *rettv)
4807{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004808 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004809}
4810
4811/*
4812 * "highlight_exists()" function
4813 */
4814 static void
4815f_hlexists(typval_T *argvars, typval_T *rettv)
4816{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004817 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818}
4819
4820/*
4821 * "hostname()" function
4822 */
4823 static void
4824f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4825{
4826 char_u hostname[256];
4827
4828 mch_get_host_name(hostname, 256);
4829 rettv->v_type = VAR_STRING;
4830 rettv->vval.v_string = vim_strsave(hostname);
4831}
4832
4833/*
4834 * iconv() function
4835 */
4836 static void
4837f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4838{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004839 char_u buf1[NUMBUFLEN];
4840 char_u buf2[NUMBUFLEN];
4841 char_u *from, *to, *str;
4842 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004843
4844 rettv->v_type = VAR_STRING;
4845 rettv->vval.v_string = NULL;
4846
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004847 str = tv_get_string(&argvars[0]);
4848 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4849 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004850 vimconv.vc_type = CONV_NONE;
4851 convert_setup(&vimconv, from, to);
4852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004854 if (vimconv.vc_type == CONV_NONE)
4855 rettv->vval.v_string = vim_strsave(str);
4856 else
4857 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4858
4859 convert_setup(&vimconv, NULL, NULL);
4860 vim_free(from);
4861 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862}
4863
4864/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004865 * "index()" function
4866 */
4867 static void
4868f_index(typval_T *argvars, typval_T *rettv)
4869{
4870 list_T *l;
4871 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004872 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 long idx = 0;
4874 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004875 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876
4877 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004878 if (argvars[0].v_type == VAR_BLOB)
4879 {
4880 typval_T tv;
4881 int start = 0;
4882
4883 if (argvars[2].v_type != VAR_UNKNOWN)
4884 {
4885 start = tv_get_number_chk(&argvars[2], &error);
4886 if (error)
4887 return;
4888 }
4889 b = argvars[0].vval.v_blob;
4890 if (b == NULL)
4891 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004892 if (start < 0)
4893 {
4894 start = blob_len(b) + start;
4895 if (start < 0)
4896 start = 0;
4897 }
4898
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004899 for (idx = start; idx < blob_len(b); ++idx)
4900 {
4901 tv.v_type = VAR_NUMBER;
4902 tv.vval.v_number = blob_get(b, idx);
4903 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4904 {
4905 rettv->vval.v_number = idx;
4906 return;
4907 }
4908 }
4909 return;
4910 }
4911 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004913 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 return;
4915 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 l = argvars[0].vval.v_list;
4918 if (l != NULL)
4919 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004920 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921 item = l->lv_first;
4922 if (argvars[2].v_type != VAR_UNKNOWN)
4923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004924 // Start at specified item. Use the cached index that list_find()
4925 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004926 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004927 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004929 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004930 if (error)
4931 item = NULL;
4932 }
4933
4934 for ( ; item != NULL; item = item->li_next, ++idx)
4935 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4936 {
4937 rettv->vval.v_number = idx;
4938 break;
4939 }
4940 }
4941}
4942
4943static int inputsecret_flag = 0;
4944
4945/*
4946 * "input()" function
4947 * Also handles inputsecret() when inputsecret is set.
4948 */
4949 static void
4950f_input(typval_T *argvars, typval_T *rettv)
4951{
4952 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4953}
4954
4955/*
4956 * "inputdialog()" function
4957 */
4958 static void
4959f_inputdialog(typval_T *argvars, typval_T *rettv)
4960{
4961#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4964 {
4965 char_u *message;
4966 char_u buf[NUMBUFLEN];
4967 char_u *defstr = (char_u *)"";
4968
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004969 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004970 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004971 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4973 else
4974 IObuff[0] = NUL;
4975 if (message != NULL && defstr != NULL
4976 && do_dialog(VIM_QUESTION, NULL, message,
4977 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4978 rettv->vval.v_string = vim_strsave(IObuff);
4979 else
4980 {
4981 if (message != NULL && defstr != NULL
4982 && argvars[1].v_type != VAR_UNKNOWN
4983 && argvars[2].v_type != VAR_UNKNOWN)
4984 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004985 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 else
4987 rettv->vval.v_string = NULL;
4988 }
4989 rettv->v_type = VAR_STRING;
4990 }
4991 else
4992#endif
4993 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4994}
4995
4996/*
4997 * "inputlist()" function
4998 */
4999 static void
5000f_inputlist(typval_T *argvars, typval_T *rettv)
5001{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005002 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005003 listitem_T *li;
5004 int selected;
5005 int mouse_used;
5006
5007#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 // While starting up, there is no place to enter text. When running tests
5009 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005010 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 return;
5012#endif
5013 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005015 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 return;
5017 }
5018
5019 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005020 msg_row = Rows - 1; // for when 'cmdheight' > 1
5021 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005022 msg_scroll = TRUE;
5023 msg_clr_eos();
5024
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005025 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005026 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005027 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005029 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030 msg_putchar('\n');
5031 }
5032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 selected = prompt_for_number(&mouse_used);
5035 if (mouse_used)
5036 selected -= lines_left;
5037
5038 rettv->vval.v_number = selected;
5039}
5040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5042
5043/*
5044 * "inputrestore()" function
5045 */
5046 static void
5047f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5048{
5049 if (ga_userinput.ga_len > 0)
5050 {
5051 --ga_userinput.ga_len;
5052 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5053 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005055 }
5056 else if (p_verbose > 1)
5057 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005058 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005059 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005060 }
5061}
5062
5063/*
5064 * "inputsave()" function
5065 */
5066 static void
5067f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5068{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070 if (ga_grow(&ga_userinput, 1) == OK)
5071 {
5072 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5073 + ga_userinput.ga_len);
5074 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005076 }
5077 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079}
5080
5081/*
5082 * "inputsecret()" function
5083 */
5084 static void
5085f_inputsecret(typval_T *argvars, typval_T *rettv)
5086{
5087 ++cmdline_star;
5088 ++inputsecret_flag;
5089 f_input(argvars, rettv);
5090 --cmdline_star;
5091 --inputsecret_flag;
5092}
5093
5094/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005095 * "interrupt()" function
5096 */
5097 static void
5098f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5099{
5100 got_int = TRUE;
5101}
5102
5103/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 * "invert(expr)" function
5105 */
5106 static void
5107f_invert(typval_T *argvars, typval_T *rettv)
5108{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110}
5111
5112/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113 * "islocked()" function
5114 */
5115 static void
5116f_islocked(typval_T *argvars, typval_T *rettv)
5117{
5118 lval_T lv;
5119 char_u *end;
5120 dictitem_T *di;
5121
5122 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005123 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005124 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 if (end != NULL && lv.ll_name != NULL)
5126 {
5127 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005128 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005129 else
5130 {
5131 if (lv.ll_tv == NULL)
5132 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005133 di = find_var(lv.ll_name, NULL, TRUE);
5134 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005136 // Consider a variable locked when:
5137 // 1. the variable itself is locked
5138 // 2. the value of the variable is locked.
5139 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005140 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5141 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005142 }
5143 }
5144 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005145 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005147 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5151 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005153 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5154 }
5155 }
5156
5157 clear_lval(&lv);
5158}
5159
5160#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5161/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005162 * "isinf()" function
5163 */
5164 static void
5165f_isinf(typval_T *argvars, typval_T *rettv)
5166{
5167 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5168 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5169}
5170
5171/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172 * "isnan()" function
5173 */
5174 static void
5175f_isnan(typval_T *argvars, typval_T *rettv)
5176{
5177 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5178 && isnan(argvars[0].vval.v_float);
5179}
5180#endif
5181
5182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 * "last_buffer_nr()" function.
5184 */
5185 static void
5186f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5187{
5188 int n = 0;
5189 buf_T *buf;
5190
Bram Moolenaar29323592016-07-24 22:04:11 +02005191 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192 if (n < buf->b_fnum)
5193 n = buf->b_fnum;
5194
5195 rettv->vval.v_number = n;
5196}
5197
5198/*
5199 * "len()" function
5200 */
5201 static void
5202f_len(typval_T *argvars, typval_T *rettv)
5203{
5204 switch (argvars[0].v_type)
5205 {
5206 case VAR_STRING:
5207 case VAR_NUMBER:
5208 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005209 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005211 case VAR_BLOB:
5212 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5213 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005214 case VAR_LIST:
5215 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5216 break;
5217 case VAR_DICT:
5218 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5219 break;
5220 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005221 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005223 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224 case VAR_SPECIAL:
5225 case VAR_FLOAT:
5226 case VAR_FUNC:
5227 case VAR_PARTIAL:
5228 case VAR_JOB:
5229 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005230 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005231 break;
5232 }
5233}
5234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005235 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005236libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237{
5238#ifdef FEAT_LIBCALL
5239 char_u *string_in;
5240 char_u **string_result;
5241 int nr_result;
5242#endif
5243
5244 rettv->v_type = type;
5245 if (type != VAR_NUMBER)
5246 rettv->vval.v_string = NULL;
5247
5248 if (check_restricted() || check_secure())
5249 return;
5250
5251#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005253 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5254 {
5255 string_in = NULL;
5256 if (argvars[2].v_type == VAR_STRING)
5257 string_in = argvars[2].vval.v_string;
5258 if (type == VAR_NUMBER)
5259 string_result = NULL;
5260 else
5261 string_result = &rettv->vval.v_string;
5262 if (mch_libcall(argvars[0].vval.v_string,
5263 argvars[1].vval.v_string,
5264 string_in,
5265 argvars[2].vval.v_number,
5266 string_result,
5267 &nr_result) == OK
5268 && type == VAR_NUMBER)
5269 rettv->vval.v_number = nr_result;
5270 }
5271#endif
5272}
5273
5274/*
5275 * "libcall()" function
5276 */
5277 static void
5278f_libcall(typval_T *argvars, typval_T *rettv)
5279{
5280 libcall_common(argvars, rettv, VAR_STRING);
5281}
5282
5283/*
5284 * "libcallnr()" function
5285 */
5286 static void
5287f_libcallnr(typval_T *argvars, typval_T *rettv)
5288{
5289 libcall_common(argvars, rettv, VAR_NUMBER);
5290}
5291
5292/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005293 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 */
5295 static void
5296f_line(typval_T *argvars, typval_T *rettv)
5297{
5298 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005299 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005301 int id;
5302 tabpage_T *tp;
5303 win_T *wp;
5304 win_T *save_curwin;
5305 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005307 if (argvars[1].v_type != VAR_UNKNOWN)
5308 {
5309 // use window specified in the second argument
5310 id = (int)tv_get_number(&argvars[1]);
5311 wp = win_id2wp_tp(id, &tp);
5312 if (wp != NULL && tp != NULL)
5313 {
5314 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5315 == OK)
5316 {
5317 check_cursor();
5318 fp = var2fpos(&argvars[0], TRUE, &fnum);
5319 }
5320 restore_win_noblock(save_curwin, save_curtab, TRUE);
5321 }
5322 }
5323 else
5324 // use current window
5325 fp = var2fpos(&argvars[0], TRUE, &fnum);
5326
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 if (fp != NULL)
5328 lnum = fp->lnum;
5329 rettv->vval.v_number = lnum;
5330}
5331
5332/*
5333 * "line2byte(lnum)" function
5334 */
5335 static void
5336f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5337{
5338#ifndef FEAT_BYTEOFF
5339 rettv->vval.v_number = -1;
5340#else
5341 linenr_T lnum;
5342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005343 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5345 rettv->vval.v_number = -1;
5346 else
5347 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5348 if (rettv->vval.v_number >= 0)
5349 ++rettv->vval.v_number;
5350#endif
5351}
5352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353#ifdef FEAT_FLOAT
5354/*
5355 * "log()" function
5356 */
5357 static void
5358f_log(typval_T *argvars, typval_T *rettv)
5359{
5360 float_T f = 0.0;
5361
5362 rettv->v_type = VAR_FLOAT;
5363 if (get_float_arg(argvars, &f) == OK)
5364 rettv->vval.v_float = log(f);
5365 else
5366 rettv->vval.v_float = 0.0;
5367}
5368
5369/*
5370 * "log10()" function
5371 */
5372 static void
5373f_log10(typval_T *argvars, typval_T *rettv)
5374{
5375 float_T f = 0.0;
5376
5377 rettv->v_type = VAR_FLOAT;
5378 if (get_float_arg(argvars, &f) == OK)
5379 rettv->vval.v_float = log10(f);
5380 else
5381 rettv->vval.v_float = 0.0;
5382}
5383#endif
5384
5385#ifdef FEAT_LUA
5386/*
5387 * "luaeval()" function
5388 */
5389 static void
5390f_luaeval(typval_T *argvars, typval_T *rettv)
5391{
5392 char_u *str;
5393 char_u buf[NUMBUFLEN];
5394
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005395 if (check_restricted() || check_secure())
5396 return;
5397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005398 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005399 do_luaeval(str, argvars + 1, rettv);
5400}
5401#endif
5402
5403/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404 * "maparg()" function
5405 */
5406 static void
5407f_maparg(typval_T *argvars, typval_T *rettv)
5408{
5409 get_maparg(argvars, rettv, TRUE);
5410}
5411
5412/*
5413 * "mapcheck()" function
5414 */
5415 static void
5416f_mapcheck(typval_T *argvars, typval_T *rettv)
5417{
5418 get_maparg(argvars, rettv, FALSE);
5419}
5420
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005421typedef enum
5422{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005423 MATCH_END, // matchend()
5424 MATCH_MATCH, // match()
5425 MATCH_STR, // matchstr()
5426 MATCH_LIST, // matchlist()
5427 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005428} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005429
5430 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005431find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005432{
5433 char_u *str = NULL;
5434 long len = 0;
5435 char_u *expr = NULL;
5436 char_u *pat;
5437 regmatch_T regmatch;
5438 char_u patbuf[NUMBUFLEN];
5439 char_u strbuf[NUMBUFLEN];
5440 char_u *save_cpo;
5441 long start = 0;
5442 long nth = 1;
5443 colnr_T startcol = 0;
5444 int match = 0;
5445 list_T *l = NULL;
5446 listitem_T *li = NULL;
5447 long idx = 0;
5448 char_u *tofree = NULL;
5449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451 save_cpo = p_cpo;
5452 p_cpo = (char_u *)"";
5453
5454 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005455 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005457 // type MATCH_LIST: return empty list when there are no matches.
5458 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005459 if (rettv_list_alloc(rettv) == FAIL)
5460 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005461 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005462 && (list_append_string(rettv->vval.v_list,
5463 (char_u *)"", 0) == FAIL
5464 || list_append_number(rettv->vval.v_list,
5465 (varnumber_T)-1) == FAIL
5466 || list_append_number(rettv->vval.v_list,
5467 (varnumber_T)-1) == FAIL
5468 || list_append_number(rettv->vval.v_list,
5469 (varnumber_T)-1) == FAIL))
5470 {
5471 list_free(rettv->vval.v_list);
5472 rettv->vval.v_list = NULL;
5473 goto theend;
5474 }
5475 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005476 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005477 {
5478 rettv->v_type = VAR_STRING;
5479 rettv->vval.v_string = NULL;
5480 }
5481
5482 if (argvars[0].v_type == VAR_LIST)
5483 {
5484 if ((l = argvars[0].vval.v_list) == NULL)
5485 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005486 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 li = l->lv_first;
5488 }
5489 else
5490 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005491 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 len = (long)STRLEN(str);
5493 }
5494
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005495 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 if (pat == NULL)
5497 goto theend;
5498
5499 if (argvars[2].v_type != VAR_UNKNOWN)
5500 {
5501 int error = FALSE;
5502
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005503 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 if (error)
5505 goto theend;
5506 if (l != NULL)
5507 {
5508 li = list_find(l, start);
5509 if (li == NULL)
5510 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005511 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 }
5513 else
5514 {
5515 if (start < 0)
5516 start = 0;
5517 if (start > len)
5518 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // When "count" argument is there ignore matches before "start",
5520 // otherwise skip part of the string. Differs when pattern is "^"
5521 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522 if (argvars[3].v_type != VAR_UNKNOWN)
5523 startcol = start;
5524 else
5525 {
5526 str += start;
5527 len -= start;
5528 }
5529 }
5530
5531 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005532 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533 if (error)
5534 goto theend;
5535 }
5536
5537 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5538 if (regmatch.regprog != NULL)
5539 {
5540 regmatch.rm_ic = p_ic;
5541
5542 for (;;)
5543 {
5544 if (l != NULL)
5545 {
5546 if (li == NULL)
5547 {
5548 match = FALSE;
5549 break;
5550 }
5551 vim_free(tofree);
5552 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5553 if (str == NULL)
5554 break;
5555 }
5556
5557 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5558
5559 if (match && --nth <= 0)
5560 break;
5561 if (l == NULL && !match)
5562 break;
5563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005564 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565 if (l != NULL)
5566 {
5567 li = li->li_next;
5568 ++idx;
5569 }
5570 else
5571 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 startcol = (colnr_T)(regmatch.startp[0]
5573 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005574 if (startcol > (colnr_T)len
5575 || str + startcol <= regmatch.startp[0])
5576 {
5577 match = FALSE;
5578 break;
5579 }
5580 }
5581 }
5582
5583 if (match)
5584 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005585 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 {
5587 listitem_T *li1 = rettv->vval.v_list->lv_first;
5588 listitem_T *li2 = li1->li_next;
5589 listitem_T *li3 = li2->li_next;
5590 listitem_T *li4 = li3->li_next;
5591
5592 vim_free(li1->li_tv.vval.v_string);
5593 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005594 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595 li3->li_tv.vval.v_number =
5596 (varnumber_T)(regmatch.startp[0] - expr);
5597 li4->li_tv.vval.v_number =
5598 (varnumber_T)(regmatch.endp[0] - expr);
5599 if (l != NULL)
5600 li2->li_tv.vval.v_number = (varnumber_T)idx;
5601 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005602 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603 {
5604 int i;
5605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005606 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607 for (i = 0; i < NSUBEXP; ++i)
5608 {
5609 if (regmatch.endp[i] == NULL)
5610 {
5611 if (list_append_string(rettv->vval.v_list,
5612 (char_u *)"", 0) == FAIL)
5613 break;
5614 }
5615 else if (list_append_string(rettv->vval.v_list,
5616 regmatch.startp[i],
5617 (int)(regmatch.endp[i] - regmatch.startp[i]))
5618 == FAIL)
5619 break;
5620 }
5621 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005622 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005624 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625 if (l != NULL)
5626 copy_tv(&li->li_tv, rettv);
5627 else
5628 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005629 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 }
5631 else if (l != NULL)
5632 rettv->vval.v_number = idx;
5633 else
5634 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005635 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 rettv->vval.v_number =
5637 (varnumber_T)(regmatch.startp[0] - str);
5638 else
5639 rettv->vval.v_number =
5640 (varnumber_T)(regmatch.endp[0] - str);
5641 rettv->vval.v_number += (varnumber_T)(str - expr);
5642 }
5643 }
5644 vim_regfree(regmatch.regprog);
5645 }
5646
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005647theend:
5648 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005649 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005650 listitem_remove(rettv->vval.v_list,
5651 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 vim_free(tofree);
5653 p_cpo = save_cpo;
5654}
5655
5656/*
5657 * "match()" function
5658 */
5659 static void
5660f_match(typval_T *argvars, typval_T *rettv)
5661{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005662 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005663}
5664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665/*
5666 * "matchend()" function
5667 */
5668 static void
5669f_matchend(typval_T *argvars, typval_T *rettv)
5670{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005671 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672}
5673
5674/*
5675 * "matchlist()" function
5676 */
5677 static void
5678f_matchlist(typval_T *argvars, typval_T *rettv)
5679{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005680 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005681}
5682
5683/*
5684 * "matchstr()" function
5685 */
5686 static void
5687f_matchstr(typval_T *argvars, typval_T *rettv)
5688{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005689 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005690}
5691
5692/*
5693 * "matchstrpos()" function
5694 */
5695 static void
5696f_matchstrpos(typval_T *argvars, typval_T *rettv)
5697{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005698 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699}
5700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701 static void
5702max_min(typval_T *argvars, typval_T *rettv, int domax)
5703{
5704 varnumber_T n = 0;
5705 varnumber_T i;
5706 int error = FALSE;
5707
5708 if (argvars[0].v_type == VAR_LIST)
5709 {
5710 list_T *l;
5711 listitem_T *li;
5712
5713 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005714 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005715 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005716 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005718 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5719 n = l->lv_u.nonmat.lv_start;
5720 else
5721 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5722 * l->lv_u.nonmat.lv_stride;
5723 }
5724 else
5725 {
5726 li = l->lv_first;
5727 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005729 n = tv_get_number_chk(&li->li_tv, &error);
5730 for (;;)
5731 {
5732 li = li->li_next;
5733 if (li == NULL)
5734 break;
5735 i = tv_get_number_chk(&li->li_tv, &error);
5736 if (domax ? i > n : i < n)
5737 n = i;
5738 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005739 }
5740 }
5741 }
5742 }
5743 else if (argvars[0].v_type == VAR_DICT)
5744 {
5745 dict_T *d;
5746 int first = TRUE;
5747 hashitem_T *hi;
5748 int todo;
5749
5750 d = argvars[0].vval.v_dict;
5751 if (d != NULL)
5752 {
5753 todo = (int)d->dv_hashtab.ht_used;
5754 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5755 {
5756 if (!HASHITEM_EMPTY(hi))
5757 {
5758 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005759 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005760 if (first)
5761 {
5762 n = i;
5763 first = FALSE;
5764 }
5765 else if (domax ? i > n : i < n)
5766 n = i;
5767 }
5768 }
5769 }
5770 }
5771 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005772 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773 rettv->vval.v_number = error ? 0 : n;
5774}
5775
5776/*
5777 * "max()" function
5778 */
5779 static void
5780f_max(typval_T *argvars, typval_T *rettv)
5781{
5782 max_min(argvars, rettv, TRUE);
5783}
5784
5785/*
5786 * "min()" function
5787 */
5788 static void
5789f_min(typval_T *argvars, typval_T *rettv)
5790{
5791 max_min(argvars, rettv, FALSE);
5792}
5793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794#if defined(FEAT_MZSCHEME) || defined(PROTO)
5795/*
5796 * "mzeval()" function
5797 */
5798 static void
5799f_mzeval(typval_T *argvars, typval_T *rettv)
5800{
5801 char_u *str;
5802 char_u buf[NUMBUFLEN];
5803
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005804 if (check_restricted() || check_secure())
5805 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005806 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005807 do_mzeval(str, rettv);
5808}
5809
5810 void
5811mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5812{
5813 typval_T argvars[3];
5814
5815 argvars[0].v_type = VAR_STRING;
5816 argvars[0].vval.v_string = name;
5817 copy_tv(args, &argvars[1]);
5818 argvars[2].v_type = VAR_UNKNOWN;
5819 f_call(argvars, rettv);
5820 clear_tv(&argvars[1]);
5821}
5822#endif
5823
5824/*
5825 * "nextnonblank()" function
5826 */
5827 static void
5828f_nextnonblank(typval_T *argvars, typval_T *rettv)
5829{
5830 linenr_T lnum;
5831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005832 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005833 {
5834 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5835 {
5836 lnum = 0;
5837 break;
5838 }
5839 if (*skipwhite(ml_get(lnum)) != NUL)
5840 break;
5841 }
5842 rettv->vval.v_number = lnum;
5843}
5844
5845/*
5846 * "nr2char()" function
5847 */
5848 static void
5849f_nr2char(typval_T *argvars, typval_T *rettv)
5850{
5851 char_u buf[NUMBUFLEN];
5852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 if (has_mbyte)
5854 {
5855 int utf8 = 0;
5856
5857 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005858 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005860 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005862 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 }
5864 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005866 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 buf[1] = NUL;
5868 }
5869 rettv->v_type = VAR_STRING;
5870 rettv->vval.v_string = vim_strsave(buf);
5871}
5872
5873/*
5874 * "or(expr, expr)" function
5875 */
5876 static void
5877f_or(typval_T *argvars, typval_T *rettv)
5878{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005879 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5880 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881}
5882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883#ifdef FEAT_PERL
5884/*
5885 * "perleval()" function
5886 */
5887 static void
5888f_perleval(typval_T *argvars, typval_T *rettv)
5889{
5890 char_u *str;
5891 char_u buf[NUMBUFLEN];
5892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005893 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 do_perleval(str, rettv);
5895}
5896#endif
5897
5898#ifdef FEAT_FLOAT
5899/*
5900 * "pow()" function
5901 */
5902 static void
5903f_pow(typval_T *argvars, typval_T *rettv)
5904{
5905 float_T fx = 0.0, fy = 0.0;
5906
5907 rettv->v_type = VAR_FLOAT;
5908 if (get_float_arg(argvars, &fx) == OK
5909 && get_float_arg(&argvars[1], &fy) == OK)
5910 rettv->vval.v_float = pow(fx, fy);
5911 else
5912 rettv->vval.v_float = 0.0;
5913}
5914#endif
5915
5916/*
5917 * "prevnonblank()" function
5918 */
5919 static void
5920f_prevnonblank(typval_T *argvars, typval_T *rettv)
5921{
5922 linenr_T lnum;
5923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005924 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5926 lnum = 0;
5927 else
5928 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5929 --lnum;
5930 rettv->vval.v_number = lnum;
5931}
5932
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005933// This dummy va_list is here because:
5934// - passing a NULL pointer doesn't work when va_list isn't a pointer
5935// - locally in the function results in a "used before set" warning
5936// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005937static va_list ap;
5938
5939/*
5940 * "printf()" function
5941 */
5942 static void
5943f_printf(typval_T *argvars, typval_T *rettv)
5944{
5945 char_u buf[NUMBUFLEN];
5946 int len;
5947 char_u *s;
5948 int saved_did_emsg = did_emsg;
5949 char *fmt;
5950
5951 rettv->v_type = VAR_STRING;
5952 rettv->vval.v_string = NULL;
5953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005954 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005956 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005957 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 if (!did_emsg)
5959 {
5960 s = alloc(len + 1);
5961 if (s != NULL)
5962 {
5963 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005964 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5965 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966 }
5967 }
5968 did_emsg |= saved_did_emsg;
5969}
5970
5971/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005972 * "pum_getpos()" function
5973 */
5974 static void
5975f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5976{
5977 if (rettv_dict_alloc(rettv) != OK)
5978 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005979 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005980}
5981
5982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 * "pumvisible()" function
5984 */
5985 static void
5986f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5987{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 if (pum_visible())
5989 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990}
5991
5992#ifdef FEAT_PYTHON3
5993/*
5994 * "py3eval()" function
5995 */
5996 static void
5997f_py3eval(typval_T *argvars, typval_T *rettv)
5998{
5999 char_u *str;
6000 char_u buf[NUMBUFLEN];
6001
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006002 if (check_restricted() || check_secure())
6003 return;
6004
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006005 if (p_pyx == 0)
6006 p_pyx = 3;
6007
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006008 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009 do_py3eval(str, rettv);
6010}
6011#endif
6012
6013#ifdef FEAT_PYTHON
6014/*
6015 * "pyeval()" function
6016 */
6017 static void
6018f_pyeval(typval_T *argvars, typval_T *rettv)
6019{
6020 char_u *str;
6021 char_u buf[NUMBUFLEN];
6022
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006023 if (check_restricted() || check_secure())
6024 return;
6025
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006026 if (p_pyx == 0)
6027 p_pyx = 2;
6028
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006029 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 do_pyeval(str, rettv);
6031}
6032#endif
6033
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006034#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6035/*
6036 * "pyxeval()" function
6037 */
6038 static void
6039f_pyxeval(typval_T *argvars, typval_T *rettv)
6040{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006041 if (check_restricted() || check_secure())
6042 return;
6043
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006044# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6045 init_pyxversion();
6046 if (p_pyx == 2)
6047 f_pyeval(argvars, rettv);
6048 else
6049 f_py3eval(argvars, rettv);
6050# elif defined(FEAT_PYTHON)
6051 f_pyeval(argvars, rettv);
6052# elif defined(FEAT_PYTHON3)
6053 f_py3eval(argvars, rettv);
6054# endif
6055}
6056#endif
6057
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006058static UINT32_T srand_seed_for_testing = 0;
6059static int srand_seed_for_testing_is_used = FALSE;
6060
6061 static void
6062f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6063{
6064 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006065 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006066 else
6067 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006068 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6069 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006070 }
6071}
6072
6073 static void
6074init_srand(UINT32_T *x)
6075{
6076#ifndef MSWIN
6077 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6078#endif
6079
6080 if (srand_seed_for_testing_is_used)
6081 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006082 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006083 return;
6084 }
6085#ifndef MSWIN
6086 if (dev_urandom_state != FAIL)
6087 {
6088 int fd = open("/dev/urandom", O_RDONLY);
6089 struct {
6090 union {
6091 UINT32_T number;
6092 char bytes[sizeof(UINT32_T)];
6093 } contents;
6094 } buf;
6095
6096 // Attempt reading /dev/urandom.
6097 if (fd == -1)
6098 dev_urandom_state = FAIL;
6099 else
6100 {
6101 buf.contents.number = 0;
6102 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6103 != sizeof(UINT32_T))
6104 dev_urandom_state = FAIL;
6105 else
6106 {
6107 dev_urandom_state = OK;
6108 *x = buf.contents.number;
6109 }
6110 close(fd);
6111 }
6112 }
6113 if (dev_urandom_state != OK)
6114 // Reading /dev/urandom doesn't work, fall back to time().
6115#endif
6116 *x = vim_time();
6117}
6118
6119#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6120#define SPLITMIX32(x, z) ( \
6121 z = (x += 0x9e3779b9), \
6122 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6123 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6124 z ^ (z >> 16) \
6125 )
6126#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6127 result = ROTL(y * 5, 7) * 9; \
6128 t = y << 9; \
6129 z ^= x; \
6130 w ^= y; \
6131 y ^= z, x ^= w; \
6132 z ^= t; \
6133 w = ROTL(w, 11);
6134
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006136 * "rand()" function
6137 */
6138 static void
6139f_rand(typval_T *argvars, typval_T *rettv)
6140{
6141 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006142 static UINT32_T gx, gy, gz, gw;
6143 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006144 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006145 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006146
6147 if (argvars[0].v_type == VAR_UNKNOWN)
6148 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006149 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006150 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006151 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006152 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006153 init_srand(&x);
6154
6155 gx = SPLITMIX32(x, z);
6156 gy = SPLITMIX32(x, z);
6157 gz = SPLITMIX32(x, z);
6158 gw = SPLITMIX32(x, z);
6159 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006160 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006161
6162 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006163 }
6164 else if (argvars[0].v_type == VAR_LIST)
6165 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006166 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006167 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006168 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006169
6170 lx = list_find(l, 0L);
6171 ly = list_find(l, 1L);
6172 lz = list_find(l, 2L);
6173 lw = list_find(l, 3L);
6174 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6175 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6176 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6177 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6178 x = (UINT32_T)lx->li_tv.vval.v_number;
6179 y = (UINT32_T)ly->li_tv.vval.v_number;
6180 z = (UINT32_T)lz->li_tv.vval.v_number;
6181 w = (UINT32_T)lw->li_tv.vval.v_number;
6182
6183 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6184
6185 lx->li_tv.vval.v_number = (varnumber_T)x;
6186 ly->li_tv.vval.v_number = (varnumber_T)y;
6187 lz->li_tv.vval.v_number = (varnumber_T)z;
6188 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006189 }
6190 else
6191 goto theend;
6192
6193 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006194 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006195 return;
6196
6197theend:
6198 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006199 rettv->v_type = VAR_NUMBER;
6200 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006201}
6202
6203/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006204 * "srand()" function
6205 */
6206 static void
6207f_srand(typval_T *argvars, typval_T *rettv)
6208{
6209 UINT32_T x = 0, z;
6210
6211 if (rettv_list_alloc(rettv) == FAIL)
6212 return;
6213 if (argvars[0].v_type == VAR_UNKNOWN)
6214 {
6215 init_srand(&x);
6216 }
6217 else
6218 {
6219 int error = FALSE;
6220
6221 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6222 if (error)
6223 return;
6224 }
6225
6226 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6227 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6228 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6229 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6230}
6231
6232#undef ROTL
6233#undef SPLITMIX32
6234#undef SHUFFLE_XOSHIRO128STARSTAR
6235
6236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 * "range()" function
6238 */
6239 static void
6240f_range(typval_T *argvars, typval_T *rettv)
6241{
6242 varnumber_T start;
6243 varnumber_T end;
6244 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 int error = FALSE;
6246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006247 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248 if (argvars[1].v_type == VAR_UNKNOWN)
6249 {
6250 end = start - 1;
6251 start = 0;
6252 }
6253 else
6254 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006255 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006257 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 }
6259
6260 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006261 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006263 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006265 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006266 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 list_T *list = rettv->vval.v_list;
6269
6270 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006271 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006272 // be called.
6273 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006274 list->lv_u.nonmat.lv_start = start;
6275 list->lv_u.nonmat.lv_end = end;
6276 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006277 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 }
6279}
6280
6281/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006282 * Materialize "list".
6283 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006284 */
6285 void
6286range_list_materialize(list_T *list)
6287{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006288 varnumber_T start = list->lv_u.nonmat.lv_start;
6289 varnumber_T end = list->lv_u.nonmat.lv_end;
6290 int stride = list->lv_u.nonmat.lv_stride;
6291 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006292
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006293 list->lv_first = NULL;
6294 list->lv_u.mat.lv_last = NULL;
6295 list->lv_len = 0;
6296 list->lv_u.mat.lv_idx_item = NULL;
6297 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6298 if (list_append_number(list, (varnumber_T)i) == FAIL)
6299 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300}
6301
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006302/*
6303 * "getreginfo()" function
6304 */
6305 static void
6306f_getreginfo(typval_T *argvars, typval_T *rettv)
6307{
6308 char_u *strregname;
6309 int regname;
6310 char_u buf[NUMBUFLEN + 2];
6311 long reglen = 0;
6312 dict_T *dict;
6313 list_T *list;
6314
6315 if (argvars[0].v_type != VAR_UNKNOWN)
6316 {
6317 strregname = tv_get_string_chk(&argvars[0]);
6318 if (strregname == NULL)
6319 return;
6320 }
6321 else
6322 strregname = get_vim_var_str(VV_REG);
6323
6324 regname = (strregname == NULL ? '"' : *strregname);
6325 if (regname == 0 || regname == '@')
6326 regname = '"';
6327
6328 if (rettv_dict_alloc(rettv) == FAIL)
6329 return;
6330 dict = rettv->vval.v_dict;
6331
6332 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6333 if (list == NULL)
6334 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006335 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006336
6337 buf[0] = NUL;
6338 buf[1] = NUL;
6339 switch (get_reg_type(regname, &reglen))
6340 {
6341 case MLINE: buf[0] = 'V'; break;
6342 case MCHAR: buf[0] = 'v'; break;
6343 case MBLOCK:
6344 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6345 reglen + 1);
6346 break;
6347 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006348 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006349
6350 buf[0] = get_register_name(get_unname_register());
6351 buf[1] = NUL;
6352 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006353 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006354 else
6355 {
6356 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6357
6358 if (item != NULL)
6359 {
6360 item->di_tv.v_type = VAR_SPECIAL;
6361 item->di_tv.vval.v_number = regname == buf[0]
6362 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006363 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006364 }
6365 }
6366}
6367
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006368 static void
6369return_register(int regname, typval_T *rettv)
6370{
6371 char_u buf[2] = {0, 0};
6372
6373 buf[0] = (char_u)regname;
6374 rettv->v_type = VAR_STRING;
6375 rettv->vval.v_string = vim_strsave(buf);
6376}
6377
6378/*
6379 * "reg_executing()" function
6380 */
6381 static void
6382f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6383{
6384 return_register(reg_executing, rettv);
6385}
6386
6387/*
6388 * "reg_recording()" function
6389 */
6390 static void
6391f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6392{
6393 return_register(reg_recording, rettv);
6394}
6395
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 * "rename({from}, {to})" function
6398 */
6399 static void
6400f_rename(typval_T *argvars, typval_T *rettv)
6401{
6402 char_u buf[NUMBUFLEN];
6403
6404 if (check_restricted() || check_secure())
6405 rettv->vval.v_number = -1;
6406 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006407 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6408 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409}
6410
6411/*
6412 * "repeat()" function
6413 */
6414 static void
6415f_repeat(typval_T *argvars, typval_T *rettv)
6416{
6417 char_u *p;
6418 int n;
6419 int slen;
6420 int len;
6421 char_u *r;
6422 int i;
6423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006424 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 if (argvars[0].v_type == VAR_LIST)
6426 {
6427 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6428 while (n-- > 0)
6429 if (list_extend(rettv->vval.v_list,
6430 argvars[0].vval.v_list, NULL) == FAIL)
6431 break;
6432 }
6433 else
6434 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006435 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436 rettv->v_type = VAR_STRING;
6437 rettv->vval.v_string = NULL;
6438
6439 slen = (int)STRLEN(p);
6440 len = slen * n;
6441 if (len <= 0)
6442 return;
6443
6444 r = alloc(len + 1);
6445 if (r != NULL)
6446 {
6447 for (i = 0; i < n; i++)
6448 mch_memmove(r + i * slen, p, (size_t)slen);
6449 r[len] = NUL;
6450 }
6451
6452 rettv->vval.v_string = r;
6453 }
6454}
6455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456#define SP_NOMOVE 0x01 // don't move cursor
6457#define SP_REPEAT 0x02 // repeat to find outer pair
6458#define SP_RETCOUNT 0x04 // return matchcount
6459#define SP_SETPCMARK 0x08 // set previous context mark
6460#define SP_START 0x10 // accept match at start position
6461#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6462#define SP_END 0x40 // leave cursor at end of match
6463#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465/*
6466 * Get flags for a search function.
6467 * Possibly sets "p_ws".
6468 * Returns BACKWARD, FORWARD or zero (for an error).
6469 */
6470 static int
6471get_search_arg(typval_T *varp, int *flagsp)
6472{
6473 int dir = FORWARD;
6474 char_u *flags;
6475 char_u nbuf[NUMBUFLEN];
6476 int mask;
6477
6478 if (varp->v_type != VAR_UNKNOWN)
6479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006480 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 while (*flags != NUL)
6484 {
6485 switch (*flags)
6486 {
6487 case 'b': dir = BACKWARD; break;
6488 case 'w': p_ws = TRUE; break;
6489 case 'W': p_ws = FALSE; break;
6490 default: mask = 0;
6491 if (flagsp != NULL)
6492 switch (*flags)
6493 {
6494 case 'c': mask = SP_START; break;
6495 case 'e': mask = SP_END; break;
6496 case 'm': mask = SP_RETCOUNT; break;
6497 case 'n': mask = SP_NOMOVE; break;
6498 case 'p': mask = SP_SUBPAT; break;
6499 case 'r': mask = SP_REPEAT; break;
6500 case 's': mask = SP_SETPCMARK; break;
6501 case 'z': mask = SP_COLUMN; break;
6502 }
6503 if (mask == 0)
6504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006505 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 dir = 0;
6507 }
6508 else
6509 *flagsp |= mask;
6510 }
6511 if (dir == 0)
6512 break;
6513 ++flags;
6514 }
6515 }
6516 return dir;
6517}
6518
6519/*
6520 * Shared by search() and searchpos() functions.
6521 */
6522 static int
6523search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6524{
6525 int flags;
6526 char_u *pat;
6527 pos_T pos;
6528 pos_T save_cursor;
6529 int save_p_ws = p_ws;
6530 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006531 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006534 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 long time_limit = 0;
6536#endif
6537 int options = SEARCH_KEEP;
6538 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006539 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006540 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006541 pos_T firstpos;
6542
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006543 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006544 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545 if (dir == 0)
6546 goto theend;
6547 flags = *flagsp;
6548 if (flags & SP_START)
6549 options |= SEARCH_START;
6550 if (flags & SP_END)
6551 options |= SEARCH_END;
6552 if (flags & SP_COLUMN)
6553 options |= SEARCH_COL;
6554
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006555 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006558 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559 if (lnum_stop < 0)
6560 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006561 if (argvars[3].v_type != VAR_UNKNOWN)
6562 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006563#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006564 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 if (time_limit < 0)
6566 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006568 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006569 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 }
6571
6572#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006573 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006574 profile_setlimit(time_limit, &tm);
6575#endif
6576
6577 /*
6578 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6579 * Check to make sure only those flags are set.
6580 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6581 * flags cannot be set. Check for that condition also.
6582 */
6583 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6584 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006586 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 goto theend;
6588 }
6589
6590 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006591 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006592 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006593 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6594#ifdef FEAT_RELTIME
6595 sia.sa_tm = &tm;
6596#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006597
6598 // Repeat until {skip} returns FALSE.
6599 for (;;)
6600 {
6601 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006602 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006603 // finding the first match again means there is no match where {skip}
6604 // evaluates to zero.
6605 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6606 subpatnum = FAIL;
6607
Bram Moolenaara9c01042020-06-07 14:50:50 +02006608 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006609 // didn't find it or no skip argument
6610 break;
6611 firstpos = pos;
6612
Bram Moolenaara9c01042020-06-07 14:50:50 +02006613 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006614 {
6615 int do_skip;
6616 int err;
6617 pos_T save_pos = curwin->w_cursor;
6618
6619 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006620 err = FALSE;
6621 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006622 curwin->w_cursor = save_pos;
6623 if (err)
6624 {
6625 // Evaluating {skip} caused an error, break here.
6626 subpatnum = FAIL;
6627 break;
6628 }
6629 if (!do_skip)
6630 break;
6631 }
6632 }
6633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 if (subpatnum != FAIL)
6635 {
6636 if (flags & SP_SUBPAT)
6637 retval = subpatnum;
6638 else
6639 retval = pos.lnum;
6640 if (flags & SP_SETPCMARK)
6641 setpcmark();
6642 curwin->w_cursor = pos;
6643 if (match_pos != NULL)
6644 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 match_pos->lnum = pos.lnum;
6647 match_pos->col = pos.col + 1;
6648 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // "/$" will put the cursor after the end of the line, may need to
6650 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 check_cursor();
6652 }
6653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006654 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655 if (flags & SP_NOMOVE)
6656 curwin->w_cursor = save_cursor;
6657 else
6658 curwin->w_set_curswant = TRUE;
6659theend:
6660 p_ws = save_p_ws;
6661
6662 return retval;
6663}
6664
6665#ifdef FEAT_FLOAT
6666
6667/*
6668 * round() is not in C90, use ceil() or floor() instead.
6669 */
6670 float_T
6671vim_round(float_T f)
6672{
6673 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6674}
6675
6676/*
6677 * "round({float})" function
6678 */
6679 static void
6680f_round(typval_T *argvars, typval_T *rettv)
6681{
6682 float_T f = 0.0;
6683
6684 rettv->v_type = VAR_FLOAT;
6685 if (get_float_arg(argvars, &f) == OK)
6686 rettv->vval.v_float = vim_round(f);
6687 else
6688 rettv->vval.v_float = 0.0;
6689}
6690#endif
6691
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006692#ifdef FEAT_RUBY
6693/*
6694 * "rubyeval()" function
6695 */
6696 static void
6697f_rubyeval(typval_T *argvars, typval_T *rettv)
6698{
6699 char_u *str;
6700 char_u buf[NUMBUFLEN];
6701
6702 str = tv_get_string_buf(&argvars[0], buf);
6703 do_rubyeval(str, rettv);
6704}
6705#endif
6706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707/*
6708 * "screenattr()" function
6709 */
6710 static void
6711f_screenattr(typval_T *argvars, typval_T *rettv)
6712{
6713 int row;
6714 int col;
6715 int c;
6716
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006717 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6718 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006719 if (row < 0 || row >= screen_Rows
6720 || col < 0 || col >= screen_Columns)
6721 c = -1;
6722 else
6723 c = ScreenAttrs[LineOffset[row] + col];
6724 rettv->vval.v_number = c;
6725}
6726
6727/*
6728 * "screenchar()" function
6729 */
6730 static void
6731f_screenchar(typval_T *argvars, typval_T *rettv)
6732{
6733 int row;
6734 int col;
6735 int off;
6736 int c;
6737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006738 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6739 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006740 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006741 c = -1;
6742 else
6743 {
6744 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 if (enc_utf8 && ScreenLinesUC[off] != 0)
6746 c = ScreenLinesUC[off];
6747 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748 c = ScreenLines[off];
6749 }
6750 rettv->vval.v_number = c;
6751}
6752
6753/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006754 * "screenchars()" function
6755 */
6756 static void
6757f_screenchars(typval_T *argvars, typval_T *rettv)
6758{
6759 int row;
6760 int col;
6761 int off;
6762 int c;
6763 int i;
6764
6765 if (rettv_list_alloc(rettv) == FAIL)
6766 return;
6767 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6768 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6769 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6770 return;
6771
6772 off = LineOffset[row] + col;
6773 if (enc_utf8 && ScreenLinesUC[off] != 0)
6774 c = ScreenLinesUC[off];
6775 else
6776 c = ScreenLines[off];
6777 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6778
6779 if (enc_utf8)
6780
6781 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6782 list_append_number(rettv->vval.v_list,
6783 (varnumber_T)ScreenLinesC[i][off]);
6784}
6785
6786/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 * "screencol()" function
6788 *
6789 * First column is 1 to be consistent with virtcol().
6790 */
6791 static void
6792f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6793{
6794 rettv->vval.v_number = screen_screencol() + 1;
6795}
6796
6797/*
6798 * "screenrow()" function
6799 */
6800 static void
6801f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6802{
6803 rettv->vval.v_number = screen_screenrow() + 1;
6804}
6805
6806/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006807 * "screenstring()" function
6808 */
6809 static void
6810f_screenstring(typval_T *argvars, typval_T *rettv)
6811{
6812 int row;
6813 int col;
6814 int off;
6815 int c;
6816 int i;
6817 char_u buf[MB_MAXBYTES + 1];
6818 int buflen = 0;
6819
6820 rettv->vval.v_string = NULL;
6821 rettv->v_type = VAR_STRING;
6822
6823 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6824 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6825 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6826 return;
6827
6828 off = LineOffset[row] + col;
6829 if (enc_utf8 && ScreenLinesUC[off] != 0)
6830 c = ScreenLinesUC[off];
6831 else
6832 c = ScreenLines[off];
6833 buflen += mb_char2bytes(c, buf);
6834
6835 if (enc_utf8)
6836 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6837 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6838
6839 buf[buflen] = NUL;
6840 rettv->vval.v_string = vim_strsave(buf);
6841}
6842
6843/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 * "search()" function
6845 */
6846 static void
6847f_search(typval_T *argvars, typval_T *rettv)
6848{
6849 int flags = 0;
6850
6851 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6852}
6853
6854/*
6855 * "searchdecl()" function
6856 */
6857 static void
6858f_searchdecl(typval_T *argvars, typval_T *rettv)
6859{
6860 int locally = 1;
6861 int thisblock = 0;
6862 int error = FALSE;
6863 char_u *name;
6864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006865 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006867 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 if (argvars[1].v_type != VAR_UNKNOWN)
6869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006870 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006871 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 }
6874 if (!error && name != NULL)
6875 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6876 locally, thisblock, SEARCH_KEEP) == FAIL;
6877}
6878
6879/*
6880 * Used by searchpair() and searchpairpos()
6881 */
6882 static int
6883searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6884{
6885 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006886 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 int save_p_ws = p_ws;
6888 int dir;
6889 int flags = 0;
6890 char_u nbuf1[NUMBUFLEN];
6891 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006892 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 long lnum_stop = 0;
6894 long time_limit = 0;
6895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006896 // Get the three pattern arguments: start, middle, end. Will result in an
6897 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 spat = tv_get_string_chk(&argvars[0]);
6899 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6900 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006902 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006904 // Handle the optional fourth argument: flags
6905 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 if (dir == 0)
6907 goto theend;
6908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006909 // Don't accept SP_END or SP_SUBPAT.
6910 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 if ((flags & (SP_END | SP_SUBPAT)) != 0
6912 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006914 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 goto theend;
6916 }
6917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006918 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 if (flags & SP_REPEAT)
6920 p_ws = FALSE;
6921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006922 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 if (argvars[3].v_type == VAR_UNKNOWN
6924 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006925 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926 else
6927 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006928 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006929 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if (argvars[5].v_type != VAR_UNKNOWN)
6932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006933 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006936 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006937 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939#ifdef FEAT_RELTIME
6940 if (argvars[6].v_type != VAR_UNKNOWN)
6941 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006945 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006947 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 }
6949#endif
6950 }
6951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952
6953 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6954 match_pos, lnum_stop, time_limit);
6955
6956theend:
6957 p_ws = save_p_ws;
6958
6959 return retval;
6960}
6961
6962/*
6963 * "searchpair()" function
6964 */
6965 static void
6966f_searchpair(typval_T *argvars, typval_T *rettv)
6967{
6968 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6969}
6970
6971/*
6972 * "searchpairpos()" function
6973 */
6974 static void
6975f_searchpairpos(typval_T *argvars, typval_T *rettv)
6976{
6977 pos_T match_pos;
6978 int lnum = 0;
6979 int col = 0;
6980
6981 if (rettv_list_alloc(rettv) == FAIL)
6982 return;
6983
6984 if (searchpair_cmn(argvars, &match_pos) > 0)
6985 {
6986 lnum = match_pos.lnum;
6987 col = match_pos.col;
6988 }
6989
6990 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6991 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6992}
6993
6994/*
6995 * Search for a start/middle/end thing.
6996 * Used by searchpair(), see its documentation for the details.
6997 * Returns 0 or -1 for no match,
6998 */
6999 long
7000do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007001 char_u *spat, // start pattern
7002 char_u *mpat, // middle pattern
7003 char_u *epat, // end pattern
7004 int dir, // BACKWARD or FORWARD
7005 typval_T *skip, // skip expression
7006 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007008 linenr_T lnum_stop, // stop at this line if not zero
7009 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010{
7011 char_u *save_cpo;
7012 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7013 long retval = 0;
7014 pos_T pos;
7015 pos_T firstpos;
7016 pos_T foundpos;
7017 pos_T save_cursor;
7018 pos_T save_pos;
7019 int n;
7020 int r;
7021 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007022 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 int err;
7024 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007025#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007027#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007029 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 save_cpo = p_cpo;
7031 p_cpo = empty_option;
7032
7033#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007034 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 profile_setlimit(time_limit, &tm);
7036#endif
7037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007038 // Make two search patterns: start/end (pat2, for in nested pairs) and
7039 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007040 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7041 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (pat2 == NULL || pat3 == NULL)
7043 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007044 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 if (*mpat == NUL)
7046 STRCPY(pat3, pat2);
7047 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007048 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 spat, epat, mpat);
7050 if (flags & SP_START)
7051 options |= SEARCH_START;
7052
Bram Moolenaar48570482017-10-30 21:48:41 +01007053 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007054 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056 save_cursor = curwin->w_cursor;
7057 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007058 CLEAR_POS(&firstpos);
7059 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 pat = pat3;
7061 for (;;)
7062 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007063 searchit_arg_T sia;
7064
Bram Moolenaara80faa82020-04-12 19:37:17 +02007065 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007066 sia.sa_stop_lnum = lnum_stop;
7067#ifdef FEAT_RELTIME
7068 sia.sa_tm = &tm;
7069#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007070 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007071 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007072 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007073 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 break;
7075
7076 if (firstpos.lnum == 0)
7077 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007078 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007080 // Found the same position again. Can happen with a pattern that
7081 // has "\zs" at the end and searching backwards. Advance one
7082 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 if (dir == BACKWARD)
7084 decl(&pos);
7085 else
7086 incl(&pos);
7087 }
7088 foundpos = pos;
7089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007090 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091 options &= ~SEARCH_START;
7092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007093 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007094 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 {
7096 save_pos = curwin->w_cursor;
7097 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007098 err = FALSE;
7099 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 curwin->w_cursor = save_pos;
7101 if (err)
7102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007103 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 curwin->w_cursor = save_cursor;
7105 retval = -1;
7106 break;
7107 }
7108 if (r)
7109 continue;
7110 }
7111
7112 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // Found end when searching backwards or start when searching
7115 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007117 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 }
7119 else
7120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007121 // Found end when searching forward or start when searching
7122 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007124 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 }
7126
7127 if (nest == 0)
7128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007129 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 if (flags & SP_RETCOUNT)
7131 ++retval;
7132 else
7133 retval = pos.lnum;
7134 if (flags & SP_SETPCMARK)
7135 setpcmark();
7136 curwin->w_cursor = pos;
7137 if (!(flags & SP_REPEAT))
7138 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007139 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140 }
7141 }
7142
7143 if (match_pos != NULL)
7144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007145 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 match_pos->lnum = curwin->w_cursor.lnum;
7147 match_pos->col = curwin->w_cursor.col + 1;
7148 }
7149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007150 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151 if ((flags & SP_NOMOVE) || retval == 0)
7152 curwin->w_cursor = save_cursor;
7153
7154theend:
7155 vim_free(pat2);
7156 vim_free(pat3);
7157 if (p_cpo == empty_option)
7158 p_cpo = save_cpo;
7159 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007160 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 free_string_option(save_cpo);
7162
7163 return retval;
7164}
7165
7166/*
7167 * "searchpos()" function
7168 */
7169 static void
7170f_searchpos(typval_T *argvars, typval_T *rettv)
7171{
7172 pos_T match_pos;
7173 int lnum = 0;
7174 int col = 0;
7175 int n;
7176 int flags = 0;
7177
7178 if (rettv_list_alloc(rettv) == FAIL)
7179 return;
7180
7181 n = search_cmn(argvars, &match_pos, &flags);
7182 if (n > 0)
7183 {
7184 lnum = match_pos.lnum;
7185 col = match_pos.col;
7186 }
7187
7188 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7189 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7190 if (flags & SP_SUBPAT)
7191 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7192}
7193
7194 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007195f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7196{
7197 dict_T *d;
7198 dictitem_T *di;
7199 char_u *csearch;
7200
7201 if (argvars[0].v_type != VAR_DICT)
7202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007203 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 return;
7205 }
7206
7207 if ((d = argvars[0].vval.v_dict) != NULL)
7208 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007209 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210 if (csearch != NULL)
7211 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007212 if (enc_utf8)
7213 {
7214 int pcc[MAX_MCO];
7215 int c = utfc_ptr2char(csearch, pcc);
7216
7217 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7218 }
7219 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007221 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 }
7223
7224 di = dict_find(d, (char_u *)"forward", -1);
7225 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007226 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 ? FORWARD : BACKWARD);
7228
7229 di = dict_find(d, (char_u *)"until", -1);
7230 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007231 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 }
7233}
7234
7235/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007236 * "setenv()" function
7237 */
7238 static void
7239f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7240{
7241 char_u namebuf[NUMBUFLEN];
7242 char_u valbuf[NUMBUFLEN];
7243 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7244
7245 if (argvars[1].v_type == VAR_SPECIAL
7246 && argvars[1].vval.v_number == VVAL_NULL)
7247 vim_unsetenv(name);
7248 else
7249 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7250}
7251
7252/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 * "setfperm({fname}, {mode})" function
7254 */
7255 static void
7256f_setfperm(typval_T *argvars, typval_T *rettv)
7257{
7258 char_u *fname;
7259 char_u modebuf[NUMBUFLEN];
7260 char_u *mode_str;
7261 int i;
7262 int mask;
7263 int mode = 0;
7264
7265 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 if (fname == NULL)
7268 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007269 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 if (mode_str == NULL)
7271 return;
7272 if (STRLEN(mode_str) != 9)
7273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007274 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 return;
7276 }
7277
7278 mask = 1;
7279 for (i = 8; i >= 0; --i)
7280 {
7281 if (mode_str[i] != '-')
7282 mode |= mask;
7283 mask = mask << 1;
7284 }
7285 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7286}
7287
7288/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 * "setpos()" function
7290 */
7291 static void
7292f_setpos(typval_T *argvars, typval_T *rettv)
7293{
7294 pos_T pos;
7295 int fnum;
7296 char_u *name;
7297 colnr_T curswant = -1;
7298
7299 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007300 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 if (name != NULL)
7302 {
7303 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7304 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007305 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 pos.col = 0;
7307 if (name[0] == '.' && name[1] == NUL)
7308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007309 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007310 curwin->w_cursor = pos;
7311 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007312 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007313 curwin->w_curswant = curswant - 1;
7314 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007316 check_cursor();
7317 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 }
7319 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007321 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322 if (setmark_pos(name[1], &pos, fnum) == OK)
7323 rettv->vval.v_number = 0;
7324 }
7325 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 }
7328 }
7329}
7330
7331/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007332 * Translate a register type string to the yank type and block length
7333 */
7334 static int
7335get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7336{
7337 char_u *stropt = *pp;
7338 switch (*stropt)
7339 {
7340 case 'v': case 'c': // character-wise selection
7341 *yank_type = MCHAR;
7342 break;
7343 case 'V': case 'l': // line-wise selection
7344 *yank_type = MLINE;
7345 break;
7346 case 'b': case Ctrl_V: // block-wise selection
7347 *yank_type = MBLOCK;
7348 if (VIM_ISDIGIT(stropt[1]))
7349 {
7350 ++stropt;
7351 *block_len = getdigits(&stropt) - 1;
7352 --stropt;
7353 }
7354 break;
7355 default:
7356 return FAIL;
7357 }
7358 *pp = stropt;
7359 return OK;
7360}
7361
7362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 * "setreg()" function
7364 */
7365 static void
7366f_setreg(typval_T *argvars, typval_T *rettv)
7367{
7368 int regname;
7369 char_u *strregname;
7370 char_u *stropt;
7371 char_u *strval;
7372 int append;
7373 char_u yank_type;
7374 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007375 typval_T *regcontents;
7376 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007378 pointreg = 0;
7379 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 block_len = -1;
7381 yank_type = MAUTO;
7382 append = FALSE;
7383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007384 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007385 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386
7387 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007388 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389 regname = *strregname;
7390 if (regname == 0 || regname == '@')
7391 regname = '"';
7392
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007393 if (argvars[1].v_type == VAR_DICT)
7394 {
7395 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007396 dictitem_T *di;
7397
7398 if (d == NULL || d->dv_hashtab.ht_used == 0)
7399 {
7400 // Empty dict, clear the register (like setreg(0, []))
7401 char_u *lstval[2] = {NULL, NULL};
7402 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7403 return;
7404 }
7405
7406 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007407 if (di != NULL)
7408 regcontents = &di->di_tv;
7409
7410 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7411 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007412 {
7413 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7414
7415 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007416 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007417 semsg(_(e_invargval), "value");
7418 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007419 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007420 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007421
7422 if (regname == '"')
7423 {
7424 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7425 if (stropt != NULL)
7426 {
7427 pointreg = *stropt;
7428 regname = pointreg;
7429 }
7430 }
7431 else if (dict_get_number(d, (char_u *)"isunnamed"))
7432 pointreg = regname;
7433 }
7434 else
7435 regcontents = &argvars[1];
7436
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 if (argvars[2].v_type != VAR_UNKNOWN)
7438 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007439 if (yank_type != MAUTO)
7440 {
7441 semsg(_(e_toomanyarg), "setreg");
7442 return;
7443 }
7444
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007445 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007447 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448 for (; *stropt != NUL; ++stropt)
7449 switch (*stropt)
7450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007451 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 append = TRUE;
7453 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007454 default:
7455 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 }
7457 }
7458
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007459 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460 {
7461 char_u **lstval;
7462 char_u **allocval;
7463 char_u buf[NUMBUFLEN];
7464 char_u **curval;
7465 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007466 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 listitem_T *li;
7468 int len;
7469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 len = ll == NULL ? 0 : ll->lv_len;
7472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007473 // First half: use for pointers to result lines; second half: use for
7474 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007475 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 if (lstval == NULL)
7477 return;
7478 curval = lstval;
7479 allocval = lstval + len + 2;
7480 curallocval = allocval;
7481
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007482 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007484 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007485 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007487 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488 if (strval == NULL)
7489 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007490 if (strval == buf)
7491 {
7492 // Need to make a copy, next tv_get_string_buf_chk() will
7493 // overwrite the string.
7494 strval = vim_strsave(buf);
7495 if (strval == NULL)
7496 goto free_lstval;
7497 *curallocval++ = strval;
7498 }
7499 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501 }
7502 *curval++ = NULL;
7503
7504 write_reg_contents_lst(regname, lstval, -1,
7505 append, yank_type, block_len);
7506free_lstval:
7507 while (curallocval > allocval)
7508 vim_free(*--curallocval);
7509 vim_free(lstval);
7510 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007511 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007513 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 if (strval == NULL)
7515 return;
7516 write_reg_contents_ex(regname, strval, -1,
7517 append, yank_type, block_len);
7518 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007519 if (pointreg != 0)
7520 get_yank_register(pointreg, TRUE);
7521
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 rettv->vval.v_number = 0;
7523}
7524
7525/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007526 * "settagstack()" function
7527 */
7528 static void
7529f_settagstack(typval_T *argvars, typval_T *rettv)
7530{
7531 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7532 win_T *wp;
7533 dict_T *d;
7534 int action = 'r';
7535
7536 rettv->vval.v_number = -1;
7537
7538 // first argument: window number or id
7539 wp = find_win_by_nr_or_id(&argvars[0]);
7540 if (wp == NULL)
7541 return;
7542
7543 // second argument: dict with items to set in the tag stack
7544 if (argvars[1].v_type != VAR_DICT)
7545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007547 return;
7548 }
7549 d = argvars[1].vval.v_dict;
7550 if (d == NULL)
7551 return;
7552
7553 // third argument: action - 'a' for append and 'r' for replace.
7554 // default is to replace the stack.
7555 if (argvars[2].v_type == VAR_UNKNOWN)
7556 action = 'r';
7557 else if (argvars[2].v_type == VAR_STRING)
7558 {
7559 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007560 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007561 if (actstr == NULL)
7562 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007563 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7564 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007565 action = *actstr;
7566 else
7567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007568 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007569 return;
7570 }
7571 }
7572 else
7573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007575 return;
7576 }
7577
7578 if (set_tagstack(wp, d, action) == OK)
7579 rettv->vval.v_number = 0;
7580}
7581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007582#ifdef FEAT_CRYPT
7583/*
7584 * "sha256({string})" function
7585 */
7586 static void
7587f_sha256(typval_T *argvars, typval_T *rettv)
7588{
7589 char_u *p;
7590
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007591 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007592 rettv->vval.v_string = vim_strsave(
7593 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7594 rettv->v_type = VAR_STRING;
7595}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007596#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597
7598/*
7599 * "shellescape({string})" function
7600 */
7601 static void
7602f_shellescape(typval_T *argvars, typval_T *rettv)
7603{
Bram Moolenaar20615522017-06-05 18:46:26 +02007604 int do_special = non_zero_arg(&argvars[1]);
7605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007607 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 rettv->v_type = VAR_STRING;
7609}
7610
7611/*
7612 * shiftwidth() function
7613 */
7614 static void
7615f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7616{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007617 rettv->vval.v_number = 0;
7618
7619 if (argvars[0].v_type != VAR_UNKNOWN)
7620 {
7621 long col;
7622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007623 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007624 if (col < 0)
7625 return; // type error; errmsg already given
7626#ifdef FEAT_VARTABS
7627 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7628 return;
7629#endif
7630 }
7631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632 rettv->vval.v_number = get_sw_value(curbuf);
7633}
7634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635#ifdef FEAT_FLOAT
7636/*
7637 * "sin()" function
7638 */
7639 static void
7640f_sin(typval_T *argvars, typval_T *rettv)
7641{
7642 float_T f = 0.0;
7643
7644 rettv->v_type = VAR_FLOAT;
7645 if (get_float_arg(argvars, &f) == OK)
7646 rettv->vval.v_float = sin(f);
7647 else
7648 rettv->vval.v_float = 0.0;
7649}
7650
7651/*
7652 * "sinh()" function
7653 */
7654 static void
7655f_sinh(typval_T *argvars, typval_T *rettv)
7656{
7657 float_T f = 0.0;
7658
7659 rettv->v_type = VAR_FLOAT;
7660 if (get_float_arg(argvars, &f) == OK)
7661 rettv->vval.v_float = sinh(f);
7662 else
7663 rettv->vval.v_float = 0.0;
7664}
7665#endif
7666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667/*
7668 * "soundfold({word})" function
7669 */
7670 static void
7671f_soundfold(typval_T *argvars, typval_T *rettv)
7672{
7673 char_u *s;
7674
7675 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677#ifdef FEAT_SPELL
7678 rettv->vval.v_string = eval_soundfold(s);
7679#else
7680 rettv->vval.v_string = vim_strsave(s);
7681#endif
7682}
7683
7684/*
7685 * "spellbadword()" function
7686 */
7687 static void
7688f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7689{
7690 char_u *word = (char_u *)"";
7691 hlf_T attr = HLF_COUNT;
7692 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007693#ifdef FEAT_SPELL
7694 int wo_spell_save = curwin->w_p_spell;
7695
7696 if (!curwin->w_p_spell)
7697 {
7698 did_set_spelllang(curwin);
7699 curwin->w_p_spell = TRUE;
7700 }
7701
7702 if (*curwin->w_s->b_p_spl == NUL)
7703 {
7704 emsg(_(e_no_spell));
7705 curwin->w_p_spell = wo_spell_save;
7706 return;
7707 }
7708#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709
7710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007711 {
7712#ifdef FEAT_SPELL
7713 curwin->w_p_spell = wo_spell_save;
7714#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007716 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717
7718#ifdef FEAT_SPELL
7719 if (argvars[0].v_type == VAR_UNKNOWN)
7720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007721 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007722 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7723 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007724 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007726 curwin->w_set_curswant = TRUE;
7727 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007728 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007729 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007731 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732 int capcol = -1;
7733
7734 if (str != NULL)
7735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007736 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737 while (*str != NUL)
7738 {
7739 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7740 if (attr != HLF_COUNT)
7741 {
7742 word = str;
7743 break;
7744 }
7745 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007746 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007747 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748 }
7749 }
7750 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007751 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752#endif
7753
7754 list_append_string(rettv->vval.v_list, word, len);
7755 list_append_string(rettv->vval.v_list, (char_u *)(
7756 attr == HLF_SPB ? "bad" :
7757 attr == HLF_SPR ? "rare" :
7758 attr == HLF_SPL ? "local" :
7759 attr == HLF_SPC ? "caps" :
7760 ""), -1);
7761}
7762
7763/*
7764 * "spellsuggest()" function
7765 */
7766 static void
7767f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7768{
7769#ifdef FEAT_SPELL
7770 char_u *str;
7771 int typeerr = FALSE;
7772 int maxcount;
7773 garray_T ga;
7774 int i;
7775 listitem_T *li;
7776 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007777 int wo_spell_save = curwin->w_p_spell;
7778
7779 if (!curwin->w_p_spell)
7780 {
7781 did_set_spelllang(curwin);
7782 curwin->w_p_spell = TRUE;
7783 }
7784
7785 if (*curwin->w_s->b_p_spl == NUL)
7786 {
7787 emsg(_(e_no_spell));
7788 curwin->w_p_spell = wo_spell_save;
7789 return;
7790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791#endif
7792
7793 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007794 {
7795#ifdef FEAT_SPELL
7796 curwin->w_p_spell = wo_spell_save;
7797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007799 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800
7801#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007802 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007804 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007805 if (argvars[1].v_type != VAR_UNKNOWN)
7806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007807 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 if (maxcount <= 0)
7809 return;
7810 if (argvars[2].v_type != VAR_UNKNOWN)
7811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007812 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 if (typeerr)
7814 return;
7815 }
7816 }
7817 else
7818 maxcount = 25;
7819
7820 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7821
7822 for (i = 0; i < ga.ga_len; ++i)
7823 {
7824 str = ((char_u **)ga.ga_data)[i];
7825
7826 li = listitem_alloc();
7827 if (li == NULL)
7828 vim_free(str);
7829 else
7830 {
7831 li->li_tv.v_type = VAR_STRING;
7832 li->li_tv.v_lock = 0;
7833 li->li_tv.vval.v_string = str;
7834 list_append(rettv->vval.v_list, li);
7835 }
7836 }
7837 ga_clear(&ga);
7838 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007839 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840#endif
7841}
7842
7843 static void
7844f_split(typval_T *argvars, typval_T *rettv)
7845{
7846 char_u *str;
7847 char_u *end;
7848 char_u *pat = NULL;
7849 regmatch_T regmatch;
7850 char_u patbuf[NUMBUFLEN];
7851 char_u *save_cpo;
7852 int match;
7853 colnr_T col = 0;
7854 int keepempty = FALSE;
7855 int typeerr = FALSE;
7856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007857 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 save_cpo = p_cpo;
7859 p_cpo = (char_u *)"";
7860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007861 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 if (argvars[1].v_type != VAR_UNKNOWN)
7863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007864 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865 if (pat == NULL)
7866 typeerr = TRUE;
7867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007868 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 }
7870 if (pat == NULL || *pat == NUL)
7871 pat = (char_u *)"[\\x01- ]\\+";
7872
7873 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007874 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007876 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877
7878 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7879 if (regmatch.regprog != NULL)
7880 {
7881 regmatch.rm_ic = FALSE;
7882 while (*str != NUL || keepempty)
7883 {
7884 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007885 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 else
7887 match = vim_regexec_nl(&regmatch, str, col);
7888 if (match)
7889 end = regmatch.startp[0];
7890 else
7891 end = str + STRLEN(str);
7892 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7893 && *str != NUL && match && end < regmatch.endp[0]))
7894 {
7895 if (list_append_string(rettv->vval.v_list, str,
7896 (int)(end - str)) == FAIL)
7897 break;
7898 }
7899 if (!match)
7900 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007901 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 if (regmatch.endp[0] > str)
7903 col = 0;
7904 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007905 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 str = regmatch.endp[0];
7908 }
7909
7910 vim_regfree(regmatch.regprog);
7911 }
7912
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007913theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914 p_cpo = save_cpo;
7915}
7916
7917#ifdef FEAT_FLOAT
7918/*
7919 * "sqrt()" function
7920 */
7921 static void
7922f_sqrt(typval_T *argvars, typval_T *rettv)
7923{
7924 float_T f = 0.0;
7925
7926 rettv->v_type = VAR_FLOAT;
7927 if (get_float_arg(argvars, &f) == OK)
7928 rettv->vval.v_float = sqrt(f);
7929 else
7930 rettv->vval.v_float = 0.0;
7931}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007932#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007934#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936 * "str2float()" function
7937 */
7938 static void
7939f_str2float(typval_T *argvars, typval_T *rettv)
7940{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007941 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007942 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943
Bram Moolenaar08243d22017-01-10 16:12:29 +01007944 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945 p = skipwhite(p + 1);
7946 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007947 if (isneg)
7948 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 rettv->v_type = VAR_FLOAT;
7950}
7951#endif
7952
7953/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007954 * "str2list()" function
7955 */
7956 static void
7957f_str2list(typval_T *argvars, typval_T *rettv)
7958{
7959 char_u *p;
7960 int utf8 = FALSE;
7961
7962 if (rettv_list_alloc(rettv) == FAIL)
7963 return;
7964
7965 if (argvars[1].v_type != VAR_UNKNOWN)
7966 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7967
7968 p = tv_get_string(&argvars[0]);
7969
7970 if (has_mbyte || utf8)
7971 {
7972 int (*ptr2len)(char_u *);
7973 int (*ptr2char)(char_u *);
7974
7975 if (utf8 || enc_utf8)
7976 {
7977 ptr2len = utf_ptr2len;
7978 ptr2char = utf_ptr2char;
7979 }
7980 else
7981 {
7982 ptr2len = mb_ptr2len;
7983 ptr2char = mb_ptr2char;
7984 }
7985
7986 for ( ; *p != NUL; p += (*ptr2len)(p))
7987 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7988 }
7989 else
7990 for ( ; *p != NUL; ++p)
7991 list_append_number(rettv->vval.v_list, *p);
7992}
7993
7994/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 * "str2nr()" function
7996 */
7997 static void
7998f_str2nr(typval_T *argvars, typval_T *rettv)
7999{
8000 int base = 10;
8001 char_u *p;
8002 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008003 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008004 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005
8006 if (argvars[1].v_type != VAR_UNKNOWN)
8007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008008 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 if (base != 2 && base != 8 && base != 10 && base != 16)
8010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008011 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 return;
8013 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008014 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8015 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 }
8017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008018 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008019 isneg = (*p == '-');
8020 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 p = skipwhite(p + 1);
8022 switch (base)
8023 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008024 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008025 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008026 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008028 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8029 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008030 if (isneg)
8031 rettv->vval.v_number = -n;
8032 else
8033 rettv->vval.v_number = n;
8034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035}
8036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037/*
8038 * "strgetchar()" function
8039 */
8040 static void
8041f_strgetchar(typval_T *argvars, typval_T *rettv)
8042{
8043 char_u *str;
8044 int len;
8045 int error = FALSE;
8046 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008047 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048
8049 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008050 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 if (str == NULL)
8052 return;
8053 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008054 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 if (error)
8056 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057
Bram Moolenaar13505972019-01-24 15:04:48 +01008058 while (charidx >= 0 && byteidx < len)
8059 {
8060 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008062 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8063 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008065 --charidx;
8066 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008067 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008068}
8069
8070/*
8071 * "stridx()" function
8072 */
8073 static void
8074f_stridx(typval_T *argvars, typval_T *rettv)
8075{
8076 char_u buf[NUMBUFLEN];
8077 char_u *needle;
8078 char_u *haystack;
8079 char_u *save_haystack;
8080 char_u *pos;
8081 int start_idx;
8082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008083 needle = tv_get_string_chk(&argvars[1]);
8084 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 rettv->vval.v_number = -1;
8086 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008087 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088
8089 if (argvars[2].v_type != VAR_UNKNOWN)
8090 {
8091 int error = FALSE;
8092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008093 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 if (error || start_idx >= (int)STRLEN(haystack))
8095 return;
8096 if (start_idx >= 0)
8097 haystack += start_idx;
8098 }
8099
8100 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8101 if (pos != NULL)
8102 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8103}
8104
8105/*
8106 * "string()" function
8107 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008108 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109f_string(typval_T *argvars, typval_T *rettv)
8110{
8111 char_u *tofree;
8112 char_u numbuf[NUMBUFLEN];
8113
8114 rettv->v_type = VAR_STRING;
8115 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8116 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008117 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (rettv->vval.v_string != NULL && tofree == NULL)
8119 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8120}
8121
8122/*
8123 * "strlen()" function
8124 */
8125 static void
8126f_strlen(typval_T *argvars, typval_T *rettv)
8127{
8128 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008129 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130}
8131
8132/*
8133 * "strchars()" function
8134 */
8135 static void
8136f_strchars(typval_T *argvars, typval_T *rettv)
8137{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008138 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 varnumber_T len = 0;
8141 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142
8143 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008144 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008145 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008146 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147 else
8148 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8150 while (*s != NUL)
8151 {
8152 func_mb_ptr2char_adv(&s);
8153 ++len;
8154 }
8155 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 }
8157}
8158
8159/*
8160 * "strdisplaywidth()" function
8161 */
8162 static void
8163f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8164{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008165 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 int col = 0;
8167
8168 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008169 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170
8171 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8172}
8173
8174/*
8175 * "strwidth()" function
8176 */
8177 static void
8178f_strwidth(typval_T *argvars, typval_T *rettv)
8179{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008180 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181
Bram Moolenaar13505972019-01-24 15:04:48 +01008182 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183}
8184
8185/*
8186 * "strcharpart()" function
8187 */
8188 static void
8189f_strcharpart(typval_T *argvars, typval_T *rettv)
8190{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008191 char_u *p;
8192 int nchar;
8193 int nbyte = 0;
8194 int charlen;
8195 int len = 0;
8196 int slen;
8197 int error = FALSE;
8198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008199 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 slen = (int)STRLEN(p);
8201
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008202 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 if (!error)
8204 {
8205 if (nchar > 0)
8206 while (nchar > 0 && nbyte < slen)
8207 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008208 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 --nchar;
8210 }
8211 else
8212 nbyte = nchar;
8213 if (argvars[2].v_type != VAR_UNKNOWN)
8214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008215 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 while (charlen > 0 && nbyte + len < slen)
8217 {
8218 int off = nbyte + len;
8219
8220 if (off < 0)
8221 len += 1;
8222 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008223 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008224 --charlen;
8225 }
8226 }
8227 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008228 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229 }
8230
8231 /*
8232 * Only return the overlap between the specified part and the actual
8233 * string.
8234 */
8235 if (nbyte < 0)
8236 {
8237 len += nbyte;
8238 nbyte = 0;
8239 }
8240 else if (nbyte > slen)
8241 nbyte = slen;
8242 if (len < 0)
8243 len = 0;
8244 else if (nbyte + len > slen)
8245 len = slen - nbyte;
8246
8247 rettv->v_type = VAR_STRING;
8248 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249}
8250
8251/*
8252 * "strpart()" function
8253 */
8254 static void
8255f_strpart(typval_T *argvars, typval_T *rettv)
8256{
8257 char_u *p;
8258 int n;
8259 int len;
8260 int slen;
8261 int error = FALSE;
8262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008263 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 slen = (int)STRLEN(p);
8265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008266 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267 if (error)
8268 len = 0;
8269 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008270 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008271 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008272 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273
8274 /*
8275 * Only return the overlap between the specified part and the actual
8276 * string.
8277 */
8278 if (n < 0)
8279 {
8280 len += n;
8281 n = 0;
8282 }
8283 else if (n > slen)
8284 n = slen;
8285 if (len < 0)
8286 len = 0;
8287 else if (n + len > slen)
8288 len = slen - n;
8289
8290 rettv->v_type = VAR_STRING;
8291 rettv->vval.v_string = vim_strnsave(p + n, len);
8292}
8293
8294/*
8295 * "strridx()" function
8296 */
8297 static void
8298f_strridx(typval_T *argvars, typval_T *rettv)
8299{
8300 char_u buf[NUMBUFLEN];
8301 char_u *needle;
8302 char_u *haystack;
8303 char_u *rest;
8304 char_u *lastmatch = NULL;
8305 int haystack_len, end_idx;
8306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 needle = tv_get_string_chk(&argvars[1]);
8308 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309
8310 rettv->vval.v_number = -1;
8311 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008312 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313
8314 haystack_len = (int)STRLEN(haystack);
8315 if (argvars[2].v_type != VAR_UNKNOWN)
8316 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008317 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008318 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008320 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 }
8322 else
8323 end_idx = haystack_len;
8324
8325 if (*needle == NUL)
8326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008327 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 lastmatch = haystack + end_idx;
8329 }
8330 else
8331 {
8332 for (rest = haystack; *rest != '\0'; ++rest)
8333 {
8334 rest = (char_u *)strstr((char *)rest, (char *)needle);
8335 if (rest == NULL || rest > haystack + end_idx)
8336 break;
8337 lastmatch = rest;
8338 }
8339 }
8340
8341 if (lastmatch == NULL)
8342 rettv->vval.v_number = -1;
8343 else
8344 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8345}
8346
8347/*
8348 * "strtrans()" function
8349 */
8350 static void
8351f_strtrans(typval_T *argvars, typval_T *rettv)
8352{
8353 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008354 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008355}
8356
8357/*
8358 * "submatch()" function
8359 */
8360 static void
8361f_submatch(typval_T *argvars, typval_T *rettv)
8362{
8363 int error = FALSE;
8364 int no;
8365 int retList = 0;
8366
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008367 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (error)
8369 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008370 if (no < 0 || no >= NSUBEXP)
8371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008372 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008373 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008374 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008376 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 if (error)
8378 return;
8379
8380 if (retList == 0)
8381 {
8382 rettv->v_type = VAR_STRING;
8383 rettv->vval.v_string = reg_submatch(no);
8384 }
8385 else
8386 {
8387 rettv->v_type = VAR_LIST;
8388 rettv->vval.v_list = reg_submatch_list(no);
8389 }
8390}
8391
8392/*
8393 * "substitute()" function
8394 */
8395 static void
8396f_substitute(typval_T *argvars, typval_T *rettv)
8397{
8398 char_u patbuf[NUMBUFLEN];
8399 char_u subbuf[NUMBUFLEN];
8400 char_u flagsbuf[NUMBUFLEN];
8401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 char_u *str = tv_get_string_chk(&argvars[0]);
8403 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008404 char_u *sub = NULL;
8405 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008406 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008408 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8409 expr = &argvars[2];
8410 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008411 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008414 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8415 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 rettv->vval.v_string = NULL;
8417 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008418 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419}
8420
8421/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008422 * "swapinfo(swap_filename)" function
8423 */
8424 static void
8425f_swapinfo(typval_T *argvars, typval_T *rettv)
8426{
8427 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008429}
8430
8431/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008432 * "swapname(expr)" function
8433 */
8434 static void
8435f_swapname(typval_T *argvars, typval_T *rettv)
8436{
8437 buf_T *buf;
8438
8439 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008440 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008441 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8442 || buf->b_ml.ml_mfp->mf_fname == NULL)
8443 rettv->vval.v_string = NULL;
8444 else
8445 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8446}
8447
8448/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 * "synID(lnum, col, trans)" function
8450 */
8451 static void
8452f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8453{
8454 int id = 0;
8455#ifdef FEAT_SYN_HL
8456 linenr_T lnum;
8457 colnr_T col;
8458 int trans;
8459 int transerr = FALSE;
8460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008461 lnum = tv_get_lnum(argvars); // -1 on type error
8462 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008463 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464
8465 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8466 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8467 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8468#endif
8469
8470 rettv->vval.v_number = id;
8471}
8472
8473/*
8474 * "synIDattr(id, what [, mode])" function
8475 */
8476 static void
8477f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8478{
8479 char_u *p = NULL;
8480#ifdef FEAT_SYN_HL
8481 int id;
8482 char_u *what;
8483 char_u *mode;
8484 char_u modebuf[NUMBUFLEN];
8485 int modec;
8486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008487 id = (int)tv_get_number(&argvars[0]);
8488 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 if (argvars[2].v_type != VAR_UNKNOWN)
8490 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008491 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 modec = TOLOWER_ASC(mode[0]);
8493 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008494 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 }
8496 else
8497 {
8498#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8499 if (USE_24BIT)
8500 modec = 'g';
8501 else
8502#endif
8503 if (t_colors > 1)
8504 modec = 'c';
8505 else
8506 modec = 't';
8507 }
8508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 switch (TOLOWER_ASC(what[0]))
8510 {
8511 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008512 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008514 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 p = highlight_has_attr(id, HL_BOLD, modec);
8516 break;
8517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 p = highlight_color(id, what, modec);
8520 break;
8521
8522 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008523 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008525 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 p = highlight_has_attr(id, HL_ITALIC, modec);
8527 break;
8528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008529 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008530 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 break;
8532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008533 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 p = highlight_has_attr(id, HL_INVERSE, modec);
8535 break;
8536
8537 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008538 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008540 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008541 else if (TOLOWER_ASC(what[1]) == 't' &&
8542 TOLOWER_ASC(what[2]) == 'r')
8543 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008544 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 p = highlight_has_attr(id, HL_STANDOUT, modec);
8546 break;
8547
8548 case 'u':
8549 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8552 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008553 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8555 break;
8556 }
8557
8558 if (p != NULL)
8559 p = vim_strsave(p);
8560#endif
8561 rettv->v_type = VAR_STRING;
8562 rettv->vval.v_string = p;
8563}
8564
8565/*
8566 * "synIDtrans(id)" function
8567 */
8568 static void
8569f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8570{
8571 int id;
8572
8573#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008574 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575
8576 if (id > 0)
8577 id = syn_get_final_id(id);
8578 else
8579#endif
8580 id = 0;
8581
8582 rettv->vval.v_number = id;
8583}
8584
8585/*
8586 * "synconcealed(lnum, col)" function
8587 */
8588 static void
8589f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8590{
8591#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8592 linenr_T lnum;
8593 colnr_T col;
8594 int syntax_flags = 0;
8595 int cchar;
8596 int matchid = 0;
8597 char_u str[NUMBUFLEN];
8598#endif
8599
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008600 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601
8602#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008603 lnum = tv_get_lnum(argvars); // -1 on type error
8604 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605
Bram Moolenaara80faa82020-04-12 19:37:17 +02008606 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607
8608 if (rettv_list_alloc(rettv) != FAIL)
8609 {
8610 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8611 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8612 && curwin->w_p_cole > 0)
8613 {
8614 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8615 syntax_flags = get_syntax_info(&matchid);
8616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008617 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8619 {
8620 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008621 if (cchar == NUL && curwin->w_p_cole == 1)
8622 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 if (cchar != NUL)
8624 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 if (has_mbyte)
8626 (*mb_char2bytes)(cchar, str);
8627 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 str[0] = cchar;
8629 }
8630 }
8631 }
8632
8633 list_append_number(rettv->vval.v_list,
8634 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008635 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 list_append_string(rettv->vval.v_list, str, -1);
8637 list_append_number(rettv->vval.v_list, matchid);
8638 }
8639#endif
8640}
8641
8642/*
8643 * "synstack(lnum, col)" function
8644 */
8645 static void
8646f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8647{
8648#ifdef FEAT_SYN_HL
8649 linenr_T lnum;
8650 colnr_T col;
8651 int i;
8652 int id;
8653#endif
8654
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008655 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656
8657#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008658 lnum = tv_get_lnum(argvars); // -1 on type error
8659 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660
8661 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8662 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8663 && rettv_list_alloc(rettv) != FAIL)
8664 {
8665 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8666 for (i = 0; ; ++i)
8667 {
8668 id = syn_get_stack_item(i);
8669 if (id < 0)
8670 break;
8671 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8672 break;
8673 }
8674 }
8675#endif
8676}
8677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678/*
8679 * "tabpagebuflist()" function
8680 */
8681 static void
8682f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8683{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 tabpage_T *tp;
8685 win_T *wp = NULL;
8686
8687 if (argvars[0].v_type == VAR_UNKNOWN)
8688 wp = firstwin;
8689 else
8690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 if (tp != NULL)
8693 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8694 }
8695 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8696 {
8697 for (; wp != NULL; wp = wp->w_next)
8698 if (list_append_number(rettv->vval.v_list,
8699 wp->w_buffer->b_fnum) == FAIL)
8700 break;
8701 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702}
8703
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705 * "tagfiles()" function
8706 */
8707 static void
8708f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8709{
8710 char_u *fname;
8711 tagname_T tn;
8712 int first;
8713
8714 if (rettv_list_alloc(rettv) == FAIL)
8715 return;
8716 fname = alloc(MAXPATHL);
8717 if (fname == NULL)
8718 return;
8719
8720 for (first = TRUE; ; first = FALSE)
8721 if (get_tagfname(&tn, first, fname) == FAIL
8722 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8723 break;
8724 tagname_free(&tn);
8725 vim_free(fname);
8726}
8727
8728/*
8729 * "taglist()" function
8730 */
8731 static void
8732f_taglist(typval_T *argvars, typval_T *rettv)
8733{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008734 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 char_u *tag_pattern;
8736
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008737 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738
8739 rettv->vval.v_number = FALSE;
8740 if (*tag_pattern == NUL)
8741 return;
8742
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008743 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008744 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008746 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747}
8748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749#ifdef FEAT_FLOAT
8750/*
8751 * "tan()" function
8752 */
8753 static void
8754f_tan(typval_T *argvars, typval_T *rettv)
8755{
8756 float_T f = 0.0;
8757
8758 rettv->v_type = VAR_FLOAT;
8759 if (get_float_arg(argvars, &f) == OK)
8760 rettv->vval.v_float = tan(f);
8761 else
8762 rettv->vval.v_float = 0.0;
8763}
8764
8765/*
8766 * "tanh()" function
8767 */
8768 static void
8769f_tanh(typval_T *argvars, typval_T *rettv)
8770{
8771 float_T f = 0.0;
8772
8773 rettv->v_type = VAR_FLOAT;
8774 if (get_float_arg(argvars, &f) == OK)
8775 rettv->vval.v_float = tanh(f);
8776 else
8777 rettv->vval.v_float = 0.0;
8778}
8779#endif
8780
8781/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782 * "tolower(string)" function
8783 */
8784 static void
8785f_tolower(typval_T *argvars, typval_T *rettv)
8786{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008788 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789}
8790
8791/*
8792 * "toupper(string)" function
8793 */
8794 static void
8795f_toupper(typval_T *argvars, typval_T *rettv)
8796{
8797 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008798 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799}
8800
8801/*
8802 * "tr(string, fromstr, tostr)" function
8803 */
8804 static void
8805f_tr(typval_T *argvars, typval_T *rettv)
8806{
8807 char_u *in_str;
8808 char_u *fromstr;
8809 char_u *tostr;
8810 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 int inlen;
8812 int fromlen;
8813 int tolen;
8814 int idx;
8815 char_u *cpstr;
8816 int cplen;
8817 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 char_u buf[NUMBUFLEN];
8819 char_u buf2[NUMBUFLEN];
8820 garray_T ga;
8821
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008822 in_str = tv_get_string(&argvars[0]);
8823 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8824 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008826 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 rettv->v_type = VAR_STRING;
8828 rettv->vval.v_string = NULL;
8829 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008830 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 ga_init2(&ga, (int)sizeof(char), 80);
8832
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008834 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 if (STRLEN(fromstr) != STRLEN(tostr))
8836 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008838 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839 ga_clear(&ga);
8840 return;
8841 }
8842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008843 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008844 while (*in_str != NUL)
8845 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 if (has_mbyte)
8847 {
8848 inlen = (*mb_ptr2len)(in_str);
8849 cpstr = in_str;
8850 cplen = inlen;
8851 idx = 0;
8852 for (p = fromstr; *p != NUL; p += fromlen)
8853 {
8854 fromlen = (*mb_ptr2len)(p);
8855 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8856 {
8857 for (p = tostr; *p != NUL; p += tolen)
8858 {
8859 tolen = (*mb_ptr2len)(p);
8860 if (idx-- == 0)
8861 {
8862 cplen = tolen;
8863 cpstr = p;
8864 break;
8865 }
8866 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008867 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868 goto error;
8869 break;
8870 }
8871 ++idx;
8872 }
8873
8874 if (first && cpstr == in_str)
8875 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008876 // Check that fromstr and tostr have the same number of
8877 // (multi-byte) characters. Done only once when a character
8878 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 first = FALSE;
8880 for (p = tostr; *p != NUL; p += tolen)
8881 {
8882 tolen = (*mb_ptr2len)(p);
8883 --idx;
8884 }
8885 if (idx != 0)
8886 goto error;
8887 }
8888
8889 (void)ga_grow(&ga, cplen);
8890 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8891 ga.ga_len += cplen;
8892
8893 in_str += inlen;
8894 }
8895 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008897 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008898 p = vim_strchr(fromstr, *in_str);
8899 if (p != NULL)
8900 ga_append(&ga, tostr[p - fromstr]);
8901 else
8902 ga_append(&ga, *in_str);
8903 ++in_str;
8904 }
8905 }
8906
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008907 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908 (void)ga_grow(&ga, 1);
8909 ga_append(&ga, NUL);
8910
8911 rettv->vval.v_string = ga.ga_data;
8912}
8913
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008914/*
8915 * "trim({expr})" function
8916 */
8917 static void
8918f_trim(typval_T *argvars, typval_T *rettv)
8919{
8920 char_u buf1[NUMBUFLEN];
8921 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008922 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008923 char_u *mask = NULL;
8924 char_u *tail;
8925 char_u *prev;
8926 char_u *p;
8927 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008928 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008929
8930 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008931 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008932 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008933 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008934
8935 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008937 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008938
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008940 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008941 int error = 0;
8942
8943 // leading or trailing characters to trim
8944 dir = (int)tv_get_number_chk(&argvars[2], &error);
8945 if (error)
8946 return;
8947 if (dir < 0 || dir > 2)
8948 {
8949 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8950 return;
8951 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008952 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008953 }
8954
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008955 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008956 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008957 // Trim leading characters
8958 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008959 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008960 c1 = PTR2CHAR(head);
8961 if (mask == NULL)
8962 {
8963 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008964 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008965 }
8966 else
8967 {
8968 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8969 if (c1 == PTR2CHAR(p))
8970 break;
8971 if (*p == NUL)
8972 break;
8973 }
8974 MB_PTR_ADV(head);
8975 }
8976 }
8977
8978 tail = head + STRLEN(head);
8979 if (dir == 0 || dir == 2)
8980 {
8981 // Trim trailing characters
8982 for (; tail > head; tail = prev)
8983 {
8984 prev = tail;
8985 MB_PTR_BACK(head, prev);
8986 c1 = PTR2CHAR(prev);
8987 if (mask == NULL)
8988 {
8989 if (c1 > ' ' && c1 != 0xa0)
8990 break;
8991 }
8992 else
8993 {
8994 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8995 if (c1 == PTR2CHAR(p))
8996 break;
8997 if (*p == NUL)
8998 break;
8999 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009000 }
9001 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009002 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009003}
9004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005#ifdef FEAT_FLOAT
9006/*
9007 * "trunc({float})" function
9008 */
9009 static void
9010f_trunc(typval_T *argvars, typval_T *rettv)
9011{
9012 float_T f = 0.0;
9013
9014 rettv->v_type = VAR_FLOAT;
9015 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009016 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9018 else
9019 rettv->vval.v_float = 0.0;
9020}
9021#endif
9022
9023/*
9024 * "type(expr)" function
9025 */
9026 static void
9027f_type(typval_T *argvars, typval_T *rettv)
9028{
9029 int n = -1;
9030
9031 switch (argvars[0].v_type)
9032 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009033 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9034 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009036 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9037 case VAR_LIST: n = VAR_TYPE_LIST; break;
9038 case VAR_DICT: n = VAR_TYPE_DICT; break;
9039 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9040 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9041 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009042 case VAR_JOB: n = VAR_TYPE_JOB; break;
9043 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009044 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009046 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009047 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009048 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 n = -1;
9050 break;
9051 }
9052 rettv->vval.v_number = n;
9053}
9054
9055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 * "virtcol(string)" function
9057 */
9058 static void
9059f_virtcol(typval_T *argvars, typval_T *rettv)
9060{
9061 colnr_T vcol = 0;
9062 pos_T *fp;
9063 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009064 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065
9066 fp = var2fpos(&argvars[0], FALSE, &fnum);
9067 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9068 && fnum == curbuf->b_fnum)
9069 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009070 // Limit the column to a valid value, getvvcol() doesn't check.
9071 if (fp->col < 0)
9072 fp->col = 0;
9073 else
9074 {
9075 len = (int)STRLEN(ml_get(fp->lnum));
9076 if (fp->col > len)
9077 fp->col = len;
9078 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 getvvcol(curwin, fp, NULL, NULL, &vcol);
9080 ++vcol;
9081 }
9082
9083 rettv->vval.v_number = vcol;
9084}
9085
9086/*
9087 * "visualmode()" function
9088 */
9089 static void
9090f_visualmode(typval_T *argvars, typval_T *rettv)
9091{
9092 char_u str[2];
9093
9094 rettv->v_type = VAR_STRING;
9095 str[0] = curbuf->b_visual_mode_eval;
9096 str[1] = NUL;
9097 rettv->vval.v_string = vim_strsave(str);
9098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009099 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009100 if (non_zero_arg(&argvars[0]))
9101 curbuf->b_visual_mode_eval = NUL;
9102}
9103
9104/*
9105 * "wildmenumode()" function
9106 */
9107 static void
9108f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9109{
9110#ifdef FEAT_WILDMENU
9111 if (wild_menu_showing)
9112 rettv->vval.v_number = 1;
9113#endif
9114}
9115
9116/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009117 * "windowsversion()" function
9118 */
9119 static void
9120f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9121{
9122 rettv->v_type = VAR_STRING;
9123 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9124}
9125
9126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009127 * "wordcount()" function
9128 */
9129 static void
9130f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9131{
9132 if (rettv_dict_alloc(rettv) == FAIL)
9133 return;
9134 cursor_pos_info(rettv->vval.v_dict);
9135}
9136
9137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009138 * "xor(expr, expr)" function
9139 */
9140 static void
9141f_xor(typval_T *argvars, typval_T *rettv)
9142{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009143 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9144 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145}
9146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009147#endif // FEAT_EVAL