blob: b8f1c2c38e05f76a677d4163a315d4677eb5c070 [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 Moolenaarfbdd08e2020-03-01 14:04:46 +0100374static type_T *ret_f_function(int argcount, type_T **argtypes);
375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200376/*
377 * Array with names and number of arguments of all internal functions
378 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
379 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200380typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200382 char *f_name; // function name
383 char f_min_argc; // minimal number of arguments
384 char f_max_argc; // maximal number of arguments
385 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100386 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
387 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200388 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200389 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200390} funcentry_T;
391
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200392// values for f_argtype; zero means it cannot be used as a method
393#define FEARG_1 1 // base is the first argument
394#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200395#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200396#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200397#define FEARG_LAST 9 // base is the last argument
398
Bram Moolenaar15c47602020-03-26 22:16:48 +0100399#ifdef FEAT_FLOAT
400# define FLOAT_FUNC(name) name
401#else
402# define FLOAT_FUNC(name) NULL
403#endif
404#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
405# define MATH_FUNC(name) name
406#else
407# define MATH_FUNC(name) NULL
408#endif
409#ifdef FEAT_TIMERS
410# define TIMER_FUNC(name) name
411#else
412# define TIMER_FUNC(name) NULL
413#endif
414#ifdef FEAT_JOB_CHANNEL
415# define JOB_FUNC(name) name
416#else
417# define JOB_FUNC(name) NULL
418#endif
419#ifdef FEAT_PROP_POPUP
420# define PROP_FUNC(name) name
421#else
422# define PROP_FUNC(name) NULL
423#endif
424#ifdef FEAT_SIGNS
425# define SIGN_FUNC(name) name
426#else
427# define SIGN_FUNC(name) NULL
428#endif
429#ifdef FEAT_SOUND
430# define SOUND_FUNC(name) name
431#else
432# define SOUND_FUNC(name) NULL
433#endif
434#ifdef FEAT_TERMINAL
435# define TERM_FUNC(name) name
436#else
437# define TERM_FUNC(name) NULL
438#endif
439
Bram Moolenaarac92e252019-08-03 21:58:38 +0200440static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200441{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100442 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
443 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100444 {"add", 2, 2, FEARG_1, ret_any, f_add},
445 {"and", 2, 2, FEARG_1, ret_number, f_and},
446 {"append", 2, 2, FEARG_LAST, ret_number, f_append},
447 {"appendbufline", 3, 3, FEARG_LAST, ret_number, f_appendbufline},
448 {"argc", 0, 1, 0, ret_number, f_argc},
449 {"argidx", 0, 0, 0, ret_number, f_argidx},
450 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
451 {"argv", 0, 2, 0, ret_any, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100452 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100453 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
454 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200455 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100456 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
457 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
458 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
459 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
460 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
461 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
462 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
463 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
464 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100465 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
466 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
467 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100468#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100469 f_balloon_gettext
470#else
471 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100472#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100473 },
474 {"balloon_show", 1, 1, FEARG_1, ret_void,
475#ifdef FEAT_BEVAL
476 f_balloon_show
477#else
478 NULL
479#endif
480 },
481 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
482#if defined(FEAT_BEVAL_TERM)
483 f_balloon_split
484#else
485 NULL
486#endif
487 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100488 {"browse", 4, 4, 0, ret_string, f_browse},
489 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
490 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
491 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
492 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
493 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
494 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
495 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
496 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
497 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
498 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
499 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
500 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
501 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
502 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
503 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
504 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
505 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100506 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
507 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
508 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
509 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
510 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
511 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
512 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
513 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
514 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
515 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
516 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
517 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
518 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
519 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
520 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
521 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
522 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
523 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
524 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100525 {"changenr", 0, 0, 0, ret_number, f_changenr},
526 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
527 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
528 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
529 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
530 {"col", 1, 1, FEARG_1, ret_number, f_col},
531 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
532 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
533 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
534 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
535 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
536 {"copy", 1, 1, FEARG_1, ret_any, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100537 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
538 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100539 {"count", 2, 4, FEARG_1, ret_number, f_count},
540 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
541 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100542 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100543#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100544 f_debugbreak
545#else
546 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200547#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100548 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100549 {"deepcopy", 1, 2, FEARG_1, ret_any, f_deepcopy},
550 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
551 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
552 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
553 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
554 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
555 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
556 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
557 {"environ", 0, 0, 0, ret_dict_string, f_environ},
558 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
559 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
560 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
561 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
562 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
563 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
564 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100565 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100566 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
567 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
568 {"extend", 2, 3, FEARG_1, ret_any, f_extend},
569 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
570 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
571 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
572 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
573 {"filter", 2, 2, FEARG_1, ret_any, f_filter},
574 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
575 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200576 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100577 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
578 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
579 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
581 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
582 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
583 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
584 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
585 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
586 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
587 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200588 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100589 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
590 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
591 {"get", 2, 3, FEARG_1, ret_any, f_get},
592 {"getbufinfo", 0, 1, 0, ret_list_dict_any, f_getbufinfo},
593 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
594 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
595 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
596 {"getchar", 0, 1, 0, ret_number, f_getchar},
597 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
598 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
599 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
600 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
601 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
602 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
603 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
604 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
605 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
606 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
607 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
608 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
609 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
610 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
611 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
612 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
613 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
614 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200615 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200616 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100617 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
618 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
619 {"getpid", 0, 0, 0, ret_number, f_getpid},
620 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200621 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100622 {"getreg", 0, 3, FEARG_1, ret_string, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200623 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100624 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
625 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
626 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
627 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
628 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
629 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
630 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
631 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
632 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
633 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
634 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
635 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
636 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100637 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100638 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
639 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
640 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
641 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
642 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
643 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
644 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
645 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
646 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
647 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
648 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
649 {"hostname", 0, 0, 0, ret_string, f_hostname},
650 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
651 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
652 {"index", 2, 4, FEARG_1, ret_number, f_index},
653 {"input", 1, 3, FEARG_1, ret_string, f_input},
654 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
655 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
656 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
657 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
658 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
659 {"insert", 2, 3, FEARG_1, ret_any, f_insert},
660 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
661 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
662 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100663 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100664 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100665 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100666 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100667 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
668 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
669 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
670 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
671 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
672 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100673 {"join", 1, 2, FEARG_1, ret_string, f_join},
674 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
675 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
676 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
677 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
678 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
679 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
680 {"len", 1, 1, FEARG_1, ret_number, f_len},
681 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
682 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
683 {"line", 1, 2, FEARG_1, ret_number, f_line},
684 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
685 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
686 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
687 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
688 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
689 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
690 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100691 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
692 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
693 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200694#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100695 f_luaeval
696#else
697 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200698#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100699 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100700 {"map", 2, 2, FEARG_1, ret_any, f_map},
701 {"maparg", 1, 4, FEARG_1, ret_string, f_maparg},
702 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200703 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100704 {"match", 2, 4, FEARG_1, ret_any, f_match},
705 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
706 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
707 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
708 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
709 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
710 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
711 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
712 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
713 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100714 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100715#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100716 f_menu_info
717#else
718 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100719#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100720 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100721 {"min", 1, 1, FEARG_1, ret_any, f_min},
722 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
723 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100724 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200725#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100726 f_mzeval
727#else
728 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200729#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100730 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100731 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
732 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
733 {"or", 2, 2, FEARG_1, ret_number, f_or},
734 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100735 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737 f_perleval
738#else
739 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200740#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741 },
742 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
743 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200744 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100745 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
746 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
747 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
748 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
749 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
750 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
751 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
752 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
753 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
754 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200755 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100756 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
757 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
758 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
759 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
760 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
761 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
762 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
763 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100764 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
765 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
767 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
768 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
769 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
770 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
771 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
772 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
773 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
774 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
775 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
776 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
777 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
778 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100779 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
780 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100781 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200782#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100783 f_py3eval
784#else
785 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100787 },
788 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200789#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100790 f_pyeval
791#else
792 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100794 },
795 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100796#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100797 f_pyxeval
798#else
799 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100800#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100801 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100802 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
803 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200804 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
805 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100806 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200807 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100808 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
809 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
810 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100811 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100812 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
813 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
814 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
815 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
816 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
817 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
818 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
819 {"remove", 2, 3, FEARG_1, ret_any, f_remove},
820 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
821 {"repeat", 2, 2, FEARG_1, ret_any, f_repeat},
822 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
823 {"reverse", 1, 1, FEARG_1, ret_any, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100824 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
825 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100826#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100827 f_rubyeval
828#else
829 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100830#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100831 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100832 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
833 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
834 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
835 {"screencol", 0, 0, 0, ret_number, f_screencol},
836 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
837 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
838 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200839 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200840 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100841 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
842 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
843 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200844 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100845 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
846 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
847 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
848 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
849 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
850 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
851 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
852 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
853 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
854 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
855 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
856 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
857 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
858 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
859 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
860 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
861 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
862 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100863 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100865 f_sha256
866#else
867 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100869 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100870 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
871 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100872 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
873 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
874 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
875 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
876 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
877 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
878 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
879 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
880 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200881 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100882 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
883 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200884 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100885 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
886 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
887 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
888 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100889 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
890 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
891 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
892 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100893 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100894 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
895 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100896 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100897 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
898 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
899 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
900 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
901 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100902 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100904 f_strftime
905#else
906 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100908 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100909 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
910 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
911 {"string", 1, 1, FEARG_1, ret_string, f_string},
912 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
913 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100914 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100915#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100916 f_strptime
917#else
918 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100919#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100920 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100921 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
922 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
923 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
924 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
925 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
926 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
927 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
928 {"synID", 3, 3, 0, ret_number, f_synID},
929 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
930 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
931 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
932 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
933 {"system", 1, 2, FEARG_1, ret_string, f_system},
934 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
935 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
936 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
937 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
938 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
939 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100940 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
941 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100942 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100943 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
944 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
945 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
946 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
947 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100948#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100949 f_term_getansicolors
950#else
951 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200952#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100953 },
954 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
955 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
956 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
957 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
958 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
959 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
960 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
961 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
962 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
963 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
964 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
965 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
966 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100967#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100968 f_term_setansicolors
969#else
970 NULL
971#endif
972 },
973 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
974 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
975 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
976 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
977 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
978 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200979 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100980 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
981 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
982 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
983 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
984 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
985 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
986 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
987 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100988 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100989 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200990 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100991 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100992 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200993 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100994 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
995 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
996 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
997 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100998 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +0200999#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001000 f_test_scrollbar
1001#else
1002 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001003#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001004 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001005 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1006 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1007 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1008 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
1009 {"test_void", 0, 0, 0, ret_any, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001010 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1011 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1012 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1013 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1014 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001015 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1016 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1017 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001018 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001019 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001020 {"type", 1, 1, FEARG_1, ret_number, f_type},
1021 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1022 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1023 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1024 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1025 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1026 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1027 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1028 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1029 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1030 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1031 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1032 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1033 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1034 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1035 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1036 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1037 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1038 {"wincol", 0, 0, 0, ret_number, f_wincol},
1039 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1040 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1041 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1042 {"winline", 0, 0, 0, ret_number, f_winline},
1043 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1044 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1045 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1046 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1047 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1048 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1049 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1050 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001051};
1052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001053/*
1054 * Function given to ExpandGeneric() to obtain the list of internal
1055 * or user defined function names.
1056 */
1057 char_u *
1058get_function_name(expand_T *xp, int idx)
1059{
1060 static int intidx = -1;
1061 char_u *name;
1062
1063 if (idx == 0)
1064 intidx = -1;
1065 if (intidx < 0)
1066 {
1067 name = get_user_func_name(xp, idx);
1068 if (name != NULL)
1069 return name;
1070 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001071 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001072 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001073 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001074 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001075 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001076 STRCAT(IObuff, ")");
1077 return IObuff;
1078 }
1079
1080 return NULL;
1081}
1082
1083/*
1084 * Function given to ExpandGeneric() to obtain the list of internal or
1085 * user defined variable or function names.
1086 */
1087 char_u *
1088get_expr_name(expand_T *xp, int idx)
1089{
1090 static int intidx = -1;
1091 char_u *name;
1092
1093 if (idx == 0)
1094 intidx = -1;
1095 if (intidx < 0)
1096 {
1097 name = get_function_name(xp, idx);
1098 if (name != NULL)
1099 return name;
1100 }
1101 return get_user_var_name(xp, ++intidx);
1102}
1103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001104/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001105 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001106 * Return index, or -1 if not found or "implemented" is TRUE and the function
1107 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001108 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001109 static int
1110find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001111{
1112 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001113 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001114 int cmp;
1115 int x;
1116
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001117 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001118
1119 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001120 while (first <= last)
1121 {
1122 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001123 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001124 if (cmp < 0)
1125 last = x - 1;
1126 else if (cmp > 0)
1127 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001128 else if (implemented && global_functions[x].f_func == NULL)
1129 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001130 else
1131 return x;
1132 }
1133 return -1;
1134}
1135
Bram Moolenaar15c47602020-03-26 22:16:48 +01001136/*
1137 * Find internal function "name" in table "global_functions".
1138 * Return index, or -1 if not found or the function is not implemented.
1139 */
1140 int
1141find_internal_func(char_u *name)
1142{
1143 return find_internal_func_opt(name, TRUE);
1144}
1145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001146 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001147has_internal_func(char_u *name)
1148{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001149 return find_internal_func_opt(name, TRUE) >= 0;
1150}
1151
1152 static int
1153has_internal_func_name(char_u *name)
1154{
1155 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001156}
1157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 char *
1159internal_func_name(int idx)
1160{
1161 return global_functions[idx].f_name;
1162}
1163
1164 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001165internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001167 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168}
1169
1170/*
1171 * Check the argument count to use for internal function "idx".
1172 * Returns OK or FAIL;
1173 */
1174 int
1175check_internal_func(int idx, int argcount)
1176{
1177 int res;
1178 char *name;
1179
1180 if (argcount < global_functions[idx].f_min_argc)
1181 res = FCERR_TOOFEW;
1182 else if (argcount > global_functions[idx].f_max_argc)
1183 res = FCERR_TOOMANY;
1184 else
1185 return OK;
1186
1187 name = internal_func_name(idx);
1188 if (res == FCERR_TOOMANY)
1189 semsg(_(e_toomanyarg), name);
1190 else
1191 semsg(_(e_toofewarg), name);
1192 return FAIL;
1193}
1194
Bram Moolenaarac92e252019-08-03 21:58:38 +02001195 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196call_internal_func(
1197 char_u *name,
1198 int argcount,
1199 typval_T *argvars,
1200 typval_T *rettv)
1201{
1202 int i;
1203
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001204 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001206 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001207 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001208 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001209 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001210 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001212 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001213 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001214}
1215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 void
1217call_internal_func_by_idx(
1218 int idx,
1219 typval_T *argvars,
1220 typval_T *rettv)
1221{
1222 global_functions[idx].f_func(argvars, rettv);
1223}
1224
Bram Moolenaarac92e252019-08-03 21:58:38 +02001225/*
1226 * Invoke a method for base->method().
1227 */
1228 int
1229call_internal_method(
1230 char_u *name,
1231 int argcount,
1232 typval_T *argvars,
1233 typval_T *rettv,
1234 typval_T *basetv)
1235{
1236 int i;
1237 int fi;
1238 typval_T argv[MAX_FUNC_ARGS + 1];
1239
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001240 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001241 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001242 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001243 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001244 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001245 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001246 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001247 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001248 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001249
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001250 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001251 {
1252 // base value goes last
1253 for (i = 0; i < argcount; ++i)
1254 argv[i] = argvars[i];
1255 argv[argcount] = *basetv;
1256 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001257 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001258 {
1259 // base value goes second
1260 argv[0] = argvars[0];
1261 argv[1] = *basetv;
1262 for (i = 1; i < argcount; ++i)
1263 argv[i + 1] = argvars[i];
1264 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001265 else if (global_functions[fi].f_argtype == FEARG_3)
1266 {
1267 // base value goes third
1268 argv[0] = argvars[0];
1269 argv[1] = argvars[1];
1270 argv[2] = *basetv;
1271 for (i = 2; i < argcount; ++i)
1272 argv[i + 1] = argvars[i];
1273 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001274 else if (global_functions[fi].f_argtype == FEARG_4)
1275 {
1276 // base value goes fourth
1277 argv[0] = argvars[0];
1278 argv[1] = argvars[1];
1279 argv[2] = argvars[2];
1280 argv[3] = *basetv;
1281 for (i = 3; i < argcount; ++i)
1282 argv[i + 1] = argvars[i];
1283 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001284 else
1285 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001286 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001287 argv[0] = *basetv;
1288 for (i = 0; i < argcount; ++i)
1289 argv[i + 1] = argvars[i];
1290 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001291 argv[argcount + 1].v_type = VAR_UNKNOWN;
1292
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001293 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001294 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001295}
1296
1297/*
1298 * Return TRUE for a non-zero Number and a non-empty String.
1299 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001300 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301non_zero_arg(typval_T *argvars)
1302{
1303 return ((argvars[0].v_type == VAR_NUMBER
1304 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001305 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 && argvars[0].vval.v_number == VVAL_TRUE)
1307 || (argvars[0].v_type == VAR_STRING
1308 && argvars[0].vval.v_string != NULL
1309 && *argvars[0].vval.v_string != NUL));
1310}
1311
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313/*
1314 * Get the float value of "argvars[0]" into "f".
1315 * Returns FAIL when the argument is not a Number or Float.
1316 */
1317 static int
1318get_float_arg(typval_T *argvars, float_T *f)
1319{
1320 if (argvars[0].v_type == VAR_FLOAT)
1321 {
1322 *f = argvars[0].vval.v_float;
1323 return OK;
1324 }
1325 if (argvars[0].v_type == VAR_NUMBER)
1326 {
1327 *f = (float_T)argvars[0].vval.v_number;
1328 return OK;
1329 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001330 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001331 return FAIL;
1332}
1333
1334/*
1335 * "abs(expr)" function
1336 */
1337 static void
1338f_abs(typval_T *argvars, typval_T *rettv)
1339{
1340 if (argvars[0].v_type == VAR_FLOAT)
1341 {
1342 rettv->v_type = VAR_FLOAT;
1343 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1344 }
1345 else
1346 {
1347 varnumber_T n;
1348 int error = FALSE;
1349
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001350 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351 if (error)
1352 rettv->vval.v_number = -1;
1353 else if (n > 0)
1354 rettv->vval.v_number = n;
1355 else
1356 rettv->vval.v_number = -n;
1357 }
1358}
1359
1360/*
1361 * "acos()" function
1362 */
1363 static void
1364f_acos(typval_T *argvars, typval_T *rettv)
1365{
1366 float_T f = 0.0;
1367
1368 rettv->v_type = VAR_FLOAT;
1369 if (get_float_arg(argvars, &f) == OK)
1370 rettv->vval.v_float = acos(f);
1371 else
1372 rettv->vval.v_float = 0.0;
1373}
1374#endif
1375
1376/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377 * "and(expr, expr)" function
1378 */
1379 static void
1380f_and(typval_T *argvars, typval_T *rettv)
1381{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001382 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1383 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001384}
1385
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386#ifdef FEAT_FLOAT
1387/*
1388 * "asin()" function
1389 */
1390 static void
1391f_asin(typval_T *argvars, typval_T *rettv)
1392{
1393 float_T f = 0.0;
1394
1395 rettv->v_type = VAR_FLOAT;
1396 if (get_float_arg(argvars, &f) == OK)
1397 rettv->vval.v_float = asin(f);
1398 else
1399 rettv->vval.v_float = 0.0;
1400}
1401
1402/*
1403 * "atan()" function
1404 */
1405 static void
1406f_atan(typval_T *argvars, typval_T *rettv)
1407{
1408 float_T f = 0.0;
1409
1410 rettv->v_type = VAR_FLOAT;
1411 if (get_float_arg(argvars, &f) == OK)
1412 rettv->vval.v_float = atan(f);
1413 else
1414 rettv->vval.v_float = 0.0;
1415}
1416
1417/*
1418 * "atan2()" function
1419 */
1420 static void
1421f_atan2(typval_T *argvars, typval_T *rettv)
1422{
1423 float_T fx = 0.0, fy = 0.0;
1424
1425 rettv->v_type = VAR_FLOAT;
1426 if (get_float_arg(argvars, &fx) == OK
1427 && get_float_arg(&argvars[1], &fy) == OK)
1428 rettv->vval.v_float = atan2(fx, fy);
1429 else
1430 rettv->vval.v_float = 0.0;
1431}
1432#endif
1433
1434/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001435 * "balloon_show()" function
1436 */
1437#ifdef FEAT_BEVAL
1438 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001439f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1440{
1441 rettv->v_type = VAR_STRING;
1442 if (balloonEval != NULL)
1443 {
1444 if (balloonEval->msg == NULL)
1445 rettv->vval.v_string = NULL;
1446 else
1447 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1448 }
1449}
1450
1451 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001452f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1453{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001454 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001455 {
1456 if (argvars[0].v_type == VAR_LIST
1457# ifdef FEAT_GUI
1458 && !gui.in_use
1459# endif
1460 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001461 {
1462 list_T *l = argvars[0].vval.v_list;
1463
1464 // empty list removes the balloon
1465 post_balloon(balloonEval, NULL,
1466 l == NULL || l->lv_len == 0 ? NULL : l);
1467 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001468 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001469 {
1470 char_u *mesg = tv_get_string_chk(&argvars[0]);
1471
1472 if (mesg != NULL)
1473 // empty string removes the balloon
1474 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1475 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001476 }
1477}
1478
Bram Moolenaar669a8282017-11-19 20:13:05 +01001479# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001480 static void
1481f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1482{
1483 if (rettv_list_alloc(rettv) == OK)
1484 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001485 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001486
1487 if (msg != NULL)
1488 {
1489 pumitem_T *array;
1490 int size = split_message(msg, &array);
1491 int i;
1492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001493 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001494 for (i = 1; i < size - 1; ++i)
1495 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001496 while (size > 0)
1497 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001498 vim_free(array);
1499 }
1500 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001501}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001502# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001503#endif
1504
1505/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001506 * Get the buffer from "arg" and give an error and return NULL if it is not
1507 * valid.
1508 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001509 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001510get_buf_arg(typval_T *arg)
1511{
1512 buf_T *buf;
1513
1514 ++emsg_off;
1515 buf = tv_get_buf(arg, FALSE);
1516 --emsg_off;
1517 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001518 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001519 return buf;
1520}
1521
1522/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 * "byte2line(byte)" function
1524 */
1525 static void
1526f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1527{
1528#ifndef FEAT_BYTEOFF
1529 rettv->vval.v_number = -1;
1530#else
1531 long boff = 0;
1532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001533 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534 if (boff < 0)
1535 rettv->vval.v_number = -1;
1536 else
1537 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1538 (linenr_T)0, &boff);
1539#endif
1540}
1541
1542 static void
1543byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1544{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001546 char_u *str;
1547 varnumber_T idx;
1548
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001549 str = tv_get_string_chk(&argvars[0]);
1550 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001551 rettv->vval.v_number = -1;
1552 if (str == NULL || idx < 0)
1553 return;
1554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001555 t = str;
1556 for ( ; idx > 0; idx--)
1557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001558 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001559 return;
1560 if (enc_utf8 && comp)
1561 t += utf_ptr2len(t);
1562 else
1563 t += (*mb_ptr2len)(t);
1564 }
1565 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566}
1567
1568/*
1569 * "byteidx()" function
1570 */
1571 static void
1572f_byteidx(typval_T *argvars, typval_T *rettv)
1573{
1574 byteidx(argvars, rettv, FALSE);
1575}
1576
1577/*
1578 * "byteidxcomp()" function
1579 */
1580 static void
1581f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1582{
1583 byteidx(argvars, rettv, TRUE);
1584}
1585
1586/*
1587 * "call(func, arglist [, dict])" function
1588 */
1589 static void
1590f_call(typval_T *argvars, typval_T *rettv)
1591{
1592 char_u *func;
1593 partial_T *partial = NULL;
1594 dict_T *selfdict = NULL;
1595
1596 if (argvars[1].v_type != VAR_LIST)
1597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001598 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599 return;
1600 }
1601 if (argvars[1].vval.v_list == NULL)
1602 return;
1603
1604 if (argvars[0].v_type == VAR_FUNC)
1605 func = argvars[0].vval.v_string;
1606 else if (argvars[0].v_type == VAR_PARTIAL)
1607 {
1608 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001609 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001610 }
1611 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001612 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001613 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001614 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615
1616 if (argvars[2].v_type != VAR_UNKNOWN)
1617 {
1618 if (argvars[2].v_type != VAR_DICT)
1619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001620 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 return;
1622 }
1623 selfdict = argvars[2].vval.v_dict;
1624 }
1625
1626 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1627}
1628
1629#ifdef FEAT_FLOAT
1630/*
1631 * "ceil({float})" function
1632 */
1633 static void
1634f_ceil(typval_T *argvars, typval_T *rettv)
1635{
1636 float_T f = 0.0;
1637
1638 rettv->v_type = VAR_FLOAT;
1639 if (get_float_arg(argvars, &f) == OK)
1640 rettv->vval.v_float = ceil(f);
1641 else
1642 rettv->vval.v_float = 0.0;
1643}
1644#endif
1645
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646/*
1647 * "changenr()" function
1648 */
1649 static void
1650f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1651{
1652 rettv->vval.v_number = curbuf->b_u_seq_cur;
1653}
1654
1655/*
1656 * "char2nr(string)" function
1657 */
1658 static void
1659f_char2nr(typval_T *argvars, typval_T *rettv)
1660{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661 if (has_mbyte)
1662 {
1663 int utf8 = 0;
1664
1665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001666 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667
1668 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001669 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001671 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672 }
1673 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001674 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675}
1676
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001677 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001678get_optional_window(typval_T *argvars, int idx)
1679{
1680 win_T *win = curwin;
1681
1682 if (argvars[idx].v_type != VAR_UNKNOWN)
1683 {
1684 win = find_win_by_nr_or_id(&argvars[idx]);
1685 if (win == NULL)
1686 {
1687 emsg(_(e_invalwindow));
1688 return NULL;
1689 }
1690 }
1691 return win;
1692}
1693
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695 * "col(string)" function
1696 */
1697 static void
1698f_col(typval_T *argvars, typval_T *rettv)
1699{
1700 colnr_T col = 0;
1701 pos_T *fp;
1702 int fnum = curbuf->b_fnum;
1703
1704 fp = var2fpos(&argvars[0], FALSE, &fnum);
1705 if (fp != NULL && fnum == curbuf->b_fnum)
1706 {
1707 if (fp->col == MAXCOL)
1708 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001709 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1711 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1712 else
1713 col = MAXCOL;
1714 }
1715 else
1716 {
1717 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001718 // col(".") when the cursor is on the NUL at the end of the line
1719 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720 if (virtual_active() && fp == &curwin->w_cursor)
1721 {
1722 char_u *p = ml_get_cursor();
1723
1724 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1725 curwin->w_virtcol - curwin->w_cursor.coladd))
1726 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727 int l;
1728
1729 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1730 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 }
1732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001733 }
1734 }
1735 rettv->vval.v_number = col;
1736}
1737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001738/*
1739 * "confirm(message, buttons[, default [, type]])" function
1740 */
1741 static void
1742f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1743{
1744#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1745 char_u *message;
1746 char_u *buttons = NULL;
1747 char_u buf[NUMBUFLEN];
1748 char_u buf2[NUMBUFLEN];
1749 int def = 1;
1750 int type = VIM_GENERIC;
1751 char_u *typestr;
1752 int error = FALSE;
1753
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001754 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001755 if (message == NULL)
1756 error = TRUE;
1757 if (argvars[1].v_type != VAR_UNKNOWN)
1758 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001759 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001760 if (buttons == NULL)
1761 error = TRUE;
1762 if (argvars[2].v_type != VAR_UNKNOWN)
1763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001764 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001765 if (argvars[3].v_type != VAR_UNKNOWN)
1766 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001767 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001768 if (typestr == NULL)
1769 error = TRUE;
1770 else
1771 {
1772 switch (TOUPPER_ASC(*typestr))
1773 {
1774 case 'E': type = VIM_ERROR; break;
1775 case 'Q': type = VIM_QUESTION; break;
1776 case 'I': type = VIM_INFO; break;
1777 case 'W': type = VIM_WARNING; break;
1778 case 'G': type = VIM_GENERIC; break;
1779 }
1780 }
1781 }
1782 }
1783 }
1784
1785 if (buttons == NULL || *buttons == NUL)
1786 buttons = (char_u *)_("&Ok");
1787
1788 if (!error)
1789 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1790 def, NULL, FALSE);
1791#endif
1792}
1793
1794/*
1795 * "copy()" function
1796 */
1797 static void
1798f_copy(typval_T *argvars, typval_T *rettv)
1799{
1800 item_copy(&argvars[0], rettv, FALSE, 0);
1801}
1802
1803#ifdef FEAT_FLOAT
1804/*
1805 * "cos()" function
1806 */
1807 static void
1808f_cos(typval_T *argvars, typval_T *rettv)
1809{
1810 float_T f = 0.0;
1811
1812 rettv->v_type = VAR_FLOAT;
1813 if (get_float_arg(argvars, &f) == OK)
1814 rettv->vval.v_float = cos(f);
1815 else
1816 rettv->vval.v_float = 0.0;
1817}
1818
1819/*
1820 * "cosh()" function
1821 */
1822 static void
1823f_cosh(typval_T *argvars, typval_T *rettv)
1824{
1825 float_T f = 0.0;
1826
1827 rettv->v_type = VAR_FLOAT;
1828 if (get_float_arg(argvars, &f) == OK)
1829 rettv->vval.v_float = cosh(f);
1830 else
1831 rettv->vval.v_float = 0.0;
1832}
1833#endif
1834
1835/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001836 * "cursor(lnum, col)" function, or
1837 * "cursor(list)"
1838 *
1839 * Moves the cursor to the specified line and column.
1840 * Returns 0 when the position could be set, -1 otherwise.
1841 */
1842 static void
1843f_cursor(typval_T *argvars, typval_T *rettv)
1844{
1845 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001846 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001847 int set_curswant = TRUE;
1848
1849 rettv->vval.v_number = -1;
1850 if (argvars[1].v_type == VAR_UNKNOWN)
1851 {
1852 pos_T pos;
1853 colnr_T curswant = -1;
1854
1855 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001857 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001858 return;
1859 }
1860 line = pos.lnum;
1861 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001862 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001863 if (curswant >= 0)
1864 {
1865 curwin->w_curswant = curswant - 1;
1866 set_curswant = FALSE;
1867 }
1868 }
1869 else
1870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001871 line = tv_get_lnum(argvars);
1872 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001874 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001876 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001877 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 if (line > 0)
1879 curwin->w_cursor.lnum = line;
1880 if (col > 0)
1881 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001882 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887 if (has_mbyte)
1888 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889
1890 curwin->w_set_curswant = set_curswant;
1891 rettv->vval.v_number = 0;
1892}
1893
Bram Moolenaar4f974752019-02-17 17:44:42 +01001894#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001895/*
1896 * "debugbreak()" function
1897 */
1898 static void
1899f_debugbreak(typval_T *argvars, typval_T *rettv)
1900{
1901 int pid;
1902
1903 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001904 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001905 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001906 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001907 else
1908 {
1909 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1910
1911 if (hProcess != NULL)
1912 {
1913 DebugBreakProcess(hProcess);
1914 CloseHandle(hProcess);
1915 rettv->vval.v_number = OK;
1916 }
1917 }
1918}
1919#endif
1920
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921/*
1922 * "deepcopy()" function
1923 */
1924 static void
1925f_deepcopy(typval_T *argvars, typval_T *rettv)
1926{
1927 int noref = 0;
1928 int copyID;
1929
1930 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001931 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001932 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001933 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 else
1935 {
1936 copyID = get_copyID();
1937 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1938 }
1939}
1940
1941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001942 * "did_filetype()" function
1943 */
1944 static void
1945f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001947 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948}
1949
1950/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001951 * "echoraw({expr})" function
1952 */
1953 static void
1954f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1955{
1956 char_u *str = tv_get_string_chk(&argvars[0]);
1957
1958 if (str != NULL && *str != NUL)
1959 {
1960 out_str(str);
1961 out_flush();
1962 }
1963}
1964
1965/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001966 * "empty({expr})" function
1967 */
1968 static void
1969f_empty(typval_T *argvars, typval_T *rettv)
1970{
1971 int n = FALSE;
1972
1973 switch (argvars[0].v_type)
1974 {
1975 case VAR_STRING:
1976 case VAR_FUNC:
1977 n = argvars[0].vval.v_string == NULL
1978 || *argvars[0].vval.v_string == NUL;
1979 break;
1980 case VAR_PARTIAL:
1981 n = FALSE;
1982 break;
1983 case VAR_NUMBER:
1984 n = argvars[0].vval.v_number == 0;
1985 break;
1986 case VAR_FLOAT:
1987#ifdef FEAT_FLOAT
1988 n = argvars[0].vval.v_float == 0.0;
1989 break;
1990#endif
1991 case VAR_LIST:
1992 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001993 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994 break;
1995 case VAR_DICT:
1996 n = argvars[0].vval.v_dict == NULL
1997 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
1998 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001999 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000 case VAR_SPECIAL:
2001 n = argvars[0].vval.v_number != VVAL_TRUE;
2002 break;
2003
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002004 case VAR_BLOB:
2005 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002006 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2007 break;
2008
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002009 case VAR_JOB:
2010#ifdef FEAT_JOB_CHANNEL
2011 n = argvars[0].vval.v_job == NULL
2012 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2013 break;
2014#endif
2015 case VAR_CHANNEL:
2016#ifdef FEAT_JOB_CHANNEL
2017 n = argvars[0].vval.v_channel == NULL
2018 || !channel_is_open(argvars[0].vval.v_channel);
2019 break;
2020#endif
2021 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002022 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002024 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002025 n = TRUE;
2026 break;
2027 }
2028
2029 rettv->vval.v_number = n;
2030}
2031
2032/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002033 * "environ()" function
2034 */
2035 static void
2036f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2037{
2038#if !defined(AMIGA)
2039 int i = 0;
2040 char_u *entry, *value;
2041# ifdef MSWIN
2042 extern wchar_t **_wenviron;
2043# else
2044 extern char **environ;
2045# endif
2046
2047 if (rettv_dict_alloc(rettv) != OK)
2048 return;
2049
2050# ifdef MSWIN
2051 if (*_wenviron == NULL)
2052 return;
2053# else
2054 if (*environ == NULL)
2055 return;
2056# endif
2057
2058 for (i = 0; ; ++i)
2059 {
2060# ifdef MSWIN
2061 short_u *p;
2062
2063 if ((p = (short_u *)_wenviron[i]) == NULL)
2064 return;
2065 entry = utf16_to_enc(p, NULL);
2066# else
2067 if ((entry = (char_u *)environ[i]) == NULL)
2068 return;
2069 entry = vim_strsave(entry);
2070# endif
2071 if (entry == NULL) // out of memory
2072 return;
2073 if ((value = vim_strchr(entry, '=')) == NULL)
2074 {
2075 vim_free(entry);
2076 continue;
2077 }
2078 *value++ = NUL;
2079 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2080 vim_free(entry);
2081 }
2082#endif
2083}
2084
2085/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 * "escape({string}, {chars})" function
2087 */
2088 static void
2089f_escape(typval_T *argvars, typval_T *rettv)
2090{
2091 char_u buf[NUMBUFLEN];
2092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2094 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 rettv->v_type = VAR_STRING;
2096}
2097
2098/*
2099 * "eval()" function
2100 */
2101 static void
2102f_eval(typval_T *argvars, typval_T *rettv)
2103{
2104 char_u *s, *p;
2105
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002106 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 if (s != NULL)
2108 s = skipwhite(s);
2109
2110 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 {
2113 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002114 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115 need_clr_eos = FALSE;
2116 rettv->v_type = VAR_NUMBER;
2117 rettv->vval.v_number = 0;
2118 }
2119 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002121}
2122
2123/*
2124 * "eventhandler()" function
2125 */
2126 static void
2127f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2128{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002129 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002130}
2131
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132static garray_T redir_execute_ga;
2133
2134/*
2135 * Append "value[value_len]" to the execute() output.
2136 */
2137 void
2138execute_redir_str(char_u *value, int value_len)
2139{
2140 int len;
2141
2142 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002143 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002145 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146 if (ga_grow(&redir_execute_ga, len) == OK)
2147 {
2148 mch_memmove((char *)redir_execute_ga.ga_data
2149 + redir_execute_ga.ga_len, value, len);
2150 redir_execute_ga.ga_len += len;
2151 }
2152}
2153
2154/*
2155 * Get next line from a list.
2156 * Called by do_cmdline() to get the next line.
2157 * Returns allocated string, or NULL for end of function.
2158 */
2159
2160 static char_u *
2161get_list_line(
2162 int c UNUSED,
2163 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002164 int indent UNUSED,
2165 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166{
2167 listitem_T **p = (listitem_T **)cookie;
2168 listitem_T *item = *p;
2169 char_u buf[NUMBUFLEN];
2170 char_u *s;
2171
2172 if (item == NULL)
2173 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002174 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 *p = item->li_next;
2176 return s == NULL ? NULL : vim_strsave(s);
2177}
2178
2179/*
2180 * "execute()" function
2181 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002182 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002183execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184{
2185 char_u *cmd = NULL;
2186 list_T *list = NULL;
2187 int save_msg_silent = msg_silent;
2188 int save_emsg_silent = emsg_silent;
2189 int save_emsg_noredir = emsg_noredir;
2190 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002191 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002193 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002194 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002195
2196 rettv->vval.v_string = NULL;
2197 rettv->v_type = VAR_STRING;
2198
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002199 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002200 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002201 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002202 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002203 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204 return;
2205 ++list->lv_refcount;
2206 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002207 else if (argvars[arg_off].v_type == VAR_JOB
2208 || argvars[arg_off].v_type == VAR_CHANNEL)
2209 {
2210 emsg(_(e_inval_string));
2211 return;
2212 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002213 else
2214 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002215 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 if (cmd == NULL)
2217 return;
2218 }
2219
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002220 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221 {
2222 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002223 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224
2225 if (s == NULL)
2226 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002227 if (*s == NUL)
2228 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 if (STRNCMP(s, "silent", 6) == 0)
2230 ++msg_silent;
2231 if (STRCMP(s, "silent!") == 0)
2232 {
2233 emsg_silent = TRUE;
2234 emsg_noredir = TRUE;
2235 }
2236 }
2237 else
2238 ++msg_silent;
2239
2240 if (redir_execute)
2241 save_ga = redir_execute_ga;
2242 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2243 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002244 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002245 if (!echo_output)
2246 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002247
2248 if (cmd != NULL)
2249 do_cmdline_cmd(cmd);
2250 else
2251 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002252 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002253
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002254 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002255 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002256 do_cmdline(NULL, get_list_line, (void *)&item,
2257 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2258 --list->lv_refcount;
2259 }
2260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002261 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002262 if (ga_grow(&redir_execute_ga, 1) == OK)
2263 {
2264 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2265 rettv->vval.v_string = redir_execute_ga.ga_data;
2266 }
2267 else
2268 {
2269 ga_clear(&redir_execute_ga);
2270 rettv->vval.v_string = NULL;
2271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 msg_silent = save_msg_silent;
2273 emsg_silent = save_emsg_silent;
2274 emsg_noredir = save_emsg_noredir;
2275
2276 redir_execute = save_redir_execute;
2277 if (redir_execute)
2278 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002279 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002281 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002282 if (echo_output)
2283 // When not working silently: put it in column zero. A following
2284 // "echon" will overwrite the message, unavoidably.
2285 msg_col = 0;
2286 else
2287 // When working silently: Put it back where it was, since nothing
2288 // should have been written.
2289 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002290}
2291
2292/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002293 * "execute()" function
2294 */
2295 static void
2296f_execute(typval_T *argvars, typval_T *rettv)
2297{
2298 execute_common(argvars, rettv, 0);
2299}
2300
2301/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302 * "exists()" function
2303 */
2304 static void
2305f_exists(typval_T *argvars, typval_T *rettv)
2306{
2307 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002309
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002310 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002311 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002313 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314 if (mch_getenv(p + 1) != NULL)
2315 n = TRUE;
2316 else
2317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002319 p = expand_env_save(p);
2320 if (p != NULL && *p != '$')
2321 n = TRUE;
2322 vim_free(p);
2323 }
2324 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002325 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 {
2327 n = (get_option_tv(&p, NULL, TRUE) == OK);
2328 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002329 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002330 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002331 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002332 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002333 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002334 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002335 else if (*p == '?') // internal function only
2336 {
2337 n = has_internal_func_name(p + 1);
2338 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002339 else if (*p == ':')
2340 {
2341 n = cmd_exists(p + 1);
2342 }
2343 else if (*p == '#')
2344 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 if (p[1] == '#')
2346 n = autocmd_supported(p + 2);
2347 else
2348 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002349 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002350 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002352 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 }
2354
2355 rettv->vval.v_number = n;
2356}
2357
2358#ifdef FEAT_FLOAT
2359/*
2360 * "exp()" function
2361 */
2362 static void
2363f_exp(typval_T *argvars, typval_T *rettv)
2364{
2365 float_T f = 0.0;
2366
2367 rettv->v_type = VAR_FLOAT;
2368 if (get_float_arg(argvars, &f) == OK)
2369 rettv->vval.v_float = exp(f);
2370 else
2371 rettv->vval.v_float = 0.0;
2372}
2373#endif
2374
2375/*
2376 * "expand()" function
2377 */
2378 static void
2379f_expand(typval_T *argvars, typval_T *rettv)
2380{
2381 char_u *s;
2382 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2385 expand_T xpc;
2386 int error = FALSE;
2387 char_u *result;
2388
2389 rettv->v_type = VAR_STRING;
2390 if (argvars[1].v_type != VAR_UNKNOWN
2391 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002392 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002394 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002396 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 if (*s == '%' || *s == '#' || *s == '<')
2398 {
2399 ++emsg_off;
2400 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2401 --emsg_off;
2402 if (rettv->v_type == VAR_LIST)
2403 {
2404 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2405 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002406 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002407 }
2408 else
2409 rettv->vval.v_string = result;
2410 }
2411 else
2412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002413 // When the optional second argument is non-zero, don't remove matches
2414 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002416 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 options |= WILD_KEEP_ALL;
2418 if (!error)
2419 {
2420 ExpandInit(&xpc);
2421 xpc.xp_context = EXPAND_FILES;
2422 if (p_wic)
2423 options += WILD_ICASE;
2424 if (rettv->v_type == VAR_STRING)
2425 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2426 options, WILD_ALL);
2427 else if (rettv_list_alloc(rettv) != FAIL)
2428 {
2429 int i;
2430
2431 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2432 for (i = 0; i < xpc.xp_numfiles; i++)
2433 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2434 ExpandCleanup(&xpc);
2435 }
2436 }
2437 else
2438 rettv->vval.v_string = NULL;
2439 }
2440}
2441
2442/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002443 * "expandcmd()" function
2444 * Expand all the special characters in a command string.
2445 */
2446 static void
2447f_expandcmd(typval_T *argvars, typval_T *rettv)
2448{
2449 exarg_T eap;
2450 char_u *cmdstr;
2451 char *errormsg = NULL;
2452
2453 rettv->v_type = VAR_STRING;
2454 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2455
2456 memset(&eap, 0, sizeof(eap));
2457 eap.cmd = cmdstr;
2458 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002459 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002460 eap.usefilter = FALSE;
2461 eap.nextcmd = NULL;
2462 eap.cmdidx = CMD_USER;
2463
2464 expand_filename(&eap, &cmdstr, &errormsg);
2465 if (errormsg != NULL && *errormsg != NUL)
2466 emsg(errormsg);
2467
2468 rettv->vval.v_string = cmdstr;
2469}
2470
2471/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002472 * "feedkeys()" function
2473 */
2474 static void
2475f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2476{
2477 int remap = TRUE;
2478 int insert = FALSE;
2479 char_u *keys, *flags;
2480 char_u nbuf[NUMBUFLEN];
2481 int typed = FALSE;
2482 int execute = FALSE;
2483 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002484 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485 char_u *keys_esc;
2486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002487 // This is not allowed in the sandbox. If the commands would still be
2488 // executed in the sandbox it would be OK, but it probably happens later,
2489 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002490 if (check_secure())
2491 return;
2492
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002493 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002494
2495 if (argvars[1].v_type != VAR_UNKNOWN)
2496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002497 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002498 for ( ; *flags != NUL; ++flags)
2499 {
2500 switch (*flags)
2501 {
2502 case 'n': remap = FALSE; break;
2503 case 'm': remap = TRUE; break;
2504 case 't': typed = TRUE; break;
2505 case 'i': insert = TRUE; break;
2506 case 'x': execute = TRUE; break;
2507 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002508 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509 }
2510 }
2511 }
2512
2513 if (*keys != NUL || execute)
2514 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002515 // Need to escape K_SPECIAL and CSI before putting the string in the
2516 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517 keys_esc = vim_strsave_escape_csi(keys);
2518 if (keys_esc != NULL)
2519 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002520 if (lowlevel)
2521 {
2522#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002523 int idx;
2524 int len = (int)STRLEN(keys);
2525
2526 for (idx = 0; idx < len; ++idx)
2527 {
2528 // if a CTRL-C was typed, set got_int, similar to what
2529 // happens in fill_input_buf()
2530 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2531 got_int = TRUE;
2532 add_to_input_buf(keys + idx, 1);
2533 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002534#else
2535 emsg(_("E980: lowlevel input not supported"));
2536#endif
2537 }
2538 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002539 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002540 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002542 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002543#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002544 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002545#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002546 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002547 typebuf_was_filled = TRUE;
2548 }
2549 vim_free(keys_esc);
2550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 if (execute)
2552 {
2553 int save_msg_scroll = msg_scroll;
2554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002555 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 msg_scroll = FALSE;
2557
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002558 if (!dangerous)
2559 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002560 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002561 if (!dangerous)
2562 --ex_normal_busy;
2563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002564 msg_scroll |= save_msg_scroll;
2565 }
2566 }
2567 }
2568}
2569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002570#ifdef FEAT_FLOAT
2571/*
2572 * "float2nr({float})" function
2573 */
2574 static void
2575f_float2nr(typval_T *argvars, typval_T *rettv)
2576{
2577 float_T f = 0.0;
2578
2579 if (get_float_arg(argvars, &f) == OK)
2580 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002581 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002582 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002583 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002584 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002585 else
2586 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002587 }
2588}
2589
2590/*
2591 * "floor({float})" function
2592 */
2593 static void
2594f_floor(typval_T *argvars, typval_T *rettv)
2595{
2596 float_T f = 0.0;
2597
2598 rettv->v_type = VAR_FLOAT;
2599 if (get_float_arg(argvars, &f) == OK)
2600 rettv->vval.v_float = floor(f);
2601 else
2602 rettv->vval.v_float = 0.0;
2603}
2604
2605/*
2606 * "fmod()" function
2607 */
2608 static void
2609f_fmod(typval_T *argvars, typval_T *rettv)
2610{
2611 float_T fx = 0.0, fy = 0.0;
2612
2613 rettv->v_type = VAR_FLOAT;
2614 if (get_float_arg(argvars, &fx) == OK
2615 && get_float_arg(&argvars[1], &fy) == OK)
2616 rettv->vval.v_float = fmod(fx, fy);
2617 else
2618 rettv->vval.v_float = 0.0;
2619}
2620#endif
2621
2622/*
2623 * "fnameescape({string})" function
2624 */
2625 static void
2626f_fnameescape(typval_T *argvars, typval_T *rettv)
2627{
2628 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002629 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 rettv->v_type = VAR_STRING;
2631}
2632
2633/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 * "foreground()" function
2635 */
2636 static void
2637f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2638{
2639#ifdef FEAT_GUI
2640 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002641 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002643 return;
2644 }
2645#endif
2646#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648#endif
2649}
2650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002652common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653{
2654 char_u *s;
2655 char_u *name;
2656 int use_string = FALSE;
2657 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002658 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002659 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660
2661 if (argvars[0].v_type == VAR_FUNC)
2662 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002663 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 s = argvars[0].vval.v_string;
2665 }
2666 else if (argvars[0].v_type == VAR_PARTIAL
2667 && argvars[0].vval.v_partial != NULL)
2668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002669 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002671 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 }
2673 else
2674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002675 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 use_string = TRUE;
2678 }
2679
Bram Moolenaar843b8842016-08-21 14:36:15 +02002680 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002681 {
2682 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002683 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002684 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2685 if (*name != NUL)
2686 s = NULL;
2687 }
2688
Bram Moolenaar843b8842016-08-21 14:36:15 +02002689 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2690 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002691 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002692 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002693 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002694 ? find_func(trans_name, is_global, NULL) == NULL
2695 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002696 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 else
2698 {
2699 int dict_idx = 0;
2700 int arg_idx = 0;
2701 list_T *list = NULL;
2702
2703 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2704 {
2705 char sid_buf[25];
2706 int off = *s == 's' ? 2 : 5;
2707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002708 // Expand s: and <SID> into <SNR>nr_, so that the function can
2709 // also be called from another script. Using trans_function_name()
2710 // would also work, but some plugins depend on the name being
2711 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002712 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002713 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002714 if (name != NULL)
2715 {
2716 STRCPY(name, sid_buf);
2717 STRCAT(name, s + off);
2718 }
2719 }
2720 else
2721 name = vim_strsave(s);
2722
2723 if (argvars[1].v_type != VAR_UNKNOWN)
2724 {
2725 if (argvars[2].v_type != VAR_UNKNOWN)
2726 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002727 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 arg_idx = 1;
2729 dict_idx = 2;
2730 }
2731 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002732 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 dict_idx = 1;
2734 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002736 arg_idx = 1;
2737 if (dict_idx > 0)
2738 {
2739 if (argvars[dict_idx].v_type != VAR_DICT)
2740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002741 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002743 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002744 }
2745 if (argvars[dict_idx].vval.v_dict == NULL)
2746 dict_idx = 0;
2747 }
2748 if (arg_idx > 0)
2749 {
2750 if (argvars[arg_idx].v_type != VAR_LIST)
2751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002752 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002754 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002755 }
2756 list = argvars[arg_idx].vval.v_list;
2757 if (list == NULL || list->lv_len == 0)
2758 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002759 else if (list->lv_len > MAX_FUNC_ARGS)
2760 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002761 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002762 vim_free(name);
2763 goto theend;
2764 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 }
2766 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002767 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002769 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002771 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 if (pt == NULL)
2773 vim_free(name);
2774 else
2775 {
2776 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2777 {
2778 listitem_T *li;
2779 int i = 0;
2780 int arg_len = 0;
2781 int lv_len = 0;
2782
2783 if (arg_pt != NULL)
2784 arg_len = arg_pt->pt_argc;
2785 if (list != NULL)
2786 lv_len = list->lv_len;
2787 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002788 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 if (pt->pt_argv == NULL)
2790 {
2791 vim_free(pt);
2792 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002793 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002795 for (i = 0; i < arg_len; i++)
2796 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2797 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002798 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002799 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002800 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002801 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 }
2804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002805 // For "function(dict.func, [], dict)" and "func" is a partial
2806 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 if (dict_idx > 0)
2808 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002809 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2811 ++pt->pt_dict->dv_refcount;
2812 }
2813 else if (arg_pt != NULL)
2814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002815 // If the dict was bound automatically the result is also
2816 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 pt->pt_dict = arg_pt->pt_dict;
2818 pt->pt_auto = arg_pt->pt_auto;
2819 if (pt->pt_dict != NULL)
2820 ++pt->pt_dict->dv_refcount;
2821 }
2822
2823 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002824 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2825 {
2826 pt->pt_func = arg_pt->pt_func;
2827 func_ptr_ref(pt->pt_func);
2828 vim_free(name);
2829 }
2830 else if (is_funcref)
2831 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002832 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002833 func_ptr_ref(pt->pt_func);
2834 vim_free(name);
2835 }
2836 else
2837 {
2838 pt->pt_name = name;
2839 func_ref(name);
2840 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002841 }
2842 rettv->v_type = VAR_PARTIAL;
2843 rettv->vval.v_partial = pt;
2844 }
2845 else
2846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002847 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 rettv->v_type = VAR_FUNC;
2849 rettv->vval.v_string = name;
2850 func_ref(name);
2851 }
2852 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002853theend:
2854 vim_free(trans_name);
2855}
2856
2857/*
2858 * "funcref()" function
2859 */
2860 static void
2861f_funcref(typval_T *argvars, typval_T *rettv)
2862{
2863 common_function(argvars, rettv, TRUE);
2864}
2865
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002866 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002867ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002868{
2869 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2870 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002871 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002872}
2873
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002874/*
2875 * "function()" function
2876 */
2877 static void
2878f_function(typval_T *argvars, typval_T *rettv)
2879{
2880 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881}
2882
2883/*
2884 * "garbagecollect()" function
2885 */
2886 static void
2887f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2888{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002889 // This is postponed until we are back at the toplevel, because we may be
2890 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002891 want_garbage_collect = TRUE;
2892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002893 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 garbage_collect_at_exit = TRUE;
2895}
2896
2897/*
2898 * "get()" function
2899 */
2900 static void
2901f_get(typval_T *argvars, typval_T *rettv)
2902{
2903 listitem_T *li;
2904 list_T *l;
2905 dictitem_T *di;
2906 dict_T *d;
2907 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002908 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002909
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002910 if (argvars[0].v_type == VAR_BLOB)
2911 {
2912 int error = FALSE;
2913 int idx = tv_get_number_chk(&argvars[1], &error);
2914
2915 if (!error)
2916 {
2917 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002918 if (idx < 0)
2919 idx = blob_len(argvars[0].vval.v_blob) + idx;
2920 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2921 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002922 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002923 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002924 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002925 tv = rettv;
2926 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002927 }
2928 }
2929 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002930 {
2931 if ((l = argvars[0].vval.v_list) != NULL)
2932 {
2933 int error = FALSE;
2934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002935 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 if (!error && li != NULL)
2937 tv = &li->li_tv;
2938 }
2939 }
2940 else if (argvars[0].v_type == VAR_DICT)
2941 {
2942 if ((d = argvars[0].vval.v_dict) != NULL)
2943 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002944 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 if (di != NULL)
2946 tv = &di->di_tv;
2947 }
2948 }
2949 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2950 {
2951 partial_T *pt;
2952 partial_T fref_pt;
2953
2954 if (argvars[0].v_type == VAR_PARTIAL)
2955 pt = argvars[0].vval.v_partial;
2956 else
2957 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002958 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002959 fref_pt.pt_name = argvars[0].vval.v_string;
2960 pt = &fref_pt;
2961 }
2962
2963 if (pt != NULL)
2964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002965 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002966 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002967
2968 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
2969 {
2970 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002971 n = partial_name(pt);
2972 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 rettv->vval.v_string = NULL;
2974 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002975 {
2976 rettv->vval.v_string = vim_strsave(n);
2977 if (rettv->v_type == VAR_FUNC)
2978 func_ref(rettv->vval.v_string);
2979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002980 }
2981 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002982 {
2983 what_is_dict = TRUE;
2984 if (pt->pt_dict != NULL)
2985 rettv_dict_set(rettv, pt->pt_dict);
2986 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 else if (STRCMP(what, "args") == 0)
2988 {
2989 rettv->v_type = VAR_LIST;
2990 if (rettv_list_alloc(rettv) == OK)
2991 {
2992 int i;
2993
2994 for (i = 0; i < pt->pt_argc; ++i)
2995 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
2996 }
2997 }
2998 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002999 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003000
3001 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3002 // third argument
3003 if (!what_is_dict)
3004 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 }
3006 }
3007 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003008 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009
3010 if (tv == NULL)
3011 {
3012 if (argvars[2].v_type != VAR_UNKNOWN)
3013 copy_tv(&argvars[2], rettv);
3014 }
3015 else
3016 copy_tv(tv, rettv);
3017}
3018
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003019/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003020 * "getchangelist()" function
3021 */
3022 static void
3023f_getchangelist(typval_T *argvars, typval_T *rettv)
3024{
3025#ifdef FEAT_JUMPLIST
3026 buf_T *buf;
3027 int i;
3028 list_T *l;
3029 dict_T *d;
3030#endif
3031
3032 if (rettv_list_alloc(rettv) != OK)
3033 return;
3034
3035#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003036 if (argvars[0].v_type == VAR_UNKNOWN)
3037 buf = curbuf;
3038 else
3039 {
3040 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3041 ++emsg_off;
3042 buf = tv_get_buf(&argvars[0], FALSE);
3043 --emsg_off;
3044 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003045 if (buf == NULL)
3046 return;
3047
3048 l = list_alloc();
3049 if (l == NULL)
3050 return;
3051
3052 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3053 return;
3054 /*
3055 * The current window change list index tracks only the position in the
3056 * current buffer change list. For other buffers, use the change list
3057 * length as the current index.
3058 */
3059 list_append_number(rettv->vval.v_list,
3060 (varnumber_T)((buf == curwin->w_buffer)
3061 ? curwin->w_changelistidx : buf->b_changelistlen));
3062
3063 for (i = 0; i < buf->b_changelistlen; ++i)
3064 {
3065 if (buf->b_changelist[i].lnum == 0)
3066 continue;
3067 if ((d = dict_alloc()) == NULL)
3068 return;
3069 if (list_append_dict(l, d) == FAIL)
3070 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003071 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3072 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003073 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003074 }
3075#endif
3076}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077
3078/*
3079 * "getcharsearch()" function
3080 */
3081 static void
3082f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3083{
3084 if (rettv_dict_alloc(rettv) != FAIL)
3085 {
3086 dict_T *dict = rettv->vval.v_dict;
3087
Bram Moolenaare0be1672018-07-08 16:50:37 +02003088 dict_add_string(dict, "char", last_csearch());
3089 dict_add_number(dict, "forward", last_csearch_forward());
3090 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 }
3092}
3093
3094/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003095 * "getenv()" function
3096 */
3097 static void
3098f_getenv(typval_T *argvars, typval_T *rettv)
3099{
3100 int mustfree = FALSE;
3101 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3102
3103 if (p == NULL)
3104 {
3105 rettv->v_type = VAR_SPECIAL;
3106 rettv->vval.v_number = VVAL_NULL;
3107 return;
3108 }
3109 if (!mustfree)
3110 p = vim_strsave(p);
3111 rettv->vval.v_string = p;
3112 rettv->v_type = VAR_STRING;
3113}
3114
3115/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 * "getfontname()" function
3117 */
3118 static void
3119f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3120{
3121 rettv->v_type = VAR_STRING;
3122 rettv->vval.v_string = NULL;
3123#ifdef FEAT_GUI
3124 if (gui.in_use)
3125 {
3126 GuiFont font;
3127 char_u *name = NULL;
3128
3129 if (argvars[0].v_type == VAR_UNKNOWN)
3130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003131 // Get the "Normal" font. Either the name saved by
3132 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 font = gui.norm_font;
3134 name = hl_get_font_name();
3135 }
3136 else
3137 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003138 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003140 return;
3141 font = gui_mch_get_font(name, FALSE);
3142 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003143 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 }
3145 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3146 if (argvars[0].v_type != VAR_UNKNOWN)
3147 gui_mch_free_font(font);
3148 }
3149#endif
3150}
3151
3152/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003153 * "getjumplist()" function
3154 */
3155 static void
3156f_getjumplist(typval_T *argvars, typval_T *rettv)
3157{
3158#ifdef FEAT_JUMPLIST
3159 win_T *wp;
3160 int i;
3161 list_T *l;
3162 dict_T *d;
3163#endif
3164
3165 if (rettv_list_alloc(rettv) != OK)
3166 return;
3167
3168#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003169 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003170 if (wp == NULL)
3171 return;
3172
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003173 cleanup_jumplist(wp, TRUE);
3174
Bram Moolenaar4f505882018-02-10 21:06:32 +01003175 l = list_alloc();
3176 if (l == NULL)
3177 return;
3178
3179 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3180 return;
3181 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3182
3183 for (i = 0; i < wp->w_jumplistlen; ++i)
3184 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003185 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3186 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003187 if ((d = dict_alloc()) == NULL)
3188 return;
3189 if (list_append_dict(l, d) == FAIL)
3190 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003191 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3192 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003193 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003194 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003195 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003196 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003197 }
3198#endif
3199}
3200
3201/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003202 * "getpid()" function
3203 */
3204 static void
3205f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3206{
3207 rettv->vval.v_number = mch_get_pid();
3208}
3209
3210 static void
3211getpos_both(
3212 typval_T *argvars,
3213 typval_T *rettv,
3214 int getcurpos)
3215{
3216 pos_T *fp;
3217 list_T *l;
3218 int fnum = -1;
3219
3220 if (rettv_list_alloc(rettv) == OK)
3221 {
3222 l = rettv->vval.v_list;
3223 if (getcurpos)
3224 fp = &curwin->w_cursor;
3225 else
3226 fp = var2fpos(&argvars[0], TRUE, &fnum);
3227 if (fnum != -1)
3228 list_append_number(l, (varnumber_T)fnum);
3229 else
3230 list_append_number(l, (varnumber_T)0);
3231 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3232 : (varnumber_T)0);
3233 list_append_number(l, (fp != NULL)
3234 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3235 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003236 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 (varnumber_T)0);
3238 if (getcurpos)
3239 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003240 int save_set_curswant = curwin->w_set_curswant;
3241 colnr_T save_curswant = curwin->w_curswant;
3242 colnr_T save_virtcol = curwin->w_virtcol;
3243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 update_curswant();
3245 list_append_number(l, curwin->w_curswant == MAXCOL ?
3246 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003247
3248 // Do not change "curswant", as it is unexpected that a get
3249 // function has a side effect.
3250 if (save_set_curswant)
3251 {
3252 curwin->w_set_curswant = save_set_curswant;
3253 curwin->w_curswant = save_curswant;
3254 curwin->w_virtcol = save_virtcol;
3255 curwin->w_valid &= ~VALID_VIRTCOL;
3256 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 }
3258 }
3259 else
3260 rettv->vval.v_number = FALSE;
3261}
3262
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263/*
3264 * "getcurpos()" function
3265 */
3266 static void
3267f_getcurpos(typval_T *argvars, typval_T *rettv)
3268{
3269 getpos_both(argvars, rettv, TRUE);
3270}
3271
3272/*
3273 * "getpos(string)" function
3274 */
3275 static void
3276f_getpos(typval_T *argvars, typval_T *rettv)
3277{
3278 getpos_both(argvars, rettv, FALSE);
3279}
3280
3281/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 * "getreg()" function
3283 */
3284 static void
3285f_getreg(typval_T *argvars, typval_T *rettv)
3286{
3287 char_u *strregname;
3288 int regname;
3289 int arg2 = FALSE;
3290 int return_list = FALSE;
3291 int error = FALSE;
3292
3293 if (argvars[0].v_type != VAR_UNKNOWN)
3294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003295 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 error = strregname == NULL;
3297 if (argvars[1].v_type != VAR_UNKNOWN)
3298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003299 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003301 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 }
3303 }
3304 else
3305 strregname = get_vim_var_str(VV_REG);
3306
3307 if (error)
3308 return;
3309
3310 regname = (strregname == NULL ? '"' : *strregname);
3311 if (regname == 0)
3312 regname = '"';
3313
3314 if (return_list)
3315 {
3316 rettv->v_type = VAR_LIST;
3317 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3318 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3319 if (rettv->vval.v_list == NULL)
3320 (void)rettv_list_alloc(rettv);
3321 else
3322 ++rettv->vval.v_list->lv_refcount;
3323 }
3324 else
3325 {
3326 rettv->v_type = VAR_STRING;
3327 rettv->vval.v_string = get_reg_contents(regname,
3328 arg2 ? GREG_EXPR_SRC : 0);
3329 }
3330}
3331
3332/*
3333 * "getregtype()" function
3334 */
3335 static void
3336f_getregtype(typval_T *argvars, typval_T *rettv)
3337{
3338 char_u *strregname;
3339 int regname;
3340 char_u buf[NUMBUFLEN + 2];
3341 long reglen = 0;
3342
3343 if (argvars[0].v_type != VAR_UNKNOWN)
3344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003345 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003346 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 {
3348 rettv->v_type = VAR_STRING;
3349 rettv->vval.v_string = NULL;
3350 return;
3351 }
3352 }
3353 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003354 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 strregname = get_vim_var_str(VV_REG);
3356
3357 regname = (strregname == NULL ? '"' : *strregname);
3358 if (regname == 0)
3359 regname = '"';
3360
3361 buf[0] = NUL;
3362 buf[1] = NUL;
3363 switch (get_reg_type(regname, &reglen))
3364 {
3365 case MLINE: buf[0] = 'V'; break;
3366 case MCHAR: buf[0] = 'v'; break;
3367 case MBLOCK:
3368 buf[0] = Ctrl_V;
3369 sprintf((char *)buf + 1, "%ld", reglen + 1);
3370 break;
3371 }
3372 rettv->v_type = VAR_STRING;
3373 rettv->vval.v_string = vim_strsave(buf);
3374}
3375
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003376/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003377 * "gettagstack()" function
3378 */
3379 static void
3380f_gettagstack(typval_T *argvars, typval_T *rettv)
3381{
3382 win_T *wp = curwin; // default is current window
3383
3384 if (rettv_dict_alloc(rettv) != OK)
3385 return;
3386
3387 if (argvars[0].v_type != VAR_UNKNOWN)
3388 {
3389 wp = find_win_by_nr_or_id(&argvars[0]);
3390 if (wp == NULL)
3391 return;
3392 }
3393
3394 get_tagstack(wp, rettv->vval.v_dict);
3395}
3396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003397// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398#include "version.h"
3399
3400/*
3401 * "has()" function
3402 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003403 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404f_has(typval_T *argvars, typval_T *rettv)
3405{
3406 int i;
3407 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003408 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003410 typedef struct {
3411 char *name;
3412 short present;
3413 } has_item_T;
3414 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003415 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003416 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003418 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003419#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003420 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003421#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003422 },
3423 {"arp",
3424#if defined(AMIGA) && defined(FEAT_ARP)
3425 1
3426#else
3427 0
3428#endif
3429 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003430 {"haiku",
3431#ifdef __HAIKU__
3432 1
3433#else
3434 0
3435#endif
3436 },
3437 {"bsd",
3438#if defined(BSD) && !defined(MACOS_X)
3439 1
3440#else
3441 0
3442#endif
3443 },
3444 {"hpux",
3445#ifdef hpux
3446 1
3447#else
3448 0
3449#endif
3450 },
3451 {"linux",
3452#ifdef __linux__
3453 1
3454#else
3455 0
3456#endif
3457 },
3458 {"mac", // Mac OS X (and, once, Mac OS Classic)
3459#ifdef MACOS_X
3460 1
3461#else
3462 0
3463#endif
3464 },
3465 {"osx", // Mac OS X
3466#ifdef MACOS_X
3467 1
3468#else
3469 0
3470#endif
3471 },
3472 {"macunix", // Mac OS X, with the darwin feature
3473#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3474 1
3475#else
3476 0
3477#endif
3478 },
3479 {"osxdarwin", // synonym for macunix
3480#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3481 1
3482#else
3483 0
3484#endif
3485 },
3486 {"qnx",
3487#ifdef __QNX__
3488 1
3489#else
3490 0
3491#endif
3492 },
3493 {"sun",
3494#ifdef SUN_SYSTEM
3495 1
3496#else
3497 0
3498#endif
3499 },
3500 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003501#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003502 1
3503#else
3504 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003506 },
3507 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003508#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003509 1
3510#else
3511 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003513 },
3514 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003515#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003516 1
3517#else
3518 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003520 },
3521 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003522#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003523 1
3524#else
3525 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003527 },
3528 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003529#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003530 1
3531#else
3532 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003534 },
3535 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003537 1
3538#else
3539 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003541 },
3542 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003544 1
3545#else
3546 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003548 },
3549 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003551 1
3552#else
3553 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003555 },
3556 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003558 1
3559#else
3560 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003562 },
3563 {"autocmd", 1},
3564 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003565#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003566 1
3567#else
3568 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003569#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003570 },
3571 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003572#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003573 1
3574#else
3575 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 },
3578 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003579#ifdef FEAT_BEVAL_GUI
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 {"balloon_multiline",
3586#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3587 // MS-Windows requires runtime check, see below
3588 1
3589#else
3590 0
3591#endif
3592 },
3593 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003594#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 1
3596#else
3597 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003598#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 },
3600 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
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 {"all_builtin_terms",
3608#if defined(ALL_BUILTIN_TCAPS)
3609 1
3610#else
3611 0
3612#endif
3613 },
3614 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003616 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003618 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003620 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003622 },
3623 {"byte_offset",
3624#ifdef FEAT_BYTEOFF
3625 1
3626#else
3627 0
3628#endif
3629 },
3630 {"channel",
3631#ifdef FEAT_JOB_CHANNEL
3632 1
3633#else
3634 0
3635#endif
3636 },
3637 {"cindent",
3638#ifdef FEAT_CINDENT
3639 1
3640#else
3641 0
3642#endif
3643 },
3644 {"clientserver",
3645#ifdef FEAT_CLIENTSERVER
3646 1
3647#else
3648 0
3649#endif
3650 },
3651 {"clipboard",
3652#ifdef FEAT_CLIPBOARD
3653 1
3654#else
3655 0
3656#endif
3657 },
3658 {"cmdline_compl", 1},
3659 {"cmdline_hist", 1},
3660 {"comments", 1},
3661 {"conceal",
3662#ifdef FEAT_CONCEAL
3663 1
3664#else
3665 0
3666#endif
3667 },
3668 {"cryptv",
3669#ifdef FEAT_CRYPT
3670 1
3671#else
3672 0
3673#endif
3674 },
3675 {"crypt-blowfish",
3676#ifdef FEAT_CRYPT
3677 1
3678#else
3679 0
3680#endif
3681 },
3682 {"crypt-blowfish2",
3683#ifdef FEAT_CRYPT
3684 1
3685#else
3686 0
3687#endif
3688 },
3689 {"cscope",
3690#ifdef FEAT_CSCOPE
3691 1
3692#else
3693 0
3694#endif
3695 },
3696 {"cursorbind", 1},
3697 {"cursorshape",
3698#ifdef CURSOR_SHAPE
3699 1
3700#else
3701 0
3702#endif
3703 },
3704 {"debug",
3705#ifdef DEBUG
3706 1
3707#else
3708 0
3709#endif
3710 },
3711 {"dialog_con",
3712#ifdef FEAT_CON_DIALOG
3713 1
3714#else
3715 0
3716#endif
3717 },
3718 {"dialog_gui",
3719#ifdef FEAT_GUI_DIALOG
3720 1
3721#else
3722 0
3723#endif
3724 },
3725 {"diff",
3726#ifdef FEAT_DIFF
3727 1
3728#else
3729 0
3730#endif
3731 },
3732 {"digraphs",
3733#ifdef FEAT_DIGRAPHS
3734 1
3735#else
3736 0
3737#endif
3738 },
3739 {"directx",
3740#ifdef FEAT_DIRECTX
3741 1
3742#else
3743 0
3744#endif
3745 },
3746 {"dnd",
3747#ifdef FEAT_DND
3748 1
3749#else
3750 0
3751#endif
3752 },
3753 {"emacs_tags",
3754#ifdef FEAT_EMACS_TAGS
3755 1
3756#else
3757 0
3758#endif
3759 },
3760 {"eval", 1}, // always present, of course!
3761 {"ex_extra", 1}, // graduated feature
3762 {"extra_search",
3763#ifdef FEAT_SEARCH_EXTRA
3764 1
3765#else
3766 0
3767#endif
3768 },
3769 {"file_in_path",
3770#ifdef FEAT_SEARCHPATH
3771 1
3772#else
3773 0
3774#endif
3775 },
3776 {"filterpipe",
3777#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3778 1
3779#else
3780 0
3781#endif
3782 },
3783 {"find_in_path",
3784#ifdef FEAT_FIND_ID
3785 1
3786#else
3787 0
3788#endif
3789 },
3790 {"float",
3791#ifdef FEAT_FLOAT
3792 1
3793#else
3794 0
3795#endif
3796 },
3797 {"folding",
3798#ifdef FEAT_FOLDING
3799 1
3800#else
3801 0
3802#endif
3803 },
3804 {"footer",
3805#ifdef FEAT_FOOTER
3806 1
3807#else
3808 0
3809#endif
3810 },
3811 {"fork",
3812#if !defined(USE_SYSTEM) && defined(UNIX)
3813 1
3814#else
3815 0
3816#endif
3817 },
3818 {"gettext",
3819#ifdef FEAT_GETTEXT
3820 1
3821#else
3822 0
3823#endif
3824 },
3825 {"gui",
3826#ifdef FEAT_GUI
3827 1
3828#else
3829 0
3830#endif
3831 },
3832 {"gui_neXtaw",
3833#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3834 1
3835#else
3836 0
3837#endif
3838 },
3839 {"gui_athena",
3840#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3841 1
3842#else
3843 0
3844#endif
3845 },
3846 {"gui_gtk",
3847#ifdef FEAT_GUI_GTK
3848 1
3849#else
3850 0
3851#endif
3852 },
3853 {"gui_gtk2",
3854#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3855 1
3856#else
3857 0
3858#endif
3859 },
3860 {"gui_gtk3",
3861#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3862 1
3863#else
3864 0
3865#endif
3866 },
3867 {"gui_gnome",
3868#ifdef FEAT_GUI_GNOME
3869 1
3870#else
3871 0
3872#endif
3873 },
3874 {"gui_haiku",
3875#ifdef FEAT_GUI_HAIKU
3876 1
3877#else
3878 0
3879#endif
3880 },
3881 {"gui_mac",
3882#ifdef FEAT_GUI_MAC
3883 1
3884#else
3885 0
3886#endif
3887 },
3888 {"gui_motif",
3889#ifdef FEAT_GUI_MOTIF
3890 1
3891#else
3892 0
3893#endif
3894 },
3895 {"gui_photon",
3896#ifdef FEAT_GUI_PHOTON
3897 1
3898#else
3899 0
3900#endif
3901 },
3902 {"gui_win32",
3903#ifdef FEAT_GUI_MSWIN
3904 1
3905#else
3906 0
3907#endif
3908 },
3909 {"iconv",
3910#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3911 1
3912#else
3913 0
3914#endif
3915 },
3916 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003917 {"ipv6",
3918#ifdef FEAT_IPV6
3919 1
3920#else
3921 0
3922#endif
3923 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003924 {"job",
3925#ifdef FEAT_JOB_CHANNEL
3926 1
3927#else
3928 0
3929#endif
3930 },
3931 {"jumplist",
3932#ifdef FEAT_JUMPLIST
3933 1
3934#else
3935 0
3936#endif
3937 },
3938 {"keymap",
3939#ifdef FEAT_KEYMAP
3940 1
3941#else
3942 0
3943#endif
3944 },
3945 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3946 {"langmap",
3947#ifdef FEAT_LANGMAP
3948 1
3949#else
3950 0
3951#endif
3952 },
3953 {"libcall",
3954#ifdef FEAT_LIBCALL
3955 1
3956#else
3957 0
3958#endif
3959 },
3960 {"linebreak",
3961#ifdef FEAT_LINEBREAK
3962 1
3963#else
3964 0
3965#endif
3966 },
3967 {"lispindent",
3968#ifdef FEAT_LISP
3969 1
3970#else
3971 0
3972#endif
3973 },
3974 {"listcmds", 1},
3975 {"localmap", 1},
3976 {"lua",
3977#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
3978 1
3979#else
3980 0
3981#endif
3982 },
3983 {"menu",
3984#ifdef FEAT_MENU
3985 1
3986#else
3987 0
3988#endif
3989 },
3990 {"mksession",
3991#ifdef FEAT_SESSION
3992 1
3993#else
3994 0
3995#endif
3996 },
3997 {"modify_fname", 1},
3998 {"mouse", 1},
3999 {"mouseshape",
4000#ifdef FEAT_MOUSESHAPE
4001 1
4002#else
4003 0
4004#endif
4005 },
4006 {"mouse_dec",
4007#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4008 1
4009#else
4010 0
4011#endif
4012 },
4013 {"mouse_gpm",
4014#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4015 1
4016#else
4017 0
4018#endif
4019 },
4020 {"mouse_jsbterm",
4021#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4022 1
4023#else
4024 0
4025#endif
4026 },
4027 {"mouse_netterm",
4028#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4029 1
4030#else
4031 0
4032#endif
4033 },
4034 {"mouse_pterm",
4035#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4036 1
4037#else
4038 0
4039#endif
4040 },
4041 {"mouse_sgr",
4042#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4043 1
4044#else
4045 0
4046#endif
4047 },
4048 {"mouse_sysmouse",
4049#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4050 1
4051#else
4052 0
4053#endif
4054 },
4055 {"mouse_urxvt",
4056#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4057 1
4058#else
4059 0
4060#endif
4061 },
4062 {"mouse_xterm",
4063#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4064 1
4065#else
4066 0
4067#endif
4068 },
4069 {"multi_byte", 1},
4070 {"multi_byte_ime",
4071#ifdef FEAT_MBYTE_IME
4072 1
4073#else
4074 0
4075#endif
4076 },
4077 {"multi_lang",
4078#ifdef FEAT_MULTI_LANG
4079 1
4080#else
4081 0
4082#endif
4083 },
4084 {"mzscheme",
4085#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4086 1
4087#else
4088 0
4089#endif
4090 },
4091 {"num64", 1},
4092 {"ole",
4093#ifdef FEAT_OLE
4094 1
4095#else
4096 0
4097#endif
4098 },
4099 {"packages",
4100#ifdef FEAT_EVAL
4101 1
4102#else
4103 0
4104#endif
4105 },
4106 {"path_extra",
4107#ifdef FEAT_PATH_EXTRA
4108 1
4109#else
4110 0
4111#endif
4112 },
4113 {"perl",
4114#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4115 1
4116#else
4117 0
4118#endif
4119 },
4120 {"persistent_undo",
4121#ifdef FEAT_PERSISTENT_UNDO
4122 1
4123#else
4124 0
4125#endif
4126 },
4127 {"python_compiled",
4128#if defined(FEAT_PYTHON)
4129 1
4130#else
4131 0
4132#endif
4133 },
4134 {"python_dynamic",
4135#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4136 1
4137#else
4138 0
4139#endif
4140 },
4141 {"python",
4142#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4143 1
4144#else
4145 0
4146#endif
4147 },
4148 {"pythonx",
4149#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4150 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4151 1
4152#else
4153 0
4154#endif
4155 },
4156 {"python3_compiled",
4157#if defined(FEAT_PYTHON3)
4158 1
4159#else
4160 0
4161#endif
4162 },
4163 {"python3_dynamic",
4164#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4165 1
4166#else
4167 0
4168#endif
4169 },
4170 {"python3",
4171#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4172 1
4173#else
4174 0
4175#endif
4176 },
4177 {"popupwin",
4178#ifdef FEAT_PROP_POPUP
4179 1
4180#else
4181 0
4182#endif
4183 },
4184 {"postscript",
4185#ifdef FEAT_POSTSCRIPT
4186 1
4187#else
4188 0
4189#endif
4190 },
4191 {"printer",
4192#ifdef FEAT_PRINTER
4193 1
4194#else
4195 0
4196#endif
4197 },
4198 {"profile",
4199#ifdef FEAT_PROFILE
4200 1
4201#else
4202 0
4203#endif
4204 },
4205 {"reltime",
4206#ifdef FEAT_RELTIME
4207 1
4208#else
4209 0
4210#endif
4211 },
4212 {"quickfix",
4213#ifdef FEAT_QUICKFIX
4214 1
4215#else
4216 0
4217#endif
4218 },
4219 {"rightleft",
4220#ifdef FEAT_RIGHTLEFT
4221 1
4222#else
4223 0
4224#endif
4225 },
4226 {"ruby",
4227#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"scrollbind", 1},
4234 {"showcmd",
4235#ifdef FEAT_CMDL_INFO
4236 1
4237#else
4238 0
4239#endif
4240 },
4241 {"cmdline_info",
4242#ifdef FEAT_CMDL_INFO
4243 1
4244#else
4245 0
4246#endif
4247 },
4248 {"signs",
4249#ifdef FEAT_SIGNS
4250 1
4251#else
4252 0
4253#endif
4254 },
4255 {"smartindent",
4256#ifdef FEAT_SMARTINDENT
4257 1
4258#else
4259 0
4260#endif
4261 },
4262 {"startuptime",
4263#ifdef STARTUPTIME
4264 1
4265#else
4266 0
4267#endif
4268 },
4269 {"statusline",
4270#ifdef FEAT_STL_OPT
4271 1
4272#else
4273 0
4274#endif
4275 },
4276 {"netbeans_intg",
4277#ifdef FEAT_NETBEANS_INTG
4278 1
4279#else
4280 0
4281#endif
4282 },
4283 {"sound",
4284#ifdef FEAT_SOUND
4285 1
4286#else
4287 0
4288#endif
4289 },
4290 {"spell",
4291#ifdef FEAT_SPELL
4292 1
4293#else
4294 0
4295#endif
4296 },
4297 {"syntax",
4298#ifdef FEAT_SYN_HL
4299 1
4300#else
4301 0
4302#endif
4303 },
4304 {"system",
4305#if defined(USE_SYSTEM) || !defined(UNIX)
4306 1
4307#else
4308 0
4309#endif
4310 },
4311 {"tag_binary",
4312#ifdef FEAT_TAG_BINS
4313 1
4314#else
4315 0
4316#endif
4317 },
4318 {"tcl",
4319#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4320 1
4321#else
4322 0
4323#endif
4324 },
4325 {"termguicolors",
4326#ifdef FEAT_TERMGUICOLORS
4327 1
4328#else
4329 0
4330#endif
4331 },
4332 {"terminal",
4333#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4334 1
4335#else
4336 0
4337#endif
4338 },
4339 {"terminfo",
4340#ifdef TERMINFO
4341 1
4342#else
4343 0
4344#endif
4345 },
4346 {"termresponse",
4347#ifdef FEAT_TERMRESPONSE
4348 1
4349#else
4350 0
4351#endif
4352 },
4353 {"textobjects",
4354#ifdef FEAT_TEXTOBJ
4355 1
4356#else
4357 0
4358#endif
4359 },
4360 {"textprop",
4361#ifdef FEAT_PROP_POPUP
4362 1
4363#else
4364 0
4365#endif
4366 },
4367 {"tgetent",
4368#ifdef HAVE_TGETENT
4369 1
4370#else
4371 0
4372#endif
4373 },
4374 {"timers",
4375#ifdef FEAT_TIMERS
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"title",
4382#ifdef FEAT_TITLE
4383 1
4384#else
4385 0
4386#endif
4387 },
4388 {"toolbar",
4389#ifdef FEAT_TOOLBAR
4390 1
4391#else
4392 0
4393#endif
4394 },
4395 {"unnamedplus",
4396#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"user-commands", 1}, // was accidentally included in 5.4
4403 {"user_commands", 1},
4404 {"vartabs",
4405#ifdef FEAT_VARTABS
4406 1
4407#else
4408 0
4409#endif
4410 },
4411 {"vertsplit", 1},
4412 {"viminfo",
4413#ifdef FEAT_VIMINFO
4414 1
4415#else
4416 0
4417#endif
4418 },
4419 {"vimscript-1", 1},
4420 {"vimscript-2", 1},
4421 {"vimscript-3", 1},
4422 {"vimscript-4", 1},
4423 {"virtualedit", 1},
4424 {"visual", 1},
4425 {"visualextra", 1},
4426 {"vreplace", 1},
4427 {"vtp",
4428#ifdef FEAT_VTP
4429 1
4430#else
4431 0
4432#endif
4433 },
4434 {"wildignore",
4435#ifdef FEAT_WILDIGN
4436 1
4437#else
4438 0
4439#endif
4440 },
4441 {"wildmenu",
4442#ifdef FEAT_WILDMENU
4443 1
4444#else
4445 0
4446#endif
4447 },
4448 {"windows", 1},
4449 {"winaltkeys",
4450#ifdef FEAT_WAK
4451 1
4452#else
4453 0
4454#endif
4455 },
4456 {"writebackup",
4457#ifdef FEAT_WRITEBACKUP
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"xim",
4464#ifdef FEAT_XIM
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"xfontset",
4471#ifdef FEAT_XFONTSET
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"xpm",
4478#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"xpm_w32", // for backward compatibility
4485#ifdef FEAT_XPM_W32
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004492#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004493 1
4494#else
4495 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004496#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004497 },
4498 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004500 1
4501#else
4502 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004503#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004504 },
4505 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004506#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004507 1
4508#else
4509 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004511 },
4512 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004514 1
4515#else
4516 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004517#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004518 },
4519 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004521 1
4522#else
4523 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004524#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004525 },
4526 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527 };
4528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004529 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 for (i = 0; has_list[i].name != NULL; ++i)
4531 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 x = TRUE;
4534 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004535 break;
4536 }
4537
Bram Moolenaar79296512020-03-22 16:17:14 +01004538 // features also in has_list[] but sometimes enabled at runtime
4539 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004541 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004542 {
4543 // intentionally empty
4544 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004545#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 else if (STRICMP(name, "balloon_multiline") == 0)
4547 n = multiline_balloon_available();
4548#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004549#ifdef VIMDLL
4550 else if (STRICMP(name, "filterpipe") == 0)
4551 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552#endif
4553#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4554 else if (STRICMP(name, "iconv") == 0)
4555 n = iconv_enabled(FALSE);
4556#endif
4557#ifdef DYNAMIC_LUA
4558 else if (STRICMP(name, "lua") == 0)
4559 n = lua_enabled(FALSE);
4560#endif
4561#ifdef DYNAMIC_MZSCHEME
4562 else if (STRICMP(name, "mzscheme") == 0)
4563 n = mzscheme_enabled(FALSE);
4564#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004565#ifdef DYNAMIC_PERL
4566 else if (STRICMP(name, "perl") == 0)
4567 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#ifdef DYNAMIC_PYTHON
4570 else if (STRICMP(name, "python") == 0)
4571 n = python_enabled(FALSE);
4572#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573#ifdef DYNAMIC_PYTHON3
4574 else if (STRICMP(name, "python3") == 0)
4575 n = python3_enabled(FALSE);
4576#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004577#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4578 else if (STRICMP(name, "pythonx") == 0)
4579 {
4580# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4581 if (p_pyx == 0)
4582 n = python3_enabled(FALSE) || python_enabled(FALSE);
4583 else if (p_pyx == 3)
4584 n = python3_enabled(FALSE);
4585 else if (p_pyx == 2)
4586 n = python_enabled(FALSE);
4587# elif defined(DYNAMIC_PYTHON)
4588 n = python_enabled(FALSE);
4589# elif defined(DYNAMIC_PYTHON3)
4590 n = python3_enabled(FALSE);
4591# endif
4592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004593#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004594#ifdef DYNAMIC_RUBY
4595 else if (STRICMP(name, "ruby") == 0)
4596 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004598#ifdef DYNAMIC_TCL
4599 else if (STRICMP(name, "tcl") == 0)
4600 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004601#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004602#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004603 else if (STRICMP(name, "terminal") == 0)
4604 n = terminal_enabled();
4605#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606 }
4607
Bram Moolenaar79296512020-03-22 16:17:14 +01004608 // features not in has_list[]
4609 if (x == FALSE)
4610 {
4611 if (STRNICMP(name, "patch", 5) == 0)
4612 {
4613 x = TRUE;
4614 if (name[5] == '-'
4615 && STRLEN(name) >= 11
4616 && vim_isdigit(name[6])
4617 && vim_isdigit(name[8])
4618 && vim_isdigit(name[10]))
4619 {
4620 int major = atoi((char *)name + 6);
4621 int minor = atoi((char *)name + 8);
4622
4623 // Expect "patch-9.9.01234".
4624 n = (major < VIM_VERSION_MAJOR
4625 || (major == VIM_VERSION_MAJOR
4626 && (minor < VIM_VERSION_MINOR
4627 || (minor == VIM_VERSION_MINOR
4628 && has_patch(atoi((char *)name + 10))))));
4629 }
4630 else
4631 n = has_patch(atoi((char *)name + 5));
4632 }
4633 else if (STRICMP(name, "vim_starting") == 0)
4634 {
4635 x = TRUE;
4636 n = (starting != 0);
4637 }
4638 else if (STRICMP(name, "ttyin") == 0)
4639 {
4640 x = TRUE;
4641 n = mch_input_isatty();
4642 }
4643 else if (STRICMP(name, "ttyout") == 0)
4644 {
4645 x = TRUE;
4646 n = stdout_isatty;
4647 }
4648 else if (STRICMP(name, "multi_byte_encoding") == 0)
4649 {
4650 x = TRUE;
4651 n = has_mbyte;
4652 }
4653 else if (STRICMP(name, "gui_running") == 0)
4654 {
4655 x = TRUE;
4656#ifdef FEAT_GUI
4657 n = (gui.in_use || gui.starting);
4658#endif
4659 }
4660 else if (STRICMP(name, "browse") == 0)
4661 {
4662 x = TRUE;
4663#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4664 n = gui.in_use; // gui_mch_browse() works when GUI is running
4665#endif
4666 }
4667 else if (STRICMP(name, "syntax_items") == 0)
4668 {
4669 x = TRUE;
4670#ifdef FEAT_SYN_HL
4671 n = syntax_present(curwin);
4672#endif
4673 }
4674 else if (STRICMP(name, "vcon") == 0)
4675 {
4676 x = TRUE;
4677#ifdef FEAT_VTP
4678 n = is_term_win32() && has_vtp_working();
4679#endif
4680 }
4681 else if (STRICMP(name, "netbeans_enabled") == 0)
4682 {
4683 x = TRUE;
4684#ifdef FEAT_NETBEANS_INTG
4685 n = netbeans_active();
4686#endif
4687 }
4688 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4689 {
4690 x = TRUE;
4691#ifdef FEAT_MOUSE_GPM
4692 n = gpm_enabled();
4693#endif
4694 }
4695 else if (STRICMP(name, "conpty") == 0)
4696 {
4697 x = TRUE;
4698#if defined(FEAT_TERMINAL) && defined(MSWIN)
4699 n = use_conpty();
4700#endif
4701 }
4702 else if (STRICMP(name, "clipboard_working") == 0)
4703 {
4704 x = TRUE;
4705#ifdef FEAT_CLIPBOARD
4706 n = clip_star.available;
4707#endif
4708 }
4709 }
4710
4711 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4712 // return whether feature could ever be enabled
4713 rettv->vval.v_number = x;
4714 else
4715 // return whether feature is enabled
4716 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717}
4718
4719/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004720 * "haslocaldir()" function
4721 */
4722 static void
4723f_haslocaldir(typval_T *argvars, typval_T *rettv)
4724{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004725 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 win_T *wp = NULL;
4727
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004728 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4729
4730 // Check for window-local and tab-local directories
4731 if (wp != NULL && wp->w_localdir != NULL)
4732 rettv->vval.v_number = 1;
4733 else if (tp != NULL && tp->tp_localdir != NULL)
4734 rettv->vval.v_number = 2;
4735 else
4736 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737}
4738
4739/*
4740 * "hasmapto()" function
4741 */
4742 static void
4743f_hasmapto(typval_T *argvars, typval_T *rettv)
4744{
4745 char_u *name;
4746 char_u *mode;
4747 char_u buf[NUMBUFLEN];
4748 int abbr = FALSE;
4749
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004750 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004751 if (argvars[1].v_type == VAR_UNKNOWN)
4752 mode = (char_u *)"nvo";
4753 else
4754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004755 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004757 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004758 }
4759
4760 if (map_to_exists(name, mode, abbr))
4761 rettv->vval.v_number = TRUE;
4762 else
4763 rettv->vval.v_number = FALSE;
4764}
4765
4766/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004767 * "highlightID(name)" function
4768 */
4769 static void
4770f_hlID(typval_T *argvars, typval_T *rettv)
4771{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004772 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773}
4774
4775/*
4776 * "highlight_exists()" function
4777 */
4778 static void
4779f_hlexists(typval_T *argvars, typval_T *rettv)
4780{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004782}
4783
4784/*
4785 * "hostname()" function
4786 */
4787 static void
4788f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4789{
4790 char_u hostname[256];
4791
4792 mch_get_host_name(hostname, 256);
4793 rettv->v_type = VAR_STRING;
4794 rettv->vval.v_string = vim_strsave(hostname);
4795}
4796
4797/*
4798 * iconv() function
4799 */
4800 static void
4801f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4802{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004803 char_u buf1[NUMBUFLEN];
4804 char_u buf2[NUMBUFLEN];
4805 char_u *from, *to, *str;
4806 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004807
4808 rettv->v_type = VAR_STRING;
4809 rettv->vval.v_string = NULL;
4810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004811 str = tv_get_string(&argvars[0]);
4812 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4813 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004814 vimconv.vc_type = CONV_NONE;
4815 convert_setup(&vimconv, from, to);
4816
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818 if (vimconv.vc_type == CONV_NONE)
4819 rettv->vval.v_string = vim_strsave(str);
4820 else
4821 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4822
4823 convert_setup(&vimconv, NULL, NULL);
4824 vim_free(from);
4825 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004826}
4827
4828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004829 * "index()" function
4830 */
4831 static void
4832f_index(typval_T *argvars, typval_T *rettv)
4833{
4834 list_T *l;
4835 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004836 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004837 long idx = 0;
4838 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004839 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840
4841 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004842 if (argvars[0].v_type == VAR_BLOB)
4843 {
4844 typval_T tv;
4845 int start = 0;
4846
4847 if (argvars[2].v_type != VAR_UNKNOWN)
4848 {
4849 start = tv_get_number_chk(&argvars[2], &error);
4850 if (error)
4851 return;
4852 }
4853 b = argvars[0].vval.v_blob;
4854 if (b == NULL)
4855 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004856 if (start < 0)
4857 {
4858 start = blob_len(b) + start;
4859 if (start < 0)
4860 start = 0;
4861 }
4862
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004863 for (idx = start; idx < blob_len(b); ++idx)
4864 {
4865 tv.v_type = VAR_NUMBER;
4866 tv.vval.v_number = blob_get(b, idx);
4867 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4868 {
4869 rettv->vval.v_number = idx;
4870 return;
4871 }
4872 }
4873 return;
4874 }
4875 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004877 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004878 return;
4879 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 l = argvars[0].vval.v_list;
4882 if (l != NULL)
4883 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004884 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 item = l->lv_first;
4886 if (argvars[2].v_type != VAR_UNKNOWN)
4887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004888 // Start at specified item. Use the cached index that list_find()
4889 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004890 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004891 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004893 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004894 if (error)
4895 item = NULL;
4896 }
4897
4898 for ( ; item != NULL; item = item->li_next, ++idx)
4899 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4900 {
4901 rettv->vval.v_number = idx;
4902 break;
4903 }
4904 }
4905}
4906
4907static int inputsecret_flag = 0;
4908
4909/*
4910 * "input()" function
4911 * Also handles inputsecret() when inputsecret is set.
4912 */
4913 static void
4914f_input(typval_T *argvars, typval_T *rettv)
4915{
4916 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4917}
4918
4919/*
4920 * "inputdialog()" function
4921 */
4922 static void
4923f_inputdialog(typval_T *argvars, typval_T *rettv)
4924{
4925#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4928 {
4929 char_u *message;
4930 char_u buf[NUMBUFLEN];
4931 char_u *defstr = (char_u *)"";
4932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004933 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004934 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004935 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4937 else
4938 IObuff[0] = NUL;
4939 if (message != NULL && defstr != NULL
4940 && do_dialog(VIM_QUESTION, NULL, message,
4941 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4942 rettv->vval.v_string = vim_strsave(IObuff);
4943 else
4944 {
4945 if (message != NULL && defstr != NULL
4946 && argvars[1].v_type != VAR_UNKNOWN
4947 && argvars[2].v_type != VAR_UNKNOWN)
4948 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004949 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950 else
4951 rettv->vval.v_string = NULL;
4952 }
4953 rettv->v_type = VAR_STRING;
4954 }
4955 else
4956#endif
4957 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4958}
4959
4960/*
4961 * "inputlist()" function
4962 */
4963 static void
4964f_inputlist(typval_T *argvars, typval_T *rettv)
4965{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004966 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 listitem_T *li;
4968 int selected;
4969 int mouse_used;
4970
4971#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 // While starting up, there is no place to enter text. When running tests
4973 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02004974 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 return;
4976#endif
4977 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004979 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980 return;
4981 }
4982
4983 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 msg_row = Rows - 1; // for when 'cmdheight' > 1
4985 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 msg_scroll = TRUE;
4987 msg_clr_eos();
4988
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004989 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004990 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02004991 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004993 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004994 msg_putchar('\n');
4995 }
4996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004998 selected = prompt_for_number(&mouse_used);
4999 if (mouse_used)
5000 selected -= lines_left;
5001
5002 rettv->vval.v_number = selected;
5003}
5004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005005static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5006
5007/*
5008 * "inputrestore()" function
5009 */
5010 static void
5011f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5012{
5013 if (ga_userinput.ga_len > 0)
5014 {
5015 --ga_userinput.ga_len;
5016 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5017 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005018 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005019 }
5020 else if (p_verbose > 1)
5021 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005022 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 }
5025}
5026
5027/*
5028 * "inputsave()" function
5029 */
5030 static void
5031f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5032{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 if (ga_grow(&ga_userinput, 1) == OK)
5035 {
5036 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5037 + ga_userinput.ga_len);
5038 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005040 }
5041 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043}
5044
5045/*
5046 * "inputsecret()" function
5047 */
5048 static void
5049f_inputsecret(typval_T *argvars, typval_T *rettv)
5050{
5051 ++cmdline_star;
5052 ++inputsecret_flag;
5053 f_input(argvars, rettv);
5054 --cmdline_star;
5055 --inputsecret_flag;
5056}
5057
5058/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005059 * "interrupt()" function
5060 */
5061 static void
5062f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5063{
5064 got_int = TRUE;
5065}
5066
5067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005068 * "invert(expr)" function
5069 */
5070 static void
5071f_invert(typval_T *argvars, typval_T *rettv)
5072{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005073 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005074}
5075
5076/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 * "islocked()" function
5078 */
5079 static void
5080f_islocked(typval_T *argvars, typval_T *rettv)
5081{
5082 lval_T lv;
5083 char_u *end;
5084 dictitem_T *di;
5085
5086 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005088 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005089 if (end != NULL && lv.ll_name != NULL)
5090 {
5091 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005092 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 else
5094 {
5095 if (lv.ll_tv == NULL)
5096 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005097 di = find_var(lv.ll_name, NULL, TRUE);
5098 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // Consider a variable locked when:
5101 // 1. the variable itself is locked
5102 // 2. the value of the variable is locked.
5103 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005104 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5105 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106 }
5107 }
5108 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005109 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005111 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5115 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005117 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5118 }
5119 }
5120
5121 clear_lval(&lv);
5122}
5123
5124#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5125/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005126 * "isinf()" function
5127 */
5128 static void
5129f_isinf(typval_T *argvars, typval_T *rettv)
5130{
5131 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5132 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5133}
5134
5135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 * "isnan()" function
5137 */
5138 static void
5139f_isnan(typval_T *argvars, typval_T *rettv)
5140{
5141 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5142 && isnan(argvars[0].vval.v_float);
5143}
5144#endif
5145
5146/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005147 * "last_buffer_nr()" function.
5148 */
5149 static void
5150f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5151{
5152 int n = 0;
5153 buf_T *buf;
5154
Bram Moolenaar29323592016-07-24 22:04:11 +02005155 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005156 if (n < buf->b_fnum)
5157 n = buf->b_fnum;
5158
5159 rettv->vval.v_number = n;
5160}
5161
5162/*
5163 * "len()" function
5164 */
5165 static void
5166f_len(typval_T *argvars, typval_T *rettv)
5167{
5168 switch (argvars[0].v_type)
5169 {
5170 case VAR_STRING:
5171 case VAR_NUMBER:
5172 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005173 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005174 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005175 case VAR_BLOB:
5176 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5177 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005178 case VAR_LIST:
5179 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5180 break;
5181 case VAR_DICT:
5182 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5183 break;
5184 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005185 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005187 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005188 case VAR_SPECIAL:
5189 case VAR_FLOAT:
5190 case VAR_FUNC:
5191 case VAR_PARTIAL:
5192 case VAR_JOB:
5193 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005194 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005195 break;
5196 }
5197}
5198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005200libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005201{
5202#ifdef FEAT_LIBCALL
5203 char_u *string_in;
5204 char_u **string_result;
5205 int nr_result;
5206#endif
5207
5208 rettv->v_type = type;
5209 if (type != VAR_NUMBER)
5210 rettv->vval.v_string = NULL;
5211
5212 if (check_restricted() || check_secure())
5213 return;
5214
5215#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5218 {
5219 string_in = NULL;
5220 if (argvars[2].v_type == VAR_STRING)
5221 string_in = argvars[2].vval.v_string;
5222 if (type == VAR_NUMBER)
5223 string_result = NULL;
5224 else
5225 string_result = &rettv->vval.v_string;
5226 if (mch_libcall(argvars[0].vval.v_string,
5227 argvars[1].vval.v_string,
5228 string_in,
5229 argvars[2].vval.v_number,
5230 string_result,
5231 &nr_result) == OK
5232 && type == VAR_NUMBER)
5233 rettv->vval.v_number = nr_result;
5234 }
5235#endif
5236}
5237
5238/*
5239 * "libcall()" function
5240 */
5241 static void
5242f_libcall(typval_T *argvars, typval_T *rettv)
5243{
5244 libcall_common(argvars, rettv, VAR_STRING);
5245}
5246
5247/*
5248 * "libcallnr()" function
5249 */
5250 static void
5251f_libcallnr(typval_T *argvars, typval_T *rettv)
5252{
5253 libcall_common(argvars, rettv, VAR_NUMBER);
5254}
5255
5256/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005257 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005258 */
5259 static void
5260f_line(typval_T *argvars, typval_T *rettv)
5261{
5262 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005263 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005264 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005265 int id;
5266 tabpage_T *tp;
5267 win_T *wp;
5268 win_T *save_curwin;
5269 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005270
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005271 if (argvars[1].v_type != VAR_UNKNOWN)
5272 {
5273 // use window specified in the second argument
5274 id = (int)tv_get_number(&argvars[1]);
5275 wp = win_id2wp_tp(id, &tp);
5276 if (wp != NULL && tp != NULL)
5277 {
5278 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5279 == OK)
5280 {
5281 check_cursor();
5282 fp = var2fpos(&argvars[0], TRUE, &fnum);
5283 }
5284 restore_win_noblock(save_curwin, save_curtab, TRUE);
5285 }
5286 }
5287 else
5288 // use current window
5289 fp = var2fpos(&argvars[0], TRUE, &fnum);
5290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 if (fp != NULL)
5292 lnum = fp->lnum;
5293 rettv->vval.v_number = lnum;
5294}
5295
5296/*
5297 * "line2byte(lnum)" function
5298 */
5299 static void
5300f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5301{
5302#ifndef FEAT_BYTEOFF
5303 rettv->vval.v_number = -1;
5304#else
5305 linenr_T lnum;
5306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005307 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005308 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5309 rettv->vval.v_number = -1;
5310 else
5311 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5312 if (rettv->vval.v_number >= 0)
5313 ++rettv->vval.v_number;
5314#endif
5315}
5316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317#ifdef FEAT_FLOAT
5318/*
5319 * "log()" function
5320 */
5321 static void
5322f_log(typval_T *argvars, typval_T *rettv)
5323{
5324 float_T f = 0.0;
5325
5326 rettv->v_type = VAR_FLOAT;
5327 if (get_float_arg(argvars, &f) == OK)
5328 rettv->vval.v_float = log(f);
5329 else
5330 rettv->vval.v_float = 0.0;
5331}
5332
5333/*
5334 * "log10()" function
5335 */
5336 static void
5337f_log10(typval_T *argvars, typval_T *rettv)
5338{
5339 float_T f = 0.0;
5340
5341 rettv->v_type = VAR_FLOAT;
5342 if (get_float_arg(argvars, &f) == OK)
5343 rettv->vval.v_float = log10(f);
5344 else
5345 rettv->vval.v_float = 0.0;
5346}
5347#endif
5348
5349#ifdef FEAT_LUA
5350/*
5351 * "luaeval()" function
5352 */
5353 static void
5354f_luaeval(typval_T *argvars, typval_T *rettv)
5355{
5356 char_u *str;
5357 char_u buf[NUMBUFLEN];
5358
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005359 if (check_restricted() || check_secure())
5360 return;
5361
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005362 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363 do_luaeval(str, argvars + 1, rettv);
5364}
5365#endif
5366
5367/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 * "maparg()" function
5369 */
5370 static void
5371f_maparg(typval_T *argvars, typval_T *rettv)
5372{
5373 get_maparg(argvars, rettv, TRUE);
5374}
5375
5376/*
5377 * "mapcheck()" function
5378 */
5379 static void
5380f_mapcheck(typval_T *argvars, typval_T *rettv)
5381{
5382 get_maparg(argvars, rettv, FALSE);
5383}
5384
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005385typedef enum
5386{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 MATCH_END, // matchend()
5388 MATCH_MATCH, // match()
5389 MATCH_STR, // matchstr()
5390 MATCH_LIST, // matchlist()
5391 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005392} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005393
5394 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005395find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396{
5397 char_u *str = NULL;
5398 long len = 0;
5399 char_u *expr = NULL;
5400 char_u *pat;
5401 regmatch_T regmatch;
5402 char_u patbuf[NUMBUFLEN];
5403 char_u strbuf[NUMBUFLEN];
5404 char_u *save_cpo;
5405 long start = 0;
5406 long nth = 1;
5407 colnr_T startcol = 0;
5408 int match = 0;
5409 list_T *l = NULL;
5410 listitem_T *li = NULL;
5411 long idx = 0;
5412 char_u *tofree = NULL;
5413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005415 save_cpo = p_cpo;
5416 p_cpo = (char_u *)"";
5417
5418 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005419 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005421 // type MATCH_LIST: return empty list when there are no matches.
5422 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423 if (rettv_list_alloc(rettv) == FAIL)
5424 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005425 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426 && (list_append_string(rettv->vval.v_list,
5427 (char_u *)"", 0) == FAIL
5428 || list_append_number(rettv->vval.v_list,
5429 (varnumber_T)-1) == FAIL
5430 || list_append_number(rettv->vval.v_list,
5431 (varnumber_T)-1) == FAIL
5432 || list_append_number(rettv->vval.v_list,
5433 (varnumber_T)-1) == FAIL))
5434 {
5435 list_free(rettv->vval.v_list);
5436 rettv->vval.v_list = NULL;
5437 goto theend;
5438 }
5439 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005440 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 {
5442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = NULL;
5444 }
5445
5446 if (argvars[0].v_type == VAR_LIST)
5447 {
5448 if ((l = argvars[0].vval.v_list) == NULL)
5449 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005450 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451 li = l->lv_first;
5452 }
5453 else
5454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005455 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456 len = (long)STRLEN(str);
5457 }
5458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005459 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460 if (pat == NULL)
5461 goto theend;
5462
5463 if (argvars[2].v_type != VAR_UNKNOWN)
5464 {
5465 int error = FALSE;
5466
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005467 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005468 if (error)
5469 goto theend;
5470 if (l != NULL)
5471 {
5472 li = list_find(l, start);
5473 if (li == NULL)
5474 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005475 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 }
5477 else
5478 {
5479 if (start < 0)
5480 start = 0;
5481 if (start > len)
5482 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 // When "count" argument is there ignore matches before "start",
5484 // otherwise skip part of the string. Differs when pattern is "^"
5485 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 if (argvars[3].v_type != VAR_UNKNOWN)
5487 startcol = start;
5488 else
5489 {
5490 str += start;
5491 len -= start;
5492 }
5493 }
5494
5495 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005496 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 if (error)
5498 goto theend;
5499 }
5500
5501 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5502 if (regmatch.regprog != NULL)
5503 {
5504 regmatch.rm_ic = p_ic;
5505
5506 for (;;)
5507 {
5508 if (l != NULL)
5509 {
5510 if (li == NULL)
5511 {
5512 match = FALSE;
5513 break;
5514 }
5515 vim_free(tofree);
5516 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5517 if (str == NULL)
5518 break;
5519 }
5520
5521 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5522
5523 if (match && --nth <= 0)
5524 break;
5525 if (l == NULL && !match)
5526 break;
5527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005528 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 if (l != NULL)
5530 {
5531 li = li->li_next;
5532 ++idx;
5533 }
5534 else
5535 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536 startcol = (colnr_T)(regmatch.startp[0]
5537 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538 if (startcol > (colnr_T)len
5539 || str + startcol <= regmatch.startp[0])
5540 {
5541 match = FALSE;
5542 break;
5543 }
5544 }
5545 }
5546
5547 if (match)
5548 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005549 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 {
5551 listitem_T *li1 = rettv->vval.v_list->lv_first;
5552 listitem_T *li2 = li1->li_next;
5553 listitem_T *li3 = li2->li_next;
5554 listitem_T *li4 = li3->li_next;
5555
5556 vim_free(li1->li_tv.vval.v_string);
5557 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005558 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 li3->li_tv.vval.v_number =
5560 (varnumber_T)(regmatch.startp[0] - expr);
5561 li4->li_tv.vval.v_number =
5562 (varnumber_T)(regmatch.endp[0] - expr);
5563 if (l != NULL)
5564 li2->li_tv.vval.v_number = (varnumber_T)idx;
5565 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005566 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 {
5568 int i;
5569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005570 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 for (i = 0; i < NSUBEXP; ++i)
5572 {
5573 if (regmatch.endp[i] == NULL)
5574 {
5575 if (list_append_string(rettv->vval.v_list,
5576 (char_u *)"", 0) == FAIL)
5577 break;
5578 }
5579 else if (list_append_string(rettv->vval.v_list,
5580 regmatch.startp[i],
5581 (int)(regmatch.endp[i] - regmatch.startp[i]))
5582 == FAIL)
5583 break;
5584 }
5585 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005586 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005588 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 if (l != NULL)
5590 copy_tv(&li->li_tv, rettv);
5591 else
5592 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005593 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594 }
5595 else if (l != NULL)
5596 rettv->vval.v_number = idx;
5597 else
5598 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005599 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 rettv->vval.v_number =
5601 (varnumber_T)(regmatch.startp[0] - str);
5602 else
5603 rettv->vval.v_number =
5604 (varnumber_T)(regmatch.endp[0] - str);
5605 rettv->vval.v_number += (varnumber_T)(str - expr);
5606 }
5607 }
5608 vim_regfree(regmatch.regprog);
5609 }
5610
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005611theend:
5612 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005613 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 listitem_remove(rettv->vval.v_list,
5615 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 vim_free(tofree);
5617 p_cpo = save_cpo;
5618}
5619
5620/*
5621 * "match()" function
5622 */
5623 static void
5624f_match(typval_T *argvars, typval_T *rettv)
5625{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005626 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627}
5628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629/*
5630 * "matchend()" function
5631 */
5632 static void
5633f_matchend(typval_T *argvars, typval_T *rettv)
5634{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005635 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636}
5637
5638/*
5639 * "matchlist()" function
5640 */
5641 static void
5642f_matchlist(typval_T *argvars, typval_T *rettv)
5643{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005644 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645}
5646
5647/*
5648 * "matchstr()" function
5649 */
5650 static void
5651f_matchstr(typval_T *argvars, typval_T *rettv)
5652{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005653 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654}
5655
5656/*
5657 * "matchstrpos()" function
5658 */
5659 static void
5660f_matchstrpos(typval_T *argvars, typval_T *rettv)
5661{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005662 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005663}
5664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665 static void
5666max_min(typval_T *argvars, typval_T *rettv, int domax)
5667{
5668 varnumber_T n = 0;
5669 varnumber_T i;
5670 int error = FALSE;
5671
5672 if (argvars[0].v_type == VAR_LIST)
5673 {
5674 list_T *l;
5675 listitem_T *li;
5676
5677 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005678 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005679 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005680 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005681 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005682 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5683 n = l->lv_u.nonmat.lv_start;
5684 else
5685 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5686 * l->lv_u.nonmat.lv_stride;
5687 }
5688 else
5689 {
5690 li = l->lv_first;
5691 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005693 n = tv_get_number_chk(&li->li_tv, &error);
5694 for (;;)
5695 {
5696 li = li->li_next;
5697 if (li == NULL)
5698 break;
5699 i = tv_get_number_chk(&li->li_tv, &error);
5700 if (domax ? i > n : i < n)
5701 n = i;
5702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 }
5704 }
5705 }
5706 }
5707 else if (argvars[0].v_type == VAR_DICT)
5708 {
5709 dict_T *d;
5710 int first = TRUE;
5711 hashitem_T *hi;
5712 int todo;
5713
5714 d = argvars[0].vval.v_dict;
5715 if (d != NULL)
5716 {
5717 todo = (int)d->dv_hashtab.ht_used;
5718 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5719 {
5720 if (!HASHITEM_EMPTY(hi))
5721 {
5722 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005723 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 if (first)
5725 {
5726 n = i;
5727 first = FALSE;
5728 }
5729 else if (domax ? i > n : i < n)
5730 n = i;
5731 }
5732 }
5733 }
5734 }
5735 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005736 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005737 rettv->vval.v_number = error ? 0 : n;
5738}
5739
5740/*
5741 * "max()" function
5742 */
5743 static void
5744f_max(typval_T *argvars, typval_T *rettv)
5745{
5746 max_min(argvars, rettv, TRUE);
5747}
5748
5749/*
5750 * "min()" function
5751 */
5752 static void
5753f_min(typval_T *argvars, typval_T *rettv)
5754{
5755 max_min(argvars, rettv, FALSE);
5756}
5757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758#if defined(FEAT_MZSCHEME) || defined(PROTO)
5759/*
5760 * "mzeval()" function
5761 */
5762 static void
5763f_mzeval(typval_T *argvars, typval_T *rettv)
5764{
5765 char_u *str;
5766 char_u buf[NUMBUFLEN];
5767
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005768 if (check_restricted() || check_secure())
5769 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005770 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 do_mzeval(str, rettv);
5772}
5773
5774 void
5775mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5776{
5777 typval_T argvars[3];
5778
5779 argvars[0].v_type = VAR_STRING;
5780 argvars[0].vval.v_string = name;
5781 copy_tv(args, &argvars[1]);
5782 argvars[2].v_type = VAR_UNKNOWN;
5783 f_call(argvars, rettv);
5784 clear_tv(&argvars[1]);
5785}
5786#endif
5787
5788/*
5789 * "nextnonblank()" function
5790 */
5791 static void
5792f_nextnonblank(typval_T *argvars, typval_T *rettv)
5793{
5794 linenr_T lnum;
5795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005796 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005797 {
5798 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5799 {
5800 lnum = 0;
5801 break;
5802 }
5803 if (*skipwhite(ml_get(lnum)) != NUL)
5804 break;
5805 }
5806 rettv->vval.v_number = lnum;
5807}
5808
5809/*
5810 * "nr2char()" function
5811 */
5812 static void
5813f_nr2char(typval_T *argvars, typval_T *rettv)
5814{
5815 char_u buf[NUMBUFLEN];
5816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817 if (has_mbyte)
5818 {
5819 int utf8 = 0;
5820
5821 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005822 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005824 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005825 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005826 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827 }
5828 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005830 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 buf[1] = NUL;
5832 }
5833 rettv->v_type = VAR_STRING;
5834 rettv->vval.v_string = vim_strsave(buf);
5835}
5836
5837/*
5838 * "or(expr, expr)" function
5839 */
5840 static void
5841f_or(typval_T *argvars, typval_T *rettv)
5842{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5844 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845}
5846
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005847#ifdef FEAT_PERL
5848/*
5849 * "perleval()" function
5850 */
5851 static void
5852f_perleval(typval_T *argvars, typval_T *rettv)
5853{
5854 char_u *str;
5855 char_u buf[NUMBUFLEN];
5856
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 do_perleval(str, rettv);
5859}
5860#endif
5861
5862#ifdef FEAT_FLOAT
5863/*
5864 * "pow()" function
5865 */
5866 static void
5867f_pow(typval_T *argvars, typval_T *rettv)
5868{
5869 float_T fx = 0.0, fy = 0.0;
5870
5871 rettv->v_type = VAR_FLOAT;
5872 if (get_float_arg(argvars, &fx) == OK
5873 && get_float_arg(&argvars[1], &fy) == OK)
5874 rettv->vval.v_float = pow(fx, fy);
5875 else
5876 rettv->vval.v_float = 0.0;
5877}
5878#endif
5879
5880/*
5881 * "prevnonblank()" function
5882 */
5883 static void
5884f_prevnonblank(typval_T *argvars, typval_T *rettv)
5885{
5886 linenr_T lnum;
5887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005888 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005889 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5890 lnum = 0;
5891 else
5892 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5893 --lnum;
5894 rettv->vval.v_number = lnum;
5895}
5896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005897// This dummy va_list is here because:
5898// - passing a NULL pointer doesn't work when va_list isn't a pointer
5899// - locally in the function results in a "used before set" warning
5900// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901static va_list ap;
5902
5903/*
5904 * "printf()" function
5905 */
5906 static void
5907f_printf(typval_T *argvars, typval_T *rettv)
5908{
5909 char_u buf[NUMBUFLEN];
5910 int len;
5911 char_u *s;
5912 int saved_did_emsg = did_emsg;
5913 char *fmt;
5914
5915 rettv->v_type = VAR_STRING;
5916 rettv->vval.v_string = NULL;
5917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005919 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005920 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005921 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005922 if (!did_emsg)
5923 {
5924 s = alloc(len + 1);
5925 if (s != NULL)
5926 {
5927 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005928 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5929 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 }
5931 }
5932 did_emsg |= saved_did_emsg;
5933}
5934
5935/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005936 * "pum_getpos()" function
5937 */
5938 static void
5939f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5940{
5941 if (rettv_dict_alloc(rettv) != OK)
5942 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005943 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005944}
5945
5946/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005947 * "pumvisible()" function
5948 */
5949 static void
5950f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5951{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 if (pum_visible())
5953 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954}
5955
5956#ifdef FEAT_PYTHON3
5957/*
5958 * "py3eval()" function
5959 */
5960 static void
5961f_py3eval(typval_T *argvars, typval_T *rettv)
5962{
5963 char_u *str;
5964 char_u buf[NUMBUFLEN];
5965
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005966 if (check_restricted() || check_secure())
5967 return;
5968
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005969 if (p_pyx == 0)
5970 p_pyx = 3;
5971
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005972 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 do_py3eval(str, rettv);
5974}
5975#endif
5976
5977#ifdef FEAT_PYTHON
5978/*
5979 * "pyeval()" function
5980 */
5981 static void
5982f_pyeval(typval_T *argvars, typval_T *rettv)
5983{
5984 char_u *str;
5985 char_u buf[NUMBUFLEN];
5986
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005987 if (check_restricted() || check_secure())
5988 return;
5989
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005990 if (p_pyx == 0)
5991 p_pyx = 2;
5992
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005993 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 do_pyeval(str, rettv);
5995}
5996#endif
5997
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005998#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
5999/*
6000 * "pyxeval()" function
6001 */
6002 static void
6003f_pyxeval(typval_T *argvars, typval_T *rettv)
6004{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006005 if (check_restricted() || check_secure())
6006 return;
6007
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006008# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6009 init_pyxversion();
6010 if (p_pyx == 2)
6011 f_pyeval(argvars, rettv);
6012 else
6013 f_py3eval(argvars, rettv);
6014# elif defined(FEAT_PYTHON)
6015 f_pyeval(argvars, rettv);
6016# elif defined(FEAT_PYTHON3)
6017 f_py3eval(argvars, rettv);
6018# endif
6019}
6020#endif
6021
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006022static UINT32_T srand_seed_for_testing = 0;
6023static int srand_seed_for_testing_is_used = FALSE;
6024
6025 static void
6026f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6027{
6028 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006029 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006030 else
6031 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006032 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6033 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006034 }
6035}
6036
6037 static void
6038init_srand(UINT32_T *x)
6039{
6040#ifndef MSWIN
6041 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6042#endif
6043
6044 if (srand_seed_for_testing_is_used)
6045 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006046 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006047 return;
6048 }
6049#ifndef MSWIN
6050 if (dev_urandom_state != FAIL)
6051 {
6052 int fd = open("/dev/urandom", O_RDONLY);
6053 struct {
6054 union {
6055 UINT32_T number;
6056 char bytes[sizeof(UINT32_T)];
6057 } contents;
6058 } buf;
6059
6060 // Attempt reading /dev/urandom.
6061 if (fd == -1)
6062 dev_urandom_state = FAIL;
6063 else
6064 {
6065 buf.contents.number = 0;
6066 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6067 != sizeof(UINT32_T))
6068 dev_urandom_state = FAIL;
6069 else
6070 {
6071 dev_urandom_state = OK;
6072 *x = buf.contents.number;
6073 }
6074 close(fd);
6075 }
6076 }
6077 if (dev_urandom_state != OK)
6078 // Reading /dev/urandom doesn't work, fall back to time().
6079#endif
6080 *x = vim_time();
6081}
6082
6083#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6084#define SPLITMIX32(x, z) ( \
6085 z = (x += 0x9e3779b9), \
6086 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6087 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6088 z ^ (z >> 16) \
6089 )
6090#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6091 result = ROTL(y * 5, 7) * 9; \
6092 t = y << 9; \
6093 z ^= x; \
6094 w ^= y; \
6095 y ^= z, x ^= w; \
6096 z ^= t; \
6097 w = ROTL(w, 11);
6098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006100 * "rand()" function
6101 */
6102 static void
6103f_rand(typval_T *argvars, typval_T *rettv)
6104{
6105 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006106 static UINT32_T gx, gy, gz, gw;
6107 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006108 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006109 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006110
6111 if (argvars[0].v_type == VAR_UNKNOWN)
6112 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006113 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006114 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006115 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006116 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006117 init_srand(&x);
6118
6119 gx = SPLITMIX32(x, z);
6120 gy = SPLITMIX32(x, z);
6121 gz = SPLITMIX32(x, z);
6122 gw = SPLITMIX32(x, z);
6123 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006124 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006125
6126 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006127 }
6128 else if (argvars[0].v_type == VAR_LIST)
6129 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006130 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006131 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006132 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006133
6134 lx = list_find(l, 0L);
6135 ly = list_find(l, 1L);
6136 lz = list_find(l, 2L);
6137 lw = list_find(l, 3L);
6138 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6139 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6140 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6141 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6142 x = (UINT32_T)lx->li_tv.vval.v_number;
6143 y = (UINT32_T)ly->li_tv.vval.v_number;
6144 z = (UINT32_T)lz->li_tv.vval.v_number;
6145 w = (UINT32_T)lw->li_tv.vval.v_number;
6146
6147 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6148
6149 lx->li_tv.vval.v_number = (varnumber_T)x;
6150 ly->li_tv.vval.v_number = (varnumber_T)y;
6151 lz->li_tv.vval.v_number = (varnumber_T)z;
6152 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006153 }
6154 else
6155 goto theend;
6156
6157 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006158 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006159 return;
6160
6161theend:
6162 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006163 rettv->v_type = VAR_NUMBER;
6164 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006165}
6166
6167/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006168 * "srand()" function
6169 */
6170 static void
6171f_srand(typval_T *argvars, typval_T *rettv)
6172{
6173 UINT32_T x = 0, z;
6174
6175 if (rettv_list_alloc(rettv) == FAIL)
6176 return;
6177 if (argvars[0].v_type == VAR_UNKNOWN)
6178 {
6179 init_srand(&x);
6180 }
6181 else
6182 {
6183 int error = FALSE;
6184
6185 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6186 if (error)
6187 return;
6188 }
6189
6190 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6191 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6192 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6193 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6194}
6195
6196#undef ROTL
6197#undef SPLITMIX32
6198#undef SHUFFLE_XOSHIRO128STARSTAR
6199
6200/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201 * "range()" function
6202 */
6203 static void
6204f_range(typval_T *argvars, typval_T *rettv)
6205{
6206 varnumber_T start;
6207 varnumber_T end;
6208 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209 int error = FALSE;
6210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006211 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212 if (argvars[1].v_type == VAR_UNKNOWN)
6213 {
6214 end = start - 1;
6215 start = 0;
6216 }
6217 else
6218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006219 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006221 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 }
6223
6224 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006227 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006229 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006230 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006232 list_T *list = rettv->vval.v_list;
6233
6234 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006235 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006236 // be called.
6237 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006238 list->lv_u.nonmat.lv_start = start;
6239 list->lv_u.nonmat.lv_end = end;
6240 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006241 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006242 }
6243}
6244
6245/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006246 * Materialize "list".
6247 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248 */
6249 void
6250range_list_materialize(list_T *list)
6251{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006252 varnumber_T start = list->lv_u.nonmat.lv_start;
6253 varnumber_T end = list->lv_u.nonmat.lv_end;
6254 int stride = list->lv_u.nonmat.lv_stride;
6255 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006256
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006257 list->lv_first = NULL;
6258 list->lv_u.mat.lv_last = NULL;
6259 list->lv_len = 0;
6260 list->lv_u.mat.lv_idx_item = NULL;
6261 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6262 if (list_append_number(list, (varnumber_T)i) == FAIL)
6263 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264}
6265
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006266/*
6267 * "getreginfo()" function
6268 */
6269 static void
6270f_getreginfo(typval_T *argvars, typval_T *rettv)
6271{
6272 char_u *strregname;
6273 int regname;
6274 char_u buf[NUMBUFLEN + 2];
6275 long reglen = 0;
6276 dict_T *dict;
6277 list_T *list;
6278
6279 if (argvars[0].v_type != VAR_UNKNOWN)
6280 {
6281 strregname = tv_get_string_chk(&argvars[0]);
6282 if (strregname == NULL)
6283 return;
6284 }
6285 else
6286 strregname = get_vim_var_str(VV_REG);
6287
6288 regname = (strregname == NULL ? '"' : *strregname);
6289 if (regname == 0 || regname == '@')
6290 regname = '"';
6291
6292 if (rettv_dict_alloc(rettv) == FAIL)
6293 return;
6294 dict = rettv->vval.v_dict;
6295
6296 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6297 if (list == NULL)
6298 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006299 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006300
6301 buf[0] = NUL;
6302 buf[1] = NUL;
6303 switch (get_reg_type(regname, &reglen))
6304 {
6305 case MLINE: buf[0] = 'V'; break;
6306 case MCHAR: buf[0] = 'v'; break;
6307 case MBLOCK:
6308 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6309 reglen + 1);
6310 break;
6311 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006312 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006313
6314 buf[0] = get_register_name(get_unname_register());
6315 buf[1] = NUL;
6316 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006317 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006318 else
6319 {
6320 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6321
6322 if (item != NULL)
6323 {
6324 item->di_tv.v_type = VAR_SPECIAL;
6325 item->di_tv.vval.v_number = regname == buf[0]
6326 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006327 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006328 }
6329 }
6330}
6331
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006332 static void
6333return_register(int regname, typval_T *rettv)
6334{
6335 char_u buf[2] = {0, 0};
6336
6337 buf[0] = (char_u)regname;
6338 rettv->v_type = VAR_STRING;
6339 rettv->vval.v_string = vim_strsave(buf);
6340}
6341
6342/*
6343 * "reg_executing()" function
6344 */
6345 static void
6346f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6347{
6348 return_register(reg_executing, rettv);
6349}
6350
6351/*
6352 * "reg_recording()" function
6353 */
6354 static void
6355f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6356{
6357 return_register(reg_recording, rettv);
6358}
6359
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006360/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361 * "rename({from}, {to})" function
6362 */
6363 static void
6364f_rename(typval_T *argvars, typval_T *rettv)
6365{
6366 char_u buf[NUMBUFLEN];
6367
6368 if (check_restricted() || check_secure())
6369 rettv->vval.v_number = -1;
6370 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006371 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6372 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373}
6374
6375/*
6376 * "repeat()" function
6377 */
6378 static void
6379f_repeat(typval_T *argvars, typval_T *rettv)
6380{
6381 char_u *p;
6382 int n;
6383 int slen;
6384 int len;
6385 char_u *r;
6386 int i;
6387
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006388 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389 if (argvars[0].v_type == VAR_LIST)
6390 {
6391 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6392 while (n-- > 0)
6393 if (list_extend(rettv->vval.v_list,
6394 argvars[0].vval.v_list, NULL) == FAIL)
6395 break;
6396 }
6397 else
6398 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006399 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 rettv->v_type = VAR_STRING;
6401 rettv->vval.v_string = NULL;
6402
6403 slen = (int)STRLEN(p);
6404 len = slen * n;
6405 if (len <= 0)
6406 return;
6407
6408 r = alloc(len + 1);
6409 if (r != NULL)
6410 {
6411 for (i = 0; i < n; i++)
6412 mch_memmove(r + i * slen, p, (size_t)slen);
6413 r[len] = NUL;
6414 }
6415
6416 rettv->vval.v_string = r;
6417 }
6418}
6419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006420#define SP_NOMOVE 0x01 // don't move cursor
6421#define SP_REPEAT 0x02 // repeat to find outer pair
6422#define SP_RETCOUNT 0x04 // return matchcount
6423#define SP_SETPCMARK 0x08 // set previous context mark
6424#define SP_START 0x10 // accept match at start position
6425#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6426#define SP_END 0x40 // leave cursor at end of match
6427#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429/*
6430 * Get flags for a search function.
6431 * Possibly sets "p_ws".
6432 * Returns BACKWARD, FORWARD or zero (for an error).
6433 */
6434 static int
6435get_search_arg(typval_T *varp, int *flagsp)
6436{
6437 int dir = FORWARD;
6438 char_u *flags;
6439 char_u nbuf[NUMBUFLEN];
6440 int mask;
6441
6442 if (varp->v_type != VAR_UNKNOWN)
6443 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006444 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006446 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 while (*flags != NUL)
6448 {
6449 switch (*flags)
6450 {
6451 case 'b': dir = BACKWARD; break;
6452 case 'w': p_ws = TRUE; break;
6453 case 'W': p_ws = FALSE; break;
6454 default: mask = 0;
6455 if (flagsp != NULL)
6456 switch (*flags)
6457 {
6458 case 'c': mask = SP_START; break;
6459 case 'e': mask = SP_END; break;
6460 case 'm': mask = SP_RETCOUNT; break;
6461 case 'n': mask = SP_NOMOVE; break;
6462 case 'p': mask = SP_SUBPAT; break;
6463 case 'r': mask = SP_REPEAT; break;
6464 case 's': mask = SP_SETPCMARK; break;
6465 case 'z': mask = SP_COLUMN; break;
6466 }
6467 if (mask == 0)
6468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006469 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470 dir = 0;
6471 }
6472 else
6473 *flagsp |= mask;
6474 }
6475 if (dir == 0)
6476 break;
6477 ++flags;
6478 }
6479 }
6480 return dir;
6481}
6482
6483/*
6484 * Shared by search() and searchpos() functions.
6485 */
6486 static int
6487search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6488{
6489 int flags;
6490 char_u *pat;
6491 pos_T pos;
6492 pos_T save_cursor;
6493 int save_p_ws = p_ws;
6494 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006495 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006498 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499 long time_limit = 0;
6500#endif
6501 int options = SEARCH_KEEP;
6502 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006503 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006504 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006505 pos_T firstpos;
6506
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006507 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006508 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006509 if (dir == 0)
6510 goto theend;
6511 flags = *flagsp;
6512 if (flags & SP_START)
6513 options |= SEARCH_START;
6514 if (flags & SP_END)
6515 options |= SEARCH_END;
6516 if (flags & SP_COLUMN)
6517 options |= SEARCH_COL;
6518
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006519 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006522 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006523 if (lnum_stop < 0)
6524 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006525 if (argvars[3].v_type != VAR_UNKNOWN)
6526 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006527#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006528 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 if (time_limit < 0)
6530 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006531#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006532 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006533 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 }
6535
6536#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006537 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538 profile_setlimit(time_limit, &tm);
6539#endif
6540
6541 /*
6542 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6543 * Check to make sure only those flags are set.
6544 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6545 * flags cannot be set. Check for that condition also.
6546 */
6547 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6548 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006550 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 goto theend;
6552 }
6553
6554 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006555 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006556 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006557 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6558#ifdef FEAT_RELTIME
6559 sia.sa_tm = &tm;
6560#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006561
6562 // Repeat until {skip} returns FALSE.
6563 for (;;)
6564 {
6565 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006566 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006567 // finding the first match again means there is no match where {skip}
6568 // evaluates to zero.
6569 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6570 subpatnum = FAIL;
6571
Bram Moolenaara9c01042020-06-07 14:50:50 +02006572 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006573 // didn't find it or no skip argument
6574 break;
6575 firstpos = pos;
6576
Bram Moolenaara9c01042020-06-07 14:50:50 +02006577 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006578 {
6579 int do_skip;
6580 int err;
6581 pos_T save_pos = curwin->w_cursor;
6582
6583 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006584 err = FALSE;
6585 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006586 curwin->w_cursor = save_pos;
6587 if (err)
6588 {
6589 // Evaluating {skip} caused an error, break here.
6590 subpatnum = FAIL;
6591 break;
6592 }
6593 if (!do_skip)
6594 break;
6595 }
6596 }
6597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 if (subpatnum != FAIL)
6599 {
6600 if (flags & SP_SUBPAT)
6601 retval = subpatnum;
6602 else
6603 retval = pos.lnum;
6604 if (flags & SP_SETPCMARK)
6605 setpcmark();
6606 curwin->w_cursor = pos;
6607 if (match_pos != NULL)
6608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006609 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610 match_pos->lnum = pos.lnum;
6611 match_pos->col = pos.col + 1;
6612 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006613 // "/$" will put the cursor after the end of the line, may need to
6614 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615 check_cursor();
6616 }
6617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006618 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619 if (flags & SP_NOMOVE)
6620 curwin->w_cursor = save_cursor;
6621 else
6622 curwin->w_set_curswant = TRUE;
6623theend:
6624 p_ws = save_p_ws;
6625
6626 return retval;
6627}
6628
6629#ifdef FEAT_FLOAT
6630
6631/*
6632 * round() is not in C90, use ceil() or floor() instead.
6633 */
6634 float_T
6635vim_round(float_T f)
6636{
6637 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6638}
6639
6640/*
6641 * "round({float})" function
6642 */
6643 static void
6644f_round(typval_T *argvars, typval_T *rettv)
6645{
6646 float_T f = 0.0;
6647
6648 rettv->v_type = VAR_FLOAT;
6649 if (get_float_arg(argvars, &f) == OK)
6650 rettv->vval.v_float = vim_round(f);
6651 else
6652 rettv->vval.v_float = 0.0;
6653}
6654#endif
6655
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006656#ifdef FEAT_RUBY
6657/*
6658 * "rubyeval()" function
6659 */
6660 static void
6661f_rubyeval(typval_T *argvars, typval_T *rettv)
6662{
6663 char_u *str;
6664 char_u buf[NUMBUFLEN];
6665
6666 str = tv_get_string_buf(&argvars[0], buf);
6667 do_rubyeval(str, rettv);
6668}
6669#endif
6670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671/*
6672 * "screenattr()" function
6673 */
6674 static void
6675f_screenattr(typval_T *argvars, typval_T *rettv)
6676{
6677 int row;
6678 int col;
6679 int c;
6680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006681 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6682 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006683 if (row < 0 || row >= screen_Rows
6684 || col < 0 || col >= screen_Columns)
6685 c = -1;
6686 else
6687 c = ScreenAttrs[LineOffset[row] + col];
6688 rettv->vval.v_number = c;
6689}
6690
6691/*
6692 * "screenchar()" function
6693 */
6694 static void
6695f_screenchar(typval_T *argvars, typval_T *rettv)
6696{
6697 int row;
6698 int col;
6699 int off;
6700 int c;
6701
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006702 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6703 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006704 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705 c = -1;
6706 else
6707 {
6708 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709 if (enc_utf8 && ScreenLinesUC[off] != 0)
6710 c = ScreenLinesUC[off];
6711 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712 c = ScreenLines[off];
6713 }
6714 rettv->vval.v_number = c;
6715}
6716
6717/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006718 * "screenchars()" function
6719 */
6720 static void
6721f_screenchars(typval_T *argvars, typval_T *rettv)
6722{
6723 int row;
6724 int col;
6725 int off;
6726 int c;
6727 int i;
6728
6729 if (rettv_list_alloc(rettv) == FAIL)
6730 return;
6731 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6732 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6733 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6734 return;
6735
6736 off = LineOffset[row] + col;
6737 if (enc_utf8 && ScreenLinesUC[off] != 0)
6738 c = ScreenLinesUC[off];
6739 else
6740 c = ScreenLines[off];
6741 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6742
6743 if (enc_utf8)
6744
6745 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6746 list_append_number(rettv->vval.v_list,
6747 (varnumber_T)ScreenLinesC[i][off]);
6748}
6749
6750/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751 * "screencol()" function
6752 *
6753 * First column is 1 to be consistent with virtcol().
6754 */
6755 static void
6756f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6757{
6758 rettv->vval.v_number = screen_screencol() + 1;
6759}
6760
6761/*
6762 * "screenrow()" function
6763 */
6764 static void
6765f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6766{
6767 rettv->vval.v_number = screen_screenrow() + 1;
6768}
6769
6770/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006771 * "screenstring()" function
6772 */
6773 static void
6774f_screenstring(typval_T *argvars, typval_T *rettv)
6775{
6776 int row;
6777 int col;
6778 int off;
6779 int c;
6780 int i;
6781 char_u buf[MB_MAXBYTES + 1];
6782 int buflen = 0;
6783
6784 rettv->vval.v_string = NULL;
6785 rettv->v_type = VAR_STRING;
6786
6787 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6788 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6789 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6790 return;
6791
6792 off = LineOffset[row] + col;
6793 if (enc_utf8 && ScreenLinesUC[off] != 0)
6794 c = ScreenLinesUC[off];
6795 else
6796 c = ScreenLines[off];
6797 buflen += mb_char2bytes(c, buf);
6798
6799 if (enc_utf8)
6800 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6801 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6802
6803 buf[buflen] = NUL;
6804 rettv->vval.v_string = vim_strsave(buf);
6805}
6806
6807/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808 * "search()" function
6809 */
6810 static void
6811f_search(typval_T *argvars, typval_T *rettv)
6812{
6813 int flags = 0;
6814
6815 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6816}
6817
6818/*
6819 * "searchdecl()" function
6820 */
6821 static void
6822f_searchdecl(typval_T *argvars, typval_T *rettv)
6823{
6824 int locally = 1;
6825 int thisblock = 0;
6826 int error = FALSE;
6827 char_u *name;
6828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006829 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006831 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006832 if (argvars[1].v_type != VAR_UNKNOWN)
6833 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006834 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006836 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837 }
6838 if (!error && name != NULL)
6839 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6840 locally, thisblock, SEARCH_KEEP) == FAIL;
6841}
6842
6843/*
6844 * Used by searchpair() and searchpairpos()
6845 */
6846 static int
6847searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6848{
6849 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006850 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006851 int save_p_ws = p_ws;
6852 int dir;
6853 int flags = 0;
6854 char_u nbuf1[NUMBUFLEN];
6855 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006856 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 long lnum_stop = 0;
6858 long time_limit = 0;
6859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006860 // Get the three pattern arguments: start, middle, end. Will result in an
6861 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006862 spat = tv_get_string_chk(&argvars[0]);
6863 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6864 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006866 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006868 // Handle the optional fourth argument: flags
6869 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 if (dir == 0)
6871 goto theend;
6872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006873 // Don't accept SP_END or SP_SUBPAT.
6874 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 if ((flags & (SP_END | SP_SUBPAT)) != 0
6876 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6877 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006878 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006879 goto theend;
6880 }
6881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006882 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006883 if (flags & SP_REPEAT)
6884 p_ws = FALSE;
6885
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006886 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 if (argvars[3].v_type == VAR_UNKNOWN
6888 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006889 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 else
6891 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006892 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006893 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 if (argvars[5].v_type != VAR_UNKNOWN)
6896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006897 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006900 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006902 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903#ifdef FEAT_RELTIME
6904 if (argvars[6].v_type != VAR_UNKNOWN)
6905 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006906 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006909 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006911 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 }
6913#endif
6914 }
6915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916
6917 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6918 match_pos, lnum_stop, time_limit);
6919
6920theend:
6921 p_ws = save_p_ws;
6922
6923 return retval;
6924}
6925
6926/*
6927 * "searchpair()" function
6928 */
6929 static void
6930f_searchpair(typval_T *argvars, typval_T *rettv)
6931{
6932 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6933}
6934
6935/*
6936 * "searchpairpos()" function
6937 */
6938 static void
6939f_searchpairpos(typval_T *argvars, typval_T *rettv)
6940{
6941 pos_T match_pos;
6942 int lnum = 0;
6943 int col = 0;
6944
6945 if (rettv_list_alloc(rettv) == FAIL)
6946 return;
6947
6948 if (searchpair_cmn(argvars, &match_pos) > 0)
6949 {
6950 lnum = match_pos.lnum;
6951 col = match_pos.col;
6952 }
6953
6954 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6955 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6956}
6957
6958/*
6959 * Search for a start/middle/end thing.
6960 * Used by searchpair(), see its documentation for the details.
6961 * Returns 0 or -1 for no match,
6962 */
6963 long
6964do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006965 char_u *spat, // start pattern
6966 char_u *mpat, // middle pattern
6967 char_u *epat, // end pattern
6968 int dir, // BACKWARD or FORWARD
6969 typval_T *skip, // skip expression
6970 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 linenr_T lnum_stop, // stop at this line if not zero
6973 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974{
6975 char_u *save_cpo;
6976 char_u *pat, *pat2 = NULL, *pat3 = NULL;
6977 long retval = 0;
6978 pos_T pos;
6979 pos_T firstpos;
6980 pos_T foundpos;
6981 pos_T save_cursor;
6982 pos_T save_pos;
6983 int n;
6984 int r;
6985 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01006986 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 int err;
6988 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006989#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006991#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006993 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 save_cpo = p_cpo;
6995 p_cpo = empty_option;
6996
6997#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006998 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999 profile_setlimit(time_limit, &tm);
7000#endif
7001
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007002 // Make two search patterns: start/end (pat2, for in nested pairs) and
7003 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007004 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7005 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 if (pat2 == NULL || pat3 == NULL)
7007 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007008 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009 if (*mpat == NUL)
7010 STRCPY(pat3, pat2);
7011 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007012 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013 spat, epat, mpat);
7014 if (flags & SP_START)
7015 options |= SEARCH_START;
7016
Bram Moolenaar48570482017-10-30 21:48:41 +01007017 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007018 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 save_cursor = curwin->w_cursor;
7021 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007022 CLEAR_POS(&firstpos);
7023 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 pat = pat3;
7025 for (;;)
7026 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007027 searchit_arg_T sia;
7028
Bram Moolenaara80faa82020-04-12 19:37:17 +02007029 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007030 sia.sa_stop_lnum = lnum_stop;
7031#ifdef FEAT_RELTIME
7032 sia.sa_tm = &tm;
7033#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007034 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007035 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007036 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007037 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 break;
7039
7040 if (firstpos.lnum == 0)
7041 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007042 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007044 // Found the same position again. Can happen with a pattern that
7045 // has "\zs" at the end and searching backwards. Advance one
7046 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 if (dir == BACKWARD)
7048 decl(&pos);
7049 else
7050 incl(&pos);
7051 }
7052 foundpos = pos;
7053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007054 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 options &= ~SEARCH_START;
7056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007057 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007058 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 {
7060 save_pos = curwin->w_cursor;
7061 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007062 err = FALSE;
7063 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 curwin->w_cursor = save_pos;
7065 if (err)
7066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007067 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 curwin->w_cursor = save_cursor;
7069 retval = -1;
7070 break;
7071 }
7072 if (r)
7073 continue;
7074 }
7075
7076 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007078 // Found end when searching backwards or start when searching
7079 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007081 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007082 }
7083 else
7084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007085 // Found end when searching forward or start when searching
7086 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007087 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007088 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007089 }
7090
7091 if (nest == 0)
7092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007093 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 if (flags & SP_RETCOUNT)
7095 ++retval;
7096 else
7097 retval = pos.lnum;
7098 if (flags & SP_SETPCMARK)
7099 setpcmark();
7100 curwin->w_cursor = pos;
7101 if (!(flags & SP_REPEAT))
7102 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007103 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 }
7105 }
7106
7107 if (match_pos != NULL)
7108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007109 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 match_pos->lnum = curwin->w_cursor.lnum;
7111 match_pos->col = curwin->w_cursor.col + 1;
7112 }
7113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 if ((flags & SP_NOMOVE) || retval == 0)
7116 curwin->w_cursor = save_cursor;
7117
7118theend:
7119 vim_free(pat2);
7120 vim_free(pat3);
7121 if (p_cpo == empty_option)
7122 p_cpo = save_cpo;
7123 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007124 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 free_string_option(save_cpo);
7126
7127 return retval;
7128}
7129
7130/*
7131 * "searchpos()" function
7132 */
7133 static void
7134f_searchpos(typval_T *argvars, typval_T *rettv)
7135{
7136 pos_T match_pos;
7137 int lnum = 0;
7138 int col = 0;
7139 int n;
7140 int flags = 0;
7141
7142 if (rettv_list_alloc(rettv) == FAIL)
7143 return;
7144
7145 n = search_cmn(argvars, &match_pos, &flags);
7146 if (n > 0)
7147 {
7148 lnum = match_pos.lnum;
7149 col = match_pos.col;
7150 }
7151
7152 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7153 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7154 if (flags & SP_SUBPAT)
7155 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7156}
7157
7158 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7160{
7161 dict_T *d;
7162 dictitem_T *di;
7163 char_u *csearch;
7164
7165 if (argvars[0].v_type != VAR_DICT)
7166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007167 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 return;
7169 }
7170
7171 if ((d = argvars[0].vval.v_dict) != NULL)
7172 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007173 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 if (csearch != NULL)
7175 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007176 if (enc_utf8)
7177 {
7178 int pcc[MAX_MCO];
7179 int c = utfc_ptr2char(csearch, pcc);
7180
7181 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7182 }
7183 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007185 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007186 }
7187
7188 di = dict_find(d, (char_u *)"forward", -1);
7189 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007190 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007191 ? FORWARD : BACKWARD);
7192
7193 di = dict_find(d, (char_u *)"until", -1);
7194 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 }
7197}
7198
7199/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007200 * "setenv()" function
7201 */
7202 static void
7203f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7204{
7205 char_u namebuf[NUMBUFLEN];
7206 char_u valbuf[NUMBUFLEN];
7207 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7208
7209 if (argvars[1].v_type == VAR_SPECIAL
7210 && argvars[1].vval.v_number == VVAL_NULL)
7211 vim_unsetenv(name);
7212 else
7213 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7214}
7215
7216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 * "setfperm({fname}, {mode})" function
7218 */
7219 static void
7220f_setfperm(typval_T *argvars, typval_T *rettv)
7221{
7222 char_u *fname;
7223 char_u modebuf[NUMBUFLEN];
7224 char_u *mode_str;
7225 int i;
7226 int mask;
7227 int mode = 0;
7228
7229 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007230 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 if (fname == NULL)
7232 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007233 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007234 if (mode_str == NULL)
7235 return;
7236 if (STRLEN(mode_str) != 9)
7237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007238 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 return;
7240 }
7241
7242 mask = 1;
7243 for (i = 8; i >= 0; --i)
7244 {
7245 if (mode_str[i] != '-')
7246 mode |= mask;
7247 mask = mask << 1;
7248 }
7249 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7250}
7251
7252/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 * "setpos()" function
7254 */
7255 static void
7256f_setpos(typval_T *argvars, typval_T *rettv)
7257{
7258 pos_T pos;
7259 int fnum;
7260 char_u *name;
7261 colnr_T curswant = -1;
7262
7263 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007264 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007265 if (name != NULL)
7266 {
7267 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7268 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007269 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 pos.col = 0;
7271 if (name[0] == '.' && name[1] == NUL)
7272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007273 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007274 curwin->w_cursor = pos;
7275 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007277 curwin->w_curswant = curswant - 1;
7278 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007279 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007280 check_cursor();
7281 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 }
7283 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007285 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 if (setmark_pos(name[1], &pos, fnum) == OK)
7287 rettv->vval.v_number = 0;
7288 }
7289 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007290 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 }
7292 }
7293}
7294
7295/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007296 * Translate a register type string to the yank type and block length
7297 */
7298 static int
7299get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7300{
7301 char_u *stropt = *pp;
7302 switch (*stropt)
7303 {
7304 case 'v': case 'c': // character-wise selection
7305 *yank_type = MCHAR;
7306 break;
7307 case 'V': case 'l': // line-wise selection
7308 *yank_type = MLINE;
7309 break;
7310 case 'b': case Ctrl_V: // block-wise selection
7311 *yank_type = MBLOCK;
7312 if (VIM_ISDIGIT(stropt[1]))
7313 {
7314 ++stropt;
7315 *block_len = getdigits(&stropt) - 1;
7316 --stropt;
7317 }
7318 break;
7319 default:
7320 return FAIL;
7321 }
7322 *pp = stropt;
7323 return OK;
7324}
7325
7326/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 * "setreg()" function
7328 */
7329 static void
7330f_setreg(typval_T *argvars, typval_T *rettv)
7331{
7332 int regname;
7333 char_u *strregname;
7334 char_u *stropt;
7335 char_u *strval;
7336 int append;
7337 char_u yank_type;
7338 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007339 typval_T *regcontents;
7340 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007342 pointreg = 0;
7343 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 block_len = -1;
7345 yank_type = MAUTO;
7346 append = FALSE;
7347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007348 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007349 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350
7351 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007352 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007353 regname = *strregname;
7354 if (regname == 0 || regname == '@')
7355 regname = '"';
7356
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007357 if (argvars[1].v_type == VAR_DICT)
7358 {
7359 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007360 dictitem_T *di;
7361
7362 if (d == NULL || d->dv_hashtab.ht_used == 0)
7363 {
7364 // Empty dict, clear the register (like setreg(0, []))
7365 char_u *lstval[2] = {NULL, NULL};
7366 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7367 return;
7368 }
7369
7370 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007371 if (di != NULL)
7372 regcontents = &di->di_tv;
7373
7374 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7375 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007376 {
7377 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7378
7379 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007380 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007381 semsg(_(e_invargval), "value");
7382 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007383 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007384 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007385
7386 if (regname == '"')
7387 {
7388 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7389 if (stropt != NULL)
7390 {
7391 pointreg = *stropt;
7392 regname = pointreg;
7393 }
7394 }
7395 else if (dict_get_number(d, (char_u *)"isunnamed"))
7396 pointreg = regname;
7397 }
7398 else
7399 regcontents = &argvars[1];
7400
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401 if (argvars[2].v_type != VAR_UNKNOWN)
7402 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007403 if (yank_type != MAUTO)
7404 {
7405 semsg(_(e_toomanyarg), "setreg");
7406 return;
7407 }
7408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007409 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007411 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 for (; *stropt != NUL; ++stropt)
7413 switch (*stropt)
7414 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007415 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007416 append = TRUE;
7417 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007418 default:
7419 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007420 }
7421 }
7422
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007423 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007424 {
7425 char_u **lstval;
7426 char_u **allocval;
7427 char_u buf[NUMBUFLEN];
7428 char_u **curval;
7429 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007430 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 listitem_T *li;
7432 int len;
7433
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007434 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 len = ll == NULL ? 0 : ll->lv_len;
7436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007437 // First half: use for pointers to result lines; second half: use for
7438 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007439 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440 if (lstval == NULL)
7441 return;
7442 curval = lstval;
7443 allocval = lstval + len + 2;
7444 curallocval = allocval;
7445
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007446 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007448 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007449 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007451 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 if (strval == NULL)
7453 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007454 if (strval == buf)
7455 {
7456 // Need to make a copy, next tv_get_string_buf_chk() will
7457 // overwrite the string.
7458 strval = vim_strsave(buf);
7459 if (strval == NULL)
7460 goto free_lstval;
7461 *curallocval++ = strval;
7462 }
7463 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465 }
7466 *curval++ = NULL;
7467
7468 write_reg_contents_lst(regname, lstval, -1,
7469 append, yank_type, block_len);
7470free_lstval:
7471 while (curallocval > allocval)
7472 vim_free(*--curallocval);
7473 vim_free(lstval);
7474 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007475 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007477 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 if (strval == NULL)
7479 return;
7480 write_reg_contents_ex(regname, strval, -1,
7481 append, yank_type, block_len);
7482 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007483 if (pointreg != 0)
7484 get_yank_register(pointreg, TRUE);
7485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 rettv->vval.v_number = 0;
7487}
7488
7489/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007490 * "settagstack()" function
7491 */
7492 static void
7493f_settagstack(typval_T *argvars, typval_T *rettv)
7494{
7495 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7496 win_T *wp;
7497 dict_T *d;
7498 int action = 'r';
7499
7500 rettv->vval.v_number = -1;
7501
7502 // first argument: window number or id
7503 wp = find_win_by_nr_or_id(&argvars[0]);
7504 if (wp == NULL)
7505 return;
7506
7507 // second argument: dict with items to set in the tag stack
7508 if (argvars[1].v_type != VAR_DICT)
7509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007510 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007511 return;
7512 }
7513 d = argvars[1].vval.v_dict;
7514 if (d == NULL)
7515 return;
7516
7517 // third argument: action - 'a' for append and 'r' for replace.
7518 // default is to replace the stack.
7519 if (argvars[2].v_type == VAR_UNKNOWN)
7520 action = 'r';
7521 else if (argvars[2].v_type == VAR_STRING)
7522 {
7523 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007524 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007525 if (actstr == NULL)
7526 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007527 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7528 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007529 action = *actstr;
7530 else
7531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007532 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007533 return;
7534 }
7535 }
7536 else
7537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007538 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007539 return;
7540 }
7541
7542 if (set_tagstack(wp, d, action) == OK)
7543 rettv->vval.v_number = 0;
7544}
7545
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007546#ifdef FEAT_CRYPT
7547/*
7548 * "sha256({string})" function
7549 */
7550 static void
7551f_sha256(typval_T *argvars, typval_T *rettv)
7552{
7553 char_u *p;
7554
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 rettv->vval.v_string = vim_strsave(
7557 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7558 rettv->v_type = VAR_STRING;
7559}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007560#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561
7562/*
7563 * "shellescape({string})" function
7564 */
7565 static void
7566f_shellescape(typval_T *argvars, typval_T *rettv)
7567{
Bram Moolenaar20615522017-06-05 18:46:26 +02007568 int do_special = non_zero_arg(&argvars[1]);
7569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007570 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 rettv->v_type = VAR_STRING;
7573}
7574
7575/*
7576 * shiftwidth() function
7577 */
7578 static void
7579f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7580{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007581 rettv->vval.v_number = 0;
7582
7583 if (argvars[0].v_type != VAR_UNKNOWN)
7584 {
7585 long col;
7586
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007587 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007588 if (col < 0)
7589 return; // type error; errmsg already given
7590#ifdef FEAT_VARTABS
7591 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7592 return;
7593#endif
7594 }
7595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 rettv->vval.v_number = get_sw_value(curbuf);
7597}
7598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599#ifdef FEAT_FLOAT
7600/*
7601 * "sin()" function
7602 */
7603 static void
7604f_sin(typval_T *argvars, typval_T *rettv)
7605{
7606 float_T f = 0.0;
7607
7608 rettv->v_type = VAR_FLOAT;
7609 if (get_float_arg(argvars, &f) == OK)
7610 rettv->vval.v_float = sin(f);
7611 else
7612 rettv->vval.v_float = 0.0;
7613}
7614
7615/*
7616 * "sinh()" function
7617 */
7618 static void
7619f_sinh(typval_T *argvars, typval_T *rettv)
7620{
7621 float_T f = 0.0;
7622
7623 rettv->v_type = VAR_FLOAT;
7624 if (get_float_arg(argvars, &f) == OK)
7625 rettv->vval.v_float = sinh(f);
7626 else
7627 rettv->vval.v_float = 0.0;
7628}
7629#endif
7630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631/*
7632 * "soundfold({word})" function
7633 */
7634 static void
7635f_soundfold(typval_T *argvars, typval_T *rettv)
7636{
7637 char_u *s;
7638
7639 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007640 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641#ifdef FEAT_SPELL
7642 rettv->vval.v_string = eval_soundfold(s);
7643#else
7644 rettv->vval.v_string = vim_strsave(s);
7645#endif
7646}
7647
7648/*
7649 * "spellbadword()" function
7650 */
7651 static void
7652f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7653{
7654 char_u *word = (char_u *)"";
7655 hlf_T attr = HLF_COUNT;
7656 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007657#ifdef FEAT_SPELL
7658 int wo_spell_save = curwin->w_p_spell;
7659
7660 if (!curwin->w_p_spell)
7661 {
7662 did_set_spelllang(curwin);
7663 curwin->w_p_spell = TRUE;
7664 }
7665
7666 if (*curwin->w_s->b_p_spl == NUL)
7667 {
7668 emsg(_(e_no_spell));
7669 curwin->w_p_spell = wo_spell_save;
7670 return;
7671 }
7672#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673
7674 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007675 {
7676#ifdef FEAT_SPELL
7677 curwin->w_p_spell = wo_spell_save;
7678#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007680 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681
7682#ifdef FEAT_SPELL
7683 if (argvars[0].v_type == VAR_UNKNOWN)
7684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007685 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007686 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7687 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007690 curwin->w_set_curswant = TRUE;
7691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007693 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007695 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696 int capcol = -1;
7697
7698 if (str != NULL)
7699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007700 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 while (*str != NUL)
7702 {
7703 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7704 if (attr != HLF_COUNT)
7705 {
7706 word = str;
7707 break;
7708 }
7709 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007710 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007711 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 }
7713 }
7714 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007715 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716#endif
7717
7718 list_append_string(rettv->vval.v_list, word, len);
7719 list_append_string(rettv->vval.v_list, (char_u *)(
7720 attr == HLF_SPB ? "bad" :
7721 attr == HLF_SPR ? "rare" :
7722 attr == HLF_SPL ? "local" :
7723 attr == HLF_SPC ? "caps" :
7724 ""), -1);
7725}
7726
7727/*
7728 * "spellsuggest()" function
7729 */
7730 static void
7731f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7732{
7733#ifdef FEAT_SPELL
7734 char_u *str;
7735 int typeerr = FALSE;
7736 int maxcount;
7737 garray_T ga;
7738 int i;
7739 listitem_T *li;
7740 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007741 int wo_spell_save = curwin->w_p_spell;
7742
7743 if (!curwin->w_p_spell)
7744 {
7745 did_set_spelllang(curwin);
7746 curwin->w_p_spell = TRUE;
7747 }
7748
7749 if (*curwin->w_s->b_p_spl == NUL)
7750 {
7751 emsg(_(e_no_spell));
7752 curwin->w_p_spell = wo_spell_save;
7753 return;
7754 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755#endif
7756
7757 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007758 {
7759#ifdef FEAT_SPELL
7760 curwin->w_p_spell = wo_spell_save;
7761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007763 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764
7765#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007766 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007768 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 if (argvars[1].v_type != VAR_UNKNOWN)
7770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007771 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 if (maxcount <= 0)
7773 return;
7774 if (argvars[2].v_type != VAR_UNKNOWN)
7775 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 if (typeerr)
7778 return;
7779 }
7780 }
7781 else
7782 maxcount = 25;
7783
7784 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7785
7786 for (i = 0; i < ga.ga_len; ++i)
7787 {
7788 str = ((char_u **)ga.ga_data)[i];
7789
7790 li = listitem_alloc();
7791 if (li == NULL)
7792 vim_free(str);
7793 else
7794 {
7795 li->li_tv.v_type = VAR_STRING;
7796 li->li_tv.v_lock = 0;
7797 li->li_tv.vval.v_string = str;
7798 list_append(rettv->vval.v_list, li);
7799 }
7800 }
7801 ga_clear(&ga);
7802 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007803 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804#endif
7805}
7806
7807 static void
7808f_split(typval_T *argvars, typval_T *rettv)
7809{
7810 char_u *str;
7811 char_u *end;
7812 char_u *pat = NULL;
7813 regmatch_T regmatch;
7814 char_u patbuf[NUMBUFLEN];
7815 char_u *save_cpo;
7816 int match;
7817 colnr_T col = 0;
7818 int keepempty = FALSE;
7819 int typeerr = FALSE;
7820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007821 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 save_cpo = p_cpo;
7823 p_cpo = (char_u *)"";
7824
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007825 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 if (argvars[1].v_type != VAR_UNKNOWN)
7827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007828 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 if (pat == NULL)
7830 typeerr = TRUE;
7831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007832 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833 }
7834 if (pat == NULL || *pat == NUL)
7835 pat = (char_u *)"[\\x01- ]\\+";
7836
7837 if (rettv_list_alloc(rettv) == FAIL)
7838 return;
7839 if (typeerr)
7840 return;
7841
7842 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7843 if (regmatch.regprog != NULL)
7844 {
7845 regmatch.rm_ic = FALSE;
7846 while (*str != NUL || keepempty)
7847 {
7848 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007849 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 else
7851 match = vim_regexec_nl(&regmatch, str, col);
7852 if (match)
7853 end = regmatch.startp[0];
7854 else
7855 end = str + STRLEN(str);
7856 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7857 && *str != NUL && match && end < regmatch.endp[0]))
7858 {
7859 if (list_append_string(rettv->vval.v_list, str,
7860 (int)(end - str)) == FAIL)
7861 break;
7862 }
7863 if (!match)
7864 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007865 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 if (regmatch.endp[0] > str)
7867 col = 0;
7868 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007869 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007871 str = regmatch.endp[0];
7872 }
7873
7874 vim_regfree(regmatch.regprog);
7875 }
7876
7877 p_cpo = save_cpo;
7878}
7879
7880#ifdef FEAT_FLOAT
7881/*
7882 * "sqrt()" function
7883 */
7884 static void
7885f_sqrt(typval_T *argvars, typval_T *rettv)
7886{
7887 float_T f = 0.0;
7888
7889 rettv->v_type = VAR_FLOAT;
7890 if (get_float_arg(argvars, &f) == OK)
7891 rettv->vval.v_float = sqrt(f);
7892 else
7893 rettv->vval.v_float = 0.0;
7894}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007895#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007896
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007897#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007898/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 * "str2float()" function
7900 */
7901 static void
7902f_str2float(typval_T *argvars, typval_T *rettv)
7903{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007904 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007905 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906
Bram Moolenaar08243d22017-01-10 16:12:29 +01007907 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 p = skipwhite(p + 1);
7909 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007910 if (isneg)
7911 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 rettv->v_type = VAR_FLOAT;
7913}
7914#endif
7915
7916/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007917 * "str2list()" function
7918 */
7919 static void
7920f_str2list(typval_T *argvars, typval_T *rettv)
7921{
7922 char_u *p;
7923 int utf8 = FALSE;
7924
7925 if (rettv_list_alloc(rettv) == FAIL)
7926 return;
7927
7928 if (argvars[1].v_type != VAR_UNKNOWN)
7929 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7930
7931 p = tv_get_string(&argvars[0]);
7932
7933 if (has_mbyte || utf8)
7934 {
7935 int (*ptr2len)(char_u *);
7936 int (*ptr2char)(char_u *);
7937
7938 if (utf8 || enc_utf8)
7939 {
7940 ptr2len = utf_ptr2len;
7941 ptr2char = utf_ptr2char;
7942 }
7943 else
7944 {
7945 ptr2len = mb_ptr2len;
7946 ptr2char = mb_ptr2char;
7947 }
7948
7949 for ( ; *p != NUL; p += (*ptr2len)(p))
7950 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7951 }
7952 else
7953 for ( ; *p != NUL; ++p)
7954 list_append_number(rettv->vval.v_list, *p);
7955}
7956
7957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 * "str2nr()" function
7959 */
7960 static void
7961f_str2nr(typval_T *argvars, typval_T *rettv)
7962{
7963 int base = 10;
7964 char_u *p;
7965 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007966 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01007967 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968
7969 if (argvars[1].v_type != VAR_UNKNOWN)
7970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007971 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 if (base != 2 && base != 8 && base != 10 && base != 16)
7973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007974 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 return;
7976 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007977 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
7978 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979 }
7980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007981 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007982 isneg = (*p == '-');
7983 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 p = skipwhite(p + 1);
7985 switch (base)
7986 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007987 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02007988 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02007989 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02007991 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
7992 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01007993 if (isneg)
7994 rettv->vval.v_number = -n;
7995 else
7996 rettv->vval.v_number = n;
7997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998}
7999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000/*
8001 * "strgetchar()" function
8002 */
8003 static void
8004f_strgetchar(typval_T *argvars, typval_T *rettv)
8005{
8006 char_u *str;
8007 int len;
8008 int error = FALSE;
8009 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008010 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011
8012 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008013 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 if (str == NULL)
8015 return;
8016 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008017 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 if (error)
8019 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020
Bram Moolenaar13505972019-01-24 15:04:48 +01008021 while (charidx >= 0 && byteidx < len)
8022 {
8023 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008025 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8026 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008028 --charidx;
8029 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031}
8032
8033/*
8034 * "stridx()" function
8035 */
8036 static void
8037f_stridx(typval_T *argvars, typval_T *rettv)
8038{
8039 char_u buf[NUMBUFLEN];
8040 char_u *needle;
8041 char_u *haystack;
8042 char_u *save_haystack;
8043 char_u *pos;
8044 int start_idx;
8045
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008046 needle = tv_get_string_chk(&argvars[1]);
8047 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 rettv->vval.v_number = -1;
8049 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008050 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051
8052 if (argvars[2].v_type != VAR_UNKNOWN)
8053 {
8054 int error = FALSE;
8055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008056 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 if (error || start_idx >= (int)STRLEN(haystack))
8058 return;
8059 if (start_idx >= 0)
8060 haystack += start_idx;
8061 }
8062
8063 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8064 if (pos != NULL)
8065 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8066}
8067
8068/*
8069 * "string()" function
8070 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008071 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072f_string(typval_T *argvars, typval_T *rettv)
8073{
8074 char_u *tofree;
8075 char_u numbuf[NUMBUFLEN];
8076
8077 rettv->v_type = VAR_STRING;
8078 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8079 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008080 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 if (rettv->vval.v_string != NULL && tofree == NULL)
8082 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8083}
8084
8085/*
8086 * "strlen()" function
8087 */
8088 static void
8089f_strlen(typval_T *argvars, typval_T *rettv)
8090{
8091 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008092 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093}
8094
8095/*
8096 * "strchars()" function
8097 */
8098 static void
8099f_strchars(typval_T *argvars, typval_T *rettv)
8100{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008101 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008103 varnumber_T len = 0;
8104 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105
8106 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008107 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008109 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 else
8111 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8113 while (*s != NUL)
8114 {
8115 func_mb_ptr2char_adv(&s);
8116 ++len;
8117 }
8118 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008119 }
8120}
8121
8122/*
8123 * "strdisplaywidth()" function
8124 */
8125 static void
8126f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8127{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008128 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129 int col = 0;
8130
8131 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008132 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133
8134 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8135}
8136
8137/*
8138 * "strwidth()" function
8139 */
8140 static void
8141f_strwidth(typval_T *argvars, typval_T *rettv)
8142{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008143 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144
Bram Moolenaar13505972019-01-24 15:04:48 +01008145 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146}
8147
8148/*
8149 * "strcharpart()" function
8150 */
8151 static void
8152f_strcharpart(typval_T *argvars, typval_T *rettv)
8153{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 char_u *p;
8155 int nchar;
8156 int nbyte = 0;
8157 int charlen;
8158 int len = 0;
8159 int slen;
8160 int error = FALSE;
8161
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 slen = (int)STRLEN(p);
8164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008165 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 if (!error)
8167 {
8168 if (nchar > 0)
8169 while (nchar > 0 && nbyte < slen)
8170 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008171 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 --nchar;
8173 }
8174 else
8175 nbyte = nchar;
8176 if (argvars[2].v_type != VAR_UNKNOWN)
8177 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008178 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 while (charlen > 0 && nbyte + len < slen)
8180 {
8181 int off = nbyte + len;
8182
8183 if (off < 0)
8184 len += 1;
8185 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008186 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 --charlen;
8188 }
8189 }
8190 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 }
8193
8194 /*
8195 * Only return the overlap between the specified part and the actual
8196 * string.
8197 */
8198 if (nbyte < 0)
8199 {
8200 len += nbyte;
8201 nbyte = 0;
8202 }
8203 else if (nbyte > slen)
8204 nbyte = slen;
8205 if (len < 0)
8206 len = 0;
8207 else if (nbyte + len > slen)
8208 len = slen - nbyte;
8209
8210 rettv->v_type = VAR_STRING;
8211 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212}
8213
8214/*
8215 * "strpart()" function
8216 */
8217 static void
8218f_strpart(typval_T *argvars, typval_T *rettv)
8219{
8220 char_u *p;
8221 int n;
8222 int len;
8223 int slen;
8224 int error = FALSE;
8225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008226 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227 slen = (int)STRLEN(p);
8228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008229 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 if (error)
8231 len = 0;
8232 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008233 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008235 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236
8237 /*
8238 * Only return the overlap between the specified part and the actual
8239 * string.
8240 */
8241 if (n < 0)
8242 {
8243 len += n;
8244 n = 0;
8245 }
8246 else if (n > slen)
8247 n = slen;
8248 if (len < 0)
8249 len = 0;
8250 else if (n + len > slen)
8251 len = slen - n;
8252
8253 rettv->v_type = VAR_STRING;
8254 rettv->vval.v_string = vim_strnsave(p + n, len);
8255}
8256
8257/*
8258 * "strridx()" function
8259 */
8260 static void
8261f_strridx(typval_T *argvars, typval_T *rettv)
8262{
8263 char_u buf[NUMBUFLEN];
8264 char_u *needle;
8265 char_u *haystack;
8266 char_u *rest;
8267 char_u *lastmatch = NULL;
8268 int haystack_len, end_idx;
8269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008270 needle = tv_get_string_chk(&argvars[1]);
8271 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272
8273 rettv->vval.v_number = -1;
8274 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008275 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276
8277 haystack_len = (int)STRLEN(haystack);
8278 if (argvars[2].v_type != VAR_UNKNOWN)
8279 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008280 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008281 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008283 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 }
8285 else
8286 end_idx = haystack_len;
8287
8288 if (*needle == NUL)
8289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008290 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 lastmatch = haystack + end_idx;
8292 }
8293 else
8294 {
8295 for (rest = haystack; *rest != '\0'; ++rest)
8296 {
8297 rest = (char_u *)strstr((char *)rest, (char *)needle);
8298 if (rest == NULL || rest > haystack + end_idx)
8299 break;
8300 lastmatch = rest;
8301 }
8302 }
8303
8304 if (lastmatch == NULL)
8305 rettv->vval.v_number = -1;
8306 else
8307 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8308}
8309
8310/*
8311 * "strtrans()" function
8312 */
8313 static void
8314f_strtrans(typval_T *argvars, typval_T *rettv)
8315{
8316 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008317 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318}
8319
8320/*
8321 * "submatch()" function
8322 */
8323 static void
8324f_submatch(typval_T *argvars, typval_T *rettv)
8325{
8326 int error = FALSE;
8327 int no;
8328 int retList = 0;
8329
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008330 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008331 if (error)
8332 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008333 if (no < 0 || no >= NSUBEXP)
8334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008335 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008336 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 if (error)
8341 return;
8342
8343 if (retList == 0)
8344 {
8345 rettv->v_type = VAR_STRING;
8346 rettv->vval.v_string = reg_submatch(no);
8347 }
8348 else
8349 {
8350 rettv->v_type = VAR_LIST;
8351 rettv->vval.v_list = reg_submatch_list(no);
8352 }
8353}
8354
8355/*
8356 * "substitute()" function
8357 */
8358 static void
8359f_substitute(typval_T *argvars, typval_T *rettv)
8360{
8361 char_u patbuf[NUMBUFLEN];
8362 char_u subbuf[NUMBUFLEN];
8363 char_u flagsbuf[NUMBUFLEN];
8364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008365 char_u *str = tv_get_string_chk(&argvars[0]);
8366 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008367 char_u *sub = NULL;
8368 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008369 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008371 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8372 expr = &argvars[2];
8373 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008377 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8378 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 rettv->vval.v_string = NULL;
8380 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008381 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382}
8383
8384/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008385 * "swapinfo(swap_filename)" function
8386 */
8387 static void
8388f_swapinfo(typval_T *argvars, typval_T *rettv)
8389{
8390 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008391 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008392}
8393
8394/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008395 * "swapname(expr)" function
8396 */
8397 static void
8398f_swapname(typval_T *argvars, typval_T *rettv)
8399{
8400 buf_T *buf;
8401
8402 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008403 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008404 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8405 || buf->b_ml.ml_mfp->mf_fname == NULL)
8406 rettv->vval.v_string = NULL;
8407 else
8408 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8409}
8410
8411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 * "synID(lnum, col, trans)" function
8413 */
8414 static void
8415f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8416{
8417 int id = 0;
8418#ifdef FEAT_SYN_HL
8419 linenr_T lnum;
8420 colnr_T col;
8421 int trans;
8422 int transerr = FALSE;
8423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008424 lnum = tv_get_lnum(argvars); // -1 on type error
8425 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008426 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427
8428 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8429 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8430 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8431#endif
8432
8433 rettv->vval.v_number = id;
8434}
8435
8436/*
8437 * "synIDattr(id, what [, mode])" function
8438 */
8439 static void
8440f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8441{
8442 char_u *p = NULL;
8443#ifdef FEAT_SYN_HL
8444 int id;
8445 char_u *what;
8446 char_u *mode;
8447 char_u modebuf[NUMBUFLEN];
8448 int modec;
8449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008450 id = (int)tv_get_number(&argvars[0]);
8451 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 if (argvars[2].v_type != VAR_UNKNOWN)
8453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008454 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455 modec = TOLOWER_ASC(mode[0]);
8456 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008457 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 }
8459 else
8460 {
8461#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8462 if (USE_24BIT)
8463 modec = 'g';
8464 else
8465#endif
8466 if (t_colors > 1)
8467 modec = 'c';
8468 else
8469 modec = 't';
8470 }
8471
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 switch (TOLOWER_ASC(what[0]))
8473 {
8474 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008475 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008476 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008477 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 p = highlight_has_attr(id, HL_BOLD, modec);
8479 break;
8480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008481 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 p = highlight_color(id, what, modec);
8483 break;
8484
8485 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008486 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008488 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 p = highlight_has_attr(id, HL_ITALIC, modec);
8490 break;
8491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008492 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008493 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494 break;
8495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008496 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 p = highlight_has_attr(id, HL_INVERSE, modec);
8498 break;
8499
8500 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008501 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008503 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008504 else if (TOLOWER_ASC(what[1]) == 't' &&
8505 TOLOWER_ASC(what[2]) == 'r')
8506 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008507 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 p = highlight_has_attr(id, HL_STANDOUT, modec);
8509 break;
8510
8511 case 'u':
8512 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008513 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8515 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008516 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8518 break;
8519 }
8520
8521 if (p != NULL)
8522 p = vim_strsave(p);
8523#endif
8524 rettv->v_type = VAR_STRING;
8525 rettv->vval.v_string = p;
8526}
8527
8528/*
8529 * "synIDtrans(id)" function
8530 */
8531 static void
8532f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8533{
8534 int id;
8535
8536#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008537 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008538
8539 if (id > 0)
8540 id = syn_get_final_id(id);
8541 else
8542#endif
8543 id = 0;
8544
8545 rettv->vval.v_number = id;
8546}
8547
8548/*
8549 * "synconcealed(lnum, col)" function
8550 */
8551 static void
8552f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8553{
8554#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8555 linenr_T lnum;
8556 colnr_T col;
8557 int syntax_flags = 0;
8558 int cchar;
8559 int matchid = 0;
8560 char_u str[NUMBUFLEN];
8561#endif
8562
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008563 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564
8565#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008566 lnum = tv_get_lnum(argvars); // -1 on type error
8567 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568
Bram Moolenaara80faa82020-04-12 19:37:17 +02008569 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570
8571 if (rettv_list_alloc(rettv) != FAIL)
8572 {
8573 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8574 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8575 && curwin->w_p_cole > 0)
8576 {
8577 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8578 syntax_flags = get_syntax_info(&matchid);
8579
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008580 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8582 {
8583 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008584 if (cchar == NUL && curwin->w_p_cole == 1)
8585 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 if (cchar != NUL)
8587 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (has_mbyte)
8589 (*mb_char2bytes)(cchar, str);
8590 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008591 str[0] = cchar;
8592 }
8593 }
8594 }
8595
8596 list_append_number(rettv->vval.v_list,
8597 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008598 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008599 list_append_string(rettv->vval.v_list, str, -1);
8600 list_append_number(rettv->vval.v_list, matchid);
8601 }
8602#endif
8603}
8604
8605/*
8606 * "synstack(lnum, col)" function
8607 */
8608 static void
8609f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8610{
8611#ifdef FEAT_SYN_HL
8612 linenr_T lnum;
8613 colnr_T col;
8614 int i;
8615 int id;
8616#endif
8617
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008618 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619
8620#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008621 lnum = tv_get_lnum(argvars); // -1 on type error
8622 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623
8624 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8625 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8626 && rettv_list_alloc(rettv) != FAIL)
8627 {
8628 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8629 for (i = 0; ; ++i)
8630 {
8631 id = syn_get_stack_item(i);
8632 if (id < 0)
8633 break;
8634 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8635 break;
8636 }
8637 }
8638#endif
8639}
8640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641/*
8642 * "tabpagebuflist()" function
8643 */
8644 static void
8645f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8646{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 tabpage_T *tp;
8648 win_T *wp = NULL;
8649
8650 if (argvars[0].v_type == VAR_UNKNOWN)
8651 wp = firstwin;
8652 else
8653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008654 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 if (tp != NULL)
8656 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8657 }
8658 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8659 {
8660 for (; wp != NULL; wp = wp->w_next)
8661 if (list_append_number(rettv->vval.v_list,
8662 wp->w_buffer->b_fnum) == FAIL)
8663 break;
8664 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665}
8666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 * "tagfiles()" function
8669 */
8670 static void
8671f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8672{
8673 char_u *fname;
8674 tagname_T tn;
8675 int first;
8676
8677 if (rettv_list_alloc(rettv) == FAIL)
8678 return;
8679 fname = alloc(MAXPATHL);
8680 if (fname == NULL)
8681 return;
8682
8683 for (first = TRUE; ; first = FALSE)
8684 if (get_tagfname(&tn, first, fname) == FAIL
8685 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8686 break;
8687 tagname_free(&tn);
8688 vim_free(fname);
8689}
8690
8691/*
8692 * "taglist()" function
8693 */
8694 static void
8695f_taglist(typval_T *argvars, typval_T *rettv)
8696{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008697 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 char_u *tag_pattern;
8699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008700 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701
8702 rettv->vval.v_number = FALSE;
8703 if (*tag_pattern == NUL)
8704 return;
8705
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008707 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008709 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710}
8711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712#ifdef FEAT_FLOAT
8713/*
8714 * "tan()" function
8715 */
8716 static void
8717f_tan(typval_T *argvars, typval_T *rettv)
8718{
8719 float_T f = 0.0;
8720
8721 rettv->v_type = VAR_FLOAT;
8722 if (get_float_arg(argvars, &f) == OK)
8723 rettv->vval.v_float = tan(f);
8724 else
8725 rettv->vval.v_float = 0.0;
8726}
8727
8728/*
8729 * "tanh()" function
8730 */
8731 static void
8732f_tanh(typval_T *argvars, typval_T *rettv)
8733{
8734 float_T f = 0.0;
8735
8736 rettv->v_type = VAR_FLOAT;
8737 if (get_float_arg(argvars, &f) == OK)
8738 rettv->vval.v_float = tanh(f);
8739 else
8740 rettv->vval.v_float = 0.0;
8741}
8742#endif
8743
8744/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 * "tolower(string)" function
8746 */
8747 static void
8748f_tolower(typval_T *argvars, typval_T *rettv)
8749{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008751 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752}
8753
8754/*
8755 * "toupper(string)" function
8756 */
8757 static void
8758f_toupper(typval_T *argvars, typval_T *rettv)
8759{
8760 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008761 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762}
8763
8764/*
8765 * "tr(string, fromstr, tostr)" function
8766 */
8767 static void
8768f_tr(typval_T *argvars, typval_T *rettv)
8769{
8770 char_u *in_str;
8771 char_u *fromstr;
8772 char_u *tostr;
8773 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 int inlen;
8775 int fromlen;
8776 int tolen;
8777 int idx;
8778 char_u *cpstr;
8779 int cplen;
8780 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 char_u buf[NUMBUFLEN];
8782 char_u buf2[NUMBUFLEN];
8783 garray_T ga;
8784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008785 in_str = tv_get_string(&argvars[0]);
8786 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8787 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008789 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 rettv->v_type = VAR_STRING;
8791 rettv->vval.v_string = NULL;
8792 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008793 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 ga_init2(&ga, (int)sizeof(char), 80);
8795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008797 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 if (STRLEN(fromstr) != STRLEN(tostr))
8799 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008801 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 ga_clear(&ga);
8803 return;
8804 }
8805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008806 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 while (*in_str != NUL)
8808 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 if (has_mbyte)
8810 {
8811 inlen = (*mb_ptr2len)(in_str);
8812 cpstr = in_str;
8813 cplen = inlen;
8814 idx = 0;
8815 for (p = fromstr; *p != NUL; p += fromlen)
8816 {
8817 fromlen = (*mb_ptr2len)(p);
8818 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8819 {
8820 for (p = tostr; *p != NUL; p += tolen)
8821 {
8822 tolen = (*mb_ptr2len)(p);
8823 if (idx-- == 0)
8824 {
8825 cplen = tolen;
8826 cpstr = p;
8827 break;
8828 }
8829 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008830 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 goto error;
8832 break;
8833 }
8834 ++idx;
8835 }
8836
8837 if (first && cpstr == in_str)
8838 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008839 // Check that fromstr and tostr have the same number of
8840 // (multi-byte) characters. Done only once when a character
8841 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 first = FALSE;
8843 for (p = tostr; *p != NUL; p += tolen)
8844 {
8845 tolen = (*mb_ptr2len)(p);
8846 --idx;
8847 }
8848 if (idx != 0)
8849 goto error;
8850 }
8851
8852 (void)ga_grow(&ga, cplen);
8853 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8854 ga.ga_len += cplen;
8855
8856 in_str += inlen;
8857 }
8858 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008860 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861 p = vim_strchr(fromstr, *in_str);
8862 if (p != NULL)
8863 ga_append(&ga, tostr[p - fromstr]);
8864 else
8865 ga_append(&ga, *in_str);
8866 ++in_str;
8867 }
8868 }
8869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008870 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871 (void)ga_grow(&ga, 1);
8872 ga_append(&ga, NUL);
8873
8874 rettv->vval.v_string = ga.ga_data;
8875}
8876
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008877/*
8878 * "trim({expr})" function
8879 */
8880 static void
8881f_trim(typval_T *argvars, typval_T *rettv)
8882{
8883 char_u buf1[NUMBUFLEN];
8884 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008885 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008886 char_u *mask = NULL;
8887 char_u *tail;
8888 char_u *prev;
8889 char_u *p;
8890 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008891 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008892
8893 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008894 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008895 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008896 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008897
8898 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008900 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008901
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008902 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008903 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008904 int error = 0;
8905
8906 // leading or trailing characters to trim
8907 dir = (int)tv_get_number_chk(&argvars[2], &error);
8908 if (error)
8909 return;
8910 if (dir < 0 || dir > 2)
8911 {
8912 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8913 return;
8914 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008915 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008916 }
8917
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008918 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008919 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008920 // Trim leading characters
8921 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008922 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008923 c1 = PTR2CHAR(head);
8924 if (mask == NULL)
8925 {
8926 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008927 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008928 }
8929 else
8930 {
8931 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8932 if (c1 == PTR2CHAR(p))
8933 break;
8934 if (*p == NUL)
8935 break;
8936 }
8937 MB_PTR_ADV(head);
8938 }
8939 }
8940
8941 tail = head + STRLEN(head);
8942 if (dir == 0 || dir == 2)
8943 {
8944 // Trim trailing characters
8945 for (; tail > head; tail = prev)
8946 {
8947 prev = tail;
8948 MB_PTR_BACK(head, prev);
8949 c1 = PTR2CHAR(prev);
8950 if (mask == NULL)
8951 {
8952 if (c1 > ' ' && c1 != 0xa0)
8953 break;
8954 }
8955 else
8956 {
8957 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8958 if (c1 == PTR2CHAR(p))
8959 break;
8960 if (*p == NUL)
8961 break;
8962 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008963 }
8964 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02008965 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008966}
8967
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008968#ifdef FEAT_FLOAT
8969/*
8970 * "trunc({float})" function
8971 */
8972 static void
8973f_trunc(typval_T *argvars, typval_T *rettv)
8974{
8975 float_T f = 0.0;
8976
8977 rettv->v_type = VAR_FLOAT;
8978 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008979 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
8981 else
8982 rettv->vval.v_float = 0.0;
8983}
8984#endif
8985
8986/*
8987 * "type(expr)" function
8988 */
8989 static void
8990f_type(typval_T *argvars, typval_T *rettv)
8991{
8992 int n = -1;
8993
8994 switch (argvars[0].v_type)
8995 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01008996 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
8997 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01008999 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9000 case VAR_LIST: n = VAR_TYPE_LIST; break;
9001 case VAR_DICT: n = VAR_TYPE_DICT; break;
9002 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9003 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9004 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009005 case VAR_JOB: n = VAR_TYPE_JOB; break;
9006 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009007 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009009 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009010 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009011 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 n = -1;
9013 break;
9014 }
9015 rettv->vval.v_number = n;
9016}
9017
9018/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 * "virtcol(string)" function
9020 */
9021 static void
9022f_virtcol(typval_T *argvars, typval_T *rettv)
9023{
9024 colnr_T vcol = 0;
9025 pos_T *fp;
9026 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009027 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028
9029 fp = var2fpos(&argvars[0], FALSE, &fnum);
9030 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9031 && fnum == curbuf->b_fnum)
9032 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009033 // Limit the column to a valid value, getvvcol() doesn't check.
9034 if (fp->col < 0)
9035 fp->col = 0;
9036 else
9037 {
9038 len = (int)STRLEN(ml_get(fp->lnum));
9039 if (fp->col > len)
9040 fp->col = len;
9041 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042 getvvcol(curwin, fp, NULL, NULL, &vcol);
9043 ++vcol;
9044 }
9045
9046 rettv->vval.v_number = vcol;
9047}
9048
9049/*
9050 * "visualmode()" function
9051 */
9052 static void
9053f_visualmode(typval_T *argvars, typval_T *rettv)
9054{
9055 char_u str[2];
9056
9057 rettv->v_type = VAR_STRING;
9058 str[0] = curbuf->b_visual_mode_eval;
9059 str[1] = NUL;
9060 rettv->vval.v_string = vim_strsave(str);
9061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009062 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 if (non_zero_arg(&argvars[0]))
9064 curbuf->b_visual_mode_eval = NUL;
9065}
9066
9067/*
9068 * "wildmenumode()" function
9069 */
9070 static void
9071f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9072{
9073#ifdef FEAT_WILDMENU
9074 if (wild_menu_showing)
9075 rettv->vval.v_number = 1;
9076#endif
9077}
9078
9079/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009080 * "windowsversion()" function
9081 */
9082 static void
9083f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9084{
9085 rettv->v_type = VAR_STRING;
9086 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9087}
9088
9089/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009090 * "wordcount()" function
9091 */
9092 static void
9093f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9094{
9095 if (rettv_dict_alloc(rettv) == FAIL)
9096 return;
9097 cursor_pos_info(rettv->vval.v_dict);
9098}
9099
9100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 * "xor(expr, expr)" function
9102 */
9103 static void
9104f_xor(typval_T *argvars, typval_T *rettv)
9105{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009106 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9107 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108}
9109
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009110#endif // FEAT_EVAL