blob: fff7a89a9aa856eca7c01e023df6406348b18077 [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 Moolenaar0b39c3f2020-08-30 15:52:10 +0200100static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
102static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_hlID(typval_T *argvars, typval_T *rettv);
104static void f_hlexists(typval_T *argvars, typval_T *rettv);
105static void f_hostname(typval_T *argvars, typval_T *rettv);
106static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200107static void f_index(typval_T *argvars, typval_T *rettv);
108static void f_input(typval_T *argvars, typval_T *rettv);
109static void f_inputdialog(typval_T *argvars, typval_T *rettv);
110static void f_inputlist(typval_T *argvars, typval_T *rettv);
111static void f_inputrestore(typval_T *argvars, typval_T *rettv);
112static void f_inputsave(typval_T *argvars, typval_T *rettv);
113static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +0100114static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_islocked(typval_T *argvars, typval_T *rettv);
117#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200118static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200119static void f_isnan(typval_T *argvars, typval_T *rettv);
120#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
122static void f_len(typval_T *argvars, typval_T *rettv);
123static void f_libcall(typval_T *argvars, typval_T *rettv);
124static void f_libcallnr(typval_T *argvars, typval_T *rettv);
125static void f_line(typval_T *argvars, typval_T *rettv);
126static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127#ifdef FEAT_FLOAT
128static void f_log(typval_T *argvars, typval_T *rettv);
129static void f_log10(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_LUA
132static void f_luaeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_maparg(typval_T *argvars, typval_T *rettv);
135static void f_mapcheck(typval_T *argvars, typval_T *rettv);
136static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200137static void f_matchend(typval_T *argvars, typval_T *rettv);
138static void f_matchlist(typval_T *argvars, typval_T *rettv);
139static void f_matchstr(typval_T *argvars, typval_T *rettv);
140static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
141static void f_max(typval_T *argvars, typval_T *rettv);
142static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200143#ifdef FEAT_MZSCHEME
144static void f_mzeval(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
147static void f_nr2char(typval_T *argvars, typval_T *rettv);
148static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200149#ifdef FEAT_PERL
150static void f_perleval(typval_T *argvars, typval_T *rettv);
151#endif
152#ifdef FEAT_FLOAT
153static void f_pow(typval_T *argvars, typval_T *rettv);
154#endif
155static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
156static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200157static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_pumvisible(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_PYTHON3
160static void f_py3eval(typval_T *argvars, typval_T *rettv);
161#endif
162#ifdef FEAT_PYTHON
163static void f_pyeval(typval_T *argvars, typval_T *rettv);
164#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100165#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
166static void f_pyxeval(typval_T *argvars, typval_T *rettv);
167#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100168static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100169static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200171static void f_reg_executing(typval_T *argvars, typval_T *rettv);
172static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_rename(typval_T *argvars, typval_T *rettv);
174static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175#ifdef FEAT_FLOAT
176static void f_round(typval_T *argvars, typval_T *rettv);
177#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100178#ifdef FEAT_RUBY
179static void f_rubyeval(typval_T *argvars, typval_T *rettv);
180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_screenattr(typval_T *argvars, typval_T *rettv);
182static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100183static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_screencol(typval_T *argvars, typval_T *rettv);
185static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100186static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_search(typval_T *argvars, typval_T *rettv);
188static void f_searchdecl(typval_T *argvars, typval_T *rettv);
189static void f_searchpair(typval_T *argvars, typval_T *rettv);
190static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
191static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200193static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100197static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198#ifdef FEAT_CRYPT
199static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200200#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_shellescape(typval_T *argvars, typval_T *rettv);
202static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200203#ifdef FEAT_FLOAT
204static void f_sin(typval_T *argvars, typval_T *rettv);
205static void f_sinh(typval_T *argvars, typval_T *rettv);
206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_soundfold(typval_T *argvars, typval_T *rettv);
208static void f_spellbadword(typval_T *argvars, typval_T *rettv);
209static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
210static void f_split(typval_T *argvars, typval_T *rettv);
211#ifdef FEAT_FLOAT
212static void f_sqrt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100213#endif
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100214static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0387cae2019-11-29 21:07:58 +0100215#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200216static void f_str2float(typval_T *argvars, typval_T *rettv);
217#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200218static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200219static void f_str2nr(typval_T *argvars, typval_T *rettv);
220static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200221static void f_strgetchar(typval_T *argvars, typval_T *rettv);
222static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200223static void f_strlen(typval_T *argvars, typval_T *rettv);
224static void f_strcharpart(typval_T *argvars, typval_T *rettv);
225static void f_strpart(typval_T *argvars, typval_T *rettv);
226static void f_strridx(typval_T *argvars, typval_T *rettv);
227static void f_strtrans(typval_T *argvars, typval_T *rettv);
228static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
229static void f_strwidth(typval_T *argvars, typval_T *rettv);
230static void f_submatch(typval_T *argvars, typval_T *rettv);
231static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200232static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200233static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200234static void f_synID(typval_T *argvars, typval_T *rettv);
235static void f_synIDattr(typval_T *argvars, typval_T *rettv);
236static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
237static void f_synstack(typval_T *argvars, typval_T *rettv);
238static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200239static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200240static void f_taglist(typval_T *argvars, typval_T *rettv);
241static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200242#ifdef FEAT_FLOAT
243static void f_tan(typval_T *argvars, typval_T *rettv);
244static void f_tanh(typval_T *argvars, typval_T *rettv);
245#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_tolower(typval_T *argvars, typval_T *rettv);
247static void f_toupper(typval_T *argvars, typval_T *rettv);
248static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100249static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200250#ifdef FEAT_FLOAT
251static void f_trunc(typval_T *argvars, typval_T *rettv);
252#endif
253static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200254static void f_virtcol(typval_T *argvars, typval_T *rettv);
255static void f_visualmode(typval_T *argvars, typval_T *rettv);
256static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100257static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200258static void f_wordcount(typval_T *argvars, typval_T *rettv);
259static void f_xor(typval_T *argvars, typval_T *rettv);
260
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100261
262 static type_T *
263ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
264{
265 return &t_void;
266}
267 static type_T *
268ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
269{
270 return &t_any;
271}
272 static type_T *
273ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
274{
275 return &t_number;
276}
277 static type_T *
278ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
279{
280 return &t_float;
281}
282 static type_T *
283ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
284{
285 return &t_string;
286}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200287 static type_T *
288ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100289{
290 return &t_list_any;
291}
292 static type_T *
293ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
294{
295 return &t_list_number;
296}
297 static type_T *
298ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
299{
300 return &t_list_string;
301}
302 static type_T *
303ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
304{
305 return &t_list_dict_any;
306}
307 static type_T *
308ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
309{
310 return &t_dict_any;
311}
312 static type_T *
313ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
314{
315 return &t_dict_number;
316}
317 static type_T *
318ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
319{
320 return &t_dict_string;
321}
322 static type_T *
323ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
324{
325 return &t_blob;
326}
327 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200328ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100329{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200330 return &t_func_any;
331}
332 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100333ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
334{
335 return &t_channel;
336}
337 static type_T *
338ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
339{
340 return &t_job;
341}
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100342
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200343 static type_T *
344ret_first_arg(int argcount, type_T **argtypes)
345{
346 if (argcount > 0)
347 return argtypes[0];
348 return &t_void;
349}
350
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200351/*
352 * Used for getqflist(): returns list if there is no argument, dict if there is
353 * one.
354 */
355 static type_T *
356ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
357{
358 if (argcount > 0)
359 return &t_dict_any;
360 return &t_list_dict_any;
361}
362
363/*
364 * Used for getloclist(): returns list if there is one argument, dict if there
365 * are two.
366 */
367 static type_T *
368ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
369{
370 if (argcount > 1)
371 return &t_dict_any;
372 return &t_list_dict_any;
373}
374
Bram Moolenaar846178a2020-07-05 17:04:13 +0200375 static type_T *
376ret_argv(int argcount, type_T **argtypes UNUSED)
377{
378 // argv() returns list of strings
379 if (argcount == 0)
380 return &t_list_string;
381
382 // argv(0) returns a string, but argv(-1] returns a list
383 return &t_any;
384}
385
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200386 static type_T *
387ret_remove(int argcount UNUSED, type_T **argtypes)
388{
389 if (argtypes[0]->tt_type == VAR_LIST
390 || argtypes[0]->tt_type == VAR_DICT)
391 return argtypes[0]->tt_member;
392 if (argtypes[0]->tt_type == VAR_BLOB)
393 return &t_number;
394 return &t_any;
395}
396
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200397 static type_T *
398ret_getreg(int argcount, type_T **argtypes UNUSED)
399{
400 // Assume that if the third argument is passed it's non-zero
401 if (argcount == 3)
402 return &t_list_string;
403 return &t_string;
404}
405
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200406 static type_T *
407ret_maparg(int argcount, type_T **argtypes UNUSED)
408{
409 // Assume that if the fourth argument is passed it's non-zero
410 if (argcount == 4)
411 return &t_dict_any;
412 return &t_string;
413}
414
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100415static type_T *ret_f_function(int argcount, type_T **argtypes);
416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200417/*
418 * Array with names and number of arguments of all internal functions
419 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
420 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200421typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200423 char *f_name; // function name
424 char f_min_argc; // minimal number of arguments
425 char f_max_argc; // maximal number of arguments
426 char f_argtype; // for method: FEARG_ values
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100427 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
428 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200430 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200431} funcentry_T;
432
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200433// values for f_argtype; zero means it cannot be used as a method
434#define FEARG_1 1 // base is the first argument
435#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200436#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200437#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200438#define FEARG_LAST 9 // base is the last argument
439
Bram Moolenaar15c47602020-03-26 22:16:48 +0100440#ifdef FEAT_FLOAT
441# define FLOAT_FUNC(name) name
442#else
443# define FLOAT_FUNC(name) NULL
444#endif
445#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
446# define MATH_FUNC(name) name
447#else
448# define MATH_FUNC(name) NULL
449#endif
450#ifdef FEAT_TIMERS
451# define TIMER_FUNC(name) name
452#else
453# define TIMER_FUNC(name) NULL
454#endif
455#ifdef FEAT_JOB_CHANNEL
456# define JOB_FUNC(name) name
457#else
458# define JOB_FUNC(name) NULL
459#endif
460#ifdef FEAT_PROP_POPUP
461# define PROP_FUNC(name) name
462#else
463# define PROP_FUNC(name) NULL
464#endif
465#ifdef FEAT_SIGNS
466# define SIGN_FUNC(name) name
467#else
468# define SIGN_FUNC(name) NULL
469#endif
470#ifdef FEAT_SOUND
471# define SOUND_FUNC(name) name
472#else
473# define SOUND_FUNC(name) NULL
474#endif
475#ifdef FEAT_TERMINAL
476# define TERM_FUNC(name) name
477#else
478# define TERM_FUNC(name) NULL
479#endif
480
Bram Moolenaarac92e252019-08-03 21:58:38 +0200481static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200482{
Bram Moolenaar15c47602020-03-26 22:16:48 +0100483 {"abs", 1, 1, FEARG_1, ret_any, FLOAT_FUNC(f_abs)},
484 {"acos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfce82b32020-07-05 16:07:21 +0200485 {"add", 2, 2, FEARG_1, ret_first_arg, f_add},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100486 {"and", 2, 2, FEARG_1, ret_number, f_and},
Bram Moolenaar389df252020-07-09 21:20:47 +0200487 {"append", 2, 2, FEARG_2, ret_number, f_append},
Bram Moolenaar92053ce2020-07-09 22:53:30 +0200488 {"appendbufline", 3, 3, FEARG_3, ret_number, f_appendbufline},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100489 {"argc", 0, 1, 0, ret_number, f_argc},
490 {"argidx", 0, 0, 0, ret_number, f_argidx},
491 {"arglistid", 0, 2, 0, ret_number, f_arglistid},
Bram Moolenaar846178a2020-07-05 17:04:13 +0200492 {"argv", 0, 2, 0, ret_argv, f_argv},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100493 {"asin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_asin)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100494 {"assert_beeps", 1, 2, FEARG_1, ret_number, f_assert_beeps},
495 {"assert_equal", 2, 3, FEARG_2, ret_number, f_assert_equal},
Bram Moolenaarfb517ba2020-06-03 19:55:35 +0200496 {"assert_equalfile", 2, 3, FEARG_1, ret_number, f_assert_equalfile},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100497 {"assert_exception", 1, 2, 0, ret_number, f_assert_exception},
Bram Moolenaar1d634542020-08-18 13:41:50 +0200498 {"assert_fails", 1, 4, FEARG_1, ret_number, f_assert_fails},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100499 {"assert_false", 1, 2, FEARG_1, ret_number, f_assert_false},
500 {"assert_inrange", 3, 4, FEARG_3, ret_number, f_assert_inrange},
501 {"assert_match", 2, 3, FEARG_2, ret_number, f_assert_match},
502 {"assert_notequal", 2, 3, FEARG_2, ret_number, f_assert_notequal},
503 {"assert_notmatch", 2, 3, FEARG_2, ret_number, f_assert_notmatch},
504 {"assert_report", 1, 1, FEARG_1, ret_number, f_assert_report},
505 {"assert_true", 1, 2, FEARG_1, ret_number, f_assert_true},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100506 {"atan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_atan)},
507 {"atan2", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_atan2)},
508 {"balloon_gettext", 0, 0, 0, ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100509#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100510 f_balloon_gettext
511#else
512 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100513#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100514 },
515 {"balloon_show", 1, 1, FEARG_1, ret_void,
516#ifdef FEAT_BEVAL
517 f_balloon_show
518#else
519 NULL
520#endif
521 },
522 {"balloon_split", 1, 1, FEARG_1, ret_list_string,
523#if defined(FEAT_BEVAL_TERM)
524 f_balloon_split
525#else
526 NULL
527#endif
528 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100529 {"browse", 4, 4, 0, ret_string, f_browse},
530 {"browsedir", 2, 2, 0, ret_string, f_browsedir},
531 {"bufadd", 1, 1, FEARG_1, ret_number, f_bufadd},
532 {"bufexists", 1, 1, FEARG_1, ret_number, f_bufexists},
533 {"buffer_exists", 1, 1, FEARG_1, ret_number, f_bufexists}, // obsolete
534 {"buffer_name", 0, 1, FEARG_1, ret_string, f_bufname}, // obsolete
535 {"buffer_number", 0, 1, FEARG_1, ret_number, f_bufnr}, // obsolete
536 {"buflisted", 1, 1, FEARG_1, ret_number, f_buflisted},
537 {"bufload", 1, 1, FEARG_1, ret_void, f_bufload},
538 {"bufloaded", 1, 1, FEARG_1, ret_number, f_bufloaded},
539 {"bufname", 0, 1, FEARG_1, ret_string, f_bufname},
540 {"bufnr", 0, 2, FEARG_1, ret_number, f_bufnr},
541 {"bufwinid", 1, 1, FEARG_1, ret_number, f_bufwinid},
542 {"bufwinnr", 1, 1, FEARG_1, ret_number, f_bufwinnr},
543 {"byte2line", 1, 1, FEARG_1, ret_number, f_byte2line},
544 {"byteidx", 2, 2, FEARG_1, ret_number, f_byteidx},
545 {"byteidxcomp", 2, 2, FEARG_1, ret_number, f_byteidxcomp},
546 {"call", 2, 3, FEARG_1, ret_any, f_call},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100547 {"ceil", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_ceil)},
548 {"ch_canread", 1, 1, FEARG_1, ret_number, JOB_FUNC(f_ch_canread)},
549 {"ch_close", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close)},
550 {"ch_close_in", 1, 1, FEARG_1, ret_void, JOB_FUNC(f_ch_close_in)},
551 {"ch_evalexpr", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalexpr)},
552 {"ch_evalraw", 2, 3, FEARG_1, ret_any, JOB_FUNC(f_ch_evalraw)},
553 {"ch_getbufnr", 2, 2, FEARG_1, ret_number, JOB_FUNC(f_ch_getbufnr)},
554 {"ch_getjob", 1, 1, FEARG_1, ret_job, JOB_FUNC(f_ch_getjob)},
555 {"ch_info", 1, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_ch_info)},
556 {"ch_log", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_log)},
557 {"ch_logfile", 1, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_logfile)},
558 {"ch_open", 1, 2, FEARG_1, ret_channel, JOB_FUNC(f_ch_open)},
559 {"ch_read", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_read)},
560 {"ch_readblob", 1, 2, FEARG_1, ret_blob, JOB_FUNC(f_ch_readblob)},
561 {"ch_readraw", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_readraw)},
562 {"ch_sendexpr", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendexpr)},
563 {"ch_sendraw", 2, 3, FEARG_1, ret_void, JOB_FUNC(f_ch_sendraw)},
564 {"ch_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_ch_setoptions)},
565 {"ch_status", 1, 2, FEARG_1, ret_string, JOB_FUNC(f_ch_status)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100566 {"changenr", 0, 0, 0, ret_number, f_changenr},
567 {"char2nr", 1, 2, FEARG_1, ret_number, f_char2nr},
Bram Moolenaar4e4473c2020-08-28 22:24:57 +0200568 {"charclass", 1, 1, FEARG_1, ret_number, f_charclass},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100569 {"chdir", 1, 1, FEARG_1, ret_string, f_chdir},
570 {"cindent", 1, 1, FEARG_1, ret_number, f_cindent},
571 {"clearmatches", 0, 1, FEARG_1, ret_void, f_clearmatches},
572 {"col", 1, 1, FEARG_1, ret_number, f_col},
573 {"complete", 2, 2, FEARG_2, ret_void, f_complete},
574 {"complete_add", 1, 1, FEARG_1, ret_number, f_complete_add},
575 {"complete_check", 0, 0, 0, ret_number, f_complete_check},
576 {"complete_info", 0, 1, FEARG_1, ret_dict_any, f_complete_info},
577 {"confirm", 1, 4, FEARG_1, ret_number, f_confirm},
Bram Moolenaara66ba012020-07-05 18:41:08 +0200578 {"copy", 1, 1, FEARG_1, ret_first_arg, f_copy},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100579 {"cos", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cos)},
580 {"cosh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_cosh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100581 {"count", 2, 4, FEARG_1, ret_number, f_count},
582 {"cscope_connection",0,3, 0, ret_number, f_cscope_connection},
583 {"cursor", 1, 3, FEARG_1, ret_number, f_cursor},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100584 {"debugbreak", 1, 1, FEARG_1, ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100585#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100586 f_debugbreak
587#else
588 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200589#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100590 },
Bram Moolenaara66ba012020-07-05 18:41:08 +0200591 {"deepcopy", 1, 2, FEARG_1, ret_first_arg, f_deepcopy},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100592 {"delete", 1, 2, FEARG_1, ret_number, f_delete},
593 {"deletebufline", 2, 3, FEARG_1, ret_number, f_deletebufline},
594 {"did_filetype", 0, 0, 0, ret_number, f_did_filetype},
595 {"diff_filler", 1, 1, FEARG_1, ret_number, f_diff_filler},
596 {"diff_hlID", 2, 2, FEARG_1, ret_number, f_diff_hlID},
597 {"echoraw", 1, 1, FEARG_1, ret_number, f_echoraw},
598 {"empty", 1, 1, FEARG_1, ret_number, f_empty},
599 {"environ", 0, 0, 0, ret_dict_string, f_environ},
600 {"escape", 2, 2, FEARG_1, ret_string, f_escape},
601 {"eval", 1, 1, FEARG_1, ret_any, f_eval},
602 {"eventhandler", 0, 0, 0, ret_number, f_eventhandler},
603 {"executable", 1, 1, FEARG_1, ret_number, f_executable},
604 {"execute", 1, 2, FEARG_1, ret_string, f_execute},
605 {"exepath", 1, 1, FEARG_1, ret_string, f_exepath},
606 {"exists", 1, 1, FEARG_1, ret_number, f_exists},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100607 {"exp", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_exp)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100608 {"expand", 1, 3, FEARG_1, ret_any, f_expand},
609 {"expandcmd", 1, 1, FEARG_1, ret_string, f_expandcmd},
Bram Moolenaarb3c019c2020-07-05 20:08:39 +0200610 {"extend", 2, 3, FEARG_1, ret_first_arg, f_extend},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100611 {"feedkeys", 1, 2, FEARG_1, ret_void, f_feedkeys},
612 {"file_readable", 1, 1, FEARG_1, ret_number, f_filereadable}, // obsolete
613 {"filereadable", 1, 1, FEARG_1, ret_number, f_filereadable},
614 {"filewritable", 1, 1, FEARG_1, ret_number, f_filewritable},
Bram Moolenaar0d94ad62020-07-05 20:16:41 +0200615 {"filter", 2, 2, FEARG_1, ret_first_arg, f_filter},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100616 {"finddir", 1, 3, FEARG_1, ret_string, f_finddir},
617 {"findfile", 1, 3, FEARG_1, ret_string, f_findfile},
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200618 {"flatten", 1, 2, FEARG_1, ret_list_any, f_flatten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100619 {"float2nr", 1, 1, FEARG_1, ret_number, FLOAT_FUNC(f_float2nr)},
620 {"floor", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_floor)},
621 {"fmod", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_fmod)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100622 {"fnameescape", 1, 1, FEARG_1, ret_string, f_fnameescape},
623 {"fnamemodify", 2, 2, FEARG_1, ret_string, f_fnamemodify},
624 {"foldclosed", 1, 1, FEARG_1, ret_number, f_foldclosed},
625 {"foldclosedend", 1, 1, FEARG_1, ret_number, f_foldclosedend},
626 {"foldlevel", 1, 1, FEARG_1, ret_number, f_foldlevel},
627 {"foldtext", 0, 0, 0, ret_string, f_foldtext},
628 {"foldtextresult", 1, 1, FEARG_1, ret_string, f_foldtextresult},
629 {"foreground", 0, 0, 0, ret_void, f_foreground},
Bram Moolenaard77a8522020-04-03 21:59:57 +0200630 {"funcref", 1, 3, FEARG_1, ret_func_any, f_funcref},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100631 {"function", 1, 3, FEARG_1, ret_f_function, f_function},
632 {"garbagecollect", 0, 1, 0, ret_void, f_garbagecollect},
633 {"get", 2, 3, FEARG_1, ret_any, f_get},
Bram Moolenaar6434fc52020-07-18 22:24:22 +0200634 {"getbufinfo", 0, 1, FEARG_1, ret_list_dict_any, f_getbufinfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100635 {"getbufline", 2, 3, FEARG_1, ret_list_string, f_getbufline},
636 {"getbufvar", 2, 3, FEARG_1, ret_any, f_getbufvar},
637 {"getchangelist", 0, 1, FEARG_1, ret_list_any, f_getchangelist},
638 {"getchar", 0, 1, 0, ret_number, f_getchar},
639 {"getcharmod", 0, 0, 0, ret_number, f_getcharmod},
640 {"getcharsearch", 0, 0, 0, ret_dict_any, f_getcharsearch},
641 {"getcmdline", 0, 0, 0, ret_string, f_getcmdline},
642 {"getcmdpos", 0, 0, 0, ret_number, f_getcmdpos},
643 {"getcmdtype", 0, 0, 0, ret_string, f_getcmdtype},
644 {"getcmdwintype", 0, 0, 0, ret_string, f_getcmdwintype},
645 {"getcompletion", 2, 3, FEARG_1, ret_list_string, f_getcompletion},
646 {"getcurpos", 0, 0, 0, ret_list_number, f_getcurpos},
647 {"getcwd", 0, 2, FEARG_1, ret_string, f_getcwd},
648 {"getenv", 1, 1, FEARG_1, ret_string, f_getenv},
649 {"getfontname", 0, 1, 0, ret_string, f_getfontname},
650 {"getfperm", 1, 1, FEARG_1, ret_string, f_getfperm},
651 {"getfsize", 1, 1, FEARG_1, ret_number, f_getfsize},
652 {"getftime", 1, 1, FEARG_1, ret_number, f_getftime},
653 {"getftype", 1, 1, FEARG_1, ret_string, f_getftype},
654 {"getimstatus", 0, 0, 0, ret_number, f_getimstatus},
655 {"getjumplist", 0, 2, FEARG_1, ret_list_any, f_getjumplist},
656 {"getline", 1, 2, FEARG_1, ret_f_getline, f_getline},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200657 {"getloclist", 1, 2, 0, ret_list_or_dict_1, f_getloclist},
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200658 {"getmarklist", 0, 1, FEARG_1, ret_list_dict_any, f_getmarklist},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100659 {"getmatches", 0, 1, 0, ret_list_dict_any, f_getmatches},
660 {"getmousepos", 0, 0, 0, ret_dict_number, f_getmousepos},
661 {"getpid", 0, 0, 0, ret_number, f_getpid},
662 {"getpos", 1, 1, FEARG_1, ret_list_number, f_getpos},
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200663 {"getqflist", 0, 1, 0, ret_list_or_dict_0, f_getqflist},
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200664 {"getreg", 0, 3, FEARG_1, ret_getreg, f_getreg},
Bram Moolenaarbb861e22020-06-07 18:16:36 +0200665 {"getreginfo", 0, 1, FEARG_1, ret_dict_any, f_getreginfo},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100666 {"getregtype", 0, 1, FEARG_1, ret_string, f_getregtype},
667 {"gettabinfo", 0, 1, FEARG_1, ret_list_dict_any, f_gettabinfo},
668 {"gettabvar", 2, 3, FEARG_1, ret_any, f_gettabvar},
669 {"gettabwinvar", 3, 4, FEARG_1, ret_any, f_gettabwinvar},
670 {"gettagstack", 0, 1, FEARG_1, ret_dict_any, f_gettagstack},
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +0200671 {"gettext", 1, 1, FEARG_1, ret_string, f_gettext},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100672 {"getwininfo", 0, 1, FEARG_1, ret_list_dict_any, f_getwininfo},
673 {"getwinpos", 0, 1, FEARG_1, ret_list_number, f_getwinpos},
674 {"getwinposx", 0, 0, 0, ret_number, f_getwinposx},
675 {"getwinposy", 0, 0, 0, ret_number, f_getwinposy},
676 {"getwinvar", 2, 3, FEARG_1, ret_any, f_getwinvar},
677 {"glob", 1, 4, FEARG_1, ret_any, f_glob},
678 {"glob2regpat", 1, 1, FEARG_1, ret_string, f_glob2regpat},
679 {"globpath", 2, 5, FEARG_2, ret_any, f_globpath},
Bram Moolenaar79296512020-03-22 16:17:14 +0100680 {"has", 1, 2, 0, ret_number, f_has},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100681 {"has_key", 2, 2, FEARG_1, ret_number, f_has_key},
682 {"haslocaldir", 0, 2, FEARG_1, ret_number, f_haslocaldir},
683 {"hasmapto", 1, 3, FEARG_1, ret_number, f_hasmapto},
684 {"highlightID", 1, 1, FEARG_1, ret_number, f_hlID}, // obsolete
685 {"highlight_exists",1, 1, FEARG_1, ret_number, f_hlexists}, // obsolete
686 {"histadd", 2, 2, FEARG_2, ret_number, f_histadd},
687 {"histdel", 1, 2, FEARG_1, ret_number, f_histdel},
688 {"histget", 1, 2, FEARG_1, ret_string, f_histget},
689 {"histnr", 1, 1, FEARG_1, ret_number, f_histnr},
690 {"hlID", 1, 1, FEARG_1, ret_number, f_hlID},
691 {"hlexists", 1, 1, FEARG_1, ret_number, f_hlexists},
692 {"hostname", 0, 0, 0, ret_string, f_hostname},
693 {"iconv", 3, 3, FEARG_1, ret_string, f_iconv},
694 {"indent", 1, 1, FEARG_1, ret_number, f_indent},
695 {"index", 2, 4, FEARG_1, ret_number, f_index},
696 {"input", 1, 3, FEARG_1, ret_string, f_input},
697 {"inputdialog", 1, 3, FEARG_1, ret_string, f_inputdialog},
698 {"inputlist", 1, 1, FEARG_1, ret_number, f_inputlist},
699 {"inputrestore", 0, 0, 0, ret_number, f_inputrestore},
700 {"inputsave", 0, 0, 0, ret_number, f_inputsave},
701 {"inputsecret", 1, 2, FEARG_1, ret_string, f_inputsecret},
Bram Moolenaar252e88a2020-07-05 20:47:18 +0200702 {"insert", 2, 3, FEARG_1, ret_first_arg, f_insert},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100703 {"interrupt", 0, 0, 0, ret_void, f_interrupt},
704 {"invert", 1, 1, FEARG_1, ret_number, f_invert},
705 {"isdirectory", 1, 1, FEARG_1, ret_number, f_isdirectory},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100706 {"isinf", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isinf)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100707 {"islocked", 1, 1, FEARG_1, ret_number, f_islocked},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 {"isnan", 1, 1, FEARG_1, ret_number, MATH_FUNC(f_isnan)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100709 {"items", 1, 1, FEARG_1, ret_list_any, f_items},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100710 {"job_getchannel", 1, 1, FEARG_1, ret_channel, JOB_FUNC(f_job_getchannel)},
711 {"job_info", 0, 1, FEARG_1, ret_dict_any, JOB_FUNC(f_job_info)},
712 {"job_setoptions", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_job_setoptions)},
713 {"job_start", 1, 2, FEARG_1, ret_job, JOB_FUNC(f_job_start)},
714 {"job_status", 1, 1, FEARG_1, ret_string, JOB_FUNC(f_job_status)},
715 {"job_stop", 1, 2, FEARG_1, ret_number, JOB_FUNC(f_job_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100716 {"join", 1, 2, FEARG_1, ret_string, f_join},
717 {"js_decode", 1, 1, FEARG_1, ret_any, f_js_decode},
718 {"js_encode", 1, 1, FEARG_1, ret_string, f_js_encode},
719 {"json_decode", 1, 1, FEARG_1, ret_any, f_json_decode},
720 {"json_encode", 1, 1, FEARG_1, ret_string, f_json_encode},
Bram Moolenaar32f335f2020-08-14 18:56:45 +0200721 {"keys", 1, 1, FEARG_1, ret_list_string, f_keys},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100722 {"last_buffer_nr", 0, 0, 0, ret_number, f_last_buffer_nr}, // obsolete
723 {"len", 1, 1, FEARG_1, ret_number, f_len},
724 {"libcall", 3, 3, FEARG_3, ret_string, f_libcall},
725 {"libcallnr", 3, 3, FEARG_3, ret_number, f_libcallnr},
726 {"line", 1, 2, FEARG_1, ret_number, f_line},
727 {"line2byte", 1, 1, FEARG_1, ret_number, f_line2byte},
728 {"lispindent", 1, 1, FEARG_1, ret_number, f_lispindent},
729 {"list2str", 1, 2, FEARG_1, ret_string, f_list2str},
730 {"listener_add", 1, 2, FEARG_2, ret_number, f_listener_add},
731 {"listener_flush", 0, 1, FEARG_1, ret_void, f_listener_flush},
732 {"listener_remove", 1, 1, FEARG_1, ret_number, f_listener_remove},
733 {"localtime", 0, 0, 0, ret_number, f_localtime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100734 {"log", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log)},
735 {"log10", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_log10)},
736 {"luaeval", 1, 2, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200737#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +0100738 f_luaeval
739#else
740 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200741#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100742 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100743 {"map", 2, 2, FEARG_1, ret_any, f_map},
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200744 {"maparg", 1, 4, FEARG_1, ret_maparg, f_maparg},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100745 {"mapcheck", 1, 3, FEARG_1, ret_string, f_mapcheck},
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200746 {"mapset", 3, 3, FEARG_1, ret_void, f_mapset},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100747 {"match", 2, 4, FEARG_1, ret_any, f_match},
748 {"matchadd", 2, 5, FEARG_1, ret_number, f_matchadd},
749 {"matchaddpos", 2, 5, FEARG_1, ret_number, f_matchaddpos},
750 {"matcharg", 1, 1, FEARG_1, ret_list_string, f_matcharg},
751 {"matchdelete", 1, 2, FEARG_1, ret_number, f_matchdelete},
752 {"matchend", 2, 4, FEARG_1, ret_number, f_matchend},
753 {"matchlist", 2, 4, FEARG_1, ret_list_string, f_matchlist},
754 {"matchstr", 2, 4, FEARG_1, ret_string, f_matchstr},
755 {"matchstrpos", 2, 4, FEARG_1, ret_list_any, f_matchstrpos},
756 {"max", 1, 1, FEARG_1, ret_any, f_max},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100757 {"menu_info", 1, 2, FEARG_1, ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100758#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +0100759 f_menu_info
760#else
761 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +0100762#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100763 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100764 {"min", 1, 1, FEARG_1, ret_any, f_min},
765 {"mkdir", 1, 3, FEARG_1, ret_number, f_mkdir},
766 {"mode", 0, 1, FEARG_1, ret_string, f_mode},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100767 {"mzeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200768#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100769 f_mzeval
770#else
771 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200772#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100773 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100774 {"nextnonblank", 1, 1, FEARG_1, ret_number, f_nextnonblank},
775 {"nr2char", 1, 2, FEARG_1, ret_string, f_nr2char},
776 {"or", 2, 2, FEARG_1, ret_number, f_or},
777 {"pathshorten", 1, 1, FEARG_1, ret_string, f_pathshorten},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100778 {"perleval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100780 f_perleval
781#else
782 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100784 },
785 {"popup_atcursor", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_atcursor)},
786 {"popup_beval", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_beval)},
Bram Moolenaar03a9f842020-05-13 13:40:16 +0200787 {"popup_clear", 0, 1, 0, ret_void, PROP_FUNC(f_popup_clear)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100788 {"popup_close", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_close)},
789 {"popup_create", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_create)},
790 {"popup_dialog", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_dialog)},
791 {"popup_filter_menu", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_menu)},
792 {"popup_filter_yesno", 2, 2, 0, ret_number, PROP_FUNC(f_popup_filter_yesno)},
793 {"popup_findinfo", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findinfo)},
794 {"popup_findpreview", 0, 0, 0, ret_number, PROP_FUNC(f_popup_findpreview)},
795 {"popup_getoptions", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getoptions)},
796 {"popup_getpos", 1, 1, FEARG_1, ret_dict_any, PROP_FUNC(f_popup_getpos)},
797 {"popup_hide", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_hide)},
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200798 {"popup_list", 0, 0, 0, ret_list_number, PROP_FUNC(f_popup_list)},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100799 {"popup_locate", 2, 2, 0, ret_number, PROP_FUNC(f_popup_locate)},
800 {"popup_menu", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_menu)},
801 {"popup_move", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_move)},
802 {"popup_notification", 2, 2, FEARG_1, ret_number, PROP_FUNC(f_popup_notification)},
803 {"popup_setoptions", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_setoptions)},
804 {"popup_settext", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_popup_settext)},
805 {"popup_show", 1, 1, FEARG_1, ret_void, PROP_FUNC(f_popup_show)},
806 {"pow", 2, 2, FEARG_1, ret_float, FLOAT_FUNC(f_pow)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100807 {"prevnonblank", 1, 1, FEARG_1, ret_number, f_prevnonblank},
808 {"printf", 1, 19, FEARG_2, ret_string, f_printf},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100809 {"prompt_setcallback", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setcallback)},
810 {"prompt_setinterrupt", 2, 2, FEARG_1,ret_void, JOB_FUNC(f_prompt_setinterrupt)},
811 {"prompt_setprompt", 2, 2, FEARG_1, ret_void, JOB_FUNC(f_prompt_setprompt)},
812 {"prop_add", 3, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_add)},
813 {"prop_clear", 1, 3, FEARG_1, ret_void, PROP_FUNC(f_prop_clear)},
814 {"prop_find", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_find)},
815 {"prop_list", 1, 2, FEARG_1, ret_list_dict_any, PROP_FUNC(f_prop_list)},
816 {"prop_remove", 1, 3, FEARG_1, ret_number, PROP_FUNC(f_prop_remove)},
817 {"prop_type_add", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_add)},
818 {"prop_type_change", 2, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_change)},
819 {"prop_type_delete", 1, 2, FEARG_1, ret_void, PROP_FUNC(f_prop_type_delete)},
820 {"prop_type_get", 1, 2, FEARG_1, ret_dict_any, PROP_FUNC(f_prop_type_get)},
821 {"prop_type_list", 0, 1, FEARG_1, ret_list_string, PROP_FUNC(f_prop_type_list)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100822 {"pum_getpos", 0, 0, 0, ret_dict_number, f_pum_getpos},
823 {"pumvisible", 0, 0, 0, ret_number, f_pumvisible},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100824 {"py3eval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +0100826 f_py3eval
827#else
828 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200829#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100830 },
831 {"pyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +0100833 f_pyeval
834#else
835 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200836#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100837 },
838 {"pyxeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100839#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +0100840 f_pyxeval
841#else
842 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100843#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100844 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100845 {"rand", 0, 1, FEARG_1, ret_number, f_rand},
846 {"range", 1, 3, FEARG_1, ret_list_number, f_range},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200847 {"readdir", 1, 3, FEARG_1, ret_list_string, f_readdir},
848 {"readdirex", 1, 3, FEARG_1, ret_list_dict_any, f_readdirex},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100849 {"readfile", 1, 3, FEARG_1, ret_any, f_readfile},
Bram Moolenaar85629982020-06-01 18:39:20 +0200850 {"reduce", 2, 3, FEARG_1, ret_any, f_reduce},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100851 {"reg_executing", 0, 0, 0, ret_string, f_reg_executing},
852 {"reg_recording", 0, 0, 0, ret_string, f_reg_recording},
853 {"reltime", 0, 2, FEARG_1, ret_list_any, f_reltime},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100854 {"reltimefloat", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_reltimefloat)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100855 {"reltimestr", 1, 1, FEARG_1, ret_string, f_reltimestr},
856 {"remote_expr", 2, 4, FEARG_1, ret_string, f_remote_expr},
857 {"remote_foreground", 1, 1, FEARG_1, ret_string, f_remote_foreground},
858 {"remote_peek", 1, 2, FEARG_1, ret_number, f_remote_peek},
859 {"remote_read", 1, 2, FEARG_1, ret_string, f_remote_read},
860 {"remote_send", 2, 3, FEARG_1, ret_string, f_remote_send},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200861 {"remote_startserver", 1, 1, FEARG_1, ret_void, f_remote_startserver},
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200862 {"remove", 2, 3, FEARG_1, ret_remove, f_remove},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100863 {"rename", 2, 2, FEARG_1, ret_number, f_rename},
Bram Moolenaar9978d472020-07-05 16:01:56 +0200864 {"repeat", 2, 2, FEARG_1, ret_first_arg, f_repeat},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100865 {"resolve", 1, 1, FEARG_1, ret_string, f_resolve},
Bram Moolenaar67627352020-07-05 21:10:24 +0200866 {"reverse", 1, 1, FEARG_1, ret_first_arg, f_reverse},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100867 {"round", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_round)},
868 {"rubyeval", 1, 1, FEARG_1, ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100869#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +0100870 f_rubyeval
871#else
872 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100873#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100874 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100875 {"screenattr", 2, 2, FEARG_1, ret_number, f_screenattr},
876 {"screenchar", 2, 2, FEARG_1, ret_number, f_screenchar},
877 {"screenchars", 2, 2, FEARG_1, ret_list_number, f_screenchars},
878 {"screencol", 0, 0, 0, ret_number, f_screencol},
879 {"screenpos", 3, 3, FEARG_1, ret_dict_number, f_screenpos},
880 {"screenrow", 0, 0, 0, ret_number, f_screenrow},
881 {"screenstring", 2, 2, FEARG_1, ret_string, f_screenstring},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200882 {"search", 1, 5, FEARG_1, ret_number, f_search},
Bram Moolenaare8f5ec02020-06-01 17:28:35 +0200883 {"searchcount", 0, 1, FEARG_1, ret_dict_any, f_searchcount},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100884 {"searchdecl", 1, 3, FEARG_1, ret_number, f_searchdecl},
885 {"searchpair", 3, 7, 0, ret_number, f_searchpair},
886 {"searchpairpos", 3, 7, 0, ret_list_number, f_searchpairpos},
Bram Moolenaaradc17a52020-06-06 18:37:51 +0200887 {"searchpos", 1, 5, FEARG_1, ret_list_number, f_searchpos},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100888 {"server2client", 2, 2, FEARG_1, ret_number, f_server2client},
889 {"serverlist", 0, 0, 0, ret_string, f_serverlist},
890 {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline},
891 {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar},
Bram Moolenaar08aac3c2020-08-28 21:04:24 +0200892 {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100893 {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch},
894 {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos},
895 {"setenv", 2, 2, FEARG_2, ret_void, f_setenv},
896 {"setfperm", 2, 2, FEARG_1, ret_number, f_setfperm},
897 {"setline", 2, 2, FEARG_2, ret_number, f_setline},
898 {"setloclist", 2, 4, FEARG_2, ret_number, f_setloclist},
899 {"setmatches", 1, 2, FEARG_1, ret_number, f_setmatches},
900 {"setpos", 2, 2, FEARG_2, ret_number, f_setpos},
901 {"setqflist", 1, 3, FEARG_1, ret_number, f_setqflist},
902 {"setreg", 2, 3, FEARG_2, ret_number, f_setreg},
903 {"settabvar", 3, 3, FEARG_3, ret_void, f_settabvar},
904 {"settabwinvar", 4, 4, FEARG_4, ret_void, f_settabwinvar},
905 {"settagstack", 2, 3, FEARG_2, ret_number, f_settagstack},
906 {"setwinvar", 3, 3, FEARG_3, ret_void, f_setwinvar},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100907 {"sha256", 1, 1, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +0100909 f_sha256
910#else
911 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100913 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100914 {"shellescape", 1, 2, FEARG_1, ret_string, f_shellescape},
915 {"shiftwidth", 0, 1, FEARG_1, ret_number, f_shiftwidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100916 {"sign_define", 1, 2, FEARG_1, ret_any, SIGN_FUNC(f_sign_define)},
917 {"sign_getdefined", 0, 1, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
918 {"sign_getplaced", 0, 2, FEARG_1, ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
919 {"sign_jump", 3, 3, FEARG_1, ret_number, SIGN_FUNC(f_sign_jump)},
920 {"sign_place", 4, 5, FEARG_1, ret_number, SIGN_FUNC(f_sign_place)},
921 {"sign_placelist", 1, 1, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_placelist)},
922 {"sign_undefine", 0, 1, FEARG_1, ret_number, SIGN_FUNC(f_sign_undefine)},
923 {"sign_unplace", 1, 2, FEARG_1, ret_number, SIGN_FUNC(f_sign_unplace)},
924 {"sign_unplacelist", 1, 2, FEARG_1, ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200925 {"simplify", 1, 1, FEARG_1, ret_string, f_simplify},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100926 {"sin", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sin)},
927 {"sinh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200928 {"sort", 1, 3, FEARG_1, ret_first_arg, f_sort},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100929 {"sound_clear", 0, 0, 0, ret_void, SOUND_FUNC(f_sound_clear)},
930 {"sound_playevent", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playevent)},
931 {"sound_playfile", 1, 2, FEARG_1, ret_number, SOUND_FUNC(f_sound_playfile)},
932 {"sound_stop", 1, 1, FEARG_1, ret_void, SOUND_FUNC(f_sound_stop)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100933 {"soundfold", 1, 1, FEARG_1, ret_string, f_soundfold},
934 {"spellbadword", 0, 1, FEARG_1, ret_list_string, f_spellbadword},
935 {"spellsuggest", 1, 3, FEARG_1, ret_list_string, f_spellsuggest},
936 {"split", 1, 3, FEARG_1, ret_list_string, f_split},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100937 {"sqrt", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_sqrt)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100938 {"srand", 0, 1, FEARG_1, ret_list_number, f_srand},
939 {"state", 0, 1, FEARG_1, ret_string, f_state},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100940 {"str2float", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_str2float)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100941 {"str2list", 1, 2, FEARG_1, ret_list_number, f_str2list},
942 {"str2nr", 1, 3, FEARG_1, ret_number, f_str2nr},
943 {"strcharpart", 2, 3, FEARG_1, ret_string, f_strcharpart},
944 {"strchars", 1, 2, FEARG_1, ret_number, f_strchars},
945 {"strdisplaywidth", 1, 2, FEARG_1, ret_number, f_strdisplaywidth},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100946 {"strftime", 1, 2, FEARG_1, ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100948 f_strftime
949#else
950 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200951#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100952 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100953 {"strgetchar", 2, 2, FEARG_1, ret_number, f_strgetchar},
954 {"stridx", 2, 3, FEARG_1, ret_number, f_stridx},
955 {"string", 1, 1, FEARG_1, ret_string, f_string},
956 {"strlen", 1, 1, FEARG_1, ret_number, f_strlen},
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200957 {"strpart", 2, 4, FEARG_1, ret_string, f_strpart},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100958 {"strptime", 2, 2, FEARG_1, ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +0100959#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +0100960 f_strptime
961#else
962 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +0100963#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100964 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100965 {"strridx", 2, 3, FEARG_1, ret_number, f_strridx},
966 {"strtrans", 1, 1, FEARG_1, ret_string, f_strtrans},
967 {"strwidth", 1, 1, FEARG_1, ret_number, f_strwidth},
968 {"submatch", 1, 2, FEARG_1, ret_string, f_submatch},
969 {"substitute", 4, 4, FEARG_1, ret_string, f_substitute},
970 {"swapinfo", 1, 1, FEARG_1, ret_dict_any, f_swapinfo},
971 {"swapname", 1, 1, FEARG_1, ret_string, f_swapname},
972 {"synID", 3, 3, 0, ret_number, f_synID},
973 {"synIDattr", 2, 3, FEARG_1, ret_string, f_synIDattr},
974 {"synIDtrans", 1, 1, FEARG_1, ret_number, f_synIDtrans},
975 {"synconcealed", 2, 2, 0, ret_list_any, f_synconcealed},
976 {"synstack", 2, 2, 0, ret_list_number, f_synstack},
977 {"system", 1, 2, FEARG_1, ret_string, f_system},
978 {"systemlist", 1, 2, FEARG_1, ret_list_string, f_systemlist},
979 {"tabpagebuflist", 0, 1, FEARG_1, ret_list_number, f_tabpagebuflist},
980 {"tabpagenr", 0, 1, 0, ret_number, f_tabpagenr},
981 {"tabpagewinnr", 1, 2, FEARG_1, ret_number, f_tabpagewinnr},
982 {"tagfiles", 0, 0, 0, ret_list_string, f_tagfiles},
983 {"taglist", 1, 2, FEARG_1, ret_list_dict_any, f_taglist},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100984 {"tan", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tan)},
985 {"tanh", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_tanh)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100986 {"tempname", 0, 0, 0, ret_string, f_tempname},
Bram Moolenaar15c47602020-03-26 22:16:48 +0100987 {"term_dumpdiff", 2, 3, FEARG_1, ret_number, TERM_FUNC(f_term_dumpdiff)},
988 {"term_dumpload", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_dumpload)},
989 {"term_dumpwrite", 2, 3, FEARG_2, ret_void, TERM_FUNC(f_term_dumpwrite)},
990 {"term_getaltscreen", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getaltscreen)},
991 {"term_getansicolors", 1, 1, FEARG_1, ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +0100992#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +0100993 f_term_getansicolors
994#else
995 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100997 },
998 {"term_getattr", 2, 2, FEARG_1, ret_number, TERM_FUNC(f_term_getattr)},
999 {"term_getcursor", 1, 1, FEARG_1, ret_list_any, TERM_FUNC(f_term_getcursor)},
1000 {"term_getjob", 1, 1, FEARG_1, ret_job, TERM_FUNC(f_term_getjob)},
1001 {"term_getline", 2, 2, FEARG_1, ret_string, TERM_FUNC(f_term_getline)},
1002 {"term_getscrolled", 1, 1, FEARG_1, ret_number, TERM_FUNC(f_term_getscrolled)},
1003 {"term_getsize", 1, 1, FEARG_1, ret_list_number, TERM_FUNC(f_term_getsize)},
1004 {"term_getstatus", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_getstatus)},
1005 {"term_gettitle", 1, 1, FEARG_1, ret_string, TERM_FUNC(f_term_gettitle)},
1006 {"term_gettty", 1, 2, FEARG_1, ret_string, TERM_FUNC(f_term_gettty)},
1007 {"term_list", 0, 0, 0, ret_list_number, TERM_FUNC(f_term_list)},
1008 {"term_scrape", 2, 2, FEARG_1, ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1009 {"term_sendkeys", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_sendkeys)},
1010 {"term_setansicolors", 2, 2, FEARG_1, ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001011#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001012 f_term_setansicolors
1013#else
1014 NULL
1015#endif
1016 },
1017 {"term_setapi", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setapi)},
1018 {"term_setkill", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setkill)},
1019 {"term_setrestore", 2, 2, FEARG_1, ret_void, TERM_FUNC(f_term_setrestore)},
1020 {"term_setsize", 3, 3, FEARG_1, ret_void, TERM_FUNC(f_term_setsize)},
1021 {"term_start", 1, 2, FEARG_1, ret_number, TERM_FUNC(f_term_start)},
1022 {"term_wait", 1, 2, FEARG_1, ret_void, TERM_FUNC(f_term_wait)},
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +02001023 {"terminalprops", 0, 0, 0, ret_dict_string, f_terminalprops},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001024 {"test_alloc_fail", 3, 3, FEARG_1, ret_void, f_test_alloc_fail},
1025 {"test_autochdir", 0, 0, 0, ret_void, f_test_autochdir},
1026 {"test_feedinput", 1, 1, FEARG_1, ret_void, f_test_feedinput},
1027 {"test_garbagecollect_now", 0, 0, 0, ret_void, f_test_garbagecollect_now},
1028 {"test_garbagecollect_soon", 0, 0, 0, ret_void, f_test_garbagecollect_soon},
1029 {"test_getvalue", 1, 1, FEARG_1, ret_number, f_test_getvalue},
1030 {"test_ignore_error", 1, 1, FEARG_1, ret_void, f_test_ignore_error},
1031 {"test_null_blob", 0, 0, 0, ret_blob, f_test_null_blob},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001032 {"test_null_channel", 0, 0, 0, ret_channel, JOB_FUNC(f_test_null_channel)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001033 {"test_null_dict", 0, 0, 0, ret_dict_any, f_test_null_dict},
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001034 {"test_null_function", 0, 0, 0, ret_func_any, f_test_null_function},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001035 {"test_null_job", 0, 0, 0, ret_job, JOB_FUNC(f_test_null_job)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001036 {"test_null_list", 0, 0, 0, ret_list_any, f_test_null_list},
Bram Moolenaard77a8522020-04-03 21:59:57 +02001037 {"test_null_partial", 0, 0, 0, ret_func_any, f_test_null_partial},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001038 {"test_null_string", 0, 0, 0, ret_string, f_test_null_string},
1039 {"test_option_not_set", 1, 1, FEARG_1,ret_void, f_test_option_not_set},
1040 {"test_override", 2, 2, FEARG_2, ret_void, f_test_override},
1041 {"test_refcount", 1, 1, FEARG_1, ret_number, f_test_refcount},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001042 {"test_scrollbar", 3, 3, FEARG_2, ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001043#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001044 f_test_scrollbar
1045#else
1046 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001047#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001048 },
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001049 {"test_setmouse", 2, 2, 0, ret_void, f_test_setmouse},
1050 {"test_settime", 1, 1, FEARG_1, ret_void, f_test_settime},
1051 {"test_srand_seed", 0, 1, FEARG_1, ret_void, f_test_srand_seed},
1052 {"test_unknown", 0, 0, 0, ret_any, f_test_unknown},
Bram Moolenaar418f1df2020-08-12 21:34:49 +02001053 {"test_void", 0, 0, 0, ret_void, f_test_void},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001054 {"timer_info", 0, 1, FEARG_1, ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1055 {"timer_pause", 2, 2, FEARG_1, ret_void, TIMER_FUNC(f_timer_pause)},
1056 {"timer_start", 2, 3, FEARG_1, ret_number, TIMER_FUNC(f_timer_start)},
1057 {"timer_stop", 1, 1, FEARG_1, ret_void, TIMER_FUNC(f_timer_stop)},
1058 {"timer_stopall", 0, 0, 0, ret_void, TIMER_FUNC(f_timer_stopall)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001059 {"tolower", 1, 1, FEARG_1, ret_string, f_tolower},
1060 {"toupper", 1, 1, FEARG_1, ret_string, f_toupper},
1061 {"tr", 3, 3, FEARG_1, ret_string, f_tr},
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001062 {"trim", 1, 3, FEARG_1, ret_string, f_trim},
Bram Moolenaar15c47602020-03-26 22:16:48 +01001063 {"trunc", 1, 1, FEARG_1, ret_float, FLOAT_FUNC(f_trunc)},
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001064 {"type", 1, 1, FEARG_1, ret_number, f_type},
1065 {"undofile", 1, 1, FEARG_1, ret_string, f_undofile},
1066 {"undotree", 0, 0, 0, ret_dict_any, f_undotree},
1067 {"uniq", 1, 3, FEARG_1, ret_list_any, f_uniq},
1068 {"values", 1, 1, FEARG_1, ret_list_any, f_values},
1069 {"virtcol", 1, 1, FEARG_1, ret_number, f_virtcol},
1070 {"visualmode", 0, 1, 0, ret_string, f_visualmode},
1071 {"wildmenumode", 0, 0, 0, ret_number, f_wildmenumode},
1072 {"win_execute", 2, 3, FEARG_2, ret_string, f_win_execute},
1073 {"win_findbuf", 1, 1, FEARG_1, ret_list_number, f_win_findbuf},
1074 {"win_getid", 0, 2, FEARG_1, ret_number, f_win_getid},
1075 {"win_gettype", 0, 1, FEARG_1, ret_string, f_win_gettype},
1076 {"win_gotoid", 1, 1, FEARG_1, ret_number, f_win_gotoid},
1077 {"win_id2tabwin", 1, 1, FEARG_1, ret_list_number, f_win_id2tabwin},
1078 {"win_id2win", 1, 1, FEARG_1, ret_number, f_win_id2win},
1079 {"win_screenpos", 1, 1, FEARG_1, ret_list_number, f_win_screenpos},
1080 {"win_splitmove", 2, 3, FEARG_1, ret_number, f_win_splitmove},
1081 {"winbufnr", 1, 1, FEARG_1, ret_number, f_winbufnr},
1082 {"wincol", 0, 0, 0, ret_number, f_wincol},
1083 {"windowsversion", 0, 0, 0, ret_string, f_windowsversion},
1084 {"winheight", 1, 1, FEARG_1, ret_number, f_winheight},
1085 {"winlayout", 0, 1, FEARG_1, ret_list_any, f_winlayout},
1086 {"winline", 0, 0, 0, ret_number, f_winline},
1087 {"winnr", 0, 1, FEARG_1, ret_number, f_winnr},
1088 {"winrestcmd", 0, 0, 0, ret_string, f_winrestcmd},
1089 {"winrestview", 1, 1, FEARG_1, ret_void, f_winrestview},
1090 {"winsaveview", 0, 0, 0, ret_dict_any, f_winsaveview},
1091 {"winwidth", 1, 1, FEARG_1, ret_number, f_winwidth},
1092 {"wordcount", 0, 0, 0, ret_dict_number, f_wordcount},
1093 {"writefile", 2, 3, FEARG_1, ret_number, f_writefile},
1094 {"xor", 2, 2, FEARG_1, ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001095};
1096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001097/*
1098 * Function given to ExpandGeneric() to obtain the list of internal
1099 * or user defined function names.
1100 */
1101 char_u *
1102get_function_name(expand_T *xp, int idx)
1103{
1104 static int intidx = -1;
1105 char_u *name;
1106
1107 if (idx == 0)
1108 intidx = -1;
1109 if (intidx < 0)
1110 {
1111 name = get_user_func_name(xp, idx);
1112 if (name != NULL)
1113 return name;
1114 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001115 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001117 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001118 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001119 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001120 STRCAT(IObuff, ")");
1121 return IObuff;
1122 }
1123
1124 return NULL;
1125}
1126
1127/*
1128 * Function given to ExpandGeneric() to obtain the list of internal or
1129 * user defined variable or function names.
1130 */
1131 char_u *
1132get_expr_name(expand_T *xp, int idx)
1133{
1134 static int intidx = -1;
1135 char_u *name;
1136
1137 if (idx == 0)
1138 intidx = -1;
1139 if (intidx < 0)
1140 {
1141 name = get_function_name(xp, idx);
1142 if (name != NULL)
1143 return name;
1144 }
1145 return get_user_var_name(xp, ++intidx);
1146}
1147
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001149 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001150 * Return index, or -1 if not found or "implemented" is TRUE and the function
1151 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001152 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001153 static int
1154find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001155{
1156 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001157 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001158 int cmp;
1159 int x;
1160
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001161 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001162
1163 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001164 while (first <= last)
1165 {
1166 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001167 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001168 if (cmp < 0)
1169 last = x - 1;
1170 else if (cmp > 0)
1171 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001172 else if (implemented && global_functions[x].f_func == NULL)
1173 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174 else
1175 return x;
1176 }
1177 return -1;
1178}
1179
Bram Moolenaar15c47602020-03-26 22:16:48 +01001180/*
1181 * Find internal function "name" in table "global_functions".
1182 * Return index, or -1 if not found or the function is not implemented.
1183 */
1184 int
1185find_internal_func(char_u *name)
1186{
1187 return find_internal_func_opt(name, TRUE);
1188}
1189
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001190 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001191has_internal_func(char_u *name)
1192{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001193 return find_internal_func_opt(name, TRUE) >= 0;
1194}
1195
1196 static int
1197has_internal_func_name(char_u *name)
1198{
1199 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001200}
1201
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 char *
1203internal_func_name(int idx)
1204{
1205 return global_functions[idx].f_name;
1206}
1207
1208 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001209internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001211 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212}
1213
1214/*
1215 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02001216 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
1217 * first argument, 2 if method base is second argument, etc. 9 if method base
1218 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 */
1220 int
1221check_internal_func(int idx, int argcount)
1222{
1223 int res;
1224 char *name;
1225
1226 if (argcount < global_functions[idx].f_min_argc)
1227 res = FCERR_TOOFEW;
1228 else if (argcount > global_functions[idx].f_max_argc)
1229 res = FCERR_TOOMANY;
1230 else
Bram Moolenaar389df252020-07-09 21:20:47 +02001231 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
1233 name = internal_func_name(idx);
1234 if (res == FCERR_TOOMANY)
1235 semsg(_(e_toomanyarg), name);
1236 else
1237 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02001238 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239}
1240
Bram Moolenaarac92e252019-08-03 21:58:38 +02001241 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242call_internal_func(
1243 char_u *name,
1244 int argcount,
1245 typval_T *argvars,
1246 typval_T *rettv)
1247{
1248 int i;
1249
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001250 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001251 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001252 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001253 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001254 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001255 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001256 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001257 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001258 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001259 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001260}
1261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 void
1263call_internal_func_by_idx(
1264 int idx,
1265 typval_T *argvars,
1266 typval_T *rettv)
1267{
1268 global_functions[idx].f_func(argvars, rettv);
1269}
1270
Bram Moolenaarac92e252019-08-03 21:58:38 +02001271/*
1272 * Invoke a method for base->method().
1273 */
1274 int
1275call_internal_method(
1276 char_u *name,
1277 int argcount,
1278 typval_T *argvars,
1279 typval_T *rettv,
1280 typval_T *basetv)
1281{
1282 int i;
1283 int fi;
1284 typval_T argv[MAX_FUNC_ARGS + 1];
1285
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001286 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001287 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001288 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001289 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001290 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001291 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001292 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001293 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01001294 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001295
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001296 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001297 {
1298 // base value goes last
1299 for (i = 0; i < argcount; ++i)
1300 argv[i] = argvars[i];
1301 argv[argcount] = *basetv;
1302 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001303 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001304 {
1305 // base value goes second
1306 argv[0] = argvars[0];
1307 argv[1] = *basetv;
1308 for (i = 1; i < argcount; ++i)
1309 argv[i + 1] = argvars[i];
1310 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001311 else if (global_functions[fi].f_argtype == FEARG_3)
1312 {
1313 // base value goes third
1314 argv[0] = argvars[0];
1315 argv[1] = argvars[1];
1316 argv[2] = *basetv;
1317 for (i = 2; i < argcount; ++i)
1318 argv[i + 1] = argvars[i];
1319 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001320 else if (global_functions[fi].f_argtype == FEARG_4)
1321 {
1322 // base value goes fourth
1323 argv[0] = argvars[0];
1324 argv[1] = argvars[1];
1325 argv[2] = argvars[2];
1326 argv[3] = *basetv;
1327 for (i = 3; i < argcount; ++i)
1328 argv[i + 1] = argvars[i];
1329 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001330 else
1331 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001332 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001333 argv[0] = *basetv;
1334 for (i = 0; i < argcount; ++i)
1335 argv[i + 1] = argvars[i];
1336 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001337 argv[argcount + 1].v_type = VAR_UNKNOWN;
1338
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001339 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01001340 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341}
1342
1343/*
1344 * Return TRUE for a non-zero Number and a non-empty String.
1345 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001346 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347non_zero_arg(typval_T *argvars)
1348{
1349 return ((argvars[0].v_type == VAR_NUMBER
1350 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001351 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 && argvars[0].vval.v_number == VVAL_TRUE)
1353 || (argvars[0].v_type == VAR_STRING
1354 && argvars[0].vval.v_string != NULL
1355 && *argvars[0].vval.v_string != NUL));
1356}
1357
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359/*
1360 * Get the float value of "argvars[0]" into "f".
1361 * Returns FAIL when the argument is not a Number or Float.
1362 */
1363 static int
1364get_float_arg(typval_T *argvars, float_T *f)
1365{
1366 if (argvars[0].v_type == VAR_FLOAT)
1367 {
1368 *f = argvars[0].vval.v_float;
1369 return OK;
1370 }
1371 if (argvars[0].v_type == VAR_NUMBER)
1372 {
1373 *f = (float_T)argvars[0].vval.v_number;
1374 return OK;
1375 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001376 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377 return FAIL;
1378}
1379
1380/*
1381 * "abs(expr)" function
1382 */
1383 static void
1384f_abs(typval_T *argvars, typval_T *rettv)
1385{
1386 if (argvars[0].v_type == VAR_FLOAT)
1387 {
1388 rettv->v_type = VAR_FLOAT;
1389 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1390 }
1391 else
1392 {
1393 varnumber_T n;
1394 int error = FALSE;
1395
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001396 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397 if (error)
1398 rettv->vval.v_number = -1;
1399 else if (n > 0)
1400 rettv->vval.v_number = n;
1401 else
1402 rettv->vval.v_number = -n;
1403 }
1404}
1405
1406/*
1407 * "acos()" function
1408 */
1409 static void
1410f_acos(typval_T *argvars, typval_T *rettv)
1411{
1412 float_T f = 0.0;
1413
1414 rettv->v_type = VAR_FLOAT;
1415 if (get_float_arg(argvars, &f) == OK)
1416 rettv->vval.v_float = acos(f);
1417 else
1418 rettv->vval.v_float = 0.0;
1419}
1420#endif
1421
1422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 * "and(expr, expr)" function
1424 */
1425 static void
1426f_and(typval_T *argvars, typval_T *rettv)
1427{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001428 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1429 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001430}
1431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001432#ifdef FEAT_FLOAT
1433/*
1434 * "asin()" function
1435 */
1436 static void
1437f_asin(typval_T *argvars, typval_T *rettv)
1438{
1439 float_T f = 0.0;
1440
1441 rettv->v_type = VAR_FLOAT;
1442 if (get_float_arg(argvars, &f) == OK)
1443 rettv->vval.v_float = asin(f);
1444 else
1445 rettv->vval.v_float = 0.0;
1446}
1447
1448/*
1449 * "atan()" function
1450 */
1451 static void
1452f_atan(typval_T *argvars, typval_T *rettv)
1453{
1454 float_T f = 0.0;
1455
1456 rettv->v_type = VAR_FLOAT;
1457 if (get_float_arg(argvars, &f) == OK)
1458 rettv->vval.v_float = atan(f);
1459 else
1460 rettv->vval.v_float = 0.0;
1461}
1462
1463/*
1464 * "atan2()" function
1465 */
1466 static void
1467f_atan2(typval_T *argvars, typval_T *rettv)
1468{
1469 float_T fx = 0.0, fy = 0.0;
1470
1471 rettv->v_type = VAR_FLOAT;
1472 if (get_float_arg(argvars, &fx) == OK
1473 && get_float_arg(&argvars[1], &fy) == OK)
1474 rettv->vval.v_float = atan2(fx, fy);
1475 else
1476 rettv->vval.v_float = 0.0;
1477}
1478#endif
1479
1480/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001481 * "balloon_show()" function
1482 */
1483#ifdef FEAT_BEVAL
1484 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001485f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1486{
1487 rettv->v_type = VAR_STRING;
1488 if (balloonEval != NULL)
1489 {
1490 if (balloonEval->msg == NULL)
1491 rettv->vval.v_string = NULL;
1492 else
1493 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1494 }
1495}
1496
1497 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001498f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1499{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001500 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001501 {
1502 if (argvars[0].v_type == VAR_LIST
1503# ifdef FEAT_GUI
1504 && !gui.in_use
1505# endif
1506 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001507 {
1508 list_T *l = argvars[0].vval.v_list;
1509
1510 // empty list removes the balloon
1511 post_balloon(balloonEval, NULL,
1512 l == NULL || l->lv_len == 0 ? NULL : l);
1513 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001514 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001515 {
1516 char_u *mesg = tv_get_string_chk(&argvars[0]);
1517
1518 if (mesg != NULL)
1519 // empty string removes the balloon
1520 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1521 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001522 }
1523}
1524
Bram Moolenaar669a8282017-11-19 20:13:05 +01001525# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001526 static void
1527f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1528{
1529 if (rettv_list_alloc(rettv) == OK)
1530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001531 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001532
1533 if (msg != NULL)
1534 {
1535 pumitem_T *array;
1536 int size = split_message(msg, &array);
1537 int i;
1538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001539 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01001540 for (i = 1; i < size - 1; ++i)
1541 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001542 while (size > 0)
1543 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001544 vim_free(array);
1545 }
1546 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001547}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001548# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001549#endif
1550
1551/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001552 * Get the buffer from "arg" and give an error and return NULL if it is not
1553 * valid.
1554 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001555 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001556get_buf_arg(typval_T *arg)
1557{
1558 buf_T *buf;
1559
1560 ++emsg_off;
1561 buf = tv_get_buf(arg, FALSE);
1562 --emsg_off;
1563 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001564 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001565 return buf;
1566}
1567
1568/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569 * "byte2line(byte)" function
1570 */
1571 static void
1572f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1573{
1574#ifndef FEAT_BYTEOFF
1575 rettv->vval.v_number = -1;
1576#else
1577 long boff = 0;
1578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001579 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580 if (boff < 0)
1581 rettv->vval.v_number = -1;
1582 else
1583 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1584 (linenr_T)0, &boff);
1585#endif
1586}
1587
1588 static void
1589byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1590{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001591 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001592 char_u *str;
1593 varnumber_T idx;
1594
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001595 str = tv_get_string_chk(&argvars[0]);
1596 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001597 rettv->vval.v_number = -1;
1598 if (str == NULL || idx < 0)
1599 return;
1600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 t = str;
1602 for ( ; idx > 0; idx--)
1603 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001604 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 return;
1606 if (enc_utf8 && comp)
1607 t += utf_ptr2len(t);
1608 else
1609 t += (*mb_ptr2len)(t);
1610 }
1611 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001612}
1613
1614/*
1615 * "byteidx()" function
1616 */
1617 static void
1618f_byteidx(typval_T *argvars, typval_T *rettv)
1619{
1620 byteidx(argvars, rettv, FALSE);
1621}
1622
1623/*
1624 * "byteidxcomp()" function
1625 */
1626 static void
1627f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1628{
1629 byteidx(argvars, rettv, TRUE);
1630}
1631
1632/*
1633 * "call(func, arglist [, dict])" function
1634 */
1635 static void
1636f_call(typval_T *argvars, typval_T *rettv)
1637{
1638 char_u *func;
1639 partial_T *partial = NULL;
1640 dict_T *selfdict = NULL;
1641
1642 if (argvars[1].v_type != VAR_LIST)
1643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001644 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645 return;
1646 }
1647 if (argvars[1].vval.v_list == NULL)
1648 return;
1649
1650 if (argvars[0].v_type == VAR_FUNC)
1651 func = argvars[0].vval.v_string;
1652 else if (argvars[0].v_type == VAR_PARTIAL)
1653 {
1654 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001655 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656 }
1657 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001658 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659 if (*func == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660 return; // type error or empty name
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661
1662 if (argvars[2].v_type != VAR_UNKNOWN)
1663 {
1664 if (argvars[2].v_type != VAR_DICT)
1665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001666 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667 return;
1668 }
1669 selfdict = argvars[2].vval.v_dict;
1670 }
1671
1672 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1673}
1674
1675#ifdef FEAT_FLOAT
1676/*
1677 * "ceil({float})" function
1678 */
1679 static void
1680f_ceil(typval_T *argvars, typval_T *rettv)
1681{
1682 float_T f = 0.0;
1683
1684 rettv->v_type = VAR_FLOAT;
1685 if (get_float_arg(argvars, &f) == OK)
1686 rettv->vval.v_float = ceil(f);
1687 else
1688 rettv->vval.v_float = 0.0;
1689}
1690#endif
1691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692/*
1693 * "changenr()" function
1694 */
1695 static void
1696f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
1697{
1698 rettv->vval.v_number = curbuf->b_u_seq_cur;
1699}
1700
1701/*
1702 * "char2nr(string)" function
1703 */
1704 static void
1705f_char2nr(typval_T *argvars, typval_T *rettv)
1706{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707 if (has_mbyte)
1708 {
1709 int utf8 = 0;
1710
1711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001712 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713
1714 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01001715 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001717 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 }
1719 else
Bram Moolenaar13505972019-01-24 15:04:48 +01001720 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001721}
1722
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001723 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01001724get_optional_window(typval_T *argvars, int idx)
1725{
1726 win_T *win = curwin;
1727
1728 if (argvars[idx].v_type != VAR_UNKNOWN)
1729 {
1730 win = find_win_by_nr_or_id(&argvars[idx]);
1731 if (win == NULL)
1732 {
1733 emsg(_(e_invalwindow));
1734 return NULL;
1735 }
1736 }
1737 return win;
1738}
1739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001740/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001741 * "col(string)" function
1742 */
1743 static void
1744f_col(typval_T *argvars, typval_T *rettv)
1745{
1746 colnr_T col = 0;
1747 pos_T *fp;
1748 int fnum = curbuf->b_fnum;
1749
1750 fp = var2fpos(&argvars[0], FALSE, &fnum);
1751 if (fp != NULL && fnum == curbuf->b_fnum)
1752 {
1753 if (fp->col == MAXCOL)
1754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001755 // '> can be MAXCOL, get the length of the line then
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001756 if (fp->lnum <= curbuf->b_ml.ml_line_count)
1757 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
1758 else
1759 col = MAXCOL;
1760 }
1761 else
1762 {
1763 col = fp->col + 1;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001764 // col(".") when the cursor is on the NUL at the end of the line
1765 // because of "coladd" can be seen as an extra column.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001766 if (virtual_active() && fp == &curwin->w_cursor)
1767 {
1768 char_u *p = ml_get_cursor();
1769
1770 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
1771 curwin->w_virtcol - curwin->w_cursor.coladd))
1772 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001773 int l;
1774
1775 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
1776 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001777 }
1778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001779 }
1780 }
1781 rettv->vval.v_number = col;
1782}
1783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001784/*
1785 * "confirm(message, buttons[, default [, type]])" function
1786 */
1787 static void
1788f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1789{
1790#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1791 char_u *message;
1792 char_u *buttons = NULL;
1793 char_u buf[NUMBUFLEN];
1794 char_u buf2[NUMBUFLEN];
1795 int def = 1;
1796 int type = VIM_GENERIC;
1797 char_u *typestr;
1798 int error = FALSE;
1799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001800 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001801 if (message == NULL)
1802 error = TRUE;
1803 if (argvars[1].v_type != VAR_UNKNOWN)
1804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001805 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001806 if (buttons == NULL)
1807 error = TRUE;
1808 if (argvars[2].v_type != VAR_UNKNOWN)
1809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001810 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001811 if (argvars[3].v_type != VAR_UNKNOWN)
1812 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001813 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001814 if (typestr == NULL)
1815 error = TRUE;
1816 else
1817 {
1818 switch (TOUPPER_ASC(*typestr))
1819 {
1820 case 'E': type = VIM_ERROR; break;
1821 case 'Q': type = VIM_QUESTION; break;
1822 case 'I': type = VIM_INFO; break;
1823 case 'W': type = VIM_WARNING; break;
1824 case 'G': type = VIM_GENERIC; break;
1825 }
1826 }
1827 }
1828 }
1829 }
1830
1831 if (buttons == NULL || *buttons == NUL)
1832 buttons = (char_u *)_("&Ok");
1833
1834 if (!error)
1835 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
1836 def, NULL, FALSE);
1837#endif
1838}
1839
1840/*
1841 * "copy()" function
1842 */
1843 static void
1844f_copy(typval_T *argvars, typval_T *rettv)
1845{
1846 item_copy(&argvars[0], rettv, FALSE, 0);
1847}
1848
1849#ifdef FEAT_FLOAT
1850/*
1851 * "cos()" function
1852 */
1853 static void
1854f_cos(typval_T *argvars, typval_T *rettv)
1855{
1856 float_T f = 0.0;
1857
1858 rettv->v_type = VAR_FLOAT;
1859 if (get_float_arg(argvars, &f) == OK)
1860 rettv->vval.v_float = cos(f);
1861 else
1862 rettv->vval.v_float = 0.0;
1863}
1864
1865/*
1866 * "cosh()" function
1867 */
1868 static void
1869f_cosh(typval_T *argvars, typval_T *rettv)
1870{
1871 float_T f = 0.0;
1872
1873 rettv->v_type = VAR_FLOAT;
1874 if (get_float_arg(argvars, &f) == OK)
1875 rettv->vval.v_float = cosh(f);
1876 else
1877 rettv->vval.v_float = 0.0;
1878}
1879#endif
1880
1881/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001882 * "cursor(lnum, col)" function, or
1883 * "cursor(list)"
1884 *
1885 * Moves the cursor to the specified line and column.
1886 * Returns 0 when the position could be set, -1 otherwise.
1887 */
1888 static void
1889f_cursor(typval_T *argvars, typval_T *rettv)
1890{
1891 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001893 int set_curswant = TRUE;
1894
1895 rettv->vval.v_number = -1;
1896 if (argvars[1].v_type == VAR_UNKNOWN)
1897 {
1898 pos_T pos;
1899 colnr_T curswant = -1;
1900
1901 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
1902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 return;
1905 }
1906 line = pos.lnum;
1907 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001908 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 if (curswant >= 0)
1910 {
1911 curwin->w_curswant = curswant - 1;
1912 set_curswant = FALSE;
1913 }
1914 }
1915 else
1916 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001917 line = tv_get_lnum(argvars);
1918 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001920 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001922 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001923 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924 if (line > 0)
1925 curwin->w_cursor.lnum = line;
1926 if (col > 0)
1927 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001932 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 if (has_mbyte)
1934 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935
1936 curwin->w_set_curswant = set_curswant;
1937 rettv->vval.v_number = 0;
1938}
1939
Bram Moolenaar4f974752019-02-17 17:44:42 +01001940#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001941/*
1942 * "debugbreak()" function
1943 */
1944 static void
1945f_debugbreak(typval_T *argvars, typval_T *rettv)
1946{
1947 int pid;
1948
1949 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001950 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001951 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001952 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02001953 else
1954 {
1955 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
1956
1957 if (hProcess != NULL)
1958 {
1959 DebugBreakProcess(hProcess);
1960 CloseHandle(hProcess);
1961 rettv->vval.v_number = OK;
1962 }
1963 }
1964}
1965#endif
1966
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001967/*
1968 * "deepcopy()" function
1969 */
1970 static void
1971f_deepcopy(typval_T *argvars, typval_T *rettv)
1972{
1973 int noref = 0;
1974 int copyID;
1975
1976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001977 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001978 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001979 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001980 else
1981 {
1982 copyID = get_copyID();
1983 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
1984 }
1985}
1986
1987/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 * "did_filetype()" function
1989 */
1990 static void
1991f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1992{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001994}
1995
1996/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01001997 * "echoraw({expr})" function
1998 */
1999 static void
2000f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2001{
2002 char_u *str = tv_get_string_chk(&argvars[0]);
2003
2004 if (str != NULL && *str != NUL)
2005 {
2006 out_str(str);
2007 out_flush();
2008 }
2009}
2010
2011/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002012 * "empty({expr})" function
2013 */
2014 static void
2015f_empty(typval_T *argvars, typval_T *rettv)
2016{
2017 int n = FALSE;
2018
2019 switch (argvars[0].v_type)
2020 {
2021 case VAR_STRING:
2022 case VAR_FUNC:
2023 n = argvars[0].vval.v_string == NULL
2024 || *argvars[0].vval.v_string == NUL;
2025 break;
2026 case VAR_PARTIAL:
2027 n = FALSE;
2028 break;
2029 case VAR_NUMBER:
2030 n = argvars[0].vval.v_number == 0;
2031 break;
2032 case VAR_FLOAT:
2033#ifdef FEAT_FLOAT
2034 n = argvars[0].vval.v_float == 0.0;
2035 break;
2036#endif
2037 case VAR_LIST:
2038 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002039 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002040 break;
2041 case VAR_DICT:
2042 n = argvars[0].vval.v_dict == NULL
2043 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2044 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002045 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002046 case VAR_SPECIAL:
2047 n = argvars[0].vval.v_number != VVAL_TRUE;
2048 break;
2049
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002050 case VAR_BLOB:
2051 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002052 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2053 break;
2054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 case VAR_JOB:
2056#ifdef FEAT_JOB_CHANNEL
2057 n = argvars[0].vval.v_job == NULL
2058 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2059 break;
2060#endif
2061 case VAR_CHANNEL:
2062#ifdef FEAT_JOB_CHANNEL
2063 n = argvars[0].vval.v_channel == NULL
2064 || !channel_is_open(argvars[0].vval.v_channel);
2065 break;
2066#endif
2067 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002068 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01002070 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 n = TRUE;
2072 break;
2073 }
2074
2075 rettv->vval.v_number = n;
2076}
2077
2078/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002079 * "environ()" function
2080 */
2081 static void
2082f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2083{
2084#if !defined(AMIGA)
2085 int i = 0;
2086 char_u *entry, *value;
2087# ifdef MSWIN
2088 extern wchar_t **_wenviron;
2089# else
2090 extern char **environ;
2091# endif
2092
2093 if (rettv_dict_alloc(rettv) != OK)
2094 return;
2095
2096# ifdef MSWIN
2097 if (*_wenviron == NULL)
2098 return;
2099# else
2100 if (*environ == NULL)
2101 return;
2102# endif
2103
2104 for (i = 0; ; ++i)
2105 {
2106# ifdef MSWIN
2107 short_u *p;
2108
2109 if ((p = (short_u *)_wenviron[i]) == NULL)
2110 return;
2111 entry = utf16_to_enc(p, NULL);
2112# else
2113 if ((entry = (char_u *)environ[i]) == NULL)
2114 return;
2115 entry = vim_strsave(entry);
2116# endif
2117 if (entry == NULL) // out of memory
2118 return;
2119 if ((value = vim_strchr(entry, '=')) == NULL)
2120 {
2121 vim_free(entry);
2122 continue;
2123 }
2124 *value++ = NUL;
2125 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2126 vim_free(entry);
2127 }
2128#endif
2129}
2130
2131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132 * "escape({string}, {chars})" function
2133 */
2134 static void
2135f_escape(typval_T *argvars, typval_T *rettv)
2136{
2137 char_u buf[NUMBUFLEN];
2138
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002139 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2140 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141 rettv->v_type = VAR_STRING;
2142}
2143
2144/*
2145 * "eval()" function
2146 */
2147 static void
2148f_eval(typval_T *argvars, typval_T *rettv)
2149{
2150 char_u *s, *p;
2151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002152 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 if (s != NULL)
2154 s = skipwhite(s);
2155
2156 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002158 {
2159 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 need_clr_eos = FALSE;
2162 rettv->v_type = VAR_NUMBER;
2163 rettv->vval.v_number = 0;
2164 }
2165 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002166 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167}
2168
2169/*
2170 * "eventhandler()" function
2171 */
2172 static void
2173f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2174{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002175 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176}
2177
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178static garray_T redir_execute_ga;
2179
2180/*
2181 * Append "value[value_len]" to the execute() output.
2182 */
2183 void
2184execute_redir_str(char_u *value, int value_len)
2185{
2186 int len;
2187
2188 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002189 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002190 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002191 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 if (ga_grow(&redir_execute_ga, len) == OK)
2193 {
2194 mch_memmove((char *)redir_execute_ga.ga_data
2195 + redir_execute_ga.ga_len, value, len);
2196 redir_execute_ga.ga_len += len;
2197 }
2198}
2199
2200/*
2201 * Get next line from a list.
2202 * Called by do_cmdline() to get the next line.
2203 * Returns allocated string, or NULL for end of function.
2204 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002205 static char_u *
2206get_list_line(
2207 int c UNUSED,
2208 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002209 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002210 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211{
2212 listitem_T **p = (listitem_T **)cookie;
2213 listitem_T *item = *p;
2214 char_u buf[NUMBUFLEN];
2215 char_u *s;
2216
2217 if (item == NULL)
2218 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002219 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002220 *p = item->li_next;
2221 return s == NULL ? NULL : vim_strsave(s);
2222}
2223
2224/*
2225 * "execute()" function
2226 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002227 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002228execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229{
2230 char_u *cmd = NULL;
2231 list_T *list = NULL;
2232 int save_msg_silent = msg_silent;
2233 int save_emsg_silent = emsg_silent;
2234 int save_emsg_noredir = emsg_noredir;
2235 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002236 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002238 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002239 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240
2241 rettv->vval.v_string = NULL;
2242 rettv->v_type = VAR_STRING;
2243
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002244 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002245 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002246 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002247 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002248 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002249 return;
2250 ++list->lv_refcount;
2251 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002252 else if (argvars[arg_off].v_type == VAR_JOB
2253 || argvars[arg_off].v_type == VAR_CHANNEL)
2254 {
2255 emsg(_(e_inval_string));
2256 return;
2257 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 else
2259 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002260 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 if (cmd == NULL)
2262 return;
2263 }
2264
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002265 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002266 {
2267 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002268 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002269
2270 if (s == NULL)
2271 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002272 if (*s == NUL)
2273 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002274 if (STRNCMP(s, "silent", 6) == 0)
2275 ++msg_silent;
2276 if (STRCMP(s, "silent!") == 0)
2277 {
2278 emsg_silent = TRUE;
2279 emsg_noredir = TRUE;
2280 }
2281 }
2282 else
2283 ++msg_silent;
2284
2285 if (redir_execute)
2286 save_ga = redir_execute_ga;
2287 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2288 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002289 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002290 if (!echo_output)
2291 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002292
2293 if (cmd != NULL)
2294 do_cmdline_cmd(cmd);
2295 else
2296 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002297 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002299 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002300 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301 do_cmdline(NULL, get_list_line, (void *)&item,
2302 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2303 --list->lv_refcount;
2304 }
2305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002306 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01002307 if (ga_grow(&redir_execute_ga, 1) == OK)
2308 {
2309 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2310 rettv->vval.v_string = redir_execute_ga.ga_data;
2311 }
2312 else
2313 {
2314 ga_clear(&redir_execute_ga);
2315 rettv->vval.v_string = NULL;
2316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002317 msg_silent = save_msg_silent;
2318 emsg_silent = save_emsg_silent;
2319 emsg_noredir = save_emsg_noredir;
2320
2321 redir_execute = save_redir_execute;
2322 if (redir_execute)
2323 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002324 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002325
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002326 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002327 if (echo_output)
2328 // When not working silently: put it in column zero. A following
2329 // "echon" will overwrite the message, unavoidably.
2330 msg_col = 0;
2331 else
2332 // When working silently: Put it back where it was, since nothing
2333 // should have been written.
2334 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335}
2336
2337/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002338 * "execute()" function
2339 */
2340 static void
2341f_execute(typval_T *argvars, typval_T *rettv)
2342{
2343 execute_common(argvars, rettv, 0);
2344}
2345
2346/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002347 * "exists()" function
2348 */
2349 static void
2350f_exists(typval_T *argvars, typval_T *rettv)
2351{
2352 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002355 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002356 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002358 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 if (mch_getenv(p + 1) != NULL)
2360 n = TRUE;
2361 else
2362 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002363 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364 p = expand_env_save(p);
2365 if (p != NULL && *p != '$')
2366 n = TRUE;
2367 vim_free(p);
2368 }
2369 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002370 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002372 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002374 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002376 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002378 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01002380 else if (*p == '?') // internal function only
2381 {
2382 n = has_internal_func_name(p + 1);
2383 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384 else if (*p == ':')
2385 {
2386 n = cmd_exists(p + 1);
2387 }
2388 else if (*p == '#')
2389 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 if (p[1] == '#')
2391 n = autocmd_supported(p + 2);
2392 else
2393 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002395 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01002397 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 }
2399
2400 rettv->vval.v_number = n;
2401}
2402
2403#ifdef FEAT_FLOAT
2404/*
2405 * "exp()" function
2406 */
2407 static void
2408f_exp(typval_T *argvars, typval_T *rettv)
2409{
2410 float_T f = 0.0;
2411
2412 rettv->v_type = VAR_FLOAT;
2413 if (get_float_arg(argvars, &f) == OK)
2414 rettv->vval.v_float = exp(f);
2415 else
2416 rettv->vval.v_float = 0.0;
2417}
2418#endif
2419
2420/*
2421 * "expand()" function
2422 */
2423 static void
2424f_expand(typval_T *argvars, typval_T *rettv)
2425{
2426 char_u *s;
2427 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002428 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002429 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2430 expand_T xpc;
2431 int error = FALSE;
2432 char_u *result;
2433
2434 rettv->v_type = VAR_STRING;
2435 if (argvars[1].v_type != VAR_UNKNOWN
2436 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002437 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002438 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02002439 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002441 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002442 if (*s == '%' || *s == '#' || *s == '<')
2443 {
2444 ++emsg_off;
2445 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
2446 --emsg_off;
2447 if (rettv->v_type == VAR_LIST)
2448 {
2449 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
2450 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02002451 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 }
2453 else
2454 rettv->vval.v_string = result;
2455 }
2456 else
2457 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002458 // When the optional second argument is non-zero, don't remove matches
2459 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002461 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 options |= WILD_KEEP_ALL;
2463 if (!error)
2464 {
2465 ExpandInit(&xpc);
2466 xpc.xp_context = EXPAND_FILES;
2467 if (p_wic)
2468 options += WILD_ICASE;
2469 if (rettv->v_type == VAR_STRING)
2470 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
2471 options, WILD_ALL);
2472 else if (rettv_list_alloc(rettv) != FAIL)
2473 {
2474 int i;
2475
2476 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
2477 for (i = 0; i < xpc.xp_numfiles; i++)
2478 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2479 ExpandCleanup(&xpc);
2480 }
2481 }
2482 else
2483 rettv->vval.v_string = NULL;
2484 }
2485}
2486
2487/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02002488 * "expandcmd()" function
2489 * Expand all the special characters in a command string.
2490 */
2491 static void
2492f_expandcmd(typval_T *argvars, typval_T *rettv)
2493{
2494 exarg_T eap;
2495 char_u *cmdstr;
2496 char *errormsg = NULL;
2497
2498 rettv->v_type = VAR_STRING;
2499 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
2500
2501 memset(&eap, 0, sizeof(eap));
2502 eap.cmd = cmdstr;
2503 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002504 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02002505 eap.usefilter = FALSE;
2506 eap.nextcmd = NULL;
2507 eap.cmdidx = CMD_USER;
2508
2509 expand_filename(&eap, &cmdstr, &errormsg);
2510 if (errormsg != NULL && *errormsg != NUL)
2511 emsg(errormsg);
2512
2513 rettv->vval.v_string = cmdstr;
2514}
2515
2516/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517 * "feedkeys()" function
2518 */
2519 static void
2520f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
2521{
2522 int remap = TRUE;
2523 int insert = FALSE;
2524 char_u *keys, *flags;
2525 char_u nbuf[NUMBUFLEN];
2526 int typed = FALSE;
2527 int execute = FALSE;
2528 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002529 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 char_u *keys_esc;
2531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002532 // This is not allowed in the sandbox. If the commands would still be
2533 // executed in the sandbox it would be OK, but it probably happens later,
2534 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 if (check_secure())
2536 return;
2537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539
2540 if (argvars[1].v_type != VAR_UNKNOWN)
2541 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002542 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 for ( ; *flags != NUL; ++flags)
2544 {
2545 switch (*flags)
2546 {
2547 case 'n': remap = FALSE; break;
2548 case 'm': remap = TRUE; break;
2549 case 't': typed = TRUE; break;
2550 case 'i': insert = TRUE; break;
2551 case 'x': execute = TRUE; break;
2552 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002553 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554 }
2555 }
2556 }
2557
2558 if (*keys != NUL || execute)
2559 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002560 // Need to escape K_SPECIAL and CSI before putting the string in the
2561 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 keys_esc = vim_strsave_escape_csi(keys);
2563 if (keys_esc != NULL)
2564 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002565 if (lowlevel)
2566 {
2567#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01002568 int idx;
2569 int len = (int)STRLEN(keys);
2570
2571 for (idx = 0; idx < len; ++idx)
2572 {
2573 // if a CTRL-C was typed, set got_int, similar to what
2574 // happens in fill_input_buf()
2575 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
2576 got_int = TRUE;
2577 add_to_input_buf(keys + idx, 1);
2578 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002579#else
2580 emsg(_("E980: lowlevel input not supported"));
2581#endif
2582 }
2583 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002584 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002585 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002587 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002588#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002589 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02002590#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002591 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01002592 typebuf_was_filled = TRUE;
2593 }
2594 vim_free(keys_esc);
2595
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002596 if (execute)
2597 {
2598 int save_msg_scroll = msg_scroll;
2599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002600 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002601 msg_scroll = FALSE;
2602
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002603 if (!dangerous)
2604 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02002605 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002606 if (!dangerous)
2607 --ex_normal_busy;
2608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609 msg_scroll |= save_msg_scroll;
2610 }
2611 }
2612 }
2613}
2614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002615#ifdef FEAT_FLOAT
2616/*
2617 * "float2nr({float})" function
2618 */
2619 static void
2620f_float2nr(typval_T *argvars, typval_T *rettv)
2621{
2622 float_T f = 0.0;
2623
2624 if (get_float_arg(argvars, &f) == OK)
2625 {
Bram Moolenaar37184272020-05-23 19:30:05 +02002626 if (f <= (float_T)-VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002627 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar37184272020-05-23 19:30:05 +02002628 else if (f >= (float_T)VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01002629 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 else
2631 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632 }
2633}
2634
2635/*
2636 * "floor({float})" function
2637 */
2638 static void
2639f_floor(typval_T *argvars, typval_T *rettv)
2640{
2641 float_T f = 0.0;
2642
2643 rettv->v_type = VAR_FLOAT;
2644 if (get_float_arg(argvars, &f) == OK)
2645 rettv->vval.v_float = floor(f);
2646 else
2647 rettv->vval.v_float = 0.0;
2648}
2649
2650/*
2651 * "fmod()" function
2652 */
2653 static void
2654f_fmod(typval_T *argvars, typval_T *rettv)
2655{
2656 float_T fx = 0.0, fy = 0.0;
2657
2658 rettv->v_type = VAR_FLOAT;
2659 if (get_float_arg(argvars, &fx) == OK
2660 && get_float_arg(&argvars[1], &fy) == OK)
2661 rettv->vval.v_float = fmod(fx, fy);
2662 else
2663 rettv->vval.v_float = 0.0;
2664}
2665#endif
2666
2667/*
2668 * "fnameescape({string})" function
2669 */
2670 static void
2671f_fnameescape(typval_T *argvars, typval_T *rettv)
2672{
2673 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002674 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 rettv->v_type = VAR_STRING;
2676}
2677
2678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 * "foreground()" function
2680 */
2681 static void
2682f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2683{
2684#ifdef FEAT_GUI
2685 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002688 return;
2689 }
2690#endif
2691#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002693#endif
2694}
2695
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002697common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698{
2699 char_u *s;
2700 char_u *name;
2701 int use_string = FALSE;
2702 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002703 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002704 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705
2706 if (argvars[0].v_type == VAR_FUNC)
2707 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002708 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 s = argvars[0].vval.v_string;
2710 }
2711 else if (argvars[0].v_type == VAR_PARTIAL
2712 && argvars[0].vval.v_partial != NULL)
2713 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002714 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002716 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717 }
2718 else
2719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002720 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002721 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002722 use_string = TRUE;
2723 }
2724
Bram Moolenaar843b8842016-08-21 14:36:15 +02002725 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002726 {
2727 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002728 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002729 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
2730 if (*name != NUL)
2731 s = NULL;
2732 }
2733
Bram Moolenaar843b8842016-08-21 14:36:15 +02002734 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
2735 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002736 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002737 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002738 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002739 ? find_func(trans_name, is_global, NULL) == NULL
2740 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002741 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 else
2743 {
2744 int dict_idx = 0;
2745 int arg_idx = 0;
2746 list_T *list = NULL;
2747
2748 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
2749 {
2750 char sid_buf[25];
2751 int off = *s == 's' ? 2 : 5;
2752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002753 // Expand s: and <SID> into <SNR>nr_, so that the function can
2754 // also be called from another script. Using trans_function_name()
2755 // would also work, but some plugins depend on the name being
2756 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002757 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02002758 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759 if (name != NULL)
2760 {
2761 STRCPY(name, sid_buf);
2762 STRCAT(name, s + off);
2763 }
2764 }
2765 else
2766 name = vim_strsave(s);
2767
2768 if (argvars[1].v_type != VAR_UNKNOWN)
2769 {
2770 if (argvars[2].v_type != VAR_UNKNOWN)
2771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002772 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 arg_idx = 1;
2774 dict_idx = 2;
2775 }
2776 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002777 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 dict_idx = 1;
2779 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002780 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 arg_idx = 1;
2782 if (dict_idx > 0)
2783 {
2784 if (argvars[dict_idx].v_type != VAR_DICT)
2785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002786 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002788 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002789 }
2790 if (argvars[dict_idx].vval.v_dict == NULL)
2791 dict_idx = 0;
2792 }
2793 if (arg_idx > 0)
2794 {
2795 if (argvars[arg_idx].v_type != VAR_LIST)
2796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002797 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002799 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 }
2801 list = argvars[arg_idx].vval.v_list;
2802 if (list == NULL || list->lv_len == 0)
2803 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002804 else if (list->lv_len > MAX_FUNC_ARGS)
2805 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01002806 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002807 vim_free(name);
2808 goto theend;
2809 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 }
2811 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002812 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002813 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002814 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002816 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002817 if (pt == NULL)
2818 vim_free(name);
2819 else
2820 {
2821 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
2822 {
2823 listitem_T *li;
2824 int i = 0;
2825 int arg_len = 0;
2826 int lv_len = 0;
2827
2828 if (arg_pt != NULL)
2829 arg_len = arg_pt->pt_argc;
2830 if (list != NULL)
2831 lv_len = list->lv_len;
2832 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002833 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002834 if (pt->pt_argv == NULL)
2835 {
2836 vim_free(pt);
2837 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002838 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002839 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002840 for (i = 0; i < arg_len; i++)
2841 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
2842 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002843 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002844 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002845 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002846 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002847 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002848 }
2849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002850 // For "function(dict.func, [], dict)" and "func" is a partial
2851 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 if (dict_idx > 0)
2853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002854 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 pt->pt_dict = argvars[dict_idx].vval.v_dict;
2856 ++pt->pt_dict->dv_refcount;
2857 }
2858 else if (arg_pt != NULL)
2859 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002860 // If the dict was bound automatically the result is also
2861 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002862 pt->pt_dict = arg_pt->pt_dict;
2863 pt->pt_auto = arg_pt->pt_auto;
2864 if (pt->pt_dict != NULL)
2865 ++pt->pt_dict->dv_refcount;
2866 }
2867
2868 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002869 if (arg_pt != NULL && arg_pt->pt_func != NULL)
2870 {
2871 pt->pt_func = arg_pt->pt_func;
2872 func_ptr_ref(pt->pt_func);
2873 vim_free(name);
2874 }
2875 else if (is_funcref)
2876 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002877 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002878 func_ptr_ref(pt->pt_func);
2879 vim_free(name);
2880 }
2881 else
2882 {
2883 pt->pt_name = name;
2884 func_ref(name);
2885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002886 }
2887 rettv->v_type = VAR_PARTIAL;
2888 rettv->vval.v_partial = pt;
2889 }
2890 else
2891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002892 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893 rettv->v_type = VAR_FUNC;
2894 rettv->vval.v_string = name;
2895 func_ref(name);
2896 }
2897 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002898theend:
2899 vim_free(trans_name);
2900}
2901
2902/*
2903 * "funcref()" function
2904 */
2905 static void
2906f_funcref(typval_T *argvars, typval_T *rettv)
2907{
2908 common_function(argvars, rettv, TRUE);
2909}
2910
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002911 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002912ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002913{
2914 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
2915 return &t_func_any;
Bram Moolenaard77a8522020-04-03 21:59:57 +02002916 return &t_func_void;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002917}
2918
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002919/*
2920 * "function()" function
2921 */
2922 static void
2923f_function(typval_T *argvars, typval_T *rettv)
2924{
2925 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926}
2927
2928/*
2929 * "garbagecollect()" function
2930 */
2931 static void
2932f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
2933{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002934 // This is postponed until we are back at the toplevel, because we may be
2935 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 want_garbage_collect = TRUE;
2937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002938 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 garbage_collect_at_exit = TRUE;
2940}
2941
2942/*
2943 * "get()" function
2944 */
2945 static void
2946f_get(typval_T *argvars, typval_T *rettv)
2947{
2948 listitem_T *li;
2949 list_T *l;
2950 dictitem_T *di;
2951 dict_T *d;
2952 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02002953 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002955 if (argvars[0].v_type == VAR_BLOB)
2956 {
2957 int error = FALSE;
2958 int idx = tv_get_number_chk(&argvars[1], &error);
2959
2960 if (!error)
2961 {
2962 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002963 if (idx < 0)
2964 idx = blob_len(argvars[0].vval.v_blob) + idx;
2965 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
2966 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002967 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002968 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002969 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01002970 tv = rettv;
2971 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002972 }
2973 }
2974 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975 {
2976 if ((l = argvars[0].vval.v_list) != NULL)
2977 {
2978 int error = FALSE;
2979
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 if (!error && li != NULL)
2982 tv = &li->li_tv;
2983 }
2984 }
2985 else if (argvars[0].v_type == VAR_DICT)
2986 {
2987 if ((d = argvars[0].vval.v_dict) != NULL)
2988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002989 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990 if (di != NULL)
2991 tv = &di->di_tv;
2992 }
2993 }
2994 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
2995 {
2996 partial_T *pt;
2997 partial_T fref_pt;
2998
2999 if (argvars[0].v_type == VAR_PARTIAL)
3000 pt = argvars[0].vval.v_partial;
3001 else
3002 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003003 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 fref_pt.pt_name = argvars[0].vval.v_string;
3005 pt = &fref_pt;
3006 }
3007
3008 if (pt != NULL)
3009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003010 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003011 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012
3013 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3014 {
3015 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003016 n = partial_name(pt);
3017 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 rettv->vval.v_string = NULL;
3019 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003020 {
3021 rettv->vval.v_string = vim_strsave(n);
3022 if (rettv->v_type == VAR_FUNC)
3023 func_ref(rettv->vval.v_string);
3024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 }
3026 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003027 {
3028 what_is_dict = TRUE;
3029 if (pt->pt_dict != NULL)
3030 rettv_dict_set(rettv, pt->pt_dict);
3031 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032 else if (STRCMP(what, "args") == 0)
3033 {
3034 rettv->v_type = VAR_LIST;
3035 if (rettv_list_alloc(rettv) == OK)
3036 {
3037 int i;
3038
3039 for (i = 0; i < pt->pt_argc; ++i)
3040 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3041 }
3042 }
3043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003044 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003045
3046 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3047 // third argument
3048 if (!what_is_dict)
3049 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050 }
3051 }
3052 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003053 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054
3055 if (tv == NULL)
3056 {
3057 if (argvars[2].v_type != VAR_UNKNOWN)
3058 copy_tv(&argvars[2], rettv);
3059 }
3060 else
3061 copy_tv(tv, rettv);
3062}
3063
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003064/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003065 * "getchangelist()" function
3066 */
3067 static void
3068f_getchangelist(typval_T *argvars, typval_T *rettv)
3069{
3070#ifdef FEAT_JUMPLIST
3071 buf_T *buf;
3072 int i;
3073 list_T *l;
3074 dict_T *d;
3075#endif
3076
3077 if (rettv_list_alloc(rettv) != OK)
3078 return;
3079
3080#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003081 if (argvars[0].v_type == VAR_UNKNOWN)
3082 buf = curbuf;
3083 else
3084 {
3085 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3086 ++emsg_off;
3087 buf = tv_get_buf(&argvars[0], FALSE);
3088 --emsg_off;
3089 }
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003090 if (buf == NULL)
3091 return;
3092
3093 l = list_alloc();
3094 if (l == NULL)
3095 return;
3096
3097 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3098 return;
3099 /*
3100 * The current window change list index tracks only the position in the
3101 * current buffer change list. For other buffers, use the change list
3102 * length as the current index.
3103 */
3104 list_append_number(rettv->vval.v_list,
3105 (varnumber_T)((buf == curwin->w_buffer)
3106 ? curwin->w_changelistidx : buf->b_changelistlen));
3107
3108 for (i = 0; i < buf->b_changelistlen; ++i)
3109 {
3110 if (buf->b_changelist[i].lnum == 0)
3111 continue;
3112 if ((d = dict_alloc()) == NULL)
3113 return;
3114 if (list_append_dict(l, d) == FAIL)
3115 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003116 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3117 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003118 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003119 }
3120#endif
3121}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122
3123/*
3124 * "getcharsearch()" function
3125 */
3126 static void
3127f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3128{
3129 if (rettv_dict_alloc(rettv) != FAIL)
3130 {
3131 dict_T *dict = rettv->vval.v_dict;
3132
Bram Moolenaare0be1672018-07-08 16:50:37 +02003133 dict_add_string(dict, "char", last_csearch());
3134 dict_add_number(dict, "forward", last_csearch_forward());
3135 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 }
3137}
3138
3139/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003140 * "getenv()" function
3141 */
3142 static void
3143f_getenv(typval_T *argvars, typval_T *rettv)
3144{
3145 int mustfree = FALSE;
3146 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3147
3148 if (p == NULL)
3149 {
3150 rettv->v_type = VAR_SPECIAL;
3151 rettv->vval.v_number = VVAL_NULL;
3152 return;
3153 }
3154 if (!mustfree)
3155 p = vim_strsave(p);
3156 rettv->vval.v_string = p;
3157 rettv->v_type = VAR_STRING;
3158}
3159
3160/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003161 * "getfontname()" function
3162 */
3163 static void
3164f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3165{
3166 rettv->v_type = VAR_STRING;
3167 rettv->vval.v_string = NULL;
3168#ifdef FEAT_GUI
3169 if (gui.in_use)
3170 {
3171 GuiFont font;
3172 char_u *name = NULL;
3173
3174 if (argvars[0].v_type == VAR_UNKNOWN)
3175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003176 // Get the "Normal" font. Either the name saved by
3177 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 font = gui.norm_font;
3179 name = hl_get_font_name();
3180 }
3181 else
3182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003183 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003184 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 return;
3186 font = gui_mch_get_font(name, FALSE);
3187 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003188 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 }
3190 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3191 if (argvars[0].v_type != VAR_UNKNOWN)
3192 gui_mch_free_font(font);
3193 }
3194#endif
3195}
3196
3197/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003198 * "getjumplist()" function
3199 */
3200 static void
3201f_getjumplist(typval_T *argvars, typval_T *rettv)
3202{
3203#ifdef FEAT_JUMPLIST
3204 win_T *wp;
3205 int i;
3206 list_T *l;
3207 dict_T *d;
3208#endif
3209
3210 if (rettv_list_alloc(rettv) != OK)
3211 return;
3212
3213#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003214 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003215 if (wp == NULL)
3216 return;
3217
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003218 cleanup_jumplist(wp, TRUE);
3219
Bram Moolenaar4f505882018-02-10 21:06:32 +01003220 l = list_alloc();
3221 if (l == NULL)
3222 return;
3223
3224 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3225 return;
3226 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3227
3228 for (i = 0; i < wp->w_jumplistlen; ++i)
3229 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003230 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3231 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003232 if ((d = dict_alloc()) == NULL)
3233 return;
3234 if (list_append_dict(l, d) == FAIL)
3235 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003236 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3237 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003238 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003239 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003240 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02003241 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003242 }
3243#endif
3244}
3245
3246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 * "getpid()" function
3248 */
3249 static void
3250f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
3251{
3252 rettv->vval.v_number = mch_get_pid();
3253}
3254
3255 static void
3256getpos_both(
3257 typval_T *argvars,
3258 typval_T *rettv,
3259 int getcurpos)
3260{
3261 pos_T *fp;
3262 list_T *l;
3263 int fnum = -1;
3264
3265 if (rettv_list_alloc(rettv) == OK)
3266 {
3267 l = rettv->vval.v_list;
3268 if (getcurpos)
3269 fp = &curwin->w_cursor;
3270 else
3271 fp = var2fpos(&argvars[0], TRUE, &fnum);
3272 if (fnum != -1)
3273 list_append_number(l, (varnumber_T)fnum);
3274 else
3275 list_append_number(l, (varnumber_T)0);
3276 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3277 : (varnumber_T)0);
3278 list_append_number(l, (fp != NULL)
3279 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3280 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003281 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 (varnumber_T)0);
3283 if (getcurpos)
3284 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01003285 int save_set_curswant = curwin->w_set_curswant;
3286 colnr_T save_curswant = curwin->w_curswant;
3287 colnr_T save_virtcol = curwin->w_virtcol;
3288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 update_curswant();
3290 list_append_number(l, curwin->w_curswant == MAXCOL ?
3291 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01003292
3293 // Do not change "curswant", as it is unexpected that a get
3294 // function has a side effect.
3295 if (save_set_curswant)
3296 {
3297 curwin->w_set_curswant = save_set_curswant;
3298 curwin->w_curswant = save_curswant;
3299 curwin->w_virtcol = save_virtcol;
3300 curwin->w_valid &= ~VALID_VIRTCOL;
3301 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 }
3303 }
3304 else
3305 rettv->vval.v_number = FALSE;
3306}
3307
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308/*
3309 * "getcurpos()" function
3310 */
3311 static void
3312f_getcurpos(typval_T *argvars, typval_T *rettv)
3313{
3314 getpos_both(argvars, rettv, TRUE);
3315}
3316
3317/*
3318 * "getpos(string)" function
3319 */
3320 static void
3321f_getpos(typval_T *argvars, typval_T *rettv)
3322{
3323 getpos_both(argvars, rettv, FALSE);
3324}
3325
3326/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003327 * "getreg()" function
3328 */
3329 static void
3330f_getreg(typval_T *argvars, typval_T *rettv)
3331{
3332 char_u *strregname;
3333 int regname;
3334 int arg2 = FALSE;
3335 int return_list = FALSE;
3336 int error = FALSE;
3337
3338 if (argvars[0].v_type != VAR_UNKNOWN)
3339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003340 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003341 error = strregname == NULL;
3342 if (argvars[1].v_type != VAR_UNKNOWN)
3343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003344 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003347 }
3348 }
3349 else
3350 strregname = get_vim_var_str(VV_REG);
3351
3352 if (error)
3353 return;
3354
3355 regname = (strregname == NULL ? '"' : *strregname);
3356 if (regname == 0)
3357 regname = '"';
3358
3359 if (return_list)
3360 {
3361 rettv->v_type = VAR_LIST;
3362 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
3363 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
3364 if (rettv->vval.v_list == NULL)
3365 (void)rettv_list_alloc(rettv);
3366 else
3367 ++rettv->vval.v_list->lv_refcount;
3368 }
3369 else
3370 {
3371 rettv->v_type = VAR_STRING;
3372 rettv->vval.v_string = get_reg_contents(regname,
3373 arg2 ? GREG_EXPR_SRC : 0);
3374 }
3375}
3376
3377/*
3378 * "getregtype()" function
3379 */
3380 static void
3381f_getregtype(typval_T *argvars, typval_T *rettv)
3382{
3383 char_u *strregname;
3384 int regname;
3385 char_u buf[NUMBUFLEN + 2];
3386 long reglen = 0;
3387
3388 if (argvars[0].v_type != VAR_UNKNOWN)
3389 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003390 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003392 {
3393 rettv->v_type = VAR_STRING;
3394 rettv->vval.v_string = NULL;
3395 return;
3396 }
3397 }
3398 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003399 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 strregname = get_vim_var_str(VV_REG);
3401
3402 regname = (strregname == NULL ? '"' : *strregname);
3403 if (regname == 0)
3404 regname = '"';
3405
3406 buf[0] = NUL;
3407 buf[1] = NUL;
3408 switch (get_reg_type(regname, &reglen))
3409 {
3410 case MLINE: buf[0] = 'V'; break;
3411 case MCHAR: buf[0] = 'v'; break;
3412 case MBLOCK:
3413 buf[0] = Ctrl_V;
3414 sprintf((char *)buf + 1, "%ld", reglen + 1);
3415 break;
3416 }
3417 rettv->v_type = VAR_STRING;
3418 rettv->vval.v_string = vim_strsave(buf);
3419}
3420
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003421/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01003422 * "gettagstack()" function
3423 */
3424 static void
3425f_gettagstack(typval_T *argvars, typval_T *rettv)
3426{
3427 win_T *wp = curwin; // default is current window
3428
3429 if (rettv_dict_alloc(rettv) != OK)
3430 return;
3431
3432 if (argvars[0].v_type != VAR_UNKNOWN)
3433 {
3434 wp = find_win_by_nr_or_id(&argvars[0]);
3435 if (wp == NULL)
3436 return;
3437 }
3438
3439 get_tagstack(wp, rettv->vval.v_dict);
3440}
3441
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02003442/*
3443 * "gettext()" function
3444 */
3445 static void
3446f_gettext(typval_T *argvars, typval_T *rettv)
3447{
3448 if (argvars[0].v_type != VAR_STRING
3449 || argvars[0].vval.v_string == NULL
3450 || *argvars[0].vval.v_string == NUL)
3451 {
3452 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
3453 }
3454 else
3455 {
3456 rettv->v_type = VAR_STRING;
3457 rettv->vval.v_string = vim_strsave(
3458 (char_u *)_(argvars[0].vval.v_string));
3459 }
3460}
3461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463#include "version.h"
3464
3465/*
3466 * "has()" function
3467 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003468 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003469f_has(typval_T *argvars, typval_T *rettv)
3470{
3471 int i;
3472 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01003473 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01003475 typedef struct {
3476 char *name;
3477 short present;
3478 } has_item_T;
3479 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003480 {
Bram Moolenaar79296512020-03-22 16:17:14 +01003481 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01003483 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003484#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003485 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01003486#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003487 },
3488 {"arp",
3489#if defined(AMIGA) && defined(FEAT_ARP)
3490 1
3491#else
3492 0
3493#endif
3494 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003495 {"haiku",
3496#ifdef __HAIKU__
3497 1
3498#else
3499 0
3500#endif
3501 },
3502 {"bsd",
3503#if defined(BSD) && !defined(MACOS_X)
3504 1
3505#else
3506 0
3507#endif
3508 },
3509 {"hpux",
3510#ifdef hpux
3511 1
3512#else
3513 0
3514#endif
3515 },
3516 {"linux",
3517#ifdef __linux__
3518 1
3519#else
3520 0
3521#endif
3522 },
3523 {"mac", // Mac OS X (and, once, Mac OS Classic)
3524#ifdef MACOS_X
3525 1
3526#else
3527 0
3528#endif
3529 },
3530 {"osx", // Mac OS X
3531#ifdef MACOS_X
3532 1
3533#else
3534 0
3535#endif
3536 },
3537 {"macunix", // Mac OS X, with the darwin feature
3538#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3539 1
3540#else
3541 0
3542#endif
3543 },
3544 {"osxdarwin", // synonym for macunix
3545#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
3546 1
3547#else
3548 0
3549#endif
3550 },
3551 {"qnx",
3552#ifdef __QNX__
3553 1
3554#else
3555 0
3556#endif
3557 },
3558 {"sun",
3559#ifdef SUN_SYSTEM
3560 1
3561#else
3562 0
3563#endif
3564 },
3565 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01003567 1
3568#else
3569 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003571 },
3572 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01003574 1
3575#else
3576 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003578 },
3579 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01003580#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01003581 1
3582#else
3583 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003585 },
3586 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01003587#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01003588 1
3589#else
3590 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003591#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003592 },
3593 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01003594#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01003595 1
3596#else
3597 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003598#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003599 },
3600 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003602 1
3603#else
3604 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003606 },
3607 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003609 1
3610#else
3611 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003613 },
3614 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01003616 1
3617#else
3618 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003620 },
3621 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01003623 1
3624#else
3625 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003627 },
3628 {"autocmd", 1},
3629 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003630#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01003631 1
3632#else
3633 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02003634#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003635 },
3636 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003637#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01003638 1
3639#else
3640 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003641#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003642 },
3643 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003644#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01003645 1
3646#else
3647 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003649 },
3650 {"balloon_multiline",
3651#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
3652 // MS-Windows requires runtime check, see below
3653 1
3654#else
3655 0
3656#endif
3657 },
3658 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003659#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01003660 1
3661#else
3662 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003663#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003664 },
3665 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01003667 1
3668#else
3669 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003670#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003671 },
3672 {"all_builtin_terms",
3673#if defined(ALL_BUILTIN_TCAPS)
3674 1
3675#else
3676 0
3677#endif
3678 },
3679 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01003681 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01003683 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684#else
Bram Moolenaar79296512020-03-22 16:17:14 +01003685 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01003687 },
3688 {"byte_offset",
3689#ifdef FEAT_BYTEOFF
3690 1
3691#else
3692 0
3693#endif
3694 },
3695 {"channel",
3696#ifdef FEAT_JOB_CHANNEL
3697 1
3698#else
3699 0
3700#endif
3701 },
3702 {"cindent",
3703#ifdef FEAT_CINDENT
3704 1
3705#else
3706 0
3707#endif
3708 },
3709 {"clientserver",
3710#ifdef FEAT_CLIENTSERVER
3711 1
3712#else
3713 0
3714#endif
3715 },
3716 {"clipboard",
3717#ifdef FEAT_CLIPBOARD
3718 1
3719#else
3720 0
3721#endif
3722 },
3723 {"cmdline_compl", 1},
3724 {"cmdline_hist", 1},
3725 {"comments", 1},
3726 {"conceal",
3727#ifdef FEAT_CONCEAL
3728 1
3729#else
3730 0
3731#endif
3732 },
3733 {"cryptv",
3734#ifdef FEAT_CRYPT
3735 1
3736#else
3737 0
3738#endif
3739 },
3740 {"crypt-blowfish",
3741#ifdef FEAT_CRYPT
3742 1
3743#else
3744 0
3745#endif
3746 },
3747 {"crypt-blowfish2",
3748#ifdef FEAT_CRYPT
3749 1
3750#else
3751 0
3752#endif
3753 },
3754 {"cscope",
3755#ifdef FEAT_CSCOPE
3756 1
3757#else
3758 0
3759#endif
3760 },
3761 {"cursorbind", 1},
3762 {"cursorshape",
3763#ifdef CURSOR_SHAPE
3764 1
3765#else
3766 0
3767#endif
3768 },
3769 {"debug",
3770#ifdef DEBUG
3771 1
3772#else
3773 0
3774#endif
3775 },
3776 {"dialog_con",
3777#ifdef FEAT_CON_DIALOG
3778 1
3779#else
3780 0
3781#endif
3782 },
3783 {"dialog_gui",
3784#ifdef FEAT_GUI_DIALOG
3785 1
3786#else
3787 0
3788#endif
3789 },
3790 {"diff",
3791#ifdef FEAT_DIFF
3792 1
3793#else
3794 0
3795#endif
3796 },
3797 {"digraphs",
3798#ifdef FEAT_DIGRAPHS
3799 1
3800#else
3801 0
3802#endif
3803 },
3804 {"directx",
3805#ifdef FEAT_DIRECTX
3806 1
3807#else
3808 0
3809#endif
3810 },
3811 {"dnd",
3812#ifdef FEAT_DND
3813 1
3814#else
3815 0
3816#endif
3817 },
3818 {"emacs_tags",
3819#ifdef FEAT_EMACS_TAGS
3820 1
3821#else
3822 0
3823#endif
3824 },
3825 {"eval", 1}, // always present, of course!
3826 {"ex_extra", 1}, // graduated feature
3827 {"extra_search",
3828#ifdef FEAT_SEARCH_EXTRA
3829 1
3830#else
3831 0
3832#endif
3833 },
3834 {"file_in_path",
3835#ifdef FEAT_SEARCHPATH
3836 1
3837#else
3838 0
3839#endif
3840 },
3841 {"filterpipe",
3842#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
3843 1
3844#else
3845 0
3846#endif
3847 },
3848 {"find_in_path",
3849#ifdef FEAT_FIND_ID
3850 1
3851#else
3852 0
3853#endif
3854 },
3855 {"float",
3856#ifdef FEAT_FLOAT
3857 1
3858#else
3859 0
3860#endif
3861 },
3862 {"folding",
3863#ifdef FEAT_FOLDING
3864 1
3865#else
3866 0
3867#endif
3868 },
3869 {"footer",
3870#ifdef FEAT_FOOTER
3871 1
3872#else
3873 0
3874#endif
3875 },
3876 {"fork",
3877#if !defined(USE_SYSTEM) && defined(UNIX)
3878 1
3879#else
3880 0
3881#endif
3882 },
3883 {"gettext",
3884#ifdef FEAT_GETTEXT
3885 1
3886#else
3887 0
3888#endif
3889 },
3890 {"gui",
3891#ifdef FEAT_GUI
3892 1
3893#else
3894 0
3895#endif
3896 },
3897 {"gui_neXtaw",
3898#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
3899 1
3900#else
3901 0
3902#endif
3903 },
3904 {"gui_athena",
3905#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
3906 1
3907#else
3908 0
3909#endif
3910 },
3911 {"gui_gtk",
3912#ifdef FEAT_GUI_GTK
3913 1
3914#else
3915 0
3916#endif
3917 },
3918 {"gui_gtk2",
3919#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
3920 1
3921#else
3922 0
3923#endif
3924 },
3925 {"gui_gtk3",
3926#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
3927 1
3928#else
3929 0
3930#endif
3931 },
3932 {"gui_gnome",
3933#ifdef FEAT_GUI_GNOME
3934 1
3935#else
3936 0
3937#endif
3938 },
3939 {"gui_haiku",
3940#ifdef FEAT_GUI_HAIKU
3941 1
3942#else
3943 0
3944#endif
3945 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02003946 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01003947 {"gui_motif",
3948#ifdef FEAT_GUI_MOTIF
3949 1
3950#else
3951 0
3952#endif
3953 },
3954 {"gui_photon",
3955#ifdef FEAT_GUI_PHOTON
3956 1
3957#else
3958 0
3959#endif
3960 },
3961 {"gui_win32",
3962#ifdef FEAT_GUI_MSWIN
3963 1
3964#else
3965 0
3966#endif
3967 },
3968 {"iconv",
3969#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
3970 1
3971#else
3972 0
3973#endif
3974 },
3975 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02003976 {"ipv6",
3977#ifdef FEAT_IPV6
3978 1
3979#else
3980 0
3981#endif
3982 },
Bram Moolenaar79296512020-03-22 16:17:14 +01003983 {"job",
3984#ifdef FEAT_JOB_CHANNEL
3985 1
3986#else
3987 0
3988#endif
3989 },
3990 {"jumplist",
3991#ifdef FEAT_JUMPLIST
3992 1
3993#else
3994 0
3995#endif
3996 },
3997 {"keymap",
3998#ifdef FEAT_KEYMAP
3999 1
4000#else
4001 0
4002#endif
4003 },
4004 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4005 {"langmap",
4006#ifdef FEAT_LANGMAP
4007 1
4008#else
4009 0
4010#endif
4011 },
4012 {"libcall",
4013#ifdef FEAT_LIBCALL
4014 1
4015#else
4016 0
4017#endif
4018 },
4019 {"linebreak",
4020#ifdef FEAT_LINEBREAK
4021 1
4022#else
4023 0
4024#endif
4025 },
4026 {"lispindent",
4027#ifdef FEAT_LISP
4028 1
4029#else
4030 0
4031#endif
4032 },
4033 {"listcmds", 1},
4034 {"localmap", 1},
4035 {"lua",
4036#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4037 1
4038#else
4039 0
4040#endif
4041 },
4042 {"menu",
4043#ifdef FEAT_MENU
4044 1
4045#else
4046 0
4047#endif
4048 },
4049 {"mksession",
4050#ifdef FEAT_SESSION
4051 1
4052#else
4053 0
4054#endif
4055 },
4056 {"modify_fname", 1},
4057 {"mouse", 1},
4058 {"mouseshape",
4059#ifdef FEAT_MOUSESHAPE
4060 1
4061#else
4062 0
4063#endif
4064 },
4065 {"mouse_dec",
4066#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4067 1
4068#else
4069 0
4070#endif
4071 },
4072 {"mouse_gpm",
4073#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4074 1
4075#else
4076 0
4077#endif
4078 },
4079 {"mouse_jsbterm",
4080#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4081 1
4082#else
4083 0
4084#endif
4085 },
4086 {"mouse_netterm",
4087#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4088 1
4089#else
4090 0
4091#endif
4092 },
4093 {"mouse_pterm",
4094#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4095 1
4096#else
4097 0
4098#endif
4099 },
4100 {"mouse_sgr",
4101#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4102 1
4103#else
4104 0
4105#endif
4106 },
4107 {"mouse_sysmouse",
4108#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4109 1
4110#else
4111 0
4112#endif
4113 },
4114 {"mouse_urxvt",
4115#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4116 1
4117#else
4118 0
4119#endif
4120 },
4121 {"mouse_xterm",
4122#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4123 1
4124#else
4125 0
4126#endif
4127 },
4128 {"multi_byte", 1},
4129 {"multi_byte_ime",
4130#ifdef FEAT_MBYTE_IME
4131 1
4132#else
4133 0
4134#endif
4135 },
4136 {"multi_lang",
4137#ifdef FEAT_MULTI_LANG
4138 1
4139#else
4140 0
4141#endif
4142 },
4143 {"mzscheme",
4144#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4145 1
4146#else
4147 0
4148#endif
4149 },
4150 {"num64", 1},
4151 {"ole",
4152#ifdef FEAT_OLE
4153 1
4154#else
4155 0
4156#endif
4157 },
4158 {"packages",
4159#ifdef FEAT_EVAL
4160 1
4161#else
4162 0
4163#endif
4164 },
4165 {"path_extra",
4166#ifdef FEAT_PATH_EXTRA
4167 1
4168#else
4169 0
4170#endif
4171 },
4172 {"perl",
4173#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4174 1
4175#else
4176 0
4177#endif
4178 },
4179 {"persistent_undo",
4180#ifdef FEAT_PERSISTENT_UNDO
4181 1
4182#else
4183 0
4184#endif
4185 },
4186 {"python_compiled",
4187#if defined(FEAT_PYTHON)
4188 1
4189#else
4190 0
4191#endif
4192 },
4193 {"python_dynamic",
4194#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4195 1
4196#else
4197 0
4198#endif
4199 },
4200 {"python",
4201#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4202 1
4203#else
4204 0
4205#endif
4206 },
4207 {"pythonx",
4208#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4209 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4210 1
4211#else
4212 0
4213#endif
4214 },
4215 {"python3_compiled",
4216#if defined(FEAT_PYTHON3)
4217 1
4218#else
4219 0
4220#endif
4221 },
4222 {"python3_dynamic",
4223#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4224 1
4225#else
4226 0
4227#endif
4228 },
4229 {"python3",
4230#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4231 1
4232#else
4233 0
4234#endif
4235 },
4236 {"popupwin",
4237#ifdef FEAT_PROP_POPUP
4238 1
4239#else
4240 0
4241#endif
4242 },
4243 {"postscript",
4244#ifdef FEAT_POSTSCRIPT
4245 1
4246#else
4247 0
4248#endif
4249 },
4250 {"printer",
4251#ifdef FEAT_PRINTER
4252 1
4253#else
4254 0
4255#endif
4256 },
4257 {"profile",
4258#ifdef FEAT_PROFILE
4259 1
4260#else
4261 0
4262#endif
4263 },
4264 {"reltime",
4265#ifdef FEAT_RELTIME
4266 1
4267#else
4268 0
4269#endif
4270 },
4271 {"quickfix",
4272#ifdef FEAT_QUICKFIX
4273 1
4274#else
4275 0
4276#endif
4277 },
4278 {"rightleft",
4279#ifdef FEAT_RIGHTLEFT
4280 1
4281#else
4282 0
4283#endif
4284 },
4285 {"ruby",
4286#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
4287 1
4288#else
4289 0
4290#endif
4291 },
4292 {"scrollbind", 1},
4293 {"showcmd",
4294#ifdef FEAT_CMDL_INFO
4295 1
4296#else
4297 0
4298#endif
4299 },
4300 {"cmdline_info",
4301#ifdef FEAT_CMDL_INFO
4302 1
4303#else
4304 0
4305#endif
4306 },
4307 {"signs",
4308#ifdef FEAT_SIGNS
4309 1
4310#else
4311 0
4312#endif
4313 },
4314 {"smartindent",
4315#ifdef FEAT_SMARTINDENT
4316 1
4317#else
4318 0
4319#endif
4320 },
4321 {"startuptime",
4322#ifdef STARTUPTIME
4323 1
4324#else
4325 0
4326#endif
4327 },
4328 {"statusline",
4329#ifdef FEAT_STL_OPT
4330 1
4331#else
4332 0
4333#endif
4334 },
4335 {"netbeans_intg",
4336#ifdef FEAT_NETBEANS_INTG
4337 1
4338#else
4339 0
4340#endif
4341 },
4342 {"sound",
4343#ifdef FEAT_SOUND
4344 1
4345#else
4346 0
4347#endif
4348 },
4349 {"spell",
4350#ifdef FEAT_SPELL
4351 1
4352#else
4353 0
4354#endif
4355 },
4356 {"syntax",
4357#ifdef FEAT_SYN_HL
4358 1
4359#else
4360 0
4361#endif
4362 },
4363 {"system",
4364#if defined(USE_SYSTEM) || !defined(UNIX)
4365 1
4366#else
4367 0
4368#endif
4369 },
4370 {"tag_binary",
4371#ifdef FEAT_TAG_BINS
4372 1
4373#else
4374 0
4375#endif
4376 },
4377 {"tcl",
4378#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
4379 1
4380#else
4381 0
4382#endif
4383 },
4384 {"termguicolors",
4385#ifdef FEAT_TERMGUICOLORS
4386 1
4387#else
4388 0
4389#endif
4390 },
4391 {"terminal",
4392#if defined(FEAT_TERMINAL) && !defined(MSWIN)
4393 1
4394#else
4395 0
4396#endif
4397 },
4398 {"terminfo",
4399#ifdef TERMINFO
4400 1
4401#else
4402 0
4403#endif
4404 },
4405 {"termresponse",
4406#ifdef FEAT_TERMRESPONSE
4407 1
4408#else
4409 0
4410#endif
4411 },
4412 {"textobjects",
4413#ifdef FEAT_TEXTOBJ
4414 1
4415#else
4416 0
4417#endif
4418 },
4419 {"textprop",
4420#ifdef FEAT_PROP_POPUP
4421 1
4422#else
4423 0
4424#endif
4425 },
4426 {"tgetent",
4427#ifdef HAVE_TGETENT
4428 1
4429#else
4430 0
4431#endif
4432 },
4433 {"timers",
4434#ifdef FEAT_TIMERS
4435 1
4436#else
4437 0
4438#endif
4439 },
4440 {"title",
4441#ifdef FEAT_TITLE
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"toolbar",
4448#ifdef FEAT_TOOLBAR
4449 1
4450#else
4451 0
4452#endif
4453 },
4454 {"unnamedplus",
4455#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
4456 1
4457#else
4458 0
4459#endif
4460 },
4461 {"user-commands", 1}, // was accidentally included in 5.4
4462 {"user_commands", 1},
4463 {"vartabs",
4464#ifdef FEAT_VARTABS
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"vertsplit", 1},
4471 {"viminfo",
4472#ifdef FEAT_VIMINFO
4473 1
4474#else
4475 0
4476#endif
4477 },
4478 {"vimscript-1", 1},
4479 {"vimscript-2", 1},
4480 {"vimscript-3", 1},
4481 {"vimscript-4", 1},
4482 {"virtualedit", 1},
4483 {"visual", 1},
4484 {"visualextra", 1},
4485 {"vreplace", 1},
4486 {"vtp",
4487#ifdef FEAT_VTP
4488 1
4489#else
4490 0
4491#endif
4492 },
4493 {"wildignore",
4494#ifdef FEAT_WILDIGN
4495 1
4496#else
4497 0
4498#endif
4499 },
4500 {"wildmenu",
4501#ifdef FEAT_WILDMENU
4502 1
4503#else
4504 0
4505#endif
4506 },
4507 {"windows", 1},
4508 {"winaltkeys",
4509#ifdef FEAT_WAK
4510 1
4511#else
4512 0
4513#endif
4514 },
4515 {"writebackup",
4516#ifdef FEAT_WRITEBACKUP
4517 1
4518#else
4519 0
4520#endif
4521 },
4522 {"xim",
4523#ifdef FEAT_XIM
4524 1
4525#else
4526 0
4527#endif
4528 },
4529 {"xfontset",
4530#ifdef FEAT_XFONTSET
4531 1
4532#else
4533 0
4534#endif
4535 },
4536 {"xpm",
4537#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
4538 1
4539#else
4540 0
4541#endif
4542 },
4543 {"xpm_w32", // for backward compatibility
4544#ifdef FEAT_XPM_W32
4545 1
4546#else
4547 0
4548#endif
4549 },
4550 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01004552 1
4553#else
4554 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004556 },
4557 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004558#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01004559 1
4560#else
4561 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004563 },
4564 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004565#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01004566 1
4567#else
4568 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004569#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004570 },
4571 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01004573 1
4574#else
4575 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004576#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004577 },
4578 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004579#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01004580 1
4581#else
4582 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004584 },
4585 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004586 };
4587
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01004589 for (i = 0; has_list[i].name != NULL; ++i)
4590 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004592 x = TRUE;
4593 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594 break;
4595 }
4596
Bram Moolenaar79296512020-03-22 16:17:14 +01004597 // features also in has_list[] but sometimes enabled at runtime
4598 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004599 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004600 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02004601 {
4602 // intentionally empty
4603 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004604#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004605 else if (STRICMP(name, "balloon_multiline") == 0)
4606 n = multiline_balloon_available();
4607#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004608#ifdef VIMDLL
4609 else if (STRICMP(name, "filterpipe") == 0)
4610 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611#endif
4612#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
4613 else if (STRICMP(name, "iconv") == 0)
4614 n = iconv_enabled(FALSE);
4615#endif
4616#ifdef DYNAMIC_LUA
4617 else if (STRICMP(name, "lua") == 0)
4618 n = lua_enabled(FALSE);
4619#endif
4620#ifdef DYNAMIC_MZSCHEME
4621 else if (STRICMP(name, "mzscheme") == 0)
4622 n = mzscheme_enabled(FALSE);
4623#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004624#ifdef DYNAMIC_PERL
4625 else if (STRICMP(name, "perl") == 0)
4626 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628#ifdef DYNAMIC_PYTHON
4629 else if (STRICMP(name, "python") == 0)
4630 n = python_enabled(FALSE);
4631#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632#ifdef DYNAMIC_PYTHON3
4633 else if (STRICMP(name, "python3") == 0)
4634 n = python3_enabled(FALSE);
4635#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01004636#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
4637 else if (STRICMP(name, "pythonx") == 0)
4638 {
4639# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
4640 if (p_pyx == 0)
4641 n = python3_enabled(FALSE) || python_enabled(FALSE);
4642 else if (p_pyx == 3)
4643 n = python3_enabled(FALSE);
4644 else if (p_pyx == 2)
4645 n = python_enabled(FALSE);
4646# elif defined(DYNAMIC_PYTHON)
4647 n = python_enabled(FALSE);
4648# elif defined(DYNAMIC_PYTHON3)
4649 n = python3_enabled(FALSE);
4650# endif
4651 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004653#ifdef DYNAMIC_RUBY
4654 else if (STRICMP(name, "ruby") == 0)
4655 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004657#ifdef DYNAMIC_TCL
4658 else if (STRICMP(name, "tcl") == 0)
4659 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02004660#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004661#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02004662 else if (STRICMP(name, "terminal") == 0)
4663 n = terminal_enabled();
4664#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665 }
4666
Bram Moolenaar79296512020-03-22 16:17:14 +01004667 // features not in has_list[]
4668 if (x == FALSE)
4669 {
4670 if (STRNICMP(name, "patch", 5) == 0)
4671 {
4672 x = TRUE;
4673 if (name[5] == '-'
4674 && STRLEN(name) >= 11
4675 && vim_isdigit(name[6])
4676 && vim_isdigit(name[8])
4677 && vim_isdigit(name[10]))
4678 {
4679 int major = atoi((char *)name + 6);
4680 int minor = atoi((char *)name + 8);
4681
4682 // Expect "patch-9.9.01234".
4683 n = (major < VIM_VERSION_MAJOR
4684 || (major == VIM_VERSION_MAJOR
4685 && (minor < VIM_VERSION_MINOR
4686 || (minor == VIM_VERSION_MINOR
4687 && has_patch(atoi((char *)name + 10))))));
4688 }
4689 else
4690 n = has_patch(atoi((char *)name + 5));
4691 }
4692 else if (STRICMP(name, "vim_starting") == 0)
4693 {
4694 x = TRUE;
4695 n = (starting != 0);
4696 }
4697 else if (STRICMP(name, "ttyin") == 0)
4698 {
4699 x = TRUE;
4700 n = mch_input_isatty();
4701 }
4702 else if (STRICMP(name, "ttyout") == 0)
4703 {
4704 x = TRUE;
4705 n = stdout_isatty;
4706 }
4707 else if (STRICMP(name, "multi_byte_encoding") == 0)
4708 {
4709 x = TRUE;
4710 n = has_mbyte;
4711 }
4712 else if (STRICMP(name, "gui_running") == 0)
4713 {
4714 x = TRUE;
4715#ifdef FEAT_GUI
4716 n = (gui.in_use || gui.starting);
4717#endif
4718 }
4719 else if (STRICMP(name, "browse") == 0)
4720 {
4721 x = TRUE;
4722#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
4723 n = gui.in_use; // gui_mch_browse() works when GUI is running
4724#endif
4725 }
4726 else if (STRICMP(name, "syntax_items") == 0)
4727 {
4728 x = TRUE;
4729#ifdef FEAT_SYN_HL
4730 n = syntax_present(curwin);
4731#endif
4732 }
4733 else if (STRICMP(name, "vcon") == 0)
4734 {
4735 x = TRUE;
4736#ifdef FEAT_VTP
4737 n = is_term_win32() && has_vtp_working();
4738#endif
4739 }
4740 else if (STRICMP(name, "netbeans_enabled") == 0)
4741 {
4742 x = TRUE;
4743#ifdef FEAT_NETBEANS_INTG
4744 n = netbeans_active();
4745#endif
4746 }
4747 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
4748 {
4749 x = TRUE;
4750#ifdef FEAT_MOUSE_GPM
4751 n = gpm_enabled();
4752#endif
4753 }
4754 else if (STRICMP(name, "conpty") == 0)
4755 {
4756 x = TRUE;
4757#if defined(FEAT_TERMINAL) && defined(MSWIN)
4758 n = use_conpty();
4759#endif
4760 }
4761 else if (STRICMP(name, "clipboard_working") == 0)
4762 {
4763 x = TRUE;
4764#ifdef FEAT_CLIPBOARD
4765 n = clip_star.available;
4766#endif
4767 }
4768 }
4769
4770 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_number(&argvars[1]) != 0)
4771 // return whether feature could ever be enabled
4772 rettv->vval.v_number = x;
4773 else
4774 // return whether feature is enabled
4775 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004776}
4777
4778/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779 * "haslocaldir()" function
4780 */
4781 static void
4782f_haslocaldir(typval_T *argvars, typval_T *rettv)
4783{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004784 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004785 win_T *wp = NULL;
4786
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004787 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
4788
4789 // Check for window-local and tab-local directories
4790 if (wp != NULL && wp->w_localdir != NULL)
4791 rettv->vval.v_number = 1;
4792 else if (tp != NULL && tp->tp_localdir != NULL)
4793 rettv->vval.v_number = 2;
4794 else
4795 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004796}
4797
4798/*
4799 * "hasmapto()" function
4800 */
4801 static void
4802f_hasmapto(typval_T *argvars, typval_T *rettv)
4803{
4804 char_u *name;
4805 char_u *mode;
4806 char_u buf[NUMBUFLEN];
4807 int abbr = FALSE;
4808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004809 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004810 if (argvars[1].v_type == VAR_UNKNOWN)
4811 mode = (char_u *)"nvo";
4812 else
4813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004814 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 }
4818
4819 if (map_to_exists(name, mode, abbr))
4820 rettv->vval.v_number = TRUE;
4821 else
4822 rettv->vval.v_number = FALSE;
4823}
4824
4825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004826 * "highlightID(name)" function
4827 */
4828 static void
4829f_hlID(typval_T *argvars, typval_T *rettv)
4830{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004831 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004832}
4833
4834/*
4835 * "highlight_exists()" function
4836 */
4837 static void
4838f_hlexists(typval_T *argvars, typval_T *rettv)
4839{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004840 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004841}
4842
4843/*
4844 * "hostname()" function
4845 */
4846 static void
4847f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
4848{
4849 char_u hostname[256];
4850
4851 mch_get_host_name(hostname, 256);
4852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = vim_strsave(hostname);
4854}
4855
4856/*
4857 * iconv() function
4858 */
4859 static void
4860f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
4861{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862 char_u buf1[NUMBUFLEN];
4863 char_u buf2[NUMBUFLEN];
4864 char_u *from, *to, *str;
4865 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004866
4867 rettv->v_type = VAR_STRING;
4868 rettv->vval.v_string = NULL;
4869
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004870 str = tv_get_string(&argvars[0]);
4871 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
4872 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 vimconv.vc_type = CONV_NONE;
4874 convert_setup(&vimconv, from, to);
4875
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004876 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 if (vimconv.vc_type == CONV_NONE)
4878 rettv->vval.v_string = vim_strsave(str);
4879 else
4880 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
4881
4882 convert_setup(&vimconv, NULL, NULL);
4883 vim_free(from);
4884 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885}
4886
4887/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 * "index()" function
4889 */
4890 static void
4891f_index(typval_T *argvars, typval_T *rettv)
4892{
4893 list_T *l;
4894 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004895 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 long idx = 0;
4897 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004898 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899
4900 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004901 if (argvars[0].v_type == VAR_BLOB)
4902 {
4903 typval_T tv;
4904 int start = 0;
4905
4906 if (argvars[2].v_type != VAR_UNKNOWN)
4907 {
4908 start = tv_get_number_chk(&argvars[2], &error);
4909 if (error)
4910 return;
4911 }
4912 b = argvars[0].vval.v_blob;
4913 if (b == NULL)
4914 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01004915 if (start < 0)
4916 {
4917 start = blob_len(b) + start;
4918 if (start < 0)
4919 start = 0;
4920 }
4921
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004922 for (idx = start; idx < blob_len(b); ++idx)
4923 {
4924 tv.v_type = VAR_NUMBER;
4925 tv.vval.v_number = blob_get(b, idx);
4926 if (tv_equal(&tv, &argvars[1], ic, FALSE))
4927 {
4928 rettv->vval.v_number = idx;
4929 return;
4930 }
4931 }
4932 return;
4933 }
4934 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004936 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 return;
4938 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 l = argvars[0].vval.v_list;
4941 if (l != NULL)
4942 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02004943 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944 item = l->lv_first;
4945 if (argvars[2].v_type != VAR_UNKNOWN)
4946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004947 // Start at specified item. Use the cached index that list_find()
4948 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004949 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01004950 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004952 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 if (error)
4954 item = NULL;
4955 }
4956
4957 for ( ; item != NULL; item = item->li_next, ++idx)
4958 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
4959 {
4960 rettv->vval.v_number = idx;
4961 break;
4962 }
4963 }
4964}
4965
4966static int inputsecret_flag = 0;
4967
4968/*
4969 * "input()" function
4970 * Also handles inputsecret() when inputsecret is set.
4971 */
4972 static void
4973f_input(typval_T *argvars, typval_T *rettv)
4974{
4975 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
4976}
4977
4978/*
4979 * "inputdialog()" function
4980 */
4981 static void
4982f_inputdialog(typval_T *argvars, typval_T *rettv)
4983{
4984#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
4987 {
4988 char_u *message;
4989 char_u buf[NUMBUFLEN];
4990 char_u *defstr = (char_u *)"";
4991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004993 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004994 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004995 vim_strncpy(IObuff, defstr, IOSIZE - 1);
4996 else
4997 IObuff[0] = NUL;
4998 if (message != NULL && defstr != NULL
4999 && do_dialog(VIM_QUESTION, NULL, message,
5000 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5001 rettv->vval.v_string = vim_strsave(IObuff);
5002 else
5003 {
5004 if (message != NULL && defstr != NULL
5005 && argvars[1].v_type != VAR_UNKNOWN
5006 && argvars[2].v_type != VAR_UNKNOWN)
5007 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005008 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005009 else
5010 rettv->vval.v_string = NULL;
5011 }
5012 rettv->v_type = VAR_STRING;
5013 }
5014 else
5015#endif
5016 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5017}
5018
5019/*
5020 * "inputlist()" function
5021 */
5022 static void
5023f_inputlist(typval_T *argvars, typval_T *rettv)
5024{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005025 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 listitem_T *li;
5027 int selected;
5028 int mouse_used;
5029
5030#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // While starting up, there is no place to enter text. When running tests
5032 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005033 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 return;
5035#endif
5036 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005038 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039 return;
5040 }
5041
5042 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 msg_row = Rows - 1; // for when 'cmdheight' > 1
5044 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045 msg_scroll = TRUE;
5046 msg_clr_eos();
5047
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005048 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005049 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005050 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005051 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005052 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005053 msg_putchar('\n');
5054 }
5055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005056 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 selected = prompt_for_number(&mouse_used);
5058 if (mouse_used)
5059 selected -= lines_left;
5060
5061 rettv->vval.v_number = selected;
5062}
5063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005064static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5065
5066/*
5067 * "inputrestore()" function
5068 */
5069 static void
5070f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5071{
5072 if (ga_userinput.ga_len > 0)
5073 {
5074 --ga_userinput.ga_len;
5075 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5076 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005077 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005078 }
5079 else if (p_verbose > 1)
5080 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005081 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 }
5084}
5085
5086/*
5087 * "inputsave()" function
5088 */
5089 static void
5090f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5091{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005092 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 if (ga_grow(&ga_userinput, 1) == OK)
5094 {
5095 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5096 + ga_userinput.ga_len);
5097 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 }
5100 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102}
5103
5104/*
5105 * "inputsecret()" function
5106 */
5107 static void
5108f_inputsecret(typval_T *argvars, typval_T *rettv)
5109{
5110 ++cmdline_star;
5111 ++inputsecret_flag;
5112 f_input(argvars, rettv);
5113 --cmdline_star;
5114 --inputsecret_flag;
5115}
5116
5117/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005118 * "interrupt()" function
5119 */
5120 static void
5121f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5122{
5123 got_int = TRUE;
5124}
5125
5126/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005127 * "invert(expr)" function
5128 */
5129 static void
5130f_invert(typval_T *argvars, typval_T *rettv)
5131{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005132 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005133}
5134
5135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 * "islocked()" function
5137 */
5138 static void
5139f_islocked(typval_T *argvars, typval_T *rettv)
5140{
5141 lval_T lv;
5142 char_u *end;
5143 dictitem_T *di;
5144
5145 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005146 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005147 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005148 if (end != NULL && lv.ll_name != NULL)
5149 {
5150 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005151 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152 else
5153 {
5154 if (lv.ll_tv == NULL)
5155 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005156 di = find_var(lv.ll_name, NULL, TRUE);
5157 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005159 // Consider a variable locked when:
5160 // 1. the variable itself is locked
5161 // 2. the value of the variable is locked.
5162 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005163 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5164 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165 }
5166 }
5167 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005168 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005170 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5174 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005175 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005176 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5177 }
5178 }
5179
5180 clear_lval(&lv);
5181}
5182
5183#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
5184/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02005185 * "isinf()" function
5186 */
5187 static void
5188f_isinf(typval_T *argvars, typval_T *rettv)
5189{
5190 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
5191 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
5192}
5193
5194/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005195 * "isnan()" function
5196 */
5197 static void
5198f_isnan(typval_T *argvars, typval_T *rettv)
5199{
5200 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
5201 && isnan(argvars[0].vval.v_float);
5202}
5203#endif
5204
5205/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 * "last_buffer_nr()" function.
5207 */
5208 static void
5209f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5210{
5211 int n = 0;
5212 buf_T *buf;
5213
Bram Moolenaar29323592016-07-24 22:04:11 +02005214 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 if (n < buf->b_fnum)
5216 n = buf->b_fnum;
5217
5218 rettv->vval.v_number = n;
5219}
5220
5221/*
5222 * "len()" function
5223 */
5224 static void
5225f_len(typval_T *argvars, typval_T *rettv)
5226{
5227 switch (argvars[0].v_type)
5228 {
5229 case VAR_STRING:
5230 case VAR_NUMBER:
5231 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005232 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005234 case VAR_BLOB:
5235 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
5236 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 case VAR_LIST:
5238 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
5239 break;
5240 case VAR_DICT:
5241 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
5242 break;
5243 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005244 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005246 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005247 case VAR_SPECIAL:
5248 case VAR_FLOAT:
5249 case VAR_FUNC:
5250 case VAR_PARTIAL:
5251 case VAR_JOB:
5252 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005253 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254 break;
5255 }
5256}
5257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005258 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01005259libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260{
5261#ifdef FEAT_LIBCALL
5262 char_u *string_in;
5263 char_u **string_result;
5264 int nr_result;
5265#endif
5266
5267 rettv->v_type = type;
5268 if (type != VAR_NUMBER)
5269 rettv->vval.v_string = NULL;
5270
5271 if (check_restricted() || check_secure())
5272 return;
5273
5274#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005276 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
5277 {
5278 string_in = NULL;
5279 if (argvars[2].v_type == VAR_STRING)
5280 string_in = argvars[2].vval.v_string;
5281 if (type == VAR_NUMBER)
5282 string_result = NULL;
5283 else
5284 string_result = &rettv->vval.v_string;
5285 if (mch_libcall(argvars[0].vval.v_string,
5286 argvars[1].vval.v_string,
5287 string_in,
5288 argvars[2].vval.v_number,
5289 string_result,
5290 &nr_result) == OK
5291 && type == VAR_NUMBER)
5292 rettv->vval.v_number = nr_result;
5293 }
5294#endif
5295}
5296
5297/*
5298 * "libcall()" function
5299 */
5300 static void
5301f_libcall(typval_T *argvars, typval_T *rettv)
5302{
5303 libcall_common(argvars, rettv, VAR_STRING);
5304}
5305
5306/*
5307 * "libcallnr()" function
5308 */
5309 static void
5310f_libcallnr(typval_T *argvars, typval_T *rettv)
5311{
5312 libcall_common(argvars, rettv, VAR_NUMBER);
5313}
5314
5315/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005316 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317 */
5318 static void
5319f_line(typval_T *argvars, typval_T *rettv)
5320{
5321 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005322 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005324 int id;
5325 tabpage_T *tp;
5326 win_T *wp;
5327 win_T *save_curwin;
5328 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02005330 if (argvars[1].v_type != VAR_UNKNOWN)
5331 {
5332 // use window specified in the second argument
5333 id = (int)tv_get_number(&argvars[1]);
5334 wp = win_id2wp_tp(id, &tp);
5335 if (wp != NULL && tp != NULL)
5336 {
5337 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
5338 == OK)
5339 {
5340 check_cursor();
5341 fp = var2fpos(&argvars[0], TRUE, &fnum);
5342 }
5343 restore_win_noblock(save_curwin, save_curtab, TRUE);
5344 }
5345 }
5346 else
5347 // use current window
5348 fp = var2fpos(&argvars[0], TRUE, &fnum);
5349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350 if (fp != NULL)
5351 lnum = fp->lnum;
5352 rettv->vval.v_number = lnum;
5353}
5354
5355/*
5356 * "line2byte(lnum)" function
5357 */
5358 static void
5359f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
5360{
5361#ifndef FEAT_BYTEOFF
5362 rettv->vval.v_number = -1;
5363#else
5364 linenr_T lnum;
5365
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005366 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
5368 rettv->vval.v_number = -1;
5369 else
5370 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
5371 if (rettv->vval.v_number >= 0)
5372 ++rettv->vval.v_number;
5373#endif
5374}
5375
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376#ifdef FEAT_FLOAT
5377/*
5378 * "log()" function
5379 */
5380 static void
5381f_log(typval_T *argvars, typval_T *rettv)
5382{
5383 float_T f = 0.0;
5384
5385 rettv->v_type = VAR_FLOAT;
5386 if (get_float_arg(argvars, &f) == OK)
5387 rettv->vval.v_float = log(f);
5388 else
5389 rettv->vval.v_float = 0.0;
5390}
5391
5392/*
5393 * "log10()" function
5394 */
5395 static void
5396f_log10(typval_T *argvars, typval_T *rettv)
5397{
5398 float_T f = 0.0;
5399
5400 rettv->v_type = VAR_FLOAT;
5401 if (get_float_arg(argvars, &f) == OK)
5402 rettv->vval.v_float = log10(f);
5403 else
5404 rettv->vval.v_float = 0.0;
5405}
5406#endif
5407
5408#ifdef FEAT_LUA
5409/*
5410 * "luaeval()" function
5411 */
5412 static void
5413f_luaeval(typval_T *argvars, typval_T *rettv)
5414{
5415 char_u *str;
5416 char_u buf[NUMBUFLEN];
5417
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005418 if (check_restricted() || check_secure())
5419 return;
5420
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005421 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005422 do_luaeval(str, argvars + 1, rettv);
5423}
5424#endif
5425
5426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005427 * "maparg()" function
5428 */
5429 static void
5430f_maparg(typval_T *argvars, typval_T *rettv)
5431{
5432 get_maparg(argvars, rettv, TRUE);
5433}
5434
5435/*
5436 * "mapcheck()" function
5437 */
5438 static void
5439f_mapcheck(typval_T *argvars, typval_T *rettv)
5440{
5441 get_maparg(argvars, rettv, FALSE);
5442}
5443
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005444typedef enum
5445{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 MATCH_END, // matchend()
5447 MATCH_MATCH, // match()
5448 MATCH_STR, // matchstr()
5449 MATCH_LIST, // matchlist()
5450 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005451} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005452
5453 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005454find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455{
5456 char_u *str = NULL;
5457 long len = 0;
5458 char_u *expr = NULL;
5459 char_u *pat;
5460 regmatch_T regmatch;
5461 char_u patbuf[NUMBUFLEN];
5462 char_u strbuf[NUMBUFLEN];
5463 char_u *save_cpo;
5464 long start = 0;
5465 long nth = 1;
5466 colnr_T startcol = 0;
5467 int match = 0;
5468 list_T *l = NULL;
5469 listitem_T *li = NULL;
5470 long idx = 0;
5471 char_u *tofree = NULL;
5472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474 save_cpo = p_cpo;
5475 p_cpo = (char_u *)"";
5476
5477 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005478 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005480 // type MATCH_LIST: return empty list when there are no matches.
5481 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 if (rettv_list_alloc(rettv) == FAIL)
5483 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005484 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005485 && (list_append_string(rettv->vval.v_list,
5486 (char_u *)"", 0) == FAIL
5487 || list_append_number(rettv->vval.v_list,
5488 (varnumber_T)-1) == FAIL
5489 || list_append_number(rettv->vval.v_list,
5490 (varnumber_T)-1) == FAIL
5491 || list_append_number(rettv->vval.v_list,
5492 (varnumber_T)-1) == FAIL))
5493 {
5494 list_free(rettv->vval.v_list);
5495 rettv->vval.v_list = NULL;
5496 goto theend;
5497 }
5498 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005499 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 {
5501 rettv->v_type = VAR_STRING;
5502 rettv->vval.v_string = NULL;
5503 }
5504
5505 if (argvars[0].v_type == VAR_LIST)
5506 {
5507 if ((l = argvars[0].vval.v_list) == NULL)
5508 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005509 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 li = l->lv_first;
5511 }
5512 else
5513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005514 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515 len = (long)STRLEN(str);
5516 }
5517
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005518 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519 if (pat == NULL)
5520 goto theend;
5521
5522 if (argvars[2].v_type != VAR_UNKNOWN)
5523 {
5524 int error = FALSE;
5525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005526 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005527 if (error)
5528 goto theend;
5529 if (l != NULL)
5530 {
5531 li = list_find(l, start);
5532 if (li == NULL)
5533 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005534 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535 }
5536 else
5537 {
5538 if (start < 0)
5539 start = 0;
5540 if (start > len)
5541 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005542 // When "count" argument is there ignore matches before "start",
5543 // otherwise skip part of the string. Differs when pattern is "^"
5544 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 if (argvars[3].v_type != VAR_UNKNOWN)
5546 startcol = start;
5547 else
5548 {
5549 str += start;
5550 len -= start;
5551 }
5552 }
5553
5554 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005555 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 if (error)
5557 goto theend;
5558 }
5559
5560 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5561 if (regmatch.regprog != NULL)
5562 {
5563 regmatch.rm_ic = p_ic;
5564
5565 for (;;)
5566 {
5567 if (l != NULL)
5568 {
5569 if (li == NULL)
5570 {
5571 match = FALSE;
5572 break;
5573 }
5574 vim_free(tofree);
5575 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
5576 if (str == NULL)
5577 break;
5578 }
5579
5580 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
5581
5582 if (match && --nth <= 0)
5583 break;
5584 if (l == NULL && !match)
5585 break;
5586
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005587 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588 if (l != NULL)
5589 {
5590 li = li->li_next;
5591 ++idx;
5592 }
5593 else
5594 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595 startcol = (colnr_T)(regmatch.startp[0]
5596 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 if (startcol > (colnr_T)len
5598 || str + startcol <= regmatch.startp[0])
5599 {
5600 match = FALSE;
5601 break;
5602 }
5603 }
5604 }
5605
5606 if (match)
5607 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005608 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005609 {
5610 listitem_T *li1 = rettv->vval.v_list->lv_first;
5611 listitem_T *li2 = li1->li_next;
5612 listitem_T *li3 = li2->li_next;
5613 listitem_T *li4 = li3->li_next;
5614
5615 vim_free(li1->li_tv.vval.v_string);
5616 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005617 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005618 li3->li_tv.vval.v_number =
5619 (varnumber_T)(regmatch.startp[0] - expr);
5620 li4->li_tv.vval.v_number =
5621 (varnumber_T)(regmatch.endp[0] - expr);
5622 if (l != NULL)
5623 li2->li_tv.vval.v_number = (varnumber_T)idx;
5624 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005625 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626 {
5627 int i;
5628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005629 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 for (i = 0; i < NSUBEXP; ++i)
5631 {
5632 if (regmatch.endp[i] == NULL)
5633 {
5634 if (list_append_string(rettv->vval.v_list,
5635 (char_u *)"", 0) == FAIL)
5636 break;
5637 }
5638 else if (list_append_string(rettv->vval.v_list,
5639 regmatch.startp[i],
5640 (int)(regmatch.endp[i] - regmatch.startp[i]))
5641 == FAIL)
5642 break;
5643 }
5644 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005645 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005647 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 if (l != NULL)
5649 copy_tv(&li->li_tv, rettv);
5650 else
5651 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02005652 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005653 }
5654 else if (l != NULL)
5655 rettv->vval.v_number = idx;
5656 else
5657 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005658 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 rettv->vval.v_number =
5660 (varnumber_T)(regmatch.startp[0] - str);
5661 else
5662 rettv->vval.v_number =
5663 (varnumber_T)(regmatch.endp[0] - str);
5664 rettv->vval.v_number += (varnumber_T)(str - expr);
5665 }
5666 }
5667 vim_regfree(regmatch.regprog);
5668 }
5669
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005670theend:
5671 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005672 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673 listitem_remove(rettv->vval.v_list,
5674 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 vim_free(tofree);
5676 p_cpo = save_cpo;
5677}
5678
5679/*
5680 * "match()" function
5681 */
5682 static void
5683f_match(typval_T *argvars, typval_T *rettv)
5684{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005685 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005686}
5687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688/*
5689 * "matchend()" function
5690 */
5691 static void
5692f_matchend(typval_T *argvars, typval_T *rettv)
5693{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005694 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695}
5696
5697/*
5698 * "matchlist()" function
5699 */
5700 static void
5701f_matchlist(typval_T *argvars, typval_T *rettv)
5702{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005703 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704}
5705
5706/*
5707 * "matchstr()" function
5708 */
5709 static void
5710f_matchstr(typval_T *argvars, typval_T *rettv)
5711{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005712 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005713}
5714
5715/*
5716 * "matchstrpos()" function
5717 */
5718 static void
5719f_matchstrpos(typval_T *argvars, typval_T *rettv)
5720{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02005721 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722}
5723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 static void
5725max_min(typval_T *argvars, typval_T *rettv, int domax)
5726{
5727 varnumber_T n = 0;
5728 varnumber_T i;
5729 int error = FALSE;
5730
5731 if (argvars[0].v_type == VAR_LIST)
5732 {
5733 list_T *l;
5734 listitem_T *li;
5735
5736 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005737 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005739 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005741 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
5742 n = l->lv_u.nonmat.lv_start;
5743 else
5744 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
5745 * l->lv_u.nonmat.lv_stride;
5746 }
5747 else
5748 {
5749 li = l->lv_first;
5750 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01005752 n = tv_get_number_chk(&li->li_tv, &error);
5753 for (;;)
5754 {
5755 li = li->li_next;
5756 if (li == NULL)
5757 break;
5758 i = tv_get_number_chk(&li->li_tv, &error);
5759 if (domax ? i > n : i < n)
5760 n = i;
5761 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005762 }
5763 }
5764 }
5765 }
5766 else if (argvars[0].v_type == VAR_DICT)
5767 {
5768 dict_T *d;
5769 int first = TRUE;
5770 hashitem_T *hi;
5771 int todo;
5772
5773 d = argvars[0].vval.v_dict;
5774 if (d != NULL)
5775 {
5776 todo = (int)d->dv_hashtab.ht_used;
5777 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
5778 {
5779 if (!HASHITEM_EMPTY(hi))
5780 {
5781 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005782 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005783 if (first)
5784 {
5785 n = i;
5786 first = FALSE;
5787 }
5788 else if (domax ? i > n : i < n)
5789 n = i;
5790 }
5791 }
5792 }
5793 }
5794 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005795 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 rettv->vval.v_number = error ? 0 : n;
5797}
5798
5799/*
5800 * "max()" function
5801 */
5802 static void
5803f_max(typval_T *argvars, typval_T *rettv)
5804{
5805 max_min(argvars, rettv, TRUE);
5806}
5807
5808/*
5809 * "min()" function
5810 */
5811 static void
5812f_min(typval_T *argvars, typval_T *rettv)
5813{
5814 max_min(argvars, rettv, FALSE);
5815}
5816
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817#if defined(FEAT_MZSCHEME) || defined(PROTO)
5818/*
5819 * "mzeval()" function
5820 */
5821 static void
5822f_mzeval(typval_T *argvars, typval_T *rettv)
5823{
5824 char_u *str;
5825 char_u buf[NUMBUFLEN];
5826
Bram Moolenaar8c62a082019-02-08 14:34:10 +01005827 if (check_restricted() || check_secure())
5828 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005829 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 do_mzeval(str, rettv);
5831}
5832
5833 void
5834mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
5835{
5836 typval_T argvars[3];
5837
5838 argvars[0].v_type = VAR_STRING;
5839 argvars[0].vval.v_string = name;
5840 copy_tv(args, &argvars[1]);
5841 argvars[2].v_type = VAR_UNKNOWN;
5842 f_call(argvars, rettv);
5843 clear_tv(&argvars[1]);
5844}
5845#endif
5846
5847/*
5848 * "nextnonblank()" function
5849 */
5850 static void
5851f_nextnonblank(typval_T *argvars, typval_T *rettv)
5852{
5853 linenr_T lnum;
5854
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 {
5857 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
5858 {
5859 lnum = 0;
5860 break;
5861 }
5862 if (*skipwhite(ml_get(lnum)) != NUL)
5863 break;
5864 }
5865 rettv->vval.v_number = lnum;
5866}
5867
5868/*
5869 * "nr2char()" function
5870 */
5871 static void
5872f_nr2char(typval_T *argvars, typval_T *rettv)
5873{
5874 char_u buf[NUMBUFLEN];
5875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876 if (has_mbyte)
5877 {
5878 int utf8 = 0;
5879
5880 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01005883 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005885 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 }
5887 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005889 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 buf[1] = NUL;
5891 }
5892 rettv->v_type = VAR_STRING;
5893 rettv->vval.v_string = vim_strsave(buf);
5894}
5895
5896/*
5897 * "or(expr, expr)" function
5898 */
5899 static void
5900f_or(typval_T *argvars, typval_T *rettv)
5901{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005902 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
5903 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904}
5905
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906#ifdef FEAT_PERL
5907/*
5908 * "perleval()" function
5909 */
5910 static void
5911f_perleval(typval_T *argvars, typval_T *rettv)
5912{
5913 char_u *str;
5914 char_u buf[NUMBUFLEN];
5915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005916 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 do_perleval(str, rettv);
5918}
5919#endif
5920
5921#ifdef FEAT_FLOAT
5922/*
5923 * "pow()" function
5924 */
5925 static void
5926f_pow(typval_T *argvars, typval_T *rettv)
5927{
5928 float_T fx = 0.0, fy = 0.0;
5929
5930 rettv->v_type = VAR_FLOAT;
5931 if (get_float_arg(argvars, &fx) == OK
5932 && get_float_arg(&argvars[1], &fy) == OK)
5933 rettv->vval.v_float = pow(fx, fy);
5934 else
5935 rettv->vval.v_float = 0.0;
5936}
5937#endif
5938
5939/*
5940 * "prevnonblank()" function
5941 */
5942 static void
5943f_prevnonblank(typval_T *argvars, typval_T *rettv)
5944{
5945 linenr_T lnum;
5946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005947 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
5949 lnum = 0;
5950 else
5951 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
5952 --lnum;
5953 rettv->vval.v_number = lnum;
5954}
5955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005956// This dummy va_list is here because:
5957// - passing a NULL pointer doesn't work when va_list isn't a pointer
5958// - locally in the function results in a "used before set" warning
5959// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005960static va_list ap;
5961
5962/*
5963 * "printf()" function
5964 */
5965 static void
5966f_printf(typval_T *argvars, typval_T *rettv)
5967{
5968 char_u buf[NUMBUFLEN];
5969 int len;
5970 char_u *s;
5971 int saved_did_emsg = did_emsg;
5972 char *fmt;
5973
5974 rettv->v_type = VAR_STRING;
5975 rettv->vval.v_string = NULL;
5976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005977 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005979 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005980 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981 if (!did_emsg)
5982 {
5983 s = alloc(len + 1);
5984 if (s != NULL)
5985 {
5986 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02005987 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
5988 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 }
5990 }
5991 did_emsg |= saved_did_emsg;
5992}
5993
5994/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02005995 * "pum_getpos()" function
5996 */
5997 static void
5998f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5999{
6000 if (rettv_dict_alloc(rettv) != OK)
6001 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006002 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006003}
6004
6005/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 * "pumvisible()" function
6007 */
6008 static void
6009f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6010{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 if (pum_visible())
6012 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013}
6014
6015#ifdef FEAT_PYTHON3
6016/*
6017 * "py3eval()" function
6018 */
6019 static void
6020f_py3eval(typval_T *argvars, typval_T *rettv)
6021{
6022 char_u *str;
6023 char_u buf[NUMBUFLEN];
6024
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006025 if (check_restricted() || check_secure())
6026 return;
6027
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006028 if (p_pyx == 0)
6029 p_pyx = 3;
6030
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006031 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 do_py3eval(str, rettv);
6033}
6034#endif
6035
6036#ifdef FEAT_PYTHON
6037/*
6038 * "pyeval()" function
6039 */
6040 static void
6041f_pyeval(typval_T *argvars, typval_T *rettv)
6042{
6043 char_u *str;
6044 char_u buf[NUMBUFLEN];
6045
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006046 if (check_restricted() || check_secure())
6047 return;
6048
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006049 if (p_pyx == 0)
6050 p_pyx = 2;
6051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006052 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 do_pyeval(str, rettv);
6054}
6055#endif
6056
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006057#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6058/*
6059 * "pyxeval()" function
6060 */
6061 static void
6062f_pyxeval(typval_T *argvars, typval_T *rettv)
6063{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006064 if (check_restricted() || check_secure())
6065 return;
6066
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006067# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6068 init_pyxversion();
6069 if (p_pyx == 2)
6070 f_pyeval(argvars, rettv);
6071 else
6072 f_py3eval(argvars, rettv);
6073# elif defined(FEAT_PYTHON)
6074 f_pyeval(argvars, rettv);
6075# elif defined(FEAT_PYTHON3)
6076 f_py3eval(argvars, rettv);
6077# endif
6078}
6079#endif
6080
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006081static UINT32_T srand_seed_for_testing = 0;
6082static int srand_seed_for_testing_is_used = FALSE;
6083
6084 static void
6085f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6086{
6087 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006088 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006089 else
6090 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006091 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6092 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006093 }
6094}
6095
6096 static void
6097init_srand(UINT32_T *x)
6098{
6099#ifndef MSWIN
6100 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6101#endif
6102
6103 if (srand_seed_for_testing_is_used)
6104 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006105 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006106 return;
6107 }
6108#ifndef MSWIN
6109 if (dev_urandom_state != FAIL)
6110 {
6111 int fd = open("/dev/urandom", O_RDONLY);
6112 struct {
6113 union {
6114 UINT32_T number;
6115 char bytes[sizeof(UINT32_T)];
6116 } contents;
6117 } buf;
6118
6119 // Attempt reading /dev/urandom.
6120 if (fd == -1)
6121 dev_urandom_state = FAIL;
6122 else
6123 {
6124 buf.contents.number = 0;
6125 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6126 != sizeof(UINT32_T))
6127 dev_urandom_state = FAIL;
6128 else
6129 {
6130 dev_urandom_state = OK;
6131 *x = buf.contents.number;
6132 }
6133 close(fd);
6134 }
6135 }
6136 if (dev_urandom_state != OK)
6137 // Reading /dev/urandom doesn't work, fall back to time().
6138#endif
6139 *x = vim_time();
6140}
6141
6142#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6143#define SPLITMIX32(x, z) ( \
6144 z = (x += 0x9e3779b9), \
6145 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6146 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6147 z ^ (z >> 16) \
6148 )
6149#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6150 result = ROTL(y * 5, 7) * 9; \
6151 t = y << 9; \
6152 z ^= x; \
6153 w ^= y; \
6154 y ^= z, x ^= w; \
6155 z ^= t; \
6156 w = ROTL(w, 11);
6157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006159 * "rand()" function
6160 */
6161 static void
6162f_rand(typval_T *argvars, typval_T *rettv)
6163{
6164 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006165 static UINT32_T gx, gy, gz, gw;
6166 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006167 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006168 UINT32_T x, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006169
6170 if (argvars[0].v_type == VAR_UNKNOWN)
6171 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006172 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006173 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006174 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006175 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006176 init_srand(&x);
6177
6178 gx = SPLITMIX32(x, z);
6179 gy = SPLITMIX32(x, z);
6180 gz = SPLITMIX32(x, z);
6181 gw = SPLITMIX32(x, z);
6182 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006183 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006184
6185 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006186 }
6187 else if (argvars[0].v_type == VAR_LIST)
6188 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006189 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006190 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006191 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006192
6193 lx = list_find(l, 0L);
6194 ly = list_find(l, 1L);
6195 lz = list_find(l, 2L);
6196 lw = list_find(l, 3L);
6197 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6198 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6199 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6200 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6201 x = (UINT32_T)lx->li_tv.vval.v_number;
6202 y = (UINT32_T)ly->li_tv.vval.v_number;
6203 z = (UINT32_T)lz->li_tv.vval.v_number;
6204 w = (UINT32_T)lw->li_tv.vval.v_number;
6205
6206 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6207
6208 lx->li_tv.vval.v_number = (varnumber_T)x;
6209 ly->li_tv.vval.v_number = (varnumber_T)y;
6210 lz->li_tv.vval.v_number = (varnumber_T)z;
6211 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006212 }
6213 else
6214 goto theend;
6215
6216 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006217 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006218 return;
6219
6220theend:
6221 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006222 rettv->v_type = VAR_NUMBER;
6223 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006224}
6225
6226/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006227 * "srand()" function
6228 */
6229 static void
6230f_srand(typval_T *argvars, typval_T *rettv)
6231{
6232 UINT32_T x = 0, z;
6233
6234 if (rettv_list_alloc(rettv) == FAIL)
6235 return;
6236 if (argvars[0].v_type == VAR_UNKNOWN)
6237 {
6238 init_srand(&x);
6239 }
6240 else
6241 {
6242 int error = FALSE;
6243
6244 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6245 if (error)
6246 return;
6247 }
6248
6249 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6250 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6251 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6252 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6253}
6254
6255#undef ROTL
6256#undef SPLITMIX32
6257#undef SHUFFLE_XOSHIRO128STARSTAR
6258
6259/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 * "range()" function
6261 */
6262 static void
6263f_range(typval_T *argvars, typval_T *rettv)
6264{
6265 varnumber_T start;
6266 varnumber_T end;
6267 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 int error = FALSE;
6269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 if (argvars[1].v_type == VAR_UNKNOWN)
6272 {
6273 end = start - 1;
6274 start = 0;
6275 }
6276 else
6277 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006278 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006280 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 }
6282
6283 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006284 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006286 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006288 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006291 list_T *list = rettv->vval.v_list;
6292
6293 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006294 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006295 // be called.
6296 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006297 list->lv_u.nonmat.lv_start = start;
6298 list->lv_u.nonmat.lv_end = end;
6299 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01006300 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 }
6302}
6303
6304/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006305 * Materialize "list".
6306 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 */
6308 void
6309range_list_materialize(list_T *list)
6310{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006311 varnumber_T start = list->lv_u.nonmat.lv_start;
6312 varnumber_T end = list->lv_u.nonmat.lv_end;
6313 int stride = list->lv_u.nonmat.lv_stride;
6314 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006316 list->lv_first = NULL;
6317 list->lv_u.mat.lv_last = NULL;
6318 list->lv_len = 0;
6319 list->lv_u.mat.lv_idx_item = NULL;
6320 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
6321 if (list_append_number(list, (varnumber_T)i) == FAIL)
6322 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006323}
6324
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006325/*
6326 * "getreginfo()" function
6327 */
6328 static void
6329f_getreginfo(typval_T *argvars, typval_T *rettv)
6330{
6331 char_u *strregname;
6332 int regname;
6333 char_u buf[NUMBUFLEN + 2];
6334 long reglen = 0;
6335 dict_T *dict;
6336 list_T *list;
6337
6338 if (argvars[0].v_type != VAR_UNKNOWN)
6339 {
6340 strregname = tv_get_string_chk(&argvars[0]);
6341 if (strregname == NULL)
6342 return;
6343 }
6344 else
6345 strregname = get_vim_var_str(VV_REG);
6346
6347 regname = (strregname == NULL ? '"' : *strregname);
6348 if (regname == 0 || regname == '@')
6349 regname = '"';
6350
6351 if (rettv_dict_alloc(rettv) == FAIL)
6352 return;
6353 dict = rettv->vval.v_dict;
6354
6355 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
6356 if (list == NULL)
6357 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02006358 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006359
6360 buf[0] = NUL;
6361 buf[1] = NUL;
6362 switch (get_reg_type(regname, &reglen))
6363 {
6364 case MLINE: buf[0] = 'V'; break;
6365 case MCHAR: buf[0] = 'v'; break;
6366 case MBLOCK:
6367 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
6368 reglen + 1);
6369 break;
6370 }
Bram Moolenaar91639192020-06-29 19:55:58 +02006371 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006372
6373 buf[0] = get_register_name(get_unname_register());
6374 buf[1] = NUL;
6375 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02006376 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006377 else
6378 {
6379 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
6380
6381 if (item != NULL)
6382 {
6383 item->di_tv.v_type = VAR_SPECIAL;
6384 item->di_tv.vval.v_number = regname == buf[0]
6385 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02006386 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02006387 }
6388 }
6389}
6390
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02006391 static void
6392return_register(int regname, typval_T *rettv)
6393{
6394 char_u buf[2] = {0, 0};
6395
6396 buf[0] = (char_u)regname;
6397 rettv->v_type = VAR_STRING;
6398 rettv->vval.v_string = vim_strsave(buf);
6399}
6400
6401/*
6402 * "reg_executing()" function
6403 */
6404 static void
6405f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
6406{
6407 return_register(reg_executing, rettv);
6408}
6409
6410/*
6411 * "reg_recording()" function
6412 */
6413 static void
6414f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
6415{
6416 return_register(reg_recording, rettv);
6417}
6418
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01006419/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 * "rename({from}, {to})" function
6421 */
6422 static void
6423f_rename(typval_T *argvars, typval_T *rettv)
6424{
6425 char_u buf[NUMBUFLEN];
6426
6427 if (check_restricted() || check_secure())
6428 rettv->vval.v_number = -1;
6429 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006430 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
6431 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432}
6433
6434/*
6435 * "repeat()" function
6436 */
6437 static void
6438f_repeat(typval_T *argvars, typval_T *rettv)
6439{
6440 char_u *p;
6441 int n;
6442 int slen;
6443 int len;
6444 char_u *r;
6445 int i;
6446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006447 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448 if (argvars[0].v_type == VAR_LIST)
6449 {
6450 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
6451 while (n-- > 0)
6452 if (list_extend(rettv->vval.v_list,
6453 argvars[0].vval.v_list, NULL) == FAIL)
6454 break;
6455 }
6456 else
6457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006458 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459 rettv->v_type = VAR_STRING;
6460 rettv->vval.v_string = NULL;
6461
6462 slen = (int)STRLEN(p);
6463 len = slen * n;
6464 if (len <= 0)
6465 return;
6466
6467 r = alloc(len + 1);
6468 if (r != NULL)
6469 {
6470 for (i = 0; i < n; i++)
6471 mch_memmove(r + i * slen, p, (size_t)slen);
6472 r[len] = NUL;
6473 }
6474
6475 rettv->vval.v_string = r;
6476 }
6477}
6478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006479#define SP_NOMOVE 0x01 // don't move cursor
6480#define SP_REPEAT 0x02 // repeat to find outer pair
6481#define SP_RETCOUNT 0x04 // return matchcount
6482#define SP_SETPCMARK 0x08 // set previous context mark
6483#define SP_START 0x10 // accept match at start position
6484#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
6485#define SP_END 0x40 // leave cursor at end of match
6486#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488/*
6489 * Get flags for a search function.
6490 * Possibly sets "p_ws".
6491 * Returns BACKWARD, FORWARD or zero (for an error).
6492 */
6493 static int
6494get_search_arg(typval_T *varp, int *flagsp)
6495{
6496 int dir = FORWARD;
6497 char_u *flags;
6498 char_u nbuf[NUMBUFLEN];
6499 int mask;
6500
6501 if (varp->v_type != VAR_UNKNOWN)
6502 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006503 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006505 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 while (*flags != NUL)
6507 {
6508 switch (*flags)
6509 {
6510 case 'b': dir = BACKWARD; break;
6511 case 'w': p_ws = TRUE; break;
6512 case 'W': p_ws = FALSE; break;
6513 default: mask = 0;
6514 if (flagsp != NULL)
6515 switch (*flags)
6516 {
6517 case 'c': mask = SP_START; break;
6518 case 'e': mask = SP_END; break;
6519 case 'm': mask = SP_RETCOUNT; break;
6520 case 'n': mask = SP_NOMOVE; break;
6521 case 'p': mask = SP_SUBPAT; break;
6522 case 'r': mask = SP_REPEAT; break;
6523 case 's': mask = SP_SETPCMARK; break;
6524 case 'z': mask = SP_COLUMN; break;
6525 }
6526 if (mask == 0)
6527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006528 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 dir = 0;
6530 }
6531 else
6532 *flagsp |= mask;
6533 }
6534 if (dir == 0)
6535 break;
6536 ++flags;
6537 }
6538 }
6539 return dir;
6540}
6541
6542/*
6543 * Shared by search() and searchpos() functions.
6544 */
6545 static int
6546search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
6547{
6548 int flags;
6549 char_u *pat;
6550 pos_T pos;
6551 pos_T save_cursor;
6552 int save_p_ws = p_ws;
6553 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006554 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006555 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006557 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558 long time_limit = 0;
6559#endif
6560 int options = SEARCH_KEEP;
6561 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006562 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006563 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006564 pos_T firstpos;
6565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006566 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568 if (dir == 0)
6569 goto theend;
6570 flags = *flagsp;
6571 if (flags & SP_START)
6572 options |= SEARCH_START;
6573 if (flags & SP_END)
6574 options |= SEARCH_END;
6575 if (flags & SP_COLUMN)
6576 options |= SEARCH_COL;
6577
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006578 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
6580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006581 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 if (lnum_stop < 0)
6583 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 if (argvars[3].v_type != VAR_UNKNOWN)
6585 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006586#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006587 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588 if (time_limit < 0)
6589 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02006591 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593 }
6594
6595#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006596 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597 profile_setlimit(time_limit, &tm);
6598#endif
6599
6600 /*
6601 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
6602 * Check to make sure only those flags are set.
6603 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
6604 * flags cannot be set. Check for that condition also.
6605 */
6606 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
6607 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006609 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610 goto theend;
6611 }
6612
6613 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006614 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02006615 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006616 sia.sa_stop_lnum = (linenr_T)lnum_stop;
6617#ifdef FEAT_RELTIME
6618 sia.sa_tm = &tm;
6619#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006620
6621 // Repeat until {skip} returns FALSE.
6622 for (;;)
6623 {
6624 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02006625 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006626 // finding the first match again means there is no match where {skip}
6627 // evaluates to zero.
6628 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
6629 subpatnum = FAIL;
6630
Bram Moolenaara9c01042020-06-07 14:50:50 +02006631 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006632 // didn't find it or no skip argument
6633 break;
6634 firstpos = pos;
6635
Bram Moolenaara9c01042020-06-07 14:50:50 +02006636 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006637 {
6638 int do_skip;
6639 int err;
6640 pos_T save_pos = curwin->w_cursor;
6641
6642 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02006643 err = FALSE;
6644 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02006645 curwin->w_cursor = save_pos;
6646 if (err)
6647 {
6648 // Evaluating {skip} caused an error, break here.
6649 subpatnum = FAIL;
6650 break;
6651 }
6652 if (!do_skip)
6653 break;
6654 }
6655 }
6656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657 if (subpatnum != FAIL)
6658 {
6659 if (flags & SP_SUBPAT)
6660 retval = subpatnum;
6661 else
6662 retval = pos.lnum;
6663 if (flags & SP_SETPCMARK)
6664 setpcmark();
6665 curwin->w_cursor = pos;
6666 if (match_pos != NULL)
6667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006668 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 match_pos->lnum = pos.lnum;
6670 match_pos->col = pos.col + 1;
6671 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006672 // "/$" will put the cursor after the end of the line, may need to
6673 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674 check_cursor();
6675 }
6676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006677 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 if (flags & SP_NOMOVE)
6679 curwin->w_cursor = save_cursor;
6680 else
6681 curwin->w_set_curswant = TRUE;
6682theend:
6683 p_ws = save_p_ws;
6684
6685 return retval;
6686}
6687
6688#ifdef FEAT_FLOAT
6689
6690/*
6691 * round() is not in C90, use ceil() or floor() instead.
6692 */
6693 float_T
6694vim_round(float_T f)
6695{
6696 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
6697}
6698
6699/*
6700 * "round({float})" function
6701 */
6702 static void
6703f_round(typval_T *argvars, typval_T *rettv)
6704{
6705 float_T f = 0.0;
6706
6707 rettv->v_type = VAR_FLOAT;
6708 if (get_float_arg(argvars, &f) == OK)
6709 rettv->vval.v_float = vim_round(f);
6710 else
6711 rettv->vval.v_float = 0.0;
6712}
6713#endif
6714
Bram Moolenaare99be0e2019-03-26 22:51:09 +01006715#ifdef FEAT_RUBY
6716/*
6717 * "rubyeval()" function
6718 */
6719 static void
6720f_rubyeval(typval_T *argvars, typval_T *rettv)
6721{
6722 char_u *str;
6723 char_u buf[NUMBUFLEN];
6724
6725 str = tv_get_string_buf(&argvars[0], buf);
6726 do_rubyeval(str, rettv);
6727}
6728#endif
6729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730/*
6731 * "screenattr()" function
6732 */
6733 static void
6734f_screenattr(typval_T *argvars, typval_T *rettv)
6735{
6736 int row;
6737 int col;
6738 int c;
6739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006740 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6741 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 if (row < 0 || row >= screen_Rows
6743 || col < 0 || col >= screen_Columns)
6744 c = -1;
6745 else
6746 c = ScreenAttrs[LineOffset[row] + col];
6747 rettv->vval.v_number = c;
6748}
6749
6750/*
6751 * "screenchar()" function
6752 */
6753 static void
6754f_screenchar(typval_T *argvars, typval_T *rettv)
6755{
6756 int row;
6757 int col;
6758 int off;
6759 int c;
6760
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006761 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6762 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006763 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764 c = -1;
6765 else
6766 {
6767 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 if (enc_utf8 && ScreenLinesUC[off] != 0)
6769 c = ScreenLinesUC[off];
6770 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771 c = ScreenLines[off];
6772 }
6773 rettv->vval.v_number = c;
6774}
6775
6776/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006777 * "screenchars()" function
6778 */
6779 static void
6780f_screenchars(typval_T *argvars, typval_T *rettv)
6781{
6782 int row;
6783 int col;
6784 int off;
6785 int c;
6786 int i;
6787
6788 if (rettv_list_alloc(rettv) == FAIL)
6789 return;
6790 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6791 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6792 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6793 return;
6794
6795 off = LineOffset[row] + col;
6796 if (enc_utf8 && ScreenLinesUC[off] != 0)
6797 c = ScreenLinesUC[off];
6798 else
6799 c = ScreenLines[off];
6800 list_append_number(rettv->vval.v_list, (varnumber_T)c);
6801
6802 if (enc_utf8)
6803
6804 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6805 list_append_number(rettv->vval.v_list,
6806 (varnumber_T)ScreenLinesC[i][off]);
6807}
6808
6809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 * "screencol()" function
6811 *
6812 * First column is 1 to be consistent with virtcol().
6813 */
6814 static void
6815f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
6816{
6817 rettv->vval.v_number = screen_screencol() + 1;
6818}
6819
6820/*
6821 * "screenrow()" function
6822 */
6823 static void
6824f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
6825{
6826 rettv->vval.v_number = screen_screenrow() + 1;
6827}
6828
6829/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01006830 * "screenstring()" function
6831 */
6832 static void
6833f_screenstring(typval_T *argvars, typval_T *rettv)
6834{
6835 int row;
6836 int col;
6837 int off;
6838 int c;
6839 int i;
6840 char_u buf[MB_MAXBYTES + 1];
6841 int buflen = 0;
6842
6843 rettv->vval.v_string = NULL;
6844 rettv->v_type = VAR_STRING;
6845
6846 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
6847 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
6848 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
6849 return;
6850
6851 off = LineOffset[row] + col;
6852 if (enc_utf8 && ScreenLinesUC[off] != 0)
6853 c = ScreenLinesUC[off];
6854 else
6855 c = ScreenLines[off];
6856 buflen += mb_char2bytes(c, buf);
6857
6858 if (enc_utf8)
6859 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
6860 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
6861
6862 buf[buflen] = NUL;
6863 rettv->vval.v_string = vim_strsave(buf);
6864}
6865
6866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867 * "search()" function
6868 */
6869 static void
6870f_search(typval_T *argvars, typval_T *rettv)
6871{
6872 int flags = 0;
6873
6874 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
6875}
6876
6877/*
6878 * "searchdecl()" function
6879 */
6880 static void
6881f_searchdecl(typval_T *argvars, typval_T *rettv)
6882{
6883 int locally = 1;
6884 int thisblock = 0;
6885 int error = FALSE;
6886 char_u *name;
6887
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006888 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006890 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 if (argvars[1].v_type != VAR_UNKNOWN)
6892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006893 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006895 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 }
6897 if (!error && name != NULL)
6898 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
6899 locally, thisblock, SEARCH_KEEP) == FAIL;
6900}
6901
6902/*
6903 * Used by searchpair() and searchpairpos()
6904 */
6905 static int
6906searchpair_cmn(typval_T *argvars, pos_T *match_pos)
6907{
6908 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01006909 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910 int save_p_ws = p_ws;
6911 int dir;
6912 int flags = 0;
6913 char_u nbuf1[NUMBUFLEN];
6914 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006915 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 long lnum_stop = 0;
6917 long time_limit = 0;
6918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006919 // Get the three pattern arguments: start, middle, end. Will result in an
6920 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006921 spat = tv_get_string_chk(&argvars[0]);
6922 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
6923 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006924 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006925 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006927 // Handle the optional fourth argument: flags
6928 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 if (dir == 0)
6930 goto theend;
6931
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006932 // Don't accept SP_END or SP_SUBPAT.
6933 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 if ((flags & (SP_END | SP_SUBPAT)) != 0
6935 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
6936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006937 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 goto theend;
6939 }
6940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006941 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (flags & SP_REPEAT)
6943 p_ws = FALSE;
6944
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006945 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 if (argvars[3].v_type == VAR_UNKNOWN
6947 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01006948 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949 else
6950 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02006951 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01006952 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02006953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006954 if (argvars[5].v_type != VAR_UNKNOWN)
6955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006959 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006960 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962#ifdef FEAT_RELTIME
6963 if (argvars[6].v_type != VAR_UNKNOWN)
6964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006965 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006968 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02006970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971 }
6972#endif
6973 }
6974 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006975
6976 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
6977 match_pos, lnum_stop, time_limit);
6978
6979theend:
6980 p_ws = save_p_ws;
6981
6982 return retval;
6983}
6984
6985/*
6986 * "searchpair()" function
6987 */
6988 static void
6989f_searchpair(typval_T *argvars, typval_T *rettv)
6990{
6991 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
6992}
6993
6994/*
6995 * "searchpairpos()" function
6996 */
6997 static void
6998f_searchpairpos(typval_T *argvars, typval_T *rettv)
6999{
7000 pos_T match_pos;
7001 int lnum = 0;
7002 int col = 0;
7003
7004 if (rettv_list_alloc(rettv) == FAIL)
7005 return;
7006
7007 if (searchpair_cmn(argvars, &match_pos) > 0)
7008 {
7009 lnum = match_pos.lnum;
7010 col = match_pos.col;
7011 }
7012
7013 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7014 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7015}
7016
7017/*
7018 * Search for a start/middle/end thing.
7019 * Used by searchpair(), see its documentation for the details.
7020 * Returns 0 or -1 for no match,
7021 */
7022 long
7023do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007024 char_u *spat, // start pattern
7025 char_u *mpat, // middle pattern
7026 char_u *epat, // end pattern
7027 int dir, // BACKWARD or FORWARD
7028 typval_T *skip, // skip expression
7029 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007031 linenr_T lnum_stop, // stop at this line if not zero
7032 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033{
7034 char_u *save_cpo;
7035 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7036 long retval = 0;
7037 pos_T pos;
7038 pos_T firstpos;
7039 pos_T foundpos;
7040 pos_T save_cursor;
7041 pos_T save_pos;
7042 int n;
7043 int r;
7044 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007045 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 int err;
7047 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007048#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007050#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007052 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 save_cpo = p_cpo;
7054 p_cpo = empty_option;
7055
7056#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007057 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 profile_setlimit(time_limit, &tm);
7059#endif
7060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007061 // Make two search patterns: start/end (pat2, for in nested pairs) and
7062 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007063 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7064 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065 if (pat2 == NULL || pat3 == NULL)
7066 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007067 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007068 if (*mpat == NUL)
7069 STRCPY(pat3, pat2);
7070 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007071 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007072 spat, epat, mpat);
7073 if (flags & SP_START)
7074 options |= SEARCH_START;
7075
Bram Moolenaar48570482017-10-30 21:48:41 +01007076 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007077 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007079 save_cursor = curwin->w_cursor;
7080 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007081 CLEAR_POS(&firstpos);
7082 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 pat = pat3;
7084 for (;;)
7085 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007086 searchit_arg_T sia;
7087
Bram Moolenaara80faa82020-04-12 19:37:17 +02007088 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007089 sia.sa_stop_lnum = lnum_stop;
7090#ifdef FEAT_RELTIME
7091 sia.sa_tm = &tm;
7092#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007093 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007094 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007095 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007096 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097 break;
7098
7099 if (firstpos.lnum == 0)
7100 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007101 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007103 // Found the same position again. Can happen with a pattern that
7104 // has "\zs" at the end and searching backwards. Advance one
7105 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 if (dir == BACKWARD)
7107 decl(&pos);
7108 else
7109 incl(&pos);
7110 }
7111 foundpos = pos;
7112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007113 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 options &= ~SEARCH_START;
7115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007116 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007117 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 {
7119 save_pos = curwin->w_cursor;
7120 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007121 err = FALSE;
7122 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 curwin->w_cursor = save_pos;
7124 if (err)
7125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007126 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007127 curwin->w_cursor = save_cursor;
7128 retval = -1;
7129 break;
7130 }
7131 if (r)
7132 continue;
7133 }
7134
7135 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007137 // Found end when searching backwards or start when searching
7138 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007140 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007141 }
7142 else
7143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007144 // Found end when searching forward or start when searching
7145 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007147 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148 }
7149
7150 if (nest == 0)
7151 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007152 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007153 if (flags & SP_RETCOUNT)
7154 ++retval;
7155 else
7156 retval = pos.lnum;
7157 if (flags & SP_SETPCMARK)
7158 setpcmark();
7159 curwin->w_cursor = pos;
7160 if (!(flags & SP_REPEAT))
7161 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007162 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 }
7164 }
7165
7166 if (match_pos != NULL)
7167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007168 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007169 match_pos->lnum = curwin->w_cursor.lnum;
7170 match_pos->col = curwin->w_cursor.col + 1;
7171 }
7172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007173 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 if ((flags & SP_NOMOVE) || retval == 0)
7175 curwin->w_cursor = save_cursor;
7176
7177theend:
7178 vim_free(pat2);
7179 vim_free(pat3);
7180 if (p_cpo == empty_option)
7181 p_cpo = save_cpo;
7182 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007183 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 free_string_option(save_cpo);
7185
7186 return retval;
7187}
7188
7189/*
7190 * "searchpos()" function
7191 */
7192 static void
7193f_searchpos(typval_T *argvars, typval_T *rettv)
7194{
7195 pos_T match_pos;
7196 int lnum = 0;
7197 int col = 0;
7198 int n;
7199 int flags = 0;
7200
7201 if (rettv_list_alloc(rettv) == FAIL)
7202 return;
7203
7204 n = search_cmn(argvars, &match_pos, &flags);
7205 if (n > 0)
7206 {
7207 lnum = match_pos.lnum;
7208 col = match_pos.col;
7209 }
7210
7211 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7212 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7213 if (flags & SP_SUBPAT)
7214 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7215}
7216
7217 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7219{
7220 dict_T *d;
7221 dictitem_T *di;
7222 char_u *csearch;
7223
7224 if (argvars[0].v_type != VAR_DICT)
7225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007226 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227 return;
7228 }
7229
7230 if ((d = argvars[0].vval.v_dict) != NULL)
7231 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01007232 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 if (csearch != NULL)
7234 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 if (enc_utf8)
7236 {
7237 int pcc[MAX_MCO];
7238 int c = utfc_ptr2char(csearch, pcc);
7239
7240 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
7241 }
7242 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007243 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02007244 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 }
7246
7247 di = dict_find(d, (char_u *)"forward", -1);
7248 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007249 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250 ? FORWARD : BACKWARD);
7251
7252 di = dict_find(d, (char_u *)"until", -1);
7253 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007254 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007255 }
7256}
7257
7258/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02007259 * "setenv()" function
7260 */
7261 static void
7262f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
7263{
7264 char_u namebuf[NUMBUFLEN];
7265 char_u valbuf[NUMBUFLEN];
7266 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
7267
7268 if (argvars[1].v_type == VAR_SPECIAL
7269 && argvars[1].vval.v_number == VVAL_NULL)
7270 vim_unsetenv(name);
7271 else
7272 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
7273}
7274
7275/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 * "setfperm({fname}, {mode})" function
7277 */
7278 static void
7279f_setfperm(typval_T *argvars, typval_T *rettv)
7280{
7281 char_u *fname;
7282 char_u modebuf[NUMBUFLEN];
7283 char_u *mode_str;
7284 int i;
7285 int mask;
7286 int mode = 0;
7287
7288 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 if (fname == NULL)
7291 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007292 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 if (mode_str == NULL)
7294 return;
7295 if (STRLEN(mode_str) != 9)
7296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007297 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 return;
7299 }
7300
7301 mask = 1;
7302 for (i = 8; i >= 0; --i)
7303 {
7304 if (mode_str[i] != '-')
7305 mode |= mask;
7306 mask = mask << 1;
7307 }
7308 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
7309}
7310
7311/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007312 * "setpos()" function
7313 */
7314 static void
7315f_setpos(typval_T *argvars, typval_T *rettv)
7316{
7317 pos_T pos;
7318 int fnum;
7319 char_u *name;
7320 colnr_T curswant = -1;
7321
7322 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007323 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324 if (name != NULL)
7325 {
7326 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
7327 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01007328 if (pos.col != MAXCOL && --pos.col < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 pos.col = 0;
7330 if (name[0] == '.' && name[1] == NUL)
7331 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007332 // set cursor; "fnum" is ignored
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007333 curwin->w_cursor = pos;
7334 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007335 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007336 curwin->w_curswant = curswant - 1;
7337 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +01007339 check_cursor();
7340 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 }
7342 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007344 // set mark
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 if (setmark_pos(name[1], &pos, fnum) == OK)
7346 rettv->vval.v_number = 0;
7347 }
7348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 }
7351 }
7352}
7353
7354/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007355 * Translate a register type string to the yank type and block length
7356 */
7357 static int
7358get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
7359{
7360 char_u *stropt = *pp;
7361 switch (*stropt)
7362 {
7363 case 'v': case 'c': // character-wise selection
7364 *yank_type = MCHAR;
7365 break;
7366 case 'V': case 'l': // line-wise selection
7367 *yank_type = MLINE;
7368 break;
7369 case 'b': case Ctrl_V: // block-wise selection
7370 *yank_type = MBLOCK;
7371 if (VIM_ISDIGIT(stropt[1]))
7372 {
7373 ++stropt;
7374 *block_len = getdigits(&stropt) - 1;
7375 --stropt;
7376 }
7377 break;
7378 default:
7379 return FAIL;
7380 }
7381 *pp = stropt;
7382 return OK;
7383}
7384
7385/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 * "setreg()" function
7387 */
7388 static void
7389f_setreg(typval_T *argvars, typval_T *rettv)
7390{
7391 int regname;
7392 char_u *strregname;
7393 char_u *stropt;
7394 char_u *strval;
7395 int append;
7396 char_u yank_type;
7397 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007398 typval_T *regcontents;
7399 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007401 pointreg = 0;
7402 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007403 block_len = -1;
7404 yank_type = MAUTO;
7405 append = FALSE;
7406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007407 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007408 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007409
7410 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007411 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 regname = *strregname;
7413 if (regname == 0 || regname == '@')
7414 regname = '"';
7415
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007416 if (argvars[1].v_type == VAR_DICT)
7417 {
7418 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007419 dictitem_T *di;
7420
7421 if (d == NULL || d->dv_hashtab.ht_used == 0)
7422 {
7423 // Empty dict, clear the register (like setreg(0, []))
7424 char_u *lstval[2] = {NULL, NULL};
7425 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
7426 return;
7427 }
7428
7429 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007430 if (di != NULL)
7431 regcontents = &di->di_tv;
7432
7433 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
7434 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007435 {
7436 int ret = get_yank_type(&stropt, &yank_type, &block_len);
7437
7438 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007439 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007440 semsg(_(e_invargval), "value");
7441 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007442 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007443 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007444
7445 if (regname == '"')
7446 {
7447 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
7448 if (stropt != NULL)
7449 {
7450 pointreg = *stropt;
7451 regname = pointreg;
7452 }
7453 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02007454 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007455 pointreg = regname;
7456 }
7457 else
7458 regcontents = &argvars[1];
7459
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460 if (argvars[2].v_type != VAR_UNKNOWN)
7461 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007462 if (yank_type != MAUTO)
7463 {
7464 semsg(_(e_toomanyarg), "setreg");
7465 return;
7466 }
7467
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007468 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007469 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007470 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 for (; *stropt != NUL; ++stropt)
7472 switch (*stropt)
7473 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007474 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 append = TRUE;
7476 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02007477 default:
7478 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 }
7480 }
7481
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007482 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 {
7484 char_u **lstval;
7485 char_u **allocval;
7486 char_u buf[NUMBUFLEN];
7487 char_u **curval;
7488 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007489 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490 listitem_T *li;
7491 int len;
7492
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007493 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007494 len = ll == NULL ? 0 : ll->lv_len;
7495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007496 // First half: use for pointers to result lines; second half: use for
7497 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007498 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 if (lstval == NULL)
7500 return;
7501 curval = lstval;
7502 allocval = lstval + len + 2;
7503 curallocval = allocval;
7504
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007505 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007507 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007508 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007509 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007510 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 if (strval == NULL)
7512 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007513 if (strval == buf)
7514 {
7515 // Need to make a copy, next tv_get_string_buf_chk() will
7516 // overwrite the string.
7517 strval = vim_strsave(buf);
7518 if (strval == NULL)
7519 goto free_lstval;
7520 *curallocval++ = strval;
7521 }
7522 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 }
7525 *curval++ = NULL;
7526
7527 write_reg_contents_lst(regname, lstval, -1,
7528 append, yank_type, block_len);
7529free_lstval:
7530 while (curallocval > allocval)
7531 vim_free(*--curallocval);
7532 vim_free(lstval);
7533 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007534 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007536 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007537 if (strval == NULL)
7538 return;
7539 write_reg_contents_ex(regname, strval, -1,
7540 append, yank_type, block_len);
7541 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007542 if (pointreg != 0)
7543 get_yank_register(pointreg, TRUE);
7544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007545 rettv->vval.v_number = 0;
7546}
7547
7548/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007549 * "settagstack()" function
7550 */
7551 static void
7552f_settagstack(typval_T *argvars, typval_T *rettv)
7553{
7554 static char *e_invact2 = N_("E962: Invalid action: '%s'");
7555 win_T *wp;
7556 dict_T *d;
7557 int action = 'r';
7558
7559 rettv->vval.v_number = -1;
7560
7561 // first argument: window number or id
7562 wp = find_win_by_nr_or_id(&argvars[0]);
7563 if (wp == NULL)
7564 return;
7565
7566 // second argument: dict with items to set in the tag stack
7567 if (argvars[1].v_type != VAR_DICT)
7568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007569 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007570 return;
7571 }
7572 d = argvars[1].vval.v_dict;
7573 if (d == NULL)
7574 return;
7575
7576 // third argument: action - 'a' for append and 'r' for replace.
7577 // default is to replace the stack.
7578 if (argvars[2].v_type == VAR_UNKNOWN)
7579 action = 'r';
7580 else if (argvars[2].v_type == VAR_STRING)
7581 {
7582 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007583 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007584 if (actstr == NULL)
7585 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01007586 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
7587 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007588 action = *actstr;
7589 else
7590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007592 return;
7593 }
7594 }
7595 else
7596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01007598 return;
7599 }
7600
7601 if (set_tagstack(wp, d, action) == OK)
7602 rettv->vval.v_number = 0;
7603}
7604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605#ifdef FEAT_CRYPT
7606/*
7607 * "sha256({string})" function
7608 */
7609 static void
7610f_sha256(typval_T *argvars, typval_T *rettv)
7611{
7612 char_u *p;
7613
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007614 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007615 rettv->vval.v_string = vim_strsave(
7616 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
7617 rettv->v_type = VAR_STRING;
7618}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007619#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620
7621/*
7622 * "shellescape({string})" function
7623 */
7624 static void
7625f_shellescape(typval_T *argvars, typval_T *rettv)
7626{
Bram Moolenaar20615522017-06-05 18:46:26 +02007627 int do_special = non_zero_arg(&argvars[1]);
7628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007630 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 rettv->v_type = VAR_STRING;
7632}
7633
7634/*
7635 * shiftwidth() function
7636 */
7637 static void
7638f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
7639{
Bram Moolenaarf9514162018-11-22 03:08:29 +01007640 rettv->vval.v_number = 0;
7641
7642 if (argvars[0].v_type != VAR_UNKNOWN)
7643 {
7644 long col;
7645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007647 if (col < 0)
7648 return; // type error; errmsg already given
7649#ifdef FEAT_VARTABS
7650 rettv->vval.v_number = get_sw_value_col(curbuf, col);
7651 return;
7652#endif
7653 }
7654
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 rettv->vval.v_number = get_sw_value(curbuf);
7656}
7657
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658#ifdef FEAT_FLOAT
7659/*
7660 * "sin()" function
7661 */
7662 static void
7663f_sin(typval_T *argvars, typval_T *rettv)
7664{
7665 float_T f = 0.0;
7666
7667 rettv->v_type = VAR_FLOAT;
7668 if (get_float_arg(argvars, &f) == OK)
7669 rettv->vval.v_float = sin(f);
7670 else
7671 rettv->vval.v_float = 0.0;
7672}
7673
7674/*
7675 * "sinh()" function
7676 */
7677 static void
7678f_sinh(typval_T *argvars, typval_T *rettv)
7679{
7680 float_T f = 0.0;
7681
7682 rettv->v_type = VAR_FLOAT;
7683 if (get_float_arg(argvars, &f) == OK)
7684 rettv->vval.v_float = sinh(f);
7685 else
7686 rettv->vval.v_float = 0.0;
7687}
7688#endif
7689
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690/*
7691 * "soundfold({word})" function
7692 */
7693 static void
7694f_soundfold(typval_T *argvars, typval_T *rettv)
7695{
7696 char_u *s;
7697
7698 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007699 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700#ifdef FEAT_SPELL
7701 rettv->vval.v_string = eval_soundfold(s);
7702#else
7703 rettv->vval.v_string = vim_strsave(s);
7704#endif
7705}
7706
7707/*
7708 * "spellbadword()" function
7709 */
7710 static void
7711f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
7712{
7713 char_u *word = (char_u *)"";
7714 hlf_T attr = HLF_COUNT;
7715 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007716#ifdef FEAT_SPELL
7717 int wo_spell_save = curwin->w_p_spell;
7718
7719 if (!curwin->w_p_spell)
7720 {
7721 did_set_spelllang(curwin);
7722 curwin->w_p_spell = TRUE;
7723 }
7724
7725 if (*curwin->w_s->b_p_spl == NUL)
7726 {
7727 emsg(_(e_no_spell));
7728 curwin->w_p_spell = wo_spell_save;
7729 return;
7730 }
7731#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732
7733 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007734 {
7735#ifdef FEAT_SPELL
7736 curwin->w_p_spell = wo_spell_save;
7737#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007740
7741#ifdef FEAT_SPELL
7742 if (argvars[0].v_type == VAR_UNKNOWN)
7743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007744 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
7746 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007747 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01007749 curwin->w_set_curswant = TRUE;
7750 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007752 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007754 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007755 int capcol = -1;
7756
7757 if (str != NULL)
7758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007759 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 while (*str != NUL)
7761 {
7762 len = spell_check(curwin, str, &attr, &capcol, FALSE);
7763 if (attr != HLF_COUNT)
7764 {
7765 word = str;
7766 break;
7767 }
7768 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02007769 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02007770 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771 }
7772 }
7773 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007774 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775#endif
7776
7777 list_append_string(rettv->vval.v_list, word, len);
7778 list_append_string(rettv->vval.v_list, (char_u *)(
7779 attr == HLF_SPB ? "bad" :
7780 attr == HLF_SPR ? "rare" :
7781 attr == HLF_SPL ? "local" :
7782 attr == HLF_SPC ? "caps" :
7783 ""), -1);
7784}
7785
7786/*
7787 * "spellsuggest()" function
7788 */
7789 static void
7790f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
7791{
7792#ifdef FEAT_SPELL
7793 char_u *str;
7794 int typeerr = FALSE;
7795 int maxcount;
7796 garray_T ga;
7797 int i;
7798 listitem_T *li;
7799 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007800 int wo_spell_save = curwin->w_p_spell;
7801
7802 if (!curwin->w_p_spell)
7803 {
7804 did_set_spelllang(curwin);
7805 curwin->w_p_spell = TRUE;
7806 }
7807
7808 if (*curwin->w_s->b_p_spl == NUL)
7809 {
7810 emsg(_(e_no_spell));
7811 curwin->w_p_spell = wo_spell_save;
7812 return;
7813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814#endif
7815
7816 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007817 {
7818#ifdef FEAT_SPELL
7819 curwin->w_p_spell = wo_spell_save;
7820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007822 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823
7824#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007825 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007827 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 if (argvars[1].v_type != VAR_UNKNOWN)
7829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007830 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 if (maxcount <= 0)
7832 return;
7833 if (argvars[2].v_type != VAR_UNKNOWN)
7834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007835 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 if (typeerr)
7837 return;
7838 }
7839 }
7840 else
7841 maxcount = 25;
7842
7843 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
7844
7845 for (i = 0; i < ga.ga_len; ++i)
7846 {
7847 str = ((char_u **)ga.ga_data)[i];
7848
7849 li = listitem_alloc();
7850 if (li == NULL)
7851 vim_free(str);
7852 else
7853 {
7854 li->li_tv.v_type = VAR_STRING;
7855 li->li_tv.v_lock = 0;
7856 li->li_tv.vval.v_string = str;
7857 list_append(rettv->vval.v_list, li);
7858 }
7859 }
7860 ga_clear(&ga);
7861 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02007862 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863#endif
7864}
7865
7866 static void
7867f_split(typval_T *argvars, typval_T *rettv)
7868{
7869 char_u *str;
7870 char_u *end;
7871 char_u *pat = NULL;
7872 regmatch_T regmatch;
7873 char_u patbuf[NUMBUFLEN];
7874 char_u *save_cpo;
7875 int match;
7876 colnr_T col = 0;
7877 int keepempty = FALSE;
7878 int typeerr = FALSE;
7879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007880 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 save_cpo = p_cpo;
7882 p_cpo = (char_u *)"";
7883
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007884 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 if (argvars[1].v_type != VAR_UNKNOWN)
7886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007887 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 if (pat == NULL)
7889 typeerr = TRUE;
7890 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007891 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 }
7893 if (pat == NULL || *pat == NUL)
7894 pat = (char_u *)"[\\x01- ]\\+";
7895
7896 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007897 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007899 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900
7901 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7902 if (regmatch.regprog != NULL)
7903 {
7904 regmatch.rm_ic = FALSE;
7905 while (*str != NUL || keepempty)
7906 {
7907 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007908 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909 else
7910 match = vim_regexec_nl(&regmatch, str, col);
7911 if (match)
7912 end = regmatch.startp[0];
7913 else
7914 end = str + STRLEN(str);
7915 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
7916 && *str != NUL && match && end < regmatch.endp[0]))
7917 {
7918 if (list_append_string(rettv->vval.v_list, str,
7919 (int)(end - str)) == FAIL)
7920 break;
7921 }
7922 if (!match)
7923 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01007924 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if (regmatch.endp[0] > str)
7926 col = 0;
7927 else
Bram Moolenaar13505972019-01-24 15:04:48 +01007928 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 str = regmatch.endp[0];
7931 }
7932
7933 vim_regfree(regmatch.regprog);
7934 }
7935
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02007936theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937 p_cpo = save_cpo;
7938}
7939
7940#ifdef FEAT_FLOAT
7941/*
7942 * "sqrt()" function
7943 */
7944 static void
7945f_sqrt(typval_T *argvars, typval_T *rettv)
7946{
7947 float_T f = 0.0;
7948
7949 rettv->v_type = VAR_FLOAT;
7950 if (get_float_arg(argvars, &f) == OK)
7951 rettv->vval.v_float = sqrt(f);
7952 else
7953 rettv->vval.v_float = 0.0;
7954}
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007955#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007956
Bram Moolenaar0387cae2019-11-29 21:07:58 +01007957#ifdef FEAT_FLOAT
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01007958/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 * "str2float()" function
7960 */
7961 static void
7962f_str2float(typval_T *argvars, typval_T *rettv)
7963{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007964 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01007965 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966
Bram Moolenaar08243d22017-01-10 16:12:29 +01007967 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968 p = skipwhite(p + 1);
7969 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +01007970 if (isneg)
7971 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 rettv->v_type = VAR_FLOAT;
7973}
7974#endif
7975
7976/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007977 * "str2list()" function
7978 */
7979 static void
7980f_str2list(typval_T *argvars, typval_T *rettv)
7981{
7982 char_u *p;
7983 int utf8 = FALSE;
7984
7985 if (rettv_list_alloc(rettv) == FAIL)
7986 return;
7987
7988 if (argvars[1].v_type != VAR_UNKNOWN)
7989 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7990
7991 p = tv_get_string(&argvars[0]);
7992
7993 if (has_mbyte || utf8)
7994 {
7995 int (*ptr2len)(char_u *);
7996 int (*ptr2char)(char_u *);
7997
7998 if (utf8 || enc_utf8)
7999 {
8000 ptr2len = utf_ptr2len;
8001 ptr2char = utf_ptr2char;
8002 }
8003 else
8004 {
8005 ptr2len = mb_ptr2len;
8006 ptr2char = mb_ptr2char;
8007 }
8008
8009 for ( ; *p != NUL; p += (*ptr2len)(p))
8010 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8011 }
8012 else
8013 for ( ; *p != NUL; ++p)
8014 list_append_number(rettv->vval.v_list, *p);
8015}
8016
8017/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 * "str2nr()" function
8019 */
8020 static void
8021f_str2nr(typval_T *argvars, typval_T *rettv)
8022{
8023 int base = 10;
8024 char_u *p;
8025 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008026 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008027 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028
8029 if (argvars[1].v_type != VAR_UNKNOWN)
8030 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008031 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 if (base != 2 && base != 8 && base != 10 && base != 16)
8033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008034 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 return;
8036 }
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008037 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_number(&argvars[2]))
8038 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008039 }
8040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008041 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008042 isneg = (*p == '-');
8043 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 p = skipwhite(p + 1);
8045 switch (base)
8046 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008047 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008048 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008049 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008051 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8052 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008053 if (isneg)
8054 rettv->vval.v_number = -n;
8055 else
8056 rettv->vval.v_number = n;
8057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058}
8059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060/*
8061 * "strgetchar()" function
8062 */
8063 static void
8064f_strgetchar(typval_T *argvars, typval_T *rettv)
8065{
8066 char_u *str;
8067 int len;
8068 int error = FALSE;
8069 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008070 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071
8072 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008073 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 if (str == NULL)
8075 return;
8076 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008077 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078 if (error)
8079 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008080
Bram Moolenaar13505972019-01-24 15:04:48 +01008081 while (charidx >= 0 && byteidx < len)
8082 {
8083 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008085 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8086 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008088 --charidx;
8089 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091}
8092
8093/*
8094 * "stridx()" function
8095 */
8096 static void
8097f_stridx(typval_T *argvars, typval_T *rettv)
8098{
8099 char_u buf[NUMBUFLEN];
8100 char_u *needle;
8101 char_u *haystack;
8102 char_u *save_haystack;
8103 char_u *pos;
8104 int start_idx;
8105
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008106 needle = tv_get_string_chk(&argvars[1]);
8107 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 rettv->vval.v_number = -1;
8109 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008110 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111
8112 if (argvars[2].v_type != VAR_UNKNOWN)
8113 {
8114 int error = FALSE;
8115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008116 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 if (error || start_idx >= (int)STRLEN(haystack))
8118 return;
8119 if (start_idx >= 0)
8120 haystack += start_idx;
8121 }
8122
8123 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8124 if (pos != NULL)
8125 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8126}
8127
8128/*
8129 * "string()" function
8130 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008131 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132f_string(typval_T *argvars, typval_T *rettv)
8133{
8134 char_u *tofree;
8135 char_u numbuf[NUMBUFLEN];
8136
8137 rettv->v_type = VAR_STRING;
8138 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8139 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008140 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141 if (rettv->vval.v_string != NULL && tofree == NULL)
8142 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8143}
8144
8145/*
8146 * "strlen()" function
8147 */
8148 static void
8149f_strlen(typval_T *argvars, typval_T *rettv)
8150{
8151 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008152 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153}
8154
8155/*
8156 * "strchars()" function
8157 */
8158 static void
8159f_strchars(typval_T *argvars, typval_T *rettv)
8160{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008161 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 varnumber_T len = 0;
8164 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165
8166 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008167 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008169 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 else
8171 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8173 while (*s != NUL)
8174 {
8175 func_mb_ptr2char_adv(&s);
8176 ++len;
8177 }
8178 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008179 }
8180}
8181
8182/*
8183 * "strdisplaywidth()" function
8184 */
8185 static void
8186f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8187{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008188 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 int col = 0;
8190
8191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008192 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193
8194 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8195}
8196
8197/*
8198 * "strwidth()" function
8199 */
8200 static void
8201f_strwidth(typval_T *argvars, typval_T *rettv)
8202{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008203 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204
Bram Moolenaar13505972019-01-24 15:04:48 +01008205 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206}
8207
8208/*
8209 * "strcharpart()" function
8210 */
8211 static void
8212f_strcharpart(typval_T *argvars, typval_T *rettv)
8213{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 char_u *p;
8215 int nchar;
8216 int nbyte = 0;
8217 int charlen;
8218 int len = 0;
8219 int slen;
8220 int error = FALSE;
8221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008222 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 slen = (int)STRLEN(p);
8224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008225 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 if (!error)
8227 {
8228 if (nchar > 0)
8229 while (nchar > 0 && nbyte < slen)
8230 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008231 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008232 --nchar;
8233 }
8234 else
8235 nbyte = nchar;
8236 if (argvars[2].v_type != VAR_UNKNOWN)
8237 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008238 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239 while (charlen > 0 && nbyte + len < slen)
8240 {
8241 int off = nbyte + len;
8242
8243 if (off < 0)
8244 len += 1;
8245 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +02008246 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247 --charlen;
8248 }
8249 }
8250 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008251 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 }
8253
8254 /*
8255 * Only return the overlap between the specified part and the actual
8256 * string.
8257 */
8258 if (nbyte < 0)
8259 {
8260 len += nbyte;
8261 nbyte = 0;
8262 }
8263 else if (nbyte > slen)
8264 nbyte = slen;
8265 if (len < 0)
8266 len = 0;
8267 else if (nbyte + len > slen)
8268 len = slen - nbyte;
8269
8270 rettv->v_type = VAR_STRING;
8271 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272}
8273
8274/*
8275 * "strpart()" function
8276 */
8277 static void
8278f_strpart(typval_T *argvars, typval_T *rettv)
8279{
8280 char_u *p;
8281 int n;
8282 int len;
8283 int slen;
8284 int error = FALSE;
8285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008286 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 slen = (int)STRLEN(p);
8288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008289 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 if (error)
8291 len = 0;
8292 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008293 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008295 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008297 // Only return the overlap between the specified part and the actual
8298 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 if (n < 0)
8300 {
8301 len += n;
8302 n = 0;
8303 }
8304 else if (n > slen)
8305 n = slen;
8306 if (len < 0)
8307 len = 0;
8308 else if (n + len > slen)
8309 len = slen - n;
8310
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02008311 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
8312 {
8313 int off;
8314
8315 // length in characters
8316 for (off = n; off < slen && len > 0; --len)
8317 off += mb_ptr2len(p + off);
8318 len = off - n;
8319 }
8320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 rettv->v_type = VAR_STRING;
8322 rettv->vval.v_string = vim_strnsave(p + n, len);
8323}
8324
8325/*
8326 * "strridx()" function
8327 */
8328 static void
8329f_strridx(typval_T *argvars, typval_T *rettv)
8330{
8331 char_u buf[NUMBUFLEN];
8332 char_u *needle;
8333 char_u *haystack;
8334 char_u *rest;
8335 char_u *lastmatch = NULL;
8336 int haystack_len, end_idx;
8337
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008338 needle = tv_get_string_chk(&argvars[1]);
8339 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340
8341 rettv->vval.v_number = -1;
8342 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008343 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344
8345 haystack_len = (int)STRLEN(haystack);
8346 if (argvars[2].v_type != VAR_UNKNOWN)
8347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008348 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008349 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008351 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 }
8353 else
8354 end_idx = haystack_len;
8355
8356 if (*needle == NUL)
8357 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008358 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359 lastmatch = haystack + end_idx;
8360 }
8361 else
8362 {
8363 for (rest = haystack; *rest != '\0'; ++rest)
8364 {
8365 rest = (char_u *)strstr((char *)rest, (char *)needle);
8366 if (rest == NULL || rest > haystack + end_idx)
8367 break;
8368 lastmatch = rest;
8369 }
8370 }
8371
8372 if (lastmatch == NULL)
8373 rettv->vval.v_number = -1;
8374 else
8375 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
8376}
8377
8378/*
8379 * "strtrans()" function
8380 */
8381 static void
8382f_strtrans(typval_T *argvars, typval_T *rettv)
8383{
8384 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008385 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386}
8387
8388/*
8389 * "submatch()" function
8390 */
8391 static void
8392f_submatch(typval_T *argvars, typval_T *rettv)
8393{
8394 int error = FALSE;
8395 int no;
8396 int retList = 0;
8397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 if (error)
8400 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008401 if (no < 0 || no >= NSUBEXP)
8402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008403 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01008404 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02008405 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 if (error)
8409 return;
8410
8411 if (retList == 0)
8412 {
8413 rettv->v_type = VAR_STRING;
8414 rettv->vval.v_string = reg_submatch(no);
8415 }
8416 else
8417 {
8418 rettv->v_type = VAR_LIST;
8419 rettv->vval.v_list = reg_submatch_list(no);
8420 }
8421}
8422
8423/*
8424 * "substitute()" function
8425 */
8426 static void
8427f_substitute(typval_T *argvars, typval_T *rettv)
8428{
8429 char_u patbuf[NUMBUFLEN];
8430 char_u subbuf[NUMBUFLEN];
8431 char_u flagsbuf[NUMBUFLEN];
8432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008433 char_u *str = tv_get_string_chk(&argvars[0]);
8434 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008435 char_u *sub = NULL;
8436 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008439 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
8440 expr = &argvars[2];
8441 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008442 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008445 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
8446 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447 rettv->vval.v_string = NULL;
8448 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02008449 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450}
8451
8452/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008453 * "swapinfo(swap_filename)" function
8454 */
8455 static void
8456f_swapinfo(typval_T *argvars, typval_T *rettv)
8457{
8458 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008459 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02008460}
8461
8462/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02008463 * "swapname(expr)" function
8464 */
8465 static void
8466f_swapname(typval_T *argvars, typval_T *rettv)
8467{
8468 buf_T *buf;
8469
8470 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008471 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02008472 if (buf == NULL || buf->b_ml.ml_mfp == NULL
8473 || buf->b_ml.ml_mfp->mf_fname == NULL)
8474 rettv->vval.v_string = NULL;
8475 else
8476 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
8477}
8478
8479/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 * "synID(lnum, col, trans)" function
8481 */
8482 static void
8483f_synID(typval_T *argvars UNUSED, typval_T *rettv)
8484{
8485 int id = 0;
8486#ifdef FEAT_SYN_HL
8487 linenr_T lnum;
8488 colnr_T col;
8489 int trans;
8490 int transerr = FALSE;
8491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008492 lnum = tv_get_lnum(argvars); // -1 on type error
8493 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008494 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495
8496 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8497 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
8498 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
8499#endif
8500
8501 rettv->vval.v_number = id;
8502}
8503
8504/*
8505 * "synIDattr(id, what [, mode])" function
8506 */
8507 static void
8508f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
8509{
8510 char_u *p = NULL;
8511#ifdef FEAT_SYN_HL
8512 int id;
8513 char_u *what;
8514 char_u *mode;
8515 char_u modebuf[NUMBUFLEN];
8516 int modec;
8517
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008518 id = (int)tv_get_number(&argvars[0]);
8519 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 if (argvars[2].v_type != VAR_UNKNOWN)
8521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008522 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008523 modec = TOLOWER_ASC(mode[0]);
8524 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008525 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 }
8527 else
8528 {
8529#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
8530 if (USE_24BIT)
8531 modec = 'g';
8532 else
8533#endif
8534 if (t_colors > 1)
8535 modec = 'c';
8536 else
8537 modec = 't';
8538 }
8539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 switch (TOLOWER_ASC(what[0]))
8541 {
8542 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008543 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008545 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 p = highlight_has_attr(id, HL_BOLD, modec);
8547 break;
8548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008549 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 p = highlight_color(id, what, modec);
8551 break;
8552
8553 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008554 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008556 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 p = highlight_has_attr(id, HL_ITALIC, modec);
8558 break;
8559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008560 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02008561 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 break;
8563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008564 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 p = highlight_has_attr(id, HL_INVERSE, modec);
8566 break;
8567
8568 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008569 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008571 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008572 else if (TOLOWER_ASC(what[1]) == 't' &&
8573 TOLOWER_ASC(what[2]) == 'r')
8574 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008575 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 p = highlight_has_attr(id, HL_STANDOUT, modec);
8577 break;
8578
8579 case 'u':
8580 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008581 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 p = highlight_has_attr(id, HL_UNDERLINE, modec);
8583 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008584 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 p = highlight_has_attr(id, HL_UNDERCURL, modec);
8586 break;
8587 }
8588
8589 if (p != NULL)
8590 p = vim_strsave(p);
8591#endif
8592 rettv->v_type = VAR_STRING;
8593 rettv->vval.v_string = p;
8594}
8595
8596/*
8597 * "synIDtrans(id)" function
8598 */
8599 static void
8600f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
8601{
8602 int id;
8603
8604#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008605 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606
8607 if (id > 0)
8608 id = syn_get_final_id(id);
8609 else
8610#endif
8611 id = 0;
8612
8613 rettv->vval.v_number = id;
8614}
8615
8616/*
8617 * "synconcealed(lnum, col)" function
8618 */
8619 static void
8620f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
8621{
8622#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
8623 linenr_T lnum;
8624 colnr_T col;
8625 int syntax_flags = 0;
8626 int cchar;
8627 int matchid = 0;
8628 char_u str[NUMBUFLEN];
8629#endif
8630
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008631 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632
8633#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008634 lnum = tv_get_lnum(argvars); // -1 on type error
8635 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636
Bram Moolenaara80faa82020-04-12 19:37:17 +02008637 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638
8639 if (rettv_list_alloc(rettv) != FAIL)
8640 {
8641 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8642 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8643 && curwin->w_p_cole > 0)
8644 {
8645 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
8646 syntax_flags = get_syntax_info(&matchid);
8647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008648 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
8650 {
8651 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02008652 if (cchar == NUL && curwin->w_p_cole == 1)
8653 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 if (cchar != NUL)
8655 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 if (has_mbyte)
8657 (*mb_char2bytes)(cchar, str);
8658 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 str[0] = cchar;
8660 }
8661 }
8662 }
8663
8664 list_append_number(rettv->vval.v_list,
8665 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008666 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 list_append_string(rettv->vval.v_list, str, -1);
8668 list_append_number(rettv->vval.v_list, matchid);
8669 }
8670#endif
8671}
8672
8673/*
8674 * "synstack(lnum, col)" function
8675 */
8676 static void
8677f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
8678{
8679#ifdef FEAT_SYN_HL
8680 linenr_T lnum;
8681 colnr_T col;
8682 int i;
8683 int id;
8684#endif
8685
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008686 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687
8688#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008689 lnum = tv_get_lnum(argvars); // -1 on type error
8690 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691
8692 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
8693 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
8694 && rettv_list_alloc(rettv) != FAIL)
8695 {
8696 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
8697 for (i = 0; ; ++i)
8698 {
8699 id = syn_get_stack_item(i);
8700 if (id < 0)
8701 break;
8702 if (list_append_number(rettv->vval.v_list, id) == FAIL)
8703 break;
8704 }
8705 }
8706#endif
8707}
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709/*
8710 * "tabpagebuflist()" function
8711 */
8712 static void
8713f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8714{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715 tabpage_T *tp;
8716 win_T *wp = NULL;
8717
8718 if (argvars[0].v_type == VAR_UNKNOWN)
8719 wp = firstwin;
8720 else
8721 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 if (tp != NULL)
8724 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
8725 }
8726 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
8727 {
8728 for (; wp != NULL; wp = wp->w_next)
8729 if (list_append_number(rettv->vval.v_list,
8730 wp->w_buffer->b_fnum) == FAIL)
8731 break;
8732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733}
8734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 * "tagfiles()" function
8737 */
8738 static void
8739f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
8740{
8741 char_u *fname;
8742 tagname_T tn;
8743 int first;
8744
8745 if (rettv_list_alloc(rettv) == FAIL)
8746 return;
8747 fname = alloc(MAXPATHL);
8748 if (fname == NULL)
8749 return;
8750
8751 for (first = TRUE; ; first = FALSE)
8752 if (get_tagfname(&tn, first, fname) == FAIL
8753 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
8754 break;
8755 tagname_free(&tn);
8756 vim_free(fname);
8757}
8758
8759/*
8760 * "taglist()" function
8761 */
8762 static void
8763f_taglist(typval_T *argvars, typval_T *rettv)
8764{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008765 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 char_u *tag_pattern;
8767
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008768 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769
8770 rettv->vval.v_number = FALSE;
8771 if (*tag_pattern == NUL)
8772 return;
8773
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008774 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008775 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01008777 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778}
8779
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780#ifdef FEAT_FLOAT
8781/*
8782 * "tan()" function
8783 */
8784 static void
8785f_tan(typval_T *argvars, typval_T *rettv)
8786{
8787 float_T f = 0.0;
8788
8789 rettv->v_type = VAR_FLOAT;
8790 if (get_float_arg(argvars, &f) == OK)
8791 rettv->vval.v_float = tan(f);
8792 else
8793 rettv->vval.v_float = 0.0;
8794}
8795
8796/*
8797 * "tanh()" function
8798 */
8799 static void
8800f_tanh(typval_T *argvars, typval_T *rettv)
8801{
8802 float_T f = 0.0;
8803
8804 rettv->v_type = VAR_FLOAT;
8805 if (get_float_arg(argvars, &f) == OK)
8806 rettv->vval.v_float = tanh(f);
8807 else
8808 rettv->vval.v_float = 0.0;
8809}
8810#endif
8811
8812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813 * "tolower(string)" function
8814 */
8815 static void
8816f_tolower(typval_T *argvars, typval_T *rettv)
8817{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008819 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008820}
8821
8822/*
8823 * "toupper(string)" function
8824 */
8825 static void
8826f_toupper(typval_T *argvars, typval_T *rettv)
8827{
8828 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008829 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830}
8831
8832/*
8833 * "tr(string, fromstr, tostr)" function
8834 */
8835 static void
8836f_tr(typval_T *argvars, typval_T *rettv)
8837{
8838 char_u *in_str;
8839 char_u *fromstr;
8840 char_u *tostr;
8841 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 int inlen;
8843 int fromlen;
8844 int tolen;
8845 int idx;
8846 char_u *cpstr;
8847 int cplen;
8848 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 char_u buf[NUMBUFLEN];
8850 char_u buf2[NUMBUFLEN];
8851 garray_T ga;
8852
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008853 in_str = tv_get_string(&argvars[0]);
8854 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
8855 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008857 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 rettv->v_type = VAR_STRING;
8859 rettv->vval.v_string = NULL;
8860 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008861 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 ga_init2(&ga, (int)sizeof(char), 80);
8863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008865 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866 if (STRLEN(fromstr) != STRLEN(tostr))
8867 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008869 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 ga_clear(&ga);
8871 return;
8872 }
8873
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008874 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 while (*in_str != NUL)
8876 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 if (has_mbyte)
8878 {
8879 inlen = (*mb_ptr2len)(in_str);
8880 cpstr = in_str;
8881 cplen = inlen;
8882 idx = 0;
8883 for (p = fromstr; *p != NUL; p += fromlen)
8884 {
8885 fromlen = (*mb_ptr2len)(p);
8886 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
8887 {
8888 for (p = tostr; *p != NUL; p += tolen)
8889 {
8890 tolen = (*mb_ptr2len)(p);
8891 if (idx-- == 0)
8892 {
8893 cplen = tolen;
8894 cpstr = p;
8895 break;
8896 }
8897 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008898 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 goto error;
8900 break;
8901 }
8902 ++idx;
8903 }
8904
8905 if (first && cpstr == in_str)
8906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008907 // Check that fromstr and tostr have the same number of
8908 // (multi-byte) characters. Done only once when a character
8909 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910 first = FALSE;
8911 for (p = tostr; *p != NUL; p += tolen)
8912 {
8913 tolen = (*mb_ptr2len)(p);
8914 --idx;
8915 }
8916 if (idx != 0)
8917 goto error;
8918 }
8919
8920 (void)ga_grow(&ga, cplen);
8921 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
8922 ga.ga_len += cplen;
8923
8924 in_str += inlen;
8925 }
8926 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008928 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 p = vim_strchr(fromstr, *in_str);
8930 if (p != NULL)
8931 ga_append(&ga, tostr[p - fromstr]);
8932 else
8933 ga_append(&ga, *in_str);
8934 ++in_str;
8935 }
8936 }
8937
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008938 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 (void)ga_grow(&ga, 1);
8940 ga_append(&ga, NUL);
8941
8942 rettv->vval.v_string = ga.ga_data;
8943}
8944
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008945/*
8946 * "trim({expr})" function
8947 */
8948 static void
8949f_trim(typval_T *argvars, typval_T *rettv)
8950{
8951 char_u buf1[NUMBUFLEN];
8952 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008953 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008954 char_u *mask = NULL;
8955 char_u *tail;
8956 char_u *prev;
8957 char_u *p;
8958 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008959 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008960
8961 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008962 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008963 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008964 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008965
8966 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008968 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008969
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008971 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008972 int error = 0;
8973
8974 // leading or trailing characters to trim
8975 dir = (int)tv_get_number_chk(&argvars[2], &error);
8976 if (error)
8977 return;
8978 if (dir < 0 || dir > 2)
8979 {
8980 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
8981 return;
8982 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008983 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008984 }
8985
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008986 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008987 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008988 // Trim leading characters
8989 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008990 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008991 c1 = PTR2CHAR(head);
8992 if (mask == NULL)
8993 {
8994 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01008995 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02008996 }
8997 else
8998 {
8999 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9000 if (c1 == PTR2CHAR(p))
9001 break;
9002 if (*p == NUL)
9003 break;
9004 }
9005 MB_PTR_ADV(head);
9006 }
9007 }
9008
9009 tail = head + STRLEN(head);
9010 if (dir == 0 || dir == 2)
9011 {
9012 // Trim trailing characters
9013 for (; tail > head; tail = prev)
9014 {
9015 prev = tail;
9016 MB_PTR_BACK(head, prev);
9017 c1 = PTR2CHAR(prev);
9018 if (mask == NULL)
9019 {
9020 if (c1 > ' ' && c1 != 0xa0)
9021 break;
9022 }
9023 else
9024 {
9025 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9026 if (c1 == PTR2CHAR(p))
9027 break;
9028 if (*p == NUL)
9029 break;
9030 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009031 }
9032 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009033 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009034}
9035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036#ifdef FEAT_FLOAT
9037/*
9038 * "trunc({float})" function
9039 */
9040 static void
9041f_trunc(typval_T *argvars, typval_T *rettv)
9042{
9043 float_T f = 0.0;
9044
9045 rettv->v_type = VAR_FLOAT;
9046 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009047 // trunc() is not in C90, use floor() or ceil() instead.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
9049 else
9050 rettv->vval.v_float = 0.0;
9051}
9052#endif
9053
9054/*
9055 * "type(expr)" function
9056 */
9057 static void
9058f_type(typval_T *argvars, typval_T *rettv)
9059{
9060 int n = -1;
9061
9062 switch (argvars[0].v_type)
9063 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009064 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9065 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009067 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9068 case VAR_LIST: n = VAR_TYPE_LIST; break;
9069 case VAR_DICT: n = VAR_TYPE_DICT; break;
9070 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9071 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9072 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009073 case VAR_JOB: n = VAR_TYPE_JOB; break;
9074 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009075 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009077 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009078 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009079 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 n = -1;
9081 break;
9082 }
9083 rettv->vval.v_number = n;
9084}
9085
9086/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 * "virtcol(string)" function
9088 */
9089 static void
9090f_virtcol(typval_T *argvars, typval_T *rettv)
9091{
9092 colnr_T vcol = 0;
9093 pos_T *fp;
9094 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009095 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096
9097 fp = var2fpos(&argvars[0], FALSE, &fnum);
9098 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9099 && fnum == curbuf->b_fnum)
9100 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009101 // Limit the column to a valid value, getvvcol() doesn't check.
9102 if (fp->col < 0)
9103 fp->col = 0;
9104 else
9105 {
9106 len = (int)STRLEN(ml_get(fp->lnum));
9107 if (fp->col > len)
9108 fp->col = len;
9109 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 getvvcol(curwin, fp, NULL, NULL, &vcol);
9111 ++vcol;
9112 }
9113
9114 rettv->vval.v_number = vcol;
9115}
9116
9117/*
9118 * "visualmode()" function
9119 */
9120 static void
9121f_visualmode(typval_T *argvars, typval_T *rettv)
9122{
9123 char_u str[2];
9124
9125 rettv->v_type = VAR_STRING;
9126 str[0] = curbuf->b_visual_mode_eval;
9127 str[1] = NUL;
9128 rettv->vval.v_string = vim_strsave(str);
9129
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009130 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131 if (non_zero_arg(&argvars[0]))
9132 curbuf->b_visual_mode_eval = NUL;
9133}
9134
9135/*
9136 * "wildmenumode()" function
9137 */
9138 static void
9139f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9140{
9141#ifdef FEAT_WILDMENU
9142 if (wild_menu_showing)
9143 rettv->vval.v_number = 1;
9144#endif
9145}
9146
9147/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009148 * "windowsversion()" function
9149 */
9150 static void
9151f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9152{
9153 rettv->v_type = VAR_STRING;
9154 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9155}
9156
9157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 * "wordcount()" function
9159 */
9160 static void
9161f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9162{
9163 if (rettv_dict_alloc(rettv) == FAIL)
9164 return;
9165 cursor_pos_info(rettv->vval.v_dict);
9166}
9167
9168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 * "xor(expr, expr)" function
9170 */
9171 static void
9172f_xor(typval_T *argvars, typval_T *rettv)
9173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009174 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9175 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009176}
9177
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009178#endif // FEAT_EVAL