blob: 0a100748d921f546dbc5db1114ee63cb1845e465 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200385 static type_T *
386ret_remove(int argcount UNUSED, type_T **argtypes)
387{
388 if (argtypes[0]->tt_type == VAR_LIST
389 || argtypes[0]->tt_type == VAR_DICT)
390 return argtypes[0]->tt_member;
391 if (argtypes[0]->tt_type == VAR_BLOB)
392 return &t_number;
393 return &t_any;
394}
395
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100396static type_T *ret_f_function(int argcount, type_T **argtypes);
397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200398/*
399 * Array with names and number of arguments of all internal functions
400 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
401 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200402typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200404 char *f_name; // function name
405 char f_min_argc; // minimal number of arguments
406 char f_max_argc; // maximal number of arguments
407 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100408 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
409 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200411 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200412} funcentry_T;
413
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200414// values for f_argtype; zero means it cannot be used as a method
415#define FEARG_1 1 // base is the first argument
416#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200417#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200418#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200419#define FEARG_LAST 9 // base is the last argument
420
Bram Moolenaar15c47602020-03-26 22:16:48 +0100421#ifdef FEAT_FLOAT
422# define FLOAT_FUNC(name) name
423#else
424# define FLOAT_FUNC(name) NULL
425#endif
426#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
427# define MATH_FUNC(name) name
428#else
429# define MATH_FUNC(name) NULL
430#endif
431#ifdef FEAT_TIMERS
432# define TIMER_FUNC(name) name
433#else
434# define TIMER_FUNC(name) NULL
435#endif
436#ifdef FEAT_JOB_CHANNEL
437# define JOB_FUNC(name) name
438#else
439# define JOB_FUNC(name) NULL
440#endif
441#ifdef FEAT_PROP_POPUP
442# define PROP_FUNC(name) name
443#else
444# define PROP_FUNC(name) NULL
445#endif
446#ifdef FEAT_SIGNS
447# define SIGN_FUNC(name) name
448#else
449# define SIGN_FUNC(name) NULL
450#endif
451#ifdef FEAT_SOUND
452# define SOUND_FUNC(name) name
453#else
454# define SOUND_FUNC(name) NULL
455#endif
456#ifdef FEAT_TERMINAL
457# define TERM_FUNC(name) name
458#else
459# define TERM_FUNC(name) NULL
460#endif
461
Bram Moolenaarac92e252019-08-03 21:58:38 +0200462static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100464 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
465 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200466 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100467 {"and", 2, 2, FEARG_1, ret_number, f_and},
468 {"append", 2, 2, FEARG_LAST, ret_number, f_append},
469 {"appendbufline", 3, 3, FEARG_LAST, ret_number, f_appendbufline},
470 {"argc", 0, 1, 0, ret_number, f_argc},
471 {"argidx", 0, 0, 0, ret_number, f_argidx},
472 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200473 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100474 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100475 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
476 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200477 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100478 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
479 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
480 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
481 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
482 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
483 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
484 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
485 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
486 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100487 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
488 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
489 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100490#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100491 f_balloon_gettext
492#else
493 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100494#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100495 },
496 {"balloon_show", 1, 1, FEARG_1, ret_void,
497#ifdef FEAT_BEVAL
498 f_balloon_show
499#else
500 NULL
501#endif
502 },
503 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
504#if defined(FEAT_BEVAL_TERM)
505 f_balloon_split
506#else
507 NULL
508#endif
509 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100510 {"browse", 4, 4, 0, ret_string, f_browse},
511 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
512 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
513 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
514 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
515 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
516 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
517 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
518 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
519 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
520 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
521 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
522 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
523 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
524 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
525 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
526 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
527 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100528 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
529 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
530 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
531 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
532 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
533 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
534 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
535 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
536 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
537 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
538 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
539 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
540 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
541 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
542 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
543 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
544 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
545 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
546 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100547 {"changenr", 0, 0, 0, ret_number, f_changenr},
548 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
549 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
550 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
551 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
552 {"col", 1, 1, FEARG_1, ret_number, f_col},
553 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
554 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
555 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
556 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
557 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200558 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100559 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
560 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100561 {"count", 2, 4, FEARG_1, ret_number, f_count},
562 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
563 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100564 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100565#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100566 f_debugbreak
567#else
568 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200569#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100570 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200571 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100572 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
573 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
574 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
575 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
576 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
577 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
578 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
579 {"environ", 0, 0, 0, ret_dict_string, f_environ},
580 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
581 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
582 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
583 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
584 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
585 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
586 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100587 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100588 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
589 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200590 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100591 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
592 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
593 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
594 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200595 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100596 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
597 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200598 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100599 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
600 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
601 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100602 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
603 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
604 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
605 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
606 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
607 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
608 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
609 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200610 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
612 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
613 {"get", 2, 3, FEARG_1, ret_any, f_get},
614 {"getbufinfo", 0, 1, 0, ret_list_dict_any, f_getbufinfo},
615 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
616 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
617 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
618 {"getchar", 0, 1, 0, ret_number, f_getchar},
619 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
620 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
621 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
622 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
623 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
624 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
625 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
626 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
627 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
628 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
629 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
630 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
631 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
632 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
633 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
634 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
635 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
636 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200637 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200638 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100639 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
640 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
641 {"getpid", 0, 0, 0, ret_number, f_getpid},
642 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200643 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100644 {"getreg", 0, 3, FEARG_1, ret_string, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200645 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100646 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
647 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
648 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
649 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
650 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
651 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
652 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
653 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
654 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
655 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
656 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
657 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
658 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100659 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100660 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
661 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
662 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
663 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
664 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
665 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
666 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
667 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
668 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
669 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
670 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
671 {"hostname", 0, 0, 0, ret_string, f_hostname},
672 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
673 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
674 {"index", 2, 4, FEARG_1, ret_number, f_index},
675 {"input", 1, 3, FEARG_1, ret_string, f_input},
676 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
677 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
678 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
679 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
680 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200681 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100682 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
683 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
684 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100685 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100686 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100687 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100688 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100689 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
690 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
691 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
692 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
693 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
694 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100695 {"join", 1, 2, FEARG_1, ret_string, f_join},
696 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
697 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
698 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
699 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
700 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
701 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
702 {"len", 1, 1, FEARG_1, ret_number, f_len},
703 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
704 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
705 {"line", 1, 2, FEARG_1, ret_number, f_line},
706 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
707 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
708 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
709 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
710 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
711 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
712 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100713 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
714 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
715 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200716#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100717 f_luaeval
718#else
719 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200720#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100721 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100722 {"map", 2, 2, FEARG_1, ret_any, f_map},
723 {"maparg", 1, 4, FEARG_1, ret_string, f_maparg},
724 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200725 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100726 {"match", 2, 4, FEARG_1, ret_any, f_match},
727 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
728 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
729 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
730 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
731 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
732 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
733 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
734 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
735 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100736 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100737#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100738 f_menu_info
739#else
740 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100741#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100742 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100743 {"min", 1, 1, FEARG_1, ret_any, f_min},
744 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
745 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100746 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100748 f_mzeval
749#else
750 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200751#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100752 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100753 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
754 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
755 {"or", 2, 2, FEARG_1, ret_number, f_or},
756 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200758#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_perleval
760#else
761 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
764 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
765 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200766 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
768 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
769 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
770 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
771 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
772 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
773 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
774 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
775 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
776 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200777 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
779 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
780 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
781 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
782 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
783 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
784 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
785 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100786 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
787 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100788 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
789 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
790 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
791 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
792 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
793 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
794 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
795 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
796 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
797 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
798 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
799 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
800 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100801 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
802 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100803 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200804#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100805 f_py3eval
806#else
807 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100809 },
810 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100812 f_pyeval
813#else
814 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200815#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100816 },
817 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100819 f_pyxeval
820#else
821 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100822#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100823 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100824 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
825 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200826 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
827 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100828 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200829 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100830 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
831 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
832 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100833 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100834 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
835 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
836 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
837 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
838 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
839 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200840 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200841 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100842 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200843 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100844 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200845 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
847 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100848#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100849 f_rubyeval
850#else
851 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100852#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100853 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100854 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
855 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
856 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
857 {"screencol", 0, 0, 0, ret_number, f_screencol},
858 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
859 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
860 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200861 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200862 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100863 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
864 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
865 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200866 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100867 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
868 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
869 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
870 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
871 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
872 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
873 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
874 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
875 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
876 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
877 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
878 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
879 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
880 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
881 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
882 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
883 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
884 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100885 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100887 f_sha256
888#else
889 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100891 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100892 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
893 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
895 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
896 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
897 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
898 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
899 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
900 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
901 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
902 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200903 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100904 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
905 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200906 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100907 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
908 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
909 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
910 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100911 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
912 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
913 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
914 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100915 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100916 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
917 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100918 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100919 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
920 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
921 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
922 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
923 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100924 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100926 f_strftime
927#else
928 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100930 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100931 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
932 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
933 {"string", 1, 1, FEARG_1, ret_string, f_string},
934 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
935 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100936 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100937#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100938 f_strptime
939#else
940 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100941#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100942 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100943 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
944 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
945 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
946 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
947 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
948 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
949 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
950 {"synID", 3, 3, 0, ret_number, f_synID},
951 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
952 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
953 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
954 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
955 {"system", 1, 2, FEARG_1, ret_string, f_system},
956 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
957 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
958 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
959 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
960 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
961 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100962 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
963 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100964 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100965 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
966 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
967 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
968 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
969 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100970#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100971 f_term_getansicolors
972#else
973 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100975 },
976 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
977 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
978 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
979 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
980 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
981 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
982 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
983 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
984 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
985 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
986 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
987 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
988 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100989#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100990 f_term_setansicolors
991#else
992 NULL
993#endif
994 },
995 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
996 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
997 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
998 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
999 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1000 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001001 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001002 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1003 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1004 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1005 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1006 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1007 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1008 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1009 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001010 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001011 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001012 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001013 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001014 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001015 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001016 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1017 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1018 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1019 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001020 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001021#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001022 f_test_scrollbar
1023#else
1024 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001025#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001026 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001027 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1028 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1029 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1030 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
1031 {"test_void", 0, 0, 0, ret_any, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001032 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1033 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1034 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1035 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1036 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001037 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1038 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1039 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001040 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001041 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001042 {"type", 1, 1, FEARG_1, ret_number, f_type},
1043 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1044 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1045 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1046 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1047 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1048 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1049 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1050 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1051 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1052 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1053 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1054 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1055 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1056 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1057 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1058 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1059 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1060 {"wincol", 0, 0, 0, ret_number, f_wincol},
1061 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1062 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1063 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1064 {"winline", 0, 0, 0, ret_number, f_winline},
1065 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1066 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1067 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1068 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1069 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1070 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1071 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1072 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001073};
1074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001075/*
1076 * Function given to ExpandGeneric() to obtain the list of internal
1077 * or user defined function names.
1078 */
1079 char_u *
1080get_function_name(expand_T *xp, int idx)
1081{
1082 static int intidx = -1;
1083 char_u *name;
1084
1085 if (idx == 0)
1086 intidx = -1;
1087 if (intidx < 0)
1088 {
1089 name = get_user_func_name(xp, idx);
1090 if (name != NULL)
1091 return name;
1092 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001093 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001094 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001095 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001096 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001097 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001098 STRCAT(IObuff, ")");
1099 return IObuff;
1100 }
1101
1102 return NULL;
1103}
1104
1105/*
1106 * Function given to ExpandGeneric() to obtain the list of internal or
1107 * user defined variable or function names.
1108 */
1109 char_u *
1110get_expr_name(expand_T *xp, int idx)
1111{
1112 static int intidx = -1;
1113 char_u *name;
1114
1115 if (idx == 0)
1116 intidx = -1;
1117 if (intidx < 0)
1118 {
1119 name = get_function_name(xp, idx);
1120 if (name != NULL)
1121 return name;
1122 }
1123 return get_user_var_name(xp, ++intidx);
1124}
1125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001127 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001128 * Return index, or -1 if not found or "implemented" is TRUE and the function
1129 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001130 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001131 static int
1132find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001133{
1134 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001135 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136 int cmp;
1137 int x;
1138
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001139 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001140
1141 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001142 while (first <= last)
1143 {
1144 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001145 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001146 if (cmp < 0)
1147 last = x - 1;
1148 else if (cmp > 0)
1149 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001150 else if (implemented && global_functions[x].f_func == NULL)
1151 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001152 else
1153 return x;
1154 }
1155 return -1;
1156}
1157
Bram Moolenaar15c47602020-03-26 22:16:48 +01001158/*
1159 * Find internal function "name" in table "global_functions".
1160 * Return index, or -1 if not found or the function is not implemented.
1161 */
1162 int
1163find_internal_func(char_u *name)
1164{
1165 return find_internal_func_opt(name, TRUE);
1166}
1167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001169has_internal_func(char_u *name)
1170{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001171 return find_internal_func_opt(name, TRUE) >= 0;
1172}
1173
1174 static int
1175has_internal_func_name(char_u *name)
1176{
1177 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001178}
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 char *
1181internal_func_name(int idx)
1182{
1183 return global_functions[idx].f_name;
1184}
1185
1186 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001187internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001189 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190}
1191
1192/*
1193 * Check the argument count to use for internal function "idx".
1194 * Returns OK or FAIL;
1195 */
1196 int
1197check_internal_func(int idx, int argcount)
1198{
1199 int res;
1200 char *name;
1201
1202 if (argcount < global_functions[idx].f_min_argc)
1203 res = FCERR_TOOFEW;
1204 else if (argcount > global_functions[idx].f_max_argc)
1205 res = FCERR_TOOMANY;
1206 else
1207 return OK;
1208
1209 name = internal_func_name(idx);
1210 if (res == FCERR_TOOMANY)
1211 semsg(_(e_toomanyarg), name);
1212 else
1213 semsg(_(e_toofewarg), name);
1214 return FAIL;
1215}
1216
Bram Moolenaarac92e252019-08-03 21:58:38 +02001217 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001218call_internal_func(
1219 char_u *name,
1220 int argcount,
1221 typval_T *argvars,
1222 typval_T *rettv)
1223{
1224 int i;
1225
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001226 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001228 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001229 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001230 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001231 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001232 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001233 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001234 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001235 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001236}
1237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 void
1239call_internal_func_by_idx(
1240 int idx,
1241 typval_T *argvars,
1242 typval_T *rettv)
1243{
1244 global_functions[idx].f_func(argvars, rettv);
1245}
1246
Bram Moolenaarac92e252019-08-03 21:58:38 +02001247/*
1248 * Invoke a method for base->method().
1249 */
1250 int
1251call_internal_method(
1252 char_u *name,
1253 int argcount,
1254 typval_T *argvars,
1255 typval_T *rettv,
1256 typval_T *basetv)
1257{
1258 int i;
1259 int fi;
1260 typval_T argv[MAX_FUNC_ARGS + 1];
1261
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001262 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001263 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001264 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001265 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001266 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001267 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001268 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001269 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001270 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001271
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001272 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001273 {
1274 // base value goes last
1275 for (i = 0; i < argcount; ++i)
1276 argv[i] = argvars[i];
1277 argv[argcount] = *basetv;
1278 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001279 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001280 {
1281 // base value goes second
1282 argv[0] = argvars[0];
1283 argv[1] = *basetv;
1284 for (i = 1; i < argcount; ++i)
1285 argv[i + 1] = argvars[i];
1286 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001287 else if (global_functions[fi].f_argtype == FEARG_3)
1288 {
1289 // base value goes third
1290 argv[0] = argvars[0];
1291 argv[1] = argvars[1];
1292 argv[2] = *basetv;
1293 for (i = 2; i < argcount; ++i)
1294 argv[i + 1] = argvars[i];
1295 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001296 else if (global_functions[fi].f_argtype == FEARG_4)
1297 {
1298 // base value goes fourth
1299 argv[0] = argvars[0];
1300 argv[1] = argvars[1];
1301 argv[2] = argvars[2];
1302 argv[3] = *basetv;
1303 for (i = 3; i < argcount; ++i)
1304 argv[i + 1] = argvars[i];
1305 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001306 else
1307 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001308 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001309 argv[0] = *basetv;
1310 for (i = 0; i < argcount; ++i)
1311 argv[i + 1] = argvars[i];
1312 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001313 argv[argcount + 1].v_type = VAR_UNKNOWN;
1314
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001315 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001316 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317}
1318
1319/*
1320 * Return TRUE for a non-zero Number and a non-empty String.
1321 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001322 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323non_zero_arg(typval_T *argvars)
1324{
1325 return ((argvars[0].v_type == VAR_NUMBER
1326 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001327 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 && argvars[0].vval.v_number == VVAL_TRUE)
1329 || (argvars[0].v_type == VAR_STRING
1330 && argvars[0].vval.v_string != NULL
1331 && *argvars[0].vval.v_string != NUL));
1332}
1333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001334#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335/*
1336 * Get the float value of "argvars[0]" into "f".
1337 * Returns FAIL when the argument is not a Number or Float.
1338 */
1339 static int
1340get_float_arg(typval_T *argvars, float_T *f)
1341{
1342 if (argvars[0].v_type == VAR_FLOAT)
1343 {
1344 *f = argvars[0].vval.v_float;
1345 return OK;
1346 }
1347 if (argvars[0].v_type == VAR_NUMBER)
1348 {
1349 *f = (float_T)argvars[0].vval.v_number;
1350 return OK;
1351 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 return FAIL;
1354}
1355
1356/*
1357 * "abs(expr)" function
1358 */
1359 static void
1360f_abs(typval_T *argvars, typval_T *rettv)
1361{
1362 if (argvars[0].v_type == VAR_FLOAT)
1363 {
1364 rettv->v_type = VAR_FLOAT;
1365 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1366 }
1367 else
1368 {
1369 varnumber_T n;
1370 int error = FALSE;
1371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001372 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 if (error)
1374 rettv->vval.v_number = -1;
1375 else if (n > 0)
1376 rettv->vval.v_number = n;
1377 else
1378 rettv->vval.v_number = -n;
1379 }
1380}
1381
1382/*
1383 * "acos()" function
1384 */
1385 static void
1386f_acos(typval_T *argvars, typval_T *rettv)
1387{
1388 float_T f = 0.0;
1389
1390 rettv->v_type = VAR_FLOAT;
1391 if (get_float_arg(argvars, &f) == OK)
1392 rettv->vval.v_float = acos(f);
1393 else
1394 rettv->vval.v_float = 0.0;
1395}
1396#endif
1397
1398/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 * "and(expr, expr)" function
1400 */
1401 static void
1402f_and(typval_T *argvars, typval_T *rettv)
1403{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001404 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1405 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001406}
1407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001408#ifdef FEAT_FLOAT
1409/*
1410 * "asin()" function
1411 */
1412 static void
1413f_asin(typval_T *argvars, typval_T *rettv)
1414{
1415 float_T f = 0.0;
1416
1417 rettv->v_type = VAR_FLOAT;
1418 if (get_float_arg(argvars, &f) == OK)
1419 rettv->vval.v_float = asin(f);
1420 else
1421 rettv->vval.v_float = 0.0;
1422}
1423
1424/*
1425 * "atan()" function
1426 */
1427 static void
1428f_atan(typval_T *argvars, typval_T *rettv)
1429{
1430 float_T f = 0.0;
1431
1432 rettv->v_type = VAR_FLOAT;
1433 if (get_float_arg(argvars, &f) == OK)
1434 rettv->vval.v_float = atan(f);
1435 else
1436 rettv->vval.v_float = 0.0;
1437}
1438
1439/*
1440 * "atan2()" function
1441 */
1442 static void
1443f_atan2(typval_T *argvars, typval_T *rettv)
1444{
1445 float_T fx = 0.0, fy = 0.0;
1446
1447 rettv->v_type = VAR_FLOAT;
1448 if (get_float_arg(argvars, &fx) == OK
1449 && get_float_arg(&argvars[1], &fy) == OK)
1450 rettv->vval.v_float = atan2(fx, fy);
1451 else
1452 rettv->vval.v_float = 0.0;
1453}
1454#endif
1455
1456/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001457 * "balloon_show()" function
1458 */
1459#ifdef FEAT_BEVAL
1460 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001461f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1462{
1463 rettv->v_type = VAR_STRING;
1464 if (balloonEval != NULL)
1465 {
1466 if (balloonEval->msg == NULL)
1467 rettv->vval.v_string = NULL;
1468 else
1469 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1470 }
1471}
1472
1473 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001474f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1475{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001476 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001477 {
1478 if (argvars[0].v_type == VAR_LIST
1479# ifdef FEAT_GUI
1480 && !gui.in_use
1481# endif
1482 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001483 {
1484 list_T *l = argvars[0].vval.v_list;
1485
1486 // empty list removes the balloon
1487 post_balloon(balloonEval, NULL,
1488 l == NULL || l->lv_len == 0 ? NULL : l);
1489 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001490 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001491 {
1492 char_u *mesg = tv_get_string_chk(&argvars[0]);
1493
1494 if (mesg != NULL)
1495 // empty string removes the balloon
1496 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1497 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001498 }
1499}
1500
Bram Moolenaar669a8282017-11-19 20:13:05 +01001501# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001502 static void
1503f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1504{
1505 if (rettv_list_alloc(rettv) == OK)
1506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001507 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001508
1509 if (msg != NULL)
1510 {
1511 pumitem_T *array;
1512 int size = split_message(msg, &array);
1513 int i;
1514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001515 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001516 for (i = 1; i < size - 1; ++i)
1517 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001518 while (size > 0)
1519 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001520 vim_free(array);
1521 }
1522 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001523}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001524# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001525#endif
1526
1527/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001528 * Get the buffer from "arg" and give an error and return NULL if it is not
1529 * valid.
1530 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001531 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001532get_buf_arg(typval_T *arg)
1533{
1534 buf_T *buf;
1535
1536 ++emsg_off;
1537 buf = tv_get_buf(arg, FALSE);
1538 --emsg_off;
1539 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001540 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001541 return buf;
1542}
1543
1544/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545 * "byte2line(byte)" function
1546 */
1547 static void
1548f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1549{
1550#ifndef FEAT_BYTEOFF
1551 rettv->vval.v_number = -1;
1552#else
1553 long boff = 0;
1554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001555 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556 if (boff < 0)
1557 rettv->vval.v_number = -1;
1558 else
1559 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1560 (linenr_T)0, &boff);
1561#endif
1562}
1563
1564 static void
1565byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1566{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 char_u *str;
1569 varnumber_T idx;
1570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001571 str = tv_get_string_chk(&argvars[0]);
1572 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573 rettv->vval.v_number = -1;
1574 if (str == NULL || idx < 0)
1575 return;
1576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001577 t = str;
1578 for ( ; idx > 0; idx--)
1579 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001580 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 return;
1582 if (enc_utf8 && comp)
1583 t += utf_ptr2len(t);
1584 else
1585 t += (*mb_ptr2len)(t);
1586 }
1587 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588}
1589
1590/*
1591 * "byteidx()" function
1592 */
1593 static void
1594f_byteidx(typval_T *argvars, typval_T *rettv)
1595{
1596 byteidx(argvars, rettv, FALSE);
1597}
1598
1599/*
1600 * "byteidxcomp()" function
1601 */
1602 static void
1603f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1604{
1605 byteidx(argvars, rettv, TRUE);
1606}
1607
1608/*
1609 * "call(func, arglist [, dict])" function
1610 */
1611 static void
1612f_call(typval_T *argvars, typval_T *rettv)
1613{
1614 char_u *func;
1615 partial_T *partial = NULL;
1616 dict_T *selfdict = NULL;
1617
1618 if (argvars[1].v_type != VAR_LIST)
1619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001620 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 return;
1622 }
1623 if (argvars[1].vval.v_list == NULL)
1624 return;
1625
1626 if (argvars[0].v_type == VAR_FUNC)
1627 func = argvars[0].vval.v_string;
1628 else if (argvars[0].v_type == VAR_PARTIAL)
1629 {
1630 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001631 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 }
1633 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001636 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637
1638 if (argvars[2].v_type != VAR_UNKNOWN)
1639 {
1640 if (argvars[2].v_type != VAR_DICT)
1641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643 return;
1644 }
1645 selfdict = argvars[2].vval.v_dict;
1646 }
1647
1648 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1649}
1650
1651#ifdef FEAT_FLOAT
1652/*
1653 * "ceil({float})" function
1654 */
1655 static void
1656f_ceil(typval_T *argvars, typval_T *rettv)
1657{
1658 float_T f = 0.0;
1659
1660 rettv->v_type = VAR_FLOAT;
1661 if (get_float_arg(argvars, &f) == OK)
1662 rettv->vval.v_float = ceil(f);
1663 else
1664 rettv->vval.v_float = 0.0;
1665}
1666#endif
1667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668/*
1669 * "changenr()" function
1670 */
1671 static void
1672f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1673{
1674 rettv->vval.v_number = curbuf->b_u_seq_cur;
1675}
1676
1677/*
1678 * "char2nr(string)" function
1679 */
1680 static void
1681f_char2nr(typval_T *argvars, typval_T *rettv)
1682{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683 if (has_mbyte)
1684 {
1685 int utf8 = 0;
1686
1687 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001688 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689
1690 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001691 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001693 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694 }
1695 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001696 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001697}
1698
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001699 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001700get_optional_window(typval_T *argvars, int idx)
1701{
1702 win_T *win = curwin;
1703
1704 if (argvars[idx].v_type != VAR_UNKNOWN)
1705 {
1706 win = find_win_by_nr_or_id(&argvars[idx]);
1707 if (win == NULL)
1708 {
1709 emsg(_(e_invalwindow));
1710 return NULL;
1711 }
1712 }
1713 return win;
1714}
1715
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 * "col(string)" function
1718 */
1719 static void
1720f_col(typval_T *argvars, typval_T *rettv)
1721{
1722 colnr_T col = 0;
1723 pos_T *fp;
1724 int fnum = curbuf->b_fnum;
1725
1726 fp = var2fpos(&argvars[0], FALSE, &fnum);
1727 if (fp != NULL && fnum == curbuf->b_fnum)
1728 {
1729 if (fp->col == MAXCOL)
1730 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001731 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1733 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1734 else
1735 col = MAXCOL;
1736 }
1737 else
1738 {
1739 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001740 // col(".") when the cursor is on the NUL at the end of the line
1741 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001742 if (virtual_active() && fp == &curwin->w_cursor)
1743 {
1744 char_u *p = ml_get_cursor();
1745
1746 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1747 curwin->w_virtcol - curwin->w_cursor.coladd))
1748 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001749 int l;
1750
1751 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1752 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001753 }
1754 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001755 }
1756 }
1757 rettv->vval.v_number = col;
1758}
1759
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001760/*
1761 * "confirm(message, buttons[, default [, type]])" function
1762 */
1763 static void
1764f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1765{
1766#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1767 char_u *message;
1768 char_u *buttons = NULL;
1769 char_u buf[NUMBUFLEN];
1770 char_u buf2[NUMBUFLEN];
1771 int def = 1;
1772 int type = VIM_GENERIC;
1773 char_u *typestr;
1774 int error = FALSE;
1775
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001776 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001777 if (message == NULL)
1778 error = TRUE;
1779 if (argvars[1].v_type != VAR_UNKNOWN)
1780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001781 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001782 if (buttons == NULL)
1783 error = TRUE;
1784 if (argvars[2].v_type != VAR_UNKNOWN)
1785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001786 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001787 if (argvars[3].v_type != VAR_UNKNOWN)
1788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001789 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001790 if (typestr == NULL)
1791 error = TRUE;
1792 else
1793 {
1794 switch (TOUPPER_ASC(*typestr))
1795 {
1796 case 'E': type = VIM_ERROR; break;
1797 case 'Q': type = VIM_QUESTION; break;
1798 case 'I': type = VIM_INFO; break;
1799 case 'W': type = VIM_WARNING; break;
1800 case 'G': type = VIM_GENERIC; break;
1801 }
1802 }
1803 }
1804 }
1805 }
1806
1807 if (buttons == NULL || *buttons == NUL)
1808 buttons = (char_u *)_("&Ok");
1809
1810 if (!error)
1811 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1812 def, NULL, FALSE);
1813#endif
1814}
1815
1816/*
1817 * "copy()" function
1818 */
1819 static void
1820f_copy(typval_T *argvars, typval_T *rettv)
1821{
1822 item_copy(&argvars[0], rettv, FALSE, 0);
1823}
1824
1825#ifdef FEAT_FLOAT
1826/*
1827 * "cos()" function
1828 */
1829 static void
1830f_cos(typval_T *argvars, typval_T *rettv)
1831{
1832 float_T f = 0.0;
1833
1834 rettv->v_type = VAR_FLOAT;
1835 if (get_float_arg(argvars, &f) == OK)
1836 rettv->vval.v_float = cos(f);
1837 else
1838 rettv->vval.v_float = 0.0;
1839}
1840
1841/*
1842 * "cosh()" function
1843 */
1844 static void
1845f_cosh(typval_T *argvars, typval_T *rettv)
1846{
1847 float_T f = 0.0;
1848
1849 rettv->v_type = VAR_FLOAT;
1850 if (get_float_arg(argvars, &f) == OK)
1851 rettv->vval.v_float = cosh(f);
1852 else
1853 rettv->vval.v_float = 0.0;
1854}
1855#endif
1856
1857/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858 * "cursor(lnum, col)" function, or
1859 * "cursor(list)"
1860 *
1861 * Moves the cursor to the specified line and column.
1862 * Returns 0 when the position could be set, -1 otherwise.
1863 */
1864 static void
1865f_cursor(typval_T *argvars, typval_T *rettv)
1866{
1867 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869 int set_curswant = TRUE;
1870
1871 rettv->vval.v_number = -1;
1872 if (argvars[1].v_type == VAR_UNKNOWN)
1873 {
1874 pos_T pos;
1875 colnr_T curswant = -1;
1876
1877 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001879 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 return;
1881 }
1882 line = pos.lnum;
1883 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 if (curswant >= 0)
1886 {
1887 curwin->w_curswant = curswant - 1;
1888 set_curswant = FALSE;
1889 }
1890 }
1891 else
1892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001893 line = tv_get_lnum(argvars);
1894 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001898 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 if (line > 0)
1901 curwin->w_cursor.lnum = line;
1902 if (col > 0)
1903 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001906 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 if (has_mbyte)
1910 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911
1912 curwin->w_set_curswant = set_curswant;
1913 rettv->vval.v_number = 0;
1914}
1915
Bram Moolenaar4f974752019-02-17 17:44:42 +01001916#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001917/*
1918 * "debugbreak()" function
1919 */
1920 static void
1921f_debugbreak(typval_T *argvars, typval_T *rettv)
1922{
1923 int pid;
1924
1925 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001926 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001927 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001928 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001929 else
1930 {
1931 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1932
1933 if (hProcess != NULL)
1934 {
1935 DebugBreakProcess(hProcess);
1936 CloseHandle(hProcess);
1937 rettv->vval.v_number = OK;
1938 }
1939 }
1940}
1941#endif
1942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943/*
1944 * "deepcopy()" function
1945 */
1946 static void
1947f_deepcopy(typval_T *argvars, typval_T *rettv)
1948{
1949 int noref = 0;
1950 int copyID;
1951
1952 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001953 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001954 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001955 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001956 else
1957 {
1958 copyID = get_copyID();
1959 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1960 }
1961}
1962
1963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 * "did_filetype()" function
1965 */
1966 static void
1967f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1968{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001969 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001970}
1971
1972/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001973 * "echoraw({expr})" function
1974 */
1975 static void
1976f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1977{
1978 char_u *str = tv_get_string_chk(&argvars[0]);
1979
1980 if (str != NULL && *str != NUL)
1981 {
1982 out_str(str);
1983 out_flush();
1984 }
1985}
1986
1987/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 * "empty({expr})" function
1989 */
1990 static void
1991f_empty(typval_T *argvars, typval_T *rettv)
1992{
1993 int n = FALSE;
1994
1995 switch (argvars[0].v_type)
1996 {
1997 case VAR_STRING:
1998 case VAR_FUNC:
1999 n = argvars[0].vval.v_string == NULL
2000 || *argvars[0].vval.v_string == NUL;
2001 break;
2002 case VAR_PARTIAL:
2003 n = FALSE;
2004 break;
2005 case VAR_NUMBER:
2006 n = argvars[0].vval.v_number == 0;
2007 break;
2008 case VAR_FLOAT:
2009#ifdef FEAT_FLOAT
2010 n = argvars[0].vval.v_float == 0.0;
2011 break;
2012#endif
2013 case VAR_LIST:
2014 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002015 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 break;
2017 case VAR_DICT:
2018 n = argvars[0].vval.v_dict == NULL
2019 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2020 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002021 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002022 case VAR_SPECIAL:
2023 n = argvars[0].vval.v_number != VVAL_TRUE;
2024 break;
2025
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002026 case VAR_BLOB:
2027 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002028 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2029 break;
2030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002031 case VAR_JOB:
2032#ifdef FEAT_JOB_CHANNEL
2033 n = argvars[0].vval.v_job == NULL
2034 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2035 break;
2036#endif
2037 case VAR_CHANNEL:
2038#ifdef FEAT_JOB_CHANNEL
2039 n = argvars[0].vval.v_channel == NULL
2040 || !channel_is_open(argvars[0].vval.v_channel);
2041 break;
2042#endif
2043 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002044 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002046 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002047 n = TRUE;
2048 break;
2049 }
2050
2051 rettv->vval.v_number = n;
2052}
2053
2054/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002055 * "environ()" function
2056 */
2057 static void
2058f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2059{
2060#if !defined(AMIGA)
2061 int i = 0;
2062 char_u *entry, *value;
2063# ifdef MSWIN
2064 extern wchar_t **_wenviron;
2065# else
2066 extern char **environ;
2067# endif
2068
2069 if (rettv_dict_alloc(rettv) != OK)
2070 return;
2071
2072# ifdef MSWIN
2073 if (*_wenviron == NULL)
2074 return;
2075# else
2076 if (*environ == NULL)
2077 return;
2078# endif
2079
2080 for (i = 0; ; ++i)
2081 {
2082# ifdef MSWIN
2083 short_u *p;
2084
2085 if ((p = (short_u *)_wenviron[i]) == NULL)
2086 return;
2087 entry = utf16_to_enc(p, NULL);
2088# else
2089 if ((entry = (char_u *)environ[i]) == NULL)
2090 return;
2091 entry = vim_strsave(entry);
2092# endif
2093 if (entry == NULL) // out of memory
2094 return;
2095 if ((value = vim_strchr(entry, '=')) == NULL)
2096 {
2097 vim_free(entry);
2098 continue;
2099 }
2100 *value++ = NUL;
2101 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2102 vim_free(entry);
2103 }
2104#endif
2105}
2106
2107/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108 * "escape({string}, {chars})" function
2109 */
2110 static void
2111f_escape(typval_T *argvars, typval_T *rettv)
2112{
2113 char_u buf[NUMBUFLEN];
2114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002115 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2116 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002117 rettv->v_type = VAR_STRING;
2118}
2119
2120/*
2121 * "eval()" function
2122 */
2123 static void
2124f_eval(typval_T *argvars, typval_T *rettv)
2125{
2126 char_u *s, *p;
2127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002128 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002129 if (s != NULL)
2130 s = skipwhite(s);
2131
2132 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002134 {
2135 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002137 need_clr_eos = FALSE;
2138 rettv->v_type = VAR_NUMBER;
2139 rettv->vval.v_number = 0;
2140 }
2141 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002142 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143}
2144
2145/*
2146 * "eventhandler()" function
2147 */
2148 static void
2149f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2150{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002151 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152}
2153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154static garray_T redir_execute_ga;
2155
2156/*
2157 * Append "value[value_len]" to the execute() output.
2158 */
2159 void
2160execute_redir_str(char_u *value, int value_len)
2161{
2162 int len;
2163
2164 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002165 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002167 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 if (ga_grow(&redir_execute_ga, len) == OK)
2169 {
2170 mch_memmove((char *)redir_execute_ga.ga_data
2171 + redir_execute_ga.ga_len, value, len);
2172 redir_execute_ga.ga_len += len;
2173 }
2174}
2175
2176/*
2177 * Get next line from a list.
2178 * Called by do_cmdline() to get the next line.
2179 * Returns allocated string, or NULL for end of function.
2180 */
2181
2182 static char_u *
2183get_list_line(
2184 int c UNUSED,
2185 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002186 int indent UNUSED,
2187 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188{
2189 listitem_T **p = (listitem_T **)cookie;
2190 listitem_T *item = *p;
2191 char_u buf[NUMBUFLEN];
2192 char_u *s;
2193
2194 if (item == NULL)
2195 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002196 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002197 *p = item->li_next;
2198 return s == NULL ? NULL : vim_strsave(s);
2199}
2200
2201/*
2202 * "execute()" function
2203 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002204 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002205execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206{
2207 char_u *cmd = NULL;
2208 list_T *list = NULL;
2209 int save_msg_silent = msg_silent;
2210 int save_emsg_silent = emsg_silent;
2211 int save_emsg_noredir = emsg_noredir;
2212 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002213 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002214 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002215 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002216 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217
2218 rettv->vval.v_string = NULL;
2219 rettv->v_type = VAR_STRING;
2220
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002221 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002223 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002224 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002225 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 return;
2227 ++list->lv_refcount;
2228 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002229 else if (argvars[arg_off].v_type == VAR_JOB
2230 || argvars[arg_off].v_type == VAR_CHANNEL)
2231 {
2232 emsg(_(e_inval_string));
2233 return;
2234 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 else
2236 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002237 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002238 if (cmd == NULL)
2239 return;
2240 }
2241
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002242 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002243 {
2244 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002245 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246
2247 if (s == NULL)
2248 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002249 if (*s == NUL)
2250 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002251 if (STRNCMP(s, "silent", 6) == 0)
2252 ++msg_silent;
2253 if (STRCMP(s, "silent!") == 0)
2254 {
2255 emsg_silent = TRUE;
2256 emsg_noredir = TRUE;
2257 }
2258 }
2259 else
2260 ++msg_silent;
2261
2262 if (redir_execute)
2263 save_ga = redir_execute_ga;
2264 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2265 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002266 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002267 if (!echo_output)
2268 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002269
2270 if (cmd != NULL)
2271 do_cmdline_cmd(cmd);
2272 else
2273 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002274 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002275
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002276 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002277 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278 do_cmdline(NULL, get_list_line, (void *)&item,
2279 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2280 --list->lv_refcount;
2281 }
2282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002283 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002284 if (ga_grow(&redir_execute_ga, 1) == OK)
2285 {
2286 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2287 rettv->vval.v_string = redir_execute_ga.ga_data;
2288 }
2289 else
2290 {
2291 ga_clear(&redir_execute_ga);
2292 rettv->vval.v_string = NULL;
2293 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294 msg_silent = save_msg_silent;
2295 emsg_silent = save_emsg_silent;
2296 emsg_noredir = save_emsg_noredir;
2297
2298 redir_execute = save_redir_execute;
2299 if (redir_execute)
2300 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002301 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002303 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002304 if (echo_output)
2305 // When not working silently: put it in column zero. A following
2306 // "echon" will overwrite the message, unavoidably.
2307 msg_col = 0;
2308 else
2309 // When working silently: Put it back where it was, since nothing
2310 // should have been written.
2311 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002312}
2313
2314/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002315 * "execute()" function
2316 */
2317 static void
2318f_execute(typval_T *argvars, typval_T *rettv)
2319{
2320 execute_common(argvars, rettv, 0);
2321}
2322
2323/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002324 * "exists()" function
2325 */
2326 static void
2327f_exists(typval_T *argvars, typval_T *rettv)
2328{
2329 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002330 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002332 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002333 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002335 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002336 if (mch_getenv(p + 1) != NULL)
2337 n = TRUE;
2338 else
2339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002340 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341 p = expand_env_save(p);
2342 if (p != NULL && *p != '$')
2343 n = TRUE;
2344 vim_free(p);
2345 }
2346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002347 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002348 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002349 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002351 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002353 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002355 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002357 else if (*p == '?') // internal function only
2358 {
2359 n = has_internal_func_name(p + 1);
2360 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 else if (*p == ':')
2362 {
2363 n = cmd_exists(p + 1);
2364 }
2365 else if (*p == '#')
2366 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367 if (p[1] == '#')
2368 n = autocmd_supported(p + 2);
2369 else
2370 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002372 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002374 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 }
2376
2377 rettv->vval.v_number = n;
2378}
2379
2380#ifdef FEAT_FLOAT
2381/*
2382 * "exp()" function
2383 */
2384 static void
2385f_exp(typval_T *argvars, typval_T *rettv)
2386{
2387 float_T f = 0.0;
2388
2389 rettv->v_type = VAR_FLOAT;
2390 if (get_float_arg(argvars, &f) == OK)
2391 rettv->vval.v_float = exp(f);
2392 else
2393 rettv->vval.v_float = 0.0;
2394}
2395#endif
2396
2397/*
2398 * "expand()" function
2399 */
2400 static void
2401f_expand(typval_T *argvars, typval_T *rettv)
2402{
2403 char_u *s;
2404 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2407 expand_T xpc;
2408 int error = FALSE;
2409 char_u *result;
2410
2411 rettv->v_type = VAR_STRING;
2412 if (argvars[1].v_type != VAR_UNKNOWN
2413 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002414 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002416 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002418 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419 if (*s == '%' || *s == '#' || *s == '<')
2420 {
2421 ++emsg_off;
2422 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2423 --emsg_off;
2424 if (rettv->v_type == VAR_LIST)
2425 {
2426 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2427 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002428 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 }
2430 else
2431 rettv->vval.v_string = result;
2432 }
2433 else
2434 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002435 // When the optional second argument is non-zero, don't remove matches
2436 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002438 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002439 options |= WILD_KEEP_ALL;
2440 if (!error)
2441 {
2442 ExpandInit(&xpc);
2443 xpc.xp_context = EXPAND_FILES;
2444 if (p_wic)
2445 options += WILD_ICASE;
2446 if (rettv->v_type == VAR_STRING)
2447 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2448 options, WILD_ALL);
2449 else if (rettv_list_alloc(rettv) != FAIL)
2450 {
2451 int i;
2452
2453 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2454 for (i = 0; i < xpc.xp_numfiles; i++)
2455 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2456 ExpandCleanup(&xpc);
2457 }
2458 }
2459 else
2460 rettv->vval.v_string = NULL;
2461 }
2462}
2463
2464/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002465 * "expandcmd()" function
2466 * Expand all the special characters in a command string.
2467 */
2468 static void
2469f_expandcmd(typval_T *argvars, typval_T *rettv)
2470{
2471 exarg_T eap;
2472 char_u *cmdstr;
2473 char *errormsg = NULL;
2474
2475 rettv->v_type = VAR_STRING;
2476 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2477
2478 memset(&eap, 0, sizeof(eap));
2479 eap.cmd = cmdstr;
2480 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002481 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002482 eap.usefilter = FALSE;
2483 eap.nextcmd = NULL;
2484 eap.cmdidx = CMD_USER;
2485
2486 expand_filename(&eap, &cmdstr, &errormsg);
2487 if (errormsg != NULL && *errormsg != NUL)
2488 emsg(errormsg);
2489
2490 rettv->vval.v_string = cmdstr;
2491}
2492
2493/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002494 * "feedkeys()" function
2495 */
2496 static void
2497f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2498{
2499 int remap = TRUE;
2500 int insert = FALSE;
2501 char_u *keys, *flags;
2502 char_u nbuf[NUMBUFLEN];
2503 int typed = FALSE;
2504 int execute = FALSE;
2505 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002506 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002507 char_u *keys_esc;
2508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002509 // This is not allowed in the sandbox. If the commands would still be
2510 // executed in the sandbox it would be OK, but it probably happens later,
2511 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002512 if (check_secure())
2513 return;
2514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002515 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002516
2517 if (argvars[1].v_type != VAR_UNKNOWN)
2518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002519 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 for ( ; *flags != NUL; ++flags)
2521 {
2522 switch (*flags)
2523 {
2524 case 'n': remap = FALSE; break;
2525 case 'm': remap = TRUE; break;
2526 case 't': typed = TRUE; break;
2527 case 'i': insert = TRUE; break;
2528 case 'x': execute = TRUE; break;
2529 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002530 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 }
2532 }
2533 }
2534
2535 if (*keys != NUL || execute)
2536 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002537 // Need to escape K_SPECIAL and CSI before putting the string in the
2538 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 keys_esc = vim_strsave_escape_csi(keys);
2540 if (keys_esc != NULL)
2541 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002542 if (lowlevel)
2543 {
2544#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002545 int idx;
2546 int len = (int)STRLEN(keys);
2547
2548 for (idx = 0; idx < len; ++idx)
2549 {
2550 // if a CTRL-C was typed, set got_int, similar to what
2551 // happens in fill_input_buf()
2552 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2553 got_int = TRUE;
2554 add_to_input_buf(keys + idx, 1);
2555 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002556#else
2557 emsg(_("E980: lowlevel input not supported"));
2558#endif
2559 }
2560 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002561 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002562 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002564 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002565#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002566 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002567#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002568 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002569 typebuf_was_filled = TRUE;
2570 }
2571 vim_free(keys_esc);
2572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002573 if (execute)
2574 {
2575 int save_msg_scroll = msg_scroll;
2576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002577 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 msg_scroll = FALSE;
2579
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002580 if (!dangerous)
2581 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002582 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002583 if (!dangerous)
2584 --ex_normal_busy;
2585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 msg_scroll |= save_msg_scroll;
2587 }
2588 }
2589 }
2590}
2591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592#ifdef FEAT_FLOAT
2593/*
2594 * "float2nr({float})" function
2595 */
2596 static void
2597f_float2nr(typval_T *argvars, typval_T *rettv)
2598{
2599 float_T f = 0.0;
2600
2601 if (get_float_arg(argvars, &f) == OK)
2602 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002603 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002604 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002605 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002606 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 else
2608 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609 }
2610}
2611
2612/*
2613 * "floor({float})" function
2614 */
2615 static void
2616f_floor(typval_T *argvars, typval_T *rettv)
2617{
2618 float_T f = 0.0;
2619
2620 rettv->v_type = VAR_FLOAT;
2621 if (get_float_arg(argvars, &f) == OK)
2622 rettv->vval.v_float = floor(f);
2623 else
2624 rettv->vval.v_float = 0.0;
2625}
2626
2627/*
2628 * "fmod()" function
2629 */
2630 static void
2631f_fmod(typval_T *argvars, typval_T *rettv)
2632{
2633 float_T fx = 0.0, fy = 0.0;
2634
2635 rettv->v_type = VAR_FLOAT;
2636 if (get_float_arg(argvars, &fx) == OK
2637 && get_float_arg(&argvars[1], &fy) == OK)
2638 rettv->vval.v_float = fmod(fx, fy);
2639 else
2640 rettv->vval.v_float = 0.0;
2641}
2642#endif
2643
2644/*
2645 * "fnameescape({string})" function
2646 */
2647 static void
2648f_fnameescape(typval_T *argvars, typval_T *rettv)
2649{
2650 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002651 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002652 rettv->v_type = VAR_STRING;
2653}
2654
2655/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 * "foreground()" function
2657 */
2658 static void
2659f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2660{
2661#ifdef FEAT_GUI
2662 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002663 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002665 return;
2666 }
2667#endif
2668#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670#endif
2671}
2672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002674common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675{
2676 char_u *s;
2677 char_u *name;
2678 int use_string = FALSE;
2679 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002680 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002681 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682
2683 if (argvars[0].v_type == VAR_FUNC)
2684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002685 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 s = argvars[0].vval.v_string;
2687 }
2688 else if (argvars[0].v_type == VAR_PARTIAL
2689 && argvars[0].vval.v_partial != NULL)
2690 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002691 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002693 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002694 }
2695 else
2696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002698 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 use_string = TRUE;
2700 }
2701
Bram Moolenaar843b8842016-08-21 14:36:15 +02002702 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002703 {
2704 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002705 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002706 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2707 if (*name != NUL)
2708 s = NULL;
2709 }
2710
Bram Moolenaar843b8842016-08-21 14:36:15 +02002711 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2712 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002713 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002714 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002715 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002716 ? find_func(trans_name, is_global, NULL) == NULL
2717 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002718 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 else
2720 {
2721 int dict_idx = 0;
2722 int arg_idx = 0;
2723 list_T *list = NULL;
2724
2725 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2726 {
2727 char sid_buf[25];
2728 int off = *s == 's' ? 2 : 5;
2729
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002730 // Expand s: and <SID> into <SNR>nr_, so that the function can
2731 // also be called from another script. Using trans_function_name()
2732 // would also work, but some plugins depend on the name being
2733 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002734 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002735 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002736 if (name != NULL)
2737 {
2738 STRCPY(name, sid_buf);
2739 STRCAT(name, s + off);
2740 }
2741 }
2742 else
2743 name = vim_strsave(s);
2744
2745 if (argvars[1].v_type != VAR_UNKNOWN)
2746 {
2747 if (argvars[2].v_type != VAR_UNKNOWN)
2748 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 arg_idx = 1;
2751 dict_idx = 2;
2752 }
2753 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002754 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 dict_idx = 1;
2756 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002757 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758 arg_idx = 1;
2759 if (dict_idx > 0)
2760 {
2761 if (argvars[dict_idx].v_type != VAR_DICT)
2762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002763 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002765 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 }
2767 if (argvars[dict_idx].vval.v_dict == NULL)
2768 dict_idx = 0;
2769 }
2770 if (arg_idx > 0)
2771 {
2772 if (argvars[arg_idx].v_type != VAR_LIST)
2773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002774 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002776 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 }
2778 list = argvars[arg_idx].vval.v_list;
2779 if (list == NULL || list->lv_len == 0)
2780 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002781 else if (list->lv_len > MAX_FUNC_ARGS)
2782 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002783 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002784 vim_free(name);
2785 goto theend;
2786 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 }
2788 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002789 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002791 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 if (pt == NULL)
2795 vim_free(name);
2796 else
2797 {
2798 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2799 {
2800 listitem_T *li;
2801 int i = 0;
2802 int arg_len = 0;
2803 int lv_len = 0;
2804
2805 if (arg_pt != NULL)
2806 arg_len = arg_pt->pt_argc;
2807 if (list != NULL)
2808 lv_len = list->lv_len;
2809 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002810 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 if (pt->pt_argv == NULL)
2812 {
2813 vim_free(pt);
2814 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002815 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002816 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002817 for (i = 0; i < arg_len; i++)
2818 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2819 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002820 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002821 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002822 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002823 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002824 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002825 }
2826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002827 // For "function(dict.func, [], dict)" and "func" is a partial
2828 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002829 if (dict_idx > 0)
2830 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002831 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2833 ++pt->pt_dict->dv_refcount;
2834 }
2835 else if (arg_pt != NULL)
2836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002837 // If the dict was bound automatically the result is also
2838 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002839 pt->pt_dict = arg_pt->pt_dict;
2840 pt->pt_auto = arg_pt->pt_auto;
2841 if (pt->pt_dict != NULL)
2842 ++pt->pt_dict->dv_refcount;
2843 }
2844
2845 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002846 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2847 {
2848 pt->pt_func = arg_pt->pt_func;
2849 func_ptr_ref(pt->pt_func);
2850 vim_free(name);
2851 }
2852 else if (is_funcref)
2853 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002854 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002855 func_ptr_ref(pt->pt_func);
2856 vim_free(name);
2857 }
2858 else
2859 {
2860 pt->pt_name = name;
2861 func_ref(name);
2862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002863 }
2864 rettv->v_type = VAR_PARTIAL;
2865 rettv->vval.v_partial = pt;
2866 }
2867 else
2868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002869 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 rettv->v_type = VAR_FUNC;
2871 rettv->vval.v_string = name;
2872 func_ref(name);
2873 }
2874 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875theend:
2876 vim_free(trans_name);
2877}
2878
2879/*
2880 * "funcref()" function
2881 */
2882 static void
2883f_funcref(typval_T *argvars, typval_T *rettv)
2884{
2885 common_function(argvars, rettv, TRUE);
2886}
2887
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002888 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002889ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002890{
2891 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2892 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002893 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002894}
2895
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002896/*
2897 * "function()" function
2898 */
2899 static void
2900f_function(typval_T *argvars, typval_T *rettv)
2901{
2902 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903}
2904
2905/*
2906 * "garbagecollect()" function
2907 */
2908 static void
2909f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2910{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002911 // This is postponed until we are back at the toplevel, because we may be
2912 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 want_garbage_collect = TRUE;
2914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002915 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 garbage_collect_at_exit = TRUE;
2917}
2918
2919/*
2920 * "get()" function
2921 */
2922 static void
2923f_get(typval_T *argvars, typval_T *rettv)
2924{
2925 listitem_T *li;
2926 list_T *l;
2927 dictitem_T *di;
2928 dict_T *d;
2929 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002930 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002932 if (argvars[0].v_type == VAR_BLOB)
2933 {
2934 int error = FALSE;
2935 int idx = tv_get_number_chk(&argvars[1], &error);
2936
2937 if (!error)
2938 {
2939 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002940 if (idx < 0)
2941 idx = blob_len(argvars[0].vval.v_blob) + idx;
2942 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2943 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002944 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002945 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002947 tv = rettv;
2948 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002949 }
2950 }
2951 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 {
2953 if ((l = argvars[0].vval.v_list) != NULL)
2954 {
2955 int error = FALSE;
2956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002957 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958 if (!error && li != NULL)
2959 tv = &li->li_tv;
2960 }
2961 }
2962 else if (argvars[0].v_type == VAR_DICT)
2963 {
2964 if ((d = argvars[0].vval.v_dict) != NULL)
2965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002966 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002967 if (di != NULL)
2968 tv = &di->di_tv;
2969 }
2970 }
2971 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2972 {
2973 partial_T *pt;
2974 partial_T fref_pt;
2975
2976 if (argvars[0].v_type == VAR_PARTIAL)
2977 pt = argvars[0].vval.v_partial;
2978 else
2979 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002980 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 fref_pt.pt_name = argvars[0].vval.v_string;
2982 pt = &fref_pt;
2983 }
2984
2985 if (pt != NULL)
2986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002987 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002988 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989
2990 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
2991 {
2992 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002993 n = partial_name(pt);
2994 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 rettv->vval.v_string = NULL;
2996 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002997 {
2998 rettv->vval.v_string = vim_strsave(n);
2999 if (rettv->v_type == VAR_FUNC)
3000 func_ref(rettv->vval.v_string);
3001 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 }
3003 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003004 {
3005 what_is_dict = TRUE;
3006 if (pt->pt_dict != NULL)
3007 rettv_dict_set(rettv, pt->pt_dict);
3008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 else if (STRCMP(what, "args") == 0)
3010 {
3011 rettv->v_type = VAR_LIST;
3012 if (rettv_list_alloc(rettv) == OK)
3013 {
3014 int i;
3015
3016 for (i = 0; i < pt->pt_argc; ++i)
3017 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3018 }
3019 }
3020 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003022
3023 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3024 // third argument
3025 if (!what_is_dict)
3026 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 }
3028 }
3029 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003030 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031
3032 if (tv == NULL)
3033 {
3034 if (argvars[2].v_type != VAR_UNKNOWN)
3035 copy_tv(&argvars[2], rettv);
3036 }
3037 else
3038 copy_tv(tv, rettv);
3039}
3040
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003041/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003042 * "getchangelist()" function
3043 */
3044 static void
3045f_getchangelist(typval_T *argvars, typval_T *rettv)
3046{
3047#ifdef FEAT_JUMPLIST
3048 buf_T *buf;
3049 int i;
3050 list_T *l;
3051 dict_T *d;
3052#endif
3053
3054 if (rettv_list_alloc(rettv) != OK)
3055 return;
3056
3057#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003058 if (argvars[0].v_type == VAR_UNKNOWN)
3059 buf = curbuf;
3060 else
3061 {
3062 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3063 ++emsg_off;
3064 buf = tv_get_buf(&argvars[0], FALSE);
3065 --emsg_off;
3066 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003067 if (buf == NULL)
3068 return;
3069
3070 l = list_alloc();
3071 if (l == NULL)
3072 return;
3073
3074 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3075 return;
3076 /*
3077 * The current window change list index tracks only the position in the
3078 * current buffer change list. For other buffers, use the change list
3079 * length as the current index.
3080 */
3081 list_append_number(rettv->vval.v_list,
3082 (varnumber_T)((buf == curwin->w_buffer)
3083 ? curwin->w_changelistidx : buf->b_changelistlen));
3084
3085 for (i = 0; i < buf->b_changelistlen; ++i)
3086 {
3087 if (buf->b_changelist[i].lnum == 0)
3088 continue;
3089 if ((d = dict_alloc()) == NULL)
3090 return;
3091 if (list_append_dict(l, d) == FAIL)
3092 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003093 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3094 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003095 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003096 }
3097#endif
3098}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099
3100/*
3101 * "getcharsearch()" function
3102 */
3103 static void
3104f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3105{
3106 if (rettv_dict_alloc(rettv) != FAIL)
3107 {
3108 dict_T *dict = rettv->vval.v_dict;
3109
Bram Moolenaare0be1672018-07-08 16:50:37 +02003110 dict_add_string(dict, "char", last_csearch());
3111 dict_add_number(dict, "forward", last_csearch_forward());
3112 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 }
3114}
3115
3116/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003117 * "getenv()" function
3118 */
3119 static void
3120f_getenv(typval_T *argvars, typval_T *rettv)
3121{
3122 int mustfree = FALSE;
3123 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3124
3125 if (p == NULL)
3126 {
3127 rettv->v_type = VAR_SPECIAL;
3128 rettv->vval.v_number = VVAL_NULL;
3129 return;
3130 }
3131 if (!mustfree)
3132 p = vim_strsave(p);
3133 rettv->vval.v_string = p;
3134 rettv->v_type = VAR_STRING;
3135}
3136
3137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138 * "getfontname()" function
3139 */
3140 static void
3141f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3142{
3143 rettv->v_type = VAR_STRING;
3144 rettv->vval.v_string = NULL;
3145#ifdef FEAT_GUI
3146 if (gui.in_use)
3147 {
3148 GuiFont font;
3149 char_u *name = NULL;
3150
3151 if (argvars[0].v_type == VAR_UNKNOWN)
3152 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003153 // Get the "Normal" font. Either the name saved by
3154 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003155 font = gui.norm_font;
3156 name = hl_get_font_name();
3157 }
3158 else
3159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003160 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003161 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 return;
3163 font = gui_mch_get_font(name, FALSE);
3164 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003165 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 }
3167 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3168 if (argvars[0].v_type != VAR_UNKNOWN)
3169 gui_mch_free_font(font);
3170 }
3171#endif
3172}
3173
3174/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003175 * "getjumplist()" function
3176 */
3177 static void
3178f_getjumplist(typval_T *argvars, typval_T *rettv)
3179{
3180#ifdef FEAT_JUMPLIST
3181 win_T *wp;
3182 int i;
3183 list_T *l;
3184 dict_T *d;
3185#endif
3186
3187 if (rettv_list_alloc(rettv) != OK)
3188 return;
3189
3190#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003191 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003192 if (wp == NULL)
3193 return;
3194
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003195 cleanup_jumplist(wp, TRUE);
3196
Bram Moolenaar4f505882018-02-10 21:06:32 +01003197 l = list_alloc();
3198 if (l == NULL)
3199 return;
3200
3201 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3202 return;
3203 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3204
3205 for (i = 0; i < wp->w_jumplistlen; ++i)
3206 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003207 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3208 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003209 if ((d = dict_alloc()) == NULL)
3210 return;
3211 if (list_append_dict(l, d) == FAIL)
3212 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003213 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3214 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003215 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003216 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003217 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003218 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003219 }
3220#endif
3221}
3222
3223/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 * "getpid()" function
3225 */
3226 static void
3227f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3228{
3229 rettv->vval.v_number = mch_get_pid();
3230}
3231
3232 static void
3233getpos_both(
3234 typval_T *argvars,
3235 typval_T *rettv,
3236 int getcurpos)
3237{
3238 pos_T *fp;
3239 list_T *l;
3240 int fnum = -1;
3241
3242 if (rettv_list_alloc(rettv) == OK)
3243 {
3244 l = rettv->vval.v_list;
3245 if (getcurpos)
3246 fp = &curwin->w_cursor;
3247 else
3248 fp = var2fpos(&argvars[0], TRUE, &fnum);
3249 if (fnum != -1)
3250 list_append_number(l, (varnumber_T)fnum);
3251 else
3252 list_append_number(l, (varnumber_T)0);
3253 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3254 : (varnumber_T)0);
3255 list_append_number(l, (fp != NULL)
3256 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3257 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003258 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 (varnumber_T)0);
3260 if (getcurpos)
3261 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003262 int save_set_curswant = curwin->w_set_curswant;
3263 colnr_T save_curswant = curwin->w_curswant;
3264 colnr_T save_virtcol = curwin->w_virtcol;
3265
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 update_curswant();
3267 list_append_number(l, curwin->w_curswant == MAXCOL ?
3268 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003269
3270 // Do not change "curswant", as it is unexpected that a get
3271 // function has a side effect.
3272 if (save_set_curswant)
3273 {
3274 curwin->w_set_curswant = save_set_curswant;
3275 curwin->w_curswant = save_curswant;
3276 curwin->w_virtcol = save_virtcol;
3277 curwin->w_valid &= ~VALID_VIRTCOL;
3278 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 }
3280 }
3281 else
3282 rettv->vval.v_number = FALSE;
3283}
3284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285/*
3286 * "getcurpos()" function
3287 */
3288 static void
3289f_getcurpos(typval_T *argvars, typval_T *rettv)
3290{
3291 getpos_both(argvars, rettv, TRUE);
3292}
3293
3294/*
3295 * "getpos(string)" function
3296 */
3297 static void
3298f_getpos(typval_T *argvars, typval_T *rettv)
3299{
3300 getpos_both(argvars, rettv, FALSE);
3301}
3302
3303/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003304 * "getreg()" function
3305 */
3306 static void
3307f_getreg(typval_T *argvars, typval_T *rettv)
3308{
3309 char_u *strregname;
3310 int regname;
3311 int arg2 = FALSE;
3312 int return_list = FALSE;
3313 int error = FALSE;
3314
3315 if (argvars[0].v_type != VAR_UNKNOWN)
3316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003317 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003318 error = strregname == NULL;
3319 if (argvars[1].v_type != VAR_UNKNOWN)
3320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003321 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003323 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 }
3325 }
3326 else
3327 strregname = get_vim_var_str(VV_REG);
3328
3329 if (error)
3330 return;
3331
3332 regname = (strregname == NULL ? '"' : *strregname);
3333 if (regname == 0)
3334 regname = '"';
3335
3336 if (return_list)
3337 {
3338 rettv->v_type = VAR_LIST;
3339 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3340 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3341 if (rettv->vval.v_list == NULL)
3342 (void)rettv_list_alloc(rettv);
3343 else
3344 ++rettv->vval.v_list->lv_refcount;
3345 }
3346 else
3347 {
3348 rettv->v_type = VAR_STRING;
3349 rettv->vval.v_string = get_reg_contents(regname,
3350 arg2 ? GREG_EXPR_SRC : 0);
3351 }
3352}
3353
3354/*
3355 * "getregtype()" function
3356 */
3357 static void
3358f_getregtype(typval_T *argvars, typval_T *rettv)
3359{
3360 char_u *strregname;
3361 int regname;
3362 char_u buf[NUMBUFLEN + 2];
3363 long reglen = 0;
3364
3365 if (argvars[0].v_type != VAR_UNKNOWN)
3366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003367 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003368 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 {
3370 rettv->v_type = VAR_STRING;
3371 rettv->vval.v_string = NULL;
3372 return;
3373 }
3374 }
3375 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003376 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377 strregname = get_vim_var_str(VV_REG);
3378
3379 regname = (strregname == NULL ? '"' : *strregname);
3380 if (regname == 0)
3381 regname = '"';
3382
3383 buf[0] = NUL;
3384 buf[1] = NUL;
3385 switch (get_reg_type(regname, &reglen))
3386 {
3387 case MLINE: buf[0] = 'V'; break;
3388 case MCHAR: buf[0] = 'v'; break;
3389 case MBLOCK:
3390 buf[0] = Ctrl_V;
3391 sprintf((char *)buf + 1, "%ld", reglen + 1);
3392 break;
3393 }
3394 rettv->v_type = VAR_STRING;
3395 rettv->vval.v_string = vim_strsave(buf);
3396}
3397
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003398/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003399 * "gettagstack()" function
3400 */
3401 static void
3402f_gettagstack(typval_T *argvars, typval_T *rettv)
3403{
3404 win_T *wp = curwin; // default is current window
3405
3406 if (rettv_dict_alloc(rettv) != OK)
3407 return;
3408
3409 if (argvars[0].v_type != VAR_UNKNOWN)
3410 {
3411 wp = find_win_by_nr_or_id(&argvars[0]);
3412 if (wp == NULL)
3413 return;
3414 }
3415
3416 get_tagstack(wp, rettv->vval.v_dict);
3417}
3418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003419// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420#include "version.h"
3421
3422/*
3423 * "has()" function
3424 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003425 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426f_has(typval_T *argvars, typval_T *rettv)
3427{
3428 int i;
3429 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003430 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003432 typedef struct {
3433 char *name;
3434 short present;
3435 } has_item_T;
3436 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003438 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003440 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003441#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003442 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003443#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003444 },
3445 {"arp",
3446#if defined(AMIGA) && defined(FEAT_ARP)
3447 1
3448#else
3449 0
3450#endif
3451 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003452 {"haiku",
3453#ifdef __HAIKU__
3454 1
3455#else
3456 0
3457#endif
3458 },
3459 {"bsd",
3460#if defined(BSD) && !defined(MACOS_X)
3461 1
3462#else
3463 0
3464#endif
3465 },
3466 {"hpux",
3467#ifdef hpux
3468 1
3469#else
3470 0
3471#endif
3472 },
3473 {"linux",
3474#ifdef __linux__
3475 1
3476#else
3477 0
3478#endif
3479 },
3480 {"mac", // Mac OS X (and, once, Mac OS Classic)
3481#ifdef MACOS_X
3482 1
3483#else
3484 0
3485#endif
3486 },
3487 {"osx", // Mac OS X
3488#ifdef MACOS_X
3489 1
3490#else
3491 0
3492#endif
3493 },
3494 {"macunix", // Mac OS X, with the darwin feature
3495#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3496 1
3497#else
3498 0
3499#endif
3500 },
3501 {"osxdarwin", // synonym for macunix
3502#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3503 1
3504#else
3505 0
3506#endif
3507 },
3508 {"qnx",
3509#ifdef __QNX__
3510 1
3511#else
3512 0
3513#endif
3514 },
3515 {"sun",
3516#ifdef SUN_SYSTEM
3517 1
3518#else
3519 0
3520#endif
3521 },
3522 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003524 1
3525#else
3526 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003528 },
3529 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003531 1
3532#else
3533 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003535 },
3536 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003537#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003538 1
3539#else
3540 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003542 },
3543 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003544#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003545 1
3546#else
3547 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003549 },
3550 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003551#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003552 1
3553#else
3554 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003556 },
3557 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003559 1
3560#else
3561 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003563 },
3564 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003566 1
3567#else
3568 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003570 },
3571 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003573 1
3574#else
3575 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 },
3578 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003580 1
3581#else
3582 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003584 },
3585 {"autocmd", 1},
3586 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003587#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003588 1
3589#else
3590 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003592 },
3593 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003594#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 1
3596#else
3597 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003598#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 },
3600 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003601#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003602 1
3603#else
3604 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 },
3607 {"balloon_multiline",
3608#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3609 // MS-Windows requires runtime check, see below
3610 1
3611#else
3612 0
3613#endif
3614 },
3615 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003616#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 1
3618#else
3619 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003620#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003621 },
3622 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003624 1
3625#else
3626 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003628 },
3629 {"all_builtin_terms",
3630#if defined(ALL_BUILTIN_TCAPS)
3631 1
3632#else
3633 0
3634#endif
3635 },
3636 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003638 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003640 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003642 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003644 },
3645 {"byte_offset",
3646#ifdef FEAT_BYTEOFF
3647 1
3648#else
3649 0
3650#endif
3651 },
3652 {"channel",
3653#ifdef FEAT_JOB_CHANNEL
3654 1
3655#else
3656 0
3657#endif
3658 },
3659 {"cindent",
3660#ifdef FEAT_CINDENT
3661 1
3662#else
3663 0
3664#endif
3665 },
3666 {"clientserver",
3667#ifdef FEAT_CLIENTSERVER
3668 1
3669#else
3670 0
3671#endif
3672 },
3673 {"clipboard",
3674#ifdef FEAT_CLIPBOARD
3675 1
3676#else
3677 0
3678#endif
3679 },
3680 {"cmdline_compl", 1},
3681 {"cmdline_hist", 1},
3682 {"comments", 1},
3683 {"conceal",
3684#ifdef FEAT_CONCEAL
3685 1
3686#else
3687 0
3688#endif
3689 },
3690 {"cryptv",
3691#ifdef FEAT_CRYPT
3692 1
3693#else
3694 0
3695#endif
3696 },
3697 {"crypt-blowfish",
3698#ifdef FEAT_CRYPT
3699 1
3700#else
3701 0
3702#endif
3703 },
3704 {"crypt-blowfish2",
3705#ifdef FEAT_CRYPT
3706 1
3707#else
3708 0
3709#endif
3710 },
3711 {"cscope",
3712#ifdef FEAT_CSCOPE
3713 1
3714#else
3715 0
3716#endif
3717 },
3718 {"cursorbind", 1},
3719 {"cursorshape",
3720#ifdef CURSOR_SHAPE
3721 1
3722#else
3723 0
3724#endif
3725 },
3726 {"debug",
3727#ifdef DEBUG
3728 1
3729#else
3730 0
3731#endif
3732 },
3733 {"dialog_con",
3734#ifdef FEAT_CON_DIALOG
3735 1
3736#else
3737 0
3738#endif
3739 },
3740 {"dialog_gui",
3741#ifdef FEAT_GUI_DIALOG
3742 1
3743#else
3744 0
3745#endif
3746 },
3747 {"diff",
3748#ifdef FEAT_DIFF
3749 1
3750#else
3751 0
3752#endif
3753 },
3754 {"digraphs",
3755#ifdef FEAT_DIGRAPHS
3756 1
3757#else
3758 0
3759#endif
3760 },
3761 {"directx",
3762#ifdef FEAT_DIRECTX
3763 1
3764#else
3765 0
3766#endif
3767 },
3768 {"dnd",
3769#ifdef FEAT_DND
3770 1
3771#else
3772 0
3773#endif
3774 },
3775 {"emacs_tags",
3776#ifdef FEAT_EMACS_TAGS
3777 1
3778#else
3779 0
3780#endif
3781 },
3782 {"eval", 1}, // always present, of course!
3783 {"ex_extra", 1}, // graduated feature
3784 {"extra_search",
3785#ifdef FEAT_SEARCH_EXTRA
3786 1
3787#else
3788 0
3789#endif
3790 },
3791 {"file_in_path",
3792#ifdef FEAT_SEARCHPATH
3793 1
3794#else
3795 0
3796#endif
3797 },
3798 {"filterpipe",
3799#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3800 1
3801#else
3802 0
3803#endif
3804 },
3805 {"find_in_path",
3806#ifdef FEAT_FIND_ID
3807 1
3808#else
3809 0
3810#endif
3811 },
3812 {"float",
3813#ifdef FEAT_FLOAT
3814 1
3815#else
3816 0
3817#endif
3818 },
3819 {"folding",
3820#ifdef FEAT_FOLDING
3821 1
3822#else
3823 0
3824#endif
3825 },
3826 {"footer",
3827#ifdef FEAT_FOOTER
3828 1
3829#else
3830 0
3831#endif
3832 },
3833 {"fork",
3834#if !defined(USE_SYSTEM) && defined(UNIX)
3835 1
3836#else
3837 0
3838#endif
3839 },
3840 {"gettext",
3841#ifdef FEAT_GETTEXT
3842 1
3843#else
3844 0
3845#endif
3846 },
3847 {"gui",
3848#ifdef FEAT_GUI
3849 1
3850#else
3851 0
3852#endif
3853 },
3854 {"gui_neXtaw",
3855#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3856 1
3857#else
3858 0
3859#endif
3860 },
3861 {"gui_athena",
3862#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3863 1
3864#else
3865 0
3866#endif
3867 },
3868 {"gui_gtk",
3869#ifdef FEAT_GUI_GTK
3870 1
3871#else
3872 0
3873#endif
3874 },
3875 {"gui_gtk2",
3876#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3877 1
3878#else
3879 0
3880#endif
3881 },
3882 {"gui_gtk3",
3883#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3884 1
3885#else
3886 0
3887#endif
3888 },
3889 {"gui_gnome",
3890#ifdef FEAT_GUI_GNOME
3891 1
3892#else
3893 0
3894#endif
3895 },
3896 {"gui_haiku",
3897#ifdef FEAT_GUI_HAIKU
3898 1
3899#else
3900 0
3901#endif
3902 },
3903 {"gui_mac",
3904#ifdef FEAT_GUI_MAC
3905 1
3906#else
3907 0
3908#endif
3909 },
3910 {"gui_motif",
3911#ifdef FEAT_GUI_MOTIF
3912 1
3913#else
3914 0
3915#endif
3916 },
3917 {"gui_photon",
3918#ifdef FEAT_GUI_PHOTON
3919 1
3920#else
3921 0
3922#endif
3923 },
3924 {"gui_win32",
3925#ifdef FEAT_GUI_MSWIN
3926 1
3927#else
3928 0
3929#endif
3930 },
3931 {"iconv",
3932#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3933 1
3934#else
3935 0
3936#endif
3937 },
3938 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003939 {"ipv6",
3940#ifdef FEAT_IPV6
3941 1
3942#else
3943 0
3944#endif
3945 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003946 {"job",
3947#ifdef FEAT_JOB_CHANNEL
3948 1
3949#else
3950 0
3951#endif
3952 },
3953 {"jumplist",
3954#ifdef FEAT_JUMPLIST
3955 1
3956#else
3957 0
3958#endif
3959 },
3960 {"keymap",
3961#ifdef FEAT_KEYMAP
3962 1
3963#else
3964 0
3965#endif
3966 },
3967 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3968 {"langmap",
3969#ifdef FEAT_LANGMAP
3970 1
3971#else
3972 0
3973#endif
3974 },
3975 {"libcall",
3976#ifdef FEAT_LIBCALL
3977 1
3978#else
3979 0
3980#endif
3981 },
3982 {"linebreak",
3983#ifdef FEAT_LINEBREAK
3984 1
3985#else
3986 0
3987#endif
3988 },
3989 {"lispindent",
3990#ifdef FEAT_LISP
3991 1
3992#else
3993 0
3994#endif
3995 },
3996 {"listcmds", 1},
3997 {"localmap", 1},
3998 {"lua",
3999#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4000 1
4001#else
4002 0
4003#endif
4004 },
4005 {"menu",
4006#ifdef FEAT_MENU
4007 1
4008#else
4009 0
4010#endif
4011 },
4012 {"mksession",
4013#ifdef FEAT_SESSION
4014 1
4015#else
4016 0
4017#endif
4018 },
4019 {"modify_fname", 1},
4020 {"mouse", 1},
4021 {"mouseshape",
4022#ifdef FEAT_MOUSESHAPE
4023 1
4024#else
4025 0
4026#endif
4027 },
4028 {"mouse_dec",
4029#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4030 1
4031#else
4032 0
4033#endif
4034 },
4035 {"mouse_gpm",
4036#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4037 1
4038#else
4039 0
4040#endif
4041 },
4042 {"mouse_jsbterm",
4043#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4044 1
4045#else
4046 0
4047#endif
4048 },
4049 {"mouse_netterm",
4050#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4051 1
4052#else
4053 0
4054#endif
4055 },
4056 {"mouse_pterm",
4057#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4058 1
4059#else
4060 0
4061#endif
4062 },
4063 {"mouse_sgr",
4064#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4065 1
4066#else
4067 0
4068#endif
4069 },
4070 {"mouse_sysmouse",
4071#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4072 1
4073#else
4074 0
4075#endif
4076 },
4077 {"mouse_urxvt",
4078#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4079 1
4080#else
4081 0
4082#endif
4083 },
4084 {"mouse_xterm",
4085#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4086 1
4087#else
4088 0
4089#endif
4090 },
4091 {"multi_byte", 1},
4092 {"multi_byte_ime",
4093#ifdef FEAT_MBYTE_IME
4094 1
4095#else
4096 0
4097#endif
4098 },
4099 {"multi_lang",
4100#ifdef FEAT_MULTI_LANG
4101 1
4102#else
4103 0
4104#endif
4105 },
4106 {"mzscheme",
4107#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4108 1
4109#else
4110 0
4111#endif
4112 },
4113 {"num64", 1},
4114 {"ole",
4115#ifdef FEAT_OLE
4116 1
4117#else
4118 0
4119#endif
4120 },
4121 {"packages",
4122#ifdef FEAT_EVAL
4123 1
4124#else
4125 0
4126#endif
4127 },
4128 {"path_extra",
4129#ifdef FEAT_PATH_EXTRA
4130 1
4131#else
4132 0
4133#endif
4134 },
4135 {"perl",
4136#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4137 1
4138#else
4139 0
4140#endif
4141 },
4142 {"persistent_undo",
4143#ifdef FEAT_PERSISTENT_UNDO
4144 1
4145#else
4146 0
4147#endif
4148 },
4149 {"python_compiled",
4150#if defined(FEAT_PYTHON)
4151 1
4152#else
4153 0
4154#endif
4155 },
4156 {"python_dynamic",
4157#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4158 1
4159#else
4160 0
4161#endif
4162 },
4163 {"python",
4164#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4165 1
4166#else
4167 0
4168#endif
4169 },
4170 {"pythonx",
4171#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4172 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4173 1
4174#else
4175 0
4176#endif
4177 },
4178 {"python3_compiled",
4179#if defined(FEAT_PYTHON3)
4180 1
4181#else
4182 0
4183#endif
4184 },
4185 {"python3_dynamic",
4186#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4187 1
4188#else
4189 0
4190#endif
4191 },
4192 {"python3",
4193#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4194 1
4195#else
4196 0
4197#endif
4198 },
4199 {"popupwin",
4200#ifdef FEAT_PROP_POPUP
4201 1
4202#else
4203 0
4204#endif
4205 },
4206 {"postscript",
4207#ifdef FEAT_POSTSCRIPT
4208 1
4209#else
4210 0
4211#endif
4212 },
4213 {"printer",
4214#ifdef FEAT_PRINTER
4215 1
4216#else
4217 0
4218#endif
4219 },
4220 {"profile",
4221#ifdef FEAT_PROFILE
4222 1
4223#else
4224 0
4225#endif
4226 },
4227 {"reltime",
4228#ifdef FEAT_RELTIME
4229 1
4230#else
4231 0
4232#endif
4233 },
4234 {"quickfix",
4235#ifdef FEAT_QUICKFIX
4236 1
4237#else
4238 0
4239#endif
4240 },
4241 {"rightleft",
4242#ifdef FEAT_RIGHTLEFT
4243 1
4244#else
4245 0
4246#endif
4247 },
4248 {"ruby",
4249#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4250 1
4251#else
4252 0
4253#endif
4254 },
4255 {"scrollbind", 1},
4256 {"showcmd",
4257#ifdef FEAT_CMDL_INFO
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"cmdline_info",
4264#ifdef FEAT_CMDL_INFO
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"signs",
4271#ifdef FEAT_SIGNS
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"smartindent",
4278#ifdef FEAT_SMARTINDENT
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"startuptime",
4285#ifdef STARTUPTIME
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"statusline",
4292#ifdef FEAT_STL_OPT
4293 1
4294#else
4295 0
4296#endif
4297 },
4298 {"netbeans_intg",
4299#ifdef FEAT_NETBEANS_INTG
4300 1
4301#else
4302 0
4303#endif
4304 },
4305 {"sound",
4306#ifdef FEAT_SOUND
4307 1
4308#else
4309 0
4310#endif
4311 },
4312 {"spell",
4313#ifdef FEAT_SPELL
4314 1
4315#else
4316 0
4317#endif
4318 },
4319 {"syntax",
4320#ifdef FEAT_SYN_HL
4321 1
4322#else
4323 0
4324#endif
4325 },
4326 {"system",
4327#if defined(USE_SYSTEM) || !defined(UNIX)
4328 1
4329#else
4330 0
4331#endif
4332 },
4333 {"tag_binary",
4334#ifdef FEAT_TAG_BINS
4335 1
4336#else
4337 0
4338#endif
4339 },
4340 {"tcl",
4341#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4342 1
4343#else
4344 0
4345#endif
4346 },
4347 {"termguicolors",
4348#ifdef FEAT_TERMGUICOLORS
4349 1
4350#else
4351 0
4352#endif
4353 },
4354 {"terminal",
4355#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4356 1
4357#else
4358 0
4359#endif
4360 },
4361 {"terminfo",
4362#ifdef TERMINFO
4363 1
4364#else
4365 0
4366#endif
4367 },
4368 {"termresponse",
4369#ifdef FEAT_TERMRESPONSE
4370 1
4371#else
4372 0
4373#endif
4374 },
4375 {"textobjects",
4376#ifdef FEAT_TEXTOBJ
4377 1
4378#else
4379 0
4380#endif
4381 },
4382 {"textprop",
4383#ifdef FEAT_PROP_POPUP
4384 1
4385#else
4386 0
4387#endif
4388 },
4389 {"tgetent",
4390#ifdef HAVE_TGETENT
4391 1
4392#else
4393 0
4394#endif
4395 },
4396 {"timers",
4397#ifdef FEAT_TIMERS
4398 1
4399#else
4400 0
4401#endif
4402 },
4403 {"title",
4404#ifdef FEAT_TITLE
4405 1
4406#else
4407 0
4408#endif
4409 },
4410 {"toolbar",
4411#ifdef FEAT_TOOLBAR
4412 1
4413#else
4414 0
4415#endif
4416 },
4417 {"unnamedplus",
4418#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4419 1
4420#else
4421 0
4422#endif
4423 },
4424 {"user-commands", 1}, // was accidentally included in 5.4
4425 {"user_commands", 1},
4426 {"vartabs",
4427#ifdef FEAT_VARTABS
4428 1
4429#else
4430 0
4431#endif
4432 },
4433 {"vertsplit", 1},
4434 {"viminfo",
4435#ifdef FEAT_VIMINFO
4436 1
4437#else
4438 0
4439#endif
4440 },
4441 {"vimscript-1", 1},
4442 {"vimscript-2", 1},
4443 {"vimscript-3", 1},
4444 {"vimscript-4", 1},
4445 {"virtualedit", 1},
4446 {"visual", 1},
4447 {"visualextra", 1},
4448 {"vreplace", 1},
4449 {"vtp",
4450#ifdef FEAT_VTP
4451 1
4452#else
4453 0
4454#endif
4455 },
4456 {"wildignore",
4457#ifdef FEAT_WILDIGN
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"wildmenu",
4464#ifdef FEAT_WILDMENU
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"windows", 1},
4471 {"winaltkeys",
4472#ifdef FEAT_WAK
4473 1
4474#else
4475 0
4476#endif
4477 },
4478 {"writebackup",
4479#ifdef FEAT_WRITEBACKUP
4480 1
4481#else
4482 0
4483#endif
4484 },
4485 {"xim",
4486#ifdef FEAT_XIM
4487 1
4488#else
4489 0
4490#endif
4491 },
4492 {"xfontset",
4493#ifdef FEAT_XFONTSET
4494 1
4495#else
4496 0
4497#endif
4498 },
4499 {"xpm",
4500#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4501 1
4502#else
4503 0
4504#endif
4505 },
4506 {"xpm_w32", // for backward compatibility
4507#ifdef FEAT_XPM_W32
4508 1
4509#else
4510 0
4511#endif
4512 },
4513 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004515 1
4516#else
4517 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004519 },
4520 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004522 1
4523#else
4524 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004526 },
4527 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004528#ifdef FEAT_XCLIPBOARD
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 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535#ifdef FEAT_XTERM_SAVE
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 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542#if defined(UNIX) && defined(FEAT_X11)
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 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 };
4550
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004551 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 for (i = 0; has_list[i].name != NULL; ++i)
4553 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004554 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004555 x = TRUE;
4556 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557 break;
4558 }
4559
Bram Moolenaar79296512020-03-22 16:17:14 +01004560 // features also in has_list[] but sometimes enabled at runtime
4561 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004564 {
4565 // intentionally empty
4566 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004567#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568 else if (STRICMP(name, "balloon_multiline") == 0)
4569 n = multiline_balloon_available();
4570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004571#ifdef VIMDLL
4572 else if (STRICMP(name, "filterpipe") == 0)
4573 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004574#endif
4575#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4576 else if (STRICMP(name, "iconv") == 0)
4577 n = iconv_enabled(FALSE);
4578#endif
4579#ifdef DYNAMIC_LUA
4580 else if (STRICMP(name, "lua") == 0)
4581 n = lua_enabled(FALSE);
4582#endif
4583#ifdef DYNAMIC_MZSCHEME
4584 else if (STRICMP(name, "mzscheme") == 0)
4585 n = mzscheme_enabled(FALSE);
4586#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004587#ifdef DYNAMIC_PERL
4588 else if (STRICMP(name, "perl") == 0)
4589 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591#ifdef DYNAMIC_PYTHON
4592 else if (STRICMP(name, "python") == 0)
4593 n = python_enabled(FALSE);
4594#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595#ifdef DYNAMIC_PYTHON3
4596 else if (STRICMP(name, "python3") == 0)
4597 n = python3_enabled(FALSE);
4598#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004599#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4600 else if (STRICMP(name, "pythonx") == 0)
4601 {
4602# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4603 if (p_pyx == 0)
4604 n = python3_enabled(FALSE) || python_enabled(FALSE);
4605 else if (p_pyx == 3)
4606 n = python3_enabled(FALSE);
4607 else if (p_pyx == 2)
4608 n = python_enabled(FALSE);
4609# elif defined(DYNAMIC_PYTHON)
4610 n = python_enabled(FALSE);
4611# elif defined(DYNAMIC_PYTHON3)
4612 n = python3_enabled(FALSE);
4613# endif
4614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004616#ifdef DYNAMIC_RUBY
4617 else if (STRICMP(name, "ruby") == 0)
4618 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004620#ifdef DYNAMIC_TCL
4621 else if (STRICMP(name, "tcl") == 0)
4622 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004623#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004624#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004625 else if (STRICMP(name, "terminal") == 0)
4626 n = terminal_enabled();
4627#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628 }
4629
Bram Moolenaar79296512020-03-22 16:17:14 +01004630 // features not in has_list[]
4631 if (x == FALSE)
4632 {
4633 if (STRNICMP(name, "patch", 5) == 0)
4634 {
4635 x = TRUE;
4636 if (name[5] == '-'
4637 && STRLEN(name) >= 11
4638 && vim_isdigit(name[6])
4639 && vim_isdigit(name[8])
4640 && vim_isdigit(name[10]))
4641 {
4642 int major = atoi((char *)name + 6);
4643 int minor = atoi((char *)name + 8);
4644
4645 // Expect "patch-9.9.01234".
4646 n = (major < VIM_VERSION_MAJOR
4647 || (major == VIM_VERSION_MAJOR
4648 && (minor < VIM_VERSION_MINOR
4649 || (minor == VIM_VERSION_MINOR
4650 && has_patch(atoi((char *)name + 10))))));
4651 }
4652 else
4653 n = has_patch(atoi((char *)name + 5));
4654 }
4655 else if (STRICMP(name, "vim_starting") == 0)
4656 {
4657 x = TRUE;
4658 n = (starting != 0);
4659 }
4660 else if (STRICMP(name, "ttyin") == 0)
4661 {
4662 x = TRUE;
4663 n = mch_input_isatty();
4664 }
4665 else if (STRICMP(name, "ttyout") == 0)
4666 {
4667 x = TRUE;
4668 n = stdout_isatty;
4669 }
4670 else if (STRICMP(name, "multi_byte_encoding") == 0)
4671 {
4672 x = TRUE;
4673 n = has_mbyte;
4674 }
4675 else if (STRICMP(name, "gui_running") == 0)
4676 {
4677 x = TRUE;
4678#ifdef FEAT_GUI
4679 n = (gui.in_use || gui.starting);
4680#endif
4681 }
4682 else if (STRICMP(name, "browse") == 0)
4683 {
4684 x = TRUE;
4685#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4686 n = gui.in_use; // gui_mch_browse() works when GUI is running
4687#endif
4688 }
4689 else if (STRICMP(name, "syntax_items") == 0)
4690 {
4691 x = TRUE;
4692#ifdef FEAT_SYN_HL
4693 n = syntax_present(curwin);
4694#endif
4695 }
4696 else if (STRICMP(name, "vcon") == 0)
4697 {
4698 x = TRUE;
4699#ifdef FEAT_VTP
4700 n = is_term_win32() && has_vtp_working();
4701#endif
4702 }
4703 else if (STRICMP(name, "netbeans_enabled") == 0)
4704 {
4705 x = TRUE;
4706#ifdef FEAT_NETBEANS_INTG
4707 n = netbeans_active();
4708#endif
4709 }
4710 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4711 {
4712 x = TRUE;
4713#ifdef FEAT_MOUSE_GPM
4714 n = gpm_enabled();
4715#endif
4716 }
4717 else if (STRICMP(name, "conpty") == 0)
4718 {
4719 x = TRUE;
4720#if defined(FEAT_TERMINAL) && defined(MSWIN)
4721 n = use_conpty();
4722#endif
4723 }
4724 else if (STRICMP(name, "clipboard_working") == 0)
4725 {
4726 x = TRUE;
4727#ifdef FEAT_CLIPBOARD
4728 n = clip_star.available;
4729#endif
4730 }
4731 }
4732
4733 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4734 // return whether feature could ever be enabled
4735 rettv->vval.v_number = x;
4736 else
4737 // return whether feature is enabled
4738 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004739}
4740
4741/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004742 * "haslocaldir()" function
4743 */
4744 static void
4745f_haslocaldir(typval_T *argvars, typval_T *rettv)
4746{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004747 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004748 win_T *wp = NULL;
4749
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004750 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4751
4752 // Check for window-local and tab-local directories
4753 if (wp != NULL && wp->w_localdir != NULL)
4754 rettv->vval.v_number = 1;
4755 else if (tp != NULL && tp->tp_localdir != NULL)
4756 rettv->vval.v_number = 2;
4757 else
4758 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759}
4760
4761/*
4762 * "hasmapto()" function
4763 */
4764 static void
4765f_hasmapto(typval_T *argvars, typval_T *rettv)
4766{
4767 char_u *name;
4768 char_u *mode;
4769 char_u buf[NUMBUFLEN];
4770 int abbr = FALSE;
4771
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773 if (argvars[1].v_type == VAR_UNKNOWN)
4774 mode = (char_u *)"nvo";
4775 else
4776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004777 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004779 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004780 }
4781
4782 if (map_to_exists(name, mode, abbr))
4783 rettv->vval.v_number = TRUE;
4784 else
4785 rettv->vval.v_number = FALSE;
4786}
4787
4788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 * "highlightID(name)" function
4790 */
4791 static void
4792f_hlID(typval_T *argvars, typval_T *rettv)
4793{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004794 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795}
4796
4797/*
4798 * "highlight_exists()" function
4799 */
4800 static void
4801f_hlexists(typval_T *argvars, typval_T *rettv)
4802{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004803 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004804}
4805
4806/*
4807 * "hostname()" function
4808 */
4809 static void
4810f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4811{
4812 char_u hostname[256];
4813
4814 mch_get_host_name(hostname, 256);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = vim_strsave(hostname);
4817}
4818
4819/*
4820 * iconv() function
4821 */
4822 static void
4823f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4824{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004825 char_u buf1[NUMBUFLEN];
4826 char_u buf2[NUMBUFLEN];
4827 char_u *from, *to, *str;
4828 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829
4830 rettv->v_type = VAR_STRING;
4831 rettv->vval.v_string = NULL;
4832
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004833 str = tv_get_string(&argvars[0]);
4834 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4835 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836 vimconv.vc_type = CONV_NONE;
4837 convert_setup(&vimconv, from, to);
4838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 if (vimconv.vc_type == CONV_NONE)
4841 rettv->vval.v_string = vim_strsave(str);
4842 else
4843 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4844
4845 convert_setup(&vimconv, NULL, NULL);
4846 vim_free(from);
4847 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848}
4849
4850/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 * "index()" function
4852 */
4853 static void
4854f_index(typval_T *argvars, typval_T *rettv)
4855{
4856 list_T *l;
4857 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004858 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859 long idx = 0;
4860 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004861 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862
4863 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004864 if (argvars[0].v_type == VAR_BLOB)
4865 {
4866 typval_T tv;
4867 int start = 0;
4868
4869 if (argvars[2].v_type != VAR_UNKNOWN)
4870 {
4871 start = tv_get_number_chk(&argvars[2], &error);
4872 if (error)
4873 return;
4874 }
4875 b = argvars[0].vval.v_blob;
4876 if (b == NULL)
4877 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004878 if (start < 0)
4879 {
4880 start = blob_len(b) + start;
4881 if (start < 0)
4882 start = 0;
4883 }
4884
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004885 for (idx = start; idx < blob_len(b); ++idx)
4886 {
4887 tv.v_type = VAR_NUMBER;
4888 tv.vval.v_number = blob_get(b, idx);
4889 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4890 {
4891 rettv->vval.v_number = idx;
4892 return;
4893 }
4894 }
4895 return;
4896 }
4897 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004898 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004899 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 return;
4901 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 l = argvars[0].vval.v_list;
4904 if (l != NULL)
4905 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004906 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907 item = l->lv_first;
4908 if (argvars[2].v_type != VAR_UNKNOWN)
4909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004910 // Start at specified item. Use the cached index that list_find()
4911 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004912 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004913 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004915 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004916 if (error)
4917 item = NULL;
4918 }
4919
4920 for ( ; item != NULL; item = item->li_next, ++idx)
4921 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4922 {
4923 rettv->vval.v_number = idx;
4924 break;
4925 }
4926 }
4927}
4928
4929static int inputsecret_flag = 0;
4930
4931/*
4932 * "input()" function
4933 * Also handles inputsecret() when inputsecret is set.
4934 */
4935 static void
4936f_input(typval_T *argvars, typval_T *rettv)
4937{
4938 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4939}
4940
4941/*
4942 * "inputdialog()" function
4943 */
4944 static void
4945f_inputdialog(typval_T *argvars, typval_T *rettv)
4946{
4947#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004949 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4950 {
4951 char_u *message;
4952 char_u buf[NUMBUFLEN];
4953 char_u *defstr = (char_u *)"";
4954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004955 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004957 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4959 else
4960 IObuff[0] = NUL;
4961 if (message != NULL && defstr != NULL
4962 && do_dialog(VIM_QUESTION, NULL, message,
4963 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4964 rettv->vval.v_string = vim_strsave(IObuff);
4965 else
4966 {
4967 if (message != NULL && defstr != NULL
4968 && argvars[1].v_type != VAR_UNKNOWN
4969 && argvars[2].v_type != VAR_UNKNOWN)
4970 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004971 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972 else
4973 rettv->vval.v_string = NULL;
4974 }
4975 rettv->v_type = VAR_STRING;
4976 }
4977 else
4978#endif
4979 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4980}
4981
4982/*
4983 * "inputlist()" function
4984 */
4985 static void
4986f_inputlist(typval_T *argvars, typval_T *rettv)
4987{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004988 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989 listitem_T *li;
4990 int selected;
4991 int mouse_used;
4992
4993#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // While starting up, there is no place to enter text. When running tests
4995 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02004996 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004997 return;
4998#endif
4999 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005001 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002 return;
5003 }
5004
5005 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 msg_row = Rows - 1; // for when 'cmdheight' > 1
5007 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008 msg_scroll = TRUE;
5009 msg_clr_eos();
5010
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005011 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005012 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005013 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005014 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005015 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 msg_putchar('\n');
5017 }
5018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005019 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020 selected = prompt_for_number(&mouse_used);
5021 if (mouse_used)
5022 selected -= lines_left;
5023
5024 rettv->vval.v_number = selected;
5025}
5026
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5028
5029/*
5030 * "inputrestore()" function
5031 */
5032 static void
5033f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5034{
5035 if (ga_userinput.ga_len > 0)
5036 {
5037 --ga_userinput.ga_len;
5038 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5039 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005040 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041 }
5042 else if (p_verbose > 1)
5043 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005044 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005046 }
5047}
5048
5049/*
5050 * "inputsave()" function
5051 */
5052 static void
5053f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5054{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005056 if (ga_grow(&ga_userinput, 1) == OK)
5057 {
5058 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5059 + ga_userinput.ga_len);
5060 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005061 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005062 }
5063 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065}
5066
5067/*
5068 * "inputsecret()" function
5069 */
5070 static void
5071f_inputsecret(typval_T *argvars, typval_T *rettv)
5072{
5073 ++cmdline_star;
5074 ++inputsecret_flag;
5075 f_input(argvars, rettv);
5076 --cmdline_star;
5077 --inputsecret_flag;
5078}
5079
5080/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005081 * "interrupt()" function
5082 */
5083 static void
5084f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5085{
5086 got_int = TRUE;
5087}
5088
5089/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090 * "invert(expr)" function
5091 */
5092 static void
5093f_invert(typval_T *argvars, typval_T *rettv)
5094{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096}
5097
5098/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 * "islocked()" function
5100 */
5101 static void
5102f_islocked(typval_T *argvars, typval_T *rettv)
5103{
5104 lval_T lv;
5105 char_u *end;
5106 dictitem_T *di;
5107
5108 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005109 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005110 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111 if (end != NULL && lv.ll_name != NULL)
5112 {
5113 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005114 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005115 else
5116 {
5117 if (lv.ll_tv == NULL)
5118 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005119 di = find_var(lv.ll_name, NULL, TRUE);
5120 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005122 // Consider a variable locked when:
5123 // 1. the variable itself is locked
5124 // 2. the value of the variable is locked.
5125 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005126 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5127 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 }
5129 }
5130 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005131 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005133 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5137 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5140 }
5141 }
5142
5143 clear_lval(&lv);
5144}
5145
5146#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5147/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005148 * "isinf()" function
5149 */
5150 static void
5151f_isinf(typval_T *argvars, typval_T *rettv)
5152{
5153 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5154 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5155}
5156
5157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005158 * "isnan()" function
5159 */
5160 static void
5161f_isnan(typval_T *argvars, typval_T *rettv)
5162{
5163 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5164 && isnan(argvars[0].vval.v_float);
5165}
5166#endif
5167
5168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 * "last_buffer_nr()" function.
5170 */
5171 static void
5172f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5173{
5174 int n = 0;
5175 buf_T *buf;
5176
Bram Moolenaar29323592016-07-24 22:04:11 +02005177 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005178 if (n < buf->b_fnum)
5179 n = buf->b_fnum;
5180
5181 rettv->vval.v_number = n;
5182}
5183
5184/*
5185 * "len()" function
5186 */
5187 static void
5188f_len(typval_T *argvars, typval_T *rettv)
5189{
5190 switch (argvars[0].v_type)
5191 {
5192 case VAR_STRING:
5193 case VAR_NUMBER:
5194 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005195 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005196 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005197 case VAR_BLOB:
5198 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5199 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005200 case VAR_LIST:
5201 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5202 break;
5203 case VAR_DICT:
5204 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5205 break;
5206 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005207 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005209 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210 case VAR_SPECIAL:
5211 case VAR_FLOAT:
5212 case VAR_FUNC:
5213 case VAR_PARTIAL:
5214 case VAR_JOB:
5215 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005216 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217 break;
5218 }
5219}
5220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005221 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005222libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223{
5224#ifdef FEAT_LIBCALL
5225 char_u *string_in;
5226 char_u **string_result;
5227 int nr_result;
5228#endif
5229
5230 rettv->v_type = type;
5231 if (type != VAR_NUMBER)
5232 rettv->vval.v_string = NULL;
5233
5234 if (check_restricted() || check_secure())
5235 return;
5236
5237#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5240 {
5241 string_in = NULL;
5242 if (argvars[2].v_type == VAR_STRING)
5243 string_in = argvars[2].vval.v_string;
5244 if (type == VAR_NUMBER)
5245 string_result = NULL;
5246 else
5247 string_result = &rettv->vval.v_string;
5248 if (mch_libcall(argvars[0].vval.v_string,
5249 argvars[1].vval.v_string,
5250 string_in,
5251 argvars[2].vval.v_number,
5252 string_result,
5253 &nr_result) == OK
5254 && type == VAR_NUMBER)
5255 rettv->vval.v_number = nr_result;
5256 }
5257#endif
5258}
5259
5260/*
5261 * "libcall()" function
5262 */
5263 static void
5264f_libcall(typval_T *argvars, typval_T *rettv)
5265{
5266 libcall_common(argvars, rettv, VAR_STRING);
5267}
5268
5269/*
5270 * "libcallnr()" function
5271 */
5272 static void
5273f_libcallnr(typval_T *argvars, typval_T *rettv)
5274{
5275 libcall_common(argvars, rettv, VAR_NUMBER);
5276}
5277
5278/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005279 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280 */
5281 static void
5282f_line(typval_T *argvars, typval_T *rettv)
5283{
5284 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005285 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005286 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005287 int id;
5288 tabpage_T *tp;
5289 win_T *wp;
5290 win_T *save_curwin;
5291 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005292
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005293 if (argvars[1].v_type != VAR_UNKNOWN)
5294 {
5295 // use window specified in the second argument
5296 id = (int)tv_get_number(&argvars[1]);
5297 wp = win_id2wp_tp(id, &tp);
5298 if (wp != NULL && tp != NULL)
5299 {
5300 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5301 == OK)
5302 {
5303 check_cursor();
5304 fp = var2fpos(&argvars[0], TRUE, &fnum);
5305 }
5306 restore_win_noblock(save_curwin, save_curtab, TRUE);
5307 }
5308 }
5309 else
5310 // use current window
5311 fp = var2fpos(&argvars[0], TRUE, &fnum);
5312
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 if (fp != NULL)
5314 lnum = fp->lnum;
5315 rettv->vval.v_number = lnum;
5316}
5317
5318/*
5319 * "line2byte(lnum)" function
5320 */
5321 static void
5322f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5323{
5324#ifndef FEAT_BYTEOFF
5325 rettv->vval.v_number = -1;
5326#else
5327 linenr_T lnum;
5328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005329 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5331 rettv->vval.v_number = -1;
5332 else
5333 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5334 if (rettv->vval.v_number >= 0)
5335 ++rettv->vval.v_number;
5336#endif
5337}
5338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339#ifdef FEAT_FLOAT
5340/*
5341 * "log()" function
5342 */
5343 static void
5344f_log(typval_T *argvars, typval_T *rettv)
5345{
5346 float_T f = 0.0;
5347
5348 rettv->v_type = VAR_FLOAT;
5349 if (get_float_arg(argvars, &f) == OK)
5350 rettv->vval.v_float = log(f);
5351 else
5352 rettv->vval.v_float = 0.0;
5353}
5354
5355/*
5356 * "log10()" function
5357 */
5358 static void
5359f_log10(typval_T *argvars, typval_T *rettv)
5360{
5361 float_T f = 0.0;
5362
5363 rettv->v_type = VAR_FLOAT;
5364 if (get_float_arg(argvars, &f) == OK)
5365 rettv->vval.v_float = log10(f);
5366 else
5367 rettv->vval.v_float = 0.0;
5368}
5369#endif
5370
5371#ifdef FEAT_LUA
5372/*
5373 * "luaeval()" function
5374 */
5375 static void
5376f_luaeval(typval_T *argvars, typval_T *rettv)
5377{
5378 char_u *str;
5379 char_u buf[NUMBUFLEN];
5380
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005381 if (check_restricted() || check_secure())
5382 return;
5383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005384 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385 do_luaeval(str, argvars + 1, rettv);
5386}
5387#endif
5388
5389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390 * "maparg()" function
5391 */
5392 static void
5393f_maparg(typval_T *argvars, typval_T *rettv)
5394{
5395 get_maparg(argvars, rettv, TRUE);
5396}
5397
5398/*
5399 * "mapcheck()" function
5400 */
5401 static void
5402f_mapcheck(typval_T *argvars, typval_T *rettv)
5403{
5404 get_maparg(argvars, rettv, FALSE);
5405}
5406
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005407typedef enum
5408{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 MATCH_END, // matchend()
5410 MATCH_MATCH, // match()
5411 MATCH_STR, // matchstr()
5412 MATCH_LIST, // matchlist()
5413 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005414} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005415
5416 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005417find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418{
5419 char_u *str = NULL;
5420 long len = 0;
5421 char_u *expr = NULL;
5422 char_u *pat;
5423 regmatch_T regmatch;
5424 char_u patbuf[NUMBUFLEN];
5425 char_u strbuf[NUMBUFLEN];
5426 char_u *save_cpo;
5427 long start = 0;
5428 long nth = 1;
5429 colnr_T startcol = 0;
5430 int match = 0;
5431 list_T *l = NULL;
5432 listitem_T *li = NULL;
5433 long idx = 0;
5434 char_u *tofree = NULL;
5435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005437 save_cpo = p_cpo;
5438 p_cpo = (char_u *)"";
5439
5440 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005441 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005442 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 // type MATCH_LIST: return empty list when there are no matches.
5444 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005445 if (rettv_list_alloc(rettv) == FAIL)
5446 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005447 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005448 && (list_append_string(rettv->vval.v_list,
5449 (char_u *)"", 0) == FAIL
5450 || list_append_number(rettv->vval.v_list,
5451 (varnumber_T)-1) == FAIL
5452 || list_append_number(rettv->vval.v_list,
5453 (varnumber_T)-1) == FAIL
5454 || list_append_number(rettv->vval.v_list,
5455 (varnumber_T)-1) == FAIL))
5456 {
5457 list_free(rettv->vval.v_list);
5458 rettv->vval.v_list = NULL;
5459 goto theend;
5460 }
5461 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005462 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005463 {
5464 rettv->v_type = VAR_STRING;
5465 rettv->vval.v_string = NULL;
5466 }
5467
5468 if (argvars[0].v_type == VAR_LIST)
5469 {
5470 if ((l = argvars[0].vval.v_list) == NULL)
5471 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005472 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473 li = l->lv_first;
5474 }
5475 else
5476 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005477 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478 len = (long)STRLEN(str);
5479 }
5480
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005481 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 if (pat == NULL)
5483 goto theend;
5484
5485 if (argvars[2].v_type != VAR_UNKNOWN)
5486 {
5487 int error = FALSE;
5488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005489 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 if (error)
5491 goto theend;
5492 if (l != NULL)
5493 {
5494 li = list_find(l, start);
5495 if (li == NULL)
5496 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005497 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 }
5499 else
5500 {
5501 if (start < 0)
5502 start = 0;
5503 if (start > len)
5504 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005505 // When "count" argument is there ignore matches before "start",
5506 // otherwise skip part of the string. Differs when pattern is "^"
5507 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 if (argvars[3].v_type != VAR_UNKNOWN)
5509 startcol = start;
5510 else
5511 {
5512 str += start;
5513 len -= start;
5514 }
5515 }
5516
5517 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005518 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519 if (error)
5520 goto theend;
5521 }
5522
5523 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5524 if (regmatch.regprog != NULL)
5525 {
5526 regmatch.rm_ic = p_ic;
5527
5528 for (;;)
5529 {
5530 if (l != NULL)
5531 {
5532 if (li == NULL)
5533 {
5534 match = FALSE;
5535 break;
5536 }
5537 vim_free(tofree);
5538 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5539 if (str == NULL)
5540 break;
5541 }
5542
5543 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5544
5545 if (match && --nth <= 0)
5546 break;
5547 if (l == NULL && !match)
5548 break;
5549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 if (l != NULL)
5552 {
5553 li = li->li_next;
5554 ++idx;
5555 }
5556 else
5557 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558 startcol = (colnr_T)(regmatch.startp[0]
5559 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 if (startcol > (colnr_T)len
5561 || str + startcol <= regmatch.startp[0])
5562 {
5563 match = FALSE;
5564 break;
5565 }
5566 }
5567 }
5568
5569 if (match)
5570 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005571 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 {
5573 listitem_T *li1 = rettv->vval.v_list->lv_first;
5574 listitem_T *li2 = li1->li_next;
5575 listitem_T *li3 = li2->li_next;
5576 listitem_T *li4 = li3->li_next;
5577
5578 vim_free(li1->li_tv.vval.v_string);
5579 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005580 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581 li3->li_tv.vval.v_number =
5582 (varnumber_T)(regmatch.startp[0] - expr);
5583 li4->li_tv.vval.v_number =
5584 (varnumber_T)(regmatch.endp[0] - expr);
5585 if (l != NULL)
5586 li2->li_tv.vval.v_number = (varnumber_T)idx;
5587 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005588 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 {
5590 int i;
5591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005592 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593 for (i = 0; i < NSUBEXP; ++i)
5594 {
5595 if (regmatch.endp[i] == NULL)
5596 {
5597 if (list_append_string(rettv->vval.v_list,
5598 (char_u *)"", 0) == FAIL)
5599 break;
5600 }
5601 else if (list_append_string(rettv->vval.v_list,
5602 regmatch.startp[i],
5603 (int)(regmatch.endp[i] - regmatch.startp[i]))
5604 == FAIL)
5605 break;
5606 }
5607 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005608 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005610 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 if (l != NULL)
5612 copy_tv(&li->li_tv, rettv);
5613 else
5614 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005615 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 }
5617 else if (l != NULL)
5618 rettv->vval.v_number = idx;
5619 else
5620 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005621 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 rettv->vval.v_number =
5623 (varnumber_T)(regmatch.startp[0] - str);
5624 else
5625 rettv->vval.v_number =
5626 (varnumber_T)(regmatch.endp[0] - str);
5627 rettv->vval.v_number += (varnumber_T)(str - expr);
5628 }
5629 }
5630 vim_regfree(regmatch.regprog);
5631 }
5632
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005633theend:
5634 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005635 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 listitem_remove(rettv->vval.v_list,
5637 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638 vim_free(tofree);
5639 p_cpo = save_cpo;
5640}
5641
5642/*
5643 * "match()" function
5644 */
5645 static void
5646f_match(typval_T *argvars, typval_T *rettv)
5647{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005648 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649}
5650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651/*
5652 * "matchend()" function
5653 */
5654 static void
5655f_matchend(typval_T *argvars, typval_T *rettv)
5656{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005657 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658}
5659
5660/*
5661 * "matchlist()" function
5662 */
5663 static void
5664f_matchlist(typval_T *argvars, typval_T *rettv)
5665{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005666 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005667}
5668
5669/*
5670 * "matchstr()" function
5671 */
5672 static void
5673f_matchstr(typval_T *argvars, typval_T *rettv)
5674{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005675 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676}
5677
5678/*
5679 * "matchstrpos()" function
5680 */
5681 static void
5682f_matchstrpos(typval_T *argvars, typval_T *rettv)
5683{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005684 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685}
5686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687 static void
5688max_min(typval_T *argvars, typval_T *rettv, int domax)
5689{
5690 varnumber_T n = 0;
5691 varnumber_T i;
5692 int error = FALSE;
5693
5694 if (argvars[0].v_type == VAR_LIST)
5695 {
5696 list_T *l;
5697 listitem_T *li;
5698
5699 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005700 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005702 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005704 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5705 n = l->lv_u.nonmat.lv_start;
5706 else
5707 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5708 * l->lv_u.nonmat.lv_stride;
5709 }
5710 else
5711 {
5712 li = l->lv_first;
5713 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005715 n = tv_get_number_chk(&li->li_tv, &error);
5716 for (;;)
5717 {
5718 li = li->li_next;
5719 if (li == NULL)
5720 break;
5721 i = tv_get_number_chk(&li->li_tv, &error);
5722 if (domax ? i > n : i < n)
5723 n = i;
5724 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 }
5726 }
5727 }
5728 }
5729 else if (argvars[0].v_type == VAR_DICT)
5730 {
5731 dict_T *d;
5732 int first = TRUE;
5733 hashitem_T *hi;
5734 int todo;
5735
5736 d = argvars[0].vval.v_dict;
5737 if (d != NULL)
5738 {
5739 todo = (int)d->dv_hashtab.ht_used;
5740 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5741 {
5742 if (!HASHITEM_EMPTY(hi))
5743 {
5744 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005745 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005746 if (first)
5747 {
5748 n = i;
5749 first = FALSE;
5750 }
5751 else if (domax ? i > n : i < n)
5752 n = i;
5753 }
5754 }
5755 }
5756 }
5757 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005758 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005759 rettv->vval.v_number = error ? 0 : n;
5760}
5761
5762/*
5763 * "max()" function
5764 */
5765 static void
5766f_max(typval_T *argvars, typval_T *rettv)
5767{
5768 max_min(argvars, rettv, TRUE);
5769}
5770
5771/*
5772 * "min()" function
5773 */
5774 static void
5775f_min(typval_T *argvars, typval_T *rettv)
5776{
5777 max_min(argvars, rettv, FALSE);
5778}
5779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780#if defined(FEAT_MZSCHEME) || defined(PROTO)
5781/*
5782 * "mzeval()" function
5783 */
5784 static void
5785f_mzeval(typval_T *argvars, typval_T *rettv)
5786{
5787 char_u *str;
5788 char_u buf[NUMBUFLEN];
5789
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005790 if (check_restricted() || check_secure())
5791 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005792 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 do_mzeval(str, rettv);
5794}
5795
5796 void
5797mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5798{
5799 typval_T argvars[3];
5800
5801 argvars[0].v_type = VAR_STRING;
5802 argvars[0].vval.v_string = name;
5803 copy_tv(args, &argvars[1]);
5804 argvars[2].v_type = VAR_UNKNOWN;
5805 f_call(argvars, rettv);
5806 clear_tv(&argvars[1]);
5807}
5808#endif
5809
5810/*
5811 * "nextnonblank()" function
5812 */
5813 static void
5814f_nextnonblank(typval_T *argvars, typval_T *rettv)
5815{
5816 linenr_T lnum;
5817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005818 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005819 {
5820 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5821 {
5822 lnum = 0;
5823 break;
5824 }
5825 if (*skipwhite(ml_get(lnum)) != NUL)
5826 break;
5827 }
5828 rettv->vval.v_number = lnum;
5829}
5830
5831/*
5832 * "nr2char()" function
5833 */
5834 static void
5835f_nr2char(typval_T *argvars, typval_T *rettv)
5836{
5837 char_u buf[NUMBUFLEN];
5838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005839 if (has_mbyte)
5840 {
5841 int utf8 = 0;
5842
5843 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005846 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005848 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 }
5850 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005852 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 buf[1] = NUL;
5854 }
5855 rettv->v_type = VAR_STRING;
5856 rettv->vval.v_string = vim_strsave(buf);
5857}
5858
5859/*
5860 * "or(expr, expr)" function
5861 */
5862 static void
5863f_or(typval_T *argvars, typval_T *rettv)
5864{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5866 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867}
5868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869#ifdef FEAT_PERL
5870/*
5871 * "perleval()" function
5872 */
5873 static void
5874f_perleval(typval_T *argvars, typval_T *rettv)
5875{
5876 char_u *str;
5877 char_u buf[NUMBUFLEN];
5878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005879 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 do_perleval(str, rettv);
5881}
5882#endif
5883
5884#ifdef FEAT_FLOAT
5885/*
5886 * "pow()" function
5887 */
5888 static void
5889f_pow(typval_T *argvars, typval_T *rettv)
5890{
5891 float_T fx = 0.0, fy = 0.0;
5892
5893 rettv->v_type = VAR_FLOAT;
5894 if (get_float_arg(argvars, &fx) == OK
5895 && get_float_arg(&argvars[1], &fy) == OK)
5896 rettv->vval.v_float = pow(fx, fy);
5897 else
5898 rettv->vval.v_float = 0.0;
5899}
5900#endif
5901
5902/*
5903 * "prevnonblank()" function
5904 */
5905 static void
5906f_prevnonblank(typval_T *argvars, typval_T *rettv)
5907{
5908 linenr_T lnum;
5909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005910 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5912 lnum = 0;
5913 else
5914 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5915 --lnum;
5916 rettv->vval.v_number = lnum;
5917}
5918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919// This dummy va_list is here because:
5920// - passing a NULL pointer doesn't work when va_list isn't a pointer
5921// - locally in the function results in a "used before set" warning
5922// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923static va_list ap;
5924
5925/*
5926 * "printf()" function
5927 */
5928 static void
5929f_printf(typval_T *argvars, typval_T *rettv)
5930{
5931 char_u buf[NUMBUFLEN];
5932 int len;
5933 char_u *s;
5934 int saved_did_emsg = did_emsg;
5935 char *fmt;
5936
5937 rettv->v_type = VAR_STRING;
5938 rettv->vval.v_string = NULL;
5939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005940 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005942 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005943 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005944 if (!did_emsg)
5945 {
5946 s = alloc(len + 1);
5947 if (s != NULL)
5948 {
5949 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005950 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5951 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 }
5953 }
5954 did_emsg |= saved_did_emsg;
5955}
5956
5957/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005958 * "pum_getpos()" function
5959 */
5960 static void
5961f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5962{
5963 if (rettv_dict_alloc(rettv) != OK)
5964 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005965 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005966}
5967
5968/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 * "pumvisible()" function
5970 */
5971 static void
5972f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5973{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 if (pum_visible())
5975 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976}
5977
5978#ifdef FEAT_PYTHON3
5979/*
5980 * "py3eval()" function
5981 */
5982 static void
5983f_py3eval(typval_T *argvars, typval_T *rettv)
5984{
5985 char_u *str;
5986 char_u buf[NUMBUFLEN];
5987
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005988 if (check_restricted() || check_secure())
5989 return;
5990
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005991 if (p_pyx == 0)
5992 p_pyx = 3;
5993
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005994 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 do_py3eval(str, rettv);
5996}
5997#endif
5998
5999#ifdef FEAT_PYTHON
6000/*
6001 * "pyeval()" function
6002 */
6003 static void
6004f_pyeval(typval_T *argvars, typval_T *rettv)
6005{
6006 char_u *str;
6007 char_u buf[NUMBUFLEN];
6008
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006009 if (check_restricted() || check_secure())
6010 return;
6011
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006012 if (p_pyx == 0)
6013 p_pyx = 2;
6014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 do_pyeval(str, rettv);
6017}
6018#endif
6019
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006020#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6021/*
6022 * "pyxeval()" function
6023 */
6024 static void
6025f_pyxeval(typval_T *argvars, typval_T *rettv)
6026{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006027 if (check_restricted() || check_secure())
6028 return;
6029
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006030# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6031 init_pyxversion();
6032 if (p_pyx == 2)
6033 f_pyeval(argvars, rettv);
6034 else
6035 f_py3eval(argvars, rettv);
6036# elif defined(FEAT_PYTHON)
6037 f_pyeval(argvars, rettv);
6038# elif defined(FEAT_PYTHON3)
6039 f_py3eval(argvars, rettv);
6040# endif
6041}
6042#endif
6043
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006044static UINT32_T srand_seed_for_testing = 0;
6045static int srand_seed_for_testing_is_used = FALSE;
6046
6047 static void
6048f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6049{
6050 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006051 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006052 else
6053 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006054 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6055 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006056 }
6057}
6058
6059 static void
6060init_srand(UINT32_T *x)
6061{
6062#ifndef MSWIN
6063 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6064#endif
6065
6066 if (srand_seed_for_testing_is_used)
6067 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006068 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006069 return;
6070 }
6071#ifndef MSWIN
6072 if (dev_urandom_state != FAIL)
6073 {
6074 int fd = open("/dev/urandom", O_RDONLY);
6075 struct {
6076 union {
6077 UINT32_T number;
6078 char bytes[sizeof(UINT32_T)];
6079 } contents;
6080 } buf;
6081
6082 // Attempt reading /dev/urandom.
6083 if (fd == -1)
6084 dev_urandom_state = FAIL;
6085 else
6086 {
6087 buf.contents.number = 0;
6088 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6089 != sizeof(UINT32_T))
6090 dev_urandom_state = FAIL;
6091 else
6092 {
6093 dev_urandom_state = OK;
6094 *x = buf.contents.number;
6095 }
6096 close(fd);
6097 }
6098 }
6099 if (dev_urandom_state != OK)
6100 // Reading /dev/urandom doesn't work, fall back to time().
6101#endif
6102 *x = vim_time();
6103}
6104
6105#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6106#define SPLITMIX32(x, z) ( \
6107 z = (x += 0x9e3779b9), \
6108 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6109 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6110 z ^ (z >> 16) \
6111 )
6112#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6113 result = ROTL(y * 5, 7) * 9; \
6114 t = y << 9; \
6115 z ^= x; \
6116 w ^= y; \
6117 y ^= z, x ^= w; \
6118 z ^= t; \
6119 w = ROTL(w, 11);
6120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006121/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006122 * "rand()" function
6123 */
6124 static void
6125f_rand(typval_T *argvars, typval_T *rettv)
6126{
6127 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006128 static UINT32_T gx, gy, gz, gw;
6129 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006130 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006131 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006132
6133 if (argvars[0].v_type == VAR_UNKNOWN)
6134 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006135 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006136 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006137 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006138 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006139 init_srand(&x);
6140
6141 gx = SPLITMIX32(x, z);
6142 gy = SPLITMIX32(x, z);
6143 gz = SPLITMIX32(x, z);
6144 gw = SPLITMIX32(x, z);
6145 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006146 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006147
6148 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006149 }
6150 else if (argvars[0].v_type == VAR_LIST)
6151 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006152 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006153 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006154 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006155
6156 lx = list_find(l, 0L);
6157 ly = list_find(l, 1L);
6158 lz = list_find(l, 2L);
6159 lw = list_find(l, 3L);
6160 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6161 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6162 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6163 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6164 x = (UINT32_T)lx->li_tv.vval.v_number;
6165 y = (UINT32_T)ly->li_tv.vval.v_number;
6166 z = (UINT32_T)lz->li_tv.vval.v_number;
6167 w = (UINT32_T)lw->li_tv.vval.v_number;
6168
6169 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6170
6171 lx->li_tv.vval.v_number = (varnumber_T)x;
6172 ly->li_tv.vval.v_number = (varnumber_T)y;
6173 lz->li_tv.vval.v_number = (varnumber_T)z;
6174 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006175 }
6176 else
6177 goto theend;
6178
6179 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006180 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006181 return;
6182
6183theend:
6184 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006185 rettv->v_type = VAR_NUMBER;
6186 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006187}
6188
6189/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006190 * "srand()" function
6191 */
6192 static void
6193f_srand(typval_T *argvars, typval_T *rettv)
6194{
6195 UINT32_T x = 0, z;
6196
6197 if (rettv_list_alloc(rettv) == FAIL)
6198 return;
6199 if (argvars[0].v_type == VAR_UNKNOWN)
6200 {
6201 init_srand(&x);
6202 }
6203 else
6204 {
6205 int error = FALSE;
6206
6207 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6208 if (error)
6209 return;
6210 }
6211
6212 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6213 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6214 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6215 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6216}
6217
6218#undef ROTL
6219#undef SPLITMIX32
6220#undef SHUFFLE_XOSHIRO128STARSTAR
6221
6222/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 * "range()" function
6224 */
6225 static void
6226f_range(typval_T *argvars, typval_T *rettv)
6227{
6228 varnumber_T start;
6229 varnumber_T end;
6230 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 int error = FALSE;
6232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006233 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 if (argvars[1].v_type == VAR_UNKNOWN)
6235 {
6236 end = start - 1;
6237 start = 0;
6238 }
6239 else
6240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006241 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006243 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 }
6245
6246 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006247 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006249 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006251 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 list_T *list = rettv->vval.v_list;
6255
6256 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006257 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006258 // be called.
6259 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006260 list->lv_u.nonmat.lv_start = start;
6261 list->lv_u.nonmat.lv_end = end;
6262 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006263 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006264 }
6265}
6266
6267/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006268 * Materialize "list".
6269 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270 */
6271 void
6272range_list_materialize(list_T *list)
6273{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006274 varnumber_T start = list->lv_u.nonmat.lv_start;
6275 varnumber_T end = list->lv_u.nonmat.lv_end;
6276 int stride = list->lv_u.nonmat.lv_stride;
6277 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006279 list->lv_first = NULL;
6280 list->lv_u.mat.lv_last = NULL;
6281 list->lv_len = 0;
6282 list->lv_u.mat.lv_idx_item = NULL;
6283 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6284 if (list_append_number(list, (varnumber_T)i) == FAIL)
6285 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286}
6287
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006288/*
6289 * "getreginfo()" function
6290 */
6291 static void
6292f_getreginfo(typval_T *argvars, typval_T *rettv)
6293{
6294 char_u *strregname;
6295 int regname;
6296 char_u buf[NUMBUFLEN + 2];
6297 long reglen = 0;
6298 dict_T *dict;
6299 list_T *list;
6300
6301 if (argvars[0].v_type != VAR_UNKNOWN)
6302 {
6303 strregname = tv_get_string_chk(&argvars[0]);
6304 if (strregname == NULL)
6305 return;
6306 }
6307 else
6308 strregname = get_vim_var_str(VV_REG);
6309
6310 regname = (strregname == NULL ? '"' : *strregname);
6311 if (regname == 0 || regname == '@')
6312 regname = '"';
6313
6314 if (rettv_dict_alloc(rettv) == FAIL)
6315 return;
6316 dict = rettv->vval.v_dict;
6317
6318 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6319 if (list == NULL)
6320 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006321 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006322
6323 buf[0] = NUL;
6324 buf[1] = NUL;
6325 switch (get_reg_type(regname, &reglen))
6326 {
6327 case MLINE: buf[0] = 'V'; break;
6328 case MCHAR: buf[0] = 'v'; break;
6329 case MBLOCK:
6330 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6331 reglen + 1);
6332 break;
6333 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006334 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006335
6336 buf[0] = get_register_name(get_unname_register());
6337 buf[1] = NUL;
6338 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006339 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006340 else
6341 {
6342 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6343
6344 if (item != NULL)
6345 {
6346 item->di_tv.v_type = VAR_SPECIAL;
6347 item->di_tv.vval.v_number = regname == buf[0]
6348 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006349 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006350 }
6351 }
6352}
6353
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006354 static void
6355return_register(int regname, typval_T *rettv)
6356{
6357 char_u buf[2] = {0, 0};
6358
6359 buf[0] = (char_u)regname;
6360 rettv->v_type = VAR_STRING;
6361 rettv->vval.v_string = vim_strsave(buf);
6362}
6363
6364/*
6365 * "reg_executing()" function
6366 */
6367 static void
6368f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6369{
6370 return_register(reg_executing, rettv);
6371}
6372
6373/*
6374 * "reg_recording()" function
6375 */
6376 static void
6377f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6378{
6379 return_register(reg_recording, rettv);
6380}
6381
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006382/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383 * "rename({from}, {to})" function
6384 */
6385 static void
6386f_rename(typval_T *argvars, typval_T *rettv)
6387{
6388 char_u buf[NUMBUFLEN];
6389
6390 if (check_restricted() || check_secure())
6391 rettv->vval.v_number = -1;
6392 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006393 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6394 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395}
6396
6397/*
6398 * "repeat()" function
6399 */
6400 static void
6401f_repeat(typval_T *argvars, typval_T *rettv)
6402{
6403 char_u *p;
6404 int n;
6405 int slen;
6406 int len;
6407 char_u *r;
6408 int i;
6409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006410 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411 if (argvars[0].v_type == VAR_LIST)
6412 {
6413 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6414 while (n-- > 0)
6415 if (list_extend(rettv->vval.v_list,
6416 argvars[0].vval.v_list, NULL) == FAIL)
6417 break;
6418 }
6419 else
6420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006421 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 rettv->v_type = VAR_STRING;
6423 rettv->vval.v_string = NULL;
6424
6425 slen = (int)STRLEN(p);
6426 len = slen * n;
6427 if (len <= 0)
6428 return;
6429
6430 r = alloc(len + 1);
6431 if (r != NULL)
6432 {
6433 for (i = 0; i < n; i++)
6434 mch_memmove(r + i * slen, p, (size_t)slen);
6435 r[len] = NUL;
6436 }
6437
6438 rettv->vval.v_string = r;
6439 }
6440}
6441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006442#define SP_NOMOVE 0x01 // don't move cursor
6443#define SP_REPEAT 0x02 // repeat to find outer pair
6444#define SP_RETCOUNT 0x04 // return matchcount
6445#define SP_SETPCMARK 0x08 // set previous context mark
6446#define SP_START 0x10 // accept match at start position
6447#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6448#define SP_END 0x40 // leave cursor at end of match
6449#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451/*
6452 * Get flags for a search function.
6453 * Possibly sets "p_ws".
6454 * Returns BACKWARD, FORWARD or zero (for an error).
6455 */
6456 static int
6457get_search_arg(typval_T *varp, int *flagsp)
6458{
6459 int dir = FORWARD;
6460 char_u *flags;
6461 char_u nbuf[NUMBUFLEN];
6462 int mask;
6463
6464 if (varp->v_type != VAR_UNKNOWN)
6465 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006466 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 while (*flags != NUL)
6470 {
6471 switch (*flags)
6472 {
6473 case 'b': dir = BACKWARD; break;
6474 case 'w': p_ws = TRUE; break;
6475 case 'W': p_ws = FALSE; break;
6476 default: mask = 0;
6477 if (flagsp != NULL)
6478 switch (*flags)
6479 {
6480 case 'c': mask = SP_START; break;
6481 case 'e': mask = SP_END; break;
6482 case 'm': mask = SP_RETCOUNT; break;
6483 case 'n': mask = SP_NOMOVE; break;
6484 case 'p': mask = SP_SUBPAT; break;
6485 case 'r': mask = SP_REPEAT; break;
6486 case 's': mask = SP_SETPCMARK; break;
6487 case 'z': mask = SP_COLUMN; break;
6488 }
6489 if (mask == 0)
6490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006491 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 dir = 0;
6493 }
6494 else
6495 *flagsp |= mask;
6496 }
6497 if (dir == 0)
6498 break;
6499 ++flags;
6500 }
6501 }
6502 return dir;
6503}
6504
6505/*
6506 * Shared by search() and searchpos() functions.
6507 */
6508 static int
6509search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6510{
6511 int flags;
6512 char_u *pat;
6513 pos_T pos;
6514 pos_T save_cursor;
6515 int save_p_ws = p_ws;
6516 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006517 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006520 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521 long time_limit = 0;
6522#endif
6523 int options = SEARCH_KEEP;
6524 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006525 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006526 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006527 pos_T firstpos;
6528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006529 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006530 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531 if (dir == 0)
6532 goto theend;
6533 flags = *flagsp;
6534 if (flags & SP_START)
6535 options |= SEARCH_START;
6536 if (flags & SP_END)
6537 options |= SEARCH_END;
6538 if (flags & SP_COLUMN)
6539 options |= SEARCH_COL;
6540
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006541 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6543 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006544 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545 if (lnum_stop < 0)
6546 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547 if (argvars[3].v_type != VAR_UNKNOWN)
6548 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006549#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006550 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 if (time_limit < 0)
6552 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006554 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006555 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 }
6557
6558#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006559 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560 profile_setlimit(time_limit, &tm);
6561#endif
6562
6563 /*
6564 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6565 * Check to make sure only those flags are set.
6566 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6567 * flags cannot be set. Check for that condition also.
6568 */
6569 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6570 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006572 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 goto theend;
6574 }
6575
6576 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006577 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006578 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006579 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6580#ifdef FEAT_RELTIME
6581 sia.sa_tm = &tm;
6582#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006583
6584 // Repeat until {skip} returns FALSE.
6585 for (;;)
6586 {
6587 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006588 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006589 // finding the first match again means there is no match where {skip}
6590 // evaluates to zero.
6591 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6592 subpatnum = FAIL;
6593
Bram Moolenaara9c01042020-06-07 14:50:50 +02006594 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006595 // didn't find it or no skip argument
6596 break;
6597 firstpos = pos;
6598
Bram Moolenaara9c01042020-06-07 14:50:50 +02006599 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006600 {
6601 int do_skip;
6602 int err;
6603 pos_T save_pos = curwin->w_cursor;
6604
6605 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006606 err = FALSE;
6607 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006608 curwin->w_cursor = save_pos;
6609 if (err)
6610 {
6611 // Evaluating {skip} caused an error, break here.
6612 subpatnum = FAIL;
6613 break;
6614 }
6615 if (!do_skip)
6616 break;
6617 }
6618 }
6619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620 if (subpatnum != FAIL)
6621 {
6622 if (flags & SP_SUBPAT)
6623 retval = subpatnum;
6624 else
6625 retval = pos.lnum;
6626 if (flags & SP_SETPCMARK)
6627 setpcmark();
6628 curwin->w_cursor = pos;
6629 if (match_pos != NULL)
6630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006631 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632 match_pos->lnum = pos.lnum;
6633 match_pos->col = pos.col + 1;
6634 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006635 // "/$" will put the cursor after the end of the line, may need to
6636 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 check_cursor();
6638 }
6639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006640 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 if (flags & SP_NOMOVE)
6642 curwin->w_cursor = save_cursor;
6643 else
6644 curwin->w_set_curswant = TRUE;
6645theend:
6646 p_ws = save_p_ws;
6647
6648 return retval;
6649}
6650
6651#ifdef FEAT_FLOAT
6652
6653/*
6654 * round() is not in C90, use ceil() or floor() instead.
6655 */
6656 float_T
6657vim_round(float_T f)
6658{
6659 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6660}
6661
6662/*
6663 * "round({float})" function
6664 */
6665 static void
6666f_round(typval_T *argvars, typval_T *rettv)
6667{
6668 float_T f = 0.0;
6669
6670 rettv->v_type = VAR_FLOAT;
6671 if (get_float_arg(argvars, &f) == OK)
6672 rettv->vval.v_float = vim_round(f);
6673 else
6674 rettv->vval.v_float = 0.0;
6675}
6676#endif
6677
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006678#ifdef FEAT_RUBY
6679/*
6680 * "rubyeval()" function
6681 */
6682 static void
6683f_rubyeval(typval_T *argvars, typval_T *rettv)
6684{
6685 char_u *str;
6686 char_u buf[NUMBUFLEN];
6687
6688 str = tv_get_string_buf(&argvars[0], buf);
6689 do_rubyeval(str, rettv);
6690}
6691#endif
6692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693/*
6694 * "screenattr()" function
6695 */
6696 static void
6697f_screenattr(typval_T *argvars, typval_T *rettv)
6698{
6699 int row;
6700 int col;
6701 int c;
6702
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006703 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6704 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705 if (row < 0 || row >= screen_Rows
6706 || col < 0 || col >= screen_Columns)
6707 c = -1;
6708 else
6709 c = ScreenAttrs[LineOffset[row] + col];
6710 rettv->vval.v_number = c;
6711}
6712
6713/*
6714 * "screenchar()" function
6715 */
6716 static void
6717f_screenchar(typval_T *argvars, typval_T *rettv)
6718{
6719 int row;
6720 int col;
6721 int off;
6722 int c;
6723
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006724 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6725 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006726 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 c = -1;
6728 else
6729 {
6730 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 if (enc_utf8 && ScreenLinesUC[off] != 0)
6732 c = ScreenLinesUC[off];
6733 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734 c = ScreenLines[off];
6735 }
6736 rettv->vval.v_number = c;
6737}
6738
6739/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006740 * "screenchars()" function
6741 */
6742 static void
6743f_screenchars(typval_T *argvars, typval_T *rettv)
6744{
6745 int row;
6746 int col;
6747 int off;
6748 int c;
6749 int i;
6750
6751 if (rettv_list_alloc(rettv) == FAIL)
6752 return;
6753 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6754 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6755 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6756 return;
6757
6758 off = LineOffset[row] + col;
6759 if (enc_utf8 && ScreenLinesUC[off] != 0)
6760 c = ScreenLinesUC[off];
6761 else
6762 c = ScreenLines[off];
6763 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6764
6765 if (enc_utf8)
6766
6767 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6768 list_append_number(rettv->vval.v_list,
6769 (varnumber_T)ScreenLinesC[i][off]);
6770}
6771
6772/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773 * "screencol()" function
6774 *
6775 * First column is 1 to be consistent with virtcol().
6776 */
6777 static void
6778f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6779{
6780 rettv->vval.v_number = screen_screencol() + 1;
6781}
6782
6783/*
6784 * "screenrow()" function
6785 */
6786 static void
6787f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789 rettv->vval.v_number = screen_screenrow() + 1;
6790}
6791
6792/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006793 * "screenstring()" function
6794 */
6795 static void
6796f_screenstring(typval_T *argvars, typval_T *rettv)
6797{
6798 int row;
6799 int col;
6800 int off;
6801 int c;
6802 int i;
6803 char_u buf[MB_MAXBYTES + 1];
6804 int buflen = 0;
6805
6806 rettv->vval.v_string = NULL;
6807 rettv->v_type = VAR_STRING;
6808
6809 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6810 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6811 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6812 return;
6813
6814 off = LineOffset[row] + col;
6815 if (enc_utf8 && ScreenLinesUC[off] != 0)
6816 c = ScreenLinesUC[off];
6817 else
6818 c = ScreenLines[off];
6819 buflen += mb_char2bytes(c, buf);
6820
6821 if (enc_utf8)
6822 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6823 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6824
6825 buf[buflen] = NUL;
6826 rettv->vval.v_string = vim_strsave(buf);
6827}
6828
6829/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830 * "search()" function
6831 */
6832 static void
6833f_search(typval_T *argvars, typval_T *rettv)
6834{
6835 int flags = 0;
6836
6837 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6838}
6839
6840/*
6841 * "searchdecl()" function
6842 */
6843 static void
6844f_searchdecl(typval_T *argvars, typval_T *rettv)
6845{
6846 int locally = 1;
6847 int thisblock = 0;
6848 int error = FALSE;
6849 char_u *name;
6850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006851 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006853 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854 if (argvars[1].v_type != VAR_UNKNOWN)
6855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006856 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006858 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 }
6860 if (!error && name != NULL)
6861 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6862 locally, thisblock, SEARCH_KEEP) == FAIL;
6863}
6864
6865/*
6866 * Used by searchpair() and searchpairpos()
6867 */
6868 static int
6869searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6870{
6871 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006872 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 int save_p_ws = p_ws;
6874 int dir;
6875 int flags = 0;
6876 char_u nbuf1[NUMBUFLEN];
6877 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006878 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 long lnum_stop = 0;
6880 long time_limit = 0;
6881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006882 // Get the three pattern arguments: start, middle, end. Will result in an
6883 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006884 spat = tv_get_string_chk(&argvars[0]);
6885 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6886 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006888 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006890 // Handle the optional fourth argument: flags
6891 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892 if (dir == 0)
6893 goto theend;
6894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006895 // Don't accept SP_END or SP_SUBPAT.
6896 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 if ((flags & (SP_END | SP_SUBPAT)) != 0
6898 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006900 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 goto theend;
6902 }
6903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006904 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 if (flags & SP_REPEAT)
6906 p_ws = FALSE;
6907
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006908 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 if (argvars[3].v_type == VAR_UNKNOWN
6910 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006911 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 else
6913 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006914 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006915 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 if (argvars[5].v_type != VAR_UNKNOWN)
6918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006919 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006922 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925#ifdef FEAT_RELTIME
6926 if (argvars[6].v_type != VAR_UNKNOWN)
6927 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006928 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006931 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006932 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006933 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 }
6935#endif
6936 }
6937 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938
6939 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6940 match_pos, lnum_stop, time_limit);
6941
6942theend:
6943 p_ws = save_p_ws;
6944
6945 return retval;
6946}
6947
6948/*
6949 * "searchpair()" function
6950 */
6951 static void
6952f_searchpair(typval_T *argvars, typval_T *rettv)
6953{
6954 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6955}
6956
6957/*
6958 * "searchpairpos()" function
6959 */
6960 static void
6961f_searchpairpos(typval_T *argvars, typval_T *rettv)
6962{
6963 pos_T match_pos;
6964 int lnum = 0;
6965 int col = 0;
6966
6967 if (rettv_list_alloc(rettv) == FAIL)
6968 return;
6969
6970 if (searchpair_cmn(argvars, &match_pos) > 0)
6971 {
6972 lnum = match_pos.lnum;
6973 col = match_pos.col;
6974 }
6975
6976 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6977 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6978}
6979
6980/*
6981 * Search for a start/middle/end thing.
6982 * Used by searchpair(), see its documentation for the details.
6983 * Returns 0 or -1 for no match,
6984 */
6985 long
6986do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006987 char_u *spat, // start pattern
6988 char_u *mpat, // middle pattern
6989 char_u *epat, // end pattern
6990 int dir, // BACKWARD or FORWARD
6991 typval_T *skip, // skip expression
6992 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006994 linenr_T lnum_stop, // stop at this line if not zero
6995 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996{
6997 char_u *save_cpo;
6998 char_u *pat, *pat2 = NULL, *pat3 = NULL;
6999 long retval = 0;
7000 pos_T pos;
7001 pos_T firstpos;
7002 pos_T foundpos;
7003 pos_T save_cursor;
7004 pos_T save_pos;
7005 int n;
7006 int r;
7007 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007008 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009 int err;
7010 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007011#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007013#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007015 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016 save_cpo = p_cpo;
7017 p_cpo = empty_option;
7018
7019#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007020 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021 profile_setlimit(time_limit, &tm);
7022#endif
7023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007024 // Make two search patterns: start/end (pat2, for in nested pairs) and
7025 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007026 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7027 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (pat2 == NULL || pat3 == NULL)
7029 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007030 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 if (*mpat == NUL)
7032 STRCPY(pat3, pat2);
7033 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007034 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 spat, epat, mpat);
7036 if (flags & SP_START)
7037 options |= SEARCH_START;
7038
Bram Moolenaar48570482017-10-30 21:48:41 +01007039 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007040 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 save_cursor = curwin->w_cursor;
7043 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007044 CLEAR_POS(&firstpos);
7045 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 pat = pat3;
7047 for (;;)
7048 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007049 searchit_arg_T sia;
7050
Bram Moolenaara80faa82020-04-12 19:37:17 +02007051 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007052 sia.sa_stop_lnum = lnum_stop;
7053#ifdef FEAT_RELTIME
7054 sia.sa_tm = &tm;
7055#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007056 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007057 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007058 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007059 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 break;
7061
7062 if (firstpos.lnum == 0)
7063 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007064 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007066 // Found the same position again. Can happen with a pattern that
7067 // has "\zs" at the end and searching backwards. Advance one
7068 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069 if (dir == BACKWARD)
7070 decl(&pos);
7071 else
7072 incl(&pos);
7073 }
7074 foundpos = pos;
7075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007076 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 options &= ~SEARCH_START;
7078
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007079 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007080 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007081 {
7082 save_pos = curwin->w_cursor;
7083 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007084 err = FALSE;
7085 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086 curwin->w_cursor = save_pos;
7087 if (err)
7088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007089 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 curwin->w_cursor = save_cursor;
7091 retval = -1;
7092 break;
7093 }
7094 if (r)
7095 continue;
7096 }
7097
7098 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007100 // Found end when searching backwards or start when searching
7101 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007103 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 }
7105 else
7106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007107 // Found end when searching forward or start when searching
7108 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007110 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111 }
7112
7113 if (nest == 0)
7114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007115 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116 if (flags & SP_RETCOUNT)
7117 ++retval;
7118 else
7119 retval = pos.lnum;
7120 if (flags & SP_SETPCMARK)
7121 setpcmark();
7122 curwin->w_cursor = pos;
7123 if (!(flags & SP_REPEAT))
7124 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007125 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 }
7127 }
7128
7129 if (match_pos != NULL)
7130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007131 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 match_pos->lnum = curwin->w_cursor.lnum;
7133 match_pos->col = curwin->w_cursor.col + 1;
7134 }
7135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007136 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 if ((flags & SP_NOMOVE) || retval == 0)
7138 curwin->w_cursor = save_cursor;
7139
7140theend:
7141 vim_free(pat2);
7142 vim_free(pat3);
7143 if (p_cpo == empty_option)
7144 p_cpo = save_cpo;
7145 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007146 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 free_string_option(save_cpo);
7148
7149 return retval;
7150}
7151
7152/*
7153 * "searchpos()" function
7154 */
7155 static void
7156f_searchpos(typval_T *argvars, typval_T *rettv)
7157{
7158 pos_T match_pos;
7159 int lnum = 0;
7160 int col = 0;
7161 int n;
7162 int flags = 0;
7163
7164 if (rettv_list_alloc(rettv) == FAIL)
7165 return;
7166
7167 n = search_cmn(argvars, &match_pos, &flags);
7168 if (n > 0)
7169 {
7170 lnum = match_pos.lnum;
7171 col = match_pos.col;
7172 }
7173
7174 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7175 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7176 if (flags & SP_SUBPAT)
7177 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7178}
7179
7180 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007181f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7182{
7183 dict_T *d;
7184 dictitem_T *di;
7185 char_u *csearch;
7186
7187 if (argvars[0].v_type != VAR_DICT)
7188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007189 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 return;
7191 }
7192
7193 if ((d = argvars[0].vval.v_dict) != NULL)
7194 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007195 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 if (csearch != NULL)
7197 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 if (enc_utf8)
7199 {
7200 int pcc[MAX_MCO];
7201 int c = utfc_ptr2char(csearch, pcc);
7202
7203 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7204 }
7205 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007206 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007207 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208 }
7209
7210 di = dict_find(d, (char_u *)"forward", -1);
7211 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007212 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 ? FORWARD : BACKWARD);
7214
7215 di = dict_find(d, (char_u *)"until", -1);
7216 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007217 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 }
7219}
7220
7221/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007222 * "setenv()" function
7223 */
7224 static void
7225f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7226{
7227 char_u namebuf[NUMBUFLEN];
7228 char_u valbuf[NUMBUFLEN];
7229 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7230
7231 if (argvars[1].v_type == VAR_SPECIAL
7232 && argvars[1].vval.v_number == VVAL_NULL)
7233 vim_unsetenv(name);
7234 else
7235 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7236}
7237
7238/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 * "setfperm({fname}, {mode})" function
7240 */
7241 static void
7242f_setfperm(typval_T *argvars, typval_T *rettv)
7243{
7244 char_u *fname;
7245 char_u modebuf[NUMBUFLEN];
7246 char_u *mode_str;
7247 int i;
7248 int mask;
7249 int mode = 0;
7250
7251 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007252 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 if (fname == NULL)
7254 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007255 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256 if (mode_str == NULL)
7257 return;
7258 if (STRLEN(mode_str) != 9)
7259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007260 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261 return;
7262 }
7263
7264 mask = 1;
7265 for (i = 8; i >= 0; --i)
7266 {
7267 if (mode_str[i] != '-')
7268 mode |= mask;
7269 mask = mask << 1;
7270 }
7271 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7272}
7273
7274/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 * "setpos()" function
7276 */
7277 static void
7278f_setpos(typval_T *argvars, typval_T *rettv)
7279{
7280 pos_T pos;
7281 int fnum;
7282 char_u *name;
7283 colnr_T curswant = -1;
7284
7285 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007286 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 if (name != NULL)
7288 {
7289 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7290 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007291 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 pos.col = 0;
7293 if (name[0] == '.' && name[1] == NUL)
7294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007295 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007296 curwin->w_cursor = pos;
7297 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007299 curwin->w_curswant = curswant - 1;
7300 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007302 check_cursor();
7303 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 }
7305 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007307 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007308 if (setmark_pos(name[1], &pos, fnum) == OK)
7309 rettv->vval.v_number = 0;
7310 }
7311 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007312 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 }
7314 }
7315}
7316
7317/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007318 * Translate a register type string to the yank type and block length
7319 */
7320 static int
7321get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7322{
7323 char_u *stropt = *pp;
7324 switch (*stropt)
7325 {
7326 case 'v': case 'c': // character-wise selection
7327 *yank_type = MCHAR;
7328 break;
7329 case 'V': case 'l': // line-wise selection
7330 *yank_type = MLINE;
7331 break;
7332 case 'b': case Ctrl_V: // block-wise selection
7333 *yank_type = MBLOCK;
7334 if (VIM_ISDIGIT(stropt[1]))
7335 {
7336 ++stropt;
7337 *block_len = getdigits(&stropt) - 1;
7338 --stropt;
7339 }
7340 break;
7341 default:
7342 return FAIL;
7343 }
7344 *pp = stropt;
7345 return OK;
7346}
7347
7348/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 * "setreg()" function
7350 */
7351 static void
7352f_setreg(typval_T *argvars, typval_T *rettv)
7353{
7354 int regname;
7355 char_u *strregname;
7356 char_u *stropt;
7357 char_u *strval;
7358 int append;
7359 char_u yank_type;
7360 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007361 typval_T *regcontents;
7362 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007364 pointreg = 0;
7365 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 block_len = -1;
7367 yank_type = MAUTO;
7368 append = FALSE;
7369
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007370 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007371 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372
7373 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007374 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 regname = *strregname;
7376 if (regname == 0 || regname == '@')
7377 regname = '"';
7378
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007379 if (argvars[1].v_type == VAR_DICT)
7380 {
7381 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007382 dictitem_T *di;
7383
7384 if (d == NULL || d->dv_hashtab.ht_used == 0)
7385 {
7386 // Empty dict, clear the register (like setreg(0, []))
7387 char_u *lstval[2] = {NULL, NULL};
7388 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7389 return;
7390 }
7391
7392 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007393 if (di != NULL)
7394 regcontents = &di->di_tv;
7395
7396 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7397 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007398 {
7399 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7400
7401 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007402 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007403 semsg(_(e_invargval), "value");
7404 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007405 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007406 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007407
7408 if (regname == '"')
7409 {
7410 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7411 if (stropt != NULL)
7412 {
7413 pointreg = *stropt;
7414 regname = pointreg;
7415 }
7416 }
7417 else if (dict_get_number(d, (char_u *)"isunnamed"))
7418 pointreg = regname;
7419 }
7420 else
7421 regcontents = &argvars[1];
7422
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 if (argvars[2].v_type != VAR_UNKNOWN)
7424 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007425 if (yank_type != MAUTO)
7426 {
7427 semsg(_(e_toomanyarg), "setreg");
7428 return;
7429 }
7430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007431 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007432 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007433 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 for (; *stropt != NUL; ++stropt)
7435 switch (*stropt)
7436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007437 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 append = TRUE;
7439 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007440 default:
7441 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 }
7443 }
7444
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007445 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 {
7447 char_u **lstval;
7448 char_u **allocval;
7449 char_u buf[NUMBUFLEN];
7450 char_u **curval;
7451 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007452 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 listitem_T *li;
7454 int len;
7455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007456 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007457 len = ll == NULL ? 0 : ll->lv_len;
7458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007459 // First half: use for pointers to result lines; second half: use for
7460 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007461 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462 if (lstval == NULL)
7463 return;
7464 curval = lstval;
7465 allocval = lstval + len + 2;
7466 curallocval = allocval;
7467
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007468 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007469 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007470 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007471 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007473 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 if (strval == NULL)
7475 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007476 if (strval == buf)
7477 {
7478 // Need to make a copy, next tv_get_string_buf_chk() will
7479 // overwrite the string.
7480 strval = vim_strsave(buf);
7481 if (strval == NULL)
7482 goto free_lstval;
7483 *curallocval++ = strval;
7484 }
7485 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 }
7488 *curval++ = NULL;
7489
7490 write_reg_contents_lst(regname, lstval, -1,
7491 append, yank_type, block_len);
7492free_lstval:
7493 while (curallocval > allocval)
7494 vim_free(*--curallocval);
7495 vim_free(lstval);
7496 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007497 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007499 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500 if (strval == NULL)
7501 return;
7502 write_reg_contents_ex(regname, strval, -1,
7503 append, yank_type, block_len);
7504 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007505 if (pointreg != 0)
7506 get_yank_register(pointreg, TRUE);
7507
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007508 rettv->vval.v_number = 0;
7509}
7510
7511/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007512 * "settagstack()" function
7513 */
7514 static void
7515f_settagstack(typval_T *argvars, typval_T *rettv)
7516{
7517 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7518 win_T *wp;
7519 dict_T *d;
7520 int action = 'r';
7521
7522 rettv->vval.v_number = -1;
7523
7524 // first argument: window number or id
7525 wp = find_win_by_nr_or_id(&argvars[0]);
7526 if (wp == NULL)
7527 return;
7528
7529 // second argument: dict with items to set in the tag stack
7530 if (argvars[1].v_type != VAR_DICT)
7531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007532 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007533 return;
7534 }
7535 d = argvars[1].vval.v_dict;
7536 if (d == NULL)
7537 return;
7538
7539 // third argument: action - 'a' for append and 'r' for replace.
7540 // default is to replace the stack.
7541 if (argvars[2].v_type == VAR_UNKNOWN)
7542 action = 'r';
7543 else if (argvars[2].v_type == VAR_STRING)
7544 {
7545 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007547 if (actstr == NULL)
7548 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007549 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7550 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007551 action = *actstr;
7552 else
7553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007554 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007555 return;
7556 }
7557 }
7558 else
7559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007560 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007561 return;
7562 }
7563
7564 if (set_tagstack(wp, d, action) == OK)
7565 rettv->vval.v_number = 0;
7566}
7567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007568#ifdef FEAT_CRYPT
7569/*
7570 * "sha256({string})" function
7571 */
7572 static void
7573f_sha256(typval_T *argvars, typval_T *rettv)
7574{
7575 char_u *p;
7576
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007577 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 rettv->vval.v_string = vim_strsave(
7579 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7580 rettv->v_type = VAR_STRING;
7581}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007582#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583
7584/*
7585 * "shellescape({string})" function
7586 */
7587 static void
7588f_shellescape(typval_T *argvars, typval_T *rettv)
7589{
Bram Moolenaar20615522017-06-05 18:46:26 +02007590 int do_special = non_zero_arg(&argvars[1]);
7591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007592 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007593 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 rettv->v_type = VAR_STRING;
7595}
7596
7597/*
7598 * shiftwidth() function
7599 */
7600 static void
7601f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7602{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007603 rettv->vval.v_number = 0;
7604
7605 if (argvars[0].v_type != VAR_UNKNOWN)
7606 {
7607 long col;
7608
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007609 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007610 if (col < 0)
7611 return; // type error; errmsg already given
7612#ifdef FEAT_VARTABS
7613 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7614 return;
7615#endif
7616 }
7617
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 rettv->vval.v_number = get_sw_value(curbuf);
7619}
7620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621#ifdef FEAT_FLOAT
7622/*
7623 * "sin()" function
7624 */
7625 static void
7626f_sin(typval_T *argvars, typval_T *rettv)
7627{
7628 float_T f = 0.0;
7629
7630 rettv->v_type = VAR_FLOAT;
7631 if (get_float_arg(argvars, &f) == OK)
7632 rettv->vval.v_float = sin(f);
7633 else
7634 rettv->vval.v_float = 0.0;
7635}
7636
7637/*
7638 * "sinh()" function
7639 */
7640 static void
7641f_sinh(typval_T *argvars, typval_T *rettv)
7642{
7643 float_T f = 0.0;
7644
7645 rettv->v_type = VAR_FLOAT;
7646 if (get_float_arg(argvars, &f) == OK)
7647 rettv->vval.v_float = sinh(f);
7648 else
7649 rettv->vval.v_float = 0.0;
7650}
7651#endif
7652
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653/*
7654 * "soundfold({word})" function
7655 */
7656 static void
7657f_soundfold(typval_T *argvars, typval_T *rettv)
7658{
7659 char_u *s;
7660
7661 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007662 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663#ifdef FEAT_SPELL
7664 rettv->vval.v_string = eval_soundfold(s);
7665#else
7666 rettv->vval.v_string = vim_strsave(s);
7667#endif
7668}
7669
7670/*
7671 * "spellbadword()" function
7672 */
7673 static void
7674f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7675{
7676 char_u *word = (char_u *)"";
7677 hlf_T attr = HLF_COUNT;
7678 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007679#ifdef FEAT_SPELL
7680 int wo_spell_save = curwin->w_p_spell;
7681
7682 if (!curwin->w_p_spell)
7683 {
7684 did_set_spelllang(curwin);
7685 curwin->w_p_spell = TRUE;
7686 }
7687
7688 if (*curwin->w_s->b_p_spl == NUL)
7689 {
7690 emsg(_(e_no_spell));
7691 curwin->w_p_spell = wo_spell_save;
7692 return;
7693 }
7694#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695
7696 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007697 {
7698#ifdef FEAT_SPELL
7699 curwin->w_p_spell = wo_spell_save;
7700#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703
7704#ifdef FEAT_SPELL
7705 if (argvars[0].v_type == VAR_UNKNOWN)
7706 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007707 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7709 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007710 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007712 curwin->w_set_curswant = TRUE;
7713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007715 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007717 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 int capcol = -1;
7719
7720 if (str != NULL)
7721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007722 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 while (*str != NUL)
7724 {
7725 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7726 if (attr != HLF_COUNT)
7727 {
7728 word = str;
7729 break;
7730 }
7731 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007732 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007733 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007734 }
7735 }
7736 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007737 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738#endif
7739
7740 list_append_string(rettv->vval.v_list, word, len);
7741 list_append_string(rettv->vval.v_list, (char_u *)(
7742 attr == HLF_SPB ? "bad" :
7743 attr == HLF_SPR ? "rare" :
7744 attr == HLF_SPL ? "local" :
7745 attr == HLF_SPC ? "caps" :
7746 ""), -1);
7747}
7748
7749/*
7750 * "spellsuggest()" function
7751 */
7752 static void
7753f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7754{
7755#ifdef FEAT_SPELL
7756 char_u *str;
7757 int typeerr = FALSE;
7758 int maxcount;
7759 garray_T ga;
7760 int i;
7761 listitem_T *li;
7762 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007763 int wo_spell_save = curwin->w_p_spell;
7764
7765 if (!curwin->w_p_spell)
7766 {
7767 did_set_spelllang(curwin);
7768 curwin->w_p_spell = TRUE;
7769 }
7770
7771 if (*curwin->w_s->b_p_spl == NUL)
7772 {
7773 emsg(_(e_no_spell));
7774 curwin->w_p_spell = wo_spell_save;
7775 return;
7776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777#endif
7778
7779 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007780 {
7781#ifdef FEAT_SPELL
7782 curwin->w_p_spell = wo_spell_save;
7783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007785 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786
7787#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007788 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007790 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791 if (argvars[1].v_type != VAR_UNKNOWN)
7792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007793 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 if (maxcount <= 0)
7795 return;
7796 if (argvars[2].v_type != VAR_UNKNOWN)
7797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007798 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 if (typeerr)
7800 return;
7801 }
7802 }
7803 else
7804 maxcount = 25;
7805
7806 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7807
7808 for (i = 0; i < ga.ga_len; ++i)
7809 {
7810 str = ((char_u **)ga.ga_data)[i];
7811
7812 li = listitem_alloc();
7813 if (li == NULL)
7814 vim_free(str);
7815 else
7816 {
7817 li->li_tv.v_type = VAR_STRING;
7818 li->li_tv.v_lock = 0;
7819 li->li_tv.vval.v_string = str;
7820 list_append(rettv->vval.v_list, li);
7821 }
7822 }
7823 ga_clear(&ga);
7824 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007825 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826#endif
7827}
7828
7829 static void
7830f_split(typval_T *argvars, typval_T *rettv)
7831{
7832 char_u *str;
7833 char_u *end;
7834 char_u *pat = NULL;
7835 regmatch_T regmatch;
7836 char_u patbuf[NUMBUFLEN];
7837 char_u *save_cpo;
7838 int match;
7839 colnr_T col = 0;
7840 int keepempty = FALSE;
7841 int typeerr = FALSE;
7842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007843 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 save_cpo = p_cpo;
7845 p_cpo = (char_u *)"";
7846
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007847 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 if (argvars[1].v_type != VAR_UNKNOWN)
7849 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007850 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851 if (pat == NULL)
7852 typeerr = TRUE;
7853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007854 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 }
7856 if (pat == NULL || *pat == NUL)
7857 pat = (char_u *)"[\\x01- ]\\+";
7858
7859 if (rettv_list_alloc(rettv) == FAIL)
7860 return;
7861 if (typeerr)
7862 return;
7863
7864 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7865 if (regmatch.regprog != NULL)
7866 {
7867 regmatch.rm_ic = FALSE;
7868 while (*str != NUL || keepempty)
7869 {
7870 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007871 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 else
7873 match = vim_regexec_nl(&regmatch, str, col);
7874 if (match)
7875 end = regmatch.startp[0];
7876 else
7877 end = str + STRLEN(str);
7878 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7879 && *str != NUL && match && end < regmatch.endp[0]))
7880 {
7881 if (list_append_string(rettv->vval.v_list, str,
7882 (int)(end - str)) == FAIL)
7883 break;
7884 }
7885 if (!match)
7886 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007887 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 if (regmatch.endp[0] > str)
7889 col = 0;
7890 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007891 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 str = regmatch.endp[0];
7894 }
7895
7896 vim_regfree(regmatch.regprog);
7897 }
7898
7899 p_cpo = save_cpo;
7900}
7901
7902#ifdef FEAT_FLOAT
7903/*
7904 * "sqrt()" function
7905 */
7906 static void
7907f_sqrt(typval_T *argvars, typval_T *rettv)
7908{
7909 float_T f = 0.0;
7910
7911 rettv->v_type = VAR_FLOAT;
7912 if (get_float_arg(argvars, &f) == OK)
7913 rettv->vval.v_float = sqrt(f);
7914 else
7915 rettv->vval.v_float = 0.0;
7916}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007919#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921 * "str2float()" function
7922 */
7923 static void
7924f_str2float(typval_T *argvars, typval_T *rettv)
7925{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007926 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007927 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928
Bram Moolenaar08243d22017-01-10 16:12:29 +01007929 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 p = skipwhite(p + 1);
7931 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007932 if (isneg)
7933 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 rettv->v_type = VAR_FLOAT;
7935}
7936#endif
7937
7938/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007939 * "str2list()" function
7940 */
7941 static void
7942f_str2list(typval_T *argvars, typval_T *rettv)
7943{
7944 char_u *p;
7945 int utf8 = FALSE;
7946
7947 if (rettv_list_alloc(rettv) == FAIL)
7948 return;
7949
7950 if (argvars[1].v_type != VAR_UNKNOWN)
7951 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7952
7953 p = tv_get_string(&argvars[0]);
7954
7955 if (has_mbyte || utf8)
7956 {
7957 int (*ptr2len)(char_u *);
7958 int (*ptr2char)(char_u *);
7959
7960 if (utf8 || enc_utf8)
7961 {
7962 ptr2len = utf_ptr2len;
7963 ptr2char = utf_ptr2char;
7964 }
7965 else
7966 {
7967 ptr2len = mb_ptr2len;
7968 ptr2char = mb_ptr2char;
7969 }
7970
7971 for ( ; *p != NUL; p += (*ptr2len)(p))
7972 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7973 }
7974 else
7975 for ( ; *p != NUL; ++p)
7976 list_append_number(rettv->vval.v_list, *p);
7977}
7978
7979/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 * "str2nr()" function
7981 */
7982 static void
7983f_str2nr(typval_T *argvars, typval_T *rettv)
7984{
7985 int base = 10;
7986 char_u *p;
7987 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007988 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01007989 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990
7991 if (argvars[1].v_type != VAR_UNKNOWN)
7992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007993 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 if (base != 2 && base != 8 && base != 10 && base != 16)
7995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007996 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 return;
7998 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007999 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8000 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 }
8002
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008003 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008004 isneg = (*p == '-');
8005 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 p = skipwhite(p + 1);
8007 switch (base)
8008 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008009 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008010 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008011 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008012 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008013 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8014 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008015 if (isneg)
8016 rettv->vval.v_number = -n;
8017 else
8018 rettv->vval.v_number = n;
8019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020}
8021
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022/*
8023 * "strgetchar()" function
8024 */
8025 static void
8026f_strgetchar(typval_T *argvars, typval_T *rettv)
8027{
8028 char_u *str;
8029 int len;
8030 int error = FALSE;
8031 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008032 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008033
8034 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008035 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 if (str == NULL)
8037 return;
8038 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008039 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 if (error)
8041 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042
Bram Moolenaar13505972019-01-24 15:04:48 +01008043 while (charidx >= 0 && byteidx < len)
8044 {
8045 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008047 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8048 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008050 --charidx;
8051 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053}
8054
8055/*
8056 * "stridx()" function
8057 */
8058 static void
8059f_stridx(typval_T *argvars, typval_T *rettv)
8060{
8061 char_u buf[NUMBUFLEN];
8062 char_u *needle;
8063 char_u *haystack;
8064 char_u *save_haystack;
8065 char_u *pos;
8066 int start_idx;
8067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008068 needle = tv_get_string_chk(&argvars[1]);
8069 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 rettv->vval.v_number = -1;
8071 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008072 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073
8074 if (argvars[2].v_type != VAR_UNKNOWN)
8075 {
8076 int error = FALSE;
8077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008078 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 if (error || start_idx >= (int)STRLEN(haystack))
8080 return;
8081 if (start_idx >= 0)
8082 haystack += start_idx;
8083 }
8084
8085 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8086 if (pos != NULL)
8087 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8088}
8089
8090/*
8091 * "string()" function
8092 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008093 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094f_string(typval_T *argvars, typval_T *rettv)
8095{
8096 char_u *tofree;
8097 char_u numbuf[NUMBUFLEN];
8098
8099 rettv->v_type = VAR_STRING;
8100 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8101 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008102 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008103 if (rettv->vval.v_string != NULL && tofree == NULL)
8104 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8105}
8106
8107/*
8108 * "strlen()" function
8109 */
8110 static void
8111f_strlen(typval_T *argvars, typval_T *rettv)
8112{
8113 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115}
8116
8117/*
8118 * "strchars()" function
8119 */
8120 static void
8121f_strchars(typval_T *argvars, typval_T *rettv)
8122{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008123 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 varnumber_T len = 0;
8126 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127
8128 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008129 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008131 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 else
8133 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8135 while (*s != NUL)
8136 {
8137 func_mb_ptr2char_adv(&s);
8138 ++len;
8139 }
8140 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 }
8142}
8143
8144/*
8145 * "strdisplaywidth()" function
8146 */
8147 static void
8148f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8149{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008150 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 int col = 0;
8152
8153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008154 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155
8156 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8157}
8158
8159/*
8160 * "strwidth()" function
8161 */
8162 static void
8163f_strwidth(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
Bram Moolenaar13505972019-01-24 15:04:48 +01008167 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168}
8169
8170/*
8171 * "strcharpart()" function
8172 */
8173 static void
8174f_strcharpart(typval_T *argvars, typval_T *rettv)
8175{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 char_u *p;
8177 int nchar;
8178 int nbyte = 0;
8179 int charlen;
8180 int len = 0;
8181 int slen;
8182 int error = FALSE;
8183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008184 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185 slen = (int)STRLEN(p);
8186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008187 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 if (!error)
8189 {
8190 if (nchar > 0)
8191 while (nchar > 0 && nbyte < slen)
8192 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008193 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194 --nchar;
8195 }
8196 else
8197 nbyte = nchar;
8198 if (argvars[2].v_type != VAR_UNKNOWN)
8199 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008200 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 while (charlen > 0 && nbyte + len < slen)
8202 {
8203 int off = nbyte + len;
8204
8205 if (off < 0)
8206 len += 1;
8207 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008208 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 --charlen;
8210 }
8211 }
8212 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008213 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 }
8215
8216 /*
8217 * Only return the overlap between the specified part and the actual
8218 * string.
8219 */
8220 if (nbyte < 0)
8221 {
8222 len += nbyte;
8223 nbyte = 0;
8224 }
8225 else if (nbyte > slen)
8226 nbyte = slen;
8227 if (len < 0)
8228 len = 0;
8229 else if (nbyte + len > slen)
8230 len = slen - nbyte;
8231
8232 rettv->v_type = VAR_STRING;
8233 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234}
8235
8236/*
8237 * "strpart()" function
8238 */
8239 static void
8240f_strpart(typval_T *argvars, typval_T *rettv)
8241{
8242 char_u *p;
8243 int n;
8244 int len;
8245 int slen;
8246 int error = FALSE;
8247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008248 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 slen = (int)STRLEN(p);
8250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008251 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 if (error)
8253 len = 0;
8254 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008255 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008257 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258
8259 /*
8260 * Only return the overlap between the specified part and the actual
8261 * string.
8262 */
8263 if (n < 0)
8264 {
8265 len += n;
8266 n = 0;
8267 }
8268 else if (n > slen)
8269 n = slen;
8270 if (len < 0)
8271 len = 0;
8272 else if (n + len > slen)
8273 len = slen - n;
8274
8275 rettv->v_type = VAR_STRING;
8276 rettv->vval.v_string = vim_strnsave(p + n, len);
8277}
8278
8279/*
8280 * "strridx()" function
8281 */
8282 static void
8283f_strridx(typval_T *argvars, typval_T *rettv)
8284{
8285 char_u buf[NUMBUFLEN];
8286 char_u *needle;
8287 char_u *haystack;
8288 char_u *rest;
8289 char_u *lastmatch = NULL;
8290 int haystack_len, end_idx;
8291
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008292 needle = tv_get_string_chk(&argvars[1]);
8293 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294
8295 rettv->vval.v_number = -1;
8296 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008297 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298
8299 haystack_len = (int)STRLEN(haystack);
8300 if (argvars[2].v_type != VAR_UNKNOWN)
8301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008302 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008303 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008305 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 }
8307 else
8308 end_idx = haystack_len;
8309
8310 if (*needle == NUL)
8311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008312 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 lastmatch = haystack + end_idx;
8314 }
8315 else
8316 {
8317 for (rest = haystack; *rest != '\0'; ++rest)
8318 {
8319 rest = (char_u *)strstr((char *)rest, (char *)needle);
8320 if (rest == NULL || rest > haystack + end_idx)
8321 break;
8322 lastmatch = rest;
8323 }
8324 }
8325
8326 if (lastmatch == NULL)
8327 rettv->vval.v_number = -1;
8328 else
8329 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8330}
8331
8332/*
8333 * "strtrans()" function
8334 */
8335 static void
8336f_strtrans(typval_T *argvars, typval_T *rettv)
8337{
8338 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340}
8341
8342/*
8343 * "submatch()" function
8344 */
8345 static void
8346f_submatch(typval_T *argvars, typval_T *rettv)
8347{
8348 int error = FALSE;
8349 int no;
8350 int retList = 0;
8351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008352 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 if (error)
8354 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008355 if (no < 0 || no >= NSUBEXP)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008358 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (error)
8363 return;
8364
8365 if (retList == 0)
8366 {
8367 rettv->v_type = VAR_STRING;
8368 rettv->vval.v_string = reg_submatch(no);
8369 }
8370 else
8371 {
8372 rettv->v_type = VAR_LIST;
8373 rettv->vval.v_list = reg_submatch_list(no);
8374 }
8375}
8376
8377/*
8378 * "substitute()" function
8379 */
8380 static void
8381f_substitute(typval_T *argvars, typval_T *rettv)
8382{
8383 char_u patbuf[NUMBUFLEN];
8384 char_u subbuf[NUMBUFLEN];
8385 char_u flagsbuf[NUMBUFLEN];
8386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008387 char_u *str = tv_get_string_chk(&argvars[0]);
8388 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008389 char_u *sub = NULL;
8390 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008391 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008393 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8394 expr = &argvars[2];
8395 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008396 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008399 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8400 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 rettv->vval.v_string = NULL;
8402 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008403 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404}
8405
8406/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008407 * "swapinfo(swap_filename)" function
8408 */
8409 static void
8410f_swapinfo(typval_T *argvars, typval_T *rettv)
8411{
8412 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008413 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008414}
8415
8416/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008417 * "swapname(expr)" function
8418 */
8419 static void
8420f_swapname(typval_T *argvars, typval_T *rettv)
8421{
8422 buf_T *buf;
8423
8424 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008425 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008426 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8427 || buf->b_ml.ml_mfp->mf_fname == NULL)
8428 rettv->vval.v_string = NULL;
8429 else
8430 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8431}
8432
8433/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008434 * "synID(lnum, col, trans)" function
8435 */
8436 static void
8437f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8438{
8439 int id = 0;
8440#ifdef FEAT_SYN_HL
8441 linenr_T lnum;
8442 colnr_T col;
8443 int trans;
8444 int transerr = FALSE;
8445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008446 lnum = tv_get_lnum(argvars); // -1 on type error
8447 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008448 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449
8450 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8451 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8452 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8453#endif
8454
8455 rettv->vval.v_number = id;
8456}
8457
8458/*
8459 * "synIDattr(id, what [, mode])" function
8460 */
8461 static void
8462f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8463{
8464 char_u *p = NULL;
8465#ifdef FEAT_SYN_HL
8466 int id;
8467 char_u *what;
8468 char_u *mode;
8469 char_u modebuf[NUMBUFLEN];
8470 int modec;
8471
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008472 id = (int)tv_get_number(&argvars[0]);
8473 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 if (argvars[2].v_type != VAR_UNKNOWN)
8475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008476 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 modec = TOLOWER_ASC(mode[0]);
8478 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008479 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 }
8481 else
8482 {
8483#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8484 if (USE_24BIT)
8485 modec = 'g';
8486 else
8487#endif
8488 if (t_colors > 1)
8489 modec = 'c';
8490 else
8491 modec = 't';
8492 }
8493
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494 switch (TOLOWER_ASC(what[0]))
8495 {
8496 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008497 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008499 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 p = highlight_has_attr(id, HL_BOLD, modec);
8501 break;
8502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008503 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 p = highlight_color(id, what, modec);
8505 break;
8506
8507 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008508 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008510 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 p = highlight_has_attr(id, HL_ITALIC, modec);
8512 break;
8513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008514 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008515 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 break;
8517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 p = highlight_has_attr(id, HL_INVERSE, modec);
8520 break;
8521
8522 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008523 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008525 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008526 else if (TOLOWER_ASC(what[1]) == 't' &&
8527 TOLOWER_ASC(what[2]) == 'r')
8528 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008529 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 p = highlight_has_attr(id, HL_STANDOUT, modec);
8531 break;
8532
8533 case 'u':
8534 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008535 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8537 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008538 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8540 break;
8541 }
8542
8543 if (p != NULL)
8544 p = vim_strsave(p);
8545#endif
8546 rettv->v_type = VAR_STRING;
8547 rettv->vval.v_string = p;
8548}
8549
8550/*
8551 * "synIDtrans(id)" function
8552 */
8553 static void
8554f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8555{
8556 int id;
8557
8558#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008559 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560
8561 if (id > 0)
8562 id = syn_get_final_id(id);
8563 else
8564#endif
8565 id = 0;
8566
8567 rettv->vval.v_number = id;
8568}
8569
8570/*
8571 * "synconcealed(lnum, col)" function
8572 */
8573 static void
8574f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8575{
8576#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8577 linenr_T lnum;
8578 colnr_T col;
8579 int syntax_flags = 0;
8580 int cchar;
8581 int matchid = 0;
8582 char_u str[NUMBUFLEN];
8583#endif
8584
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008585 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586
8587#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008588 lnum = tv_get_lnum(argvars); // -1 on type error
8589 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590
Bram Moolenaara80faa82020-04-12 19:37:17 +02008591 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592
8593 if (rettv_list_alloc(rettv) != FAIL)
8594 {
8595 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8596 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8597 && curwin->w_p_cole > 0)
8598 {
8599 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8600 syntax_flags = get_syntax_info(&matchid);
8601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008602 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8604 {
8605 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008606 if (cchar == NUL && curwin->w_p_cole == 1)
8607 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 if (cchar != NUL)
8609 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 if (has_mbyte)
8611 (*mb_char2bytes)(cchar, str);
8612 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 str[0] = cchar;
8614 }
8615 }
8616 }
8617
8618 list_append_number(rettv->vval.v_list,
8619 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008620 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 list_append_string(rettv->vval.v_list, str, -1);
8622 list_append_number(rettv->vval.v_list, matchid);
8623 }
8624#endif
8625}
8626
8627/*
8628 * "synstack(lnum, col)" function
8629 */
8630 static void
8631f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8632{
8633#ifdef FEAT_SYN_HL
8634 linenr_T lnum;
8635 colnr_T col;
8636 int i;
8637 int id;
8638#endif
8639
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008640 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641
8642#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008643 lnum = tv_get_lnum(argvars); // -1 on type error
8644 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645
8646 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8647 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8648 && rettv_list_alloc(rettv) != FAIL)
8649 {
8650 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8651 for (i = 0; ; ++i)
8652 {
8653 id = syn_get_stack_item(i);
8654 if (id < 0)
8655 break;
8656 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8657 break;
8658 }
8659 }
8660#endif
8661}
8662
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008663/*
8664 * "tabpagebuflist()" function
8665 */
8666 static void
8667f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8668{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 tabpage_T *tp;
8670 win_T *wp = NULL;
8671
8672 if (argvars[0].v_type == VAR_UNKNOWN)
8673 wp = firstwin;
8674 else
8675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008676 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 if (tp != NULL)
8678 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8679 }
8680 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8681 {
8682 for (; wp != NULL; wp = wp->w_next)
8683 if (list_append_number(rettv->vval.v_list,
8684 wp->w_buffer->b_fnum) == FAIL)
8685 break;
8686 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687}
8688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 * "tagfiles()" function
8691 */
8692 static void
8693f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8694{
8695 char_u *fname;
8696 tagname_T tn;
8697 int first;
8698
8699 if (rettv_list_alloc(rettv) == FAIL)
8700 return;
8701 fname = alloc(MAXPATHL);
8702 if (fname == NULL)
8703 return;
8704
8705 for (first = TRUE; ; first = FALSE)
8706 if (get_tagfname(&tn, first, fname) == FAIL
8707 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8708 break;
8709 tagname_free(&tn);
8710 vim_free(fname);
8711}
8712
8713/*
8714 * "taglist()" function
8715 */
8716 static void
8717f_taglist(typval_T *argvars, typval_T *rettv)
8718{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008719 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 char_u *tag_pattern;
8721
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723
8724 rettv->vval.v_number = FALSE;
8725 if (*tag_pattern == NUL)
8726 return;
8727
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008728 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008729 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008731 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732}
8733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734#ifdef FEAT_FLOAT
8735/*
8736 * "tan()" function
8737 */
8738 static void
8739f_tan(typval_T *argvars, typval_T *rettv)
8740{
8741 float_T f = 0.0;
8742
8743 rettv->v_type = VAR_FLOAT;
8744 if (get_float_arg(argvars, &f) == OK)
8745 rettv->vval.v_float = tan(f);
8746 else
8747 rettv->vval.v_float = 0.0;
8748}
8749
8750/*
8751 * "tanh()" function
8752 */
8753 static void
8754f_tanh(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 = tanh(f);
8761 else
8762 rettv->vval.v_float = 0.0;
8763}
8764#endif
8765
8766/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 * "tolower(string)" function
8768 */
8769 static void
8770f_tolower(typval_T *argvars, typval_T *rettv)
8771{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008773 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774}
8775
8776/*
8777 * "toupper(string)" function
8778 */
8779 static void
8780f_toupper(typval_T *argvars, typval_T *rettv)
8781{
8782 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008783 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784}
8785
8786/*
8787 * "tr(string, fromstr, tostr)" function
8788 */
8789 static void
8790f_tr(typval_T *argvars, typval_T *rettv)
8791{
8792 char_u *in_str;
8793 char_u *fromstr;
8794 char_u *tostr;
8795 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 int inlen;
8797 int fromlen;
8798 int tolen;
8799 int idx;
8800 char_u *cpstr;
8801 int cplen;
8802 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 char_u buf[NUMBUFLEN];
8804 char_u buf2[NUMBUFLEN];
8805 garray_T ga;
8806
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008807 in_str = tv_get_string(&argvars[0]);
8808 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8809 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008811 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 rettv->v_type = VAR_STRING;
8813 rettv->vval.v_string = NULL;
8814 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008815 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 ga_init2(&ga, (int)sizeof(char), 80);
8817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008819 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820 if (STRLEN(fromstr) != STRLEN(tostr))
8821 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008823 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 ga_clear(&ga);
8825 return;
8826 }
8827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008828 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 while (*in_str != NUL)
8830 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 if (has_mbyte)
8832 {
8833 inlen = (*mb_ptr2len)(in_str);
8834 cpstr = in_str;
8835 cplen = inlen;
8836 idx = 0;
8837 for (p = fromstr; *p != NUL; p += fromlen)
8838 {
8839 fromlen = (*mb_ptr2len)(p);
8840 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8841 {
8842 for (p = tostr; *p != NUL; p += tolen)
8843 {
8844 tolen = (*mb_ptr2len)(p);
8845 if (idx-- == 0)
8846 {
8847 cplen = tolen;
8848 cpstr = p;
8849 break;
8850 }
8851 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008852 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 goto error;
8854 break;
8855 }
8856 ++idx;
8857 }
8858
8859 if (first && cpstr == in_str)
8860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008861 // Check that fromstr and tostr have the same number of
8862 // (multi-byte) characters. Done only once when a character
8863 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 first = FALSE;
8865 for (p = tostr; *p != NUL; p += tolen)
8866 {
8867 tolen = (*mb_ptr2len)(p);
8868 --idx;
8869 }
8870 if (idx != 0)
8871 goto error;
8872 }
8873
8874 (void)ga_grow(&ga, cplen);
8875 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8876 ga.ga_len += cplen;
8877
8878 in_str += inlen;
8879 }
8880 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008882 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 p = vim_strchr(fromstr, *in_str);
8884 if (p != NULL)
8885 ga_append(&ga, tostr[p - fromstr]);
8886 else
8887 ga_append(&ga, *in_str);
8888 ++in_str;
8889 }
8890 }
8891
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008892 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 (void)ga_grow(&ga, 1);
8894 ga_append(&ga, NUL);
8895
8896 rettv->vval.v_string = ga.ga_data;
8897}
8898
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008899/*
8900 * "trim({expr})" function
8901 */
8902 static void
8903f_trim(typval_T *argvars, typval_T *rettv)
8904{
8905 char_u buf1[NUMBUFLEN];
8906 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008907 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008908 char_u *mask = NULL;
8909 char_u *tail;
8910 char_u *prev;
8911 char_u *p;
8912 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008913 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008914
8915 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008916 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008917 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008918 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008919
8920 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008922 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008923
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008925 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008926 int error = 0;
8927
8928 // leading or trailing characters to trim
8929 dir = (int)tv_get_number_chk(&argvars[2], &error);
8930 if (error)
8931 return;
8932 if (dir < 0 || dir > 2)
8933 {
8934 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8935 return;
8936 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008937 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008938 }
8939
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008940 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008941 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008942 // Trim leading characters
8943 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008944 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008945 c1 = PTR2CHAR(head);
8946 if (mask == NULL)
8947 {
8948 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008949 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008950 }
8951 else
8952 {
8953 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8954 if (c1 == PTR2CHAR(p))
8955 break;
8956 if (*p == NUL)
8957 break;
8958 }
8959 MB_PTR_ADV(head);
8960 }
8961 }
8962
8963 tail = head + STRLEN(head);
8964 if (dir == 0 || dir == 2)
8965 {
8966 // Trim trailing characters
8967 for (; tail > head; tail = prev)
8968 {
8969 prev = tail;
8970 MB_PTR_BACK(head, prev);
8971 c1 = PTR2CHAR(prev);
8972 if (mask == NULL)
8973 {
8974 if (c1 > ' ' && c1 != 0xa0)
8975 break;
8976 }
8977 else
8978 {
8979 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8980 if (c1 == PTR2CHAR(p))
8981 break;
8982 if (*p == NUL)
8983 break;
8984 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008985 }
8986 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02008987 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008988}
8989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990#ifdef FEAT_FLOAT
8991/*
8992 * "trunc({float})" function
8993 */
8994 static void
8995f_trunc(typval_T *argvars, typval_T *rettv)
8996{
8997 float_T f = 0.0;
8998
8999 rettv->v_type = VAR_FLOAT;
9000 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009001 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9003 else
9004 rettv->vval.v_float = 0.0;
9005}
9006#endif
9007
9008/*
9009 * "type(expr)" function
9010 */
9011 static void
9012f_type(typval_T *argvars, typval_T *rettv)
9013{
9014 int n = -1;
9015
9016 switch (argvars[0].v_type)
9017 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009018 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9019 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009021 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9022 case VAR_LIST: n = VAR_TYPE_LIST; break;
9023 case VAR_DICT: n = VAR_TYPE_DICT; break;
9024 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9025 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9026 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009027 case VAR_JOB: n = VAR_TYPE_JOB; break;
9028 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009029 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009031 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009032 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009033 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 n = -1;
9035 break;
9036 }
9037 rettv->vval.v_number = n;
9038}
9039
9040/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 * "virtcol(string)" function
9042 */
9043 static void
9044f_virtcol(typval_T *argvars, typval_T *rettv)
9045{
9046 colnr_T vcol = 0;
9047 pos_T *fp;
9048 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009049 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050
9051 fp = var2fpos(&argvars[0], FALSE, &fnum);
9052 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9053 && fnum == curbuf->b_fnum)
9054 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009055 // Limit the column to a valid value, getvvcol() doesn't check.
9056 if (fp->col < 0)
9057 fp->col = 0;
9058 else
9059 {
9060 len = (int)STRLEN(ml_get(fp->lnum));
9061 if (fp->col > len)
9062 fp->col = len;
9063 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 getvvcol(curwin, fp, NULL, NULL, &vcol);
9065 ++vcol;
9066 }
9067
9068 rettv->vval.v_number = vcol;
9069}
9070
9071/*
9072 * "visualmode()" function
9073 */
9074 static void
9075f_visualmode(typval_T *argvars, typval_T *rettv)
9076{
9077 char_u str[2];
9078
9079 rettv->v_type = VAR_STRING;
9080 str[0] = curbuf->b_visual_mode_eval;
9081 str[1] = NUL;
9082 rettv->vval.v_string = vim_strsave(str);
9083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009084 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009085 if (non_zero_arg(&argvars[0]))
9086 curbuf->b_visual_mode_eval = NUL;
9087}
9088
9089/*
9090 * "wildmenumode()" function
9091 */
9092 static void
9093f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9094{
9095#ifdef FEAT_WILDMENU
9096 if (wild_menu_showing)
9097 rettv->vval.v_number = 1;
9098#endif
9099}
9100
9101/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009102 * "windowsversion()" function
9103 */
9104 static void
9105f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9106{
9107 rettv->v_type = VAR_STRING;
9108 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9109}
9110
9111/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 * "wordcount()" function
9113 */
9114 static void
9115f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9116{
9117 if (rettv_dict_alloc(rettv) == FAIL)
9118 return;
9119 cursor_pos_info(rettv->vval.v_dict);
9120}
9121
9122/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123 * "xor(expr, expr)" function
9124 */
9125 static void
9126f_xor(typval_T *argvars, typval_T *rettv)
9127{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009128 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9129 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009130}
9131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009132#endif // FEAT_EVAL