blob: a421690e499e4eb2ef9f41cb831e57397212fb6b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023#ifdef FEAT_FLOAT
24static void f_abs(typval_T *argvars, typval_T *rettv);
25static void f_acos(typval_T *argvars, typval_T *rettv);
26#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020027static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028#ifdef FEAT_FLOAT
29static void f_asin(typval_T *argvars, typval_T *rettv);
30static void f_atan(typval_T *argvars, typval_T *rettv);
31static void f_atan2(typval_T *argvars, typval_T *rettv);
32#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010033#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020034static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010035static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010036# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010037static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010038# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010039#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_byte2line(typval_T *argvars, typval_T *rettv);
41static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
42static void f_byteidx(typval_T *argvars, typval_T *rettv);
43static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
44static void f_call(typval_T *argvars, typval_T *rettv);
45#ifdef FEAT_FLOAT
46static void f_ceil(typval_T *argvars, typval_T *rettv);
47#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_changenr(typval_T *argvars, typval_T *rettv);
49static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_confirm(typval_T *argvars, typval_T *rettv);
52static void f_copy(typval_T *argvars, typval_T *rettv);
53#ifdef FEAT_FLOAT
54static void f_cos(typval_T *argvars, typval_T *rettv);
55static void f_cosh(typval_T *argvars, typval_T *rettv);
56#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020059static void f_debugbreak(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020062static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010063static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020065static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020066static void f_escape(typval_T *argvars, typval_T *rettv);
67static void f_eval(typval_T *argvars, typval_T *rettv);
68static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_exists(typval_T *argvars, typval_T *rettv);
71#ifdef FEAT_FLOAT
72static void f_exp(typval_T *argvars, typval_T *rettv);
73#endif
74static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020075static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020077#ifdef FEAT_FLOAT
78static void f_float2nr(typval_T *argvars, typval_T *rettv);
79static void f_floor(typval_T *argvars, typval_T *rettv);
80static void f_fmod(typval_T *argvars, typval_T *rettv);
81#endif
82static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020084static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_function(typval_T *argvars, typval_T *rettv);
86static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
87static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010088static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020090static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010092static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_getpid(typval_T *argvars, typval_T *rettv);
94static void f_getcurpos(typval_T *argvars, typval_T *rettv);
95static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020097static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020098static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010099static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
101static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200102static void f_hlID(typval_T *argvars, typval_T *rettv);
103static void f_hlexists(typval_T *argvars, typval_T *rettv);
104static void f_hostname(typval_T *argvars, typval_T *rettv);
105static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_index(typval_T *argvars, typval_T *rettv);
107static void f_input(typval_T *argvars, typval_T *rettv);
108static void f_inputdialog(typval_T *argvars, typval_T *rettv);
109static void f_inputlist(typval_T *argvars, typval_T *rettv);
110static void f_inputrestore(typval_T *argvars, typval_T *rettv);
111static void f_inputsave(typval_T *argvars, typval_T *rettv);
112static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100113static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200114static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_islocked(typval_T *argvars, typval_T *rettv);
116#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200117static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200118static void f_isnan(typval_T *argvars, typval_T *rettv);
119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
121static void f_len(typval_T *argvars, typval_T *rettv);
122static void f_libcall(typval_T *argvars, typval_T *rettv);
123static void f_libcallnr(typval_T *argvars, typval_T *rettv);
124static void f_line(typval_T *argvars, typval_T *rettv);
125static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200126#ifdef FEAT_FLOAT
127static void f_log(typval_T *argvars, typval_T *rettv);
128static void f_log10(typval_T *argvars, typval_T *rettv);
129#endif
130#ifdef FEAT_LUA
131static void f_luaeval(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_maparg(typval_T *argvars, typval_T *rettv);
134static void f_mapcheck(typval_T *argvars, typval_T *rettv);
135static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_matchend(typval_T *argvars, typval_T *rettv);
137static void f_matchlist(typval_T *argvars, typval_T *rettv);
138static void f_matchstr(typval_T *argvars, typval_T *rettv);
139static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
140static void f_max(typval_T *argvars, typval_T *rettv);
141static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142#ifdef FEAT_MZSCHEME
143static void f_mzeval(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
146static void f_nr2char(typval_T *argvars, typval_T *rettv);
147static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200148#ifdef FEAT_PERL
149static void f_perleval(typval_T *argvars, typval_T *rettv);
150#endif
151#ifdef FEAT_FLOAT
152static void f_pow(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
155static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200156static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_pumvisible(typval_T *argvars, typval_T *rettv);
158#ifdef FEAT_PYTHON3
159static void f_py3eval(typval_T *argvars, typval_T *rettv);
160#endif
161#ifdef FEAT_PYTHON
162static void f_pyeval(typval_T *argvars, typval_T *rettv);
163#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
165static void f_pyxeval(typval_T *argvars, typval_T *rettv);
166#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100167static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100168static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200170static void f_reg_executing(typval_T *argvars, typval_T *rettv);
171static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_rename(typval_T *argvars, typval_T *rettv);
173static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174#ifdef FEAT_FLOAT
175static void f_round(typval_T *argvars, typval_T *rettv);
176#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100177#ifdef FEAT_RUBY
178static void f_rubyeval(typval_T *argvars, typval_T *rettv);
179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_screenattr(typval_T *argvars, typval_T *rettv);
181static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100182static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200183static void f_screencol(typval_T *argvars, typval_T *rettv);
184static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100185static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_search(typval_T *argvars, typval_T *rettv);
187static void f_searchdecl(typval_T *argvars, typval_T *rettv);
188static void f_searchpair(typval_T *argvars, typval_T *rettv);
189static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
190static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200191static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100196static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197#ifdef FEAT_CRYPT
198static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_shellescape(typval_T *argvars, typval_T *rettv);
201static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202#ifdef FEAT_FLOAT
203static void f_sin(typval_T *argvars, typval_T *rettv);
204static void f_sinh(typval_T *argvars, typval_T *rettv);
205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_soundfold(typval_T *argvars, typval_T *rettv);
207static void f_spellbadword(typval_T *argvars, typval_T *rettv);
208static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
209static void f_split(typval_T *argvars, typval_T *rettv);
210#ifdef FEAT_FLOAT
211static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100212#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100213static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100214#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200215static void f_str2float(typval_T *argvars, typval_T *rettv);
216#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200217static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_str2nr(typval_T *argvars, typval_T *rettv);
219static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_strgetchar(typval_T *argvars, typval_T *rettv);
221static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200222static void f_strlen(typval_T *argvars, typval_T *rettv);
223static void f_strcharpart(typval_T *argvars, typval_T *rettv);
224static void f_strpart(typval_T *argvars, typval_T *rettv);
225static void f_strridx(typval_T *argvars, typval_T *rettv);
226static void f_strtrans(typval_T *argvars, typval_T *rettv);
227static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
228static void f_strwidth(typval_T *argvars, typval_T *rettv);
229static void f_submatch(typval_T *argvars, typval_T *rettv);
230static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200231static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200232static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200233static void f_synID(typval_T *argvars, typval_T *rettv);
234static void f_synIDattr(typval_T *argvars, typval_T *rettv);
235static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
236static void f_synstack(typval_T *argvars, typval_T *rettv);
237static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200238static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_taglist(typval_T *argvars, typval_T *rettv);
240static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200241#ifdef FEAT_FLOAT
242static void f_tan(typval_T *argvars, typval_T *rettv);
243static void f_tanh(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_tolower(typval_T *argvars, typval_T *rettv);
246static void f_toupper(typval_T *argvars, typval_T *rettv);
247static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100248static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249#ifdef FEAT_FLOAT
250static void f_trunc(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253static void f_virtcol(typval_T *argvars, typval_T *rettv);
254static void f_visualmode(typval_T *argvars, typval_T *rettv);
255static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100256static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200257static void f_wordcount(typval_T *argvars, typval_T *rettv);
258static void f_xor(typval_T *argvars, typval_T *rettv);
259
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100260
261 static type_T *
262ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
263{
264 return &t_void;
265}
266 static type_T *
267ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
268{
269 return &t_any;
270}
271 static type_T *
272ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
273{
274 return &t_number;
275}
276 static type_T *
277ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
278{
279 return &t_float;
280}
281 static type_T *
282ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
283{
284 return &t_string;
285}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200286 static type_T *
287ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100288{
289 return &t_list_any;
290}
291 static type_T *
292ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
293{
294 return &t_list_number;
295}
296 static type_T *
297ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
298{
299 return &t_list_string;
300}
301 static type_T *
302ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
303{
304 return &t_list_dict_any;
305}
306 static type_T *
307ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
308{
309 return &t_dict_any;
310}
311 static type_T *
312ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
313{
314 return &t_dict_number;
315}
316 static type_T *
317ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
318{
319 return &t_dict_string;
320}
321 static type_T *
322ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
323{
324 return &t_blob;
325}
326 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200327ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100328{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200329 return &t_func_any;
330}
331 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100332ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
333{
334 return &t_channel;
335}
336 static type_T *
337ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
338{
339 return &t_job;
340}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100341
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200342 static type_T *
343ret_first_arg(int argcount, type_T **argtypes)
344{
345 if (argcount > 0)
346 return argtypes[0];
347 return &t_void;
348}
349
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200350/*
351 * Used for getqflist(): returns list if there is no argument, dict if there is
352 * one.
353 */
354 static type_T *
355ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
356{
357 if (argcount > 0)
358 return &t_dict_any;
359 return &t_list_dict_any;
360}
361
362/*
363 * Used for getloclist(): returns list if there is one argument, dict if there
364 * are two.
365 */
366 static type_T *
367ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
368{
369 if (argcount > 1)
370 return &t_dict_any;
371 return &t_list_dict_any;
372}
373
Bram Moolenaar846178a2020-07-05 17:04:13 +0200374 static type_T *
375ret_argv(int argcount, type_T **argtypes UNUSED)
376{
377 // argv() returns list of strings
378 if (argcount == 0)
379 return &t_list_string;
380
381 // argv(0) returns a string, but argv(-1] returns a list
382 return &t_any;
383}
384
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200385 static type_T *
386ret_remove(int argcount UNUSED, type_T **argtypes)
387{
388 if (argtypes[0]->tt_type == VAR_LIST
389 || argtypes[0]->tt_type == VAR_DICT)
390 return argtypes[0]->tt_member;
391 if (argtypes[0]->tt_type == VAR_BLOB)
392 return &t_number;
393 return &t_any;
394}
395
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200396 static type_T *
397ret_getreg(int argcount, type_T **argtypes UNUSED)
398{
399 // Assume that if the third argument is passed it's non-zero
400 if (argcount == 3)
401 return &t_list_string;
402 return &t_string;
403}
404
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100405static type_T *ret_f_function(int argcount, type_T **argtypes);
406
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407/*
408 * Array with names and number of arguments of all internal functions
409 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
410 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200411typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200413 char *f_name; // function name
414 char f_min_argc; // minimal number of arguments
415 char f_max_argc; // maximal number of arguments
416 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100417 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
418 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200420 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200421} funcentry_T;
422
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200423// values for f_argtype; zero means it cannot be used as a method
424#define FEARG_1 1 // base is the first argument
425#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200426#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200427#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200428#define FEARG_LAST 9 // base is the last argument
429
Bram Moolenaar15c47602020-03-26 22:16:48 +0100430#ifdef FEAT_FLOAT
431# define FLOAT_FUNC(name) name
432#else
433# define FLOAT_FUNC(name) NULL
434#endif
435#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
436# define MATH_FUNC(name) name
437#else
438# define MATH_FUNC(name) NULL
439#endif
440#ifdef FEAT_TIMERS
441# define TIMER_FUNC(name) name
442#else
443# define TIMER_FUNC(name) NULL
444#endif
445#ifdef FEAT_JOB_CHANNEL
446# define JOB_FUNC(name) name
447#else
448# define JOB_FUNC(name) NULL
449#endif
450#ifdef FEAT_PROP_POPUP
451# define PROP_FUNC(name) name
452#else
453# define PROP_FUNC(name) NULL
454#endif
455#ifdef FEAT_SIGNS
456# define SIGN_FUNC(name) name
457#else
458# define SIGN_FUNC(name) NULL
459#endif
460#ifdef FEAT_SOUND
461# define SOUND_FUNC(name) name
462#else
463# define SOUND_FUNC(name) NULL
464#endif
465#ifdef FEAT_TERMINAL
466# define TERM_FUNC(name) name
467#else
468# define TERM_FUNC(name) NULL
469#endif
470
Bram Moolenaarac92e252019-08-03 21:58:38 +0200471static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200472{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100473 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
474 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200475 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100476 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200477 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200478 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100479 {"argc", 0, 1, 0, ret_number, f_argc},
480 {"argidx", 0, 0, 0, ret_number, f_argidx},
481 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200482 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100483 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100484 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
485 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200486 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100487 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
488 {"assert_fails", 1, 3, FEARG_1, ret_number, f_assert_fails},
489 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
490 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
491 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
492 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
493 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
494 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
495 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100496 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
497 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
498 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100499#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100500 f_balloon_gettext
501#else
502 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100503#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100504 },
505 {"balloon_show", 1, 1, FEARG_1, ret_void,
506#ifdef FEAT_BEVAL
507 f_balloon_show
508#else
509 NULL
510#endif
511 },
512 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
513#if defined(FEAT_BEVAL_TERM)
514 f_balloon_split
515#else
516 NULL
517#endif
518 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100519 {"browse", 4, 4, 0, ret_string, f_browse},
520 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
521 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
522 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
523 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
524 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
525 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
526 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
527 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
528 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
529 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
530 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
531 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
532 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
533 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
534 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
535 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
536 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100537 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
538 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
539 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
540 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
541 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
542 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
543 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
544 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
545 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
546 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
547 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
548 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
549 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
550 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
551 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
552 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
553 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
554 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
555 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100556 {"changenr", 0, 0, 0, ret_number, f_changenr},
557 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
558 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
559 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
560 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
561 {"col", 1, 1, FEARG_1, ret_number, f_col},
562 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
563 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
564 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
565 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
566 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200567 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100568 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
569 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100570 {"count", 2, 4, FEARG_1, ret_number, f_count},
571 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
572 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100573 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100574#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100575 f_debugbreak
576#else
577 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200578#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100579 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200580 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100581 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
582 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
583 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
584 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
585 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
586 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
587 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
588 {"environ", 0, 0, 0, ret_dict_string, f_environ},
589 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
590 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
591 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
592 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
593 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
594 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
595 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100596 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100597 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
598 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200599 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100600 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
601 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
602 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
603 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200604 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100605 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
606 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200607 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100608 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
609 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
610 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
612 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
613 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
614 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
615 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
616 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
617 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
618 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200619 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100620 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
621 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
622 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200623 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100624 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
625 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
626 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
627 {"getchar", 0, 1, 0, ret_number, f_getchar},
628 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
629 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
630 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
631 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
632 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
633 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
634 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
635 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
636 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
637 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
638 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
639 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
640 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
641 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
642 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
643 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
644 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
645 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200646 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200647 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100648 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
649 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
650 {"getpid", 0, 0, 0, ret_number, f_getpid},
651 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200652 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200653 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200654 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100655 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
656 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
657 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
658 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
659 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
660 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
661 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
662 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
663 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
664 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
665 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
666 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
667 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100668 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100669 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
670 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
671 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
672 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
673 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
674 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
675 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
676 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
677 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
678 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
679 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
680 {"hostname", 0, 0, 0, ret_string, f_hostname},
681 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
682 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
683 {"index", 2, 4, FEARG_1, ret_number, f_index},
684 {"input", 1, 3, FEARG_1, ret_string, f_input},
685 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
686 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
687 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
688 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
689 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200690 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100691 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
692 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
693 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100694 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100695 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100696 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100697 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100698 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
699 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
700 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
701 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
702 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
703 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100704 {"join", 1, 2, FEARG_1, ret_string, f_join},
705 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
706 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
707 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
708 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
709 {"keys", 1, 1, FEARG_1, ret_list_any, f_keys},
710 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
711 {"len", 1, 1, FEARG_1, ret_number, f_len},
712 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
713 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
714 {"line", 1, 2, FEARG_1, ret_number, f_line},
715 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
716 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
717 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
718 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
719 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
720 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
721 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100722 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
723 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
724 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200725#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100726 f_luaeval
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 {"map", 2, 2, FEARG_1, ret_any, f_map},
732 {"maparg", 1, 4, FEARG_1, ret_string, f_maparg},
733 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200734 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100735 {"match", 2, 4, FEARG_1, ret_any, f_match},
736 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
737 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
738 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
739 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
740 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
741 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
742 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
743 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
744 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100745 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100746#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100747 f_menu_info
748#else
749 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100750#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100751 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100752 {"min", 1, 1, FEARG_1, ret_any, f_min},
753 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
754 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100755 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200756#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 f_mzeval
758#else
759 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200760#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100761 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100762 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
763 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
764 {"or", 2, 2, FEARG_1, ret_number, f_or},
765 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100766 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100768 f_perleval
769#else
770 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200771#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100772 },
773 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
774 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200775 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100776 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
777 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
778 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
779 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
780 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
781 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
782 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
783 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
784 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
785 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200786 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100787 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
788 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
789 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
790 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
791 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
792 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
793 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
794 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100795 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
796 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100797 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
798 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
799 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
800 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
801 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
802 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
803 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
804 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
805 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
806 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
807 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
808 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
809 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100810 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
811 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100812 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100814 f_py3eval
815#else
816 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200817#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100818 },
819 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200820#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100821 f_pyeval
822#else
823 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100825 },
826 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100827#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100828 f_pyxeval
829#else
830 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100831#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100832 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100833 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
834 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200835 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
836 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100837 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200838 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100839 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
840 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
841 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100843 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
844 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
845 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
846 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
847 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
848 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200849 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200850 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100851 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200852 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100853 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200854 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100855 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
856 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100857#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100858 f_rubyeval
859#else
860 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100861#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100862 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100863 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
864 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
865 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
866 {"screencol", 0, 0, 0, ret_number, f_screencol},
867 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
868 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
869 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200870 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200871 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100872 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
873 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
874 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200875 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100876 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
877 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
878 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
879 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
880 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
881 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
882 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
883 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
884 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
885 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
886 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
887 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
888 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
889 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
890 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
891 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
892 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
893 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100894 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100896 f_sha256
897#else
898 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100900 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100901 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
902 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100903 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
904 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
905 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
906 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
907 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
908 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
909 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
910 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
911 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200912 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100913 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
914 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200915 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100916 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
917 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
918 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
919 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100920 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
921 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
922 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
923 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100924 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100925 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
926 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100927 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100928 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
929 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
930 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
931 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
932 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100933 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100935 f_strftime
936#else
937 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100939 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100940 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
941 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
942 {"string", 1, 1, FEARG_1, ret_string, f_string},
943 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
944 {"strpart", 2, 3, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100945 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100946#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100947 f_strptime
948#else
949 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100950#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100951 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100952 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
953 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
954 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
955 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
956 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
957 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
958 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
959 {"synID", 3, 3, 0, ret_number, f_synID},
960 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
961 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
962 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
963 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
964 {"system", 1, 2, FEARG_1, ret_string, f_system},
965 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
966 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
967 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
968 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
969 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
970 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100971 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
972 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100973 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100974 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
975 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
976 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
977 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
978 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100979#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100980 f_term_getansicolors
981#else
982 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100984 },
985 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
986 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
987 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
988 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
989 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
990 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
991 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
992 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
993 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
994 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
995 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
996 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
997 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100998#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100999 f_term_setansicolors
1000#else
1001 NULL
1002#endif
1003 },
1004 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1005 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1006 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1007 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1008 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1009 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001010 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001011 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1012 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1013 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1014 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1015 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1016 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1017 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1018 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001019 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001020 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001021 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001022 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001023 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001024 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001025 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1026 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1027 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1028 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001029 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001030#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001031 f_test_scrollbar
1032#else
1033 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001034#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001035 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001036 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1037 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1038 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1039 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
1040 {"test_void", 0, 0, 0, ret_any, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001041 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1042 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1043 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1044 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1045 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001046 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1047 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1048 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001049 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001050 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001051 {"type", 1, 1, FEARG_1, ret_number, f_type},
1052 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1053 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1054 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1055 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1056 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1057 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1058 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1059 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1060 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1061 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1062 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1063 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1064 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1065 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1066 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1067 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1068 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1069 {"wincol", 0, 0, 0, ret_number, f_wincol},
1070 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1071 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1072 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1073 {"winline", 0, 0, 0, ret_number, f_winline},
1074 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1075 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1076 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1077 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1078 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1079 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1080 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1081 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001082};
1083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001084/*
1085 * Function given to ExpandGeneric() to obtain the list of internal
1086 * or user defined function names.
1087 */
1088 char_u *
1089get_function_name(expand_T *xp, int idx)
1090{
1091 static int intidx = -1;
1092 char_u *name;
1093
1094 if (idx == 0)
1095 intidx = -1;
1096 if (intidx < 0)
1097 {
1098 name = get_user_func_name(xp, idx);
1099 if (name != NULL)
1100 return name;
1101 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001102 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001103 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001104 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001105 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001106 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001107 STRCAT(IObuff, ")");
1108 return IObuff;
1109 }
1110
1111 return NULL;
1112}
1113
1114/*
1115 * Function given to ExpandGeneric() to obtain the list of internal or
1116 * user defined variable or function names.
1117 */
1118 char_u *
1119get_expr_name(expand_T *xp, int idx)
1120{
1121 static int intidx = -1;
1122 char_u *name;
1123
1124 if (idx == 0)
1125 intidx = -1;
1126 if (intidx < 0)
1127 {
1128 name = get_function_name(xp, idx);
1129 if (name != NULL)
1130 return name;
1131 }
1132 return get_user_var_name(xp, ++intidx);
1133}
1134
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001135/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001136 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001137 * Return index, or -1 if not found or "implemented" is TRUE and the function
1138 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001139 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001140 static int
1141find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001142{
1143 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001144 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001145 int cmp;
1146 int x;
1147
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001148 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001149
1150 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001151 while (first <= last)
1152 {
1153 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001154 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001155 if (cmp < 0)
1156 last = x - 1;
1157 else if (cmp > 0)
1158 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001159 else if (implemented && global_functions[x].f_func == NULL)
1160 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001161 else
1162 return x;
1163 }
1164 return -1;
1165}
1166
Bram Moolenaar15c47602020-03-26 22:16:48 +01001167/*
1168 * Find internal function "name" in table "global_functions".
1169 * Return index, or -1 if not found or the function is not implemented.
1170 */
1171 int
1172find_internal_func(char_u *name)
1173{
1174 return find_internal_func_opt(name, TRUE);
1175}
1176
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001178has_internal_func(char_u *name)
1179{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001180 return find_internal_func_opt(name, TRUE) >= 0;
1181}
1182
1183 static int
1184has_internal_func_name(char_u *name)
1185{
1186 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001187}
1188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 char *
1190internal_func_name(int idx)
1191{
1192 return global_functions[idx].f_name;
1193}
1194
1195 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001196internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001198 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199}
1200
1201/*
1202 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001203 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1204 * first argument, 2 if method base is second argument, etc. 9 if method base
1205 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 */
1207 int
1208check_internal_func(int idx, int argcount)
1209{
1210 int res;
1211 char *name;
1212
1213 if (argcount < global_functions[idx].f_min_argc)
1214 res = FCERR_TOOFEW;
1215 else if (argcount > global_functions[idx].f_max_argc)
1216 res = FCERR_TOOMANY;
1217 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001218 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219
1220 name = internal_func_name(idx);
1221 if (res == FCERR_TOOMANY)
1222 semsg(_(e_toomanyarg), name);
1223 else
1224 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001225 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226}
1227
Bram Moolenaarac92e252019-08-03 21:58:38 +02001228 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229call_internal_func(
1230 char_u *name,
1231 int argcount,
1232 typval_T *argvars,
1233 typval_T *rettv)
1234{
1235 int i;
1236
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001237 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001238 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001239 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001240 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001241 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001242 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001243 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001244 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001245 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001246 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001247}
1248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 void
1250call_internal_func_by_idx(
1251 int idx,
1252 typval_T *argvars,
1253 typval_T *rettv)
1254{
1255 global_functions[idx].f_func(argvars, rettv);
1256}
1257
Bram Moolenaarac92e252019-08-03 21:58:38 +02001258/*
1259 * Invoke a method for base->method().
1260 */
1261 int
1262call_internal_method(
1263 char_u *name,
1264 int argcount,
1265 typval_T *argvars,
1266 typval_T *rettv,
1267 typval_T *basetv)
1268{
1269 int i;
1270 int fi;
1271 typval_T argv[MAX_FUNC_ARGS + 1];
1272
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001273 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001274 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001275 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001276 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001277 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001278 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001279 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001280 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001281 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001282
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001283 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001284 {
1285 // base value goes last
1286 for (i = 0; i < argcount; ++i)
1287 argv[i] = argvars[i];
1288 argv[argcount] = *basetv;
1289 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001290 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001291 {
1292 // base value goes second
1293 argv[0] = argvars[0];
1294 argv[1] = *basetv;
1295 for (i = 1; i < argcount; ++i)
1296 argv[i + 1] = argvars[i];
1297 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001298 else if (global_functions[fi].f_argtype == FEARG_3)
1299 {
1300 // base value goes third
1301 argv[0] = argvars[0];
1302 argv[1] = argvars[1];
1303 argv[2] = *basetv;
1304 for (i = 2; i < argcount; ++i)
1305 argv[i + 1] = argvars[i];
1306 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001307 else if (global_functions[fi].f_argtype == FEARG_4)
1308 {
1309 // base value goes fourth
1310 argv[0] = argvars[0];
1311 argv[1] = argvars[1];
1312 argv[2] = argvars[2];
1313 argv[3] = *basetv;
1314 for (i = 3; i < argcount; ++i)
1315 argv[i + 1] = argvars[i];
1316 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001317 else
1318 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001319 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001320 argv[0] = *basetv;
1321 for (i = 0; i < argcount; ++i)
1322 argv[i + 1] = argvars[i];
1323 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001324 argv[argcount + 1].v_type = VAR_UNKNOWN;
1325
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001326 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001327 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328}
1329
1330/*
1331 * Return TRUE for a non-zero Number and a non-empty String.
1332 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001333 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001334non_zero_arg(typval_T *argvars)
1335{
1336 return ((argvars[0].v_type == VAR_NUMBER
1337 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001338 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339 && argvars[0].vval.v_number == VVAL_TRUE)
1340 || (argvars[0].v_type == VAR_STRING
1341 && argvars[0].vval.v_string != NULL
1342 && *argvars[0].vval.v_string != NUL));
1343}
1344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001346/*
1347 * Get the float value of "argvars[0]" into "f".
1348 * Returns FAIL when the argument is not a Number or Float.
1349 */
1350 static int
1351get_float_arg(typval_T *argvars, float_T *f)
1352{
1353 if (argvars[0].v_type == VAR_FLOAT)
1354 {
1355 *f = argvars[0].vval.v_float;
1356 return OK;
1357 }
1358 if (argvars[0].v_type == VAR_NUMBER)
1359 {
1360 *f = (float_T)argvars[0].vval.v_number;
1361 return OK;
1362 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001363 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 return FAIL;
1365}
1366
1367/*
1368 * "abs(expr)" function
1369 */
1370 static void
1371f_abs(typval_T *argvars, typval_T *rettv)
1372{
1373 if (argvars[0].v_type == VAR_FLOAT)
1374 {
1375 rettv->v_type = VAR_FLOAT;
1376 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1377 }
1378 else
1379 {
1380 varnumber_T n;
1381 int error = FALSE;
1382
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001383 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384 if (error)
1385 rettv->vval.v_number = -1;
1386 else if (n > 0)
1387 rettv->vval.v_number = n;
1388 else
1389 rettv->vval.v_number = -n;
1390 }
1391}
1392
1393/*
1394 * "acos()" function
1395 */
1396 static void
1397f_acos(typval_T *argvars, typval_T *rettv)
1398{
1399 float_T f = 0.0;
1400
1401 rettv->v_type = VAR_FLOAT;
1402 if (get_float_arg(argvars, &f) == OK)
1403 rettv->vval.v_float = acos(f);
1404 else
1405 rettv->vval.v_float = 0.0;
1406}
1407#endif
1408
1409/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001410 * "and(expr, expr)" function
1411 */
1412 static void
1413f_and(typval_T *argvars, typval_T *rettv)
1414{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1416 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001417}
1418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419#ifdef FEAT_FLOAT
1420/*
1421 * "asin()" function
1422 */
1423 static void
1424f_asin(typval_T *argvars, typval_T *rettv)
1425{
1426 float_T f = 0.0;
1427
1428 rettv->v_type = VAR_FLOAT;
1429 if (get_float_arg(argvars, &f) == OK)
1430 rettv->vval.v_float = asin(f);
1431 else
1432 rettv->vval.v_float = 0.0;
1433}
1434
1435/*
1436 * "atan()" function
1437 */
1438 static void
1439f_atan(typval_T *argvars, typval_T *rettv)
1440{
1441 float_T f = 0.0;
1442
1443 rettv->v_type = VAR_FLOAT;
1444 if (get_float_arg(argvars, &f) == OK)
1445 rettv->vval.v_float = atan(f);
1446 else
1447 rettv->vval.v_float = 0.0;
1448}
1449
1450/*
1451 * "atan2()" function
1452 */
1453 static void
1454f_atan2(typval_T *argvars, typval_T *rettv)
1455{
1456 float_T fx = 0.0, fy = 0.0;
1457
1458 rettv->v_type = VAR_FLOAT;
1459 if (get_float_arg(argvars, &fx) == OK
1460 && get_float_arg(&argvars[1], &fy) == OK)
1461 rettv->vval.v_float = atan2(fx, fy);
1462 else
1463 rettv->vval.v_float = 0.0;
1464}
1465#endif
1466
1467/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001468 * "balloon_show()" function
1469 */
1470#ifdef FEAT_BEVAL
1471 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001472f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1473{
1474 rettv->v_type = VAR_STRING;
1475 if (balloonEval != NULL)
1476 {
1477 if (balloonEval->msg == NULL)
1478 rettv->vval.v_string = NULL;
1479 else
1480 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1481 }
1482}
1483
1484 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001485f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1486{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001487 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001488 {
1489 if (argvars[0].v_type == VAR_LIST
1490# ifdef FEAT_GUI
1491 && !gui.in_use
1492# endif
1493 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001494 {
1495 list_T *l = argvars[0].vval.v_list;
1496
1497 // empty list removes the balloon
1498 post_balloon(balloonEval, NULL,
1499 l == NULL || l->lv_len == 0 ? NULL : l);
1500 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001501 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001502 {
1503 char_u *mesg = tv_get_string_chk(&argvars[0]);
1504
1505 if (mesg != NULL)
1506 // empty string removes the balloon
1507 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1508 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001509 }
1510}
1511
Bram Moolenaar669a8282017-11-19 20:13:05 +01001512# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001513 static void
1514f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1515{
1516 if (rettv_list_alloc(rettv) == OK)
1517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001519
1520 if (msg != NULL)
1521 {
1522 pumitem_T *array;
1523 int size = split_message(msg, &array);
1524 int i;
1525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001526 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001527 for (i = 1; i < size - 1; ++i)
1528 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001529 while (size > 0)
1530 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001531 vim_free(array);
1532 }
1533 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001534}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001535# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001536#endif
1537
1538/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001539 * Get the buffer from "arg" and give an error and return NULL if it is not
1540 * valid.
1541 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001542 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001543get_buf_arg(typval_T *arg)
1544{
1545 buf_T *buf;
1546
1547 ++emsg_off;
1548 buf = tv_get_buf(arg, FALSE);
1549 --emsg_off;
1550 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001552 return buf;
1553}
1554
1555/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001556 * "byte2line(byte)" function
1557 */
1558 static void
1559f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1560{
1561#ifndef FEAT_BYTEOFF
1562 rettv->vval.v_number = -1;
1563#else
1564 long boff = 0;
1565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001566 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 if (boff < 0)
1568 rettv->vval.v_number = -1;
1569 else
1570 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1571 (linenr_T)0, &boff);
1572#endif
1573}
1574
1575 static void
1576byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1577{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001578 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 char_u *str;
1580 varnumber_T idx;
1581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 str = tv_get_string_chk(&argvars[0]);
1583 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001584 rettv->vval.v_number = -1;
1585 if (str == NULL || idx < 0)
1586 return;
1587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588 t = str;
1589 for ( ; idx > 0; idx--)
1590 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001591 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592 return;
1593 if (enc_utf8 && comp)
1594 t += utf_ptr2len(t);
1595 else
1596 t += (*mb_ptr2len)(t);
1597 }
1598 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001599}
1600
1601/*
1602 * "byteidx()" function
1603 */
1604 static void
1605f_byteidx(typval_T *argvars, typval_T *rettv)
1606{
1607 byteidx(argvars, rettv, FALSE);
1608}
1609
1610/*
1611 * "byteidxcomp()" function
1612 */
1613 static void
1614f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1615{
1616 byteidx(argvars, rettv, TRUE);
1617}
1618
1619/*
1620 * "call(func, arglist [, dict])" function
1621 */
1622 static void
1623f_call(typval_T *argvars, typval_T *rettv)
1624{
1625 char_u *func;
1626 partial_T *partial = NULL;
1627 dict_T *selfdict = NULL;
1628
1629 if (argvars[1].v_type != VAR_LIST)
1630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001631 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 return;
1633 }
1634 if (argvars[1].vval.v_list == NULL)
1635 return;
1636
1637 if (argvars[0].v_type == VAR_FUNC)
1638 func = argvars[0].vval.v_string;
1639 else if (argvars[0].v_type == VAR_PARTIAL)
1640 {
1641 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001642 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643 }
1644 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001645 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001647 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648
1649 if (argvars[2].v_type != VAR_UNKNOWN)
1650 {
1651 if (argvars[2].v_type != VAR_DICT)
1652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001653 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 return;
1655 }
1656 selfdict = argvars[2].vval.v_dict;
1657 }
1658
1659 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1660}
1661
1662#ifdef FEAT_FLOAT
1663/*
1664 * "ceil({float})" function
1665 */
1666 static void
1667f_ceil(typval_T *argvars, typval_T *rettv)
1668{
1669 float_T f = 0.0;
1670
1671 rettv->v_type = VAR_FLOAT;
1672 if (get_float_arg(argvars, &f) == OK)
1673 rettv->vval.v_float = ceil(f);
1674 else
1675 rettv->vval.v_float = 0.0;
1676}
1677#endif
1678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679/*
1680 * "changenr()" function
1681 */
1682 static void
1683f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1684{
1685 rettv->vval.v_number = curbuf->b_u_seq_cur;
1686}
1687
1688/*
1689 * "char2nr(string)" function
1690 */
1691 static void
1692f_char2nr(typval_T *argvars, typval_T *rettv)
1693{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694 if (has_mbyte)
1695 {
1696 int utf8 = 0;
1697
1698 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001699 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700
1701 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001702 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001704 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705 }
1706 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001707 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708}
1709
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001710 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001711get_optional_window(typval_T *argvars, int idx)
1712{
1713 win_T *win = curwin;
1714
1715 if (argvars[idx].v_type != VAR_UNKNOWN)
1716 {
1717 win = find_win_by_nr_or_id(&argvars[idx]);
1718 if (win == NULL)
1719 {
1720 emsg(_(e_invalwindow));
1721 return NULL;
1722 }
1723 }
1724 return win;
1725}
1726
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001728 * "col(string)" function
1729 */
1730 static void
1731f_col(typval_T *argvars, typval_T *rettv)
1732{
1733 colnr_T col = 0;
1734 pos_T *fp;
1735 int fnum = curbuf->b_fnum;
1736
1737 fp = var2fpos(&argvars[0], FALSE, &fnum);
1738 if (fp != NULL && fnum == curbuf->b_fnum)
1739 {
1740 if (fp->col == MAXCOL)
1741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001742 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1744 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1745 else
1746 col = MAXCOL;
1747 }
1748 else
1749 {
1750 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001751 // col(".") when the cursor is on the NUL at the end of the line
1752 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001753 if (virtual_active() && fp == &curwin->w_cursor)
1754 {
1755 char_u *p = ml_get_cursor();
1756
1757 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1758 curwin->w_virtcol - curwin->w_cursor.coladd))
1759 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001760 int l;
1761
1762 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1763 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001764 }
1765 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001766 }
1767 }
1768 rettv->vval.v_number = col;
1769}
1770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001771/*
1772 * "confirm(message, buttons[, default [, type]])" function
1773 */
1774 static void
1775f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1776{
1777#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1778 char_u *message;
1779 char_u *buttons = NULL;
1780 char_u buf[NUMBUFLEN];
1781 char_u buf2[NUMBUFLEN];
1782 int def = 1;
1783 int type = VIM_GENERIC;
1784 char_u *typestr;
1785 int error = FALSE;
1786
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001787 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001788 if (message == NULL)
1789 error = TRUE;
1790 if (argvars[1].v_type != VAR_UNKNOWN)
1791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001792 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001793 if (buttons == NULL)
1794 error = TRUE;
1795 if (argvars[2].v_type != VAR_UNKNOWN)
1796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001797 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001798 if (argvars[3].v_type != VAR_UNKNOWN)
1799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001800 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001801 if (typestr == NULL)
1802 error = TRUE;
1803 else
1804 {
1805 switch (TOUPPER_ASC(*typestr))
1806 {
1807 case 'E': type = VIM_ERROR; break;
1808 case 'Q': type = VIM_QUESTION; break;
1809 case 'I': type = VIM_INFO; break;
1810 case 'W': type = VIM_WARNING; break;
1811 case 'G': type = VIM_GENERIC; break;
1812 }
1813 }
1814 }
1815 }
1816 }
1817
1818 if (buttons == NULL || *buttons == NUL)
1819 buttons = (char_u *)_("&Ok");
1820
1821 if (!error)
1822 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1823 def, NULL, FALSE);
1824#endif
1825}
1826
1827/*
1828 * "copy()" function
1829 */
1830 static void
1831f_copy(typval_T *argvars, typval_T *rettv)
1832{
1833 item_copy(&argvars[0], rettv, FALSE, 0);
1834}
1835
1836#ifdef FEAT_FLOAT
1837/*
1838 * "cos()" function
1839 */
1840 static void
1841f_cos(typval_T *argvars, typval_T *rettv)
1842{
1843 float_T f = 0.0;
1844
1845 rettv->v_type = VAR_FLOAT;
1846 if (get_float_arg(argvars, &f) == OK)
1847 rettv->vval.v_float = cos(f);
1848 else
1849 rettv->vval.v_float = 0.0;
1850}
1851
1852/*
1853 * "cosh()" function
1854 */
1855 static void
1856f_cosh(typval_T *argvars, typval_T *rettv)
1857{
1858 float_T f = 0.0;
1859
1860 rettv->v_type = VAR_FLOAT;
1861 if (get_float_arg(argvars, &f) == OK)
1862 rettv->vval.v_float = cosh(f);
1863 else
1864 rettv->vval.v_float = 0.0;
1865}
1866#endif
1867
1868/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869 * "cursor(lnum, col)" function, or
1870 * "cursor(list)"
1871 *
1872 * Moves the cursor to the specified line and column.
1873 * Returns 0 when the position could be set, -1 otherwise.
1874 */
1875 static void
1876f_cursor(typval_T *argvars, typval_T *rettv)
1877{
1878 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 int set_curswant = TRUE;
1881
1882 rettv->vval.v_number = -1;
1883 if (argvars[1].v_type == VAR_UNKNOWN)
1884 {
1885 pos_T pos;
1886 colnr_T curswant = -1;
1887
1888 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001890 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 return;
1892 }
1893 line = pos.lnum;
1894 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896 if (curswant >= 0)
1897 {
1898 curwin->w_curswant = curswant - 1;
1899 set_curswant = FALSE;
1900 }
1901 }
1902 else
1903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001904 line = tv_get_lnum(argvars);
1905 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001907 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001909 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 if (line > 0)
1912 curwin->w_cursor.lnum = line;
1913 if (col > 0)
1914 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001917 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001920 if (has_mbyte)
1921 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922
1923 curwin->w_set_curswant = set_curswant;
1924 rettv->vval.v_number = 0;
1925}
1926
Bram Moolenaar4f974752019-02-17 17:44:42 +01001927#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001928/*
1929 * "debugbreak()" function
1930 */
1931 static void
1932f_debugbreak(typval_T *argvars, typval_T *rettv)
1933{
1934 int pid;
1935
1936 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001937 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001938 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001940 else
1941 {
1942 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1943
1944 if (hProcess != NULL)
1945 {
1946 DebugBreakProcess(hProcess);
1947 CloseHandle(hProcess);
1948 rettv->vval.v_number = OK;
1949 }
1950 }
1951}
1952#endif
1953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001954/*
1955 * "deepcopy()" function
1956 */
1957 static void
1958f_deepcopy(typval_T *argvars, typval_T *rettv)
1959{
1960 int noref = 0;
1961 int copyID;
1962
1963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001964 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001965 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001966 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967 else
1968 {
1969 copyID = get_copyID();
1970 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1971 }
1972}
1973
1974/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001975 * "did_filetype()" function
1976 */
1977 static void
1978f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1979{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001980 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001981}
1982
1983/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001984 * "echoraw({expr})" function
1985 */
1986 static void
1987f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
1988{
1989 char_u *str = tv_get_string_chk(&argvars[0]);
1990
1991 if (str != NULL && *str != NUL)
1992 {
1993 out_str(str);
1994 out_flush();
1995 }
1996}
1997
1998/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 * "empty({expr})" function
2000 */
2001 static void
2002f_empty(typval_T *argvars, typval_T *rettv)
2003{
2004 int n = FALSE;
2005
2006 switch (argvars[0].v_type)
2007 {
2008 case VAR_STRING:
2009 case VAR_FUNC:
2010 n = argvars[0].vval.v_string == NULL
2011 || *argvars[0].vval.v_string == NUL;
2012 break;
2013 case VAR_PARTIAL:
2014 n = FALSE;
2015 break;
2016 case VAR_NUMBER:
2017 n = argvars[0].vval.v_number == 0;
2018 break;
2019 case VAR_FLOAT:
2020#ifdef FEAT_FLOAT
2021 n = argvars[0].vval.v_float == 0.0;
2022 break;
2023#endif
2024 case VAR_LIST:
2025 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002026 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002027 break;
2028 case VAR_DICT:
2029 n = argvars[0].vval.v_dict == NULL
2030 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2031 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002032 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002033 case VAR_SPECIAL:
2034 n = argvars[0].vval.v_number != VVAL_TRUE;
2035 break;
2036
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002037 case VAR_BLOB:
2038 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002039 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2040 break;
2041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002042 case VAR_JOB:
2043#ifdef FEAT_JOB_CHANNEL
2044 n = argvars[0].vval.v_job == NULL
2045 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2046 break;
2047#endif
2048 case VAR_CHANNEL:
2049#ifdef FEAT_JOB_CHANNEL
2050 n = argvars[0].vval.v_channel == NULL
2051 || !channel_is_open(argvars[0].vval.v_channel);
2052 break;
2053#endif
2054 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002055 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002057 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 n = TRUE;
2059 break;
2060 }
2061
2062 rettv->vval.v_number = n;
2063}
2064
2065/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002066 * "environ()" function
2067 */
2068 static void
2069f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2070{
2071#if !defined(AMIGA)
2072 int i = 0;
2073 char_u *entry, *value;
2074# ifdef MSWIN
2075 extern wchar_t **_wenviron;
2076# else
2077 extern char **environ;
2078# endif
2079
2080 if (rettv_dict_alloc(rettv) != OK)
2081 return;
2082
2083# ifdef MSWIN
2084 if (*_wenviron == NULL)
2085 return;
2086# else
2087 if (*environ == NULL)
2088 return;
2089# endif
2090
2091 for (i = 0; ; ++i)
2092 {
2093# ifdef MSWIN
2094 short_u *p;
2095
2096 if ((p = (short_u *)_wenviron[i]) == NULL)
2097 return;
2098 entry = utf16_to_enc(p, NULL);
2099# else
2100 if ((entry = (char_u *)environ[i]) == NULL)
2101 return;
2102 entry = vim_strsave(entry);
2103# endif
2104 if (entry == NULL) // out of memory
2105 return;
2106 if ((value = vim_strchr(entry, '=')) == NULL)
2107 {
2108 vim_free(entry);
2109 continue;
2110 }
2111 *value++ = NUL;
2112 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2113 vim_free(entry);
2114 }
2115#endif
2116}
2117
2118/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002119 * "escape({string}, {chars})" function
2120 */
2121 static void
2122f_escape(typval_T *argvars, typval_T *rettv)
2123{
2124 char_u buf[NUMBUFLEN];
2125
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002126 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2127 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 rettv->v_type = VAR_STRING;
2129}
2130
2131/*
2132 * "eval()" function
2133 */
2134 static void
2135f_eval(typval_T *argvars, typval_T *rettv)
2136{
2137 char_u *s, *p;
2138
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002139 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002140 if (s != NULL)
2141 s = skipwhite(s);
2142
2143 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002144 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 {
2146 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002148 need_clr_eos = FALSE;
2149 rettv->v_type = VAR_NUMBER;
2150 rettv->vval.v_number = 0;
2151 }
2152 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002153 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154}
2155
2156/*
2157 * "eventhandler()" function
2158 */
2159 static void
2160f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2161{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002162 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163}
2164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165static garray_T redir_execute_ga;
2166
2167/*
2168 * Append "value[value_len]" to the execute() output.
2169 */
2170 void
2171execute_redir_str(char_u *value, int value_len)
2172{
2173 int len;
2174
2175 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002176 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002178 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002179 if (ga_grow(&redir_execute_ga, len) == OK)
2180 {
2181 mch_memmove((char *)redir_execute_ga.ga_data
2182 + redir_execute_ga.ga_len, value, len);
2183 redir_execute_ga.ga_len += len;
2184 }
2185}
2186
2187/*
2188 * Get next line from a list.
2189 * Called by do_cmdline() to get the next line.
2190 * Returns allocated string, or NULL for end of function.
2191 */
2192
2193 static char_u *
2194get_list_line(
2195 int c UNUSED,
2196 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002197 int indent UNUSED,
2198 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002199{
2200 listitem_T **p = (listitem_T **)cookie;
2201 listitem_T *item = *p;
2202 char_u buf[NUMBUFLEN];
2203 char_u *s;
2204
2205 if (item == NULL)
2206 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002207 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 *p = item->li_next;
2209 return s == NULL ? NULL : vim_strsave(s);
2210}
2211
2212/*
2213 * "execute()" function
2214 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002215 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002216execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217{
2218 char_u *cmd = NULL;
2219 list_T *list = NULL;
2220 int save_msg_silent = msg_silent;
2221 int save_emsg_silent = emsg_silent;
2222 int save_emsg_noredir = emsg_noredir;
2223 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002224 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002225 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002226 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002227 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228
2229 rettv->vval.v_string = NULL;
2230 rettv->v_type = VAR_STRING;
2231
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002232 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002234 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002235 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002236 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237 return;
2238 ++list->lv_refcount;
2239 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002240 else if (argvars[arg_off].v_type == VAR_JOB
2241 || argvars[arg_off].v_type == VAR_CHANNEL)
2242 {
2243 emsg(_(e_inval_string));
2244 return;
2245 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 else
2247 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002248 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002249 if (cmd == NULL)
2250 return;
2251 }
2252
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002253 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002254 {
2255 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002256 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002257
2258 if (s == NULL)
2259 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002260 if (*s == NUL)
2261 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002262 if (STRNCMP(s, "silent", 6) == 0)
2263 ++msg_silent;
2264 if (STRCMP(s, "silent!") == 0)
2265 {
2266 emsg_silent = TRUE;
2267 emsg_noredir = TRUE;
2268 }
2269 }
2270 else
2271 ++msg_silent;
2272
2273 if (redir_execute)
2274 save_ga = redir_execute_ga;
2275 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2276 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002277 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002278 if (!echo_output)
2279 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280
2281 if (cmd != NULL)
2282 do_cmdline_cmd(cmd);
2283 else
2284 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002285 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002287 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002288 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289 do_cmdline(NULL, get_list_line, (void *)&item,
2290 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2291 --list->lv_refcount;
2292 }
2293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002294 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002295 if (ga_grow(&redir_execute_ga, 1) == OK)
2296 {
2297 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2298 rettv->vval.v_string = redir_execute_ga.ga_data;
2299 }
2300 else
2301 {
2302 ga_clear(&redir_execute_ga);
2303 rettv->vval.v_string = NULL;
2304 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002305 msg_silent = save_msg_silent;
2306 emsg_silent = save_emsg_silent;
2307 emsg_noredir = save_emsg_noredir;
2308
2309 redir_execute = save_redir_execute;
2310 if (redir_execute)
2311 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002312 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002314 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002315 if (echo_output)
2316 // When not working silently: put it in column zero. A following
2317 // "echon" will overwrite the message, unavoidably.
2318 msg_col = 0;
2319 else
2320 // When working silently: Put it back where it was, since nothing
2321 // should have been written.
2322 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323}
2324
2325/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002326 * "execute()" function
2327 */
2328 static void
2329f_execute(typval_T *argvars, typval_T *rettv)
2330{
2331 execute_common(argvars, rettv, 0);
2332}
2333
2334/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335 * "exists()" function
2336 */
2337 static void
2338f_exists(typval_T *argvars, typval_T *rettv)
2339{
2340 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002341 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002343 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002344 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002346 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002347 if (mch_getenv(p + 1) != NULL)
2348 n = TRUE;
2349 else
2350 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002351 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002352 p = expand_env_save(p);
2353 if (p != NULL && *p != '$')
2354 n = TRUE;
2355 vim_free(p);
2356 }
2357 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002360 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002364 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002366 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002368 else if (*p == '?') // internal function only
2369 {
2370 n = has_internal_func_name(p + 1);
2371 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 else if (*p == ':')
2373 {
2374 n = cmd_exists(p + 1);
2375 }
2376 else if (*p == '#')
2377 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378 if (p[1] == '#')
2379 n = autocmd_supported(p + 2);
2380 else
2381 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002383 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002385 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 }
2387
2388 rettv->vval.v_number = n;
2389}
2390
2391#ifdef FEAT_FLOAT
2392/*
2393 * "exp()" function
2394 */
2395 static void
2396f_exp(typval_T *argvars, typval_T *rettv)
2397{
2398 float_T f = 0.0;
2399
2400 rettv->v_type = VAR_FLOAT;
2401 if (get_float_arg(argvars, &f) == OK)
2402 rettv->vval.v_float = exp(f);
2403 else
2404 rettv->vval.v_float = 0.0;
2405}
2406#endif
2407
2408/*
2409 * "expand()" function
2410 */
2411 static void
2412f_expand(typval_T *argvars, typval_T *rettv)
2413{
2414 char_u *s;
2415 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002416 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2418 expand_T xpc;
2419 int error = FALSE;
2420 char_u *result;
2421
2422 rettv->v_type = VAR_STRING;
2423 if (argvars[1].v_type != VAR_UNKNOWN
2424 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002425 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002427 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002429 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430 if (*s == '%' || *s == '#' || *s == '<')
2431 {
2432 ++emsg_off;
2433 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2434 --emsg_off;
2435 if (rettv->v_type == VAR_LIST)
2436 {
2437 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2438 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002439 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440 }
2441 else
2442 rettv->vval.v_string = result;
2443 }
2444 else
2445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002446 // When the optional second argument is non-zero, don't remove matches
2447 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002449 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 options |= WILD_KEEP_ALL;
2451 if (!error)
2452 {
2453 ExpandInit(&xpc);
2454 xpc.xp_context = EXPAND_FILES;
2455 if (p_wic)
2456 options += WILD_ICASE;
2457 if (rettv->v_type == VAR_STRING)
2458 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2459 options, WILD_ALL);
2460 else if (rettv_list_alloc(rettv) != FAIL)
2461 {
2462 int i;
2463
2464 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2465 for (i = 0; i < xpc.xp_numfiles; i++)
2466 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2467 ExpandCleanup(&xpc);
2468 }
2469 }
2470 else
2471 rettv->vval.v_string = NULL;
2472 }
2473}
2474
2475/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002476 * "expandcmd()" function
2477 * Expand all the special characters in a command string.
2478 */
2479 static void
2480f_expandcmd(typval_T *argvars, typval_T *rettv)
2481{
2482 exarg_T eap;
2483 char_u *cmdstr;
2484 char *errormsg = NULL;
2485
2486 rettv->v_type = VAR_STRING;
2487 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2488
2489 memset(&eap, 0, sizeof(eap));
2490 eap.cmd = cmdstr;
2491 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002492 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002493 eap.usefilter = FALSE;
2494 eap.nextcmd = NULL;
2495 eap.cmdidx = CMD_USER;
2496
2497 expand_filename(&eap, &cmdstr, &errormsg);
2498 if (errormsg != NULL && *errormsg != NUL)
2499 emsg(errormsg);
2500
2501 rettv->vval.v_string = cmdstr;
2502}
2503
2504/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505 * "feedkeys()" function
2506 */
2507 static void
2508f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2509{
2510 int remap = TRUE;
2511 int insert = FALSE;
2512 char_u *keys, *flags;
2513 char_u nbuf[NUMBUFLEN];
2514 int typed = FALSE;
2515 int execute = FALSE;
2516 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002517 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 char_u *keys_esc;
2519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002520 // This is not allowed in the sandbox. If the commands would still be
2521 // executed in the sandbox it would be OK, but it probably happens later,
2522 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 if (check_secure())
2524 return;
2525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002526 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527
2528 if (argvars[1].v_type != VAR_UNKNOWN)
2529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002530 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 for ( ; *flags != NUL; ++flags)
2532 {
2533 switch (*flags)
2534 {
2535 case 'n': remap = FALSE; break;
2536 case 'm': remap = TRUE; break;
2537 case 't': typed = TRUE; break;
2538 case 'i': insert = TRUE; break;
2539 case 'x': execute = TRUE; break;
2540 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002541 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542 }
2543 }
2544 }
2545
2546 if (*keys != NUL || execute)
2547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002548 // Need to escape K_SPECIAL and CSI before putting the string in the
2549 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550 keys_esc = vim_strsave_escape_csi(keys);
2551 if (keys_esc != NULL)
2552 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002553 if (lowlevel)
2554 {
2555#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002556 int idx;
2557 int len = (int)STRLEN(keys);
2558
2559 for (idx = 0; idx < len; ++idx)
2560 {
2561 // if a CTRL-C was typed, set got_int, similar to what
2562 // happens in fill_input_buf()
2563 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2564 got_int = TRUE;
2565 add_to_input_buf(keys + idx, 1);
2566 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002567#else
2568 emsg(_("E980: lowlevel input not supported"));
2569#endif
2570 }
2571 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002572 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002573 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002574 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002575 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002576#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002577 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002578#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002579 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002580 typebuf_was_filled = TRUE;
2581 }
2582 vim_free(keys_esc);
2583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002584 if (execute)
2585 {
2586 int save_msg_scroll = msg_scroll;
2587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002588 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 msg_scroll = FALSE;
2590
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002591 if (!dangerous)
2592 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002593 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002594 if (!dangerous)
2595 --ex_normal_busy;
2596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 msg_scroll |= save_msg_scroll;
2598 }
2599 }
2600 }
2601}
2602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603#ifdef FEAT_FLOAT
2604/*
2605 * "float2nr({float})" function
2606 */
2607 static void
2608f_float2nr(typval_T *argvars, typval_T *rettv)
2609{
2610 float_T f = 0.0;
2611
2612 if (get_float_arg(argvars, &f) == OK)
2613 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002614 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002615 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002616 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002617 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 else
2619 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 }
2621}
2622
2623/*
2624 * "floor({float})" function
2625 */
2626 static void
2627f_floor(typval_T *argvars, typval_T *rettv)
2628{
2629 float_T f = 0.0;
2630
2631 rettv->v_type = VAR_FLOAT;
2632 if (get_float_arg(argvars, &f) == OK)
2633 rettv->vval.v_float = floor(f);
2634 else
2635 rettv->vval.v_float = 0.0;
2636}
2637
2638/*
2639 * "fmod()" function
2640 */
2641 static void
2642f_fmod(typval_T *argvars, typval_T *rettv)
2643{
2644 float_T fx = 0.0, fy = 0.0;
2645
2646 rettv->v_type = VAR_FLOAT;
2647 if (get_float_arg(argvars, &fx) == OK
2648 && get_float_arg(&argvars[1], &fy) == OK)
2649 rettv->vval.v_float = fmod(fx, fy);
2650 else
2651 rettv->vval.v_float = 0.0;
2652}
2653#endif
2654
2655/*
2656 * "fnameescape({string})" function
2657 */
2658 static void
2659f_fnameescape(typval_T *argvars, typval_T *rettv)
2660{
2661 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002662 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 rettv->v_type = VAR_STRING;
2664}
2665
2666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667 * "foreground()" function
2668 */
2669 static void
2670f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2671{
2672#ifdef FEAT_GUI
2673 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002674 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002676 return;
2677 }
2678#endif
2679#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681#endif
2682}
2683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002685common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686{
2687 char_u *s;
2688 char_u *name;
2689 int use_string = FALSE;
2690 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002691 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002692 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693
2694 if (argvars[0].v_type == VAR_FUNC)
2695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002696 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 s = argvars[0].vval.v_string;
2698 }
2699 else if (argvars[0].v_type == VAR_PARTIAL
2700 && argvars[0].vval.v_partial != NULL)
2701 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002702 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002704 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 }
2706 else
2707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002708 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002709 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002710 use_string = TRUE;
2711 }
2712
Bram Moolenaar843b8842016-08-21 14:36:15 +02002713 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002714 {
2715 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002716 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002717 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2718 if (*name != NUL)
2719 s = NULL;
2720 }
2721
Bram Moolenaar843b8842016-08-21 14:36:15 +02002722 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2723 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002724 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002725 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002726 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002727 ? find_func(trans_name, is_global, NULL) == NULL
2728 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002729 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 else
2731 {
2732 int dict_idx = 0;
2733 int arg_idx = 0;
2734 list_T *list = NULL;
2735
2736 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2737 {
2738 char sid_buf[25];
2739 int off = *s == 's' ? 2 : 5;
2740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002741 // Expand s: and <SID> into <SNR>nr_, so that the function can
2742 // also be called from another script. Using trans_function_name()
2743 // would also work, but some plugins depend on the name being
2744 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002745 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002746 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 if (name != NULL)
2748 {
2749 STRCPY(name, sid_buf);
2750 STRCAT(name, s + off);
2751 }
2752 }
2753 else
2754 name = vim_strsave(s);
2755
2756 if (argvars[1].v_type != VAR_UNKNOWN)
2757 {
2758 if (argvars[2].v_type != VAR_UNKNOWN)
2759 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002760 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 arg_idx = 1;
2762 dict_idx = 2;
2763 }
2764 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002765 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766 dict_idx = 1;
2767 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002768 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 arg_idx = 1;
2770 if (dict_idx > 0)
2771 {
2772 if (argvars[dict_idx].v_type != VAR_DICT)
2773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002774 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002776 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 }
2778 if (argvars[dict_idx].vval.v_dict == NULL)
2779 dict_idx = 0;
2780 }
2781 if (arg_idx > 0)
2782 {
2783 if (argvars[arg_idx].v_type != VAR_LIST)
2784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002785 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002787 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 }
2789 list = argvars[arg_idx].vval.v_list;
2790 if (list == NULL || list->lv_len == 0)
2791 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002792 else if (list->lv_len > MAX_FUNC_ARGS)
2793 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002794 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002795 vim_free(name);
2796 goto theend;
2797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 }
2799 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002800 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002801 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002802 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002804 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 if (pt == NULL)
2806 vim_free(name);
2807 else
2808 {
2809 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2810 {
2811 listitem_T *li;
2812 int i = 0;
2813 int arg_len = 0;
2814 int lv_len = 0;
2815
2816 if (arg_pt != NULL)
2817 arg_len = arg_pt->pt_argc;
2818 if (list != NULL)
2819 lv_len = list->lv_len;
2820 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002821 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (pt->pt_argv == NULL)
2823 {
2824 vim_free(pt);
2825 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002826 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002827 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002828 for (i = 0; i < arg_len; i++)
2829 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2830 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002831 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002832 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002833 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002834 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002835 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 }
2837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002838 // For "function(dict.func, [], dict)" and "func" is a partial
2839 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 if (dict_idx > 0)
2841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002842 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002843 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2844 ++pt->pt_dict->dv_refcount;
2845 }
2846 else if (arg_pt != NULL)
2847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002848 // If the dict was bound automatically the result is also
2849 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002850 pt->pt_dict = arg_pt->pt_dict;
2851 pt->pt_auto = arg_pt->pt_auto;
2852 if (pt->pt_dict != NULL)
2853 ++pt->pt_dict->dv_refcount;
2854 }
2855
2856 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002857 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2858 {
2859 pt->pt_func = arg_pt->pt_func;
2860 func_ptr_ref(pt->pt_func);
2861 vim_free(name);
2862 }
2863 else if (is_funcref)
2864 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002865 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002866 func_ptr_ref(pt->pt_func);
2867 vim_free(name);
2868 }
2869 else
2870 {
2871 pt->pt_name = name;
2872 func_ref(name);
2873 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874 }
2875 rettv->v_type = VAR_PARTIAL;
2876 rettv->vval.v_partial = pt;
2877 }
2878 else
2879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002880 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881 rettv->v_type = VAR_FUNC;
2882 rettv->vval.v_string = name;
2883 func_ref(name);
2884 }
2885 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002886theend:
2887 vim_free(trans_name);
2888}
2889
2890/*
2891 * "funcref()" function
2892 */
2893 static void
2894f_funcref(typval_T *argvars, typval_T *rettv)
2895{
2896 common_function(argvars, rettv, TRUE);
2897}
2898
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002899 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002900ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002901{
2902 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2903 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002904 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002905}
2906
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002907/*
2908 * "function()" function
2909 */
2910 static void
2911f_function(typval_T *argvars, typval_T *rettv)
2912{
2913 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914}
2915
2916/*
2917 * "garbagecollect()" function
2918 */
2919 static void
2920f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2921{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002922 // This is postponed until we are back at the toplevel, because we may be
2923 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 want_garbage_collect = TRUE;
2925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002926 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 garbage_collect_at_exit = TRUE;
2928}
2929
2930/*
2931 * "get()" function
2932 */
2933 static void
2934f_get(typval_T *argvars, typval_T *rettv)
2935{
2936 listitem_T *li;
2937 list_T *l;
2938 dictitem_T *di;
2939 dict_T *d;
2940 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002941 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002943 if (argvars[0].v_type == VAR_BLOB)
2944 {
2945 int error = FALSE;
2946 int idx = tv_get_number_chk(&argvars[1], &error);
2947
2948 if (!error)
2949 {
2950 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002951 if (idx < 0)
2952 idx = blob_len(argvars[0].vval.v_blob) + idx;
2953 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2954 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002955 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002956 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002957 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002958 tv = rettv;
2959 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002960 }
2961 }
2962 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 {
2964 if ((l = argvars[0].vval.v_list) != NULL)
2965 {
2966 int error = FALSE;
2967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002968 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 if (!error && li != NULL)
2970 tv = &li->li_tv;
2971 }
2972 }
2973 else if (argvars[0].v_type == VAR_DICT)
2974 {
2975 if ((d = argvars[0].vval.v_dict) != NULL)
2976 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (di != NULL)
2979 tv = &di->di_tv;
2980 }
2981 }
2982 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2983 {
2984 partial_T *pt;
2985 partial_T fref_pt;
2986
2987 if (argvars[0].v_type == VAR_PARTIAL)
2988 pt = argvars[0].vval.v_partial;
2989 else
2990 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02002991 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 fref_pt.pt_name = argvars[0].vval.v_string;
2993 pt = &fref_pt;
2994 }
2995
2996 if (pt != NULL)
2997 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002998 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002999 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000
3001 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3002 {
3003 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003004 n = partial_name(pt);
3005 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 rettv->vval.v_string = NULL;
3007 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003008 {
3009 rettv->vval.v_string = vim_strsave(n);
3010 if (rettv->v_type == VAR_FUNC)
3011 func_ref(rettv->vval.v_string);
3012 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 }
3014 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003015 {
3016 what_is_dict = TRUE;
3017 if (pt->pt_dict != NULL)
3018 rettv_dict_set(rettv, pt->pt_dict);
3019 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 else if (STRCMP(what, "args") == 0)
3021 {
3022 rettv->v_type = VAR_LIST;
3023 if (rettv_list_alloc(rettv) == OK)
3024 {
3025 int i;
3026
3027 for (i = 0; i < pt->pt_argc; ++i)
3028 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3029 }
3030 }
3031 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003032 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003033
3034 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3035 // third argument
3036 if (!what_is_dict)
3037 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 }
3039 }
3040 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003041 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042
3043 if (tv == NULL)
3044 {
3045 if (argvars[2].v_type != VAR_UNKNOWN)
3046 copy_tv(&argvars[2], rettv);
3047 }
3048 else
3049 copy_tv(tv, rettv);
3050}
3051
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003052/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003053 * "getchangelist()" function
3054 */
3055 static void
3056f_getchangelist(typval_T *argvars, typval_T *rettv)
3057{
3058#ifdef FEAT_JUMPLIST
3059 buf_T *buf;
3060 int i;
3061 list_T *l;
3062 dict_T *d;
3063#endif
3064
3065 if (rettv_list_alloc(rettv) != OK)
3066 return;
3067
3068#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003069 if (argvars[0].v_type == VAR_UNKNOWN)
3070 buf = curbuf;
3071 else
3072 {
3073 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3074 ++emsg_off;
3075 buf = tv_get_buf(&argvars[0], FALSE);
3076 --emsg_off;
3077 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003078 if (buf == NULL)
3079 return;
3080
3081 l = list_alloc();
3082 if (l == NULL)
3083 return;
3084
3085 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3086 return;
3087 /*
3088 * The current window change list index tracks only the position in the
3089 * current buffer change list. For other buffers, use the change list
3090 * length as the current index.
3091 */
3092 list_append_number(rettv->vval.v_list,
3093 (varnumber_T)((buf == curwin->w_buffer)
3094 ? curwin->w_changelistidx : buf->b_changelistlen));
3095
3096 for (i = 0; i < buf->b_changelistlen; ++i)
3097 {
3098 if (buf->b_changelist[i].lnum == 0)
3099 continue;
3100 if ((d = dict_alloc()) == NULL)
3101 return;
3102 if (list_append_dict(l, d) == FAIL)
3103 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003104 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3105 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003106 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003107 }
3108#endif
3109}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110
3111/*
3112 * "getcharsearch()" function
3113 */
3114 static void
3115f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3116{
3117 if (rettv_dict_alloc(rettv) != FAIL)
3118 {
3119 dict_T *dict = rettv->vval.v_dict;
3120
Bram Moolenaare0be1672018-07-08 16:50:37 +02003121 dict_add_string(dict, "char", last_csearch());
3122 dict_add_number(dict, "forward", last_csearch_forward());
3123 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 }
3125}
3126
3127/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003128 * "getenv()" function
3129 */
3130 static void
3131f_getenv(typval_T *argvars, typval_T *rettv)
3132{
3133 int mustfree = FALSE;
3134 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3135
3136 if (p == NULL)
3137 {
3138 rettv->v_type = VAR_SPECIAL;
3139 rettv->vval.v_number = VVAL_NULL;
3140 return;
3141 }
3142 if (!mustfree)
3143 p = vim_strsave(p);
3144 rettv->vval.v_string = p;
3145 rettv->v_type = VAR_STRING;
3146}
3147
3148/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149 * "getfontname()" function
3150 */
3151 static void
3152f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3153{
3154 rettv->v_type = VAR_STRING;
3155 rettv->vval.v_string = NULL;
3156#ifdef FEAT_GUI
3157 if (gui.in_use)
3158 {
3159 GuiFont font;
3160 char_u *name = NULL;
3161
3162 if (argvars[0].v_type == VAR_UNKNOWN)
3163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003164 // Get the "Normal" font. Either the name saved by
3165 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 font = gui.norm_font;
3167 name = hl_get_font_name();
3168 }
3169 else
3170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003171 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003172 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003173 return;
3174 font = gui_mch_get_font(name, FALSE);
3175 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003176 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003177 }
3178 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3179 if (argvars[0].v_type != VAR_UNKNOWN)
3180 gui_mch_free_font(font);
3181 }
3182#endif
3183}
3184
3185/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003186 * "getjumplist()" function
3187 */
3188 static void
3189f_getjumplist(typval_T *argvars, typval_T *rettv)
3190{
3191#ifdef FEAT_JUMPLIST
3192 win_T *wp;
3193 int i;
3194 list_T *l;
3195 dict_T *d;
3196#endif
3197
3198 if (rettv_list_alloc(rettv) != OK)
3199 return;
3200
3201#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003202 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003203 if (wp == NULL)
3204 return;
3205
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003206 cleanup_jumplist(wp, TRUE);
3207
Bram Moolenaar4f505882018-02-10 21:06:32 +01003208 l = list_alloc();
3209 if (l == NULL)
3210 return;
3211
3212 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3213 return;
3214 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3215
3216 for (i = 0; i < wp->w_jumplistlen; ++i)
3217 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003218 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3219 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003220 if ((d = dict_alloc()) == NULL)
3221 return;
3222 if (list_append_dict(l, d) == FAIL)
3223 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003224 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3225 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003226 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003227 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003228 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003229 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003230 }
3231#endif
3232}
3233
3234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 * "getpid()" function
3236 */
3237 static void
3238f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3239{
3240 rettv->vval.v_number = mch_get_pid();
3241}
3242
3243 static void
3244getpos_both(
3245 typval_T *argvars,
3246 typval_T *rettv,
3247 int getcurpos)
3248{
3249 pos_T *fp;
3250 list_T *l;
3251 int fnum = -1;
3252
3253 if (rettv_list_alloc(rettv) == OK)
3254 {
3255 l = rettv->vval.v_list;
3256 if (getcurpos)
3257 fp = &curwin->w_cursor;
3258 else
3259 fp = var2fpos(&argvars[0], TRUE, &fnum);
3260 if (fnum != -1)
3261 list_append_number(l, (varnumber_T)fnum);
3262 else
3263 list_append_number(l, (varnumber_T)0);
3264 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3265 : (varnumber_T)0);
3266 list_append_number(l, (fp != NULL)
3267 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3268 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003269 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 (varnumber_T)0);
3271 if (getcurpos)
3272 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003273 int save_set_curswant = curwin->w_set_curswant;
3274 colnr_T save_curswant = curwin->w_curswant;
3275 colnr_T save_virtcol = curwin->w_virtcol;
3276
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 update_curswant();
3278 list_append_number(l, curwin->w_curswant == MAXCOL ?
3279 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003280
3281 // Do not change "curswant", as it is unexpected that a get
3282 // function has a side effect.
3283 if (save_set_curswant)
3284 {
3285 curwin->w_set_curswant = save_set_curswant;
3286 curwin->w_curswant = save_curswant;
3287 curwin->w_virtcol = save_virtcol;
3288 curwin->w_valid &= ~VALID_VIRTCOL;
3289 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 }
3291 }
3292 else
3293 rettv->vval.v_number = FALSE;
3294}
3295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296/*
3297 * "getcurpos()" function
3298 */
3299 static void
3300f_getcurpos(typval_T *argvars, typval_T *rettv)
3301{
3302 getpos_both(argvars, rettv, TRUE);
3303}
3304
3305/*
3306 * "getpos(string)" function
3307 */
3308 static void
3309f_getpos(typval_T *argvars, typval_T *rettv)
3310{
3311 getpos_both(argvars, rettv, FALSE);
3312}
3313
3314/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003315 * "getreg()" function
3316 */
3317 static void
3318f_getreg(typval_T *argvars, typval_T *rettv)
3319{
3320 char_u *strregname;
3321 int regname;
3322 int arg2 = FALSE;
3323 int return_list = FALSE;
3324 int error = FALSE;
3325
3326 if (argvars[0].v_type != VAR_UNKNOWN)
3327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003328 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329 error = strregname == NULL;
3330 if (argvars[1].v_type != VAR_UNKNOWN)
3331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003332 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003334 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335 }
3336 }
3337 else
3338 strregname = get_vim_var_str(VV_REG);
3339
3340 if (error)
3341 return;
3342
3343 regname = (strregname == NULL ? '"' : *strregname);
3344 if (regname == 0)
3345 regname = '"';
3346
3347 if (return_list)
3348 {
3349 rettv->v_type = VAR_LIST;
3350 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3351 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3352 if (rettv->vval.v_list == NULL)
3353 (void)rettv_list_alloc(rettv);
3354 else
3355 ++rettv->vval.v_list->lv_refcount;
3356 }
3357 else
3358 {
3359 rettv->v_type = VAR_STRING;
3360 rettv->vval.v_string = get_reg_contents(regname,
3361 arg2 ? GREG_EXPR_SRC : 0);
3362 }
3363}
3364
3365/*
3366 * "getregtype()" function
3367 */
3368 static void
3369f_getregtype(typval_T *argvars, typval_T *rettv)
3370{
3371 char_u *strregname;
3372 int regname;
3373 char_u buf[NUMBUFLEN + 2];
3374 long reglen = 0;
3375
3376 if (argvars[0].v_type != VAR_UNKNOWN)
3377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003378 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003379 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 {
3381 rettv->v_type = VAR_STRING;
3382 rettv->vval.v_string = NULL;
3383 return;
3384 }
3385 }
3386 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003387 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003388 strregname = get_vim_var_str(VV_REG);
3389
3390 regname = (strregname == NULL ? '"' : *strregname);
3391 if (regname == 0)
3392 regname = '"';
3393
3394 buf[0] = NUL;
3395 buf[1] = NUL;
3396 switch (get_reg_type(regname, &reglen))
3397 {
3398 case MLINE: buf[0] = 'V'; break;
3399 case MCHAR: buf[0] = 'v'; break;
3400 case MBLOCK:
3401 buf[0] = Ctrl_V;
3402 sprintf((char *)buf + 1, "%ld", reglen + 1);
3403 break;
3404 }
3405 rettv->v_type = VAR_STRING;
3406 rettv->vval.v_string = vim_strsave(buf);
3407}
3408
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003409/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003410 * "gettagstack()" function
3411 */
3412 static void
3413f_gettagstack(typval_T *argvars, typval_T *rettv)
3414{
3415 win_T *wp = curwin; // default is current window
3416
3417 if (rettv_dict_alloc(rettv) != OK)
3418 return;
3419
3420 if (argvars[0].v_type != VAR_UNKNOWN)
3421 {
3422 wp = find_win_by_nr_or_id(&argvars[0]);
3423 if (wp == NULL)
3424 return;
3425 }
3426
3427 get_tagstack(wp, rettv->vval.v_dict);
3428}
3429
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003430// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431#include "version.h"
3432
3433/*
3434 * "has()" function
3435 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003436 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003437f_has(typval_T *argvars, typval_T *rettv)
3438{
3439 int i;
3440 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003441 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003443 typedef struct {
3444 char *name;
3445 short present;
3446 } has_item_T;
3447 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003449 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003451 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003452#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003453 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003454#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003455 },
3456 {"arp",
3457#if defined(AMIGA) && defined(FEAT_ARP)
3458 1
3459#else
3460 0
3461#endif
3462 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003463 {"haiku",
3464#ifdef __HAIKU__
3465 1
3466#else
3467 0
3468#endif
3469 },
3470 {"bsd",
3471#if defined(BSD) && !defined(MACOS_X)
3472 1
3473#else
3474 0
3475#endif
3476 },
3477 {"hpux",
3478#ifdef hpux
3479 1
3480#else
3481 0
3482#endif
3483 },
3484 {"linux",
3485#ifdef __linux__
3486 1
3487#else
3488 0
3489#endif
3490 },
3491 {"mac", // Mac OS X (and, once, Mac OS Classic)
3492#ifdef MACOS_X
3493 1
3494#else
3495 0
3496#endif
3497 },
3498 {"osx", // Mac OS X
3499#ifdef MACOS_X
3500 1
3501#else
3502 0
3503#endif
3504 },
3505 {"macunix", // Mac OS X, with the darwin feature
3506#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3507 1
3508#else
3509 0
3510#endif
3511 },
3512 {"osxdarwin", // synonym for macunix
3513#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3514 1
3515#else
3516 0
3517#endif
3518 },
3519 {"qnx",
3520#ifdef __QNX__
3521 1
3522#else
3523 0
3524#endif
3525 },
3526 {"sun",
3527#ifdef SUN_SYSTEM
3528 1
3529#else
3530 0
3531#endif
3532 },
3533 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003535 1
3536#else
3537 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003539 },
3540 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003542 1
3543#else
3544 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003546 },
3547 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003548#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003549 1
3550#else
3551 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003553 },
3554 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003555#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003556 1
3557#else
3558 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003559#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003560 },
3561 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003562#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003563 1
3564#else
3565 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003567 },
3568 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003570 1
3571#else
3572 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003574 },
3575 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003577 1
3578#else
3579 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003581 },
3582 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003584 1
3585#else
3586 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003587#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003588 },
3589 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003590#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003591 1
3592#else
3593 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 },
3596 {"autocmd", 1},
3597 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003598#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 1
3600#else
3601 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003602#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003603 },
3604 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003605#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 1
3607#else
3608 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003609#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003610 },
3611 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003612#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 1
3614#else
3615 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003617 },
3618 {"balloon_multiline",
3619#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3620 // MS-Windows requires runtime check, see below
3621 1
3622#else
3623 0
3624#endif
3625 },
3626 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003627#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003628 1
3629#else
3630 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003631#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003632 },
3633 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003635 1
3636#else
3637 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003639 },
3640 {"all_builtin_terms",
3641#if defined(ALL_BUILTIN_TCAPS)
3642 1
3643#else
3644 0
3645#endif
3646 },
3647 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003649 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003651 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003653 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003655 },
3656 {"byte_offset",
3657#ifdef FEAT_BYTEOFF
3658 1
3659#else
3660 0
3661#endif
3662 },
3663 {"channel",
3664#ifdef FEAT_JOB_CHANNEL
3665 1
3666#else
3667 0
3668#endif
3669 },
3670 {"cindent",
3671#ifdef FEAT_CINDENT
3672 1
3673#else
3674 0
3675#endif
3676 },
3677 {"clientserver",
3678#ifdef FEAT_CLIENTSERVER
3679 1
3680#else
3681 0
3682#endif
3683 },
3684 {"clipboard",
3685#ifdef FEAT_CLIPBOARD
3686 1
3687#else
3688 0
3689#endif
3690 },
3691 {"cmdline_compl", 1},
3692 {"cmdline_hist", 1},
3693 {"comments", 1},
3694 {"conceal",
3695#ifdef FEAT_CONCEAL
3696 1
3697#else
3698 0
3699#endif
3700 },
3701 {"cryptv",
3702#ifdef FEAT_CRYPT
3703 1
3704#else
3705 0
3706#endif
3707 },
3708 {"crypt-blowfish",
3709#ifdef FEAT_CRYPT
3710 1
3711#else
3712 0
3713#endif
3714 },
3715 {"crypt-blowfish2",
3716#ifdef FEAT_CRYPT
3717 1
3718#else
3719 0
3720#endif
3721 },
3722 {"cscope",
3723#ifdef FEAT_CSCOPE
3724 1
3725#else
3726 0
3727#endif
3728 },
3729 {"cursorbind", 1},
3730 {"cursorshape",
3731#ifdef CURSOR_SHAPE
3732 1
3733#else
3734 0
3735#endif
3736 },
3737 {"debug",
3738#ifdef DEBUG
3739 1
3740#else
3741 0
3742#endif
3743 },
3744 {"dialog_con",
3745#ifdef FEAT_CON_DIALOG
3746 1
3747#else
3748 0
3749#endif
3750 },
3751 {"dialog_gui",
3752#ifdef FEAT_GUI_DIALOG
3753 1
3754#else
3755 0
3756#endif
3757 },
3758 {"diff",
3759#ifdef FEAT_DIFF
3760 1
3761#else
3762 0
3763#endif
3764 },
3765 {"digraphs",
3766#ifdef FEAT_DIGRAPHS
3767 1
3768#else
3769 0
3770#endif
3771 },
3772 {"directx",
3773#ifdef FEAT_DIRECTX
3774 1
3775#else
3776 0
3777#endif
3778 },
3779 {"dnd",
3780#ifdef FEAT_DND
3781 1
3782#else
3783 0
3784#endif
3785 },
3786 {"emacs_tags",
3787#ifdef FEAT_EMACS_TAGS
3788 1
3789#else
3790 0
3791#endif
3792 },
3793 {"eval", 1}, // always present, of course!
3794 {"ex_extra", 1}, // graduated feature
3795 {"extra_search",
3796#ifdef FEAT_SEARCH_EXTRA
3797 1
3798#else
3799 0
3800#endif
3801 },
3802 {"file_in_path",
3803#ifdef FEAT_SEARCHPATH
3804 1
3805#else
3806 0
3807#endif
3808 },
3809 {"filterpipe",
3810#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3811 1
3812#else
3813 0
3814#endif
3815 },
3816 {"find_in_path",
3817#ifdef FEAT_FIND_ID
3818 1
3819#else
3820 0
3821#endif
3822 },
3823 {"float",
3824#ifdef FEAT_FLOAT
3825 1
3826#else
3827 0
3828#endif
3829 },
3830 {"folding",
3831#ifdef FEAT_FOLDING
3832 1
3833#else
3834 0
3835#endif
3836 },
3837 {"footer",
3838#ifdef FEAT_FOOTER
3839 1
3840#else
3841 0
3842#endif
3843 },
3844 {"fork",
3845#if !defined(USE_SYSTEM) && defined(UNIX)
3846 1
3847#else
3848 0
3849#endif
3850 },
3851 {"gettext",
3852#ifdef FEAT_GETTEXT
3853 1
3854#else
3855 0
3856#endif
3857 },
3858 {"gui",
3859#ifdef FEAT_GUI
3860 1
3861#else
3862 0
3863#endif
3864 },
3865 {"gui_neXtaw",
3866#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3867 1
3868#else
3869 0
3870#endif
3871 },
3872 {"gui_athena",
3873#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3874 1
3875#else
3876 0
3877#endif
3878 },
3879 {"gui_gtk",
3880#ifdef FEAT_GUI_GTK
3881 1
3882#else
3883 0
3884#endif
3885 },
3886 {"gui_gtk2",
3887#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3888 1
3889#else
3890 0
3891#endif
3892 },
3893 {"gui_gtk3",
3894#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3895 1
3896#else
3897 0
3898#endif
3899 },
3900 {"gui_gnome",
3901#ifdef FEAT_GUI_GNOME
3902 1
3903#else
3904 0
3905#endif
3906 },
3907 {"gui_haiku",
3908#ifdef FEAT_GUI_HAIKU
3909 1
3910#else
3911 0
3912#endif
3913 },
3914 {"gui_mac",
3915#ifdef FEAT_GUI_MAC
3916 1
3917#else
3918 0
3919#endif
3920 },
3921 {"gui_motif",
3922#ifdef FEAT_GUI_MOTIF
3923 1
3924#else
3925 0
3926#endif
3927 },
3928 {"gui_photon",
3929#ifdef FEAT_GUI_PHOTON
3930 1
3931#else
3932 0
3933#endif
3934 },
3935 {"gui_win32",
3936#ifdef FEAT_GUI_MSWIN
3937 1
3938#else
3939 0
3940#endif
3941 },
3942 {"iconv",
3943#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3944 1
3945#else
3946 0
3947#endif
3948 },
3949 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003950 {"ipv6",
3951#ifdef FEAT_IPV6
3952 1
3953#else
3954 0
3955#endif
3956 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003957 {"job",
3958#ifdef FEAT_JOB_CHANNEL
3959 1
3960#else
3961 0
3962#endif
3963 },
3964 {"jumplist",
3965#ifdef FEAT_JUMPLIST
3966 1
3967#else
3968 0
3969#endif
3970 },
3971 {"keymap",
3972#ifdef FEAT_KEYMAP
3973 1
3974#else
3975 0
3976#endif
3977 },
3978 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
3979 {"langmap",
3980#ifdef FEAT_LANGMAP
3981 1
3982#else
3983 0
3984#endif
3985 },
3986 {"libcall",
3987#ifdef FEAT_LIBCALL
3988 1
3989#else
3990 0
3991#endif
3992 },
3993 {"linebreak",
3994#ifdef FEAT_LINEBREAK
3995 1
3996#else
3997 0
3998#endif
3999 },
4000 {"lispindent",
4001#ifdef FEAT_LISP
4002 1
4003#else
4004 0
4005#endif
4006 },
4007 {"listcmds", 1},
4008 {"localmap", 1},
4009 {"lua",
4010#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4011 1
4012#else
4013 0
4014#endif
4015 },
4016 {"menu",
4017#ifdef FEAT_MENU
4018 1
4019#else
4020 0
4021#endif
4022 },
4023 {"mksession",
4024#ifdef FEAT_SESSION
4025 1
4026#else
4027 0
4028#endif
4029 },
4030 {"modify_fname", 1},
4031 {"mouse", 1},
4032 {"mouseshape",
4033#ifdef FEAT_MOUSESHAPE
4034 1
4035#else
4036 0
4037#endif
4038 },
4039 {"mouse_dec",
4040#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4041 1
4042#else
4043 0
4044#endif
4045 },
4046 {"mouse_gpm",
4047#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4048 1
4049#else
4050 0
4051#endif
4052 },
4053 {"mouse_jsbterm",
4054#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4055 1
4056#else
4057 0
4058#endif
4059 },
4060 {"mouse_netterm",
4061#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4062 1
4063#else
4064 0
4065#endif
4066 },
4067 {"mouse_pterm",
4068#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4069 1
4070#else
4071 0
4072#endif
4073 },
4074 {"mouse_sgr",
4075#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4076 1
4077#else
4078 0
4079#endif
4080 },
4081 {"mouse_sysmouse",
4082#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4083 1
4084#else
4085 0
4086#endif
4087 },
4088 {"mouse_urxvt",
4089#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4090 1
4091#else
4092 0
4093#endif
4094 },
4095 {"mouse_xterm",
4096#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4097 1
4098#else
4099 0
4100#endif
4101 },
4102 {"multi_byte", 1},
4103 {"multi_byte_ime",
4104#ifdef FEAT_MBYTE_IME
4105 1
4106#else
4107 0
4108#endif
4109 },
4110 {"multi_lang",
4111#ifdef FEAT_MULTI_LANG
4112 1
4113#else
4114 0
4115#endif
4116 },
4117 {"mzscheme",
4118#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4119 1
4120#else
4121 0
4122#endif
4123 },
4124 {"num64", 1},
4125 {"ole",
4126#ifdef FEAT_OLE
4127 1
4128#else
4129 0
4130#endif
4131 },
4132 {"packages",
4133#ifdef FEAT_EVAL
4134 1
4135#else
4136 0
4137#endif
4138 },
4139 {"path_extra",
4140#ifdef FEAT_PATH_EXTRA
4141 1
4142#else
4143 0
4144#endif
4145 },
4146 {"perl",
4147#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4148 1
4149#else
4150 0
4151#endif
4152 },
4153 {"persistent_undo",
4154#ifdef FEAT_PERSISTENT_UNDO
4155 1
4156#else
4157 0
4158#endif
4159 },
4160 {"python_compiled",
4161#if defined(FEAT_PYTHON)
4162 1
4163#else
4164 0
4165#endif
4166 },
4167 {"python_dynamic",
4168#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4169 1
4170#else
4171 0
4172#endif
4173 },
4174 {"python",
4175#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4176 1
4177#else
4178 0
4179#endif
4180 },
4181 {"pythonx",
4182#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4183 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4184 1
4185#else
4186 0
4187#endif
4188 },
4189 {"python3_compiled",
4190#if defined(FEAT_PYTHON3)
4191 1
4192#else
4193 0
4194#endif
4195 },
4196 {"python3_dynamic",
4197#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4198 1
4199#else
4200 0
4201#endif
4202 },
4203 {"python3",
4204#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4205 1
4206#else
4207 0
4208#endif
4209 },
4210 {"popupwin",
4211#ifdef FEAT_PROP_POPUP
4212 1
4213#else
4214 0
4215#endif
4216 },
4217 {"postscript",
4218#ifdef FEAT_POSTSCRIPT
4219 1
4220#else
4221 0
4222#endif
4223 },
4224 {"printer",
4225#ifdef FEAT_PRINTER
4226 1
4227#else
4228 0
4229#endif
4230 },
4231 {"profile",
4232#ifdef FEAT_PROFILE
4233 1
4234#else
4235 0
4236#endif
4237 },
4238 {"reltime",
4239#ifdef FEAT_RELTIME
4240 1
4241#else
4242 0
4243#endif
4244 },
4245 {"quickfix",
4246#ifdef FEAT_QUICKFIX
4247 1
4248#else
4249 0
4250#endif
4251 },
4252 {"rightleft",
4253#ifdef FEAT_RIGHTLEFT
4254 1
4255#else
4256 0
4257#endif
4258 },
4259 {"ruby",
4260#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4261 1
4262#else
4263 0
4264#endif
4265 },
4266 {"scrollbind", 1},
4267 {"showcmd",
4268#ifdef FEAT_CMDL_INFO
4269 1
4270#else
4271 0
4272#endif
4273 },
4274 {"cmdline_info",
4275#ifdef FEAT_CMDL_INFO
4276 1
4277#else
4278 0
4279#endif
4280 },
4281 {"signs",
4282#ifdef FEAT_SIGNS
4283 1
4284#else
4285 0
4286#endif
4287 },
4288 {"smartindent",
4289#ifdef FEAT_SMARTINDENT
4290 1
4291#else
4292 0
4293#endif
4294 },
4295 {"startuptime",
4296#ifdef STARTUPTIME
4297 1
4298#else
4299 0
4300#endif
4301 },
4302 {"statusline",
4303#ifdef FEAT_STL_OPT
4304 1
4305#else
4306 0
4307#endif
4308 },
4309 {"netbeans_intg",
4310#ifdef FEAT_NETBEANS_INTG
4311 1
4312#else
4313 0
4314#endif
4315 },
4316 {"sound",
4317#ifdef FEAT_SOUND
4318 1
4319#else
4320 0
4321#endif
4322 },
4323 {"spell",
4324#ifdef FEAT_SPELL
4325 1
4326#else
4327 0
4328#endif
4329 },
4330 {"syntax",
4331#ifdef FEAT_SYN_HL
4332 1
4333#else
4334 0
4335#endif
4336 },
4337 {"system",
4338#if defined(USE_SYSTEM) || !defined(UNIX)
4339 1
4340#else
4341 0
4342#endif
4343 },
4344 {"tag_binary",
4345#ifdef FEAT_TAG_BINS
4346 1
4347#else
4348 0
4349#endif
4350 },
4351 {"tcl",
4352#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4353 1
4354#else
4355 0
4356#endif
4357 },
4358 {"termguicolors",
4359#ifdef FEAT_TERMGUICOLORS
4360 1
4361#else
4362 0
4363#endif
4364 },
4365 {"terminal",
4366#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4367 1
4368#else
4369 0
4370#endif
4371 },
4372 {"terminfo",
4373#ifdef TERMINFO
4374 1
4375#else
4376 0
4377#endif
4378 },
4379 {"termresponse",
4380#ifdef FEAT_TERMRESPONSE
4381 1
4382#else
4383 0
4384#endif
4385 },
4386 {"textobjects",
4387#ifdef FEAT_TEXTOBJ
4388 1
4389#else
4390 0
4391#endif
4392 },
4393 {"textprop",
4394#ifdef FEAT_PROP_POPUP
4395 1
4396#else
4397 0
4398#endif
4399 },
4400 {"tgetent",
4401#ifdef HAVE_TGETENT
4402 1
4403#else
4404 0
4405#endif
4406 },
4407 {"timers",
4408#ifdef FEAT_TIMERS
4409 1
4410#else
4411 0
4412#endif
4413 },
4414 {"title",
4415#ifdef FEAT_TITLE
4416 1
4417#else
4418 0
4419#endif
4420 },
4421 {"toolbar",
4422#ifdef FEAT_TOOLBAR
4423 1
4424#else
4425 0
4426#endif
4427 },
4428 {"unnamedplus",
4429#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4430 1
4431#else
4432 0
4433#endif
4434 },
4435 {"user-commands", 1}, // was accidentally included in 5.4
4436 {"user_commands", 1},
4437 {"vartabs",
4438#ifdef FEAT_VARTABS
4439 1
4440#else
4441 0
4442#endif
4443 },
4444 {"vertsplit", 1},
4445 {"viminfo",
4446#ifdef FEAT_VIMINFO
4447 1
4448#else
4449 0
4450#endif
4451 },
4452 {"vimscript-1", 1},
4453 {"vimscript-2", 1},
4454 {"vimscript-3", 1},
4455 {"vimscript-4", 1},
4456 {"virtualedit", 1},
4457 {"visual", 1},
4458 {"visualextra", 1},
4459 {"vreplace", 1},
4460 {"vtp",
4461#ifdef FEAT_VTP
4462 1
4463#else
4464 0
4465#endif
4466 },
4467 {"wildignore",
4468#ifdef FEAT_WILDIGN
4469 1
4470#else
4471 0
4472#endif
4473 },
4474 {"wildmenu",
4475#ifdef FEAT_WILDMENU
4476 1
4477#else
4478 0
4479#endif
4480 },
4481 {"windows", 1},
4482 {"winaltkeys",
4483#ifdef FEAT_WAK
4484 1
4485#else
4486 0
4487#endif
4488 },
4489 {"writebackup",
4490#ifdef FEAT_WRITEBACKUP
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"xim",
4497#ifdef FEAT_XIM
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"xfontset",
4504#ifdef FEAT_XFONTSET
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"xpm",
4511#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4512 1
4513#else
4514 0
4515#endif
4516 },
4517 {"xpm_w32", // for backward compatibility
4518#ifdef FEAT_XPM_W32
4519 1
4520#else
4521 0
4522#endif
4523 },
4524 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004526 1
4527#else
4528 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004530 },
4531 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004533 1
4534#else
4535 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004537 },
4538 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004540 1
4541#else
4542 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004543#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004544 },
4545 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004547 1
4548#else
4549 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004551 },
4552 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004554 1
4555#else
4556 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004557#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004558 },
4559 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004560 };
4561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004562 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 for (i = 0; has_list[i].name != NULL; ++i)
4564 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 x = TRUE;
4567 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004568 break;
4569 }
4570
Bram Moolenaar79296512020-03-22 16:17:14 +01004571 // features also in has_list[] but sometimes enabled at runtime
4572 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004573 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004574 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004575 {
4576 // intentionally empty
4577 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004578#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579 else if (STRICMP(name, "balloon_multiline") == 0)
4580 n = multiline_balloon_available();
4581#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004582#ifdef VIMDLL
4583 else if (STRICMP(name, "filterpipe") == 0)
4584 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585#endif
4586#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4587 else if (STRICMP(name, "iconv") == 0)
4588 n = iconv_enabled(FALSE);
4589#endif
4590#ifdef DYNAMIC_LUA
4591 else if (STRICMP(name, "lua") == 0)
4592 n = lua_enabled(FALSE);
4593#endif
4594#ifdef DYNAMIC_MZSCHEME
4595 else if (STRICMP(name, "mzscheme") == 0)
4596 n = mzscheme_enabled(FALSE);
4597#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004598#ifdef DYNAMIC_PERL
4599 else if (STRICMP(name, "perl") == 0)
4600 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004602#ifdef DYNAMIC_PYTHON
4603 else if (STRICMP(name, "python") == 0)
4604 n = python_enabled(FALSE);
4605#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004606#ifdef DYNAMIC_PYTHON3
4607 else if (STRICMP(name, "python3") == 0)
4608 n = python3_enabled(FALSE);
4609#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004610#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4611 else if (STRICMP(name, "pythonx") == 0)
4612 {
4613# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4614 if (p_pyx == 0)
4615 n = python3_enabled(FALSE) || python_enabled(FALSE);
4616 else if (p_pyx == 3)
4617 n = python3_enabled(FALSE);
4618 else if (p_pyx == 2)
4619 n = python_enabled(FALSE);
4620# elif defined(DYNAMIC_PYTHON)
4621 n = python_enabled(FALSE);
4622# elif defined(DYNAMIC_PYTHON3)
4623 n = python3_enabled(FALSE);
4624# endif
4625 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004626#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004627#ifdef DYNAMIC_RUBY
4628 else if (STRICMP(name, "ruby") == 0)
4629 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004631#ifdef DYNAMIC_TCL
4632 else if (STRICMP(name, "tcl") == 0)
4633 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004634#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004635#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004636 else if (STRICMP(name, "terminal") == 0)
4637 n = terminal_enabled();
4638#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004639 }
4640
Bram Moolenaar79296512020-03-22 16:17:14 +01004641 // features not in has_list[]
4642 if (x == FALSE)
4643 {
4644 if (STRNICMP(name, "patch", 5) == 0)
4645 {
4646 x = TRUE;
4647 if (name[5] == '-'
4648 && STRLEN(name) >= 11
4649 && vim_isdigit(name[6])
4650 && vim_isdigit(name[8])
4651 && vim_isdigit(name[10]))
4652 {
4653 int major = atoi((char *)name + 6);
4654 int minor = atoi((char *)name + 8);
4655
4656 // Expect "patch-9.9.01234".
4657 n = (major < VIM_VERSION_MAJOR
4658 || (major == VIM_VERSION_MAJOR
4659 && (minor < VIM_VERSION_MINOR
4660 || (minor == VIM_VERSION_MINOR
4661 && has_patch(atoi((char *)name + 10))))));
4662 }
4663 else
4664 n = has_patch(atoi((char *)name + 5));
4665 }
4666 else if (STRICMP(name, "vim_starting") == 0)
4667 {
4668 x = TRUE;
4669 n = (starting != 0);
4670 }
4671 else if (STRICMP(name, "ttyin") == 0)
4672 {
4673 x = TRUE;
4674 n = mch_input_isatty();
4675 }
4676 else if (STRICMP(name, "ttyout") == 0)
4677 {
4678 x = TRUE;
4679 n = stdout_isatty;
4680 }
4681 else if (STRICMP(name, "multi_byte_encoding") == 0)
4682 {
4683 x = TRUE;
4684 n = has_mbyte;
4685 }
4686 else if (STRICMP(name, "gui_running") == 0)
4687 {
4688 x = TRUE;
4689#ifdef FEAT_GUI
4690 n = (gui.in_use || gui.starting);
4691#endif
4692 }
4693 else if (STRICMP(name, "browse") == 0)
4694 {
4695 x = TRUE;
4696#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4697 n = gui.in_use; // gui_mch_browse() works when GUI is running
4698#endif
4699 }
4700 else if (STRICMP(name, "syntax_items") == 0)
4701 {
4702 x = TRUE;
4703#ifdef FEAT_SYN_HL
4704 n = syntax_present(curwin);
4705#endif
4706 }
4707 else if (STRICMP(name, "vcon") == 0)
4708 {
4709 x = TRUE;
4710#ifdef FEAT_VTP
4711 n = is_term_win32() && has_vtp_working();
4712#endif
4713 }
4714 else if (STRICMP(name, "netbeans_enabled") == 0)
4715 {
4716 x = TRUE;
4717#ifdef FEAT_NETBEANS_INTG
4718 n = netbeans_active();
4719#endif
4720 }
4721 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4722 {
4723 x = TRUE;
4724#ifdef FEAT_MOUSE_GPM
4725 n = gpm_enabled();
4726#endif
4727 }
4728 else if (STRICMP(name, "conpty") == 0)
4729 {
4730 x = TRUE;
4731#if defined(FEAT_TERMINAL) && defined(MSWIN)
4732 n = use_conpty();
4733#endif
4734 }
4735 else if (STRICMP(name, "clipboard_working") == 0)
4736 {
4737 x = TRUE;
4738#ifdef FEAT_CLIPBOARD
4739 n = clip_star.available;
4740#endif
4741 }
4742 }
4743
4744 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4745 // return whether feature could ever be enabled
4746 rettv->vval.v_number = x;
4747 else
4748 // return whether feature is enabled
4749 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004750}
4751
4752/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004753 * "haslocaldir()" function
4754 */
4755 static void
4756f_haslocaldir(typval_T *argvars, typval_T *rettv)
4757{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004758 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759 win_T *wp = NULL;
4760
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004761 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4762
4763 // Check for window-local and tab-local directories
4764 if (wp != NULL && wp->w_localdir != NULL)
4765 rettv->vval.v_number = 1;
4766 else if (tp != NULL && tp->tp_localdir != NULL)
4767 rettv->vval.v_number = 2;
4768 else
4769 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770}
4771
4772/*
4773 * "hasmapto()" function
4774 */
4775 static void
4776f_hasmapto(typval_T *argvars, typval_T *rettv)
4777{
4778 char_u *name;
4779 char_u *mode;
4780 char_u buf[NUMBUFLEN];
4781 int abbr = FALSE;
4782
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004784 if (argvars[1].v_type == VAR_UNKNOWN)
4785 mode = (char_u *)"nvo";
4786 else
4787 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004788 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004790 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 }
4792
4793 if (map_to_exists(name, mode, abbr))
4794 rettv->vval.v_number = TRUE;
4795 else
4796 rettv->vval.v_number = FALSE;
4797}
4798
4799/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004800 * "highlightID(name)" function
4801 */
4802 static void
4803f_hlID(typval_T *argvars, typval_T *rettv)
4804{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004806}
4807
4808/*
4809 * "highlight_exists()" function
4810 */
4811 static void
4812f_hlexists(typval_T *argvars, typval_T *rettv)
4813{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004814 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815}
4816
4817/*
4818 * "hostname()" function
4819 */
4820 static void
4821f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4822{
4823 char_u hostname[256];
4824
4825 mch_get_host_name(hostname, 256);
4826 rettv->v_type = VAR_STRING;
4827 rettv->vval.v_string = vim_strsave(hostname);
4828}
4829
4830/*
4831 * iconv() function
4832 */
4833 static void
4834f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4835{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836 char_u buf1[NUMBUFLEN];
4837 char_u buf2[NUMBUFLEN];
4838 char_u *from, *to, *str;
4839 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840
4841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = NULL;
4843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 str = tv_get_string(&argvars[0]);
4845 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4846 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847 vimconv.vc_type = CONV_NONE;
4848 convert_setup(&vimconv, from, to);
4849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 if (vimconv.vc_type == CONV_NONE)
4852 rettv->vval.v_string = vim_strsave(str);
4853 else
4854 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4855
4856 convert_setup(&vimconv, NULL, NULL);
4857 vim_free(from);
4858 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859}
4860
4861/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862 * "index()" function
4863 */
4864 static void
4865f_index(typval_T *argvars, typval_T *rettv)
4866{
4867 list_T *l;
4868 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004869 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 long idx = 0;
4871 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004872 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873
4874 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004875 if (argvars[0].v_type == VAR_BLOB)
4876 {
4877 typval_T tv;
4878 int start = 0;
4879
4880 if (argvars[2].v_type != VAR_UNKNOWN)
4881 {
4882 start = tv_get_number_chk(&argvars[2], &error);
4883 if (error)
4884 return;
4885 }
4886 b = argvars[0].vval.v_blob;
4887 if (b == NULL)
4888 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004889 if (start < 0)
4890 {
4891 start = blob_len(b) + start;
4892 if (start < 0)
4893 start = 0;
4894 }
4895
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004896 for (idx = start; idx < blob_len(b); ++idx)
4897 {
4898 tv.v_type = VAR_NUMBER;
4899 tv.vval.v_number = blob_get(b, idx);
4900 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4901 {
4902 rettv->vval.v_number = idx;
4903 return;
4904 }
4905 }
4906 return;
4907 }
4908 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004910 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911 return;
4912 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 l = argvars[0].vval.v_list;
4915 if (l != NULL)
4916 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004917 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 item = l->lv_first;
4919 if (argvars[2].v_type != VAR_UNKNOWN)
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Start at specified item. Use the cached index that list_find()
4922 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004923 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004924 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004926 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004927 if (error)
4928 item = NULL;
4929 }
4930
4931 for ( ; item != NULL; item = item->li_next, ++idx)
4932 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4933 {
4934 rettv->vval.v_number = idx;
4935 break;
4936 }
4937 }
4938}
4939
4940static int inputsecret_flag = 0;
4941
4942/*
4943 * "input()" function
4944 * Also handles inputsecret() when inputsecret is set.
4945 */
4946 static void
4947f_input(typval_T *argvars, typval_T *rettv)
4948{
4949 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4950}
4951
4952/*
4953 * "inputdialog()" function
4954 */
4955 static void
4956f_inputdialog(typval_T *argvars, typval_T *rettv)
4957{
4958#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004959 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4961 {
4962 char_u *message;
4963 char_u buf[NUMBUFLEN];
4964 char_u *defstr = (char_u *)"";
4965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004966 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004968 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004969 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4970 else
4971 IObuff[0] = NUL;
4972 if (message != NULL && defstr != NULL
4973 && do_dialog(VIM_QUESTION, NULL, message,
4974 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
4975 rettv->vval.v_string = vim_strsave(IObuff);
4976 else
4977 {
4978 if (message != NULL && defstr != NULL
4979 && argvars[1].v_type != VAR_UNKNOWN
4980 && argvars[2].v_type != VAR_UNKNOWN)
4981 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004982 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004983 else
4984 rettv->vval.v_string = NULL;
4985 }
4986 rettv->v_type = VAR_STRING;
4987 }
4988 else
4989#endif
4990 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
4991}
4992
4993/*
4994 * "inputlist()" function
4995 */
4996 static void
4997f_inputlist(typval_T *argvars, typval_T *rettv)
4998{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004999 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005000 listitem_T *li;
5001 int selected;
5002 int mouse_used;
5003
5004#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 // While starting up, there is no place to enter text. When running tests
5006 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005007 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008 return;
5009#endif
5010 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005012 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 return;
5014 }
5015
5016 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005017 msg_row = Rows - 1; // for when 'cmdheight' > 1
5018 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005019 msg_scroll = TRUE;
5020 msg_clr_eos();
5021
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005022 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005023 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005024 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005026 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005027 msg_putchar('\n');
5028 }
5029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005030 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 selected = prompt_for_number(&mouse_used);
5032 if (mouse_used)
5033 selected -= lines_left;
5034
5035 rettv->vval.v_number = selected;
5036}
5037
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005038static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5039
5040/*
5041 * "inputrestore()" function
5042 */
5043 static void
5044f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5045{
5046 if (ga_userinput.ga_len > 0)
5047 {
5048 --ga_userinput.ga_len;
5049 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5050 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052 }
5053 else if (p_verbose > 1)
5054 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005055 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 }
5058}
5059
5060/*
5061 * "inputsave()" function
5062 */
5063 static void
5064f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5065{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005066 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005067 if (ga_grow(&ga_userinput, 1) == OK)
5068 {
5069 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5070 + ga_userinput.ga_len);
5071 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005073 }
5074 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005076}
5077
5078/*
5079 * "inputsecret()" function
5080 */
5081 static void
5082f_inputsecret(typval_T *argvars, typval_T *rettv)
5083{
5084 ++cmdline_star;
5085 ++inputsecret_flag;
5086 f_input(argvars, rettv);
5087 --cmdline_star;
5088 --inputsecret_flag;
5089}
5090
5091/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005092 * "interrupt()" function
5093 */
5094 static void
5095f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5096{
5097 got_int = TRUE;
5098}
5099
5100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 * "invert(expr)" function
5102 */
5103 static void
5104f_invert(typval_T *argvars, typval_T *rettv)
5105{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005106 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107}
5108
5109/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 * "islocked()" function
5111 */
5112 static void
5113f_islocked(typval_T *argvars, typval_T *rettv)
5114{
5115 lval_T lv;
5116 char_u *end;
5117 dictitem_T *di;
5118
5119 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005120 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005121 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 if (end != NULL && lv.ll_name != NULL)
5123 {
5124 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005125 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 else
5127 {
5128 if (lv.ll_tv == NULL)
5129 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005130 di = find_var(lv.ll_name, NULL, TRUE);
5131 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005133 // Consider a variable locked when:
5134 // 1. the variable itself is locked
5135 // 2. the value of the variable is locked.
5136 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005137 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5138 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 }
5140 }
5141 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005142 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005144 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005145 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005147 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5148 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5151 }
5152 }
5153
5154 clear_lval(&lv);
5155}
5156
5157#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5158/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005159 * "isinf()" function
5160 */
5161 static void
5162f_isinf(typval_T *argvars, typval_T *rettv)
5163{
5164 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5165 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5166}
5167
5168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 * "isnan()" function
5170 */
5171 static void
5172f_isnan(typval_T *argvars, typval_T *rettv)
5173{
5174 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5175 && isnan(argvars[0].vval.v_float);
5176}
5177#endif
5178
5179/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005180 * "last_buffer_nr()" function.
5181 */
5182 static void
5183f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5184{
5185 int n = 0;
5186 buf_T *buf;
5187
Bram Moolenaar29323592016-07-24 22:04:11 +02005188 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005189 if (n < buf->b_fnum)
5190 n = buf->b_fnum;
5191
5192 rettv->vval.v_number = n;
5193}
5194
5195/*
5196 * "len()" function
5197 */
5198 static void
5199f_len(typval_T *argvars, typval_T *rettv)
5200{
5201 switch (argvars[0].v_type)
5202 {
5203 case VAR_STRING:
5204 case VAR_NUMBER:
5205 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005206 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005207 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005208 case VAR_BLOB:
5209 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5210 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211 case VAR_LIST:
5212 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5213 break;
5214 case VAR_DICT:
5215 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5216 break;
5217 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005218 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005220 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005221 case VAR_SPECIAL:
5222 case VAR_FLOAT:
5223 case VAR_FUNC:
5224 case VAR_PARTIAL:
5225 case VAR_JOB:
5226 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005227 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228 break;
5229 }
5230}
5231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005232 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005233libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234{
5235#ifdef FEAT_LIBCALL
5236 char_u *string_in;
5237 char_u **string_result;
5238 int nr_result;
5239#endif
5240
5241 rettv->v_type = type;
5242 if (type != VAR_NUMBER)
5243 rettv->vval.v_string = NULL;
5244
5245 if (check_restricted() || check_secure())
5246 return;
5247
5248#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005249 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005250 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5251 {
5252 string_in = NULL;
5253 if (argvars[2].v_type == VAR_STRING)
5254 string_in = argvars[2].vval.v_string;
5255 if (type == VAR_NUMBER)
5256 string_result = NULL;
5257 else
5258 string_result = &rettv->vval.v_string;
5259 if (mch_libcall(argvars[0].vval.v_string,
5260 argvars[1].vval.v_string,
5261 string_in,
5262 argvars[2].vval.v_number,
5263 string_result,
5264 &nr_result) == OK
5265 && type == VAR_NUMBER)
5266 rettv->vval.v_number = nr_result;
5267 }
5268#endif
5269}
5270
5271/*
5272 * "libcall()" function
5273 */
5274 static void
5275f_libcall(typval_T *argvars, typval_T *rettv)
5276{
5277 libcall_common(argvars, rettv, VAR_STRING);
5278}
5279
5280/*
5281 * "libcallnr()" function
5282 */
5283 static void
5284f_libcallnr(typval_T *argvars, typval_T *rettv)
5285{
5286 libcall_common(argvars, rettv, VAR_NUMBER);
5287}
5288
5289/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005290 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 */
5292 static void
5293f_line(typval_T *argvars, typval_T *rettv)
5294{
5295 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005296 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005297 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005298 int id;
5299 tabpage_T *tp;
5300 win_T *wp;
5301 win_T *save_curwin;
5302 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005304 if (argvars[1].v_type != VAR_UNKNOWN)
5305 {
5306 // use window specified in the second argument
5307 id = (int)tv_get_number(&argvars[1]);
5308 wp = win_id2wp_tp(id, &tp);
5309 if (wp != NULL && tp != NULL)
5310 {
5311 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5312 == OK)
5313 {
5314 check_cursor();
5315 fp = var2fpos(&argvars[0], TRUE, &fnum);
5316 }
5317 restore_win_noblock(save_curwin, save_curtab, TRUE);
5318 }
5319 }
5320 else
5321 // use current window
5322 fp = var2fpos(&argvars[0], TRUE, &fnum);
5323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 if (fp != NULL)
5325 lnum = fp->lnum;
5326 rettv->vval.v_number = lnum;
5327}
5328
5329/*
5330 * "line2byte(lnum)" function
5331 */
5332 static void
5333f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5334{
5335#ifndef FEAT_BYTEOFF
5336 rettv->vval.v_number = -1;
5337#else
5338 linenr_T lnum;
5339
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005340 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5342 rettv->vval.v_number = -1;
5343 else
5344 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5345 if (rettv->vval.v_number >= 0)
5346 ++rettv->vval.v_number;
5347#endif
5348}
5349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350#ifdef FEAT_FLOAT
5351/*
5352 * "log()" function
5353 */
5354 static void
5355f_log(typval_T *argvars, typval_T *rettv)
5356{
5357 float_T f = 0.0;
5358
5359 rettv->v_type = VAR_FLOAT;
5360 if (get_float_arg(argvars, &f) == OK)
5361 rettv->vval.v_float = log(f);
5362 else
5363 rettv->vval.v_float = 0.0;
5364}
5365
5366/*
5367 * "log10()" function
5368 */
5369 static void
5370f_log10(typval_T *argvars, typval_T *rettv)
5371{
5372 float_T f = 0.0;
5373
5374 rettv->v_type = VAR_FLOAT;
5375 if (get_float_arg(argvars, &f) == OK)
5376 rettv->vval.v_float = log10(f);
5377 else
5378 rettv->vval.v_float = 0.0;
5379}
5380#endif
5381
5382#ifdef FEAT_LUA
5383/*
5384 * "luaeval()" function
5385 */
5386 static void
5387f_luaeval(typval_T *argvars, typval_T *rettv)
5388{
5389 char_u *str;
5390 char_u buf[NUMBUFLEN];
5391
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005392 if (check_restricted() || check_secure())
5393 return;
5394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005395 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396 do_luaeval(str, argvars + 1, rettv);
5397}
5398#endif
5399
5400/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005401 * "maparg()" function
5402 */
5403 static void
5404f_maparg(typval_T *argvars, typval_T *rettv)
5405{
5406 get_maparg(argvars, rettv, TRUE);
5407}
5408
5409/*
5410 * "mapcheck()" function
5411 */
5412 static void
5413f_mapcheck(typval_T *argvars, typval_T *rettv)
5414{
5415 get_maparg(argvars, rettv, FALSE);
5416}
5417
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005418typedef enum
5419{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005420 MATCH_END, // matchend()
5421 MATCH_MATCH, // match()
5422 MATCH_STR, // matchstr()
5423 MATCH_LIST, // matchlist()
5424 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005425} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426
5427 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005428find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005429{
5430 char_u *str = NULL;
5431 long len = 0;
5432 char_u *expr = NULL;
5433 char_u *pat;
5434 regmatch_T regmatch;
5435 char_u patbuf[NUMBUFLEN];
5436 char_u strbuf[NUMBUFLEN];
5437 char_u *save_cpo;
5438 long start = 0;
5439 long nth = 1;
5440 colnr_T startcol = 0;
5441 int match = 0;
5442 list_T *l = NULL;
5443 listitem_T *li = NULL;
5444 long idx = 0;
5445 char_u *tofree = NULL;
5446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005448 save_cpo = p_cpo;
5449 p_cpo = (char_u *)"";
5450
5451 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005452 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 // type MATCH_LIST: return empty list when there are no matches.
5455 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005456 if (rettv_list_alloc(rettv) == FAIL)
5457 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005458 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005459 && (list_append_string(rettv->vval.v_list,
5460 (char_u *)"", 0) == FAIL
5461 || list_append_number(rettv->vval.v_list,
5462 (varnumber_T)-1) == FAIL
5463 || list_append_number(rettv->vval.v_list,
5464 (varnumber_T)-1) == FAIL
5465 || list_append_number(rettv->vval.v_list,
5466 (varnumber_T)-1) == FAIL))
5467 {
5468 list_free(rettv->vval.v_list);
5469 rettv->vval.v_list = NULL;
5470 goto theend;
5471 }
5472 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005473 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 {
5475 rettv->v_type = VAR_STRING;
5476 rettv->vval.v_string = NULL;
5477 }
5478
5479 if (argvars[0].v_type == VAR_LIST)
5480 {
5481 if ((l = argvars[0].vval.v_list) == NULL)
5482 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005483 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 li = l->lv_first;
5485 }
5486 else
5487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005488 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005489 len = (long)STRLEN(str);
5490 }
5491
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 if (pat == NULL)
5494 goto theend;
5495
5496 if (argvars[2].v_type != VAR_UNKNOWN)
5497 {
5498 int error = FALSE;
5499
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005500 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 if (error)
5502 goto theend;
5503 if (l != NULL)
5504 {
5505 li = list_find(l, start);
5506 if (li == NULL)
5507 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005508 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509 }
5510 else
5511 {
5512 if (start < 0)
5513 start = 0;
5514 if (start > len)
5515 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // When "count" argument is there ignore matches before "start",
5517 // otherwise skip part of the string. Differs when pattern is "^"
5518 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519 if (argvars[3].v_type != VAR_UNKNOWN)
5520 startcol = start;
5521 else
5522 {
5523 str += start;
5524 len -= start;
5525 }
5526 }
5527
5528 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005529 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530 if (error)
5531 goto theend;
5532 }
5533
5534 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5535 if (regmatch.regprog != NULL)
5536 {
5537 regmatch.rm_ic = p_ic;
5538
5539 for (;;)
5540 {
5541 if (l != NULL)
5542 {
5543 if (li == NULL)
5544 {
5545 match = FALSE;
5546 break;
5547 }
5548 vim_free(tofree);
5549 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5550 if (str == NULL)
5551 break;
5552 }
5553
5554 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5555
5556 if (match && --nth <= 0)
5557 break;
5558 if (l == NULL && !match)
5559 break;
5560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005561 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562 if (l != NULL)
5563 {
5564 li = li->li_next;
5565 ++idx;
5566 }
5567 else
5568 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005569 startcol = (colnr_T)(regmatch.startp[0]
5570 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 if (startcol > (colnr_T)len
5572 || str + startcol <= regmatch.startp[0])
5573 {
5574 match = FALSE;
5575 break;
5576 }
5577 }
5578 }
5579
5580 if (match)
5581 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005582 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 {
5584 listitem_T *li1 = rettv->vval.v_list->lv_first;
5585 listitem_T *li2 = li1->li_next;
5586 listitem_T *li3 = li2->li_next;
5587 listitem_T *li4 = li3->li_next;
5588
5589 vim_free(li1->li_tv.vval.v_string);
5590 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005591 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 li3->li_tv.vval.v_number =
5593 (varnumber_T)(regmatch.startp[0] - expr);
5594 li4->li_tv.vval.v_number =
5595 (varnumber_T)(regmatch.endp[0] - expr);
5596 if (l != NULL)
5597 li2->li_tv.vval.v_number = (varnumber_T)idx;
5598 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005599 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005600 {
5601 int i;
5602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005603 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 for (i = 0; i < NSUBEXP; ++i)
5605 {
5606 if (regmatch.endp[i] == NULL)
5607 {
5608 if (list_append_string(rettv->vval.v_list,
5609 (char_u *)"", 0) == FAIL)
5610 break;
5611 }
5612 else if (list_append_string(rettv->vval.v_list,
5613 regmatch.startp[i],
5614 (int)(regmatch.endp[i] - regmatch.startp[i]))
5615 == FAIL)
5616 break;
5617 }
5618 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005619 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005621 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 if (l != NULL)
5623 copy_tv(&li->li_tv, rettv);
5624 else
5625 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005626 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 }
5628 else if (l != NULL)
5629 rettv->vval.v_number = idx;
5630 else
5631 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005632 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633 rettv->vval.v_number =
5634 (varnumber_T)(regmatch.startp[0] - str);
5635 else
5636 rettv->vval.v_number =
5637 (varnumber_T)(regmatch.endp[0] - str);
5638 rettv->vval.v_number += (varnumber_T)(str - expr);
5639 }
5640 }
5641 vim_regfree(regmatch.regprog);
5642 }
5643
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005644theend:
5645 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005646 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647 listitem_remove(rettv->vval.v_list,
5648 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 vim_free(tofree);
5650 p_cpo = save_cpo;
5651}
5652
5653/*
5654 * "match()" function
5655 */
5656 static void
5657f_match(typval_T *argvars, typval_T *rettv)
5658{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005659 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660}
5661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662/*
5663 * "matchend()" function
5664 */
5665 static void
5666f_matchend(typval_T *argvars, typval_T *rettv)
5667{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005668 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669}
5670
5671/*
5672 * "matchlist()" function
5673 */
5674 static void
5675f_matchlist(typval_T *argvars, typval_T *rettv)
5676{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005677 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678}
5679
5680/*
5681 * "matchstr()" function
5682 */
5683 static void
5684f_matchstr(typval_T *argvars, typval_T *rettv)
5685{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005686 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687}
5688
5689/*
5690 * "matchstrpos()" function
5691 */
5692 static void
5693f_matchstrpos(typval_T *argvars, typval_T *rettv)
5694{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005695 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696}
5697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005698 static void
5699max_min(typval_T *argvars, typval_T *rettv, int domax)
5700{
5701 varnumber_T n = 0;
5702 varnumber_T i;
5703 int error = FALSE;
5704
5705 if (argvars[0].v_type == VAR_LIST)
5706 {
5707 list_T *l;
5708 listitem_T *li;
5709
5710 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005711 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005712 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005713 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005715 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5716 n = l->lv_u.nonmat.lv_start;
5717 else
5718 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5719 * l->lv_u.nonmat.lv_stride;
5720 }
5721 else
5722 {
5723 li = l->lv_first;
5724 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005726 n = tv_get_number_chk(&li->li_tv, &error);
5727 for (;;)
5728 {
5729 li = li->li_next;
5730 if (li == NULL)
5731 break;
5732 i = tv_get_number_chk(&li->li_tv, &error);
5733 if (domax ? i > n : i < n)
5734 n = i;
5735 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 }
5737 }
5738 }
5739 }
5740 else if (argvars[0].v_type == VAR_DICT)
5741 {
5742 dict_T *d;
5743 int first = TRUE;
5744 hashitem_T *hi;
5745 int todo;
5746
5747 d = argvars[0].vval.v_dict;
5748 if (d != NULL)
5749 {
5750 todo = (int)d->dv_hashtab.ht_used;
5751 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5752 {
5753 if (!HASHITEM_EMPTY(hi))
5754 {
5755 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005756 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005757 if (first)
5758 {
5759 n = i;
5760 first = FALSE;
5761 }
5762 else if (domax ? i > n : i < n)
5763 n = i;
5764 }
5765 }
5766 }
5767 }
5768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005769 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 rettv->vval.v_number = error ? 0 : n;
5771}
5772
5773/*
5774 * "max()" function
5775 */
5776 static void
5777f_max(typval_T *argvars, typval_T *rettv)
5778{
5779 max_min(argvars, rettv, TRUE);
5780}
5781
5782/*
5783 * "min()" function
5784 */
5785 static void
5786f_min(typval_T *argvars, typval_T *rettv)
5787{
5788 max_min(argvars, rettv, FALSE);
5789}
5790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005791#if defined(FEAT_MZSCHEME) || defined(PROTO)
5792/*
5793 * "mzeval()" function
5794 */
5795 static void
5796f_mzeval(typval_T *argvars, typval_T *rettv)
5797{
5798 char_u *str;
5799 char_u buf[NUMBUFLEN];
5800
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005801 if (check_restricted() || check_secure())
5802 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005803 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005804 do_mzeval(str, rettv);
5805}
5806
5807 void
5808mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5809{
5810 typval_T argvars[3];
5811
5812 argvars[0].v_type = VAR_STRING;
5813 argvars[0].vval.v_string = name;
5814 copy_tv(args, &argvars[1]);
5815 argvars[2].v_type = VAR_UNKNOWN;
5816 f_call(argvars, rettv);
5817 clear_tv(&argvars[1]);
5818}
5819#endif
5820
5821/*
5822 * "nextnonblank()" function
5823 */
5824 static void
5825f_nextnonblank(typval_T *argvars, typval_T *rettv)
5826{
5827 linenr_T lnum;
5828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005829 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 {
5831 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5832 {
5833 lnum = 0;
5834 break;
5835 }
5836 if (*skipwhite(ml_get(lnum)) != NUL)
5837 break;
5838 }
5839 rettv->vval.v_number = lnum;
5840}
5841
5842/*
5843 * "nr2char()" function
5844 */
5845 static void
5846f_nr2char(typval_T *argvars, typval_T *rettv)
5847{
5848 char_u buf[NUMBUFLEN];
5849
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 if (has_mbyte)
5851 {
5852 int utf8 = 0;
5853
5854 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005857 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 }
5861 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 buf[1] = NUL;
5865 }
5866 rettv->v_type = VAR_STRING;
5867 rettv->vval.v_string = vim_strsave(buf);
5868}
5869
5870/*
5871 * "or(expr, expr)" function
5872 */
5873 static void
5874f_or(typval_T *argvars, typval_T *rettv)
5875{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005876 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5877 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878}
5879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880#ifdef FEAT_PERL
5881/*
5882 * "perleval()" function
5883 */
5884 static void
5885f_perleval(typval_T *argvars, typval_T *rettv)
5886{
5887 char_u *str;
5888 char_u buf[NUMBUFLEN];
5889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005890 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 do_perleval(str, rettv);
5892}
5893#endif
5894
5895#ifdef FEAT_FLOAT
5896/*
5897 * "pow()" function
5898 */
5899 static void
5900f_pow(typval_T *argvars, typval_T *rettv)
5901{
5902 float_T fx = 0.0, fy = 0.0;
5903
5904 rettv->v_type = VAR_FLOAT;
5905 if (get_float_arg(argvars, &fx) == OK
5906 && get_float_arg(&argvars[1], &fy) == OK)
5907 rettv->vval.v_float = pow(fx, fy);
5908 else
5909 rettv->vval.v_float = 0.0;
5910}
5911#endif
5912
5913/*
5914 * "prevnonblank()" function
5915 */
5916 static void
5917f_prevnonblank(typval_T *argvars, typval_T *rettv)
5918{
5919 linenr_T lnum;
5920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005921 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005922 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5923 lnum = 0;
5924 else
5925 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5926 --lnum;
5927 rettv->vval.v_number = lnum;
5928}
5929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930// This dummy va_list is here because:
5931// - passing a NULL pointer doesn't work when va_list isn't a pointer
5932// - locally in the function results in a "used before set" warning
5933// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934static va_list ap;
5935
5936/*
5937 * "printf()" function
5938 */
5939 static void
5940f_printf(typval_T *argvars, typval_T *rettv)
5941{
5942 char_u buf[NUMBUFLEN];
5943 int len;
5944 char_u *s;
5945 int saved_did_emsg = did_emsg;
5946 char *fmt;
5947
5948 rettv->v_type = VAR_STRING;
5949 rettv->vval.v_string = NULL;
5950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005951 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005953 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005954 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955 if (!did_emsg)
5956 {
5957 s = alloc(len + 1);
5958 if (s != NULL)
5959 {
5960 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005961 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5962 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 }
5964 }
5965 did_emsg |= saved_did_emsg;
5966}
5967
5968/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005969 * "pum_getpos()" function
5970 */
5971 static void
5972f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5973{
5974 if (rettv_dict_alloc(rettv) != OK)
5975 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005976 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005977}
5978
5979/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005980 * "pumvisible()" function
5981 */
5982 static void
5983f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5984{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 if (pum_visible())
5986 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987}
5988
5989#ifdef FEAT_PYTHON3
5990/*
5991 * "py3eval()" function
5992 */
5993 static void
5994f_py3eval(typval_T *argvars, typval_T *rettv)
5995{
5996 char_u *str;
5997 char_u buf[NUMBUFLEN];
5998
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005999 if (check_restricted() || check_secure())
6000 return;
6001
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006002 if (p_pyx == 0)
6003 p_pyx = 3;
6004
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006005 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 do_py3eval(str, rettv);
6007}
6008#endif
6009
6010#ifdef FEAT_PYTHON
6011/*
6012 * "pyeval()" function
6013 */
6014 static void
6015f_pyeval(typval_T *argvars, typval_T *rettv)
6016{
6017 char_u *str;
6018 char_u buf[NUMBUFLEN];
6019
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006020 if (check_restricted() || check_secure())
6021 return;
6022
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006023 if (p_pyx == 0)
6024 p_pyx = 2;
6025
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006026 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 do_pyeval(str, rettv);
6028}
6029#endif
6030
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006031#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6032/*
6033 * "pyxeval()" function
6034 */
6035 static void
6036f_pyxeval(typval_T *argvars, typval_T *rettv)
6037{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006038 if (check_restricted() || check_secure())
6039 return;
6040
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006041# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6042 init_pyxversion();
6043 if (p_pyx == 2)
6044 f_pyeval(argvars, rettv);
6045 else
6046 f_py3eval(argvars, rettv);
6047# elif defined(FEAT_PYTHON)
6048 f_pyeval(argvars, rettv);
6049# elif defined(FEAT_PYTHON3)
6050 f_py3eval(argvars, rettv);
6051# endif
6052}
6053#endif
6054
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006055static UINT32_T srand_seed_for_testing = 0;
6056static int srand_seed_for_testing_is_used = FALSE;
6057
6058 static void
6059f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6060{
6061 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006062 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006063 else
6064 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006065 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6066 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006067 }
6068}
6069
6070 static void
6071init_srand(UINT32_T *x)
6072{
6073#ifndef MSWIN
6074 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6075#endif
6076
6077 if (srand_seed_for_testing_is_used)
6078 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006079 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006080 return;
6081 }
6082#ifndef MSWIN
6083 if (dev_urandom_state != FAIL)
6084 {
6085 int fd = open("/dev/urandom", O_RDONLY);
6086 struct {
6087 union {
6088 UINT32_T number;
6089 char bytes[sizeof(UINT32_T)];
6090 } contents;
6091 } buf;
6092
6093 // Attempt reading /dev/urandom.
6094 if (fd == -1)
6095 dev_urandom_state = FAIL;
6096 else
6097 {
6098 buf.contents.number = 0;
6099 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6100 != sizeof(UINT32_T))
6101 dev_urandom_state = FAIL;
6102 else
6103 {
6104 dev_urandom_state = OK;
6105 *x = buf.contents.number;
6106 }
6107 close(fd);
6108 }
6109 }
6110 if (dev_urandom_state != OK)
6111 // Reading /dev/urandom doesn't work, fall back to time().
6112#endif
6113 *x = vim_time();
6114}
6115
6116#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6117#define SPLITMIX32(x, z) ( \
6118 z = (x += 0x9e3779b9), \
6119 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6120 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6121 z ^ (z >> 16) \
6122 )
6123#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6124 result = ROTL(y * 5, 7) * 9; \
6125 t = y << 9; \
6126 z ^= x; \
6127 w ^= y; \
6128 y ^= z, x ^= w; \
6129 z ^= t; \
6130 w = ROTL(w, 11);
6131
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006133 * "rand()" function
6134 */
6135 static void
6136f_rand(typval_T *argvars, typval_T *rettv)
6137{
6138 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006139 static UINT32_T gx, gy, gz, gw;
6140 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006141 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006142 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006143
6144 if (argvars[0].v_type == VAR_UNKNOWN)
6145 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006146 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006147 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006148 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006149 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006150 init_srand(&x);
6151
6152 gx = SPLITMIX32(x, z);
6153 gy = SPLITMIX32(x, z);
6154 gz = SPLITMIX32(x, z);
6155 gw = SPLITMIX32(x, z);
6156 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006157 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006158
6159 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006160 }
6161 else if (argvars[0].v_type == VAR_LIST)
6162 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006163 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006164 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006165 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006166
6167 lx = list_find(l, 0L);
6168 ly = list_find(l, 1L);
6169 lz = list_find(l, 2L);
6170 lw = list_find(l, 3L);
6171 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6172 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6173 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6174 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6175 x = (UINT32_T)lx->li_tv.vval.v_number;
6176 y = (UINT32_T)ly->li_tv.vval.v_number;
6177 z = (UINT32_T)lz->li_tv.vval.v_number;
6178 w = (UINT32_T)lw->li_tv.vval.v_number;
6179
6180 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6181
6182 lx->li_tv.vval.v_number = (varnumber_T)x;
6183 ly->li_tv.vval.v_number = (varnumber_T)y;
6184 lz->li_tv.vval.v_number = (varnumber_T)z;
6185 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006186 }
6187 else
6188 goto theend;
6189
6190 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006191 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006192 return;
6193
6194theend:
6195 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006196 rettv->v_type = VAR_NUMBER;
6197 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006198}
6199
6200/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006201 * "srand()" function
6202 */
6203 static void
6204f_srand(typval_T *argvars, typval_T *rettv)
6205{
6206 UINT32_T x = 0, z;
6207
6208 if (rettv_list_alloc(rettv) == FAIL)
6209 return;
6210 if (argvars[0].v_type == VAR_UNKNOWN)
6211 {
6212 init_srand(&x);
6213 }
6214 else
6215 {
6216 int error = FALSE;
6217
6218 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6219 if (error)
6220 return;
6221 }
6222
6223 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6224 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6225 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6226 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6227}
6228
6229#undef ROTL
6230#undef SPLITMIX32
6231#undef SHUFFLE_XOSHIRO128STARSTAR
6232
6233/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234 * "range()" function
6235 */
6236 static void
6237f_range(typval_T *argvars, typval_T *rettv)
6238{
6239 varnumber_T start;
6240 varnumber_T end;
6241 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 int error = FALSE;
6243
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006244 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 if (argvars[1].v_type == VAR_UNKNOWN)
6246 {
6247 end = start - 1;
6248 start = 0;
6249 }
6250 else
6251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006252 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006254 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 }
6256
6257 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006258 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006260 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006262 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006263 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006265 list_T *list = rettv->vval.v_list;
6266
6267 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006268 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 // be called.
6270 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006271 list->lv_u.nonmat.lv_start = start;
6272 list->lv_u.nonmat.lv_end = end;
6273 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006274 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 }
6276}
6277
6278/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006279 * Materialize "list".
6280 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006281 */
6282 void
6283range_list_materialize(list_T *list)
6284{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006285 varnumber_T start = list->lv_u.nonmat.lv_start;
6286 varnumber_T end = list->lv_u.nonmat.lv_end;
6287 int stride = list->lv_u.nonmat.lv_stride;
6288 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006290 list->lv_first = NULL;
6291 list->lv_u.mat.lv_last = NULL;
6292 list->lv_len = 0;
6293 list->lv_u.mat.lv_idx_item = NULL;
6294 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6295 if (list_append_number(list, (varnumber_T)i) == FAIL)
6296 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297}
6298
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006299/*
6300 * "getreginfo()" function
6301 */
6302 static void
6303f_getreginfo(typval_T *argvars, typval_T *rettv)
6304{
6305 char_u *strregname;
6306 int regname;
6307 char_u buf[NUMBUFLEN + 2];
6308 long reglen = 0;
6309 dict_T *dict;
6310 list_T *list;
6311
6312 if (argvars[0].v_type != VAR_UNKNOWN)
6313 {
6314 strregname = tv_get_string_chk(&argvars[0]);
6315 if (strregname == NULL)
6316 return;
6317 }
6318 else
6319 strregname = get_vim_var_str(VV_REG);
6320
6321 regname = (strregname == NULL ? '"' : *strregname);
6322 if (regname == 0 || regname == '@')
6323 regname = '"';
6324
6325 if (rettv_dict_alloc(rettv) == FAIL)
6326 return;
6327 dict = rettv->vval.v_dict;
6328
6329 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6330 if (list == NULL)
6331 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006332 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006333
6334 buf[0] = NUL;
6335 buf[1] = NUL;
6336 switch (get_reg_type(regname, &reglen))
6337 {
6338 case MLINE: buf[0] = 'V'; break;
6339 case MCHAR: buf[0] = 'v'; break;
6340 case MBLOCK:
6341 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6342 reglen + 1);
6343 break;
6344 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006345 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006346
6347 buf[0] = get_register_name(get_unname_register());
6348 buf[1] = NUL;
6349 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006350 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006351 else
6352 {
6353 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6354
6355 if (item != NULL)
6356 {
6357 item->di_tv.v_type = VAR_SPECIAL;
6358 item->di_tv.vval.v_number = regname == buf[0]
6359 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006360 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006361 }
6362 }
6363}
6364
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006365 static void
6366return_register(int regname, typval_T *rettv)
6367{
6368 char_u buf[2] = {0, 0};
6369
6370 buf[0] = (char_u)regname;
6371 rettv->v_type = VAR_STRING;
6372 rettv->vval.v_string = vim_strsave(buf);
6373}
6374
6375/*
6376 * "reg_executing()" function
6377 */
6378 static void
6379f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6380{
6381 return_register(reg_executing, rettv);
6382}
6383
6384/*
6385 * "reg_recording()" function
6386 */
6387 static void
6388f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6389{
6390 return_register(reg_recording, rettv);
6391}
6392
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006393/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394 * "rename({from}, {to})" function
6395 */
6396 static void
6397f_rename(typval_T *argvars, typval_T *rettv)
6398{
6399 char_u buf[NUMBUFLEN];
6400
6401 if (check_restricted() || check_secure())
6402 rettv->vval.v_number = -1;
6403 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006404 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6405 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406}
6407
6408/*
6409 * "repeat()" function
6410 */
6411 static void
6412f_repeat(typval_T *argvars, typval_T *rettv)
6413{
6414 char_u *p;
6415 int n;
6416 int slen;
6417 int len;
6418 char_u *r;
6419 int i;
6420
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006421 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 if (argvars[0].v_type == VAR_LIST)
6423 {
6424 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6425 while (n-- > 0)
6426 if (list_extend(rettv->vval.v_list,
6427 argvars[0].vval.v_list, NULL) == FAIL)
6428 break;
6429 }
6430 else
6431 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006432 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433 rettv->v_type = VAR_STRING;
6434 rettv->vval.v_string = NULL;
6435
6436 slen = (int)STRLEN(p);
6437 len = slen * n;
6438 if (len <= 0)
6439 return;
6440
6441 r = alloc(len + 1);
6442 if (r != NULL)
6443 {
6444 for (i = 0; i < n; i++)
6445 mch_memmove(r + i * slen, p, (size_t)slen);
6446 r[len] = NUL;
6447 }
6448
6449 rettv->vval.v_string = r;
6450 }
6451}
6452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006453#define SP_NOMOVE 0x01 // don't move cursor
6454#define SP_REPEAT 0x02 // repeat to find outer pair
6455#define SP_RETCOUNT 0x04 // return matchcount
6456#define SP_SETPCMARK 0x08 // set previous context mark
6457#define SP_START 0x10 // accept match at start position
6458#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6459#define SP_END 0x40 // leave cursor at end of match
6460#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462/*
6463 * Get flags for a search function.
6464 * Possibly sets "p_ws".
6465 * Returns BACKWARD, FORWARD or zero (for an error).
6466 */
6467 static int
6468get_search_arg(typval_T *varp, int *flagsp)
6469{
6470 int dir = FORWARD;
6471 char_u *flags;
6472 char_u nbuf[NUMBUFLEN];
6473 int mask;
6474
6475 if (varp->v_type != VAR_UNKNOWN)
6476 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006477 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480 while (*flags != NUL)
6481 {
6482 switch (*flags)
6483 {
6484 case 'b': dir = BACKWARD; break;
6485 case 'w': p_ws = TRUE; break;
6486 case 'W': p_ws = FALSE; break;
6487 default: mask = 0;
6488 if (flagsp != NULL)
6489 switch (*flags)
6490 {
6491 case 'c': mask = SP_START; break;
6492 case 'e': mask = SP_END; break;
6493 case 'm': mask = SP_RETCOUNT; break;
6494 case 'n': mask = SP_NOMOVE; break;
6495 case 'p': mask = SP_SUBPAT; break;
6496 case 'r': mask = SP_REPEAT; break;
6497 case 's': mask = SP_SETPCMARK; break;
6498 case 'z': mask = SP_COLUMN; break;
6499 }
6500 if (mask == 0)
6501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006502 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 dir = 0;
6504 }
6505 else
6506 *flagsp |= mask;
6507 }
6508 if (dir == 0)
6509 break;
6510 ++flags;
6511 }
6512 }
6513 return dir;
6514}
6515
6516/*
6517 * Shared by search() and searchpos() functions.
6518 */
6519 static int
6520search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6521{
6522 int flags;
6523 char_u *pat;
6524 pos_T pos;
6525 pos_T save_cursor;
6526 int save_p_ws = p_ws;
6527 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006528 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006531 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 long time_limit = 0;
6533#endif
6534 int options = SEARCH_KEEP;
6535 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006536 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006537 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006538 pos_T firstpos;
6539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006540 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006541 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 if (dir == 0)
6543 goto theend;
6544 flags = *flagsp;
6545 if (flags & SP_START)
6546 options |= SEARCH_START;
6547 if (flags & SP_END)
6548 options |= SEARCH_END;
6549 if (flags & SP_COLUMN)
6550 options |= SEARCH_COL;
6551
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006552 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6554 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006555 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 if (lnum_stop < 0)
6557 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558 if (argvars[3].v_type != VAR_UNKNOWN)
6559 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006560#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006561 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 if (time_limit < 0)
6563 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006565 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006566 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567 }
6568
6569#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006570 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 profile_setlimit(time_limit, &tm);
6572#endif
6573
6574 /*
6575 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6576 * Check to make sure only those flags are set.
6577 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6578 * flags cannot be set. Check for that condition also.
6579 */
6580 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6581 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006583 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 goto theend;
6585 }
6586
6587 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006588 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006589 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006590 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6591#ifdef FEAT_RELTIME
6592 sia.sa_tm = &tm;
6593#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006594
6595 // Repeat until {skip} returns FALSE.
6596 for (;;)
6597 {
6598 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006599 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006600 // finding the first match again means there is no match where {skip}
6601 // evaluates to zero.
6602 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6603 subpatnum = FAIL;
6604
Bram Moolenaara9c01042020-06-07 14:50:50 +02006605 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006606 // didn't find it or no skip argument
6607 break;
6608 firstpos = pos;
6609
Bram Moolenaara9c01042020-06-07 14:50:50 +02006610 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006611 {
6612 int do_skip;
6613 int err;
6614 pos_T save_pos = curwin->w_cursor;
6615
6616 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006617 err = FALSE;
6618 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006619 curwin->w_cursor = save_pos;
6620 if (err)
6621 {
6622 // Evaluating {skip} caused an error, break here.
6623 subpatnum = FAIL;
6624 break;
6625 }
6626 if (!do_skip)
6627 break;
6628 }
6629 }
6630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006631 if (subpatnum != FAIL)
6632 {
6633 if (flags & SP_SUBPAT)
6634 retval = subpatnum;
6635 else
6636 retval = pos.lnum;
6637 if (flags & SP_SETPCMARK)
6638 setpcmark();
6639 curwin->w_cursor = pos;
6640 if (match_pos != NULL)
6641 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006642 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 match_pos->lnum = pos.lnum;
6644 match_pos->col = pos.col + 1;
6645 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006646 // "/$" will put the cursor after the end of the line, may need to
6647 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 check_cursor();
6649 }
6650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006651 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 if (flags & SP_NOMOVE)
6653 curwin->w_cursor = save_cursor;
6654 else
6655 curwin->w_set_curswant = TRUE;
6656theend:
6657 p_ws = save_p_ws;
6658
6659 return retval;
6660}
6661
6662#ifdef FEAT_FLOAT
6663
6664/*
6665 * round() is not in C90, use ceil() or floor() instead.
6666 */
6667 float_T
6668vim_round(float_T f)
6669{
6670 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6671}
6672
6673/*
6674 * "round({float})" function
6675 */
6676 static void
6677f_round(typval_T *argvars, typval_T *rettv)
6678{
6679 float_T f = 0.0;
6680
6681 rettv->v_type = VAR_FLOAT;
6682 if (get_float_arg(argvars, &f) == OK)
6683 rettv->vval.v_float = vim_round(f);
6684 else
6685 rettv->vval.v_float = 0.0;
6686}
6687#endif
6688
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006689#ifdef FEAT_RUBY
6690/*
6691 * "rubyeval()" function
6692 */
6693 static void
6694f_rubyeval(typval_T *argvars, typval_T *rettv)
6695{
6696 char_u *str;
6697 char_u buf[NUMBUFLEN];
6698
6699 str = tv_get_string_buf(&argvars[0], buf);
6700 do_rubyeval(str, rettv);
6701}
6702#endif
6703
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704/*
6705 * "screenattr()" function
6706 */
6707 static void
6708f_screenattr(typval_T *argvars, typval_T *rettv)
6709{
6710 int row;
6711 int col;
6712 int c;
6713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006714 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6715 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006716 if (row < 0 || row >= screen_Rows
6717 || col < 0 || col >= screen_Columns)
6718 c = -1;
6719 else
6720 c = ScreenAttrs[LineOffset[row] + col];
6721 rettv->vval.v_number = c;
6722}
6723
6724/*
6725 * "screenchar()" function
6726 */
6727 static void
6728f_screenchar(typval_T *argvars, typval_T *rettv)
6729{
6730 int row;
6731 int col;
6732 int off;
6733 int c;
6734
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006735 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6736 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006737 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 c = -1;
6739 else
6740 {
6741 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 if (enc_utf8 && ScreenLinesUC[off] != 0)
6743 c = ScreenLinesUC[off];
6744 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 c = ScreenLines[off];
6746 }
6747 rettv->vval.v_number = c;
6748}
6749
6750/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006751 * "screenchars()" function
6752 */
6753 static void
6754f_screenchars(typval_T *argvars, typval_T *rettv)
6755{
6756 int row;
6757 int col;
6758 int off;
6759 int c;
6760 int i;
6761
6762 if (rettv_list_alloc(rettv) == FAIL)
6763 return;
6764 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6765 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6766 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6767 return;
6768
6769 off = LineOffset[row] + col;
6770 if (enc_utf8 && ScreenLinesUC[off] != 0)
6771 c = ScreenLinesUC[off];
6772 else
6773 c = ScreenLines[off];
6774 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6775
6776 if (enc_utf8)
6777
6778 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6779 list_append_number(rettv->vval.v_list,
6780 (varnumber_T)ScreenLinesC[i][off]);
6781}
6782
6783/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784 * "screencol()" function
6785 *
6786 * First column is 1 to be consistent with virtcol().
6787 */
6788 static void
6789f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6790{
6791 rettv->vval.v_number = screen_screencol() + 1;
6792}
6793
6794/*
6795 * "screenrow()" function
6796 */
6797 static void
6798f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6799{
6800 rettv->vval.v_number = screen_screenrow() + 1;
6801}
6802
6803/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006804 * "screenstring()" function
6805 */
6806 static void
6807f_screenstring(typval_T *argvars, typval_T *rettv)
6808{
6809 int row;
6810 int col;
6811 int off;
6812 int c;
6813 int i;
6814 char_u buf[MB_MAXBYTES + 1];
6815 int buflen = 0;
6816
6817 rettv->vval.v_string = NULL;
6818 rettv->v_type = VAR_STRING;
6819
6820 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6821 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6822 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6823 return;
6824
6825 off = LineOffset[row] + col;
6826 if (enc_utf8 && ScreenLinesUC[off] != 0)
6827 c = ScreenLinesUC[off];
6828 else
6829 c = ScreenLines[off];
6830 buflen += mb_char2bytes(c, buf);
6831
6832 if (enc_utf8)
6833 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6834 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6835
6836 buf[buflen] = NUL;
6837 rettv->vval.v_string = vim_strsave(buf);
6838}
6839
6840/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841 * "search()" function
6842 */
6843 static void
6844f_search(typval_T *argvars, typval_T *rettv)
6845{
6846 int flags = 0;
6847
6848 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6849}
6850
6851/*
6852 * "searchdecl()" function
6853 */
6854 static void
6855f_searchdecl(typval_T *argvars, typval_T *rettv)
6856{
6857 int locally = 1;
6858 int thisblock = 0;
6859 int error = FALSE;
6860 char_u *name;
6861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006862 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006864 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006865 if (argvars[1].v_type != VAR_UNKNOWN)
6866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006867 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006869 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 }
6871 if (!error && name != NULL)
6872 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6873 locally, thisblock, SEARCH_KEEP) == FAIL;
6874}
6875
6876/*
6877 * Used by searchpair() and searchpairpos()
6878 */
6879 static int
6880searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6881{
6882 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006883 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006884 int save_p_ws = p_ws;
6885 int dir;
6886 int flags = 0;
6887 char_u nbuf1[NUMBUFLEN];
6888 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006889 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 long lnum_stop = 0;
6891 long time_limit = 0;
6892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006893 // Get the three pattern arguments: start, middle, end. Will result in an
6894 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006895 spat = tv_get_string_chk(&argvars[0]);
6896 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6897 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006899 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006901 // Handle the optional fourth argument: flags
6902 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 if (dir == 0)
6904 goto theend;
6905
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006906 // Don't accept SP_END or SP_SUBPAT.
6907 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 if ((flags & (SP_END | SP_SUBPAT)) != 0
6909 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006911 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 goto theend;
6913 }
6914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 if (flags & SP_REPEAT)
6917 p_ws = FALSE;
6918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (argvars[3].v_type == VAR_UNKNOWN
6921 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006922 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923 else
6924 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006925 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006926 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006928 if (argvars[5].v_type != VAR_UNKNOWN)
6929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006930 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006933 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936#ifdef FEAT_RELTIME
6937 if (argvars[6].v_type != VAR_UNKNOWN)
6938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006942 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006944 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945 }
6946#endif
6947 }
6948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949
6950 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6951 match_pos, lnum_stop, time_limit);
6952
6953theend:
6954 p_ws = save_p_ws;
6955
6956 return retval;
6957}
6958
6959/*
6960 * "searchpair()" function
6961 */
6962 static void
6963f_searchpair(typval_T *argvars, typval_T *rettv)
6964{
6965 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6966}
6967
6968/*
6969 * "searchpairpos()" function
6970 */
6971 static void
6972f_searchpairpos(typval_T *argvars, typval_T *rettv)
6973{
6974 pos_T match_pos;
6975 int lnum = 0;
6976 int col = 0;
6977
6978 if (rettv_list_alloc(rettv) == FAIL)
6979 return;
6980
6981 if (searchpair_cmn(argvars, &match_pos) > 0)
6982 {
6983 lnum = match_pos.lnum;
6984 col = match_pos.col;
6985 }
6986
6987 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
6988 list_append_number(rettv->vval.v_list, (varnumber_T)col);
6989}
6990
6991/*
6992 * Search for a start/middle/end thing.
6993 * Used by searchpair(), see its documentation for the details.
6994 * Returns 0 or -1 for no match,
6995 */
6996 long
6997do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006998 char_u *spat, // start pattern
6999 char_u *mpat, // middle pattern
7000 char_u *epat, // end pattern
7001 int dir, // BACKWARD or FORWARD
7002 typval_T *skip, // skip expression
7003 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007005 linenr_T lnum_stop, // stop at this line if not zero
7006 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007{
7008 char_u *save_cpo;
7009 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7010 long retval = 0;
7011 pos_T pos;
7012 pos_T firstpos;
7013 pos_T foundpos;
7014 pos_T save_cursor;
7015 pos_T save_pos;
7016 int n;
7017 int r;
7018 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007019 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 int err;
7021 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007022#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007026 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 save_cpo = p_cpo;
7028 p_cpo = empty_option;
7029
7030#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007031 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032 profile_setlimit(time_limit, &tm);
7033#endif
7034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007035 // Make two search patterns: start/end (pat2, for in nested pairs) and
7036 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007037 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7038 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 if (pat2 == NULL || pat3 == NULL)
7040 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007041 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (*mpat == NUL)
7043 STRCPY(pat3, pat2);
7044 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007045 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 spat, epat, mpat);
7047 if (flags & SP_START)
7048 options |= SEARCH_START;
7049
Bram Moolenaar48570482017-10-30 21:48:41 +01007050 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007051 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 save_cursor = curwin->w_cursor;
7054 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007055 CLEAR_POS(&firstpos);
7056 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 pat = pat3;
7058 for (;;)
7059 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007060 searchit_arg_T sia;
7061
Bram Moolenaara80faa82020-04-12 19:37:17 +02007062 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007063 sia.sa_stop_lnum = lnum_stop;
7064#ifdef FEAT_RELTIME
7065 sia.sa_tm = &tm;
7066#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007067 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007068 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007069 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007070 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 break;
7072
7073 if (firstpos.lnum == 0)
7074 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007075 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007077 // Found the same position again. Can happen with a pattern that
7078 // has "\zs" at the end and searching backwards. Advance one
7079 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007080 if (dir == BACKWARD)
7081 decl(&pos);
7082 else
7083 incl(&pos);
7084 }
7085 foundpos = pos;
7086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007087 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088 options &= ~SEARCH_START;
7089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007090 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007091 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092 {
7093 save_pos = curwin->w_cursor;
7094 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007095 err = FALSE;
7096 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 curwin->w_cursor = save_pos;
7098 if (err)
7099 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007100 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 curwin->w_cursor = save_cursor;
7102 retval = -1;
7103 break;
7104 }
7105 if (r)
7106 continue;
7107 }
7108
7109 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007111 // Found end when searching backwards or start when searching
7112 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007114 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115 }
7116 else
7117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007118 // Found end when searching forward or start when searching
7119 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007120 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007121 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007122 }
7123
7124 if (nest == 0)
7125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007126 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007127 if (flags & SP_RETCOUNT)
7128 ++retval;
7129 else
7130 retval = pos.lnum;
7131 if (flags & SP_SETPCMARK)
7132 setpcmark();
7133 curwin->w_cursor = pos;
7134 if (!(flags & SP_REPEAT))
7135 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007136 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 }
7138 }
7139
7140 if (match_pos != NULL)
7141 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007142 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007143 match_pos->lnum = curwin->w_cursor.lnum;
7144 match_pos->col = curwin->w_cursor.col + 1;
7145 }
7146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007147 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148 if ((flags & SP_NOMOVE) || retval == 0)
7149 curwin->w_cursor = save_cursor;
7150
7151theend:
7152 vim_free(pat2);
7153 vim_free(pat3);
7154 if (p_cpo == empty_option)
7155 p_cpo = save_cpo;
7156 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007157 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158 free_string_option(save_cpo);
7159
7160 return retval;
7161}
7162
7163/*
7164 * "searchpos()" function
7165 */
7166 static void
7167f_searchpos(typval_T *argvars, typval_T *rettv)
7168{
7169 pos_T match_pos;
7170 int lnum = 0;
7171 int col = 0;
7172 int n;
7173 int flags = 0;
7174
7175 if (rettv_list_alloc(rettv) == FAIL)
7176 return;
7177
7178 n = search_cmn(argvars, &match_pos, &flags);
7179 if (n > 0)
7180 {
7181 lnum = match_pos.lnum;
7182 col = match_pos.col;
7183 }
7184
7185 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7186 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7187 if (flags & SP_SUBPAT)
7188 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7189}
7190
7191 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7193{
7194 dict_T *d;
7195 dictitem_T *di;
7196 char_u *csearch;
7197
7198 if (argvars[0].v_type != VAR_DICT)
7199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007200 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 return;
7202 }
7203
7204 if ((d = argvars[0].vval.v_dict) != NULL)
7205 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007206 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 if (csearch != NULL)
7208 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 if (enc_utf8)
7210 {
7211 int pcc[MAX_MCO];
7212 int c = utfc_ptr2char(csearch, pcc);
7213
7214 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7215 }
7216 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007218 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219 }
7220
7221 di = dict_find(d, (char_u *)"forward", -1);
7222 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007223 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 ? FORWARD : BACKWARD);
7225
7226 di = dict_find(d, (char_u *)"until", -1);
7227 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007228 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 }
7230}
7231
7232/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007233 * "setenv()" function
7234 */
7235 static void
7236f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7237{
7238 char_u namebuf[NUMBUFLEN];
7239 char_u valbuf[NUMBUFLEN];
7240 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7241
7242 if (argvars[1].v_type == VAR_SPECIAL
7243 && argvars[1].vval.v_number == VVAL_NULL)
7244 vim_unsetenv(name);
7245 else
7246 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7247}
7248
7249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250 * "setfperm({fname}, {mode})" function
7251 */
7252 static void
7253f_setfperm(typval_T *argvars, typval_T *rettv)
7254{
7255 char_u *fname;
7256 char_u modebuf[NUMBUFLEN];
7257 char_u *mode_str;
7258 int i;
7259 int mask;
7260 int mode = 0;
7261
7262 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007263 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264 if (fname == NULL)
7265 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 if (mode_str == NULL)
7268 return;
7269 if (STRLEN(mode_str) != 9)
7270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007271 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 return;
7273 }
7274
7275 mask = 1;
7276 for (i = 8; i >= 0; --i)
7277 {
7278 if (mode_str[i] != '-')
7279 mode |= mask;
7280 mask = mask << 1;
7281 }
7282 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7283}
7284
7285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 * "setpos()" function
7287 */
7288 static void
7289f_setpos(typval_T *argvars, typval_T *rettv)
7290{
7291 pos_T pos;
7292 int fnum;
7293 char_u *name;
7294 colnr_T curswant = -1;
7295
7296 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007297 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 if (name != NULL)
7299 {
7300 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7301 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007302 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 pos.col = 0;
7304 if (name[0] == '.' && name[1] == NUL)
7305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007306 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007307 curwin->w_cursor = pos;
7308 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007310 curwin->w_curswant = curswant - 1;
7311 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007312 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007313 check_cursor();
7314 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 }
7316 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007318 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319 if (setmark_pos(name[1], &pos, fnum) == OK)
7320 rettv->vval.v_number = 0;
7321 }
7322 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007323 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 }
7325 }
7326}
7327
7328/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007329 * Translate a register type string to the yank type and block length
7330 */
7331 static int
7332get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7333{
7334 char_u *stropt = *pp;
7335 switch (*stropt)
7336 {
7337 case 'v': case 'c': // character-wise selection
7338 *yank_type = MCHAR;
7339 break;
7340 case 'V': case 'l': // line-wise selection
7341 *yank_type = MLINE;
7342 break;
7343 case 'b': case Ctrl_V: // block-wise selection
7344 *yank_type = MBLOCK;
7345 if (VIM_ISDIGIT(stropt[1]))
7346 {
7347 ++stropt;
7348 *block_len = getdigits(&stropt) - 1;
7349 --stropt;
7350 }
7351 break;
7352 default:
7353 return FAIL;
7354 }
7355 *pp = stropt;
7356 return OK;
7357}
7358
7359/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 * "setreg()" function
7361 */
7362 static void
7363f_setreg(typval_T *argvars, typval_T *rettv)
7364{
7365 int regname;
7366 char_u *strregname;
7367 char_u *stropt;
7368 char_u *strval;
7369 int append;
7370 char_u yank_type;
7371 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007372 typval_T *regcontents;
7373 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007374
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007375 pointreg = 0;
7376 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 block_len = -1;
7378 yank_type = MAUTO;
7379 append = FALSE;
7380
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007381 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007382 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383
7384 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007385 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 regname = *strregname;
7387 if (regname == 0 || regname == '@')
7388 regname = '"';
7389
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007390 if (argvars[1].v_type == VAR_DICT)
7391 {
7392 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007393 dictitem_T *di;
7394
7395 if (d == NULL || d->dv_hashtab.ht_used == 0)
7396 {
7397 // Empty dict, clear the register (like setreg(0, []))
7398 char_u *lstval[2] = {NULL, NULL};
7399 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7400 return;
7401 }
7402
7403 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007404 if (di != NULL)
7405 regcontents = &di->di_tv;
7406
7407 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7408 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007409 {
7410 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7411
7412 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007413 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007414 semsg(_(e_invargval), "value");
7415 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007416 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007417 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007418
7419 if (regname == '"')
7420 {
7421 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7422 if (stropt != NULL)
7423 {
7424 pointreg = *stropt;
7425 regname = pointreg;
7426 }
7427 }
7428 else if (dict_get_number(d, (char_u *)"isunnamed"))
7429 pointreg = regname;
7430 }
7431 else
7432 regcontents = &argvars[1];
7433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 if (argvars[2].v_type != VAR_UNKNOWN)
7435 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007436 if (yank_type != MAUTO)
7437 {
7438 semsg(_(e_toomanyarg), "setreg");
7439 return;
7440 }
7441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007442 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007444 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445 for (; *stropt != NUL; ++stropt)
7446 switch (*stropt)
7447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007448 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 append = TRUE;
7450 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007451 default:
7452 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 }
7454 }
7455
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007456 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007457 {
7458 char_u **lstval;
7459 char_u **allocval;
7460 char_u buf[NUMBUFLEN];
7461 char_u **curval;
7462 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007463 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 listitem_T *li;
7465 int len;
7466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007467 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468 len = ll == NULL ? 0 : ll->lv_len;
7469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 // First half: use for pointers to result lines; second half: use for
7471 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007472 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473 if (lstval == NULL)
7474 return;
7475 curval = lstval;
7476 allocval = lstval + len + 2;
7477 curallocval = allocval;
7478
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007479 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007481 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007482 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007484 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 if (strval == NULL)
7486 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007487 if (strval == buf)
7488 {
7489 // Need to make a copy, next tv_get_string_buf_chk() will
7490 // overwrite the string.
7491 strval = vim_strsave(buf);
7492 if (strval == NULL)
7493 goto free_lstval;
7494 *curallocval++ = strval;
7495 }
7496 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498 }
7499 *curval++ = NULL;
7500
7501 write_reg_contents_lst(regname, lstval, -1,
7502 append, yank_type, block_len);
7503free_lstval:
7504 while (curallocval > allocval)
7505 vim_free(*--curallocval);
7506 vim_free(lstval);
7507 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007508 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007509 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007510 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 if (strval == NULL)
7512 return;
7513 write_reg_contents_ex(regname, strval, -1,
7514 append, yank_type, block_len);
7515 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007516 if (pointreg != 0)
7517 get_yank_register(pointreg, TRUE);
7518
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 rettv->vval.v_number = 0;
7520}
7521
7522/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007523 * "settagstack()" function
7524 */
7525 static void
7526f_settagstack(typval_T *argvars, typval_T *rettv)
7527{
7528 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7529 win_T *wp;
7530 dict_T *d;
7531 int action = 'r';
7532
7533 rettv->vval.v_number = -1;
7534
7535 // first argument: window number or id
7536 wp = find_win_by_nr_or_id(&argvars[0]);
7537 if (wp == NULL)
7538 return;
7539
7540 // second argument: dict with items to set in the tag stack
7541 if (argvars[1].v_type != VAR_DICT)
7542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007544 return;
7545 }
7546 d = argvars[1].vval.v_dict;
7547 if (d == NULL)
7548 return;
7549
7550 // third argument: action - 'a' for append and 'r' for replace.
7551 // default is to replace the stack.
7552 if (argvars[2].v_type == VAR_UNKNOWN)
7553 action = 'r';
7554 else if (argvars[2].v_type == VAR_STRING)
7555 {
7556 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007557 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007558 if (actstr == NULL)
7559 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007560 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7561 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007562 action = *actstr;
7563 else
7564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007565 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007566 return;
7567 }
7568 }
7569 else
7570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007571 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007572 return;
7573 }
7574
7575 if (set_tagstack(wp, d, action) == OK)
7576 rettv->vval.v_number = 0;
7577}
7578
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579#ifdef FEAT_CRYPT
7580/*
7581 * "sha256({string})" function
7582 */
7583 static void
7584f_sha256(typval_T *argvars, typval_T *rettv)
7585{
7586 char_u *p;
7587
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007588 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589 rettv->vval.v_string = vim_strsave(
7590 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7591 rettv->v_type = VAR_STRING;
7592}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007593#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594
7595/*
7596 * "shellescape({string})" function
7597 */
7598 static void
7599f_shellescape(typval_T *argvars, typval_T *rettv)
7600{
Bram Moolenaar20615522017-06-05 18:46:26 +02007601 int do_special = non_zero_arg(&argvars[1]);
7602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007604 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 rettv->v_type = VAR_STRING;
7606}
7607
7608/*
7609 * shiftwidth() function
7610 */
7611 static void
7612f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7613{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007614 rettv->vval.v_number = 0;
7615
7616 if (argvars[0].v_type != VAR_UNKNOWN)
7617 {
7618 long col;
7619
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007620 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007621 if (col < 0)
7622 return; // type error; errmsg already given
7623#ifdef FEAT_VARTABS
7624 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7625 return;
7626#endif
7627 }
7628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 rettv->vval.v_number = get_sw_value(curbuf);
7630}
7631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632#ifdef FEAT_FLOAT
7633/*
7634 * "sin()" function
7635 */
7636 static void
7637f_sin(typval_T *argvars, typval_T *rettv)
7638{
7639 float_T f = 0.0;
7640
7641 rettv->v_type = VAR_FLOAT;
7642 if (get_float_arg(argvars, &f) == OK)
7643 rettv->vval.v_float = sin(f);
7644 else
7645 rettv->vval.v_float = 0.0;
7646}
7647
7648/*
7649 * "sinh()" function
7650 */
7651 static void
7652f_sinh(typval_T *argvars, typval_T *rettv)
7653{
7654 float_T f = 0.0;
7655
7656 rettv->v_type = VAR_FLOAT;
7657 if (get_float_arg(argvars, &f) == OK)
7658 rettv->vval.v_float = sinh(f);
7659 else
7660 rettv->vval.v_float = 0.0;
7661}
7662#endif
7663
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664/*
7665 * "soundfold({word})" function
7666 */
7667 static void
7668f_soundfold(typval_T *argvars, typval_T *rettv)
7669{
7670 char_u *s;
7671
7672 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007673 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674#ifdef FEAT_SPELL
7675 rettv->vval.v_string = eval_soundfold(s);
7676#else
7677 rettv->vval.v_string = vim_strsave(s);
7678#endif
7679}
7680
7681/*
7682 * "spellbadword()" function
7683 */
7684 static void
7685f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7686{
7687 char_u *word = (char_u *)"";
7688 hlf_T attr = HLF_COUNT;
7689 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007690#ifdef FEAT_SPELL
7691 int wo_spell_save = curwin->w_p_spell;
7692
7693 if (!curwin->w_p_spell)
7694 {
7695 did_set_spelllang(curwin);
7696 curwin->w_p_spell = TRUE;
7697 }
7698
7699 if (*curwin->w_s->b_p_spl == NUL)
7700 {
7701 emsg(_(e_no_spell));
7702 curwin->w_p_spell = wo_spell_save;
7703 return;
7704 }
7705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706
7707 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007708 {
7709#ifdef FEAT_SPELL
7710 curwin->w_p_spell = wo_spell_save;
7711#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714
7715#ifdef FEAT_SPELL
7716 if (argvars[0].v_type == VAR_UNKNOWN)
7717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007718 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7720 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007721 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007722 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007723 curwin->w_set_curswant = TRUE;
7724 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007726 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007728 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 int capcol = -1;
7730
7731 if (str != NULL)
7732 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007733 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007734 while (*str != NUL)
7735 {
7736 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7737 if (attr != HLF_COUNT)
7738 {
7739 word = str;
7740 break;
7741 }
7742 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007743 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007744 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 }
7746 }
7747 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007748 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007749#endif
7750
7751 list_append_string(rettv->vval.v_list, word, len);
7752 list_append_string(rettv->vval.v_list, (char_u *)(
7753 attr == HLF_SPB ? "bad" :
7754 attr == HLF_SPR ? "rare" :
7755 attr == HLF_SPL ? "local" :
7756 attr == HLF_SPC ? "caps" :
7757 ""), -1);
7758}
7759
7760/*
7761 * "spellsuggest()" function
7762 */
7763 static void
7764f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7765{
7766#ifdef FEAT_SPELL
7767 char_u *str;
7768 int typeerr = FALSE;
7769 int maxcount;
7770 garray_T ga;
7771 int i;
7772 listitem_T *li;
7773 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007774 int wo_spell_save = curwin->w_p_spell;
7775
7776 if (!curwin->w_p_spell)
7777 {
7778 did_set_spelllang(curwin);
7779 curwin->w_p_spell = TRUE;
7780 }
7781
7782 if (*curwin->w_s->b_p_spl == NUL)
7783 {
7784 emsg(_(e_no_spell));
7785 curwin->w_p_spell = wo_spell_save;
7786 return;
7787 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007788#endif
7789
7790 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007791 {
7792#ifdef FEAT_SPELL
7793 curwin->w_p_spell = wo_spell_save;
7794#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007796 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797
7798#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007799 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007801 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 if (argvars[1].v_type != VAR_UNKNOWN)
7803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007804 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007805 if (maxcount <= 0)
7806 return;
7807 if (argvars[2].v_type != VAR_UNKNOWN)
7808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007809 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 if (typeerr)
7811 return;
7812 }
7813 }
7814 else
7815 maxcount = 25;
7816
7817 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7818
7819 for (i = 0; i < ga.ga_len; ++i)
7820 {
7821 str = ((char_u **)ga.ga_data)[i];
7822
7823 li = listitem_alloc();
7824 if (li == NULL)
7825 vim_free(str);
7826 else
7827 {
7828 li->li_tv.v_type = VAR_STRING;
7829 li->li_tv.v_lock = 0;
7830 li->li_tv.vval.v_string = str;
7831 list_append(rettv->vval.v_list, li);
7832 }
7833 }
7834 ga_clear(&ga);
7835 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007836 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837#endif
7838}
7839
7840 static void
7841f_split(typval_T *argvars, typval_T *rettv)
7842{
7843 char_u *str;
7844 char_u *end;
7845 char_u *pat = NULL;
7846 regmatch_T regmatch;
7847 char_u patbuf[NUMBUFLEN];
7848 char_u *save_cpo;
7849 int match;
7850 colnr_T col = 0;
7851 int keepempty = FALSE;
7852 int typeerr = FALSE;
7853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007854 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 save_cpo = p_cpo;
7856 p_cpo = (char_u *)"";
7857
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007858 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 if (argvars[1].v_type != VAR_UNKNOWN)
7860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007861 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 if (pat == NULL)
7863 typeerr = TRUE;
7864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007865 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 }
7867 if (pat == NULL || *pat == NUL)
7868 pat = (char_u *)"[\\x01- ]\\+";
7869
7870 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007871 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007873 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874
7875 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7876 if (regmatch.regprog != NULL)
7877 {
7878 regmatch.rm_ic = FALSE;
7879 while (*str != NUL || keepempty)
7880 {
7881 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007882 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883 else
7884 match = vim_regexec_nl(&regmatch, str, col);
7885 if (match)
7886 end = regmatch.startp[0];
7887 else
7888 end = str + STRLEN(str);
7889 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7890 && *str != NUL && match && end < regmatch.endp[0]))
7891 {
7892 if (list_append_string(rettv->vval.v_list, str,
7893 (int)(end - str)) == FAIL)
7894 break;
7895 }
7896 if (!match)
7897 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007898 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 if (regmatch.endp[0] > str)
7900 col = 0;
7901 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007902 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 str = regmatch.endp[0];
7905 }
7906
7907 vim_regfree(regmatch.regprog);
7908 }
7909
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007910theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 p_cpo = save_cpo;
7912}
7913
7914#ifdef FEAT_FLOAT
7915/*
7916 * "sqrt()" function
7917 */
7918 static void
7919f_sqrt(typval_T *argvars, typval_T *rettv)
7920{
7921 float_T f = 0.0;
7922
7923 rettv->v_type = VAR_FLOAT;
7924 if (get_float_arg(argvars, &f) == OK)
7925 rettv->vval.v_float = sqrt(f);
7926 else
7927 rettv->vval.v_float = 0.0;
7928}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007929#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007931#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007932/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 * "str2float()" function
7934 */
7935 static void
7936f_str2float(typval_T *argvars, typval_T *rettv)
7937{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007938 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007939 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940
Bram Moolenaar08243d22017-01-10 16:12:29 +01007941 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942 p = skipwhite(p + 1);
7943 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007944 if (isneg)
7945 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946 rettv->v_type = VAR_FLOAT;
7947}
7948#endif
7949
7950/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007951 * "str2list()" function
7952 */
7953 static void
7954f_str2list(typval_T *argvars, typval_T *rettv)
7955{
7956 char_u *p;
7957 int utf8 = FALSE;
7958
7959 if (rettv_list_alloc(rettv) == FAIL)
7960 return;
7961
7962 if (argvars[1].v_type != VAR_UNKNOWN)
7963 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7964
7965 p = tv_get_string(&argvars[0]);
7966
7967 if (has_mbyte || utf8)
7968 {
7969 int (*ptr2len)(char_u *);
7970 int (*ptr2char)(char_u *);
7971
7972 if (utf8 || enc_utf8)
7973 {
7974 ptr2len = utf_ptr2len;
7975 ptr2char = utf_ptr2char;
7976 }
7977 else
7978 {
7979 ptr2len = mb_ptr2len;
7980 ptr2char = mb_ptr2char;
7981 }
7982
7983 for ( ; *p != NUL; p += (*ptr2len)(p))
7984 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
7985 }
7986 else
7987 for ( ; *p != NUL; ++p)
7988 list_append_number(rettv->vval.v_list, *p);
7989}
7990
7991/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 * "str2nr()" function
7993 */
7994 static void
7995f_str2nr(typval_T *argvars, typval_T *rettv)
7996{
7997 int base = 10;
7998 char_u *p;
7999 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008000 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008001 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008002
8003 if (argvars[1].v_type != VAR_UNKNOWN)
8004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008005 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 if (base != 2 && base != 8 && base != 10 && base != 16)
8007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008008 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 return;
8010 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008011 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8012 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 }
8014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008015 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008016 isneg = (*p == '-');
8017 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 p = skipwhite(p + 1);
8019 switch (base)
8020 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008021 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008022 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008023 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008025 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8026 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008027 if (isneg)
8028 rettv->vval.v_number = -n;
8029 else
8030 rettv->vval.v_number = n;
8031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032}
8033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034/*
8035 * "strgetchar()" function
8036 */
8037 static void
8038f_strgetchar(typval_T *argvars, typval_T *rettv)
8039{
8040 char_u *str;
8041 int len;
8042 int error = FALSE;
8043 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008044 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045
8046 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008047 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 if (str == NULL)
8049 return;
8050 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008051 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 if (error)
8053 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054
Bram Moolenaar13505972019-01-24 15:04:48 +01008055 while (charidx >= 0 && byteidx < len)
8056 {
8057 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008059 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8060 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008062 --charidx;
8063 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065}
8066
8067/*
8068 * "stridx()" function
8069 */
8070 static void
8071f_stridx(typval_T *argvars, typval_T *rettv)
8072{
8073 char_u buf[NUMBUFLEN];
8074 char_u *needle;
8075 char_u *haystack;
8076 char_u *save_haystack;
8077 char_u *pos;
8078 int start_idx;
8079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008080 needle = tv_get_string_chk(&argvars[1]);
8081 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 rettv->vval.v_number = -1;
8083 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008084 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085
8086 if (argvars[2].v_type != VAR_UNKNOWN)
8087 {
8088 int error = FALSE;
8089
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008090 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 if (error || start_idx >= (int)STRLEN(haystack))
8092 return;
8093 if (start_idx >= 0)
8094 haystack += start_idx;
8095 }
8096
8097 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8098 if (pos != NULL)
8099 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8100}
8101
8102/*
8103 * "string()" function
8104 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008105 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008106f_string(typval_T *argvars, typval_T *rettv)
8107{
8108 char_u *tofree;
8109 char_u numbuf[NUMBUFLEN];
8110
8111 rettv->v_type = VAR_STRING;
8112 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8113 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008114 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (rettv->vval.v_string != NULL && tofree == NULL)
8116 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8117}
8118
8119/*
8120 * "strlen()" function
8121 */
8122 static void
8123f_strlen(typval_T *argvars, typval_T *rettv)
8124{
8125 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127}
8128
8129/*
8130 * "strchars()" function
8131 */
8132 static void
8133f_strchars(typval_T *argvars, typval_T *rettv)
8134{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008135 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008137 varnumber_T len = 0;
8138 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139
8140 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008141 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008143 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008144 else
8145 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8147 while (*s != NUL)
8148 {
8149 func_mb_ptr2char_adv(&s);
8150 ++len;
8151 }
8152 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 }
8154}
8155
8156/*
8157 * "strdisplaywidth()" function
8158 */
8159 static void
8160f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8161{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 int col = 0;
8164
8165 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008166 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167
8168 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8169}
8170
8171/*
8172 * "strwidth()" function
8173 */
8174 static void
8175f_strwidth(typval_T *argvars, typval_T *rettv)
8176{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008177 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178
Bram Moolenaar13505972019-01-24 15:04:48 +01008179 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180}
8181
8182/*
8183 * "strcharpart()" function
8184 */
8185 static void
8186f_strcharpart(typval_T *argvars, typval_T *rettv)
8187{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008188 char_u *p;
8189 int nchar;
8190 int nbyte = 0;
8191 int charlen;
8192 int len = 0;
8193 int slen;
8194 int error = FALSE;
8195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008196 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 slen = (int)STRLEN(p);
8198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008199 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 if (!error)
8201 {
8202 if (nchar > 0)
8203 while (nchar > 0 && nbyte < slen)
8204 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008205 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 --nchar;
8207 }
8208 else
8209 nbyte = nchar;
8210 if (argvars[2].v_type != VAR_UNKNOWN)
8211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008212 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 while (charlen > 0 && nbyte + len < slen)
8214 {
8215 int off = nbyte + len;
8216
8217 if (off < 0)
8218 len += 1;
8219 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008220 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 --charlen;
8222 }
8223 }
8224 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008225 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 }
8227
8228 /*
8229 * Only return the overlap between the specified part and the actual
8230 * string.
8231 */
8232 if (nbyte < 0)
8233 {
8234 len += nbyte;
8235 nbyte = 0;
8236 }
8237 else if (nbyte > slen)
8238 nbyte = slen;
8239 if (len < 0)
8240 len = 0;
8241 else if (nbyte + len > slen)
8242 len = slen - nbyte;
8243
8244 rettv->v_type = VAR_STRING;
8245 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246}
8247
8248/*
8249 * "strpart()" function
8250 */
8251 static void
8252f_strpart(typval_T *argvars, typval_T *rettv)
8253{
8254 char_u *p;
8255 int n;
8256 int len;
8257 int slen;
8258 int error = FALSE;
8259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008260 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261 slen = (int)STRLEN(p);
8262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008263 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 if (error)
8265 len = 0;
8266 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008267 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008269 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270
8271 /*
8272 * Only return the overlap between the specified part and the actual
8273 * string.
8274 */
8275 if (n < 0)
8276 {
8277 len += n;
8278 n = 0;
8279 }
8280 else if (n > slen)
8281 n = slen;
8282 if (len < 0)
8283 len = 0;
8284 else if (n + len > slen)
8285 len = slen - n;
8286
8287 rettv->v_type = VAR_STRING;
8288 rettv->vval.v_string = vim_strnsave(p + n, len);
8289}
8290
8291/*
8292 * "strridx()" function
8293 */
8294 static void
8295f_strridx(typval_T *argvars, typval_T *rettv)
8296{
8297 char_u buf[NUMBUFLEN];
8298 char_u *needle;
8299 char_u *haystack;
8300 char_u *rest;
8301 char_u *lastmatch = NULL;
8302 int haystack_len, end_idx;
8303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 needle = tv_get_string_chk(&argvars[1]);
8305 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306
8307 rettv->vval.v_number = -1;
8308 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008309 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310
8311 haystack_len = (int)STRLEN(haystack);
8312 if (argvars[2].v_type != VAR_UNKNOWN)
8313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008314 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008315 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008317 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 }
8319 else
8320 end_idx = haystack_len;
8321
8322 if (*needle == NUL)
8323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008324 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 lastmatch = haystack + end_idx;
8326 }
8327 else
8328 {
8329 for (rest = haystack; *rest != '\0'; ++rest)
8330 {
8331 rest = (char_u *)strstr((char *)rest, (char *)needle);
8332 if (rest == NULL || rest > haystack + end_idx)
8333 break;
8334 lastmatch = rest;
8335 }
8336 }
8337
8338 if (lastmatch == NULL)
8339 rettv->vval.v_number = -1;
8340 else
8341 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8342}
8343
8344/*
8345 * "strtrans()" function
8346 */
8347 static void
8348f_strtrans(typval_T *argvars, typval_T *rettv)
8349{
8350 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008351 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352}
8353
8354/*
8355 * "submatch()" function
8356 */
8357 static void
8358f_submatch(typval_T *argvars, typval_T *rettv)
8359{
8360 int error = FALSE;
8361 int no;
8362 int retList = 0;
8363
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008364 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008365 if (error)
8366 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008367 if (no < 0 || no >= NSUBEXP)
8368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008369 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008370 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008371 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008373 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 if (error)
8375 return;
8376
8377 if (retList == 0)
8378 {
8379 rettv->v_type = VAR_STRING;
8380 rettv->vval.v_string = reg_submatch(no);
8381 }
8382 else
8383 {
8384 rettv->v_type = VAR_LIST;
8385 rettv->vval.v_list = reg_submatch_list(no);
8386 }
8387}
8388
8389/*
8390 * "substitute()" function
8391 */
8392 static void
8393f_substitute(typval_T *argvars, typval_T *rettv)
8394{
8395 char_u patbuf[NUMBUFLEN];
8396 char_u subbuf[NUMBUFLEN];
8397 char_u flagsbuf[NUMBUFLEN];
8398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 char_u *str = tv_get_string_chk(&argvars[0]);
8400 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008401 char_u *sub = NULL;
8402 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008405 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8406 expr = &argvars[2];
8407 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008411 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8412 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 rettv->vval.v_string = NULL;
8414 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008415 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416}
8417
8418/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008419 * "swapinfo(swap_filename)" function
8420 */
8421 static void
8422f_swapinfo(typval_T *argvars, typval_T *rettv)
8423{
8424 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008425 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008426}
8427
8428/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008429 * "swapname(expr)" function
8430 */
8431 static void
8432f_swapname(typval_T *argvars, typval_T *rettv)
8433{
8434 buf_T *buf;
8435
8436 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008437 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008438 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8439 || buf->b_ml.ml_mfp->mf_fname == NULL)
8440 rettv->vval.v_string = NULL;
8441 else
8442 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8443}
8444
8445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 * "synID(lnum, col, trans)" function
8447 */
8448 static void
8449f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8450{
8451 int id = 0;
8452#ifdef FEAT_SYN_HL
8453 linenr_T lnum;
8454 colnr_T col;
8455 int trans;
8456 int transerr = FALSE;
8457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008458 lnum = tv_get_lnum(argvars); // -1 on type error
8459 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008460 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461
8462 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8463 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8464 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8465#endif
8466
8467 rettv->vval.v_number = id;
8468}
8469
8470/*
8471 * "synIDattr(id, what [, mode])" function
8472 */
8473 static void
8474f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8475{
8476 char_u *p = NULL;
8477#ifdef FEAT_SYN_HL
8478 int id;
8479 char_u *what;
8480 char_u *mode;
8481 char_u modebuf[NUMBUFLEN];
8482 int modec;
8483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008484 id = (int)tv_get_number(&argvars[0]);
8485 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486 if (argvars[2].v_type != VAR_UNKNOWN)
8487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008488 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 modec = TOLOWER_ASC(mode[0]);
8490 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008491 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 }
8493 else
8494 {
8495#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8496 if (USE_24BIT)
8497 modec = 'g';
8498 else
8499#endif
8500 if (t_colors > 1)
8501 modec = 'c';
8502 else
8503 modec = 't';
8504 }
8505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 switch (TOLOWER_ASC(what[0]))
8507 {
8508 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008509 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008511 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 p = highlight_has_attr(id, HL_BOLD, modec);
8513 break;
8514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008515 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 p = highlight_color(id, what, modec);
8517 break;
8518
8519 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008520 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008522 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 p = highlight_has_attr(id, HL_ITALIC, modec);
8524 break;
8525
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008526 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008527 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 break;
8529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008530 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 p = highlight_has_attr(id, HL_INVERSE, modec);
8532 break;
8533
8534 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008535 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008537 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008538 else if (TOLOWER_ASC(what[1]) == 't' &&
8539 TOLOWER_ASC(what[2]) == 'r')
8540 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008541 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 p = highlight_has_attr(id, HL_STANDOUT, modec);
8543 break;
8544
8545 case 'u':
8546 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008547 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8549 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008550 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8552 break;
8553 }
8554
8555 if (p != NULL)
8556 p = vim_strsave(p);
8557#endif
8558 rettv->v_type = VAR_STRING;
8559 rettv->vval.v_string = p;
8560}
8561
8562/*
8563 * "synIDtrans(id)" function
8564 */
8565 static void
8566f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8567{
8568 int id;
8569
8570#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008571 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572
8573 if (id > 0)
8574 id = syn_get_final_id(id);
8575 else
8576#endif
8577 id = 0;
8578
8579 rettv->vval.v_number = id;
8580}
8581
8582/*
8583 * "synconcealed(lnum, col)" function
8584 */
8585 static void
8586f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8587{
8588#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8589 linenr_T lnum;
8590 colnr_T col;
8591 int syntax_flags = 0;
8592 int cchar;
8593 int matchid = 0;
8594 char_u str[NUMBUFLEN];
8595#endif
8596
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008597 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598
8599#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008600 lnum = tv_get_lnum(argvars); // -1 on type error
8601 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602
Bram Moolenaara80faa82020-04-12 19:37:17 +02008603 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604
8605 if (rettv_list_alloc(rettv) != FAIL)
8606 {
8607 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8608 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8609 && curwin->w_p_cole > 0)
8610 {
8611 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8612 syntax_flags = get_syntax_info(&matchid);
8613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008614 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008615 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8616 {
8617 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008618 if (cchar == NUL && curwin->w_p_cole == 1)
8619 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 if (cchar != NUL)
8621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 if (has_mbyte)
8623 (*mb_char2bytes)(cchar, str);
8624 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 str[0] = cchar;
8626 }
8627 }
8628 }
8629
8630 list_append_number(rettv->vval.v_list,
8631 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008632 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008633 list_append_string(rettv->vval.v_list, str, -1);
8634 list_append_number(rettv->vval.v_list, matchid);
8635 }
8636#endif
8637}
8638
8639/*
8640 * "synstack(lnum, col)" function
8641 */
8642 static void
8643f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8644{
8645#ifdef FEAT_SYN_HL
8646 linenr_T lnum;
8647 colnr_T col;
8648 int i;
8649 int id;
8650#endif
8651
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008652 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653
8654#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008655 lnum = tv_get_lnum(argvars); // -1 on type error
8656 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657
8658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8659 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8660 && rettv_list_alloc(rettv) != FAIL)
8661 {
8662 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8663 for (i = 0; ; ++i)
8664 {
8665 id = syn_get_stack_item(i);
8666 if (id < 0)
8667 break;
8668 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8669 break;
8670 }
8671 }
8672#endif
8673}
8674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675/*
8676 * "tabpagebuflist()" function
8677 */
8678 static void
8679f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8680{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 tabpage_T *tp;
8682 win_T *wp = NULL;
8683
8684 if (argvars[0].v_type == VAR_UNKNOWN)
8685 wp = firstwin;
8686 else
8687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008688 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 if (tp != NULL)
8690 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8691 }
8692 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8693 {
8694 for (; wp != NULL; wp = wp->w_next)
8695 if (list_append_number(rettv->vval.v_list,
8696 wp->w_buffer->b_fnum) == FAIL)
8697 break;
8698 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699}
8700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 * "tagfiles()" function
8703 */
8704 static void
8705f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8706{
8707 char_u *fname;
8708 tagname_T tn;
8709 int first;
8710
8711 if (rettv_list_alloc(rettv) == FAIL)
8712 return;
8713 fname = alloc(MAXPATHL);
8714 if (fname == NULL)
8715 return;
8716
8717 for (first = TRUE; ; first = FALSE)
8718 if (get_tagfname(&tn, first, fname) == FAIL
8719 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8720 break;
8721 tagname_free(&tn);
8722 vim_free(fname);
8723}
8724
8725/*
8726 * "taglist()" function
8727 */
8728 static void
8729f_taglist(typval_T *argvars, typval_T *rettv)
8730{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008731 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 char_u *tag_pattern;
8733
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735
8736 rettv->vval.v_number = FALSE;
8737 if (*tag_pattern == NUL)
8738 return;
8739
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008740 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008741 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008743 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744}
8745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746#ifdef FEAT_FLOAT
8747/*
8748 * "tan()" function
8749 */
8750 static void
8751f_tan(typval_T *argvars, typval_T *rettv)
8752{
8753 float_T f = 0.0;
8754
8755 rettv->v_type = VAR_FLOAT;
8756 if (get_float_arg(argvars, &f) == OK)
8757 rettv->vval.v_float = tan(f);
8758 else
8759 rettv->vval.v_float = 0.0;
8760}
8761
8762/*
8763 * "tanh()" function
8764 */
8765 static void
8766f_tanh(typval_T *argvars, typval_T *rettv)
8767{
8768 float_T f = 0.0;
8769
8770 rettv->v_type = VAR_FLOAT;
8771 if (get_float_arg(argvars, &f) == OK)
8772 rettv->vval.v_float = tanh(f);
8773 else
8774 rettv->vval.v_float = 0.0;
8775}
8776#endif
8777
8778/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 * "tolower(string)" function
8780 */
8781 static void
8782f_tolower(typval_T *argvars, typval_T *rettv)
8783{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008785 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786}
8787
8788/*
8789 * "toupper(string)" function
8790 */
8791 static void
8792f_toupper(typval_T *argvars, typval_T *rettv)
8793{
8794 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008795 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796}
8797
8798/*
8799 * "tr(string, fromstr, tostr)" function
8800 */
8801 static void
8802f_tr(typval_T *argvars, typval_T *rettv)
8803{
8804 char_u *in_str;
8805 char_u *fromstr;
8806 char_u *tostr;
8807 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 int inlen;
8809 int fromlen;
8810 int tolen;
8811 int idx;
8812 char_u *cpstr;
8813 int cplen;
8814 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 char_u buf[NUMBUFLEN];
8816 char_u buf2[NUMBUFLEN];
8817 garray_T ga;
8818
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008819 in_str = tv_get_string(&argvars[0]);
8820 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8821 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008823 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 rettv->v_type = VAR_STRING;
8825 rettv->vval.v_string = NULL;
8826 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008827 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 ga_init2(&ga, (int)sizeof(char), 80);
8829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008831 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 if (STRLEN(fromstr) != STRLEN(tostr))
8833 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008835 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 ga_clear(&ga);
8837 return;
8838 }
8839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008840 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 while (*in_str != NUL)
8842 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 if (has_mbyte)
8844 {
8845 inlen = (*mb_ptr2len)(in_str);
8846 cpstr = in_str;
8847 cplen = inlen;
8848 idx = 0;
8849 for (p = fromstr; *p != NUL; p += fromlen)
8850 {
8851 fromlen = (*mb_ptr2len)(p);
8852 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8853 {
8854 for (p = tostr; *p != NUL; p += tolen)
8855 {
8856 tolen = (*mb_ptr2len)(p);
8857 if (idx-- == 0)
8858 {
8859 cplen = tolen;
8860 cpstr = p;
8861 break;
8862 }
8863 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008864 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865 goto error;
8866 break;
8867 }
8868 ++idx;
8869 }
8870
8871 if (first && cpstr == in_str)
8872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008873 // Check that fromstr and tostr have the same number of
8874 // (multi-byte) characters. Done only once when a character
8875 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 first = FALSE;
8877 for (p = tostr; *p != NUL; p += tolen)
8878 {
8879 tolen = (*mb_ptr2len)(p);
8880 --idx;
8881 }
8882 if (idx != 0)
8883 goto error;
8884 }
8885
8886 (void)ga_grow(&ga, cplen);
8887 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8888 ga.ga_len += cplen;
8889
8890 in_str += inlen;
8891 }
8892 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008894 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 p = vim_strchr(fromstr, *in_str);
8896 if (p != NULL)
8897 ga_append(&ga, tostr[p - fromstr]);
8898 else
8899 ga_append(&ga, *in_str);
8900 ++in_str;
8901 }
8902 }
8903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008904 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 (void)ga_grow(&ga, 1);
8906 ga_append(&ga, NUL);
8907
8908 rettv->vval.v_string = ga.ga_data;
8909}
8910
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008911/*
8912 * "trim({expr})" function
8913 */
8914 static void
8915f_trim(typval_T *argvars, typval_T *rettv)
8916{
8917 char_u buf1[NUMBUFLEN];
8918 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008919 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008920 char_u *mask = NULL;
8921 char_u *tail;
8922 char_u *prev;
8923 char_u *p;
8924 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008925 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008926
8927 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008928 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008929 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008930 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008931
8932 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008933 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008934 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008935
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008937 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008938 int error = 0;
8939
8940 // leading or trailing characters to trim
8941 dir = (int)tv_get_number_chk(&argvars[2], &error);
8942 if (error)
8943 return;
8944 if (dir < 0 || dir > 2)
8945 {
8946 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8947 return;
8948 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008949 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008950 }
8951
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008952 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008953 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008954 // Trim leading characters
8955 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008956 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008957 c1 = PTR2CHAR(head);
8958 if (mask == NULL)
8959 {
8960 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008961 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008962 }
8963 else
8964 {
8965 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8966 if (c1 == PTR2CHAR(p))
8967 break;
8968 if (*p == NUL)
8969 break;
8970 }
8971 MB_PTR_ADV(head);
8972 }
8973 }
8974
8975 tail = head + STRLEN(head);
8976 if (dir == 0 || dir == 2)
8977 {
8978 // Trim trailing characters
8979 for (; tail > head; tail = prev)
8980 {
8981 prev = tail;
8982 MB_PTR_BACK(head, prev);
8983 c1 = PTR2CHAR(prev);
8984 if (mask == NULL)
8985 {
8986 if (c1 > ' ' && c1 != 0xa0)
8987 break;
8988 }
8989 else
8990 {
8991 for (p = mask; *p != NUL; MB_PTR_ADV(p))
8992 if (c1 == PTR2CHAR(p))
8993 break;
8994 if (*p == NUL)
8995 break;
8996 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008997 }
8998 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02008999 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009000}
9001
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002#ifdef FEAT_FLOAT
9003/*
9004 * "trunc({float})" function
9005 */
9006 static void
9007f_trunc(typval_T *argvars, typval_T *rettv)
9008{
9009 float_T f = 0.0;
9010
9011 rettv->v_type = VAR_FLOAT;
9012 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009013 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9015 else
9016 rettv->vval.v_float = 0.0;
9017}
9018#endif
9019
9020/*
9021 * "type(expr)" function
9022 */
9023 static void
9024f_type(typval_T *argvars, typval_T *rettv)
9025{
9026 int n = -1;
9027
9028 switch (argvars[0].v_type)
9029 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009030 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9031 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009033 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9034 case VAR_LIST: n = VAR_TYPE_LIST; break;
9035 case VAR_DICT: n = VAR_TYPE_DICT; break;
9036 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9037 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9038 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009039 case VAR_JOB: n = VAR_TYPE_JOB; break;
9040 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009041 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009043 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009044 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009045 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046 n = -1;
9047 break;
9048 }
9049 rettv->vval.v_number = n;
9050}
9051
9052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 * "virtcol(string)" function
9054 */
9055 static void
9056f_virtcol(typval_T *argvars, typval_T *rettv)
9057{
9058 colnr_T vcol = 0;
9059 pos_T *fp;
9060 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009061 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062
9063 fp = var2fpos(&argvars[0], FALSE, &fnum);
9064 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9065 && fnum == curbuf->b_fnum)
9066 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009067 // Limit the column to a valid value, getvvcol() doesn't check.
9068 if (fp->col < 0)
9069 fp->col = 0;
9070 else
9071 {
9072 len = (int)STRLEN(ml_get(fp->lnum));
9073 if (fp->col > len)
9074 fp->col = len;
9075 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 getvvcol(curwin, fp, NULL, NULL, &vcol);
9077 ++vcol;
9078 }
9079
9080 rettv->vval.v_number = vcol;
9081}
9082
9083/*
9084 * "visualmode()" function
9085 */
9086 static void
9087f_visualmode(typval_T *argvars, typval_T *rettv)
9088{
9089 char_u str[2];
9090
9091 rettv->v_type = VAR_STRING;
9092 str[0] = curbuf->b_visual_mode_eval;
9093 str[1] = NUL;
9094 rettv->vval.v_string = vim_strsave(str);
9095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009096 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 if (non_zero_arg(&argvars[0]))
9098 curbuf->b_visual_mode_eval = NUL;
9099}
9100
9101/*
9102 * "wildmenumode()" function
9103 */
9104 static void
9105f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9106{
9107#ifdef FEAT_WILDMENU
9108 if (wild_menu_showing)
9109 rettv->vval.v_number = 1;
9110#endif
9111}
9112
9113/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009114 * "windowsversion()" function
9115 */
9116 static void
9117f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9118{
9119 rettv->v_type = VAR_STRING;
9120 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9121}
9122
9123/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124 * "wordcount()" function
9125 */
9126 static void
9127f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9128{
9129 if (rettv_dict_alloc(rettv) == FAIL)
9130 return;
9131 cursor_pos_info(rettv->vval.v_dict);
9132}
9133
9134/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 * "xor(expr, expr)" function
9136 */
9137 static void
9138f_xor(typval_T *argvars, typval_T *rettv)
9139{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009140 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9141 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009142}
9143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009144#endif // FEAT_EVAL