blob: f8af8ef8125092f64cf0478353fea72a5dc62b31 [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 +020023static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010024#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020025static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010026static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010027# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010028static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010029# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031static void f_byte2line(typval_T *argvars, typval_T *rettv);
32static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
33static void f_byteidx(typval_T *argvars, typval_T *rettv);
34static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
35static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020036static void f_changenr(typval_T *argvars, typval_T *rettv);
37static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010038static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010039static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020041static void f_confirm(typval_T *argvars, typval_T *rettv);
42static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010044#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020045static void f_debugbreak(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010049static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020051static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_escape(typval_T *argvars, typval_T *rettv);
53static void f_eval(typval_T *argvars, typval_T *rettv);
54static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020058static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020062static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_function(typval_T *argvars, typval_T *rettv);
64static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
65static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010066static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010067static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020069static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010071static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_getpid(typval_T *argvars, typval_T *rettv);
73static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010074static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020077static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010079static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +020080static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020081static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
82static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_hlID(typval_T *argvars, typval_T *rettv);
84static void f_hlexists(typval_T *argvars, typval_T *rettv);
85static void f_hostname(typval_T *argvars, typval_T *rettv);
86static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_index(typval_T *argvars, typval_T *rettv);
88static void f_input(typval_T *argvars, typval_T *rettv);
89static void f_inputdialog(typval_T *argvars, typval_T *rettv);
90static void f_inputlist(typval_T *argvars, typval_T *rettv);
91static void f_inputrestore(typval_T *argvars, typval_T *rettv);
92static void f_inputsave(typval_T *argvars, typval_T *rettv);
93static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +010094static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020095static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020097static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
98static void f_len(typval_T *argvars, typval_T *rettv);
99static void f_libcall(typval_T *argvars, typval_T *rettv);
100static void f_libcallnr(typval_T *argvars, typval_T *rettv);
101static void f_line(typval_T *argvars, typval_T *rettv);
102static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103#ifdef FEAT_LUA
104static void f_luaeval(typval_T *argvars, typval_T *rettv);
105#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_maparg(typval_T *argvars, typval_T *rettv);
107static void f_mapcheck(typval_T *argvars, typval_T *rettv);
108static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200109static void f_matchend(typval_T *argvars, typval_T *rettv);
110static void f_matchlist(typval_T *argvars, typval_T *rettv);
111static void f_matchstr(typval_T *argvars, typval_T *rettv);
112static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
113static void f_max(typval_T *argvars, typval_T *rettv);
114static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115#ifdef FEAT_MZSCHEME
116static void f_mzeval(typval_T *argvars, typval_T *rettv);
117#endif
118static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
119static void f_nr2char(typval_T *argvars, typval_T *rettv);
120static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121#ifdef FEAT_PERL
122static void f_perleval(typval_T *argvars, typval_T *rettv);
123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200124static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
125static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200126static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_pumvisible(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_PYTHON3
129static void f_py3eval(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_PYTHON
132static void f_pyeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100134#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
135static void f_pyxeval(typval_T *argvars, typval_T *rettv);
136#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100137static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100138static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200139static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200140static void f_reg_executing(typval_T *argvars, typval_T *rettv);
141static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142static void f_rename(typval_T *argvars, typval_T *rettv);
143static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100144#ifdef FEAT_RUBY
145static void f_rubyeval(typval_T *argvars, typval_T *rettv);
146#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147static void f_screenattr(typval_T *argvars, typval_T *rettv);
148static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100149static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200150static void f_screencol(typval_T *argvars, typval_T *rettv);
151static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100152static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_search(typval_T *argvars, typval_T *rettv);
154static void f_searchdecl(typval_T *argvars, typval_T *rettv);
155static void f_searchpair(typval_T *argvars, typval_T *rettv);
156static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
157static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100158static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100160static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200161static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100165static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166#ifdef FEAT_CRYPT
167static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_shellescape(typval_T *argvars, typval_T *rettv);
170static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_soundfold(typval_T *argvars, typval_T *rettv);
172static void f_spellbadword(typval_T *argvars, typval_T *rettv);
173static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
174static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100175static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200176static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_str2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100178static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_strgetchar(typval_T *argvars, typval_T *rettv);
181static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_strlen(typval_T *argvars, typval_T *rettv);
183static void f_strcharpart(typval_T *argvars, typval_T *rettv);
184static void f_strpart(typval_T *argvars, typval_T *rettv);
185static void f_strridx(typval_T *argvars, typval_T *rettv);
186static void f_strtrans(typval_T *argvars, typval_T *rettv);
187static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
188static void f_strwidth(typval_T *argvars, typval_T *rettv);
189static void f_submatch(typval_T *argvars, typval_T *rettv);
190static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200191static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200192static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_synID(typval_T *argvars, typval_T *rettv);
194static void f_synIDattr(typval_T *argvars, typval_T *rettv);
195static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
196static void f_synstack(typval_T *argvars, typval_T *rettv);
197static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_taglist(typval_T *argvars, typval_T *rettv);
200static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_tolower(typval_T *argvars, typval_T *rettv);
202static void f_toupper(typval_T *argvars, typval_T *rettv);
203static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100204static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_virtcol(typval_T *argvars, typval_T *rettv);
207static void f_visualmode(typval_T *argvars, typval_T *rettv);
208static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100209static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200210static void f_wordcount(typval_T *argvars, typval_T *rettv);
211static void f_xor(typval_T *argvars, typval_T *rettv);
212
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100213
Bram Moolenaar94738d82020-10-21 14:25:07 +0200214/*
215 * Functions that check the argument type of a builtin function.
216 * Each function returns FAIL and gives an error message if the type is wrong.
217 */
218
219// Context passed to an arg_ function.
220typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200221 int arg_count; // actual argument count
222 type_T **arg_types; // list of argument types
223 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100224 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200225} argcontext_T;
226
227// A function to check one argument type. The first argument is the type to
228// check. If needed, other argument types can be obtained with the context.
229// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
230typedef int (*argcheck_T)(type_T *, argcontext_T *);
231
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100232/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100233 * Call need_type() to check an argument type.
234 */
235 static int
236check_arg_type(
237 type_T *expected,
238 type_T *actual,
239 argcontext_T *context)
240{
241 // TODO: would be useful to know if "actual" is a constant and pass it to
242 // need_type() to get a compile time error if possible.
243 return need_type(actual, expected,
244 context->arg_idx - context->arg_count, context->arg_idx + 1,
245 context->arg_cctx, FALSE, FALSE);
246}
247
248/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100249 * Check "type" is a float or a number.
250 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200251 static int
252arg_float_or_nr(type_T *type, argcontext_T *context)
253{
Bram Moolenaarca174532020-10-21 16:42:22 +0200254 if (type->tt_type == VAR_ANY
255 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200256 return OK;
257 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
258 return FAIL;
259}
260
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100261/*
262 * Check "type" is a number.
263 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200264 static int
265arg_number(type_T *type, argcontext_T *context)
266{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100267 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200268}
269
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100270/*
271 * Check "type" is a string.
272 */
273 static int
274arg_string(type_T *type, argcontext_T *context)
275{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100276 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100277}
278
279/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100280 * Check "type" is a bool or number 0 or 1.
281 */
282 static int
283arg_bool(type_T *type, argcontext_T *context)
284{
285 if (type->tt_type == VAR_ANY
286 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
287 return OK;
288 return check_arg_type(&t_bool, type, context);
289}
290
291/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100292 * Check "type" is a list or a blob.
293 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200294 static int
295arg_list_or_blob(type_T *type, argcontext_T *context)
296{
297 if (type->tt_type == VAR_ANY
298 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
299 return OK;
300 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
301 return FAIL;
302}
303
304/*
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200305 * Check "type" is a string or a list of strings.
306 */
307 static int
308arg_string_or_list(type_T *type, argcontext_T *context)
309{
310 if (type->tt_type == VAR_ANY || type->tt_type == VAR_STRING)
311 return OK;
312 if (type->tt_type != VAR_LIST)
313 {
314 arg_type_mismatch(&t_string, type, context->arg_idx + 1);
315 return FAIL;
316 }
317 if (type->tt_member->tt_type == VAR_ANY
318 || type->tt_member->tt_type == VAR_STRING)
319 return OK;
320
321 arg_type_mismatch(&t_list_string, type, context->arg_idx + 1);
322 return FAIL;
323}
324
325/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100326 * Check "type" is a list or a dict.
327 */
328 static int
329arg_list_or_dict(type_T *type, argcontext_T *context)
330{
331 if (type->tt_type == VAR_ANY
332 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
333 return OK;
334 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
335 return FAIL;
336}
337
338/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100339 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100340 * Must not be used for the first argcheck_T entry.
341 */
342 static int
343arg_same_as_prev(type_T *type, argcontext_T *context)
344{
345 type_T *prev_type = context->arg_types[context->arg_idx - 1];
346
Bram Moolenaar351ead02021-01-16 16:07:01 +0100347 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100348}
349
350/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100351 * Check "type" is the same basic type as the previous argument, checks list or
352 * dict vs other type, but not member type.
353 * Must not be used for the first argcheck_T entry.
354 */
355 static int
356arg_same_struct_as_prev(type_T *type, argcontext_T *context)
357{
358 type_T *prev_type = context->arg_types[context->arg_idx - 1];
359
360 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100361 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100362 return OK;
363}
364
365/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100366 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200367 * Must not be used for the first argcheck_T entry.
368 */
369 static int
370arg_item_of_prev(type_T *type, argcontext_T *context)
371{
372 type_T *prev_type = context->arg_types[context->arg_idx - 1];
373 type_T *expected;
374
375 if (prev_type->tt_type == VAR_LIST)
376 expected = prev_type->tt_member;
377 else if (prev_type->tt_type == VAR_BLOB)
378 expected = &t_number;
379 else
380 // probably VAR_ANY, can't check
381 return OK;
382
Bram Moolenaar351ead02021-01-16 16:07:01 +0100383 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200384}
385
Bram Moolenaar94738d82020-10-21 14:25:07 +0200386/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387 * Check "type" which is the third argument of extend().
388 */
389 static int
390arg_extend3(type_T *type, argcontext_T *context)
391{
392 type_T *first_type = context->arg_types[context->arg_idx - 2];
393
394 if (first_type->tt_type == VAR_LIST)
395 return arg_number(type, context);
396 if (first_type->tt_type == VAR_DICT)
397 return arg_string(type, context);
398 return OK;
399}
400
401
402/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200403 * Lists of functions that check the argument types of a builtin function.
404 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100405argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100406argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100408argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200409argcheck_T arg2_execute[] = {arg_string_or_list, arg_string};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100410argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100411argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200412argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200413
414/*
415 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200416 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200417 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100418 static type_T *
419ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
420{
421 return &t_void;
422}
423 static type_T *
424ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
425{
426 return &t_any;
427}
428 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200429ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
430{
431 return &t_bool;
432}
433 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100434ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
435{
436 return &t_number_bool;
437}
438 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100439ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
440{
441 return &t_number;
442}
443 static type_T *
444ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
445{
446 return &t_float;
447}
448 static type_T *
449ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
450{
451 return &t_string;
452}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200453 static type_T *
454ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100455{
456 return &t_list_any;
457}
458 static type_T *
459ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
460{
461 return &t_list_number;
462}
463 static type_T *
464ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
465{
466 return &t_list_string;
467}
468 static type_T *
469ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
470{
471 return &t_list_dict_any;
472}
473 static type_T *
474ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
475{
476 return &t_dict_any;
477}
478 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100479ret_job_info(int argcount, type_T **argtypes UNUSED)
480{
481 if (argcount == 0)
482 return &t_list_job;
483 return &t_dict_any;
484}
485 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100486ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
487{
488 return &t_dict_number;
489}
490 static type_T *
491ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
492{
493 return &t_dict_string;
494}
495 static type_T *
496ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
497{
498 return &t_blob;
499}
500 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200501ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100502{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200503 return &t_func_any;
504}
505 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100506ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
507{
508 return &t_channel;
509}
510 static type_T *
511ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
512{
513 return &t_job;
514}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200515 static type_T *
516ret_first_arg(int argcount, type_T **argtypes)
517{
518 if (argcount > 0)
519 return argtypes[0];
520 return &t_void;
521}
Bram Moolenaarea696852020-11-09 18:31:39 +0100522// for map(): returns first argument but item type may differ
523 static type_T *
524ret_first_cont(int argcount UNUSED, type_T **argtypes)
525{
526 if (argtypes[0]->tt_type == VAR_LIST)
527 return &t_list_any;
528 if (argtypes[0]->tt_type == VAR_DICT)
529 return &t_dict_any;
530 if (argtypes[0]->tt_type == VAR_BLOB)
531 return argtypes[0];
532 return &t_any;
533}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200534
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200535/*
536 * Used for getqflist(): returns list if there is no argument, dict if there is
537 * one.
538 */
539 static type_T *
540ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
541{
542 if (argcount > 0)
543 return &t_dict_any;
544 return &t_list_dict_any;
545}
546
547/*
548 * Used for getloclist(): returns list if there is one argument, dict if there
549 * are two.
550 */
551 static type_T *
552ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
553{
554 if (argcount > 1)
555 return &t_dict_any;
556 return &t_list_dict_any;
557}
558
Bram Moolenaar846178a2020-07-05 17:04:13 +0200559 static type_T *
560ret_argv(int argcount, type_T **argtypes UNUSED)
561{
562 // argv() returns list of strings
563 if (argcount == 0)
564 return &t_list_string;
565
566 // argv(0) returns a string, but argv(-1] returns a list
567 return &t_any;
568}
569
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200570 static type_T *
571ret_remove(int argcount UNUSED, type_T **argtypes)
572{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200573 if (argtypes != NULL)
574 {
575 if (argtypes[0]->tt_type == VAR_LIST
576 || argtypes[0]->tt_type == VAR_DICT)
577 return argtypes[0]->tt_member;
578 if (argtypes[0]->tt_type == VAR_BLOB)
579 return &t_number;
580 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200581 return &t_any;
582}
583
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200584 static type_T *
585ret_getreg(int argcount, type_T **argtypes UNUSED)
586{
587 // Assume that if the third argument is passed it's non-zero
588 if (argcount == 3)
589 return &t_list_string;
590 return &t_string;
591}
592
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200593 static type_T *
594ret_maparg(int argcount, type_T **argtypes UNUSED)
595{
596 // Assume that if the fourth argument is passed it's non-zero
597 if (argcount == 4)
598 return &t_dict_any;
599 return &t_string;
600}
601
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100602static type_T *ret_f_function(int argcount, type_T **argtypes);
603
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604/*
605 * Array with names and number of arguments of all internal functions
606 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
607 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200608typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200610 char *f_name; // function name
611 char f_min_argc; // minimal number of arguments
612 char f_max_argc; // maximal number of arguments
613 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200614 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100615 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
616 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200618 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200619} funcentry_T;
620
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200621// values for f_argtype; zero means it cannot be used as a method
622#define FEARG_1 1 // base is the first argument
623#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200624#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200625#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200626#define FEARG_LAST 9 // base is the last argument
627
Bram Moolenaar15c47602020-03-26 22:16:48 +0100628#ifdef FEAT_FLOAT
629# define FLOAT_FUNC(name) name
630#else
631# define FLOAT_FUNC(name) NULL
632#endif
633#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
634# define MATH_FUNC(name) name
635#else
636# define MATH_FUNC(name) NULL
637#endif
638#ifdef FEAT_TIMERS
639# define TIMER_FUNC(name) name
640#else
641# define TIMER_FUNC(name) NULL
642#endif
643#ifdef FEAT_JOB_CHANNEL
644# define JOB_FUNC(name) name
645#else
646# define JOB_FUNC(name) NULL
647#endif
648#ifdef FEAT_PROP_POPUP
649# define PROP_FUNC(name) name
650#else
651# define PROP_FUNC(name) NULL
652#endif
653#ifdef FEAT_SIGNS
654# define SIGN_FUNC(name) name
655#else
656# define SIGN_FUNC(name) NULL
657#endif
658#ifdef FEAT_SOUND
659# define SOUND_FUNC(name) name
660#else
661# define SOUND_FUNC(name) NULL
662#endif
663#ifdef FEAT_TERMINAL
664# define TERM_FUNC(name) name
665#else
666# define TERM_FUNC(name) NULL
667#endif
668
Bram Moolenaarac92e252019-08-03 21:58:38 +0200669static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200671 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
672 ret_any, FLOAT_FUNC(f_abs)},
673 {"acos", 1, 1, FEARG_1, NULL,
674 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100675 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200676 ret_first_arg, f_add},
677 {"and", 2, 2, FEARG_1, NULL,
678 ret_number, f_and},
679 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100680 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200681 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100682 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200683 {"argc", 0, 1, 0, NULL,
684 ret_number, f_argc},
685 {"argidx", 0, 0, 0, NULL,
686 ret_number, f_argidx},
687 {"arglistid", 0, 2, 0, NULL,
688 ret_number, f_arglistid},
689 {"argv", 0, 2, 0, NULL,
690 ret_argv, f_argv},
691 {"asin", 1, 1, FEARG_1, NULL,
692 ret_float, FLOAT_FUNC(f_asin)},
693 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100694 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200695 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100696 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200697 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100698 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200699 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100700 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200701 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100702 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100704 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200705 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100706 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200707 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100708 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200709 {"assert_nobeep", 1, 2, FEARG_1, NULL,
710 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200711 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200713 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100714 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100716 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100718 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200719 {"atan", 1, 1, FEARG_1, NULL,
720 ret_float, FLOAT_FUNC(f_atan)},
721 {"atan2", 2, 2, FEARG_1, NULL,
722 ret_float, FLOAT_FUNC(f_atan2)},
723 {"balloon_gettext", 0, 0, 0, NULL,
724 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100725#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100726 f_balloon_gettext
727#else
728 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100729#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100730 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200731 {"balloon_show", 1, 1, FEARG_1, NULL,
732 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100733#ifdef FEAT_BEVAL
734 f_balloon_show
735#else
736 NULL
737#endif
738 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"balloon_split", 1, 1, FEARG_1, NULL,
740 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100741#if defined(FEAT_BEVAL_TERM)
742 f_balloon_split
743#else
744 NULL
745#endif
746 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200747 {"browse", 4, 4, 0, NULL,
748 ret_string, f_browse},
749 {"browsedir", 2, 2, 0, NULL,
750 ret_string, f_browsedir},
751 {"bufadd", 1, 1, FEARG_1, NULL,
752 ret_number, f_bufadd},
753 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100754 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200755 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100756 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200757 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
758 ret_string, f_bufname},
759 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
760 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200761 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100762 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200763 {"bufload", 1, 1, FEARG_1, NULL,
764 ret_void, f_bufload},
765 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100766 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200767 {"bufname", 0, 1, FEARG_1, NULL,
768 ret_string, f_bufname},
769 {"bufnr", 0, 2, FEARG_1, NULL,
770 ret_number, f_bufnr},
771 {"bufwinid", 1, 1, FEARG_1, NULL,
772 ret_number, f_bufwinid},
773 {"bufwinnr", 1, 1, FEARG_1, NULL,
774 ret_number, f_bufwinnr},
775 {"byte2line", 1, 1, FEARG_1, NULL,
776 ret_number, f_byte2line},
777 {"byteidx", 2, 2, FEARG_1, NULL,
778 ret_number, f_byteidx},
779 {"byteidxcomp", 2, 2, FEARG_1, NULL,
780 ret_number, f_byteidxcomp},
781 {"call", 2, 3, FEARG_1, NULL,
782 ret_any, f_call},
783 {"ceil", 1, 1, FEARG_1, NULL,
784 ret_float, FLOAT_FUNC(f_ceil)},
785 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100786 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200787 {"ch_close", 1, 1, FEARG_1, NULL,
788 ret_void, JOB_FUNC(f_ch_close)},
789 {"ch_close_in", 1, 1, FEARG_1, NULL,
790 ret_void, JOB_FUNC(f_ch_close_in)},
791 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
792 ret_any, JOB_FUNC(f_ch_evalexpr)},
793 {"ch_evalraw", 2, 3, FEARG_1, NULL,
794 ret_any, JOB_FUNC(f_ch_evalraw)},
795 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
796 ret_number, JOB_FUNC(f_ch_getbufnr)},
797 {"ch_getjob", 1, 1, FEARG_1, NULL,
798 ret_job, JOB_FUNC(f_ch_getjob)},
799 {"ch_info", 1, 1, FEARG_1, NULL,
800 ret_dict_any, JOB_FUNC(f_ch_info)},
801 {"ch_log", 1, 2, FEARG_1, NULL,
802 ret_void, JOB_FUNC(f_ch_log)},
803 {"ch_logfile", 1, 2, FEARG_1, NULL,
804 ret_void, JOB_FUNC(f_ch_logfile)},
805 {"ch_open", 1, 2, FEARG_1, NULL,
806 ret_channel, JOB_FUNC(f_ch_open)},
807 {"ch_read", 1, 2, FEARG_1, NULL,
808 ret_string, JOB_FUNC(f_ch_read)},
809 {"ch_readblob", 1, 2, FEARG_1, NULL,
810 ret_blob, JOB_FUNC(f_ch_readblob)},
811 {"ch_readraw", 1, 2, FEARG_1, NULL,
812 ret_string, JOB_FUNC(f_ch_readraw)},
813 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
814 ret_void, JOB_FUNC(f_ch_sendexpr)},
815 {"ch_sendraw", 2, 3, FEARG_1, NULL,
816 ret_void, JOB_FUNC(f_ch_sendraw)},
817 {"ch_setoptions", 2, 2, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_setoptions)},
819 {"ch_status", 1, 2, FEARG_1, NULL,
820 ret_string, JOB_FUNC(f_ch_status)},
821 {"changenr", 0, 0, 0, NULL,
822 ret_number, f_changenr},
823 {"char2nr", 1, 2, FEARG_1, NULL,
824 ret_number, f_char2nr},
825 {"charclass", 1, 1, FEARG_1, NULL,
826 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100827 {"charcol", 1, 1, FEARG_1, NULL,
828 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100829 {"charidx", 2, 3, FEARG_1, NULL,
830 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200831 {"chdir", 1, 1, FEARG_1, NULL,
832 ret_string, f_chdir},
833 {"cindent", 1, 1, FEARG_1, NULL,
834 ret_number, f_cindent},
835 {"clearmatches", 0, 1, FEARG_1, NULL,
836 ret_void, f_clearmatches},
837 {"col", 1, 1, FEARG_1, NULL,
838 ret_number, f_col},
839 {"complete", 2, 2, FEARG_2, NULL,
840 ret_void, f_complete},
841 {"complete_add", 1, 1, FEARG_1, NULL,
842 ret_number, f_complete_add},
843 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100844 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200845 {"complete_info", 0, 1, FEARG_1, NULL,
846 ret_dict_any, f_complete_info},
847 {"confirm", 1, 4, FEARG_1, NULL,
848 ret_number, f_confirm},
849 {"copy", 1, 1, FEARG_1, NULL,
850 ret_first_arg, f_copy},
851 {"cos", 1, 1, FEARG_1, NULL,
852 ret_float, FLOAT_FUNC(f_cos)},
853 {"cosh", 1, 1, FEARG_1, NULL,
854 ret_float, FLOAT_FUNC(f_cosh)},
855 {"count", 2, 4, FEARG_1, NULL,
856 ret_number, f_count},
857 {"cscope_connection",0,3, 0, NULL,
858 ret_number, f_cscope_connection},
859 {"cursor", 1, 3, FEARG_1, NULL,
860 ret_number, f_cursor},
861 {"debugbreak", 1, 1, FEARG_1, NULL,
862 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100863#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100864 f_debugbreak
865#else
866 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200867#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100868 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200869 {"deepcopy", 1, 2, FEARG_1, NULL,
870 ret_first_arg, f_deepcopy},
871 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100872 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200873 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100874 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200875 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100876 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200877 {"diff_filler", 1, 1, FEARG_1, NULL,
878 ret_number, f_diff_filler},
879 {"diff_hlID", 2, 2, FEARG_1, NULL,
880 ret_number, f_diff_hlID},
881 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100882 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200883 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100884 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200885 {"environ", 0, 0, 0, NULL,
886 ret_dict_string, f_environ},
887 {"escape", 2, 2, FEARG_1, NULL,
888 ret_string, f_escape},
889 {"eval", 1, 1, FEARG_1, NULL,
890 ret_any, f_eval},
891 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100892 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200893 {"executable", 1, 1, FEARG_1, NULL,
894 ret_number, f_executable},
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200895 {"execute", 1, 2, FEARG_1, arg2_execute,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200896 ret_string, f_execute},
897 {"exepath", 1, 1, FEARG_1, NULL,
898 ret_string, f_exepath},
899 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100900 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200901 {"exp", 1, 1, FEARG_1, NULL,
902 ret_float, FLOAT_FUNC(f_exp)},
903 {"expand", 1, 3, FEARG_1, NULL,
904 ret_any, f_expand},
905 {"expandcmd", 1, 1, FEARG_1, NULL,
906 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100907 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200908 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100909 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
910 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200911 {"feedkeys", 1, 2, FEARG_1, NULL,
912 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200913 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100914 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100916 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200917 {"filewritable", 1, 1, FEARG_1, NULL,
918 ret_number, f_filewritable},
919 {"filter", 2, 2, FEARG_1, NULL,
920 ret_first_arg, f_filter},
921 {"finddir", 1, 3, FEARG_1, NULL,
922 ret_string, f_finddir},
923 {"findfile", 1, 3, FEARG_1, NULL,
924 ret_string, f_findfile},
925 {"flatten", 1, 2, FEARG_1, NULL,
926 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100927 {"flattennew", 1, 2, FEARG_1, NULL,
928 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200929 {"float2nr", 1, 1, FEARG_1, NULL,
930 ret_number, FLOAT_FUNC(f_float2nr)},
931 {"floor", 1, 1, FEARG_1, NULL,
932 ret_float, FLOAT_FUNC(f_floor)},
933 {"fmod", 2, 2, FEARG_1, NULL,
934 ret_float, FLOAT_FUNC(f_fmod)},
935 {"fnameescape", 1, 1, FEARG_1, NULL,
936 ret_string, f_fnameescape},
937 {"fnamemodify", 2, 2, FEARG_1, NULL,
938 ret_string, f_fnamemodify},
939 {"foldclosed", 1, 1, FEARG_1, NULL,
940 ret_number, f_foldclosed},
941 {"foldclosedend", 1, 1, FEARG_1, NULL,
942 ret_number, f_foldclosedend},
943 {"foldlevel", 1, 1, FEARG_1, NULL,
944 ret_number, f_foldlevel},
945 {"foldtext", 0, 0, 0, NULL,
946 ret_string, f_foldtext},
947 {"foldtextresult", 1, 1, FEARG_1, NULL,
948 ret_string, f_foldtextresult},
949 {"foreground", 0, 0, 0, NULL,
950 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100951 {"fullcommand", 1, 1, FEARG_1, arg1_string,
952 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200953 {"funcref", 1, 3, FEARG_1, NULL,
954 ret_func_any, f_funcref},
955 {"function", 1, 3, FEARG_1, NULL,
956 ret_f_function, f_function},
957 {"garbagecollect", 0, 1, 0, NULL,
958 ret_void, f_garbagecollect},
959 {"get", 2, 3, FEARG_1, NULL,
960 ret_any, f_get},
961 {"getbufinfo", 0, 1, FEARG_1, NULL,
962 ret_list_dict_any, f_getbufinfo},
963 {"getbufline", 2, 3, FEARG_1, NULL,
964 ret_list_string, f_getbufline},
965 {"getbufvar", 2, 3, FEARG_1, NULL,
966 ret_any, f_getbufvar},
967 {"getchangelist", 0, 1, FEARG_1, NULL,
968 ret_list_any, f_getchangelist},
969 {"getchar", 0, 1, 0, NULL,
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200970 ret_any, f_getchar},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200971 {"getcharmod", 0, 0, 0, NULL,
972 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100973 {"getcharpos", 1, 1, FEARG_1, NULL,
974 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200975 {"getcharsearch", 0, 0, 0, NULL,
976 ret_dict_any, f_getcharsearch},
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200977 {"getcharstr", 0, 1, 0, NULL,
978 ret_string, f_getcharstr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200979 {"getcmdline", 0, 0, 0, NULL,
980 ret_string, f_getcmdline},
981 {"getcmdpos", 0, 0, 0, NULL,
982 ret_number, f_getcmdpos},
983 {"getcmdtype", 0, 0, 0, NULL,
984 ret_string, f_getcmdtype},
985 {"getcmdwintype", 0, 0, 0, NULL,
986 ret_string, f_getcmdwintype},
987 {"getcompletion", 2, 3, FEARG_1, NULL,
988 ret_list_string, f_getcompletion},
989 {"getcurpos", 0, 1, FEARG_1, NULL,
990 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100991 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
992 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200993 {"getcwd", 0, 2, FEARG_1, NULL,
994 ret_string, f_getcwd},
995 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100996 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200997 {"getfontname", 0, 1, 0, NULL,
998 ret_string, f_getfontname},
999 {"getfperm", 1, 1, FEARG_1, NULL,
1000 ret_string, f_getfperm},
1001 {"getfsize", 1, 1, FEARG_1, NULL,
1002 ret_number, f_getfsize},
1003 {"getftime", 1, 1, FEARG_1, NULL,
1004 ret_number, f_getftime},
1005 {"getftype", 1, 1, FEARG_1, NULL,
1006 ret_string, f_getftype},
1007 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001008 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001009 {"getjumplist", 0, 2, FEARG_1, NULL,
1010 ret_list_any, f_getjumplist},
1011 {"getline", 1, 2, FEARG_1, NULL,
1012 ret_f_getline, f_getline},
1013 {"getloclist", 1, 2, 0, NULL,
1014 ret_list_or_dict_1, f_getloclist},
1015 {"getmarklist", 0, 1, FEARG_1, NULL,
1016 ret_list_dict_any, f_getmarklist},
1017 {"getmatches", 0, 1, 0, NULL,
1018 ret_list_dict_any, f_getmatches},
1019 {"getmousepos", 0, 0, 0, NULL,
1020 ret_dict_number, f_getmousepos},
1021 {"getpid", 0, 0, 0, NULL,
1022 ret_number, f_getpid},
1023 {"getpos", 1, 1, FEARG_1, NULL,
1024 ret_list_number, f_getpos},
1025 {"getqflist", 0, 1, 0, NULL,
1026 ret_list_or_dict_0, f_getqflist},
1027 {"getreg", 0, 3, FEARG_1, NULL,
1028 ret_getreg, f_getreg},
1029 {"getreginfo", 0, 1, FEARG_1, NULL,
1030 ret_dict_any, f_getreginfo},
1031 {"getregtype", 0, 1, FEARG_1, NULL,
1032 ret_string, f_getregtype},
1033 {"gettabinfo", 0, 1, FEARG_1, NULL,
1034 ret_list_dict_any, f_gettabinfo},
1035 {"gettabvar", 2, 3, FEARG_1, NULL,
1036 ret_any, f_gettabvar},
1037 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1038 ret_any, f_gettabwinvar},
1039 {"gettagstack", 0, 1, FEARG_1, NULL,
1040 ret_dict_any, f_gettagstack},
1041 {"gettext", 1, 1, FEARG_1, NULL,
1042 ret_string, f_gettext},
1043 {"getwininfo", 0, 1, FEARG_1, NULL,
1044 ret_list_dict_any, f_getwininfo},
1045 {"getwinpos", 0, 1, FEARG_1, NULL,
1046 ret_list_number, f_getwinpos},
1047 {"getwinposx", 0, 0, 0, NULL,
1048 ret_number, f_getwinposx},
1049 {"getwinposy", 0, 0, 0, NULL,
1050 ret_number, f_getwinposy},
1051 {"getwinvar", 2, 3, FEARG_1, NULL,
1052 ret_any, f_getwinvar},
1053 {"glob", 1, 4, FEARG_1, NULL,
1054 ret_any, f_glob},
1055 {"glob2regpat", 1, 1, FEARG_1, NULL,
1056 ret_string, f_glob2regpat},
1057 {"globpath", 2, 5, FEARG_2, NULL,
1058 ret_any, f_globpath},
1059 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001060 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001061 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001062 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001063 {"haslocaldir", 0, 2, FEARG_1, NULL,
1064 ret_number, f_haslocaldir},
1065 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001066 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001067 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1068 ret_number, f_hlID},
1069 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001070 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001071 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001072 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001073 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001074 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001075 {"histget", 1, 2, FEARG_1, NULL,
1076 ret_string, f_histget},
1077 {"histnr", 1, 1, FEARG_1, NULL,
1078 ret_number, f_histnr},
1079 {"hlID", 1, 1, FEARG_1, NULL,
1080 ret_number, f_hlID},
1081 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001082 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001083 {"hostname", 0, 0, 0, NULL,
1084 ret_string, f_hostname},
1085 {"iconv", 3, 3, FEARG_1, NULL,
1086 ret_string, f_iconv},
1087 {"indent", 1, 1, FEARG_1, NULL,
1088 ret_number, f_indent},
1089 {"index", 2, 4, FEARG_1, NULL,
1090 ret_number, f_index},
1091 {"input", 1, 3, FEARG_1, NULL,
1092 ret_string, f_input},
1093 {"inputdialog", 1, 3, FEARG_1, NULL,
1094 ret_string, f_inputdialog},
1095 {"inputlist", 1, 1, FEARG_1, NULL,
1096 ret_number, f_inputlist},
1097 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001098 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001100 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"inputsecret", 1, 2, FEARG_1, NULL,
1102 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001103 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001104 ret_first_arg, f_insert},
1105 {"interrupt", 0, 0, 0, NULL,
1106 ret_void, f_interrupt},
1107 {"invert", 1, 1, FEARG_1, NULL,
1108 ret_number, f_invert},
1109 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001110 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001111 {"isinf", 1, 1, FEARG_1, NULL,
1112 ret_number, MATH_FUNC(f_isinf)},
1113 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001114 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001115 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001116 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001117 {"items", 1, 1, FEARG_1, NULL,
1118 ret_list_any, f_items},
1119 {"job_getchannel", 1, 1, FEARG_1, NULL,
1120 ret_channel, JOB_FUNC(f_job_getchannel)},
1121 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001122 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001123 {"job_setoptions", 2, 2, FEARG_1, NULL,
1124 ret_void, JOB_FUNC(f_job_setoptions)},
1125 {"job_start", 1, 2, FEARG_1, NULL,
1126 ret_job, JOB_FUNC(f_job_start)},
1127 {"job_status", 1, 1, FEARG_1, NULL,
1128 ret_string, JOB_FUNC(f_job_status)},
1129 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001130 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001131 {"join", 1, 2, FEARG_1, NULL,
1132 ret_string, f_join},
1133 {"js_decode", 1, 1, FEARG_1, NULL,
1134 ret_any, f_js_decode},
1135 {"js_encode", 1, 1, FEARG_1, NULL,
1136 ret_string, f_js_encode},
1137 {"json_decode", 1, 1, FEARG_1, NULL,
1138 ret_any, f_json_decode},
1139 {"json_encode", 1, 1, FEARG_1, NULL,
1140 ret_string, f_json_encode},
1141 {"keys", 1, 1, FEARG_1, NULL,
1142 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001143 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1144 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001145 {"len", 1, 1, FEARG_1, NULL,
1146 ret_number, f_len},
1147 {"libcall", 3, 3, FEARG_3, NULL,
1148 ret_string, f_libcall},
1149 {"libcallnr", 3, 3, FEARG_3, NULL,
1150 ret_number, f_libcallnr},
1151 {"line", 1, 2, FEARG_1, NULL,
1152 ret_number, f_line},
1153 {"line2byte", 1, 1, FEARG_1, NULL,
1154 ret_number, f_line2byte},
1155 {"lispindent", 1, 1, FEARG_1, NULL,
1156 ret_number, f_lispindent},
1157 {"list2str", 1, 2, FEARG_1, NULL,
1158 ret_string, f_list2str},
1159 {"listener_add", 1, 2, FEARG_2, NULL,
1160 ret_number, f_listener_add},
1161 {"listener_flush", 0, 1, FEARG_1, NULL,
1162 ret_void, f_listener_flush},
1163 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001164 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001165 {"localtime", 0, 0, 0, NULL,
1166 ret_number, f_localtime},
1167 {"log", 1, 1, FEARG_1, NULL,
1168 ret_float, FLOAT_FUNC(f_log)},
1169 {"log10", 1, 1, FEARG_1, NULL,
1170 ret_float, FLOAT_FUNC(f_log10)},
1171 {"luaeval", 1, 2, FEARG_1, NULL,
1172 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001173#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001174 f_luaeval
1175#else
1176 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001178 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001179 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001180 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001181 {"maparg", 1, 4, FEARG_1, NULL,
1182 ret_maparg, f_maparg},
1183 {"mapcheck", 1, 3, FEARG_1, NULL,
1184 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001185 {"mapnew", 2, 2, FEARG_1, NULL,
1186 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001187 {"mapset", 3, 3, FEARG_1, NULL,
1188 ret_void, f_mapset},
1189 {"match", 2, 4, FEARG_1, NULL,
1190 ret_any, f_match},
1191 {"matchadd", 2, 5, FEARG_1, NULL,
1192 ret_number, f_matchadd},
1193 {"matchaddpos", 2, 5, FEARG_1, NULL,
1194 ret_number, f_matchaddpos},
1195 {"matcharg", 1, 1, FEARG_1, NULL,
1196 ret_list_string, f_matcharg},
1197 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001198 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001199 {"matchend", 2, 4, FEARG_1, NULL,
1200 ret_number, f_matchend},
1201 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1202 ret_list_string, f_matchfuzzy},
1203 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1204 ret_list_any, f_matchfuzzypos},
1205 {"matchlist", 2, 4, FEARG_1, NULL,
1206 ret_list_string, f_matchlist},
1207 {"matchstr", 2, 4, FEARG_1, NULL,
1208 ret_string, f_matchstr},
1209 {"matchstrpos", 2, 4, FEARG_1, NULL,
1210 ret_list_any, f_matchstrpos},
1211 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001212 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001213 {"menu_info", 1, 2, FEARG_1, NULL,
1214 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001215#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001216 f_menu_info
1217#else
1218 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001219#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001220 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001221 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001222 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001223 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001224 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001225 {"mode", 0, 1, FEARG_1, NULL,
1226 ret_string, f_mode},
1227 {"mzeval", 1, 1, FEARG_1, NULL,
1228 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001230 f_mzeval
1231#else
1232 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001233#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001234 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001235 {"nextnonblank", 1, 1, FEARG_1, NULL,
1236 ret_number, f_nextnonblank},
1237 {"nr2char", 1, 2, FEARG_1, NULL,
1238 ret_string, f_nr2char},
1239 {"or", 2, 2, FEARG_1, NULL,
1240 ret_number, f_or},
1241 {"pathshorten", 1, 2, FEARG_1, NULL,
1242 ret_string, f_pathshorten},
1243 {"perleval", 1, 1, FEARG_1, NULL,
1244 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001246 f_perleval
1247#else
1248 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001250 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001251 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1252 ret_number, PROP_FUNC(f_popup_atcursor)},
1253 {"popup_beval", 2, 2, FEARG_1, NULL,
1254 ret_number, PROP_FUNC(f_popup_beval)},
1255 {"popup_clear", 0, 1, 0, NULL,
1256 ret_void, PROP_FUNC(f_popup_clear)},
1257 {"popup_close", 1, 2, FEARG_1, NULL,
1258 ret_void, PROP_FUNC(f_popup_close)},
1259 {"popup_create", 2, 2, FEARG_1, NULL,
1260 ret_number, PROP_FUNC(f_popup_create)},
1261 {"popup_dialog", 2, 2, FEARG_1, NULL,
1262 ret_number, PROP_FUNC(f_popup_dialog)},
1263 {"popup_filter_menu", 2, 2, 0, NULL,
1264 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1265 {"popup_filter_yesno", 2, 2, 0, NULL,
1266 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1267 {"popup_findinfo", 0, 0, 0, NULL,
1268 ret_number, PROP_FUNC(f_popup_findinfo)},
1269 {"popup_findpreview", 0, 0, 0, NULL,
1270 ret_number, PROP_FUNC(f_popup_findpreview)},
1271 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1272 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1273 {"popup_getpos", 1, 1, FEARG_1, NULL,
1274 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1275 {"popup_hide", 1, 1, FEARG_1, NULL,
1276 ret_void, PROP_FUNC(f_popup_hide)},
1277 {"popup_list", 0, 0, 0, NULL,
1278 ret_list_number, PROP_FUNC(f_popup_list)},
1279 {"popup_locate", 2, 2, 0, NULL,
1280 ret_number, PROP_FUNC(f_popup_locate)},
1281 {"popup_menu", 2, 2, FEARG_1, NULL,
1282 ret_number, PROP_FUNC(f_popup_menu)},
1283 {"popup_move", 2, 2, FEARG_1, NULL,
1284 ret_void, PROP_FUNC(f_popup_move)},
1285 {"popup_notification", 2, 2, FEARG_1, NULL,
1286 ret_number, PROP_FUNC(f_popup_notification)},
1287 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1288 ret_void, PROP_FUNC(f_popup_setoptions)},
1289 {"popup_settext", 2, 2, FEARG_1, NULL,
1290 ret_void, PROP_FUNC(f_popup_settext)},
1291 {"popup_show", 1, 1, FEARG_1, NULL,
1292 ret_void, PROP_FUNC(f_popup_show)},
1293 {"pow", 2, 2, FEARG_1, NULL,
1294 ret_float, FLOAT_FUNC(f_pow)},
1295 {"prevnonblank", 1, 1, FEARG_1, NULL,
1296 ret_number, f_prevnonblank},
1297 {"printf", 1, 19, FEARG_2, NULL,
1298 ret_string, f_printf},
1299 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1300 ret_string, JOB_FUNC(f_prompt_getprompt)},
1301 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1302 ret_void, JOB_FUNC(f_prompt_setcallback)},
1303 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1304 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1305 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1306 ret_void, JOB_FUNC(f_prompt_setprompt)},
1307 {"prop_add", 3, 3, FEARG_1, NULL,
1308 ret_void, PROP_FUNC(f_prop_add)},
1309 {"prop_clear", 1, 3, FEARG_1, NULL,
1310 ret_void, PROP_FUNC(f_prop_clear)},
1311 {"prop_find", 1, 2, FEARG_1, NULL,
1312 ret_dict_any, PROP_FUNC(f_prop_find)},
1313 {"prop_list", 1, 2, FEARG_1, NULL,
1314 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1315 {"prop_remove", 1, 3, FEARG_1, NULL,
1316 ret_number, PROP_FUNC(f_prop_remove)},
1317 {"prop_type_add", 2, 2, FEARG_1, NULL,
1318 ret_void, PROP_FUNC(f_prop_type_add)},
1319 {"prop_type_change", 2, 2, FEARG_1, NULL,
1320 ret_void, PROP_FUNC(f_prop_type_change)},
1321 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1322 ret_void, PROP_FUNC(f_prop_type_delete)},
1323 {"prop_type_get", 1, 2, FEARG_1, NULL,
1324 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1325 {"prop_type_list", 0, 1, FEARG_1, NULL,
1326 ret_list_string, PROP_FUNC(f_prop_type_list)},
1327 {"pum_getpos", 0, 0, 0, NULL,
1328 ret_dict_number, f_pum_getpos},
1329 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001330 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001331 {"py3eval", 1, 1, FEARG_1, NULL,
1332 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001333#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001334 f_py3eval
1335#else
1336 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001338 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001339 {"pyeval", 1, 1, FEARG_1, NULL,
1340 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001342 f_pyeval
1343#else
1344 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001346 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001347 {"pyxeval", 1, 1, FEARG_1, NULL,
1348 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001349#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001350 f_pyxeval
1351#else
1352 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001353#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001354 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001355 {"rand", 0, 1, FEARG_1, NULL,
1356 ret_number, f_rand},
1357 {"range", 1, 3, FEARG_1, NULL,
1358 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001359 {"readblob", 1, 1, FEARG_1, NULL,
1360 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001361 {"readdir", 1, 3, FEARG_1, NULL,
1362 ret_list_string, f_readdir},
1363 {"readdirex", 1, 3, FEARG_1, NULL,
1364 ret_list_dict_any, f_readdirex},
1365 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001366 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001367 {"reduce", 2, 3, FEARG_1, NULL,
1368 ret_any, f_reduce},
1369 {"reg_executing", 0, 0, 0, NULL,
1370 ret_string, f_reg_executing},
1371 {"reg_recording", 0, 0, 0, NULL,
1372 ret_string, f_reg_recording},
1373 {"reltime", 0, 2, FEARG_1, NULL,
1374 ret_list_any, f_reltime},
1375 {"reltimefloat", 1, 1, FEARG_1, NULL,
1376 ret_float, FLOAT_FUNC(f_reltimefloat)},
1377 {"reltimestr", 1, 1, FEARG_1, NULL,
1378 ret_string, f_reltimestr},
1379 {"remote_expr", 2, 4, FEARG_1, NULL,
1380 ret_string, f_remote_expr},
1381 {"remote_foreground", 1, 1, FEARG_1, NULL,
1382 ret_string, f_remote_foreground},
1383 {"remote_peek", 1, 2, FEARG_1, NULL,
1384 ret_number, f_remote_peek},
1385 {"remote_read", 1, 2, FEARG_1, NULL,
1386 ret_string, f_remote_read},
1387 {"remote_send", 2, 3, FEARG_1, NULL,
1388 ret_string, f_remote_send},
1389 {"remote_startserver", 1, 1, FEARG_1, NULL,
1390 ret_void, f_remote_startserver},
1391 {"remove", 2, 3, FEARG_1, NULL,
1392 ret_remove, f_remove},
1393 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001394 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001395 {"repeat", 2, 2, FEARG_1, NULL,
1396 ret_first_arg, f_repeat},
1397 {"resolve", 1, 1, FEARG_1, NULL,
1398 ret_string, f_resolve},
1399 {"reverse", 1, 1, FEARG_1, NULL,
1400 ret_first_arg, f_reverse},
1401 {"round", 1, 1, FEARG_1, NULL,
1402 ret_float, FLOAT_FUNC(f_round)},
1403 {"rubyeval", 1, 1, FEARG_1, NULL,
1404 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001405#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001406 f_rubyeval
1407#else
1408 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001410 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001411 {"screenattr", 2, 2, FEARG_1, NULL,
1412 ret_number, f_screenattr},
1413 {"screenchar", 2, 2, FEARG_1, NULL,
1414 ret_number, f_screenchar},
1415 {"screenchars", 2, 2, FEARG_1, NULL,
1416 ret_list_number, f_screenchars},
1417 {"screencol", 0, 0, 0, NULL,
1418 ret_number, f_screencol},
1419 {"screenpos", 3, 3, FEARG_1, NULL,
1420 ret_dict_number, f_screenpos},
1421 {"screenrow", 0, 0, 0, NULL,
1422 ret_number, f_screenrow},
1423 {"screenstring", 2, 2, FEARG_1, NULL,
1424 ret_string, f_screenstring},
1425 {"search", 1, 5, FEARG_1, NULL,
1426 ret_number, f_search},
1427 {"searchcount", 0, 1, FEARG_1, NULL,
1428 ret_dict_any, f_searchcount},
1429 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001430 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001431 {"searchpair", 3, 7, 0, NULL,
1432 ret_number, f_searchpair},
1433 {"searchpairpos", 3, 7, 0, NULL,
1434 ret_list_number, f_searchpairpos},
1435 {"searchpos", 1, 5, FEARG_1, NULL,
1436 ret_list_number, f_searchpos},
1437 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001438 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"serverlist", 0, 0, 0, NULL,
1440 ret_string, f_serverlist},
1441 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"setbufvar", 3, 3, FEARG_3, NULL,
1444 ret_void, f_setbufvar},
1445 {"setcellwidths", 1, 1, FEARG_1, NULL,
1446 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001447 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001448 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001449 {"setcharsearch", 1, 1, FEARG_1, NULL,
1450 ret_void, f_setcharsearch},
1451 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001452 ret_number_bool, f_setcmdpos},
1453 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1454 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001455 {"setenv", 2, 2, FEARG_2, NULL,
1456 ret_void, f_setenv},
1457 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001458 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001459 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001460 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001461 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001462 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001464 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001466 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001467 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001468 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001469 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001470 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001471 {"settabvar", 3, 3, FEARG_3, NULL,
1472 ret_void, f_settabvar},
1473 {"settabwinvar", 4, 4, FEARG_4, NULL,
1474 ret_void, f_settabwinvar},
1475 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001476 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001477 {"setwinvar", 3, 3, FEARG_3, NULL,
1478 ret_void, f_setwinvar},
1479 {"sha256", 1, 1, FEARG_1, NULL,
1480 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001481#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001482 f_sha256
1483#else
1484 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001485#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001486 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001487 {"shellescape", 1, 2, FEARG_1, NULL,
1488 ret_string, f_shellescape},
1489 {"shiftwidth", 0, 1, FEARG_1, NULL,
1490 ret_number, f_shiftwidth},
1491 {"sign_define", 1, 2, FEARG_1, NULL,
1492 ret_any, SIGN_FUNC(f_sign_define)},
1493 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1494 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1495 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1496 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1497 {"sign_jump", 3, 3, FEARG_1, NULL,
1498 ret_number, SIGN_FUNC(f_sign_jump)},
1499 {"sign_place", 4, 5, FEARG_1, NULL,
1500 ret_number, SIGN_FUNC(f_sign_place)},
1501 {"sign_placelist", 1, 1, FEARG_1, NULL,
1502 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1503 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001504 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001505 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001506 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001507 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1508 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1509 {"simplify", 1, 1, FEARG_1, NULL,
1510 ret_string, f_simplify},
1511 {"sin", 1, 1, FEARG_1, NULL,
1512 ret_float, FLOAT_FUNC(f_sin)},
1513 {"sinh", 1, 1, FEARG_1, NULL,
1514 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001515 {"slice", 2, 3, FEARG_1, NULL,
1516 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001517 {"sort", 1, 3, FEARG_1, NULL,
1518 ret_first_arg, f_sort},
1519 {"sound_clear", 0, 0, 0, NULL,
1520 ret_void, SOUND_FUNC(f_sound_clear)},
1521 {"sound_playevent", 1, 2, FEARG_1, NULL,
1522 ret_number, SOUND_FUNC(f_sound_playevent)},
1523 {"sound_playfile", 1, 2, FEARG_1, NULL,
1524 ret_number, SOUND_FUNC(f_sound_playfile)},
1525 {"sound_stop", 1, 1, FEARG_1, NULL,
1526 ret_void, SOUND_FUNC(f_sound_stop)},
1527 {"soundfold", 1, 1, FEARG_1, NULL,
1528 ret_string, f_soundfold},
1529 {"spellbadword", 0, 1, FEARG_1, NULL,
1530 ret_list_string, f_spellbadword},
1531 {"spellsuggest", 1, 3, FEARG_1, NULL,
1532 ret_list_string, f_spellsuggest},
1533 {"split", 1, 3, FEARG_1, NULL,
1534 ret_list_string, f_split},
1535 {"sqrt", 1, 1, FEARG_1, NULL,
1536 ret_float, FLOAT_FUNC(f_sqrt)},
1537 {"srand", 0, 1, FEARG_1, NULL,
1538 ret_list_number, f_srand},
1539 {"state", 0, 1, FEARG_1, NULL,
1540 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001541 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001542 ret_float, FLOAT_FUNC(f_str2float)},
1543 {"str2list", 1, 2, FEARG_1, NULL,
1544 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001545 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001546 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001547 {"strcharlen", 1, 1, FEARG_1, NULL,
1548 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001549 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001550 ret_string, f_strcharpart},
1551 {"strchars", 1, 2, FEARG_1, NULL,
1552 ret_number, f_strchars},
1553 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1554 ret_number, f_strdisplaywidth},
1555 {"strftime", 1, 2, FEARG_1, NULL,
1556 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001557#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001558 f_strftime
1559#else
1560 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001562 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001563 {"strgetchar", 2, 2, FEARG_1, NULL,
1564 ret_number, f_strgetchar},
1565 {"stridx", 2, 3, FEARG_1, NULL,
1566 ret_number, f_stridx},
1567 {"string", 1, 1, FEARG_1, NULL,
1568 ret_string, f_string},
1569 {"strlen", 1, 1, FEARG_1, NULL,
1570 ret_number, f_strlen},
1571 {"strpart", 2, 4, FEARG_1, NULL,
1572 ret_string, f_strpart},
1573 {"strptime", 2, 2, FEARG_1, NULL,
1574 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001575#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001576 f_strptime
1577#else
1578 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001579#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001580 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001581 {"strridx", 2, 3, FEARG_1, NULL,
1582 ret_number, f_strridx},
1583 {"strtrans", 1, 1, FEARG_1, NULL,
1584 ret_string, f_strtrans},
1585 {"strwidth", 1, 1, FEARG_1, NULL,
1586 ret_number, f_strwidth},
1587 {"submatch", 1, 2, FEARG_1, NULL,
1588 ret_string, f_submatch},
1589 {"substitute", 4, 4, FEARG_1, NULL,
1590 ret_string, f_substitute},
1591 {"swapinfo", 1, 1, FEARG_1, NULL,
1592 ret_dict_any, f_swapinfo},
1593 {"swapname", 1, 1, FEARG_1, NULL,
1594 ret_string, f_swapname},
1595 {"synID", 3, 3, 0, NULL,
1596 ret_number, f_synID},
1597 {"synIDattr", 2, 3, FEARG_1, NULL,
1598 ret_string, f_synIDattr},
1599 {"synIDtrans", 1, 1, FEARG_1, NULL,
1600 ret_number, f_synIDtrans},
1601 {"synconcealed", 2, 2, 0, NULL,
1602 ret_list_any, f_synconcealed},
1603 {"synstack", 2, 2, 0, NULL,
1604 ret_list_number, f_synstack},
1605 {"system", 1, 2, FEARG_1, NULL,
1606 ret_string, f_system},
1607 {"systemlist", 1, 2, FEARG_1, NULL,
1608 ret_list_string, f_systemlist},
1609 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1610 ret_list_number, f_tabpagebuflist},
1611 {"tabpagenr", 0, 1, 0, NULL,
1612 ret_number, f_tabpagenr},
1613 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1614 ret_number, f_tabpagewinnr},
1615 {"tagfiles", 0, 0, 0, NULL,
1616 ret_list_string, f_tagfiles},
1617 {"taglist", 1, 2, FEARG_1, NULL,
1618 ret_list_dict_any, f_taglist},
1619 {"tan", 1, 1, FEARG_1, NULL,
1620 ret_float, FLOAT_FUNC(f_tan)},
1621 {"tanh", 1, 1, FEARG_1, NULL,
1622 ret_float, FLOAT_FUNC(f_tanh)},
1623 {"tempname", 0, 0, 0, NULL,
1624 ret_string, f_tempname},
1625 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1626 ret_number, TERM_FUNC(f_term_dumpdiff)},
1627 {"term_dumpload", 1, 2, FEARG_1, NULL,
1628 ret_number, TERM_FUNC(f_term_dumpload)},
1629 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1630 ret_void, TERM_FUNC(f_term_dumpwrite)},
1631 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1632 ret_number, TERM_FUNC(f_term_getaltscreen)},
1633 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1634 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001635#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001636 f_term_getansicolors
1637#else
1638 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001639#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001640 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001641 {"term_getattr", 2, 2, FEARG_1, NULL,
1642 ret_number, TERM_FUNC(f_term_getattr)},
1643 {"term_getcursor", 1, 1, FEARG_1, NULL,
1644 ret_list_any, TERM_FUNC(f_term_getcursor)},
1645 {"term_getjob", 1, 1, FEARG_1, NULL,
1646 ret_job, TERM_FUNC(f_term_getjob)},
1647 {"term_getline", 2, 2, FEARG_1, NULL,
1648 ret_string, TERM_FUNC(f_term_getline)},
1649 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1650 ret_number, TERM_FUNC(f_term_getscrolled)},
1651 {"term_getsize", 1, 1, FEARG_1, NULL,
1652 ret_list_number, TERM_FUNC(f_term_getsize)},
1653 {"term_getstatus", 1, 1, FEARG_1, NULL,
1654 ret_string, TERM_FUNC(f_term_getstatus)},
1655 {"term_gettitle", 1, 1, FEARG_1, NULL,
1656 ret_string, TERM_FUNC(f_term_gettitle)},
1657 {"term_gettty", 1, 2, FEARG_1, NULL,
1658 ret_string, TERM_FUNC(f_term_gettty)},
1659 {"term_list", 0, 0, 0, NULL,
1660 ret_list_number, TERM_FUNC(f_term_list)},
1661 {"term_scrape", 2, 2, FEARG_1, NULL,
1662 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1663 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1664 ret_void, TERM_FUNC(f_term_sendkeys)},
1665 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1666 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001667#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001668 f_term_setansicolors
1669#else
1670 NULL
1671#endif
1672 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001673 {"term_setapi", 2, 2, FEARG_1, NULL,
1674 ret_void, TERM_FUNC(f_term_setapi)},
1675 {"term_setkill", 2, 2, FEARG_1, NULL,
1676 ret_void, TERM_FUNC(f_term_setkill)},
1677 {"term_setrestore", 2, 2, FEARG_1, NULL,
1678 ret_void, TERM_FUNC(f_term_setrestore)},
1679 {"term_setsize", 3, 3, FEARG_1, NULL,
1680 ret_void, TERM_FUNC(f_term_setsize)},
1681 {"term_start", 1, 2, FEARG_1, NULL,
1682 ret_number, TERM_FUNC(f_term_start)},
1683 {"term_wait", 1, 2, FEARG_1, NULL,
1684 ret_void, TERM_FUNC(f_term_wait)},
1685 {"terminalprops", 0, 0, 0, NULL,
1686 ret_dict_string, f_terminalprops},
1687 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1688 ret_void, f_test_alloc_fail},
1689 {"test_autochdir", 0, 0, 0, NULL,
1690 ret_void, f_test_autochdir},
1691 {"test_feedinput", 1, 1, FEARG_1, NULL,
1692 ret_void, f_test_feedinput},
1693 {"test_garbagecollect_now", 0, 0, 0, NULL,
1694 ret_void, f_test_garbagecollect_now},
1695 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1696 ret_void, f_test_garbagecollect_soon},
1697 {"test_getvalue", 1, 1, FEARG_1, NULL,
1698 ret_number, f_test_getvalue},
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001699 {"test_gui_mouse_event", 5, 5, 0, NULL,
1700 ret_void, f_test_gui_mouse_event},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001701 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1702 ret_void, f_test_ignore_error},
1703 {"test_null_blob", 0, 0, 0, NULL,
1704 ret_blob, f_test_null_blob},
1705 {"test_null_channel", 0, 0, 0, NULL,
1706 ret_channel, JOB_FUNC(f_test_null_channel)},
1707 {"test_null_dict", 0, 0, 0, NULL,
1708 ret_dict_any, f_test_null_dict},
1709 {"test_null_function", 0, 0, 0, NULL,
1710 ret_func_any, f_test_null_function},
1711 {"test_null_job", 0, 0, 0, NULL,
1712 ret_job, JOB_FUNC(f_test_null_job)},
1713 {"test_null_list", 0, 0, 0, NULL,
1714 ret_list_any, f_test_null_list},
1715 {"test_null_partial", 0, 0, 0, NULL,
1716 ret_func_any, f_test_null_partial},
1717 {"test_null_string", 0, 0, 0, NULL,
1718 ret_string, f_test_null_string},
1719 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1720 ret_void, f_test_option_not_set},
1721 {"test_override", 2, 2, FEARG_2, NULL,
1722 ret_void, f_test_override},
1723 {"test_refcount", 1, 1, FEARG_1, NULL,
1724 ret_number, f_test_refcount},
1725 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1726 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001727#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001728 f_test_scrollbar
1729#else
1730 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001731#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001732 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001733 {"test_setmouse", 2, 2, 0, NULL,
1734 ret_void, f_test_setmouse},
1735 {"test_settime", 1, 1, FEARG_1, NULL,
1736 ret_void, f_test_settime},
1737 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1738 ret_void, f_test_srand_seed},
1739 {"test_unknown", 0, 0, 0, NULL,
1740 ret_any, f_test_unknown},
1741 {"test_void", 0, 0, 0, NULL,
1742 ret_void, f_test_void},
1743 {"timer_info", 0, 1, FEARG_1, NULL,
1744 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1745 {"timer_pause", 2, 2, FEARG_1, NULL,
1746 ret_void, TIMER_FUNC(f_timer_pause)},
1747 {"timer_start", 2, 3, FEARG_1, NULL,
1748 ret_number, TIMER_FUNC(f_timer_start)},
1749 {"timer_stop", 1, 1, FEARG_1, NULL,
1750 ret_void, TIMER_FUNC(f_timer_stop)},
1751 {"timer_stopall", 0, 0, 0, NULL,
1752 ret_void, TIMER_FUNC(f_timer_stopall)},
1753 {"tolower", 1, 1, FEARG_1, NULL,
1754 ret_string, f_tolower},
1755 {"toupper", 1, 1, FEARG_1, NULL,
1756 ret_string, f_toupper},
1757 {"tr", 3, 3, FEARG_1, NULL,
1758 ret_string, f_tr},
1759 {"trim", 1, 3, FEARG_1, NULL,
1760 ret_string, f_trim},
1761 {"trunc", 1, 1, FEARG_1, NULL,
1762 ret_float, FLOAT_FUNC(f_trunc)},
1763 {"type", 1, 1, FEARG_1, NULL,
1764 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001765 {"typename", 1, 1, FEARG_1, NULL,
1766 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001767 {"undofile", 1, 1, FEARG_1, NULL,
1768 ret_string, f_undofile},
1769 {"undotree", 0, 0, 0, NULL,
1770 ret_dict_any, f_undotree},
1771 {"uniq", 1, 3, FEARG_1, NULL,
1772 ret_list_any, f_uniq},
1773 {"values", 1, 1, FEARG_1, NULL,
1774 ret_list_any, f_values},
1775 {"virtcol", 1, 1, FEARG_1, NULL,
1776 ret_number, f_virtcol},
1777 {"visualmode", 0, 1, 0, NULL,
1778 ret_string, f_visualmode},
1779 {"wildmenumode", 0, 0, 0, NULL,
1780 ret_number, f_wildmenumode},
1781 {"win_execute", 2, 3, FEARG_2, NULL,
1782 ret_string, f_win_execute},
1783 {"win_findbuf", 1, 1, FEARG_1, NULL,
1784 ret_list_number, f_win_findbuf},
1785 {"win_getid", 0, 2, FEARG_1, NULL,
1786 ret_number, f_win_getid},
1787 {"win_gettype", 0, 1, FEARG_1, NULL,
1788 ret_string, f_win_gettype},
1789 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001790 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001791 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1792 ret_list_number, f_win_id2tabwin},
1793 {"win_id2win", 1, 1, FEARG_1, NULL,
1794 ret_number, f_win_id2win},
1795 {"win_screenpos", 1, 1, FEARG_1, NULL,
1796 ret_list_number, f_win_screenpos},
1797 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001798 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001799 {"winbufnr", 1, 1, FEARG_1, NULL,
1800 ret_number, f_winbufnr},
1801 {"wincol", 0, 0, 0, NULL,
1802 ret_number, f_wincol},
1803 {"windowsversion", 0, 0, 0, NULL,
1804 ret_string, f_windowsversion},
1805 {"winheight", 1, 1, FEARG_1, NULL,
1806 ret_number, f_winheight},
1807 {"winlayout", 0, 1, FEARG_1, NULL,
1808 ret_list_any, f_winlayout},
1809 {"winline", 0, 0, 0, NULL,
1810 ret_number, f_winline},
1811 {"winnr", 0, 1, FEARG_1, NULL,
1812 ret_number, f_winnr},
1813 {"winrestcmd", 0, 0, 0, NULL,
1814 ret_string, f_winrestcmd},
1815 {"winrestview", 1, 1, FEARG_1, NULL,
1816 ret_void, f_winrestview},
1817 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001818 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001819 {"winwidth", 1, 1, FEARG_1, NULL,
1820 ret_number, f_winwidth},
1821 {"wordcount", 0, 0, 0, NULL,
1822 ret_dict_number, f_wordcount},
1823 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001824 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001825 {"xor", 2, 2, FEARG_1, NULL,
1826 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001827};
1828
K.Takataeecf2b32021-06-02 14:56:39 +02001829#if defined(EBCDIC) || defined(PROTO)
1830/*
1831 * Compare funcentry_T by function name.
1832 */
1833 static int
1834compare_func_name(const void *s1, const void *s2)
1835{
1836 funcentry_T *p1 = (funcentry_T *)s1;
1837 funcentry_T *p2 = (funcentry_T *)s2;
1838
1839 return STRCMP(p1->f_name, p2->f_name);
1840}
1841
1842/*
1843 * Sort the function table by function name.
1844 * The sorting of the table above is ASCII dependent.
1845 * On machines using EBCDIC we have to sort it.
1846 */
1847 void
1848sortFunctions(void)
1849{
1850 size_t funcCnt = ARRAY_LENGTH(global_functions);
1851
1852 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1853}
1854#endif
1855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001856/*
1857 * Function given to ExpandGeneric() to obtain the list of internal
1858 * or user defined function names.
1859 */
1860 char_u *
1861get_function_name(expand_T *xp, int idx)
1862{
1863 static int intidx = -1;
1864 char_u *name;
1865
1866 if (idx == 0)
1867 intidx = -1;
1868 if (intidx < 0)
1869 {
1870 name = get_user_func_name(xp, idx);
1871 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001872 {
1873 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1874 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001876 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 }
K.Takataeeec2542021-06-02 13:28:16 +02001878 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001880 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001882 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 STRCAT(IObuff, ")");
1884 return IObuff;
1885 }
1886
1887 return NULL;
1888}
1889
1890/*
1891 * Function given to ExpandGeneric() to obtain the list of internal or
1892 * user defined variable or function names.
1893 */
1894 char_u *
1895get_expr_name(expand_T *xp, int idx)
1896{
1897 static int intidx = -1;
1898 char_u *name;
1899
1900 if (idx == 0)
1901 intidx = -1;
1902 if (intidx < 0)
1903 {
1904 name = get_function_name(xp, idx);
1905 if (name != NULL)
1906 return name;
1907 }
1908 return get_user_var_name(xp, ++intidx);
1909}
1910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001912 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001913 * Return index, or -1 if not found or "implemented" is TRUE and the function
1914 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001916 static int
1917find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918{
1919 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001920 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 int cmp;
1922 int x;
1923
K.Takataeeec2542021-06-02 13:28:16 +02001924 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001925
1926 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 while (first <= last)
1928 {
1929 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001930 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 if (cmp < 0)
1932 last = x - 1;
1933 else if (cmp > 0)
1934 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001935 else if (implemented && global_functions[x].f_func == NULL)
1936 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 else
1938 return x;
1939 }
1940 return -1;
1941}
1942
Bram Moolenaar15c47602020-03-26 22:16:48 +01001943/*
1944 * Find internal function "name" in table "global_functions".
1945 * Return index, or -1 if not found or the function is not implemented.
1946 */
1947 int
1948find_internal_func(char_u *name)
1949{
1950 return find_internal_func_opt(name, TRUE);
1951}
1952
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001953 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001954has_internal_func(char_u *name)
1955{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001956 return find_internal_func_opt(name, TRUE) >= 0;
1957}
1958
1959 static int
1960has_internal_func_name(char_u *name)
1961{
1962 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001963}
1964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 char *
1966internal_func_name(int idx)
1967{
1968 return global_functions[idx].f_name;
1969}
1970
Bram Moolenaar94738d82020-10-21 14:25:07 +02001971/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001972 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001973 * Uses the list of types on the type stack: "types".
1974 * Return FAIL and gives an error message when a type is wrong.
1975 */
1976 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001977internal_func_check_arg_types(
1978 type_T **types,
1979 int idx,
1980 int argcount,
1981 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001982{
1983 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1984 int i;
1985
1986 if (argchecks != NULL)
1987 {
1988 argcontext_T context;
1989
1990 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001991 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001992 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001993 for (i = 0; i < argcount; ++i)
1994 if (argchecks[i] != NULL)
1995 {
1996 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001997 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001998 return FAIL;
1999 }
2000 }
2001 return OK;
2002}
2003
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002004/*
2005 * Call the "f_retfunc" function to obtain the return type of function "idx".
2006 * "argtypes" is the list of argument types or NULL when there are no
2007 * arguments.
2008 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002010internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002012 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013}
2014
2015/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002016 * Return TRUE if "idx" is for the map() function.
2017 */
2018 int
2019internal_func_is_map(int idx)
2020{
2021 return global_functions[idx].f_func == f_map;
2022}
2023
2024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002026 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2027 * first argument, 2 if method base is second argument, etc. 9 if method base
2028 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 */
2030 int
2031check_internal_func(int idx, int argcount)
2032{
2033 int res;
2034 char *name;
2035
2036 if (argcount < global_functions[idx].f_min_argc)
2037 res = FCERR_TOOFEW;
2038 else if (argcount > global_functions[idx].f_max_argc)
2039 res = FCERR_TOOMANY;
2040 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002041 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042
2043 name = internal_func_name(idx);
2044 if (res == FCERR_TOOMANY)
2045 semsg(_(e_toomanyarg), name);
2046 else
2047 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002048 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049}
2050
Bram Moolenaarac92e252019-08-03 21:58:38 +02002051 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052call_internal_func(
2053 char_u *name,
2054 int argcount,
2055 typval_T *argvars,
2056 typval_T *rettv)
2057{
2058 int i;
2059
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002060 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002062 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002065 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002068 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002069 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002070}
2071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 void
2073call_internal_func_by_idx(
2074 int idx,
2075 typval_T *argvars,
2076 typval_T *rettv)
2077{
2078 global_functions[idx].f_func(argvars, rettv);
2079}
2080
Bram Moolenaarac92e252019-08-03 21:58:38 +02002081/*
2082 * Invoke a method for base->method().
2083 */
2084 int
2085call_internal_method(
2086 char_u *name,
2087 int argcount,
2088 typval_T *argvars,
2089 typval_T *rettv,
2090 typval_T *basetv)
2091{
2092 int i;
2093 int fi;
2094 typval_T argv[MAX_FUNC_ARGS + 1];
2095
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002096 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002097 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002098 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002099 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002100 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002103 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002105
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002106 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002107 {
2108 // base value goes last
2109 for (i = 0; i < argcount; ++i)
2110 argv[i] = argvars[i];
2111 argv[argcount] = *basetv;
2112 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002113 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 {
2115 // base value goes second
2116 argv[0] = argvars[0];
2117 argv[1] = *basetv;
2118 for (i = 1; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002121 else if (global_functions[fi].f_argtype == FEARG_3)
2122 {
2123 // base value goes third
2124 argv[0] = argvars[0];
2125 argv[1] = argvars[1];
2126 argv[2] = *basetv;
2127 for (i = 2; i < argcount; ++i)
2128 argv[i + 1] = argvars[i];
2129 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002130 else if (global_functions[fi].f_argtype == FEARG_4)
2131 {
2132 // base value goes fourth
2133 argv[0] = argvars[0];
2134 argv[1] = argvars[1];
2135 argv[2] = argvars[2];
2136 argv[3] = *basetv;
2137 for (i = 3; i < argcount; ++i)
2138 argv[i + 1] = argvars[i];
2139 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002140 else
2141 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002142 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002143 argv[0] = *basetv;
2144 for (i = 0; i < argcount; ++i)
2145 argv[i + 1] = argvars[i];
2146 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002147 argv[argcount + 1].v_type = VAR_UNKNOWN;
2148
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002149 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002150 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151}
2152
2153/*
2154 * Return TRUE for a non-zero Number and a non-empty String.
2155 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002156 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157non_zero_arg(typval_T *argvars)
2158{
2159 return ((argvars[0].v_type == VAR_NUMBER
2160 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002161 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002162 && argvars[0].vval.v_number == VVAL_TRUE)
2163 || (argvars[0].v_type == VAR_STRING
2164 && argvars[0].vval.v_string != NULL
2165 && *argvars[0].vval.v_string != NUL));
2166}
2167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 * "and(expr, expr)" function
2170 */
2171 static void
2172f_and(typval_T *argvars, typval_T *rettv)
2173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002174 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2175 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002176}
2177
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002179 * "balloon_show()" function
2180 */
2181#ifdef FEAT_BEVAL
2182 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002183f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2184{
2185 rettv->v_type = VAR_STRING;
2186 if (balloonEval != NULL)
2187 {
2188 if (balloonEval->msg == NULL)
2189 rettv->vval.v_string = NULL;
2190 else
2191 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2192 }
2193}
2194
2195 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002196f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2197{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002198 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002199 {
2200 if (argvars[0].v_type == VAR_LIST
2201# ifdef FEAT_GUI
2202 && !gui.in_use
2203# endif
2204 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002205 {
2206 list_T *l = argvars[0].vval.v_list;
2207
2208 // empty list removes the balloon
2209 post_balloon(balloonEval, NULL,
2210 l == NULL || l->lv_len == 0 ? NULL : l);
2211 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002212 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002213 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002214 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002215
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002216 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2217 return;
2218
2219 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002220 if (mesg != NULL)
2221 // empty string removes the balloon
2222 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2223 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002224 }
2225}
2226
Bram Moolenaar669a8282017-11-19 20:13:05 +01002227# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002228 static void
2229f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2230{
2231 if (rettv_list_alloc(rettv) == OK)
2232 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002233 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002234
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002235 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2236 return;
2237 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002238 if (msg != NULL)
2239 {
2240 pumitem_T *array;
2241 int size = split_message(msg, &array);
2242 int i;
2243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002244 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002245 for (i = 1; i < size - 1; ++i)
2246 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002247 while (size > 0)
2248 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002249 vim_free(array);
2250 }
2251 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002252}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002253# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002254#endif
2255
2256/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002257 * Get the buffer from "arg" and give an error and return NULL if it is not
2258 * valid.
2259 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002260 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002261get_buf_arg(typval_T *arg)
2262{
2263 buf_T *buf;
2264
2265 ++emsg_off;
2266 buf = tv_get_buf(arg, FALSE);
2267 --emsg_off;
2268 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002269 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002270 return buf;
2271}
2272
2273/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002274 * "byte2line(byte)" function
2275 */
2276 static void
2277f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2278{
2279#ifndef FEAT_BYTEOFF
2280 rettv->vval.v_number = -1;
2281#else
2282 long boff = 0;
2283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002284 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002285 if (boff < 0)
2286 rettv->vval.v_number = -1;
2287 else
2288 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2289 (linenr_T)0, &boff);
2290#endif
2291}
2292
2293 static void
2294byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2295{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002297 char_u *str;
2298 varnumber_T idx;
2299
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002300 str = tv_get_string_chk(&argvars[0]);
2301 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302 rettv->vval.v_number = -1;
2303 if (str == NULL || idx < 0)
2304 return;
2305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 t = str;
2307 for ( ; idx > 0; idx--)
2308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002309 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002310 return;
2311 if (enc_utf8 && comp)
2312 t += utf_ptr2len(t);
2313 else
2314 t += (*mb_ptr2len)(t);
2315 }
2316 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002317}
2318
2319/*
2320 * "byteidx()" function
2321 */
2322 static void
2323f_byteidx(typval_T *argvars, typval_T *rettv)
2324{
2325 byteidx(argvars, rettv, FALSE);
2326}
2327
2328/*
2329 * "byteidxcomp()" function
2330 */
2331 static void
2332f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2333{
2334 byteidx(argvars, rettv, TRUE);
2335}
2336
2337/*
2338 * "call(func, arglist [, dict])" function
2339 */
2340 static void
2341f_call(typval_T *argvars, typval_T *rettv)
2342{
2343 char_u *func;
2344 partial_T *partial = NULL;
2345 dict_T *selfdict = NULL;
2346
2347 if (argvars[1].v_type != VAR_LIST)
2348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002349 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 return;
2351 }
2352 if (argvars[1].vval.v_list == NULL)
2353 return;
2354
2355 if (argvars[0].v_type == VAR_FUNC)
2356 func = argvars[0].vval.v_string;
2357 else if (argvars[0].v_type == VAR_PARTIAL)
2358 {
2359 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002360 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 }
2362 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002363 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002364 if (func == NULL || *func == NUL)
2365 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002366
2367 if (argvars[2].v_type != VAR_UNKNOWN)
2368 {
2369 if (argvars[2].v_type != VAR_DICT)
2370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 return;
2373 }
2374 selfdict = argvars[2].vval.v_dict;
2375 }
2376
2377 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2378}
2379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380/*
2381 * "changenr()" function
2382 */
2383 static void
2384f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2385{
2386 rettv->vval.v_number = curbuf->b_u_seq_cur;
2387}
2388
2389/*
2390 * "char2nr(string)" function
2391 */
2392 static void
2393f_char2nr(typval_T *argvars, typval_T *rettv)
2394{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002395 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2396 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 if (has_mbyte)
2398 {
2399 int utf8 = 0;
2400
2401 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002402 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403
2404 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002405 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002407 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002408 }
2409 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002410 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411}
2412
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002413/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002414 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2415 * returns the character index of the column. Otherwise, returns the byte index
2416 * of the column.
2417 */
2418 static void
2419get_col(typval_T *argvars, typval_T *rettv, int charcol)
2420{
2421 colnr_T col = 0;
2422 pos_T *fp;
2423 int fnum = curbuf->b_fnum;
2424
2425 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2426 if (fp != NULL && fnum == curbuf->b_fnum)
2427 {
2428 if (fp->col == MAXCOL)
2429 {
2430 // '> can be MAXCOL, get the length of the line then
2431 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2432 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2433 else
2434 col = MAXCOL;
2435 }
2436 else
2437 {
2438 col = fp->col + 1;
2439 // col(".") when the cursor is on the NUL at the end of the line
2440 // because of "coladd" can be seen as an extra column.
2441 if (virtual_active() && fp == &curwin->w_cursor)
2442 {
2443 char_u *p = ml_get_cursor();
2444
2445 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2446 curwin->w_virtcol - curwin->w_cursor.coladd))
2447 {
2448 int l;
2449
2450 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2451 col += l;
2452 }
2453 }
2454 }
2455 }
2456 rettv->vval.v_number = col;
2457}
2458
2459/*
2460 * "charcol()" function
2461 */
2462 static void
2463f_charcol(typval_T *argvars, typval_T *rettv)
2464{
2465 get_col(argvars, rettv, TRUE);
2466}
2467
2468/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002469 * "charidx()" function
2470 */
2471 static void
2472f_charidx(typval_T *argvars, typval_T *rettv)
2473{
2474 char_u *str;
2475 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002476 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002477 char_u *p;
2478 int len;
2479 int (*ptr2len)(char_u *);
2480
2481 rettv->vval.v_number = -1;
2482
2483 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2484 || (argvars[2].v_type != VAR_UNKNOWN
2485 && argvars[2].v_type != VAR_NUMBER))
2486 {
2487 emsg(_(e_invarg));
2488 return;
2489 }
2490
2491 str = tv_get_string_chk(&argvars[0]);
2492 idx = tv_get_number_chk(&argvars[1], NULL);
2493 if (str == NULL || idx < 0)
2494 return;
2495
2496 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002497 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002498 if (countcc < 0 || countcc > 1)
2499 {
2500 semsg(_(e_using_number_as_bool_nr), countcc);
2501 return;
2502 }
2503
2504 if (enc_utf8 && countcc)
2505 ptr2len = utf_ptr2len;
2506 else
2507 ptr2len = mb_ptr2len;
2508
2509 for (p = str, len = 0; p <= str + idx; len++)
2510 {
2511 if (*p == NUL)
2512 return;
2513 p += ptr2len(p);
2514 }
2515
2516 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2517}
2518
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002519 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002520get_optional_window(typval_T *argvars, int idx)
2521{
2522 win_T *win = curwin;
2523
2524 if (argvars[idx].v_type != VAR_UNKNOWN)
2525 {
2526 win = find_win_by_nr_or_id(&argvars[idx]);
2527 if (win == NULL)
2528 {
2529 emsg(_(e_invalwindow));
2530 return NULL;
2531 }
2532 }
2533 return win;
2534}
2535
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 * "col(string)" function
2538 */
2539 static void
2540f_col(typval_T *argvars, typval_T *rettv)
2541{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002542 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543}
2544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545/*
2546 * "confirm(message, buttons[, default [, type]])" function
2547 */
2548 static void
2549f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2550{
2551#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2552 char_u *message;
2553 char_u *buttons = NULL;
2554 char_u buf[NUMBUFLEN];
2555 char_u buf2[NUMBUFLEN];
2556 int def = 1;
2557 int type = VIM_GENERIC;
2558 char_u *typestr;
2559 int error = FALSE;
2560
Bram Moolenaarc5809432021-03-27 21:23:30 +01002561 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2562 return;
2563
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002564 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 if (message == NULL)
2566 error = TRUE;
2567 if (argvars[1].v_type != VAR_UNKNOWN)
2568 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002569 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2570 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002571 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002572 if (buttons == NULL)
2573 error = TRUE;
2574 if (argvars[2].v_type != VAR_UNKNOWN)
2575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002576 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002577 if (argvars[3].v_type != VAR_UNKNOWN)
2578 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002579 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2580 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002581 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 if (typestr == NULL)
2583 error = TRUE;
2584 else
2585 {
2586 switch (TOUPPER_ASC(*typestr))
2587 {
2588 case 'E': type = VIM_ERROR; break;
2589 case 'Q': type = VIM_QUESTION; break;
2590 case 'I': type = VIM_INFO; break;
2591 case 'W': type = VIM_WARNING; break;
2592 case 'G': type = VIM_GENERIC; break;
2593 }
2594 }
2595 }
2596 }
2597 }
2598
2599 if (buttons == NULL || *buttons == NUL)
2600 buttons = (char_u *)_("&Ok");
2601
2602 if (!error)
2603 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2604 def, NULL, FALSE);
2605#endif
2606}
2607
2608/*
2609 * "copy()" function
2610 */
2611 static void
2612f_copy(typval_T *argvars, typval_T *rettv)
2613{
2614 item_copy(&argvars[0], rettv, FALSE, 0);
2615}
2616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002618 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002619 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002620 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 */
2622 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002623set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624{
2625 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002626 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 int set_curswant = TRUE;
2628
2629 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002630 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 {
2632 pos_T pos;
2633 colnr_T curswant = -1;
2634
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002635 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002637 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 return;
2639 }
2640 line = pos.lnum;
2641 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002643 if (curswant >= 0)
2644 {
2645 curwin->w_curswant = curswant - 1;
2646 set_curswant = FALSE;
2647 }
2648 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002649 else if ((argvars[0].v_type == VAR_NUMBER ||
2650 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002651 && (argvars[1].v_type == VAR_NUMBER ||
2652 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002654 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002655 if (line < 0)
2656 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002657 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002658 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002659 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002661 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002663 else
2664 {
2665 emsg(_(e_invarg));
2666 return;
2667 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002668 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002669 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 if (line > 0)
2671 curwin->w_cursor.lnum = line;
2672 if (col > 0)
2673 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002676 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002678 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 if (has_mbyte)
2680 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681
2682 curwin->w_set_curswant = set_curswant;
2683 rettv->vval.v_number = 0;
2684}
2685
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002686/*
2687 * "cursor(lnum, col)" function, or
2688 * "cursor(list)"
2689 *
2690 * Moves the cursor to the specified line and column.
2691 * Returns 0 when the position could be set, -1 otherwise.
2692 */
2693 static void
2694f_cursor(typval_T *argvars, typval_T *rettv)
2695{
2696 set_cursorpos(argvars, rettv, FALSE);
2697}
2698
Bram Moolenaar4f974752019-02-17 17:44:42 +01002699#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002700/*
2701 * "debugbreak()" function
2702 */
2703 static void
2704f_debugbreak(typval_T *argvars, typval_T *rettv)
2705{
2706 int pid;
2707
2708 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002709 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002710 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002711 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002712 else
2713 {
2714 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2715
2716 if (hProcess != NULL)
2717 {
2718 DebugBreakProcess(hProcess);
2719 CloseHandle(hProcess);
2720 rettv->vval.v_number = OK;
2721 }
2722 }
2723}
2724#endif
2725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726/*
2727 * "deepcopy()" function
2728 */
2729 static void
2730f_deepcopy(typval_T *argvars, typval_T *rettv)
2731{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002732 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 int copyID;
2734
2735 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002736 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002737 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002738 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 else
2740 {
2741 copyID = get_copyID();
2742 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2743 }
2744}
2745
2746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 * "did_filetype()" function
2748 */
2749 static void
2750f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2751{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002752 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753}
2754
2755/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002756 * "echoraw({expr})" function
2757 */
2758 static void
2759f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2760{
2761 char_u *str = tv_get_string_chk(&argvars[0]);
2762
2763 if (str != NULL && *str != NUL)
2764 {
2765 out_str(str);
2766 out_flush();
2767 }
2768}
2769
2770/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 * "empty({expr})" function
2772 */
2773 static void
2774f_empty(typval_T *argvars, typval_T *rettv)
2775{
2776 int n = FALSE;
2777
2778 switch (argvars[0].v_type)
2779 {
2780 case VAR_STRING:
2781 case VAR_FUNC:
2782 n = argvars[0].vval.v_string == NULL
2783 || *argvars[0].vval.v_string == NUL;
2784 break;
2785 case VAR_PARTIAL:
2786 n = FALSE;
2787 break;
2788 case VAR_NUMBER:
2789 n = argvars[0].vval.v_number == 0;
2790 break;
2791 case VAR_FLOAT:
2792#ifdef FEAT_FLOAT
2793 n = argvars[0].vval.v_float == 0.0;
2794 break;
2795#endif
2796 case VAR_LIST:
2797 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002798 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 break;
2800 case VAR_DICT:
2801 n = argvars[0].vval.v_dict == NULL
2802 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2803 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002804 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 case VAR_SPECIAL:
2806 n = argvars[0].vval.v_number != VVAL_TRUE;
2807 break;
2808
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002809 case VAR_BLOB:
2810 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002811 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2812 break;
2813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 case VAR_JOB:
2815#ifdef FEAT_JOB_CHANNEL
2816 n = argvars[0].vval.v_job == NULL
2817 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2818 break;
2819#endif
2820 case VAR_CHANNEL:
2821#ifdef FEAT_JOB_CHANNEL
2822 n = argvars[0].vval.v_channel == NULL
2823 || !channel_is_open(argvars[0].vval.v_channel);
2824 break;
2825#endif
2826 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002827 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002829 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002830 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002831 n = TRUE;
2832 break;
2833 }
2834
2835 rettv->vval.v_number = n;
2836}
2837
2838/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002839 * "environ()" function
2840 */
2841 static void
2842f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2843{
2844#if !defined(AMIGA)
2845 int i = 0;
2846 char_u *entry, *value;
2847# ifdef MSWIN
2848 extern wchar_t **_wenviron;
2849# else
2850 extern char **environ;
2851# endif
2852
2853 if (rettv_dict_alloc(rettv) != OK)
2854 return;
2855
2856# ifdef MSWIN
2857 if (*_wenviron == NULL)
2858 return;
2859# else
2860 if (*environ == NULL)
2861 return;
2862# endif
2863
2864 for (i = 0; ; ++i)
2865 {
2866# ifdef MSWIN
2867 short_u *p;
2868
2869 if ((p = (short_u *)_wenviron[i]) == NULL)
2870 return;
2871 entry = utf16_to_enc(p, NULL);
2872# else
2873 if ((entry = (char_u *)environ[i]) == NULL)
2874 return;
2875 entry = vim_strsave(entry);
2876# endif
2877 if (entry == NULL) // out of memory
2878 return;
2879 if ((value = vim_strchr(entry, '=')) == NULL)
2880 {
2881 vim_free(entry);
2882 continue;
2883 }
2884 *value++ = NUL;
2885 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2886 vim_free(entry);
2887 }
2888#endif
2889}
2890
2891/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 * "escape({string}, {chars})" function
2893 */
2894 static void
2895f_escape(typval_T *argvars, typval_T *rettv)
2896{
2897 char_u buf[NUMBUFLEN];
2898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002899 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2900 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901 rettv->v_type = VAR_STRING;
2902}
2903
2904/*
2905 * "eval()" function
2906 */
2907 static void
2908f_eval(typval_T *argvars, typval_T *rettv)
2909{
2910 char_u *s, *p;
2911
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002912 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 if (s != NULL)
2914 s = skipwhite(s);
2915
2916 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002918 {
2919 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002920 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002921 need_clr_eos = FALSE;
2922 rettv->v_type = VAR_NUMBER;
2923 rettv->vval.v_number = 0;
2924 }
2925 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002926 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927}
2928
2929/*
2930 * "eventhandler()" function
2931 */
2932 static void
2933f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2934{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002935 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936}
2937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938static garray_T redir_execute_ga;
2939
2940/*
2941 * Append "value[value_len]" to the execute() output.
2942 */
2943 void
2944execute_redir_str(char_u *value, int value_len)
2945{
2946 int len;
2947
2948 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002949 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002951 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 if (ga_grow(&redir_execute_ga, len) == OK)
2953 {
2954 mch_memmove((char *)redir_execute_ga.ga_data
2955 + redir_execute_ga.ga_len, value, len);
2956 redir_execute_ga.ga_len += len;
2957 }
2958}
2959
2960/*
2961 * Get next line from a list.
2962 * Called by do_cmdline() to get the next line.
2963 * Returns allocated string, or NULL for end of function.
2964 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965 static char_u *
2966get_list_line(
2967 int c UNUSED,
2968 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002969 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002970 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971{
2972 listitem_T **p = (listitem_T **)cookie;
2973 listitem_T *item = *p;
2974 char_u buf[NUMBUFLEN];
2975 char_u *s;
2976
2977 if (item == NULL)
2978 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002980 *p = item->li_next;
2981 return s == NULL ? NULL : vim_strsave(s);
2982}
2983
2984/*
2985 * "execute()" function
2986 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002987 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002988execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989{
2990 char_u *cmd = NULL;
2991 list_T *list = NULL;
2992 int save_msg_silent = msg_silent;
2993 int save_emsg_silent = emsg_silent;
2994 int save_emsg_noredir = emsg_noredir;
2995 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002996 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002998 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002999 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000
3001 rettv->vval.v_string = NULL;
3002 rettv->v_type = VAR_STRING;
3003
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003004 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003006 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003007 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003008 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 return;
3010 ++list->lv_refcount;
3011 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003012 else if (argvars[arg_off].v_type == VAR_JOB
3013 || argvars[arg_off].v_type == VAR_CHANNEL)
3014 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003015 semsg(_(e_using_invalid_value_as_string_str),
3016 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003017 return;
3018 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 else
3020 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003021 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 if (cmd == NULL)
3023 return;
3024 }
3025
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003026 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 {
3028 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003029 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3030 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031
3032 if (s == NULL)
3033 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003034 if (*s == NUL)
3035 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 if (STRNCMP(s, "silent", 6) == 0)
3037 ++msg_silent;
3038 if (STRCMP(s, "silent!") == 0)
3039 {
3040 emsg_silent = TRUE;
3041 emsg_noredir = TRUE;
3042 }
3043 }
3044 else
3045 ++msg_silent;
3046
3047 if (redir_execute)
3048 save_ga = redir_execute_ga;
3049 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3050 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003051 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003052 if (!echo_output)
3053 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054
3055 if (cmd != NULL)
3056 do_cmdline_cmd(cmd);
3057 else
3058 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003059 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003061 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003062 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063 do_cmdline(NULL, get_list_line, (void *)&item,
3064 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3065 --list->lv_refcount;
3066 }
3067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003068 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003069 if (ga_grow(&redir_execute_ga, 1) == OK)
3070 {
3071 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3072 rettv->vval.v_string = redir_execute_ga.ga_data;
3073 }
3074 else
3075 {
3076 ga_clear(&redir_execute_ga);
3077 rettv->vval.v_string = NULL;
3078 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003079 msg_silent = save_msg_silent;
3080 emsg_silent = save_emsg_silent;
3081 emsg_noredir = save_emsg_noredir;
3082
3083 redir_execute = save_redir_execute;
3084 if (redir_execute)
3085 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003086 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003088 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003089 if (echo_output)
3090 // When not working silently: put it in column zero. A following
3091 // "echon" will overwrite the message, unavoidably.
3092 msg_col = 0;
3093 else
3094 // When working silently: Put it back where it was, since nothing
3095 // should have been written.
3096 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097}
3098
3099/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003100 * "execute()" function
3101 */
3102 static void
3103f_execute(typval_T *argvars, typval_T *rettv)
3104{
3105 execute_common(argvars, rettv, 0);
3106}
3107
3108/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 * "exists()" function
3110 */
3111 static void
3112f_exists(typval_T *argvars, typval_T *rettv)
3113{
3114 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003117 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003120 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 if (mch_getenv(p + 1) != NULL)
3122 n = TRUE;
3123 else
3124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 p = expand_env_save(p);
3127 if (p != NULL && *p != '$')
3128 n = TRUE;
3129 vim_free(p);
3130 }
3131 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003132 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003134 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003136 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003138 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003140 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003142 else if (*p == '?') // internal function only
3143 {
3144 n = has_internal_func_name(p + 1);
3145 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 else if (*p == ':')
3147 {
3148 n = cmd_exists(p + 1);
3149 }
3150 else if (*p == '#')
3151 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152 if (p[1] == '#')
3153 n = autocmd_supported(p + 2);
3154 else
3155 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003157 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003159 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 }
3161
3162 rettv->vval.v_number = n;
3163}
3164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165/*
3166 * "expand()" function
3167 */
3168 static void
3169f_expand(typval_T *argvars, typval_T *rettv)
3170{
3171 char_u *s;
3172 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003173 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003174 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3175 expand_T xpc;
3176 int error = FALSE;
3177 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003178#ifdef BACKSLASH_IN_FILENAME
3179 char_u *p_csl_save = p_csl;
3180
3181 // avoid using 'completeslash' here
3182 p_csl = empty_option;
3183#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003184
3185 rettv->v_type = VAR_STRING;
3186 if (argvars[1].v_type != VAR_UNKNOWN
3187 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003188 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003190 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003192 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 if (*s == '%' || *s == '#' || *s == '<')
3194 {
3195 ++emsg_off;
3196 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3197 --emsg_off;
3198 if (rettv->v_type == VAR_LIST)
3199 {
3200 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3201 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003202 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003203 }
3204 else
3205 rettv->vval.v_string = result;
3206 }
3207 else
3208 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003209 // When the optional second argument is non-zero, don't remove matches
3210 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003211 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003212 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213 options |= WILD_KEEP_ALL;
3214 if (!error)
3215 {
3216 ExpandInit(&xpc);
3217 xpc.xp_context = EXPAND_FILES;
3218 if (p_wic)
3219 options += WILD_ICASE;
3220 if (rettv->v_type == VAR_STRING)
3221 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3222 options, WILD_ALL);
3223 else if (rettv_list_alloc(rettv) != FAIL)
3224 {
3225 int i;
3226
3227 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3228 for (i = 0; i < xpc.xp_numfiles; i++)
3229 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3230 ExpandCleanup(&xpc);
3231 }
3232 }
3233 else
3234 rettv->vval.v_string = NULL;
3235 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003236#ifdef BACKSLASH_IN_FILENAME
3237 p_csl = p_csl_save;
3238#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239}
3240
3241/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003242 * "expandcmd()" function
3243 * Expand all the special characters in a command string.
3244 */
3245 static void
3246f_expandcmd(typval_T *argvars, typval_T *rettv)
3247{
3248 exarg_T eap;
3249 char_u *cmdstr;
3250 char *errormsg = NULL;
3251
3252 rettv->v_type = VAR_STRING;
3253 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3254
3255 memset(&eap, 0, sizeof(eap));
3256 eap.cmd = cmdstr;
3257 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003258 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003259 eap.usefilter = FALSE;
3260 eap.nextcmd = NULL;
3261 eap.cmdidx = CMD_USER;
3262
3263 expand_filename(&eap, &cmdstr, &errormsg);
3264 if (errormsg != NULL && *errormsg != NUL)
3265 emsg(errormsg);
3266
3267 rettv->vval.v_string = cmdstr;
3268}
3269
3270/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 * "feedkeys()" function
3272 */
3273 static void
3274f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3275{
3276 int remap = TRUE;
3277 int insert = FALSE;
3278 char_u *keys, *flags;
3279 char_u nbuf[NUMBUFLEN];
3280 int typed = FALSE;
3281 int execute = FALSE;
3282 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003283 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 char_u *keys_esc;
3285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003286 // This is not allowed in the sandbox. If the commands would still be
3287 // executed in the sandbox it would be OK, but it probably happens later,
3288 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 if (check_secure())
3290 return;
3291
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003292 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293
3294 if (argvars[1].v_type != VAR_UNKNOWN)
3295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 for ( ; *flags != NUL; ++flags)
3298 {
3299 switch (*flags)
3300 {
3301 case 'n': remap = FALSE; break;
3302 case 'm': remap = TRUE; break;
3303 case 't': typed = TRUE; break;
3304 case 'i': insert = TRUE; break;
3305 case 'x': execute = TRUE; break;
3306 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003307 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 }
3309 }
3310 }
3311
3312 if (*keys != NUL || execute)
3313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003314 // Need to escape K_SPECIAL and CSI before putting the string in the
3315 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 keys_esc = vim_strsave_escape_csi(keys);
3317 if (keys_esc != NULL)
3318 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003319 if (lowlevel)
3320 {
3321#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003322 int idx;
3323 int len = (int)STRLEN(keys);
3324
3325 for (idx = 0; idx < len; ++idx)
3326 {
3327 // if a CTRL-C was typed, set got_int, similar to what
3328 // happens in fill_input_buf()
3329 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3330 got_int = TRUE;
3331 add_to_input_buf(keys + idx, 1);
3332 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003333#else
3334 emsg(_("E980: lowlevel input not supported"));
3335#endif
3336 }
3337 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003338 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003339 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003341 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003342#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003343 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003344#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003345 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003346 typebuf_was_filled = TRUE;
3347 }
3348 vim_free(keys_esc);
3349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 if (execute)
3351 {
3352 int save_msg_scroll = msg_scroll;
3353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003354 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 msg_scroll = FALSE;
3356
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003357 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003358 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003359 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003360 ++in_feedkeys;
3361 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003362 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003363 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003364 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003365 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003366 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003367 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 msg_scroll |= save_msg_scroll;
3370 }
3371 }
3372 }
3373}
3374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003375/*
3376 * "fnameescape({string})" function
3377 */
3378 static void
3379f_fnameescape(typval_T *argvars, typval_T *rettv)
3380{
3381 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003382 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003383 rettv->v_type = VAR_STRING;
3384}
3385
3386/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 * "foreground()" function
3388 */
3389 static void
3390f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3391{
3392#ifdef FEAT_GUI
3393 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003394 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003396 return;
3397 }
3398#endif
3399#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401#endif
3402}
3403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003405common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406{
3407 char_u *s;
3408 char_u *name;
3409 int use_string = FALSE;
3410 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003411 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003412 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413
3414 if (argvars[0].v_type == VAR_FUNC)
3415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003416 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 s = argvars[0].vval.v_string;
3418 }
3419 else if (argvars[0].v_type == VAR_PARTIAL
3420 && argvars[0].vval.v_partial != NULL)
3421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003424 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 }
3426 else
3427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003428 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003429 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 use_string = TRUE;
3431 }
3432
Bram Moolenaar843b8842016-08-21 14:36:15 +02003433 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003434 {
3435 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003436 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003437 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3438 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003439 if (*name != NUL)
3440 s = NULL;
3441 }
3442
Bram Moolenaar843b8842016-08-21 14:36:15 +02003443 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3444 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003446 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003447 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003448 ? find_func(trans_name, is_global, NULL) == NULL
3449 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003450 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 else
3452 {
3453 int dict_idx = 0;
3454 int arg_idx = 0;
3455 list_T *list = NULL;
3456
3457 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3458 {
3459 char sid_buf[25];
3460 int off = *s == 's' ? 2 : 5;
3461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 // Expand s: and <SID> into <SNR>nr_, so that the function can
3463 // also be called from another script. Using trans_function_name()
3464 // would also work, but some plugins depend on the name being
3465 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003466 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003467 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 if (name != NULL)
3469 {
3470 STRCPY(name, sid_buf);
3471 STRCAT(name, s + off);
3472 }
3473 }
3474 else
3475 name = vim_strsave(s);
3476
3477 if (argvars[1].v_type != VAR_UNKNOWN)
3478 {
3479 if (argvars[2].v_type != VAR_UNKNOWN)
3480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003481 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 arg_idx = 1;
3483 dict_idx = 2;
3484 }
3485 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003486 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487 dict_idx = 1;
3488 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003489 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 arg_idx = 1;
3491 if (dict_idx > 0)
3492 {
3493 if (argvars[dict_idx].v_type != VAR_DICT)
3494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003495 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003497 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 }
3499 if (argvars[dict_idx].vval.v_dict == NULL)
3500 dict_idx = 0;
3501 }
3502 if (arg_idx > 0)
3503 {
3504 if (argvars[arg_idx].v_type != VAR_LIST)
3505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003506 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003508 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003509 }
3510 list = argvars[arg_idx].vval.v_list;
3511 if (list == NULL || list->lv_len == 0)
3512 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003513 else if (list->lv_len > MAX_FUNC_ARGS)
3514 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003515 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003516 vim_free(name);
3517 goto theend;
3518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 }
3520 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003521 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003523 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003525 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526 if (pt == NULL)
3527 vim_free(name);
3528 else
3529 {
3530 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3531 {
3532 listitem_T *li;
3533 int i = 0;
3534 int arg_len = 0;
3535 int lv_len = 0;
3536
3537 if (arg_pt != NULL)
3538 arg_len = arg_pt->pt_argc;
3539 if (list != NULL)
3540 lv_len = list->lv_len;
3541 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003542 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 if (pt->pt_argv == NULL)
3544 {
3545 vim_free(pt);
3546 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003547 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003549 for (i = 0; i < arg_len; i++)
3550 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3551 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003552 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003553 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003554 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003555 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557 }
3558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003559 // For "function(dict.func, [], dict)" and "func" is a partial
3560 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (dict_idx > 0)
3562 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003563 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3565 ++pt->pt_dict->dv_refcount;
3566 }
3567 else if (arg_pt != NULL)
3568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003569 // If the dict was bound automatically the result is also
3570 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 pt->pt_dict = arg_pt->pt_dict;
3572 pt->pt_auto = arg_pt->pt_auto;
3573 if (pt->pt_dict != NULL)
3574 ++pt->pt_dict->dv_refcount;
3575 }
3576
3577 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003578 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3579 {
3580 pt->pt_func = arg_pt->pt_func;
3581 func_ptr_ref(pt->pt_func);
3582 vim_free(name);
3583 }
3584 else if (is_funcref)
3585 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003586 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003587 func_ptr_ref(pt->pt_func);
3588 vim_free(name);
3589 }
3590 else
3591 {
3592 pt->pt_name = name;
3593 func_ref(name);
3594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 }
3596 rettv->v_type = VAR_PARTIAL;
3597 rettv->vval.v_partial = pt;
3598 }
3599 else
3600 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 rettv->v_type = VAR_FUNC;
3603 rettv->vval.v_string = name;
3604 func_ref(name);
3605 }
3606 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003607theend:
3608 vim_free(trans_name);
3609}
3610
3611/*
3612 * "funcref()" function
3613 */
3614 static void
3615f_funcref(typval_T *argvars, typval_T *rettv)
3616{
3617 common_function(argvars, rettv, TRUE);
3618}
3619
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003620 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003621ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003622{
3623 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3624 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003625 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003626}
3627
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003628/*
3629 * "function()" function
3630 */
3631 static void
3632f_function(typval_T *argvars, typval_T *rettv)
3633{
3634 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635}
3636
3637/*
3638 * "garbagecollect()" function
3639 */
3640 static void
3641f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3642{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003643 // This is postponed until we are back at the toplevel, because we may be
3644 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 want_garbage_collect = TRUE;
3646
Bram Moolenaar2df47312020-09-05 17:30:44 +02003647 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 garbage_collect_at_exit = TRUE;
3649}
3650
3651/*
3652 * "get()" function
3653 */
3654 static void
3655f_get(typval_T *argvars, typval_T *rettv)
3656{
3657 listitem_T *li;
3658 list_T *l;
3659 dictitem_T *di;
3660 dict_T *d;
3661 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003662 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003664 if (argvars[0].v_type == VAR_BLOB)
3665 {
3666 int error = FALSE;
3667 int idx = tv_get_number_chk(&argvars[1], &error);
3668
3669 if (!error)
3670 {
3671 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003672 if (idx < 0)
3673 idx = blob_len(argvars[0].vval.v_blob) + idx;
3674 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3675 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003676 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003677 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003678 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003679 tv = rettv;
3680 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003681 }
3682 }
3683 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684 {
3685 if ((l = argvars[0].vval.v_list) != NULL)
3686 {
3687 int error = FALSE;
3688
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003689 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 if (!error && li != NULL)
3691 tv = &li->li_tv;
3692 }
3693 }
3694 else if (argvars[0].v_type == VAR_DICT)
3695 {
3696 if ((d = argvars[0].vval.v_dict) != NULL)
3697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003698 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 if (di != NULL)
3700 tv = &di->di_tv;
3701 }
3702 }
3703 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3704 {
3705 partial_T *pt;
3706 partial_T fref_pt;
3707
3708 if (argvars[0].v_type == VAR_PARTIAL)
3709 pt = argvars[0].vval.v_partial;
3710 else
3711 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003712 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713 fref_pt.pt_name = argvars[0].vval.v_string;
3714 pt = &fref_pt;
3715 }
3716
3717 if (pt != NULL)
3718 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003719 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003720 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003721
3722 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3723 {
3724 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 n = partial_name(pt);
3726 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 rettv->vval.v_string = NULL;
3728 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 {
3730 rettv->vval.v_string = vim_strsave(n);
3731 if (rettv->v_type == VAR_FUNC)
3732 func_ref(rettv->vval.v_string);
3733 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 }
3735 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003736 {
3737 what_is_dict = TRUE;
3738 if (pt->pt_dict != NULL)
3739 rettv_dict_set(rettv, pt->pt_dict);
3740 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 else if (STRCMP(what, "args") == 0)
3742 {
3743 rettv->v_type = VAR_LIST;
3744 if (rettv_list_alloc(rettv) == OK)
3745 {
3746 int i;
3747
3748 for (i = 0; i < pt->pt_argc; ++i)
3749 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3750 }
3751 }
3752 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003753 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003754
3755 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3756 // third argument
3757 if (!what_is_dict)
3758 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003759 }
3760 }
3761 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003762 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763
3764 if (tv == NULL)
3765 {
3766 if (argvars[2].v_type != VAR_UNKNOWN)
3767 copy_tv(&argvars[2], rettv);
3768 }
3769 else
3770 copy_tv(tv, rettv);
3771}
3772
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003773/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003774 * "getchangelist()" function
3775 */
3776 static void
3777f_getchangelist(typval_T *argvars, typval_T *rettv)
3778{
3779#ifdef FEAT_JUMPLIST
3780 buf_T *buf;
3781 int i;
3782 list_T *l;
3783 dict_T *d;
3784#endif
3785
3786 if (rettv_list_alloc(rettv) != OK)
3787 return;
3788
3789#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003790 if (argvars[0].v_type == VAR_UNKNOWN)
3791 buf = curbuf;
3792 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003793 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003794 if (buf == NULL)
3795 return;
3796
3797 l = list_alloc();
3798 if (l == NULL)
3799 return;
3800
3801 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3802 return;
3803 /*
3804 * The current window change list index tracks only the position in the
3805 * current buffer change list. For other buffers, use the change list
3806 * length as the current index.
3807 */
3808 list_append_number(rettv->vval.v_list,
3809 (varnumber_T)((buf == curwin->w_buffer)
3810 ? curwin->w_changelistidx : buf->b_changelistlen));
3811
3812 for (i = 0; i < buf->b_changelistlen; ++i)
3813 {
3814 if (buf->b_changelist[i].lnum == 0)
3815 continue;
3816 if ((d = dict_alloc()) == NULL)
3817 return;
3818 if (list_append_dict(l, d) == FAIL)
3819 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003820 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3821 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003822 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003823 }
3824#endif
3825}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003827 static void
3828getpos_both(
3829 typval_T *argvars,
3830 typval_T *rettv,
3831 int getcurpos,
3832 int charcol)
3833{
3834 pos_T *fp = NULL;
3835 pos_T pos;
3836 win_T *wp = curwin;
3837 list_T *l;
3838 int fnum = -1;
3839
3840 if (rettv_list_alloc(rettv) == OK)
3841 {
3842 l = rettv->vval.v_list;
3843 if (getcurpos)
3844 {
3845 if (argvars[0].v_type != VAR_UNKNOWN)
3846 {
3847 wp = find_win_by_nr_or_id(&argvars[0]);
3848 if (wp != NULL)
3849 fp = &wp->w_cursor;
3850 }
3851 else
3852 fp = &curwin->w_cursor;
3853 if (fp != NULL && charcol)
3854 {
3855 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003856 pos.col =
3857 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003858 fp = &pos;
3859 }
3860 }
3861 else
3862 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3863 if (fnum != -1)
3864 list_append_number(l, (varnumber_T)fnum);
3865 else
3866 list_append_number(l, (varnumber_T)0);
3867 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3868 : (varnumber_T)0);
3869 list_append_number(l, (fp != NULL)
3870 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3871 : (varnumber_T)0);
3872 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3873 (varnumber_T)0);
3874 if (getcurpos)
3875 {
3876 int save_set_curswant = curwin->w_set_curswant;
3877 colnr_T save_curswant = curwin->w_curswant;
3878 colnr_T save_virtcol = curwin->w_virtcol;
3879
3880 if (wp == curwin)
3881 update_curswant();
3882 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3883 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3884
3885 // Do not change "curswant", as it is unexpected that a get
3886 // function has a side effect.
3887 if (wp == curwin && save_set_curswant)
3888 {
3889 curwin->w_set_curswant = save_set_curswant;
3890 curwin->w_curswant = save_curswant;
3891 curwin->w_virtcol = save_virtcol;
3892 curwin->w_valid &= ~VALID_VIRTCOL;
3893 }
3894 }
3895 }
3896 else
3897 rettv->vval.v_number = FALSE;
3898}
3899
3900/*
3901 * "getcharpos()" function
3902 */
3903 static void
3904f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3905{
3906 getpos_both(argvars, rettv, FALSE, TRUE);
3907}
3908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909/*
3910 * "getcharsearch()" function
3911 */
3912 static void
3913f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3914{
3915 if (rettv_dict_alloc(rettv) != FAIL)
3916 {
3917 dict_T *dict = rettv->vval.v_dict;
3918
Bram Moolenaare0be1672018-07-08 16:50:37 +02003919 dict_add_string(dict, "char", last_csearch());
3920 dict_add_number(dict, "forward", last_csearch_forward());
3921 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 }
3923}
3924
3925/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003926 * "getenv()" function
3927 */
3928 static void
3929f_getenv(typval_T *argvars, typval_T *rettv)
3930{
3931 int mustfree = FALSE;
3932 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3933
3934 if (p == NULL)
3935 {
3936 rettv->v_type = VAR_SPECIAL;
3937 rettv->vval.v_number = VVAL_NULL;
3938 return;
3939 }
3940 if (!mustfree)
3941 p = vim_strsave(p);
3942 rettv->vval.v_string = p;
3943 rettv->v_type = VAR_STRING;
3944}
3945
3946/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 * "getfontname()" function
3948 */
3949 static void
3950f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3951{
3952 rettv->v_type = VAR_STRING;
3953 rettv->vval.v_string = NULL;
3954#ifdef FEAT_GUI
3955 if (gui.in_use)
3956 {
3957 GuiFont font;
3958 char_u *name = NULL;
3959
3960 if (argvars[0].v_type == VAR_UNKNOWN)
3961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003962 // Get the "Normal" font. Either the name saved by
3963 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 font = gui.norm_font;
3965 name = hl_get_font_name();
3966 }
3967 else
3968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003969 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003970 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 return;
3972 font = gui_mch_get_font(name, FALSE);
3973 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 }
3976 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3977 if (argvars[0].v_type != VAR_UNKNOWN)
3978 gui_mch_free_font(font);
3979 }
3980#endif
3981}
3982
3983/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003984 * "getjumplist()" function
3985 */
3986 static void
3987f_getjumplist(typval_T *argvars, typval_T *rettv)
3988{
3989#ifdef FEAT_JUMPLIST
3990 win_T *wp;
3991 int i;
3992 list_T *l;
3993 dict_T *d;
3994#endif
3995
3996 if (rettv_list_alloc(rettv) != OK)
3997 return;
3998
3999#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004000 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004001 if (wp == NULL)
4002 return;
4003
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004004 cleanup_jumplist(wp, TRUE);
4005
Bram Moolenaar4f505882018-02-10 21:06:32 +01004006 l = list_alloc();
4007 if (l == NULL)
4008 return;
4009
4010 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4011 return;
4012 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4013
4014 for (i = 0; i < wp->w_jumplistlen; ++i)
4015 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004016 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4017 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004018 if ((d = dict_alloc()) == NULL)
4019 return;
4020 if (list_append_dict(l, d) == FAIL)
4021 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004022 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4023 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004024 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004025 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004026 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004027 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004028 }
4029#endif
4030}
4031
4032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 * "getpid()" function
4034 */
4035 static void
4036f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4037{
4038 rettv->vval.v_number = mch_get_pid();
4039}
4040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041/*
4042 * "getcurpos()" function
4043 */
4044 static void
4045f_getcurpos(typval_T *argvars, typval_T *rettv)
4046{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004047 getpos_both(argvars, rettv, TRUE, FALSE);
4048}
4049
4050 static void
4051f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4052{
4053 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054}
4055
4056/*
4057 * "getpos(string)" function
4058 */
4059 static void
4060f_getpos(typval_T *argvars, typval_T *rettv)
4061{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004062 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063}
4064
4065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 * "getreg()" function
4067 */
4068 static void
4069f_getreg(typval_T *argvars, typval_T *rettv)
4070{
4071 char_u *strregname;
4072 int regname;
4073 int arg2 = FALSE;
4074 int return_list = FALSE;
4075 int error = FALSE;
4076
4077 if (argvars[0].v_type != VAR_UNKNOWN)
4078 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004079 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004080 if (strregname == NULL)
4081 error = TRUE;
4082 else if (in_vim9script() && STRLEN(strregname) > 1)
4083 {
4084 semsg(_(e_register_name_must_be_one_char_str), strregname);
4085 error = TRUE;
4086 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004087 if (argvars[1].v_type != VAR_UNKNOWN)
4088 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004089 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004091 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004092 }
4093 }
4094 else
4095 strregname = get_vim_var_str(VV_REG);
4096
4097 if (error)
4098 return;
4099
4100 regname = (strregname == NULL ? '"' : *strregname);
4101 if (regname == 0)
4102 regname = '"';
4103
4104 if (return_list)
4105 {
4106 rettv->v_type = VAR_LIST;
4107 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4108 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4109 if (rettv->vval.v_list == NULL)
4110 (void)rettv_list_alloc(rettv);
4111 else
4112 ++rettv->vval.v_list->lv_refcount;
4113 }
4114 else
4115 {
4116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = get_reg_contents(regname,
4118 arg2 ? GREG_EXPR_SRC : 0);
4119 }
4120}
4121
4122/*
4123 * "getregtype()" function
4124 */
4125 static void
4126f_getregtype(typval_T *argvars, typval_T *rettv)
4127{
4128 char_u *strregname;
4129 int regname;
4130 char_u buf[NUMBUFLEN + 2];
4131 long reglen = 0;
4132
4133 if (argvars[0].v_type != VAR_UNKNOWN)
4134 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004135 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004136 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4137 {
4138 semsg(_(e_register_name_must_be_one_char_str), strregname);
4139 strregname = NULL;
4140 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004142 {
4143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = NULL;
4145 return;
4146 }
4147 }
4148 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004149 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 strregname = get_vim_var_str(VV_REG);
4151
4152 regname = (strregname == NULL ? '"' : *strregname);
4153 if (regname == 0)
4154 regname = '"';
4155
4156 buf[0] = NUL;
4157 buf[1] = NUL;
4158 switch (get_reg_type(regname, &reglen))
4159 {
4160 case MLINE: buf[0] = 'V'; break;
4161 case MCHAR: buf[0] = 'v'; break;
4162 case MBLOCK:
4163 buf[0] = Ctrl_V;
4164 sprintf((char *)buf + 1, "%ld", reglen + 1);
4165 break;
4166 }
4167 rettv->v_type = VAR_STRING;
4168 rettv->vval.v_string = vim_strsave(buf);
4169}
4170
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004171/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004172 * "gettagstack()" function
4173 */
4174 static void
4175f_gettagstack(typval_T *argvars, typval_T *rettv)
4176{
4177 win_T *wp = curwin; // default is current window
4178
4179 if (rettv_dict_alloc(rettv) != OK)
4180 return;
4181
4182 if (argvars[0].v_type != VAR_UNKNOWN)
4183 {
4184 wp = find_win_by_nr_or_id(&argvars[0]);
4185 if (wp == NULL)
4186 return;
4187 }
4188
4189 get_tagstack(wp, rettv->vval.v_dict);
4190}
4191
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004192/*
4193 * "gettext()" function
4194 */
4195 static void
4196f_gettext(typval_T *argvars, typval_T *rettv)
4197{
4198 if (argvars[0].v_type != VAR_STRING
4199 || argvars[0].vval.v_string == NULL
4200 || *argvars[0].vval.v_string == NUL)
4201 {
4202 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4203 }
4204 else
4205 {
4206 rettv->v_type = VAR_STRING;
4207 rettv->vval.v_string = vim_strsave(
4208 (char_u *)_(argvars[0].vval.v_string));
4209 }
4210}
4211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004212// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213#include "version.h"
4214
4215/*
4216 * "has()" function
4217 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004218 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004219f_has(typval_T *argvars, typval_T *rettv)
4220{
4221 int i;
4222 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004223 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004224 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004225 typedef struct {
4226 char *name;
4227 short present;
4228 } has_item_T;
4229 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004231 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004232#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004233 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004234#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004235 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004236#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004237 },
4238 {"arp",
4239#if defined(AMIGA) && defined(FEAT_ARP)
4240 1
4241#else
4242 0
4243#endif
4244 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004245 {"haiku",
4246#ifdef __HAIKU__
4247 1
4248#else
4249 0
4250#endif
4251 },
4252 {"bsd",
4253#if defined(BSD) && !defined(MACOS_X)
4254 1
4255#else
4256 0
4257#endif
4258 },
4259 {"hpux",
4260#ifdef hpux
4261 1
4262#else
4263 0
4264#endif
4265 },
4266 {"linux",
4267#ifdef __linux__
4268 1
4269#else
4270 0
4271#endif
4272 },
4273 {"mac", // Mac OS X (and, once, Mac OS Classic)
4274#ifdef MACOS_X
4275 1
4276#else
4277 0
4278#endif
4279 },
4280 {"osx", // Mac OS X
4281#ifdef MACOS_X
4282 1
4283#else
4284 0
4285#endif
4286 },
4287 {"macunix", // Mac OS X, with the darwin feature
4288#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4289 1
4290#else
4291 0
4292#endif
4293 },
4294 {"osxdarwin", // synonym for macunix
4295#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4296 1
4297#else
4298 0
4299#endif
4300 },
4301 {"qnx",
4302#ifdef __QNX__
4303 1
4304#else
4305 0
4306#endif
4307 },
4308 {"sun",
4309#ifdef SUN_SYSTEM
4310 1
4311#else
4312 0
4313#endif
4314 },
4315 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004316#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004317 1
4318#else
4319 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004321 },
4322 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004324 1
4325#else
4326 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004328 },
4329 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004330#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004331 1
4332#else
4333 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004335 },
4336 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004337#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004338 1
4339#else
4340 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004342 },
4343 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004344#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004345 1
4346#else
4347 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004349 },
4350 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004351#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004352 1
4353#else
4354 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004355#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004356 },
4357 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004359 1
4360#else
4361 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004363 },
4364 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004366 1
4367#else
4368 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004369#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004370 },
4371 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004372#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004373 1
4374#else
4375 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004376#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004377 },
4378 {"autocmd", 1},
4379 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004380#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004381 1
4382#else
4383 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004384#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004385 },
4386 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004387#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004388 1
4389#else
4390 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004391#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004392 },
4393 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004394#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004395 1
4396#else
4397 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004399 },
4400 {"balloon_multiline",
4401#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4402 // MS-Windows requires runtime check, see below
4403 1
4404#else
4405 0
4406#endif
4407 },
4408 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004409#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004410 1
4411#else
4412 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004413#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004414 },
4415 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004417 1
4418#else
4419 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004421 },
4422 {"all_builtin_terms",
4423#if defined(ALL_BUILTIN_TCAPS)
4424 1
4425#else
4426 0
4427#endif
4428 },
4429 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004430#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004431 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004433 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004435 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004437 },
4438 {"byte_offset",
4439#ifdef FEAT_BYTEOFF
4440 1
4441#else
4442 0
4443#endif
4444 },
4445 {"channel",
4446#ifdef FEAT_JOB_CHANNEL
4447 1
4448#else
4449 0
4450#endif
4451 },
4452 {"cindent",
4453#ifdef FEAT_CINDENT
4454 1
4455#else
4456 0
4457#endif
4458 },
4459 {"clientserver",
4460#ifdef FEAT_CLIENTSERVER
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"clipboard",
4467#ifdef FEAT_CLIPBOARD
4468 1
4469#else
4470 0
4471#endif
4472 },
4473 {"cmdline_compl", 1},
4474 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004475 {"cmdwin",
4476#ifdef FEAT_CMDWIN
4477 1
4478#else
4479 0
4480#endif
4481 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004482 {"comments", 1},
4483 {"conceal",
4484#ifdef FEAT_CONCEAL
4485 1
4486#else
4487 0
4488#endif
4489 },
4490 {"cryptv",
4491#ifdef FEAT_CRYPT
4492 1
4493#else
4494 0
4495#endif
4496 },
4497 {"crypt-blowfish",
4498#ifdef FEAT_CRYPT
4499 1
4500#else
4501 0
4502#endif
4503 },
4504 {"crypt-blowfish2",
4505#ifdef FEAT_CRYPT
4506 1
4507#else
4508 0
4509#endif
4510 },
4511 {"cscope",
4512#ifdef FEAT_CSCOPE
4513 1
4514#else
4515 0
4516#endif
4517 },
4518 {"cursorbind", 1},
4519 {"cursorshape",
4520#ifdef CURSOR_SHAPE
4521 1
4522#else
4523 0
4524#endif
4525 },
4526 {"debug",
4527#ifdef DEBUG
4528 1
4529#else
4530 0
4531#endif
4532 },
4533 {"dialog_con",
4534#ifdef FEAT_CON_DIALOG
4535 1
4536#else
4537 0
4538#endif
4539 },
4540 {"dialog_gui",
4541#ifdef FEAT_GUI_DIALOG
4542 1
4543#else
4544 0
4545#endif
4546 },
4547 {"diff",
4548#ifdef FEAT_DIFF
4549 1
4550#else
4551 0
4552#endif
4553 },
4554 {"digraphs",
4555#ifdef FEAT_DIGRAPHS
4556 1
4557#else
4558 0
4559#endif
4560 },
4561 {"directx",
4562#ifdef FEAT_DIRECTX
4563 1
4564#else
4565 0
4566#endif
4567 },
4568 {"dnd",
4569#ifdef FEAT_DND
4570 1
4571#else
4572 0
4573#endif
4574 },
4575 {"emacs_tags",
4576#ifdef FEAT_EMACS_TAGS
4577 1
4578#else
4579 0
4580#endif
4581 },
4582 {"eval", 1}, // always present, of course!
4583 {"ex_extra", 1}, // graduated feature
4584 {"extra_search",
4585#ifdef FEAT_SEARCH_EXTRA
4586 1
4587#else
4588 0
4589#endif
4590 },
4591 {"file_in_path",
4592#ifdef FEAT_SEARCHPATH
4593 1
4594#else
4595 0
4596#endif
4597 },
4598 {"filterpipe",
4599#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4600 1
4601#else
4602 0
4603#endif
4604 },
4605 {"find_in_path",
4606#ifdef FEAT_FIND_ID
4607 1
4608#else
4609 0
4610#endif
4611 },
4612 {"float",
4613#ifdef FEAT_FLOAT
4614 1
4615#else
4616 0
4617#endif
4618 },
4619 {"folding",
4620#ifdef FEAT_FOLDING
4621 1
4622#else
4623 0
4624#endif
4625 },
4626 {"footer",
4627#ifdef FEAT_FOOTER
4628 1
4629#else
4630 0
4631#endif
4632 },
4633 {"fork",
4634#if !defined(USE_SYSTEM) && defined(UNIX)
4635 1
4636#else
4637 0
4638#endif
4639 },
4640 {"gettext",
4641#ifdef FEAT_GETTEXT
4642 1
4643#else
4644 0
4645#endif
4646 },
4647 {"gui",
4648#ifdef FEAT_GUI
4649 1
4650#else
4651 0
4652#endif
4653 },
4654 {"gui_neXtaw",
4655#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4656 1
4657#else
4658 0
4659#endif
4660 },
4661 {"gui_athena",
4662#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4663 1
4664#else
4665 0
4666#endif
4667 },
4668 {"gui_gtk",
4669#ifdef FEAT_GUI_GTK
4670 1
4671#else
4672 0
4673#endif
4674 },
4675 {"gui_gtk2",
4676#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4677 1
4678#else
4679 0
4680#endif
4681 },
4682 {"gui_gtk3",
4683#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4684 1
4685#else
4686 0
4687#endif
4688 },
4689 {"gui_gnome",
4690#ifdef FEAT_GUI_GNOME
4691 1
4692#else
4693 0
4694#endif
4695 },
4696 {"gui_haiku",
4697#ifdef FEAT_GUI_HAIKU
4698 1
4699#else
4700 0
4701#endif
4702 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004703 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004704 {"gui_motif",
4705#ifdef FEAT_GUI_MOTIF
4706 1
4707#else
4708 0
4709#endif
4710 },
4711 {"gui_photon",
4712#ifdef FEAT_GUI_PHOTON
4713 1
4714#else
4715 0
4716#endif
4717 },
4718 {"gui_win32",
4719#ifdef FEAT_GUI_MSWIN
4720 1
4721#else
4722 0
4723#endif
4724 },
4725 {"iconv",
4726#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4727 1
4728#else
4729 0
4730#endif
4731 },
4732 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004733 {"ipv6",
4734#ifdef FEAT_IPV6
4735 1
4736#else
4737 0
4738#endif
4739 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004740 {"job",
4741#ifdef FEAT_JOB_CHANNEL
4742 1
4743#else
4744 0
4745#endif
4746 },
4747 {"jumplist",
4748#ifdef FEAT_JUMPLIST
4749 1
4750#else
4751 0
4752#endif
4753 },
4754 {"keymap",
4755#ifdef FEAT_KEYMAP
4756 1
4757#else
4758 0
4759#endif
4760 },
4761 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4762 {"langmap",
4763#ifdef FEAT_LANGMAP
4764 1
4765#else
4766 0
4767#endif
4768 },
4769 {"libcall",
4770#ifdef FEAT_LIBCALL
4771 1
4772#else
4773 0
4774#endif
4775 },
4776 {"linebreak",
4777#ifdef FEAT_LINEBREAK
4778 1
4779#else
4780 0
4781#endif
4782 },
4783 {"lispindent",
4784#ifdef FEAT_LISP
4785 1
4786#else
4787 0
4788#endif
4789 },
4790 {"listcmds", 1},
4791 {"localmap", 1},
4792 {"lua",
4793#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4794 1
4795#else
4796 0
4797#endif
4798 },
4799 {"menu",
4800#ifdef FEAT_MENU
4801 1
4802#else
4803 0
4804#endif
4805 },
4806 {"mksession",
4807#ifdef FEAT_SESSION
4808 1
4809#else
4810 0
4811#endif
4812 },
4813 {"modify_fname", 1},
4814 {"mouse", 1},
4815 {"mouseshape",
4816#ifdef FEAT_MOUSESHAPE
4817 1
4818#else
4819 0
4820#endif
4821 },
4822 {"mouse_dec",
4823#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4824 1
4825#else
4826 0
4827#endif
4828 },
4829 {"mouse_gpm",
4830#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4831 1
4832#else
4833 0
4834#endif
4835 },
4836 {"mouse_jsbterm",
4837#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4838 1
4839#else
4840 0
4841#endif
4842 },
4843 {"mouse_netterm",
4844#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4845 1
4846#else
4847 0
4848#endif
4849 },
4850 {"mouse_pterm",
4851#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4852 1
4853#else
4854 0
4855#endif
4856 },
4857 {"mouse_sgr",
4858#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4859 1
4860#else
4861 0
4862#endif
4863 },
4864 {"mouse_sysmouse",
4865#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4866 1
4867#else
4868 0
4869#endif
4870 },
4871 {"mouse_urxvt",
4872#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4873 1
4874#else
4875 0
4876#endif
4877 },
4878 {"mouse_xterm",
4879#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4880 1
4881#else
4882 0
4883#endif
4884 },
4885 {"multi_byte", 1},
4886 {"multi_byte_ime",
4887#ifdef FEAT_MBYTE_IME
4888 1
4889#else
4890 0
4891#endif
4892 },
4893 {"multi_lang",
4894#ifdef FEAT_MULTI_LANG
4895 1
4896#else
4897 0
4898#endif
4899 },
4900 {"mzscheme",
4901#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4902 1
4903#else
4904 0
4905#endif
4906 },
4907 {"num64", 1},
4908 {"ole",
4909#ifdef FEAT_OLE
4910 1
4911#else
4912 0
4913#endif
4914 },
4915 {"packages",
4916#ifdef FEAT_EVAL
4917 1
4918#else
4919 0
4920#endif
4921 },
4922 {"path_extra",
4923#ifdef FEAT_PATH_EXTRA
4924 1
4925#else
4926 0
4927#endif
4928 },
4929 {"perl",
4930#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4931 1
4932#else
4933 0
4934#endif
4935 },
4936 {"persistent_undo",
4937#ifdef FEAT_PERSISTENT_UNDO
4938 1
4939#else
4940 0
4941#endif
4942 },
4943 {"python_compiled",
4944#if defined(FEAT_PYTHON)
4945 1
4946#else
4947 0
4948#endif
4949 },
4950 {"python_dynamic",
4951#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4952 1
4953#else
4954 0
4955#endif
4956 },
4957 {"python",
4958#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4959 1
4960#else
4961 0
4962#endif
4963 },
4964 {"pythonx",
4965#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4966 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4967 1
4968#else
4969 0
4970#endif
4971 },
4972 {"python3_compiled",
4973#if defined(FEAT_PYTHON3)
4974 1
4975#else
4976 0
4977#endif
4978 },
4979 {"python3_dynamic",
4980#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4981 1
4982#else
4983 0
4984#endif
4985 },
4986 {"python3",
4987#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4988 1
4989#else
4990 0
4991#endif
4992 },
4993 {"popupwin",
4994#ifdef FEAT_PROP_POPUP
4995 1
4996#else
4997 0
4998#endif
4999 },
5000 {"postscript",
5001#ifdef FEAT_POSTSCRIPT
5002 1
5003#else
5004 0
5005#endif
5006 },
5007 {"printer",
5008#ifdef FEAT_PRINTER
5009 1
5010#else
5011 0
5012#endif
5013 },
5014 {"profile",
5015#ifdef FEAT_PROFILE
5016 1
5017#else
5018 0
5019#endif
5020 },
5021 {"reltime",
5022#ifdef FEAT_RELTIME
5023 1
5024#else
5025 0
5026#endif
5027 },
5028 {"quickfix",
5029#ifdef FEAT_QUICKFIX
5030 1
5031#else
5032 0
5033#endif
5034 },
5035 {"rightleft",
5036#ifdef FEAT_RIGHTLEFT
5037 1
5038#else
5039 0
5040#endif
5041 },
5042 {"ruby",
5043#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5044 1
5045#else
5046 0
5047#endif
5048 },
5049 {"scrollbind", 1},
5050 {"showcmd",
5051#ifdef FEAT_CMDL_INFO
5052 1
5053#else
5054 0
5055#endif
5056 },
5057 {"cmdline_info",
5058#ifdef FEAT_CMDL_INFO
5059 1
5060#else
5061 0
5062#endif
5063 },
5064 {"signs",
5065#ifdef FEAT_SIGNS
5066 1
5067#else
5068 0
5069#endif
5070 },
5071 {"smartindent",
5072#ifdef FEAT_SMARTINDENT
5073 1
5074#else
5075 0
5076#endif
5077 },
5078 {"startuptime",
5079#ifdef STARTUPTIME
5080 1
5081#else
5082 0
5083#endif
5084 },
5085 {"statusline",
5086#ifdef FEAT_STL_OPT
5087 1
5088#else
5089 0
5090#endif
5091 },
5092 {"netbeans_intg",
5093#ifdef FEAT_NETBEANS_INTG
5094 1
5095#else
5096 0
5097#endif
5098 },
Christian Brabandtf573c6e2021-06-20 14:02:16 +02005099 {"sodium",
5100#ifdef FEAT_SODIUM
5101 1
5102#else
5103 0
5104#endif
5105 },
Bram Moolenaar79296512020-03-22 16:17:14 +01005106 {"sound",
5107#ifdef FEAT_SOUND
5108 1
5109#else
5110 0
5111#endif
5112 },
5113 {"spell",
5114#ifdef FEAT_SPELL
5115 1
5116#else
5117 0
5118#endif
5119 },
5120 {"syntax",
5121#ifdef FEAT_SYN_HL
5122 1
5123#else
5124 0
5125#endif
5126 },
5127 {"system",
5128#if defined(USE_SYSTEM) || !defined(UNIX)
5129 1
5130#else
5131 0
5132#endif
5133 },
5134 {"tag_binary",
5135#ifdef FEAT_TAG_BINS
5136 1
5137#else
5138 0
5139#endif
5140 },
5141 {"tcl",
5142#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5143 1
5144#else
5145 0
5146#endif
5147 },
5148 {"termguicolors",
5149#ifdef FEAT_TERMGUICOLORS
5150 1
5151#else
5152 0
5153#endif
5154 },
5155 {"terminal",
5156#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5157 1
5158#else
5159 0
5160#endif
5161 },
5162 {"terminfo",
5163#ifdef TERMINFO
5164 1
5165#else
5166 0
5167#endif
5168 },
5169 {"termresponse",
5170#ifdef FEAT_TERMRESPONSE
5171 1
5172#else
5173 0
5174#endif
5175 },
5176 {"textobjects",
5177#ifdef FEAT_TEXTOBJ
5178 1
5179#else
5180 0
5181#endif
5182 },
5183 {"textprop",
5184#ifdef FEAT_PROP_POPUP
5185 1
5186#else
5187 0
5188#endif
5189 },
5190 {"tgetent",
5191#ifdef HAVE_TGETENT
5192 1
5193#else
5194 0
5195#endif
5196 },
5197 {"timers",
5198#ifdef FEAT_TIMERS
5199 1
5200#else
5201 0
5202#endif
5203 },
5204 {"title",
5205#ifdef FEAT_TITLE
5206 1
5207#else
5208 0
5209#endif
5210 },
5211 {"toolbar",
5212#ifdef FEAT_TOOLBAR
5213 1
5214#else
5215 0
5216#endif
5217 },
5218 {"unnamedplus",
5219#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5220 1
5221#else
5222 0
5223#endif
5224 },
5225 {"user-commands", 1}, // was accidentally included in 5.4
5226 {"user_commands", 1},
5227 {"vartabs",
5228#ifdef FEAT_VARTABS
5229 1
5230#else
5231 0
5232#endif
5233 },
5234 {"vertsplit", 1},
5235 {"viminfo",
5236#ifdef FEAT_VIMINFO
5237 1
5238#else
5239 0
5240#endif
5241 },
5242 {"vimscript-1", 1},
5243 {"vimscript-2", 1},
5244 {"vimscript-3", 1},
5245 {"vimscript-4", 1},
5246 {"virtualedit", 1},
5247 {"visual", 1},
5248 {"visualextra", 1},
5249 {"vreplace", 1},
5250 {"vtp",
5251#ifdef FEAT_VTP
5252 1
5253#else
5254 0
5255#endif
5256 },
5257 {"wildignore",
5258#ifdef FEAT_WILDIGN
5259 1
5260#else
5261 0
5262#endif
5263 },
5264 {"wildmenu",
5265#ifdef FEAT_WILDMENU
5266 1
5267#else
5268 0
5269#endif
5270 },
5271 {"windows", 1},
5272 {"winaltkeys",
5273#ifdef FEAT_WAK
5274 1
5275#else
5276 0
5277#endif
5278 },
5279 {"writebackup",
5280#ifdef FEAT_WRITEBACKUP
5281 1
5282#else
5283 0
5284#endif
5285 },
5286 {"xim",
5287#ifdef FEAT_XIM
5288 1
5289#else
5290 0
5291#endif
5292 },
5293 {"xfontset",
5294#ifdef FEAT_XFONTSET
5295 1
5296#else
5297 0
5298#endif
5299 },
5300 {"xpm",
5301#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5302 1
5303#else
5304 0
5305#endif
5306 },
5307 {"xpm_w32", // for backward compatibility
5308#ifdef FEAT_XPM_W32
5309 1
5310#else
5311 0
5312#endif
5313 },
5314 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005315#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005316 1
5317#else
5318 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005320 },
5321 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005323 1
5324#else
5325 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005327 },
5328 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005330 1
5331#else
5332 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005334 },
5335 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005337 1
5338#else
5339 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005340#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005341 },
5342 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005344 1
5345#else
5346 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005348 },
5349 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350 };
5351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005352 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005353 for (i = 0; has_list[i].name != NULL; ++i)
5354 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005355 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005356 x = TRUE;
5357 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 break;
5359 }
5360
Bram Moolenaar79296512020-03-22 16:17:14 +01005361 // features also in has_list[] but sometimes enabled at runtime
5362 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005364 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005365 {
5366 // intentionally empty
5367 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005368#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 else if (STRICMP(name, "balloon_multiline") == 0)
5370 n = multiline_balloon_available();
5371#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005372#ifdef VIMDLL
5373 else if (STRICMP(name, "filterpipe") == 0)
5374 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375#endif
5376#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5377 else if (STRICMP(name, "iconv") == 0)
5378 n = iconv_enabled(FALSE);
5379#endif
5380#ifdef DYNAMIC_LUA
5381 else if (STRICMP(name, "lua") == 0)
5382 n = lua_enabled(FALSE);
5383#endif
5384#ifdef DYNAMIC_MZSCHEME
5385 else if (STRICMP(name, "mzscheme") == 0)
5386 n = mzscheme_enabled(FALSE);
5387#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005388#ifdef DYNAMIC_PERL
5389 else if (STRICMP(name, "perl") == 0)
5390 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005391#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005392#ifdef DYNAMIC_PYTHON
5393 else if (STRICMP(name, "python") == 0)
5394 n = python_enabled(FALSE);
5395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396#ifdef DYNAMIC_PYTHON3
5397 else if (STRICMP(name, "python3") == 0)
5398 n = python3_enabled(FALSE);
5399#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005400#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5401 else if (STRICMP(name, "pythonx") == 0)
5402 {
5403# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5404 if (p_pyx == 0)
5405 n = python3_enabled(FALSE) || python_enabled(FALSE);
5406 else if (p_pyx == 3)
5407 n = python3_enabled(FALSE);
5408 else if (p_pyx == 2)
5409 n = python_enabled(FALSE);
5410# elif defined(DYNAMIC_PYTHON)
5411 n = python_enabled(FALSE);
5412# elif defined(DYNAMIC_PYTHON3)
5413 n = python3_enabled(FALSE);
5414# endif
5415 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005416#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005417#ifdef DYNAMIC_RUBY
5418 else if (STRICMP(name, "ruby") == 0)
5419 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005421#ifdef DYNAMIC_TCL
5422 else if (STRICMP(name, "tcl") == 0)
5423 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005424#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005425#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005426 else if (STRICMP(name, "terminal") == 0)
5427 n = terminal_enabled();
5428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005429 }
5430
Bram Moolenaar79296512020-03-22 16:17:14 +01005431 // features not in has_list[]
5432 if (x == FALSE)
5433 {
5434 if (STRNICMP(name, "patch", 5) == 0)
5435 {
5436 x = TRUE;
5437 if (name[5] == '-'
5438 && STRLEN(name) >= 11
5439 && vim_isdigit(name[6])
5440 && vim_isdigit(name[8])
5441 && vim_isdigit(name[10]))
5442 {
5443 int major = atoi((char *)name + 6);
5444 int minor = atoi((char *)name + 8);
5445
5446 // Expect "patch-9.9.01234".
5447 n = (major < VIM_VERSION_MAJOR
5448 || (major == VIM_VERSION_MAJOR
5449 && (minor < VIM_VERSION_MINOR
5450 || (minor == VIM_VERSION_MINOR
5451 && has_patch(atoi((char *)name + 10))))));
5452 }
5453 else
5454 n = has_patch(atoi((char *)name + 5));
5455 }
5456 else if (STRICMP(name, "vim_starting") == 0)
5457 {
5458 x = TRUE;
5459 n = (starting != 0);
5460 }
5461 else if (STRICMP(name, "ttyin") == 0)
5462 {
5463 x = TRUE;
5464 n = mch_input_isatty();
5465 }
5466 else if (STRICMP(name, "ttyout") == 0)
5467 {
5468 x = TRUE;
5469 n = stdout_isatty;
5470 }
5471 else if (STRICMP(name, "multi_byte_encoding") == 0)
5472 {
5473 x = TRUE;
5474 n = has_mbyte;
5475 }
5476 else if (STRICMP(name, "gui_running") == 0)
5477 {
5478 x = TRUE;
5479#ifdef FEAT_GUI
5480 n = (gui.in_use || gui.starting);
5481#endif
5482 }
5483 else if (STRICMP(name, "browse") == 0)
5484 {
5485 x = TRUE;
5486#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5487 n = gui.in_use; // gui_mch_browse() works when GUI is running
5488#endif
5489 }
5490 else if (STRICMP(name, "syntax_items") == 0)
5491 {
5492 x = TRUE;
5493#ifdef FEAT_SYN_HL
5494 n = syntax_present(curwin);
5495#endif
5496 }
5497 else if (STRICMP(name, "vcon") == 0)
5498 {
5499 x = TRUE;
5500#ifdef FEAT_VTP
5501 n = is_term_win32() && has_vtp_working();
5502#endif
5503 }
5504 else if (STRICMP(name, "netbeans_enabled") == 0)
5505 {
5506 x = TRUE;
5507#ifdef FEAT_NETBEANS_INTG
5508 n = netbeans_active();
5509#endif
5510 }
5511 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5512 {
5513 x = TRUE;
5514#ifdef FEAT_MOUSE_GPM
5515 n = gpm_enabled();
5516#endif
5517 }
5518 else if (STRICMP(name, "conpty") == 0)
5519 {
5520 x = TRUE;
5521#if defined(FEAT_TERMINAL) && defined(MSWIN)
5522 n = use_conpty();
5523#endif
5524 }
5525 else if (STRICMP(name, "clipboard_working") == 0)
5526 {
5527 x = TRUE;
5528#ifdef FEAT_CLIPBOARD
5529 n = clip_star.available;
5530#endif
5531 }
5532 }
5533
Bram Moolenaar04637e22020-09-05 18:45:29 +02005534 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005535 // return whether feature could ever be enabled
5536 rettv->vval.v_number = x;
5537 else
5538 // return whether feature is enabled
5539 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540}
5541
5542/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005543 * Return TRUE if "feature" can change later.
5544 * Also when checking for the feature has side effects, such as loading a DLL.
5545 */
5546 int
5547dynamic_feature(char_u *feature)
5548{
5549 return (feature == NULL
5550#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5551 || STRICMP(feature, "balloon_multiline") == 0
5552#endif
5553#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5554 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5555#endif
5556#ifdef VIMDLL
5557 || STRICMP(feature, "filterpipe") == 0
5558#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005559#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005560 // this can only change on Unix where the ":gui" command could be
5561 // used.
5562 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5563#endif
5564#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5565 || STRICMP(feature, "iconv") == 0
5566#endif
5567#ifdef DYNAMIC_LUA
5568 || STRICMP(feature, "lua") == 0
5569#endif
5570#ifdef FEAT_MOUSE_GPM
5571 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5572#endif
5573#ifdef DYNAMIC_MZSCHEME
5574 || STRICMP(feature, "mzscheme") == 0
5575#endif
5576#ifdef FEAT_NETBEANS_INTG
5577 || STRICMP(feature, "netbeans_enabled") == 0
5578#endif
5579#ifdef DYNAMIC_PERL
5580 || STRICMP(feature, "perl") == 0
5581#endif
5582#ifdef DYNAMIC_PYTHON
5583 || STRICMP(feature, "python") == 0
5584#endif
5585#ifdef DYNAMIC_PYTHON3
5586 || STRICMP(feature, "python3") == 0
5587#endif
5588#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5589 || STRICMP(feature, "pythonx") == 0
5590#endif
5591#ifdef DYNAMIC_RUBY
5592 || STRICMP(feature, "ruby") == 0
5593#endif
5594#ifdef FEAT_SYN_HL
5595 || STRICMP(feature, "syntax_items") == 0
5596#endif
5597#ifdef DYNAMIC_TCL
5598 || STRICMP(feature, "tcl") == 0
5599#endif
5600 // once "starting" is zero it will stay that way
5601 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5602 || STRICMP(feature, "multi_byte_encoding") == 0
5603#if defined(FEAT_TERMINAL) && defined(MSWIN)
5604 || STRICMP(feature, "conpty") == 0
5605#endif
5606 );
5607}
5608
5609/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 * "haslocaldir()" function
5611 */
5612 static void
5613f_haslocaldir(typval_T *argvars, typval_T *rettv)
5614{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005615 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 win_T *wp = NULL;
5617
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005618 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5619
5620 // Check for window-local and tab-local directories
5621 if (wp != NULL && wp->w_localdir != NULL)
5622 rettv->vval.v_number = 1;
5623 else if (tp != NULL && tp->tp_localdir != NULL)
5624 rettv->vval.v_number = 2;
5625 else
5626 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627}
5628
5629/*
5630 * "hasmapto()" function
5631 */
5632 static void
5633f_hasmapto(typval_T *argvars, typval_T *rettv)
5634{
5635 char_u *name;
5636 char_u *mode;
5637 char_u buf[NUMBUFLEN];
5638 int abbr = FALSE;
5639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005640 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641 if (argvars[1].v_type == VAR_UNKNOWN)
5642 mode = (char_u *)"nvo";
5643 else
5644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005645 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005647 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648 }
5649
5650 if (map_to_exists(name, mode, abbr))
5651 rettv->vval.v_number = TRUE;
5652 else
5653 rettv->vval.v_number = FALSE;
5654}
5655
5656/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 * "highlightID(name)" function
5658 */
5659 static void
5660f_hlID(typval_T *argvars, typval_T *rettv)
5661{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005662 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005663}
5664
5665/*
5666 * "highlight_exists()" function
5667 */
5668 static void
5669f_hlexists(typval_T *argvars, typval_T *rettv)
5670{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005671 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672}
5673
5674/*
5675 * "hostname()" function
5676 */
5677 static void
5678f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5679{
5680 char_u hostname[256];
5681
5682 mch_get_host_name(hostname, 256);
5683 rettv->v_type = VAR_STRING;
5684 rettv->vval.v_string = vim_strsave(hostname);
5685}
5686
5687/*
5688 * iconv() function
5689 */
5690 static void
5691f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5692{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 char_u buf1[NUMBUFLEN];
5694 char_u buf2[NUMBUFLEN];
5695 char_u *from, *to, *str;
5696 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005697
5698 rettv->v_type = VAR_STRING;
5699 rettv->vval.v_string = NULL;
5700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005701 str = tv_get_string(&argvars[0]);
5702 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5703 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704 vimconv.vc_type = CONV_NONE;
5705 convert_setup(&vimconv, from, to);
5706
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005707 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005708 if (vimconv.vc_type == CONV_NONE)
5709 rettv->vval.v_string = vim_strsave(str);
5710 else
5711 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5712
5713 convert_setup(&vimconv, NULL, NULL);
5714 vim_free(from);
5715 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716}
5717
5718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719 * "index()" function
5720 */
5721 static void
5722f_index(typval_T *argvars, typval_T *rettv)
5723{
5724 list_T *l;
5725 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005726 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005727 long idx = 0;
5728 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005729 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005730
5731 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005732 if (argvars[0].v_type == VAR_BLOB)
5733 {
5734 typval_T tv;
5735 int start = 0;
5736
5737 if (argvars[2].v_type != VAR_UNKNOWN)
5738 {
5739 start = tv_get_number_chk(&argvars[2], &error);
5740 if (error)
5741 return;
5742 }
5743 b = argvars[0].vval.v_blob;
5744 if (b == NULL)
5745 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005746 if (start < 0)
5747 {
5748 start = blob_len(b) + start;
5749 if (start < 0)
5750 start = 0;
5751 }
5752
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005753 for (idx = start; idx < blob_len(b); ++idx)
5754 {
5755 tv.v_type = VAR_NUMBER;
5756 tv.vval.v_number = blob_get(b, idx);
5757 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5758 {
5759 rettv->vval.v_number = idx;
5760 return;
5761 }
5762 }
5763 return;
5764 }
5765 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005767 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005768 return;
5769 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 l = argvars[0].vval.v_list;
5772 if (l != NULL)
5773 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005774 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 item = l->lv_first;
5776 if (argvars[2].v_type != VAR_UNKNOWN)
5777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005778 // Start at specified item. Use the cached index that list_find()
5779 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005780 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005781 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005783 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 if (error)
5785 item = NULL;
5786 }
5787
5788 for ( ; item != NULL; item = item->li_next, ++idx)
5789 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5790 {
5791 rettv->vval.v_number = idx;
5792 break;
5793 }
5794 }
5795}
5796
5797static int inputsecret_flag = 0;
5798
5799/*
5800 * "input()" function
5801 * Also handles inputsecret() when inputsecret is set.
5802 */
5803 static void
5804f_input(typval_T *argvars, typval_T *rettv)
5805{
5806 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5807}
5808
5809/*
5810 * "inputdialog()" function
5811 */
5812 static void
5813f_inputdialog(typval_T *argvars, typval_T *rettv)
5814{
5815#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005816 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5818 {
5819 char_u *message;
5820 char_u buf[NUMBUFLEN];
5821 char_u *defstr = (char_u *)"";
5822
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005823 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005825 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005826 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5827 else
5828 IObuff[0] = NUL;
5829 if (message != NULL && defstr != NULL
5830 && do_dialog(VIM_QUESTION, NULL, message,
5831 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5832 rettv->vval.v_string = vim_strsave(IObuff);
5833 else
5834 {
5835 if (message != NULL && defstr != NULL
5836 && argvars[1].v_type != VAR_UNKNOWN
5837 && argvars[2].v_type != VAR_UNKNOWN)
5838 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005839 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840 else
5841 rettv->vval.v_string = NULL;
5842 }
5843 rettv->v_type = VAR_STRING;
5844 }
5845 else
5846#endif
5847 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5848}
5849
5850/*
5851 * "inputlist()" function
5852 */
5853 static void
5854f_inputlist(typval_T *argvars, typval_T *rettv)
5855{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005856 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 listitem_T *li;
5858 int selected;
5859 int mouse_used;
5860
5861#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005862 // While starting up, there is no place to enter text. When running tests
5863 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005864 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 return;
5866#endif
5867 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005869 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005870 return;
5871 }
5872
5873 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005874 msg_row = Rows - 1; // for when 'cmdheight' > 1
5875 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876 msg_scroll = TRUE;
5877 msg_clr_eos();
5878
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005879 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005880 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005881 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005883 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005884 msg_putchar('\n');
5885 }
5886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005887 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 selected = prompt_for_number(&mouse_used);
5889 if (mouse_used)
5890 selected -= lines_left;
5891
5892 rettv->vval.v_number = selected;
5893}
5894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5896
5897/*
5898 * "inputrestore()" function
5899 */
5900 static void
5901f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5902{
5903 if (ga_userinput.ga_len > 0)
5904 {
5905 --ga_userinput.ga_len;
5906 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005907 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005908 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909 }
5910 else if (p_verbose > 1)
5911 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005912 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005913 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005914 }
5915}
5916
5917/*
5918 * "inputsave()" function
5919 */
5920 static void
5921f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5922{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 if (ga_grow(&ga_userinput, 1) == OK)
5925 {
5926 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5927 + ga_userinput.ga_len);
5928 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005929 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 }
5931 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933}
5934
5935/*
5936 * "inputsecret()" function
5937 */
5938 static void
5939f_inputsecret(typval_T *argvars, typval_T *rettv)
5940{
5941 ++cmdline_star;
5942 ++inputsecret_flag;
5943 f_input(argvars, rettv);
5944 --cmdline_star;
5945 --inputsecret_flag;
5946}
5947
5948/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005949 * "interrupt()" function
5950 */
5951 static void
5952f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5953{
5954 got_int = TRUE;
5955}
5956
5957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 * "invert(expr)" function
5959 */
5960 static void
5961f_invert(typval_T *argvars, typval_T *rettv)
5962{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005963 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005964}
5965
5966/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 * "islocked()" function
5968 */
5969 static void
5970f_islocked(typval_T *argvars, typval_T *rettv)
5971{
5972 lval_T lv;
5973 char_u *end;
5974 dictitem_T *di;
5975
5976 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005977 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005978 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 if (end != NULL && lv.ll_name != NULL)
5980 {
5981 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005982 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 else
5984 {
5985 if (lv.ll_tv == NULL)
5986 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005987 di = find_var(lv.ll_name, NULL, TRUE);
5988 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005990 // Consider a variable locked when:
5991 // 1. the variable itself is locked
5992 // 2. the value of the variable is locked.
5993 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005994 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5995 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996 }
5997 }
5998 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005999 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006001 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006003 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006004 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6005 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006006 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6008 }
6009 }
6010
6011 clear_lval(&lv);
6012}
6013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006014/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 * "last_buffer_nr()" function.
6016 */
6017 static void
6018f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6019{
6020 int n = 0;
6021 buf_T *buf;
6022
Bram Moolenaar29323592016-07-24 22:04:11 +02006023 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 if (n < buf->b_fnum)
6025 n = buf->b_fnum;
6026
6027 rettv->vval.v_number = n;
6028}
6029
6030/*
6031 * "len()" function
6032 */
6033 static void
6034f_len(typval_T *argvars, typval_T *rettv)
6035{
6036 switch (argvars[0].v_type)
6037 {
6038 case VAR_STRING:
6039 case VAR_NUMBER:
6040 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006041 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006043 case VAR_BLOB:
6044 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6045 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046 case VAR_LIST:
6047 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6048 break;
6049 case VAR_DICT:
6050 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6051 break;
6052 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006053 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006055 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006056 case VAR_SPECIAL:
6057 case VAR_FLOAT:
6058 case VAR_FUNC:
6059 case VAR_PARTIAL:
6060 case VAR_JOB:
6061 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006062 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006063 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 break;
6065 }
6066}
6067
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006069libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070{
6071#ifdef FEAT_LIBCALL
6072 char_u *string_in;
6073 char_u **string_result;
6074 int nr_result;
6075#endif
6076
6077 rettv->v_type = type;
6078 if (type != VAR_NUMBER)
6079 rettv->vval.v_string = NULL;
6080
6081 if (check_restricted() || check_secure())
6082 return;
6083
6084#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6087 {
6088 string_in = NULL;
6089 if (argvars[2].v_type == VAR_STRING)
6090 string_in = argvars[2].vval.v_string;
6091 if (type == VAR_NUMBER)
6092 string_result = NULL;
6093 else
6094 string_result = &rettv->vval.v_string;
6095 if (mch_libcall(argvars[0].vval.v_string,
6096 argvars[1].vval.v_string,
6097 string_in,
6098 argvars[2].vval.v_number,
6099 string_result,
6100 &nr_result) == OK
6101 && type == VAR_NUMBER)
6102 rettv->vval.v_number = nr_result;
6103 }
6104#endif
6105}
6106
6107/*
6108 * "libcall()" function
6109 */
6110 static void
6111f_libcall(typval_T *argvars, typval_T *rettv)
6112{
6113 libcall_common(argvars, rettv, VAR_STRING);
6114}
6115
6116/*
6117 * "libcallnr()" function
6118 */
6119 static void
6120f_libcallnr(typval_T *argvars, typval_T *rettv)
6121{
6122 libcall_common(argvars, rettv, VAR_NUMBER);
6123}
6124
6125/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006126 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 */
6128 static void
6129f_line(typval_T *argvars, typval_T *rettv)
6130{
6131 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006132 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006134 int id;
6135 tabpage_T *tp;
6136 win_T *wp;
6137 win_T *save_curwin;
6138 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006140 if (argvars[1].v_type != VAR_UNKNOWN)
6141 {
6142 // use window specified in the second argument
6143 id = (int)tv_get_number(&argvars[1]);
6144 wp = win_id2wp_tp(id, &tp);
6145 if (wp != NULL && tp != NULL)
6146 {
6147 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6148 == OK)
6149 {
6150 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006151 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006152 }
6153 restore_win_noblock(save_curwin, save_curtab, TRUE);
6154 }
6155 }
6156 else
6157 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006158 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160 if (fp != NULL)
6161 lnum = fp->lnum;
6162 rettv->vval.v_number = lnum;
6163}
6164
6165/*
6166 * "line2byte(lnum)" function
6167 */
6168 static void
6169f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6170{
6171#ifndef FEAT_BYTEOFF
6172 rettv->vval.v_number = -1;
6173#else
6174 linenr_T lnum;
6175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006176 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6178 rettv->vval.v_number = -1;
6179 else
6180 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6181 if (rettv->vval.v_number >= 0)
6182 ++rettv->vval.v_number;
6183#endif
6184}
6185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186#ifdef FEAT_LUA
6187/*
6188 * "luaeval()" function
6189 */
6190 static void
6191f_luaeval(typval_T *argvars, typval_T *rettv)
6192{
6193 char_u *str;
6194 char_u buf[NUMBUFLEN];
6195
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006196 if (check_restricted() || check_secure())
6197 return;
6198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006199 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200 do_luaeval(str, argvars + 1, rettv);
6201}
6202#endif
6203
6204/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 * "maparg()" function
6206 */
6207 static void
6208f_maparg(typval_T *argvars, typval_T *rettv)
6209{
6210 get_maparg(argvars, rettv, TRUE);
6211}
6212
6213/*
6214 * "mapcheck()" function
6215 */
6216 static void
6217f_mapcheck(typval_T *argvars, typval_T *rettv)
6218{
6219 get_maparg(argvars, rettv, FALSE);
6220}
6221
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006222typedef enum
6223{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 MATCH_END, // matchend()
6225 MATCH_MATCH, // match()
6226 MATCH_STR, // matchstr()
6227 MATCH_LIST, // matchlist()
6228 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006229} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230
6231 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006232find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233{
6234 char_u *str = NULL;
6235 long len = 0;
6236 char_u *expr = NULL;
6237 char_u *pat;
6238 regmatch_T regmatch;
6239 char_u patbuf[NUMBUFLEN];
6240 char_u strbuf[NUMBUFLEN];
6241 char_u *save_cpo;
6242 long start = 0;
6243 long nth = 1;
6244 colnr_T startcol = 0;
6245 int match = 0;
6246 list_T *l = NULL;
6247 listitem_T *li = NULL;
6248 long idx = 0;
6249 char_u *tofree = NULL;
6250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006251 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006253 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254
6255 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006256 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006258 // type MATCH_LIST: return empty list when there are no matches.
6259 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 if (rettv_list_alloc(rettv) == FAIL)
6261 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006262 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 && (list_append_string(rettv->vval.v_list,
6264 (char_u *)"", 0) == FAIL
6265 || list_append_number(rettv->vval.v_list,
6266 (varnumber_T)-1) == FAIL
6267 || list_append_number(rettv->vval.v_list,
6268 (varnumber_T)-1) == FAIL
6269 || list_append_number(rettv->vval.v_list,
6270 (varnumber_T)-1) == FAIL))
6271 {
6272 list_free(rettv->vval.v_list);
6273 rettv->vval.v_list = NULL;
6274 goto theend;
6275 }
6276 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006277 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 {
6279 rettv->v_type = VAR_STRING;
6280 rettv->vval.v_string = NULL;
6281 }
6282
6283 if (argvars[0].v_type == VAR_LIST)
6284 {
6285 if ((l = argvars[0].vval.v_list) == NULL)
6286 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006287 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006288 li = l->lv_first;
6289 }
6290 else
6291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006292 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 len = (long)STRLEN(str);
6294 }
6295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 if (pat == NULL)
6298 goto theend;
6299
6300 if (argvars[2].v_type != VAR_UNKNOWN)
6301 {
6302 int error = FALSE;
6303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006304 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 if (error)
6306 goto theend;
6307 if (l != NULL)
6308 {
6309 li = list_find(l, start);
6310 if (li == NULL)
6311 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006312 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 }
6314 else
6315 {
6316 if (start < 0)
6317 start = 0;
6318 if (start > len)
6319 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006320 // When "count" argument is there ignore matches before "start",
6321 // otherwise skip part of the string. Differs when pattern is "^"
6322 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006323 if (argvars[3].v_type != VAR_UNKNOWN)
6324 startcol = start;
6325 else
6326 {
6327 str += start;
6328 len -= start;
6329 }
6330 }
6331
6332 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 if (error)
6335 goto theend;
6336 }
6337
6338 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6339 if (regmatch.regprog != NULL)
6340 {
6341 regmatch.rm_ic = p_ic;
6342
6343 for (;;)
6344 {
6345 if (l != NULL)
6346 {
6347 if (li == NULL)
6348 {
6349 match = FALSE;
6350 break;
6351 }
6352 vim_free(tofree);
6353 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6354 if (str == NULL)
6355 break;
6356 }
6357
6358 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6359
6360 if (match && --nth <= 0)
6361 break;
6362 if (l == NULL && !match)
6363 break;
6364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006365 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366 if (l != NULL)
6367 {
6368 li = li->li_next;
6369 ++idx;
6370 }
6371 else
6372 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 startcol = (colnr_T)(regmatch.startp[0]
6374 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006375 if (startcol > (colnr_T)len
6376 || str + startcol <= regmatch.startp[0])
6377 {
6378 match = FALSE;
6379 break;
6380 }
6381 }
6382 }
6383
6384 if (match)
6385 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006386 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 {
6388 listitem_T *li1 = rettv->vval.v_list->lv_first;
6389 listitem_T *li2 = li1->li_next;
6390 listitem_T *li3 = li2->li_next;
6391 listitem_T *li4 = li3->li_next;
6392
6393 vim_free(li1->li_tv.vval.v_string);
6394 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006395 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 li3->li_tv.vval.v_number =
6397 (varnumber_T)(regmatch.startp[0] - expr);
6398 li4->li_tv.vval.v_number =
6399 (varnumber_T)(regmatch.endp[0] - expr);
6400 if (l != NULL)
6401 li2->li_tv.vval.v_number = (varnumber_T)idx;
6402 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006403 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 {
6405 int i;
6406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006407 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 for (i = 0; i < NSUBEXP; ++i)
6409 {
6410 if (regmatch.endp[i] == NULL)
6411 {
6412 if (list_append_string(rettv->vval.v_list,
6413 (char_u *)"", 0) == FAIL)
6414 break;
6415 }
6416 else if (list_append_string(rettv->vval.v_list,
6417 regmatch.startp[i],
6418 (int)(regmatch.endp[i] - regmatch.startp[i]))
6419 == FAIL)
6420 break;
6421 }
6422 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006423 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006425 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006426 if (l != NULL)
6427 copy_tv(&li->li_tv, rettv);
6428 else
6429 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006430 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 }
6432 else if (l != NULL)
6433 rettv->vval.v_number = idx;
6434 else
6435 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006436 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 rettv->vval.v_number =
6438 (varnumber_T)(regmatch.startp[0] - str);
6439 else
6440 rettv->vval.v_number =
6441 (varnumber_T)(regmatch.endp[0] - str);
6442 rettv->vval.v_number += (varnumber_T)(str - expr);
6443 }
6444 }
6445 vim_regfree(regmatch.regprog);
6446 }
6447
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006448theend:
6449 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006450 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451 listitem_remove(rettv->vval.v_list,
6452 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006453 vim_free(tofree);
6454 p_cpo = save_cpo;
6455}
6456
6457/*
6458 * "match()" function
6459 */
6460 static void
6461f_match(typval_T *argvars, typval_T *rettv)
6462{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006463 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464}
6465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466/*
6467 * "matchend()" function
6468 */
6469 static void
6470f_matchend(typval_T *argvars, typval_T *rettv)
6471{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006472 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473}
6474
6475/*
6476 * "matchlist()" function
6477 */
6478 static void
6479f_matchlist(typval_T *argvars, typval_T *rettv)
6480{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006481 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482}
6483
6484/*
6485 * "matchstr()" function
6486 */
6487 static void
6488f_matchstr(typval_T *argvars, typval_T *rettv)
6489{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006490 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491}
6492
6493/*
6494 * "matchstrpos()" function
6495 */
6496 static void
6497f_matchstrpos(typval_T *argvars, typval_T *rettv)
6498{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006499 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500}
6501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006502 static void
6503max_min(typval_T *argvars, typval_T *rettv, int domax)
6504{
6505 varnumber_T n = 0;
6506 varnumber_T i;
6507 int error = FALSE;
6508
6509 if (argvars[0].v_type == VAR_LIST)
6510 {
6511 list_T *l;
6512 listitem_T *li;
6513
6514 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006515 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006517 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006519 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6520 n = l->lv_u.nonmat.lv_start;
6521 else
6522 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6523 * l->lv_u.nonmat.lv_stride;
6524 }
6525 else
6526 {
6527 li = l->lv_first;
6528 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006529 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006530 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006531 if (error)
6532 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006533 for (;;)
6534 {
6535 li = li->li_next;
6536 if (li == NULL)
6537 break;
6538 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006539 if (error)
6540 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006541 if (domax ? i > n : i < n)
6542 n = i;
6543 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544 }
6545 }
6546 }
6547 }
6548 else if (argvars[0].v_type == VAR_DICT)
6549 {
6550 dict_T *d;
6551 int first = TRUE;
6552 hashitem_T *hi;
6553 int todo;
6554
6555 d = argvars[0].vval.v_dict;
6556 if (d != NULL)
6557 {
6558 todo = (int)d->dv_hashtab.ht_used;
6559 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6560 {
6561 if (!HASHITEM_EMPTY(hi))
6562 {
6563 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006564 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006565 if (error)
6566 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567 if (first)
6568 {
6569 n = i;
6570 first = FALSE;
6571 }
6572 else if (domax ? i > n : i < n)
6573 n = i;
6574 }
6575 }
6576 }
6577 }
6578 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006579 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006580
6581 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582}
6583
6584/*
6585 * "max()" function
6586 */
6587 static void
6588f_max(typval_T *argvars, typval_T *rettv)
6589{
6590 max_min(argvars, rettv, TRUE);
6591}
6592
6593/*
6594 * "min()" function
6595 */
6596 static void
6597f_min(typval_T *argvars, typval_T *rettv)
6598{
6599 max_min(argvars, rettv, FALSE);
6600}
6601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602#if defined(FEAT_MZSCHEME) || defined(PROTO)
6603/*
6604 * "mzeval()" function
6605 */
6606 static void
6607f_mzeval(typval_T *argvars, typval_T *rettv)
6608{
6609 char_u *str;
6610 char_u buf[NUMBUFLEN];
6611
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006612 if (check_restricted() || check_secure())
6613 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006614 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006615 do_mzeval(str, rettv);
6616}
6617
6618 void
6619mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6620{
6621 typval_T argvars[3];
6622
6623 argvars[0].v_type = VAR_STRING;
6624 argvars[0].vval.v_string = name;
6625 copy_tv(args, &argvars[1]);
6626 argvars[2].v_type = VAR_UNKNOWN;
6627 f_call(argvars, rettv);
6628 clear_tv(&argvars[1]);
6629}
6630#endif
6631
6632/*
6633 * "nextnonblank()" function
6634 */
6635 static void
6636f_nextnonblank(typval_T *argvars, typval_T *rettv)
6637{
6638 linenr_T lnum;
6639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006640 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 {
6642 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6643 {
6644 lnum = 0;
6645 break;
6646 }
6647 if (*skipwhite(ml_get(lnum)) != NUL)
6648 break;
6649 }
6650 rettv->vval.v_number = lnum;
6651}
6652
6653/*
6654 * "nr2char()" function
6655 */
6656 static void
6657f_nr2char(typval_T *argvars, typval_T *rettv)
6658{
6659 char_u buf[NUMBUFLEN];
6660
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661 if (has_mbyte)
6662 {
6663 int utf8 = 0;
6664
6665 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006666 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006668 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006670 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 }
6672 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006674 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 buf[1] = NUL;
6676 }
6677 rettv->v_type = VAR_STRING;
6678 rettv->vval.v_string = vim_strsave(buf);
6679}
6680
6681/*
6682 * "or(expr, expr)" function
6683 */
6684 static void
6685f_or(typval_T *argvars, typval_T *rettv)
6686{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006687 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6688 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689}
6690
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691#ifdef FEAT_PERL
6692/*
6693 * "perleval()" function
6694 */
6695 static void
6696f_perleval(typval_T *argvars, typval_T *rettv)
6697{
6698 char_u *str;
6699 char_u buf[NUMBUFLEN];
6700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006701 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006702 do_perleval(str, rettv);
6703}
6704#endif
6705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706/*
6707 * "prevnonblank()" function
6708 */
6709 static void
6710f_prevnonblank(typval_T *argvars, typval_T *rettv)
6711{
6712 linenr_T lnum;
6713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006714 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6716 lnum = 0;
6717 else
6718 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6719 --lnum;
6720 rettv->vval.v_number = lnum;
6721}
6722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006723// This dummy va_list is here because:
6724// - passing a NULL pointer doesn't work when va_list isn't a pointer
6725// - locally in the function results in a "used before set" warning
6726// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727static va_list ap;
6728
6729/*
6730 * "printf()" function
6731 */
6732 static void
6733f_printf(typval_T *argvars, typval_T *rettv)
6734{
6735 char_u buf[NUMBUFLEN];
6736 int len;
6737 char_u *s;
6738 int saved_did_emsg = did_emsg;
6739 char *fmt;
6740
6741 rettv->v_type = VAR_STRING;
6742 rettv->vval.v_string = NULL;
6743
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006744 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006746 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006747 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748 if (!did_emsg)
6749 {
6750 s = alloc(len + 1);
6751 if (s != NULL)
6752 {
6753 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006754 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6755 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 }
6757 }
6758 did_emsg |= saved_did_emsg;
6759}
6760
6761/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006762 * "pum_getpos()" function
6763 */
6764 static void
6765f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6766{
6767 if (rettv_dict_alloc(rettv) != OK)
6768 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006769 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006770}
6771
6772/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006773 * "pumvisible()" function
6774 */
6775 static void
6776f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6777{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778 if (pum_visible())
6779 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780}
6781
6782#ifdef FEAT_PYTHON3
6783/*
6784 * "py3eval()" function
6785 */
6786 static void
6787f_py3eval(typval_T *argvars, typval_T *rettv)
6788{
6789 char_u *str;
6790 char_u buf[NUMBUFLEN];
6791
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006792 if (check_restricted() || check_secure())
6793 return;
6794
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006795 if (p_pyx == 0)
6796 p_pyx = 3;
6797
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006798 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 do_py3eval(str, rettv);
6800}
6801#endif
6802
6803#ifdef FEAT_PYTHON
6804/*
6805 * "pyeval()" function
6806 */
6807 static void
6808f_pyeval(typval_T *argvars, typval_T *rettv)
6809{
6810 char_u *str;
6811 char_u buf[NUMBUFLEN];
6812
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006813 if (check_restricted() || check_secure())
6814 return;
6815
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006816 if (p_pyx == 0)
6817 p_pyx = 2;
6818
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006819 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820 do_pyeval(str, rettv);
6821}
6822#endif
6823
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006824#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6825/*
6826 * "pyxeval()" function
6827 */
6828 static void
6829f_pyxeval(typval_T *argvars, typval_T *rettv)
6830{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006831 if (check_restricted() || check_secure())
6832 return;
6833
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006834# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6835 init_pyxversion();
6836 if (p_pyx == 2)
6837 f_pyeval(argvars, rettv);
6838 else
6839 f_py3eval(argvars, rettv);
6840# elif defined(FEAT_PYTHON)
6841 f_pyeval(argvars, rettv);
6842# elif defined(FEAT_PYTHON3)
6843 f_py3eval(argvars, rettv);
6844# endif
6845}
6846#endif
6847
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006848static UINT32_T srand_seed_for_testing = 0;
6849static int srand_seed_for_testing_is_used = FALSE;
6850
6851 static void
6852f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6853{
6854 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006855 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006856 else
6857 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006858 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6859 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006860 }
6861}
6862
6863 static void
6864init_srand(UINT32_T *x)
6865{
6866#ifndef MSWIN
6867 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6868#endif
6869
6870 if (srand_seed_for_testing_is_used)
6871 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006872 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006873 return;
6874 }
6875#ifndef MSWIN
6876 if (dev_urandom_state != FAIL)
6877 {
6878 int fd = open("/dev/urandom", O_RDONLY);
6879 struct {
6880 union {
6881 UINT32_T number;
6882 char bytes[sizeof(UINT32_T)];
6883 } contents;
6884 } buf;
6885
6886 // Attempt reading /dev/urandom.
6887 if (fd == -1)
6888 dev_urandom_state = FAIL;
6889 else
6890 {
6891 buf.contents.number = 0;
6892 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6893 != sizeof(UINT32_T))
6894 dev_urandom_state = FAIL;
6895 else
6896 {
6897 dev_urandom_state = OK;
6898 *x = buf.contents.number;
6899 }
6900 close(fd);
6901 }
6902 }
6903 if (dev_urandom_state != OK)
6904 // Reading /dev/urandom doesn't work, fall back to time().
6905#endif
6906 *x = vim_time();
6907}
6908
6909#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6910#define SPLITMIX32(x, z) ( \
6911 z = (x += 0x9e3779b9), \
6912 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6913 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6914 z ^ (z >> 16) \
6915 )
6916#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6917 result = ROTL(y * 5, 7) * 9; \
6918 t = y << 9; \
6919 z ^= x; \
6920 w ^= y; \
6921 y ^= z, x ^= w; \
6922 z ^= t; \
6923 w = ROTL(w, 11);
6924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006926 * "rand()" function
6927 */
6928 static void
6929f_rand(typval_T *argvars, typval_T *rettv)
6930{
6931 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006932 static UINT32_T gx, gy, gz, gw;
6933 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006934 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006935 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006936
6937 if (argvars[0].v_type == VAR_UNKNOWN)
6938 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006939 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006940 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006941 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006942 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006943 init_srand(&x);
6944
6945 gx = SPLITMIX32(x, z);
6946 gy = SPLITMIX32(x, z);
6947 gz = SPLITMIX32(x, z);
6948 gw = SPLITMIX32(x, z);
6949 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006950 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006951
6952 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006953 }
6954 else if (argvars[0].v_type == VAR_LIST)
6955 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006956 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006957 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006958 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006959
6960 lx = list_find(l, 0L);
6961 ly = list_find(l, 1L);
6962 lz = list_find(l, 2L);
6963 lw = list_find(l, 3L);
6964 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6965 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6966 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6967 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6968 x = (UINT32_T)lx->li_tv.vval.v_number;
6969 y = (UINT32_T)ly->li_tv.vval.v_number;
6970 z = (UINT32_T)lz->li_tv.vval.v_number;
6971 w = (UINT32_T)lw->li_tv.vval.v_number;
6972
6973 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6974
6975 lx->li_tv.vval.v_number = (varnumber_T)x;
6976 ly->li_tv.vval.v_number = (varnumber_T)y;
6977 lz->li_tv.vval.v_number = (varnumber_T)z;
6978 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006979 }
6980 else
6981 goto theend;
6982
6983 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006984 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006985 return;
6986
6987theend:
6988 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006989 rettv->v_type = VAR_NUMBER;
6990 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006991}
6992
6993/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006994 * "srand()" function
6995 */
6996 static void
6997f_srand(typval_T *argvars, typval_T *rettv)
6998{
6999 UINT32_T x = 0, z;
7000
7001 if (rettv_list_alloc(rettv) == FAIL)
7002 return;
7003 if (argvars[0].v_type == VAR_UNKNOWN)
7004 {
7005 init_srand(&x);
7006 }
7007 else
7008 {
7009 int error = FALSE;
7010
7011 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7012 if (error)
7013 return;
7014 }
7015
7016 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7017 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7018 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7019 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7020}
7021
7022#undef ROTL
7023#undef SPLITMIX32
7024#undef SHUFFLE_XOSHIRO128STARSTAR
7025
7026/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027 * "range()" function
7028 */
7029 static void
7030f_range(typval_T *argvars, typval_T *rettv)
7031{
7032 varnumber_T start;
7033 varnumber_T end;
7034 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 int error = FALSE;
7036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007037 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 if (argvars[1].v_type == VAR_UNKNOWN)
7039 {
7040 end = start - 1;
7041 start = 0;
7042 }
7043 else
7044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007045 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007047 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 }
7049
7050 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007051 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007053 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007055 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058 list_T *list = rettv->vval.v_list;
7059
7060 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007061 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 // be called.
7063 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007064 list->lv_u.nonmat.lv_start = start;
7065 list->lv_u.nonmat.lv_end = end;
7066 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007067 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 }
7069}
7070
7071/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007072 * Materialize "list".
7073 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 */
7075 void
7076range_list_materialize(list_T *list)
7077{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007078 varnumber_T start = list->lv_u.nonmat.lv_start;
7079 varnumber_T end = list->lv_u.nonmat.lv_end;
7080 int stride = list->lv_u.nonmat.lv_stride;
7081 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007082
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007083 list->lv_first = NULL;
7084 list->lv_u.mat.lv_last = NULL;
7085 list->lv_len = 0;
7086 list->lv_u.mat.lv_idx_item = NULL;
7087 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7088 if (list_append_number(list, (varnumber_T)i) == FAIL)
7089 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090}
7091
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007092/*
7093 * "getreginfo()" function
7094 */
7095 static void
7096f_getreginfo(typval_T *argvars, typval_T *rettv)
7097{
7098 char_u *strregname;
7099 int regname;
7100 char_u buf[NUMBUFLEN + 2];
7101 long reglen = 0;
7102 dict_T *dict;
7103 list_T *list;
7104
7105 if (argvars[0].v_type != VAR_UNKNOWN)
7106 {
7107 strregname = tv_get_string_chk(&argvars[0]);
7108 if (strregname == NULL)
7109 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007110 if (in_vim9script() && STRLEN(strregname) > 1)
7111 {
7112 semsg(_(e_register_name_must_be_one_char_str), strregname);
7113 return;
7114 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007115 }
7116 else
7117 strregname = get_vim_var_str(VV_REG);
7118
7119 regname = (strregname == NULL ? '"' : *strregname);
7120 if (regname == 0 || regname == '@')
7121 regname = '"';
7122
7123 if (rettv_dict_alloc(rettv) == FAIL)
7124 return;
7125 dict = rettv->vval.v_dict;
7126
7127 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7128 if (list == NULL)
7129 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007130 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007131
7132 buf[0] = NUL;
7133 buf[1] = NUL;
7134 switch (get_reg_type(regname, &reglen))
7135 {
7136 case MLINE: buf[0] = 'V'; break;
7137 case MCHAR: buf[0] = 'v'; break;
7138 case MBLOCK:
7139 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7140 reglen + 1);
7141 break;
7142 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007143 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007144
7145 buf[0] = get_register_name(get_unname_register());
7146 buf[1] = NUL;
7147 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007148 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007149 else
7150 {
7151 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7152
7153 if (item != NULL)
7154 {
7155 item->di_tv.v_type = VAR_SPECIAL;
7156 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007157 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007158 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007159 }
7160 }
7161}
7162
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007163 static void
7164return_register(int regname, typval_T *rettv)
7165{
7166 char_u buf[2] = {0, 0};
7167
7168 buf[0] = (char_u)regname;
7169 rettv->v_type = VAR_STRING;
7170 rettv->vval.v_string = vim_strsave(buf);
7171}
7172
7173/*
7174 * "reg_executing()" function
7175 */
7176 static void
7177f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7178{
7179 return_register(reg_executing, rettv);
7180}
7181
7182/*
7183 * "reg_recording()" function
7184 */
7185 static void
7186f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7187{
7188 return_register(reg_recording, rettv);
7189}
7190
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007192 * "rename({from}, {to})" function
7193 */
7194 static void
7195f_rename(typval_T *argvars, typval_T *rettv)
7196{
7197 char_u buf[NUMBUFLEN];
7198
7199 if (check_restricted() || check_secure())
7200 rettv->vval.v_number = -1;
7201 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007202 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7203 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204}
7205
7206/*
7207 * "repeat()" function
7208 */
7209 static void
7210f_repeat(typval_T *argvars, typval_T *rettv)
7211{
7212 char_u *p;
7213 int n;
7214 int slen;
7215 int len;
7216 char_u *r;
7217 int i;
7218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007219 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 if (argvars[0].v_type == VAR_LIST)
7221 {
7222 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7223 while (n-- > 0)
7224 if (list_extend(rettv->vval.v_list,
7225 argvars[0].vval.v_list, NULL) == FAIL)
7226 break;
7227 }
7228 else
7229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007230 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 rettv->v_type = VAR_STRING;
7232 rettv->vval.v_string = NULL;
7233
7234 slen = (int)STRLEN(p);
7235 len = slen * n;
7236 if (len <= 0)
7237 return;
7238
7239 r = alloc(len + 1);
7240 if (r != NULL)
7241 {
7242 for (i = 0; i < n; i++)
7243 mch_memmove(r + i * slen, p, (size_t)slen);
7244 r[len] = NUL;
7245 }
7246
7247 rettv->vval.v_string = r;
7248 }
7249}
7250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007251#define SP_NOMOVE 0x01 // don't move cursor
7252#define SP_REPEAT 0x02 // repeat to find outer pair
7253#define SP_RETCOUNT 0x04 // return matchcount
7254#define SP_SETPCMARK 0x08 // set previous context mark
7255#define SP_START 0x10 // accept match at start position
7256#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7257#define SP_END 0x40 // leave cursor at end of match
7258#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260/*
7261 * Get flags for a search function.
7262 * Possibly sets "p_ws".
7263 * Returns BACKWARD, FORWARD or zero (for an error).
7264 */
7265 static int
7266get_search_arg(typval_T *varp, int *flagsp)
7267{
7268 int dir = FORWARD;
7269 char_u *flags;
7270 char_u nbuf[NUMBUFLEN];
7271 int mask;
7272
7273 if (varp->v_type != VAR_UNKNOWN)
7274 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007275 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007277 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 while (*flags != NUL)
7279 {
7280 switch (*flags)
7281 {
7282 case 'b': dir = BACKWARD; break;
7283 case 'w': p_ws = TRUE; break;
7284 case 'W': p_ws = FALSE; break;
7285 default: mask = 0;
7286 if (flagsp != NULL)
7287 switch (*flags)
7288 {
7289 case 'c': mask = SP_START; break;
7290 case 'e': mask = SP_END; break;
7291 case 'm': mask = SP_RETCOUNT; break;
7292 case 'n': mask = SP_NOMOVE; break;
7293 case 'p': mask = SP_SUBPAT; break;
7294 case 'r': mask = SP_REPEAT; break;
7295 case 's': mask = SP_SETPCMARK; break;
7296 case 'z': mask = SP_COLUMN; break;
7297 }
7298 if (mask == 0)
7299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007300 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301 dir = 0;
7302 }
7303 else
7304 *flagsp |= mask;
7305 }
7306 if (dir == 0)
7307 break;
7308 ++flags;
7309 }
7310 }
7311 return dir;
7312}
7313
7314/*
7315 * Shared by search() and searchpos() functions.
7316 */
7317 static int
7318search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7319{
7320 int flags;
7321 char_u *pat;
7322 pos_T pos;
7323 pos_T save_cursor;
7324 int save_p_ws = p_ws;
7325 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007326 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007329 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 long time_limit = 0;
7331#endif
7332 int options = SEARCH_KEEP;
7333 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007334 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007335 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007336 pos_T firstpos;
7337
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007338 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007339 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 if (dir == 0)
7341 goto theend;
7342 flags = *flagsp;
7343 if (flags & SP_START)
7344 options |= SEARCH_START;
7345 if (flags & SP_END)
7346 options |= SEARCH_END;
7347 if (flags & SP_COLUMN)
7348 options |= SEARCH_COL;
7349
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007350 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007353 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 if (lnum_stop < 0)
7355 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007356 if (argvars[3].v_type != VAR_UNKNOWN)
7357 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007358#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 if (time_limit < 0)
7361 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007363 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007364 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 }
7366
7367#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007368 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 profile_setlimit(time_limit, &tm);
7370#endif
7371
7372 /*
7373 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7374 * Check to make sure only those flags are set.
7375 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7376 * flags cannot be set. Check for that condition also.
7377 */
7378 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7379 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007381 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 goto theend;
7383 }
7384
7385 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007386 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007387 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007388 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7389#ifdef FEAT_RELTIME
7390 sia.sa_tm = &tm;
7391#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007392
7393 // Repeat until {skip} returns FALSE.
7394 for (;;)
7395 {
7396 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007397 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007398 // finding the first match again means there is no match where {skip}
7399 // evaluates to zero.
7400 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7401 subpatnum = FAIL;
7402
Bram Moolenaara9c01042020-06-07 14:50:50 +02007403 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007404 // didn't find it or no skip argument
7405 break;
7406 firstpos = pos;
7407
Bram Moolenaara9c01042020-06-07 14:50:50 +02007408 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007409 {
7410 int do_skip;
7411 int err;
7412 pos_T save_pos = curwin->w_cursor;
7413
7414 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007415 err = FALSE;
7416 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007417 curwin->w_cursor = save_pos;
7418 if (err)
7419 {
7420 // Evaluating {skip} caused an error, break here.
7421 subpatnum = FAIL;
7422 break;
7423 }
7424 if (!do_skip)
7425 break;
7426 }
7427 }
7428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 if (subpatnum != FAIL)
7430 {
7431 if (flags & SP_SUBPAT)
7432 retval = subpatnum;
7433 else
7434 retval = pos.lnum;
7435 if (flags & SP_SETPCMARK)
7436 setpcmark();
7437 curwin->w_cursor = pos;
7438 if (match_pos != NULL)
7439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007440 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 match_pos->lnum = pos.lnum;
7442 match_pos->col = pos.col + 1;
7443 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007444 // "/$" will put the cursor after the end of the line, may need to
7445 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007446 check_cursor();
7447 }
7448
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007449 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 if (flags & SP_NOMOVE)
7451 curwin->w_cursor = save_cursor;
7452 else
7453 curwin->w_set_curswant = TRUE;
7454theend:
7455 p_ws = save_p_ws;
7456
7457 return retval;
7458}
7459
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007460#ifdef FEAT_RUBY
7461/*
7462 * "rubyeval()" function
7463 */
7464 static void
7465f_rubyeval(typval_T *argvars, typval_T *rettv)
7466{
7467 char_u *str;
7468 char_u buf[NUMBUFLEN];
7469
7470 str = tv_get_string_buf(&argvars[0], buf);
7471 do_rubyeval(str, rettv);
7472}
7473#endif
7474
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475/*
7476 * "screenattr()" function
7477 */
7478 static void
7479f_screenattr(typval_T *argvars, typval_T *rettv)
7480{
7481 int row;
7482 int col;
7483 int c;
7484
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007485 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7486 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 if (row < 0 || row >= screen_Rows
7488 || col < 0 || col >= screen_Columns)
7489 c = -1;
7490 else
7491 c = ScreenAttrs[LineOffset[row] + col];
7492 rettv->vval.v_number = c;
7493}
7494
7495/*
7496 * "screenchar()" function
7497 */
7498 static void
7499f_screenchar(typval_T *argvars, typval_T *rettv)
7500{
7501 int row;
7502 int col;
7503 int off;
7504 int c;
7505
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007506 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7507 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007508 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007509 c = -1;
7510 else
7511 {
7512 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 if (enc_utf8 && ScreenLinesUC[off] != 0)
7514 c = ScreenLinesUC[off];
7515 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 c = ScreenLines[off];
7517 }
7518 rettv->vval.v_number = c;
7519}
7520
7521/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007522 * "screenchars()" function
7523 */
7524 static void
7525f_screenchars(typval_T *argvars, typval_T *rettv)
7526{
7527 int row;
7528 int col;
7529 int off;
7530 int c;
7531 int i;
7532
7533 if (rettv_list_alloc(rettv) == FAIL)
7534 return;
7535 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7536 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7537 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7538 return;
7539
7540 off = LineOffset[row] + col;
7541 if (enc_utf8 && ScreenLinesUC[off] != 0)
7542 c = ScreenLinesUC[off];
7543 else
7544 c = ScreenLines[off];
7545 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7546
7547 if (enc_utf8)
7548
7549 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7550 list_append_number(rettv->vval.v_list,
7551 (varnumber_T)ScreenLinesC[i][off]);
7552}
7553
7554/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555 * "screencol()" function
7556 *
7557 * First column is 1 to be consistent with virtcol().
7558 */
7559 static void
7560f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7561{
7562 rettv->vval.v_number = screen_screencol() + 1;
7563}
7564
7565/*
7566 * "screenrow()" function
7567 */
7568 static void
7569f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7570{
7571 rettv->vval.v_number = screen_screenrow() + 1;
7572}
7573
7574/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007575 * "screenstring()" function
7576 */
7577 static void
7578f_screenstring(typval_T *argvars, typval_T *rettv)
7579{
7580 int row;
7581 int col;
7582 int off;
7583 int c;
7584 int i;
7585 char_u buf[MB_MAXBYTES + 1];
7586 int buflen = 0;
7587
7588 rettv->vval.v_string = NULL;
7589 rettv->v_type = VAR_STRING;
7590
7591 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7592 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7593 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7594 return;
7595
7596 off = LineOffset[row] + col;
7597 if (enc_utf8 && ScreenLinesUC[off] != 0)
7598 c = ScreenLinesUC[off];
7599 else
7600 c = ScreenLines[off];
7601 buflen += mb_char2bytes(c, buf);
7602
Bram Moolenaarf1387282021-03-22 17:11:15 +01007603 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007604 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7605 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7606
7607 buf[buflen] = NUL;
7608 rettv->vval.v_string = vim_strsave(buf);
7609}
7610
7611/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 * "search()" function
7613 */
7614 static void
7615f_search(typval_T *argvars, typval_T *rettv)
7616{
7617 int flags = 0;
7618
7619 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7620}
7621
7622/*
7623 * "searchdecl()" function
7624 */
7625 static void
7626f_searchdecl(typval_T *argvars, typval_T *rettv)
7627{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007628 int locally = TRUE;
7629 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 int error = FALSE;
7631 char_u *name;
7632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007633 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 if (argvars[1].v_type != VAR_UNKNOWN)
7637 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007638 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007640 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 }
7642 if (!error && name != NULL)
7643 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7644 locally, thisblock, SEARCH_KEEP) == FAIL;
7645}
7646
7647/*
7648 * Used by searchpair() and searchpairpos()
7649 */
7650 static int
7651searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7652{
7653 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007654 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 int save_p_ws = p_ws;
7656 int dir;
7657 int flags = 0;
7658 char_u nbuf1[NUMBUFLEN];
7659 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007660 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 long lnum_stop = 0;
7662 long time_limit = 0;
7663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007664 // Get the three pattern arguments: start, middle, end. Will result in an
7665 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007666 spat = tv_get_string_chk(&argvars[0]);
7667 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7668 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007672 // Handle the optional fourth argument: flags
7673 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 if (dir == 0)
7675 goto theend;
7676
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007677 // Don't accept SP_END or SP_SUBPAT.
7678 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 if ((flags & (SP_END | SP_SUBPAT)) != 0
7680 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007682 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 goto theend;
7684 }
7685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007686 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 if (flags & SP_REPEAT)
7688 p_ws = FALSE;
7689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007690 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691 if (argvars[3].v_type == VAR_UNKNOWN
7692 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007693 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694 else
7695 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007696 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007697 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 if (argvars[5].v_type != VAR_UNKNOWN)
7700 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007701 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007704 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007706 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007707#ifdef FEAT_RELTIME
7708 if (argvars[6].v_type != VAR_UNKNOWN)
7709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007710 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007713 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 }
7717#endif
7718 }
7719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720
7721 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7722 match_pos, lnum_stop, time_limit);
7723
7724theend:
7725 p_ws = save_p_ws;
7726
7727 return retval;
7728}
7729
7730/*
7731 * "searchpair()" function
7732 */
7733 static void
7734f_searchpair(typval_T *argvars, typval_T *rettv)
7735{
7736 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7737}
7738
7739/*
7740 * "searchpairpos()" function
7741 */
7742 static void
7743f_searchpairpos(typval_T *argvars, typval_T *rettv)
7744{
7745 pos_T match_pos;
7746 int lnum = 0;
7747 int col = 0;
7748
7749 if (rettv_list_alloc(rettv) == FAIL)
7750 return;
7751
7752 if (searchpair_cmn(argvars, &match_pos) > 0)
7753 {
7754 lnum = match_pos.lnum;
7755 col = match_pos.col;
7756 }
7757
7758 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7759 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7760}
7761
7762/*
7763 * Search for a start/middle/end thing.
7764 * Used by searchpair(), see its documentation for the details.
7765 * Returns 0 or -1 for no match,
7766 */
7767 long
7768do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007769 char_u *spat, // start pattern
7770 char_u *mpat, // middle pattern
7771 char_u *epat, // end pattern
7772 int dir, // BACKWARD or FORWARD
7773 typval_T *skip, // skip expression
7774 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007775 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007776 linenr_T lnum_stop, // stop at this line if not zero
7777 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778{
7779 char_u *save_cpo;
7780 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7781 long retval = 0;
7782 pos_T pos;
7783 pos_T firstpos;
7784 pos_T foundpos;
7785 pos_T save_cursor;
7786 pos_T save_pos;
7787 int n;
7788 int r;
7789 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007790 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791 int err;
7792 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007793#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007795#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007797 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 save_cpo = p_cpo;
7799 p_cpo = empty_option;
7800
7801#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007802 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007803 profile_setlimit(time_limit, &tm);
7804#endif
7805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007806 // Make two search patterns: start/end (pat2, for in nested pairs) and
7807 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007808 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7809 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 if (pat2 == NULL || pat3 == NULL)
7811 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007812 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 if (*mpat == NUL)
7814 STRCPY(pat3, pat2);
7815 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007816 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 spat, epat, mpat);
7818 if (flags & SP_START)
7819 options |= SEARCH_START;
7820
Bram Moolenaar48570482017-10-30 21:48:41 +01007821 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007822 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 save_cursor = curwin->w_cursor;
7825 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007826 CLEAR_POS(&firstpos);
7827 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 pat = pat3;
7829 for (;;)
7830 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007831 searchit_arg_T sia;
7832
Bram Moolenaara80faa82020-04-12 19:37:17 +02007833 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007834 sia.sa_stop_lnum = lnum_stop;
7835#ifdef FEAT_RELTIME
7836 sia.sa_tm = &tm;
7837#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007838 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007839 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007840 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007841 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 break;
7843
7844 if (firstpos.lnum == 0)
7845 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007846 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007848 // Found the same position again. Can happen with a pattern that
7849 // has "\zs" at the end and searching backwards. Advance one
7850 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851 if (dir == BACKWARD)
7852 decl(&pos);
7853 else
7854 incl(&pos);
7855 }
7856 foundpos = pos;
7857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007858 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 options &= ~SEARCH_START;
7860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007861 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007862 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 {
7864 save_pos = curwin->w_cursor;
7865 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007866 err = FALSE;
7867 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007868 curwin->w_cursor = save_pos;
7869 if (err)
7870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007871 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 curwin->w_cursor = save_cursor;
7873 retval = -1;
7874 break;
7875 }
7876 if (r)
7877 continue;
7878 }
7879
7880 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007882 // Found end when searching backwards or start when searching
7883 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007885 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 }
7887 else
7888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007889 // Found end when searching forward or start when searching
7890 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007892 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 }
7894
7895 if (nest == 0)
7896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007897 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 if (flags & SP_RETCOUNT)
7899 ++retval;
7900 else
7901 retval = pos.lnum;
7902 if (flags & SP_SETPCMARK)
7903 setpcmark();
7904 curwin->w_cursor = pos;
7905 if (!(flags & SP_REPEAT))
7906 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007907 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 }
7909 }
7910
7911 if (match_pos != NULL)
7912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007913 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914 match_pos->lnum = curwin->w_cursor.lnum;
7915 match_pos->col = curwin->w_cursor.col + 1;
7916 }
7917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007918 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 if ((flags & SP_NOMOVE) || retval == 0)
7920 curwin->w_cursor = save_cursor;
7921
7922theend:
7923 vim_free(pat2);
7924 vim_free(pat3);
7925 if (p_cpo == empty_option)
7926 p_cpo = save_cpo;
7927 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007929 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007930 // If it's still empty it was changed and restored, need to restore in
7931 // the complicated way.
7932 if (*p_cpo == NUL)
7933 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007935 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007936
7937 return retval;
7938}
7939
7940/*
7941 * "searchpos()" function
7942 */
7943 static void
7944f_searchpos(typval_T *argvars, typval_T *rettv)
7945{
7946 pos_T match_pos;
7947 int lnum = 0;
7948 int col = 0;
7949 int n;
7950 int flags = 0;
7951
7952 if (rettv_list_alloc(rettv) == FAIL)
7953 return;
7954
7955 n = search_cmn(argvars, &match_pos, &flags);
7956 if (n > 0)
7957 {
7958 lnum = match_pos.lnum;
7959 col = match_pos.col;
7960 }
7961
7962 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7963 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7964 if (flags & SP_SUBPAT)
7965 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7966}
7967
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007968/*
7969 * Set the cursor or mark position.
7970 * If 'charpos' is TRUE, then use the column number as a character offet.
7971 * Otherwise use the column number as a byte offset.
7972 */
7973 static void
7974set_position(typval_T *argvars, typval_T *rettv, int charpos)
7975{
7976 pos_T pos;
7977 int fnum;
7978 char_u *name;
7979 colnr_T curswant = -1;
7980
7981 rettv->vval.v_number = -1;
7982
7983 name = tv_get_string_chk(argvars);
7984 if (name != NULL)
7985 {
7986 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7987 {
7988 if (pos.col != MAXCOL && --pos.col < 0)
7989 pos.col = 0;
7990 if ((name[0] == '.' && name[1] == NUL))
7991 {
7992 // set cursor; "fnum" is ignored
7993 curwin->w_cursor = pos;
7994 if (curswant >= 0)
7995 {
7996 curwin->w_curswant = curswant - 1;
7997 curwin->w_set_curswant = FALSE;
7998 }
7999 check_cursor();
8000 rettv->vval.v_number = 0;
8001 }
8002 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8003 {
8004 // set mark
8005 if (setmark_pos(name[1], &pos, fnum) == OK)
8006 rettv->vval.v_number = 0;
8007 }
8008 else
8009 emsg(_(e_invarg));
8010 }
8011 }
8012}
8013/*
8014 * "setcharpos()" function
8015 */
8016 static void
8017f_setcharpos(typval_T *argvars, typval_T *rettv)
8018{
8019 set_position(argvars, rettv, TRUE);
8020}
8021
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8024{
8025 dict_T *d;
8026 dictitem_T *di;
8027 char_u *csearch;
8028
8029 if (argvars[0].v_type != VAR_DICT)
8030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008031 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 return;
8033 }
8034
8035 if ((d = argvars[0].vval.v_dict) != NULL)
8036 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008037 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 if (csearch != NULL)
8039 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 if (enc_utf8)
8041 {
8042 int pcc[MAX_MCO];
8043 int c = utfc_ptr2char(csearch, pcc);
8044
8045 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8046 }
8047 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008049 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008050 }
8051
8052 di = dict_find(d, (char_u *)"forward", -1);
8053 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008054 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008055 ? FORWARD : BACKWARD);
8056
8057 di = dict_find(d, (char_u *)"until", -1);
8058 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008059 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008060 }
8061}
8062
8063/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008064 * "setcursorcharpos" function
8065 */
8066 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008067f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008068{
8069 set_cursorpos(argvars, rettv, TRUE);
8070}
8071
8072/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008073 * "setenv()" function
8074 */
8075 static void
8076f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8077{
8078 char_u namebuf[NUMBUFLEN];
8079 char_u valbuf[NUMBUFLEN];
8080 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8081
8082 if (argvars[1].v_type == VAR_SPECIAL
8083 && argvars[1].vval.v_number == VVAL_NULL)
8084 vim_unsetenv(name);
8085 else
8086 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8087}
8088
8089/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 * "setfperm({fname}, {mode})" function
8091 */
8092 static void
8093f_setfperm(typval_T *argvars, typval_T *rettv)
8094{
8095 char_u *fname;
8096 char_u modebuf[NUMBUFLEN];
8097 char_u *mode_str;
8098 int i;
8099 int mask;
8100 int mode = 0;
8101
8102 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008103 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008104 if (fname == NULL)
8105 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008106 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 if (mode_str == NULL)
8108 return;
8109 if (STRLEN(mode_str) != 9)
8110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008111 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 return;
8113 }
8114
8115 mask = 1;
8116 for (i = 8; i >= 0; --i)
8117 {
8118 if (mode_str[i] != '-')
8119 mode |= mask;
8120 mask = mask << 1;
8121 }
8122 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8123}
8124
8125/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 * "setpos()" function
8127 */
8128 static void
8129f_setpos(typval_T *argvars, typval_T *rettv)
8130{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008131 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132}
8133
8134/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008135 * Translate a register type string to the yank type and block length
8136 */
8137 static int
8138get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8139{
8140 char_u *stropt = *pp;
8141 switch (*stropt)
8142 {
8143 case 'v': case 'c': // character-wise selection
8144 *yank_type = MCHAR;
8145 break;
8146 case 'V': case 'l': // line-wise selection
8147 *yank_type = MLINE;
8148 break;
8149 case 'b': case Ctrl_V: // block-wise selection
8150 *yank_type = MBLOCK;
8151 if (VIM_ISDIGIT(stropt[1]))
8152 {
8153 ++stropt;
8154 *block_len = getdigits(&stropt) - 1;
8155 --stropt;
8156 }
8157 break;
8158 default:
8159 return FAIL;
8160 }
8161 *pp = stropt;
8162 return OK;
8163}
8164
8165/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 * "setreg()" function
8167 */
8168 static void
8169f_setreg(typval_T *argvars, typval_T *rettv)
8170{
8171 int regname;
8172 char_u *strregname;
8173 char_u *stropt;
8174 char_u *strval;
8175 int append;
8176 char_u yank_type;
8177 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008178 typval_T *regcontents;
8179 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008181 pointreg = 0;
8182 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 block_len = -1;
8184 yank_type = MAUTO;
8185 append = FALSE;
8186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008187 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008188 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189
8190 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008191 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008192 if (in_vim9script() && STRLEN(strregname) > 1)
8193 {
8194 semsg(_(e_register_name_must_be_one_char_str), strregname);
8195 return;
8196 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 regname = *strregname;
8198 if (regname == 0 || regname == '@')
8199 regname = '"';
8200
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008201 if (argvars[1].v_type == VAR_DICT)
8202 {
8203 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008204 dictitem_T *di;
8205
8206 if (d == NULL || d->dv_hashtab.ht_used == 0)
8207 {
8208 // Empty dict, clear the register (like setreg(0, []))
8209 char_u *lstval[2] = {NULL, NULL};
8210 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8211 return;
8212 }
8213
8214 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008215 if (di != NULL)
8216 regcontents = &di->di_tv;
8217
8218 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8219 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008220 {
8221 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8222
8223 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008224 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008225 semsg(_(e_invargval), "value");
8226 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008227 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008228 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008229
8230 if (regname == '"')
8231 {
8232 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8233 if (stropt != NULL)
8234 {
8235 pointreg = *stropt;
8236 regname = pointreg;
8237 }
8238 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008239 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008240 pointreg = regname;
8241 }
8242 else
8243 regcontents = &argvars[1];
8244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245 if (argvars[2].v_type != VAR_UNKNOWN)
8246 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008247 if (yank_type != MAUTO)
8248 {
8249 semsg(_(e_toomanyarg), "setreg");
8250 return;
8251 }
8252
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008253 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008254 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008255 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 for (; *stropt != NUL; ++stropt)
8257 switch (*stropt)
8258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008259 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 append = TRUE;
8261 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008262 default:
8263 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 }
8265 }
8266
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008267 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 {
8269 char_u **lstval;
8270 char_u **allocval;
8271 char_u buf[NUMBUFLEN];
8272 char_u **curval;
8273 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008274 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 listitem_T *li;
8276 int len;
8277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008278 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 len = ll == NULL ? 0 : ll->lv_len;
8280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008281 // First half: use for pointers to result lines; second half: use for
8282 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008283 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 if (lstval == NULL)
8285 return;
8286 curval = lstval;
8287 allocval = lstval + len + 2;
8288 curallocval = allocval;
8289
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008290 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008292 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008293 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008295 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 if (strval == NULL)
8297 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008298 if (strval == buf)
8299 {
8300 // Need to make a copy, next tv_get_string_buf_chk() will
8301 // overwrite the string.
8302 strval = vim_strsave(buf);
8303 if (strval == NULL)
8304 goto free_lstval;
8305 *curallocval++ = strval;
8306 }
8307 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 }
8310 *curval++ = NULL;
8311
8312 write_reg_contents_lst(regname, lstval, -1,
8313 append, yank_type, block_len);
8314free_lstval:
8315 while (curallocval > allocval)
8316 vim_free(*--curallocval);
8317 vim_free(lstval);
8318 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008319 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008321 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 if (strval == NULL)
8323 return;
8324 write_reg_contents_ex(regname, strval, -1,
8325 append, yank_type, block_len);
8326 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008327 if (pointreg != 0)
8328 get_yank_register(pointreg, TRUE);
8329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008330 rettv->vval.v_number = 0;
8331}
8332
8333/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008334 * "settagstack()" function
8335 */
8336 static void
8337f_settagstack(typval_T *argvars, typval_T *rettv)
8338{
8339 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8340 win_T *wp;
8341 dict_T *d;
8342 int action = 'r';
8343
8344 rettv->vval.v_number = -1;
8345
8346 // first argument: window number or id
8347 wp = find_win_by_nr_or_id(&argvars[0]);
8348 if (wp == NULL)
8349 return;
8350
8351 // second argument: dict with items to set in the tag stack
8352 if (argvars[1].v_type != VAR_DICT)
8353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008354 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008355 return;
8356 }
8357 d = argvars[1].vval.v_dict;
8358 if (d == NULL)
8359 return;
8360
8361 // third argument: action - 'a' for append and 'r' for replace.
8362 // default is to replace the stack.
8363 if (argvars[2].v_type == VAR_UNKNOWN)
8364 action = 'r';
8365 else if (argvars[2].v_type == VAR_STRING)
8366 {
8367 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008368 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008369 if (actstr == NULL)
8370 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008371 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8372 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008373 action = *actstr;
8374 else
8375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008376 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008377 return;
8378 }
8379 }
8380 else
8381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008383 return;
8384 }
8385
8386 if (set_tagstack(wp, d, action) == OK)
8387 rettv->vval.v_number = 0;
8388}
8389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390#ifdef FEAT_CRYPT
8391/*
8392 * "sha256({string})" function
8393 */
8394 static void
8395f_sha256(typval_T *argvars, typval_T *rettv)
8396{
8397 char_u *p;
8398
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008399 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 rettv->vval.v_string = vim_strsave(
8401 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8402 rettv->v_type = VAR_STRING;
8403}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008404#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405
8406/*
8407 * "shellescape({string})" function
8408 */
8409 static void
8410f_shellescape(typval_T *argvars, typval_T *rettv)
8411{
Bram Moolenaar20615522017-06-05 18:46:26 +02008412 int do_special = non_zero_arg(&argvars[1]);
8413
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008415 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 rettv->v_type = VAR_STRING;
8417}
8418
8419/*
8420 * shiftwidth() function
8421 */
8422 static void
8423f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8424{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008425 rettv->vval.v_number = 0;
8426
8427 if (argvars[0].v_type != VAR_UNKNOWN)
8428 {
8429 long col;
8430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008431 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008432 if (col < 0)
8433 return; // type error; errmsg already given
8434#ifdef FEAT_VARTABS
8435 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8436 return;
8437#endif
8438 }
8439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 rettv->vval.v_number = get_sw_value(curbuf);
8441}
8442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008443/*
8444 * "soundfold({word})" function
8445 */
8446 static void
8447f_soundfold(typval_T *argvars, typval_T *rettv)
8448{
8449 char_u *s;
8450
8451 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008452 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453#ifdef FEAT_SPELL
8454 rettv->vval.v_string = eval_soundfold(s);
8455#else
8456 rettv->vval.v_string = vim_strsave(s);
8457#endif
8458}
8459
8460/*
8461 * "spellbadword()" function
8462 */
8463 static void
8464f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8465{
8466 char_u *word = (char_u *)"";
8467 hlf_T attr = HLF_COUNT;
8468 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008469#ifdef FEAT_SPELL
8470 int wo_spell_save = curwin->w_p_spell;
8471
8472 if (!curwin->w_p_spell)
8473 {
8474 did_set_spelllang(curwin);
8475 curwin->w_p_spell = TRUE;
8476 }
8477
8478 if (*curwin->w_s->b_p_spl == NUL)
8479 {
8480 emsg(_(e_no_spell));
8481 curwin->w_p_spell = wo_spell_save;
8482 return;
8483 }
8484#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485
8486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008487 {
8488#ifdef FEAT_SPELL
8489 curwin->w_p_spell = wo_spell_save;
8490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008492 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493
8494#ifdef FEAT_SPELL
8495 if (argvars[0].v_type == VAR_UNKNOWN)
8496 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008497 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008498 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8499 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008500 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008502 curwin->w_set_curswant = TRUE;
8503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008505 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008507 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 int capcol = -1;
8509
8510 if (str != NULL)
8511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008512 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008513 while (*str != NUL)
8514 {
8515 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8516 if (attr != HLF_COUNT)
8517 {
8518 word = str;
8519 break;
8520 }
8521 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008522 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008523 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524 }
8525 }
8526 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008527 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528#endif
8529
8530 list_append_string(rettv->vval.v_list, word, len);
8531 list_append_string(rettv->vval.v_list, (char_u *)(
8532 attr == HLF_SPB ? "bad" :
8533 attr == HLF_SPR ? "rare" :
8534 attr == HLF_SPL ? "local" :
8535 attr == HLF_SPC ? "caps" :
8536 ""), -1);
8537}
8538
8539/*
8540 * "spellsuggest()" function
8541 */
8542 static void
8543f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8544{
8545#ifdef FEAT_SPELL
8546 char_u *str;
8547 int typeerr = FALSE;
8548 int maxcount;
8549 garray_T ga;
8550 int i;
8551 listitem_T *li;
8552 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008553 int wo_spell_save = curwin->w_p_spell;
8554
8555 if (!curwin->w_p_spell)
8556 {
8557 did_set_spelllang(curwin);
8558 curwin->w_p_spell = TRUE;
8559 }
8560
8561 if (*curwin->w_s->b_p_spl == NUL)
8562 {
8563 emsg(_(e_no_spell));
8564 curwin->w_p_spell = wo_spell_save;
8565 return;
8566 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008567#endif
8568
8569 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008570 {
8571#ifdef FEAT_SPELL
8572 curwin->w_p_spell = wo_spell_save;
8573#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576
8577#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008578 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 if (argvars[1].v_type != VAR_UNKNOWN)
8582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008583 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 if (maxcount <= 0)
8585 return;
8586 if (argvars[2].v_type != VAR_UNKNOWN)
8587 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008588 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 if (typeerr)
8590 return;
8591 }
8592 }
8593 else
8594 maxcount = 25;
8595
8596 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8597
8598 for (i = 0; i < ga.ga_len; ++i)
8599 {
8600 str = ((char_u **)ga.ga_data)[i];
8601
8602 li = listitem_alloc();
8603 if (li == NULL)
8604 vim_free(str);
8605 else
8606 {
8607 li->li_tv.v_type = VAR_STRING;
8608 li->li_tv.v_lock = 0;
8609 li->li_tv.vval.v_string = str;
8610 list_append(rettv->vval.v_list, li);
8611 }
8612 }
8613 ga_clear(&ga);
8614 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008615 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616#endif
8617}
8618
8619 static void
8620f_split(typval_T *argvars, typval_T *rettv)
8621{
8622 char_u *str;
8623 char_u *end;
8624 char_u *pat = NULL;
8625 regmatch_T regmatch;
8626 char_u patbuf[NUMBUFLEN];
8627 char_u *save_cpo;
8628 int match;
8629 colnr_T col = 0;
8630 int keepempty = FALSE;
8631 int typeerr = FALSE;
8632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008633 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008635 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 if (argvars[1].v_type != VAR_UNKNOWN)
8639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 if (pat == NULL)
8642 typeerr = TRUE;
8643 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008644 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 }
8646 if (pat == NULL || *pat == NUL)
8647 pat = (char_u *)"[\\x01- ]\\+";
8648
8649 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008650 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008652 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653
8654 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8655 if (regmatch.regprog != NULL)
8656 {
8657 regmatch.rm_ic = FALSE;
8658 while (*str != NUL || keepempty)
8659 {
8660 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008661 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662 else
8663 match = vim_regexec_nl(&regmatch, str, col);
8664 if (match)
8665 end = regmatch.startp[0];
8666 else
8667 end = str + STRLEN(str);
8668 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8669 && *str != NUL && match && end < regmatch.endp[0]))
8670 {
8671 if (list_append_string(rettv->vval.v_list, str,
8672 (int)(end - str)) == FAIL)
8673 break;
8674 }
8675 if (!match)
8676 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008677 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678 if (regmatch.endp[0] > str)
8679 col = 0;
8680 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008681 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 str = regmatch.endp[0];
8684 }
8685
8686 vim_regfree(regmatch.regprog);
8687 }
8688
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008689theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 p_cpo = save_cpo;
8691}
8692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008694 * "str2list()" function
8695 */
8696 static void
8697f_str2list(typval_T *argvars, typval_T *rettv)
8698{
8699 char_u *p;
8700 int utf8 = FALSE;
8701
8702 if (rettv_list_alloc(rettv) == FAIL)
8703 return;
8704
8705 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008706 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008707
8708 p = tv_get_string(&argvars[0]);
8709
8710 if (has_mbyte || utf8)
8711 {
8712 int (*ptr2len)(char_u *);
8713 int (*ptr2char)(char_u *);
8714
8715 if (utf8 || enc_utf8)
8716 {
8717 ptr2len = utf_ptr2len;
8718 ptr2char = utf_ptr2char;
8719 }
8720 else
8721 {
8722 ptr2len = mb_ptr2len;
8723 ptr2char = mb_ptr2char;
8724 }
8725
8726 for ( ; *p != NUL; p += (*ptr2len)(p))
8727 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8728 }
8729 else
8730 for ( ; *p != NUL; ++p)
8731 list_append_number(rettv->vval.v_list, *p);
8732}
8733
8734/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 * "str2nr()" function
8736 */
8737 static void
8738f_str2nr(typval_T *argvars, typval_T *rettv)
8739{
8740 int base = 10;
8741 char_u *p;
8742 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008743 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008744 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745
8746 if (argvars[1].v_type != VAR_UNKNOWN)
8747 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008748 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 if (base != 2 && base != 8 && base != 10 && base != 16)
8750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008751 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 return;
8753 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008754 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008755 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 }
8757
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008758 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008759 isneg = (*p == '-');
8760 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 p = skipwhite(p + 1);
8762 switch (base)
8763 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008764 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008765 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008766 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008768 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8769 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008770 if (isneg)
8771 rettv->vval.v_number = -n;
8772 else
8773 rettv->vval.v_number = n;
8774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775}
8776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777/*
8778 * "strgetchar()" function
8779 */
8780 static void
8781f_strgetchar(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u *str;
8784 int len;
8785 int error = FALSE;
8786 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008787 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788
8789 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008790 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 if (str == NULL)
8792 return;
8793 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008794 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 if (error)
8796 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797
Bram Moolenaar13505972019-01-24 15:04:48 +01008798 while (charidx >= 0 && byteidx < len)
8799 {
8800 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008802 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8803 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008805 --charidx;
8806 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808}
8809
8810/*
8811 * "stridx()" function
8812 */
8813 static void
8814f_stridx(typval_T *argvars, typval_T *rettv)
8815{
8816 char_u buf[NUMBUFLEN];
8817 char_u *needle;
8818 char_u *haystack;
8819 char_u *save_haystack;
8820 char_u *pos;
8821 int start_idx;
8822
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008823 needle = tv_get_string_chk(&argvars[1]);
8824 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 rettv->vval.v_number = -1;
8826 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008827 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828
8829 if (argvars[2].v_type != VAR_UNKNOWN)
8830 {
8831 int error = FALSE;
8832
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008833 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 if (error || start_idx >= (int)STRLEN(haystack))
8835 return;
8836 if (start_idx >= 0)
8837 haystack += start_idx;
8838 }
8839
8840 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8841 if (pos != NULL)
8842 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8843}
8844
8845/*
8846 * "string()" function
8847 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008848 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849f_string(typval_T *argvars, typval_T *rettv)
8850{
8851 char_u *tofree;
8852 char_u numbuf[NUMBUFLEN];
8853
8854 rettv->v_type = VAR_STRING;
8855 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8856 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008857 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 if (rettv->vval.v_string != NULL && tofree == NULL)
8859 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8860}
8861
8862/*
8863 * "strlen()" function
8864 */
8865 static void
8866f_strlen(typval_T *argvars, typval_T *rettv)
8867{
8868 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008869 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870}
8871
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008872 static void
8873strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8874{
8875 char_u *s = tv_get_string(&argvars[0]);
8876 varnumber_T len = 0;
8877 int (*func_mb_ptr2char_adv)(char_u **pp);
8878
8879 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8880 while (*s != NUL)
8881 {
8882 func_mb_ptr2char_adv(&s);
8883 ++len;
8884 }
8885 rettv->vval.v_number = len;
8886}
8887
8888/*
8889 * "strcharlen()" function
8890 */
8891 static void
8892f_strcharlen(typval_T *argvars, typval_T *rettv)
8893{
8894 strchar_common(argvars, rettv, TRUE);
8895}
8896
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897/*
8898 * "strchars()" function
8899 */
8900 static void
8901f_strchars(typval_T *argvars, typval_T *rettv)
8902{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008903 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904
8905 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008906 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008908 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008910 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911}
8912
8913/*
8914 * "strdisplaywidth()" function
8915 */
8916 static void
8917f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8918{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008919 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008920 int col = 0;
8921
8922 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924
8925 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8926}
8927
8928/*
8929 * "strwidth()" function
8930 */
8931 static void
8932f_strwidth(typval_T *argvars, typval_T *rettv)
8933{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008934 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935
Bram Moolenaar13505972019-01-24 15:04:48 +01008936 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937}
8938
8939/*
8940 * "strcharpart()" function
8941 */
8942 static void
8943f_strcharpart(typval_T *argvars, typval_T *rettv)
8944{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 char_u *p;
8946 int nchar;
8947 int nbyte = 0;
8948 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008949 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 int len = 0;
8951 int slen;
8952 int error = FALSE;
8953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008954 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008955 slen = (int)STRLEN(p);
8956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008957 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008958 if (!error)
8959 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008960 if (argvars[2].v_type != VAR_UNKNOWN
8961 && argvars[3].v_type != VAR_UNKNOWN)
8962 {
8963 skipcc = tv_get_bool(&argvars[3]);
8964 if (skipcc < 0 || skipcc > 1)
8965 {
8966 semsg(_(e_using_number_as_bool_nr), skipcc);
8967 return;
8968 }
8969 }
8970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 if (nchar > 0)
8972 while (nchar > 0 && nbyte < slen)
8973 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008974 if (skipcc)
8975 nbyte += mb_ptr2len(p + nbyte);
8976 else
8977 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978 --nchar;
8979 }
8980 else
8981 nbyte = nchar;
8982 if (argvars[2].v_type != VAR_UNKNOWN)
8983 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008984 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 while (charlen > 0 && nbyte + len < slen)
8986 {
8987 int off = nbyte + len;
8988
8989 if (off < 0)
8990 len += 1;
8991 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008992 {
8993 if (skipcc)
8994 len += mb_ptr2len(p + off);
8995 else
8996 len += MB_CPTR2LEN(p + off);
8997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 --charlen;
8999 }
9000 }
9001 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009002 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 }
9004
9005 /*
9006 * Only return the overlap between the specified part and the actual
9007 * string.
9008 */
9009 if (nbyte < 0)
9010 {
9011 len += nbyte;
9012 nbyte = 0;
9013 }
9014 else if (nbyte > slen)
9015 nbyte = slen;
9016 if (len < 0)
9017 len = 0;
9018 else if (nbyte + len > slen)
9019 len = slen - nbyte;
9020
9021 rettv->v_type = VAR_STRING;
9022 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023}
9024
9025/*
9026 * "strpart()" function
9027 */
9028 static void
9029f_strpart(typval_T *argvars, typval_T *rettv)
9030{
9031 char_u *p;
9032 int n;
9033 int len;
9034 int slen;
9035 int error = FALSE;
9036
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009037 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 slen = (int)STRLEN(p);
9039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009040 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 if (error)
9042 len = 0;
9043 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009044 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009046 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009048 // Only return the overlap between the specified part and the actual
9049 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050 if (n < 0)
9051 {
9052 len += n;
9053 n = 0;
9054 }
9055 else if (n > slen)
9056 n = slen;
9057 if (len < 0)
9058 len = 0;
9059 else if (n + len > slen)
9060 len = slen - n;
9061
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009062 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9063 {
9064 int off;
9065
9066 // length in characters
9067 for (off = n; off < slen && len > 0; --len)
9068 off += mb_ptr2len(p + off);
9069 len = off - n;
9070 }
9071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 rettv->v_type = VAR_STRING;
9073 rettv->vval.v_string = vim_strnsave(p + n, len);
9074}
9075
9076/*
9077 * "strridx()" function
9078 */
9079 static void
9080f_strridx(typval_T *argvars, typval_T *rettv)
9081{
9082 char_u buf[NUMBUFLEN];
9083 char_u *needle;
9084 char_u *haystack;
9085 char_u *rest;
9086 char_u *lastmatch = NULL;
9087 int haystack_len, end_idx;
9088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009089 needle = tv_get_string_chk(&argvars[1]);
9090 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009091
9092 rettv->vval.v_number = -1;
9093 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009094 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095
9096 haystack_len = (int)STRLEN(haystack);
9097 if (argvars[2].v_type != VAR_UNKNOWN)
9098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009099 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009100 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009102 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 }
9104 else
9105 end_idx = haystack_len;
9106
9107 if (*needle == NUL)
9108 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009109 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 lastmatch = haystack + end_idx;
9111 }
9112 else
9113 {
9114 for (rest = haystack; *rest != '\0'; ++rest)
9115 {
9116 rest = (char_u *)strstr((char *)rest, (char *)needle);
9117 if (rest == NULL || rest > haystack + end_idx)
9118 break;
9119 lastmatch = rest;
9120 }
9121 }
9122
9123 if (lastmatch == NULL)
9124 rettv->vval.v_number = -1;
9125 else
9126 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9127}
9128
9129/*
9130 * "strtrans()" function
9131 */
9132 static void
9133f_strtrans(typval_T *argvars, typval_T *rettv)
9134{
9135 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009136 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137}
9138
9139/*
9140 * "submatch()" function
9141 */
9142 static void
9143f_submatch(typval_T *argvars, typval_T *rettv)
9144{
9145 int error = FALSE;
9146 int no;
9147 int retList = 0;
9148
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009149 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009150 if (error)
9151 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009152 if (no < 0 || no >= NSUBEXP)
9153 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009154 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009155 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009156 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009158 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159 if (error)
9160 return;
9161
9162 if (retList == 0)
9163 {
9164 rettv->v_type = VAR_STRING;
9165 rettv->vval.v_string = reg_submatch(no);
9166 }
9167 else
9168 {
9169 rettv->v_type = VAR_LIST;
9170 rettv->vval.v_list = reg_submatch_list(no);
9171 }
9172}
9173
9174/*
9175 * "substitute()" function
9176 */
9177 static void
9178f_substitute(typval_T *argvars, typval_T *rettv)
9179{
9180 char_u patbuf[NUMBUFLEN];
9181 char_u subbuf[NUMBUFLEN];
9182 char_u flagsbuf[NUMBUFLEN];
9183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009184 char_u *str = tv_get_string_chk(&argvars[0]);
9185 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009186 char_u *sub = NULL;
9187 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009188 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009190 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9191 expr = &argvars[2];
9192 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009193 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009194
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009196 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9197 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 rettv->vval.v_string = NULL;
9199 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009200 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201}
9202
9203/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009204 * "swapinfo(swap_filename)" function
9205 */
9206 static void
9207f_swapinfo(typval_T *argvars, typval_T *rettv)
9208{
9209 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009210 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009211}
9212
9213/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009214 * "swapname(expr)" function
9215 */
9216 static void
9217f_swapname(typval_T *argvars, typval_T *rettv)
9218{
9219 buf_T *buf;
9220
9221 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009222 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009223 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9224 || buf->b_ml.ml_mfp->mf_fname == NULL)
9225 rettv->vval.v_string = NULL;
9226 else
9227 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9228}
9229
9230/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 * "synID(lnum, col, trans)" function
9232 */
9233 static void
9234f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9235{
9236 int id = 0;
9237#ifdef FEAT_SYN_HL
9238 linenr_T lnum;
9239 colnr_T col;
9240 int trans;
9241 int transerr = FALSE;
9242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009243 lnum = tv_get_lnum(argvars); // -1 on type error
9244 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009245 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009246
9247 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9248 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9249 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9250#endif
9251
9252 rettv->vval.v_number = id;
9253}
9254
9255/*
9256 * "synIDattr(id, what [, mode])" function
9257 */
9258 static void
9259f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9260{
9261 char_u *p = NULL;
9262#ifdef FEAT_SYN_HL
9263 int id;
9264 char_u *what;
9265 char_u *mode;
9266 char_u modebuf[NUMBUFLEN];
9267 int modec;
9268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009269 id = (int)tv_get_number(&argvars[0]);
9270 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271 if (argvars[2].v_type != VAR_UNKNOWN)
9272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009273 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274 modec = TOLOWER_ASC(mode[0]);
9275 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009276 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 }
9278 else
9279 {
9280#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9281 if (USE_24BIT)
9282 modec = 'g';
9283 else
9284#endif
9285 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009286 modec = 'c';
9287 else
9288 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 }
9290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009291 switch (TOLOWER_ASC(what[0]))
9292 {
9293 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009294 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009296 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297 p = highlight_has_attr(id, HL_BOLD, modec);
9298 break;
9299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009300 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 p = highlight_color(id, what, modec);
9302 break;
9303
9304 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009305 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009307 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308 p = highlight_has_attr(id, HL_ITALIC, modec);
9309 break;
9310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009311 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009312 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 break;
9314
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009315 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316 p = highlight_has_attr(id, HL_INVERSE, modec);
9317 break;
9318
9319 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009320 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009322 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009323 else if (TOLOWER_ASC(what[1]) == 't' &&
9324 TOLOWER_ASC(what[2]) == 'r')
9325 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009326 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 p = highlight_has_attr(id, HL_STANDOUT, modec);
9328 break;
9329
9330 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009331 if (TOLOWER_ASC(what[1]) == 'l') // ul
9332 p = highlight_color(id, what, modec);
9333 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009334 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9336 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009337 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9339 break;
9340 }
9341
9342 if (p != NULL)
9343 p = vim_strsave(p);
9344#endif
9345 rettv->v_type = VAR_STRING;
9346 rettv->vval.v_string = p;
9347}
9348
9349/*
9350 * "synIDtrans(id)" function
9351 */
9352 static void
9353f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9354{
9355 int id;
9356
9357#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009358 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359
9360 if (id > 0)
9361 id = syn_get_final_id(id);
9362 else
9363#endif
9364 id = 0;
9365
9366 rettv->vval.v_number = id;
9367}
9368
9369/*
9370 * "synconcealed(lnum, col)" function
9371 */
9372 static void
9373f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9374{
9375#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9376 linenr_T lnum;
9377 colnr_T col;
9378 int syntax_flags = 0;
9379 int cchar;
9380 int matchid = 0;
9381 char_u str[NUMBUFLEN];
9382#endif
9383
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009384 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385
9386#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009387 lnum = tv_get_lnum(argvars); // -1 on type error
9388 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389
Bram Moolenaara80faa82020-04-12 19:37:17 +02009390 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391
9392 if (rettv_list_alloc(rettv) != FAIL)
9393 {
9394 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9395 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9396 && curwin->w_p_cole > 0)
9397 {
9398 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9399 syntax_flags = get_syntax_info(&matchid);
9400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009401 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9403 {
9404 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009405 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009406 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9407 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 if (cchar != NUL)
9409 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 if (has_mbyte)
9411 (*mb_char2bytes)(cchar, str);
9412 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 str[0] = cchar;
9414 }
9415 }
9416 }
9417
9418 list_append_number(rettv->vval.v_list,
9419 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009420 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 list_append_string(rettv->vval.v_list, str, -1);
9422 list_append_number(rettv->vval.v_list, matchid);
9423 }
9424#endif
9425}
9426
9427/*
9428 * "synstack(lnum, col)" function
9429 */
9430 static void
9431f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9432{
9433#ifdef FEAT_SYN_HL
9434 linenr_T lnum;
9435 colnr_T col;
9436 int i;
9437 int id;
9438#endif
9439
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009440 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441
9442#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009443 lnum = tv_get_lnum(argvars); // -1 on type error
9444 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445
9446 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9447 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9448 && rettv_list_alloc(rettv) != FAIL)
9449 {
9450 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9451 for (i = 0; ; ++i)
9452 {
9453 id = syn_get_stack_item(i);
9454 if (id < 0)
9455 break;
9456 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9457 break;
9458 }
9459 }
9460#endif
9461}
9462
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463/*
9464 * "tabpagebuflist()" function
9465 */
9466 static void
9467f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9468{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 tabpage_T *tp;
9470 win_T *wp = NULL;
9471
9472 if (argvars[0].v_type == VAR_UNKNOWN)
9473 wp = firstwin;
9474 else
9475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009476 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 if (tp != NULL)
9478 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9479 }
9480 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9481 {
9482 for (; wp != NULL; wp = wp->w_next)
9483 if (list_append_number(rettv->vval.v_list,
9484 wp->w_buffer->b_fnum) == FAIL)
9485 break;
9486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487}
9488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 * "tagfiles()" function
9491 */
9492 static void
9493f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9494{
9495 char_u *fname;
9496 tagname_T tn;
9497 int first;
9498
9499 if (rettv_list_alloc(rettv) == FAIL)
9500 return;
9501 fname = alloc(MAXPATHL);
9502 if (fname == NULL)
9503 return;
9504
9505 for (first = TRUE; ; first = FALSE)
9506 if (get_tagfname(&tn, first, fname) == FAIL
9507 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9508 break;
9509 tagname_free(&tn);
9510 vim_free(fname);
9511}
9512
9513/*
9514 * "taglist()" function
9515 */
9516 static void
9517f_taglist(typval_T *argvars, typval_T *rettv)
9518{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009519 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520 char_u *tag_pattern;
9521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009522 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009523
9524 rettv->vval.v_number = FALSE;
9525 if (*tag_pattern == NUL)
9526 return;
9527
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009528 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009529 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009531 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532}
9533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009535 * "tolower(string)" function
9536 */
9537 static void
9538f_tolower(typval_T *argvars, typval_T *rettv)
9539{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009541 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542}
9543
9544/*
9545 * "toupper(string)" function
9546 */
9547 static void
9548f_toupper(typval_T *argvars, typval_T *rettv)
9549{
9550 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009551 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552}
9553
9554/*
9555 * "tr(string, fromstr, tostr)" function
9556 */
9557 static void
9558f_tr(typval_T *argvars, typval_T *rettv)
9559{
9560 char_u *in_str;
9561 char_u *fromstr;
9562 char_u *tostr;
9563 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 int inlen;
9565 int fromlen;
9566 int tolen;
9567 int idx;
9568 char_u *cpstr;
9569 int cplen;
9570 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 char_u buf[NUMBUFLEN];
9572 char_u buf2[NUMBUFLEN];
9573 garray_T ga;
9574
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009575 in_str = tv_get_string(&argvars[0]);
9576 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9577 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009579 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009580 rettv->v_type = VAR_STRING;
9581 rettv->vval.v_string = NULL;
9582 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009583 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 ga_init2(&ga, (int)sizeof(char), 80);
9585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009587 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 if (STRLEN(fromstr) != STRLEN(tostr))
9589 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009591 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 ga_clear(&ga);
9593 return;
9594 }
9595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009596 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 while (*in_str != NUL)
9598 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 if (has_mbyte)
9600 {
9601 inlen = (*mb_ptr2len)(in_str);
9602 cpstr = in_str;
9603 cplen = inlen;
9604 idx = 0;
9605 for (p = fromstr; *p != NUL; p += fromlen)
9606 {
9607 fromlen = (*mb_ptr2len)(p);
9608 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9609 {
9610 for (p = tostr; *p != NUL; p += tolen)
9611 {
9612 tolen = (*mb_ptr2len)(p);
9613 if (idx-- == 0)
9614 {
9615 cplen = tolen;
9616 cpstr = p;
9617 break;
9618 }
9619 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009620 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 goto error;
9622 break;
9623 }
9624 ++idx;
9625 }
9626
9627 if (first && cpstr == in_str)
9628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009629 // Check that fromstr and tostr have the same number of
9630 // (multi-byte) characters. Done only once when a character
9631 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632 first = FALSE;
9633 for (p = tostr; *p != NUL; p += tolen)
9634 {
9635 tolen = (*mb_ptr2len)(p);
9636 --idx;
9637 }
9638 if (idx != 0)
9639 goto error;
9640 }
9641
9642 (void)ga_grow(&ga, cplen);
9643 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9644 ga.ga_len += cplen;
9645
9646 in_str += inlen;
9647 }
9648 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009650 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009651 p = vim_strchr(fromstr, *in_str);
9652 if (p != NULL)
9653 ga_append(&ga, tostr[p - fromstr]);
9654 else
9655 ga_append(&ga, *in_str);
9656 ++in_str;
9657 }
9658 }
9659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009660 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 (void)ga_grow(&ga, 1);
9662 ga_append(&ga, NUL);
9663
9664 rettv->vval.v_string = ga.ga_data;
9665}
9666
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009667/*
9668 * "trim({expr})" function
9669 */
9670 static void
9671f_trim(typval_T *argvars, typval_T *rettv)
9672{
9673 char_u buf1[NUMBUFLEN];
9674 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009675 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009676 char_u *mask = NULL;
9677 char_u *tail;
9678 char_u *prev;
9679 char_u *p;
9680 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009681 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009682
9683 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009684 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009685 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009686 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009687
9688 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009690 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009691
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009693 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009694 int error = 0;
9695
9696 // leading or trailing characters to trim
9697 dir = (int)tv_get_number_chk(&argvars[2], &error);
9698 if (error)
9699 return;
9700 if (dir < 0 || dir > 2)
9701 {
9702 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9703 return;
9704 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009705 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009706 }
9707
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009708 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009709 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009710 // Trim leading characters
9711 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009712 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009713 c1 = PTR2CHAR(head);
9714 if (mask == NULL)
9715 {
9716 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009717 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009718 }
9719 else
9720 {
9721 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9722 if (c1 == PTR2CHAR(p))
9723 break;
9724 if (*p == NUL)
9725 break;
9726 }
9727 MB_PTR_ADV(head);
9728 }
9729 }
9730
9731 tail = head + STRLEN(head);
9732 if (dir == 0 || dir == 2)
9733 {
9734 // Trim trailing characters
9735 for (; tail > head; tail = prev)
9736 {
9737 prev = tail;
9738 MB_PTR_BACK(head, prev);
9739 c1 = PTR2CHAR(prev);
9740 if (mask == NULL)
9741 {
9742 if (c1 > ' ' && c1 != 0xa0)
9743 break;
9744 }
9745 else
9746 {
9747 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9748 if (c1 == PTR2CHAR(p))
9749 break;
9750 if (*p == NUL)
9751 break;
9752 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009753 }
9754 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009755 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009756}
9757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758/*
9759 * "type(expr)" function
9760 */
9761 static void
9762f_type(typval_T *argvars, typval_T *rettv)
9763{
9764 int n = -1;
9765
9766 switch (argvars[0].v_type)
9767 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009768 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9769 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009771 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9772 case VAR_LIST: n = VAR_TYPE_LIST; break;
9773 case VAR_DICT: n = VAR_TYPE_DICT; break;
9774 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9775 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9776 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009777 case VAR_JOB: n = VAR_TYPE_JOB; break;
9778 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009779 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009780 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009782 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009783 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009784 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 n = -1;
9786 break;
9787 }
9788 rettv->vval.v_number = n;
9789}
9790
9791/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 * "virtcol(string)" function
9793 */
9794 static void
9795f_virtcol(typval_T *argvars, typval_T *rettv)
9796{
9797 colnr_T vcol = 0;
9798 pos_T *fp;
9799 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009800 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009802 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9804 && fnum == curbuf->b_fnum)
9805 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009806 // Limit the column to a valid value, getvvcol() doesn't check.
9807 if (fp->col < 0)
9808 fp->col = 0;
9809 else
9810 {
9811 len = (int)STRLEN(ml_get(fp->lnum));
9812 if (fp->col > len)
9813 fp->col = len;
9814 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815 getvvcol(curwin, fp, NULL, NULL, &vcol);
9816 ++vcol;
9817 }
9818
9819 rettv->vval.v_number = vcol;
9820}
9821
9822/*
9823 * "visualmode()" function
9824 */
9825 static void
9826f_visualmode(typval_T *argvars, typval_T *rettv)
9827{
9828 char_u str[2];
9829
9830 rettv->v_type = VAR_STRING;
9831 str[0] = curbuf->b_visual_mode_eval;
9832 str[1] = NUL;
9833 rettv->vval.v_string = vim_strsave(str);
9834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009835 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009836 if (non_zero_arg(&argvars[0]))
9837 curbuf->b_visual_mode_eval = NUL;
9838}
9839
9840/*
9841 * "wildmenumode()" function
9842 */
9843 static void
9844f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9845{
9846#ifdef FEAT_WILDMENU
9847 if (wild_menu_showing)
9848 rettv->vval.v_number = 1;
9849#endif
9850}
9851
9852/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009853 * "windowsversion()" function
9854 */
9855 static void
9856f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9857{
9858 rettv->v_type = VAR_STRING;
9859 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9860}
9861
9862/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863 * "wordcount()" function
9864 */
9865 static void
9866f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9867{
9868 if (rettv_dict_alloc(rettv) == FAIL)
9869 return;
9870 cursor_pos_info(rettv->vval.v_dict);
9871}
9872
9873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874 * "xor(expr, expr)" function
9875 */
9876 static void
9877f_xor(typval_T *argvars, typval_T *rettv)
9878{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9880 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009881}
9882
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009883#endif // FEAT_EVAL