blob: fdcc00fe87cfdae561f49ffd778a20de2194190d [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},
1699 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1700 ret_void, f_test_ignore_error},
1701 {"test_null_blob", 0, 0, 0, NULL,
1702 ret_blob, f_test_null_blob},
1703 {"test_null_channel", 0, 0, 0, NULL,
1704 ret_channel, JOB_FUNC(f_test_null_channel)},
1705 {"test_null_dict", 0, 0, 0, NULL,
1706 ret_dict_any, f_test_null_dict},
1707 {"test_null_function", 0, 0, 0, NULL,
1708 ret_func_any, f_test_null_function},
1709 {"test_null_job", 0, 0, 0, NULL,
1710 ret_job, JOB_FUNC(f_test_null_job)},
1711 {"test_null_list", 0, 0, 0, NULL,
1712 ret_list_any, f_test_null_list},
1713 {"test_null_partial", 0, 0, 0, NULL,
1714 ret_func_any, f_test_null_partial},
1715 {"test_null_string", 0, 0, 0, NULL,
1716 ret_string, f_test_null_string},
1717 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1718 ret_void, f_test_option_not_set},
1719 {"test_override", 2, 2, FEARG_2, NULL,
1720 ret_void, f_test_override},
1721 {"test_refcount", 1, 1, FEARG_1, NULL,
1722 ret_number, f_test_refcount},
1723 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1724 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001725#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001726 f_test_scrollbar
1727#else
1728 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001729#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001730 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001731 {"test_setmouse", 2, 2, 0, NULL,
1732 ret_void, f_test_setmouse},
1733 {"test_settime", 1, 1, FEARG_1, NULL,
1734 ret_void, f_test_settime},
1735 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1736 ret_void, f_test_srand_seed},
1737 {"test_unknown", 0, 0, 0, NULL,
1738 ret_any, f_test_unknown},
1739 {"test_void", 0, 0, 0, NULL,
1740 ret_void, f_test_void},
1741 {"timer_info", 0, 1, FEARG_1, NULL,
1742 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1743 {"timer_pause", 2, 2, FEARG_1, NULL,
1744 ret_void, TIMER_FUNC(f_timer_pause)},
1745 {"timer_start", 2, 3, FEARG_1, NULL,
1746 ret_number, TIMER_FUNC(f_timer_start)},
1747 {"timer_stop", 1, 1, FEARG_1, NULL,
1748 ret_void, TIMER_FUNC(f_timer_stop)},
1749 {"timer_stopall", 0, 0, 0, NULL,
1750 ret_void, TIMER_FUNC(f_timer_stopall)},
1751 {"tolower", 1, 1, FEARG_1, NULL,
1752 ret_string, f_tolower},
1753 {"toupper", 1, 1, FEARG_1, NULL,
1754 ret_string, f_toupper},
1755 {"tr", 3, 3, FEARG_1, NULL,
1756 ret_string, f_tr},
1757 {"trim", 1, 3, FEARG_1, NULL,
1758 ret_string, f_trim},
1759 {"trunc", 1, 1, FEARG_1, NULL,
1760 ret_float, FLOAT_FUNC(f_trunc)},
1761 {"type", 1, 1, FEARG_1, NULL,
1762 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001763 {"typename", 1, 1, FEARG_1, NULL,
1764 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001765 {"undofile", 1, 1, FEARG_1, NULL,
1766 ret_string, f_undofile},
1767 {"undotree", 0, 0, 0, NULL,
1768 ret_dict_any, f_undotree},
1769 {"uniq", 1, 3, FEARG_1, NULL,
1770 ret_list_any, f_uniq},
1771 {"values", 1, 1, FEARG_1, NULL,
1772 ret_list_any, f_values},
1773 {"virtcol", 1, 1, FEARG_1, NULL,
1774 ret_number, f_virtcol},
1775 {"visualmode", 0, 1, 0, NULL,
1776 ret_string, f_visualmode},
1777 {"wildmenumode", 0, 0, 0, NULL,
1778 ret_number, f_wildmenumode},
1779 {"win_execute", 2, 3, FEARG_2, NULL,
1780 ret_string, f_win_execute},
1781 {"win_findbuf", 1, 1, FEARG_1, NULL,
1782 ret_list_number, f_win_findbuf},
1783 {"win_getid", 0, 2, FEARG_1, NULL,
1784 ret_number, f_win_getid},
1785 {"win_gettype", 0, 1, FEARG_1, NULL,
1786 ret_string, f_win_gettype},
1787 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001788 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001789 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1790 ret_list_number, f_win_id2tabwin},
1791 {"win_id2win", 1, 1, FEARG_1, NULL,
1792 ret_number, f_win_id2win},
1793 {"win_screenpos", 1, 1, FEARG_1, NULL,
1794 ret_list_number, f_win_screenpos},
1795 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001796 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001797 {"winbufnr", 1, 1, FEARG_1, NULL,
1798 ret_number, f_winbufnr},
1799 {"wincol", 0, 0, 0, NULL,
1800 ret_number, f_wincol},
1801 {"windowsversion", 0, 0, 0, NULL,
1802 ret_string, f_windowsversion},
1803 {"winheight", 1, 1, FEARG_1, NULL,
1804 ret_number, f_winheight},
1805 {"winlayout", 0, 1, FEARG_1, NULL,
1806 ret_list_any, f_winlayout},
1807 {"winline", 0, 0, 0, NULL,
1808 ret_number, f_winline},
1809 {"winnr", 0, 1, FEARG_1, NULL,
1810 ret_number, f_winnr},
1811 {"winrestcmd", 0, 0, 0, NULL,
1812 ret_string, f_winrestcmd},
1813 {"winrestview", 1, 1, FEARG_1, NULL,
1814 ret_void, f_winrestview},
1815 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001816 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001817 {"winwidth", 1, 1, FEARG_1, NULL,
1818 ret_number, f_winwidth},
1819 {"wordcount", 0, 0, 0, NULL,
1820 ret_dict_number, f_wordcount},
1821 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001822 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001823 {"xor", 2, 2, FEARG_1, NULL,
1824 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001825};
1826
K.Takataeecf2b32021-06-02 14:56:39 +02001827#if defined(EBCDIC) || defined(PROTO)
1828/*
1829 * Compare funcentry_T by function name.
1830 */
1831 static int
1832compare_func_name(const void *s1, const void *s2)
1833{
1834 funcentry_T *p1 = (funcentry_T *)s1;
1835 funcentry_T *p2 = (funcentry_T *)s2;
1836
1837 return STRCMP(p1->f_name, p2->f_name);
1838}
1839
1840/*
1841 * Sort the function table by function name.
1842 * The sorting of the table above is ASCII dependent.
1843 * On machines using EBCDIC we have to sort it.
1844 */
1845 void
1846sortFunctions(void)
1847{
1848 size_t funcCnt = ARRAY_LENGTH(global_functions);
1849
1850 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1851}
1852#endif
1853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854/*
1855 * Function given to ExpandGeneric() to obtain the list of internal
1856 * or user defined function names.
1857 */
1858 char_u *
1859get_function_name(expand_T *xp, int idx)
1860{
1861 static int intidx = -1;
1862 char_u *name;
1863
1864 if (idx == 0)
1865 intidx = -1;
1866 if (intidx < 0)
1867 {
1868 name = get_user_func_name(xp, idx);
1869 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001870 {
1871 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1872 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001873 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 }
K.Takataeeec2542021-06-02 13:28:16 +02001876 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001878 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001880 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 STRCAT(IObuff, ")");
1882 return IObuff;
1883 }
1884
1885 return NULL;
1886}
1887
1888/*
1889 * Function given to ExpandGeneric() to obtain the list of internal or
1890 * user defined variable or function names.
1891 */
1892 char_u *
1893get_expr_name(expand_T *xp, int idx)
1894{
1895 static int intidx = -1;
1896 char_u *name;
1897
1898 if (idx == 0)
1899 intidx = -1;
1900 if (intidx < 0)
1901 {
1902 name = get_function_name(xp, idx);
1903 if (name != NULL)
1904 return name;
1905 }
1906 return get_user_var_name(xp, ++intidx);
1907}
1908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001910 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001911 * Return index, or -1 if not found or "implemented" is TRUE and the function
1912 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001914 static int
1915find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916{
1917 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001918 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 int cmp;
1920 int x;
1921
K.Takataeeec2542021-06-02 13:28:16 +02001922 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001923
1924 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 while (first <= last)
1926 {
1927 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001928 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 if (cmp < 0)
1930 last = x - 1;
1931 else if (cmp > 0)
1932 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001933 else if (implemented && global_functions[x].f_func == NULL)
1934 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 else
1936 return x;
1937 }
1938 return -1;
1939}
1940
Bram Moolenaar15c47602020-03-26 22:16:48 +01001941/*
1942 * Find internal function "name" in table "global_functions".
1943 * Return index, or -1 if not found or the function is not implemented.
1944 */
1945 int
1946find_internal_func(char_u *name)
1947{
1948 return find_internal_func_opt(name, TRUE);
1949}
1950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001951 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001952has_internal_func(char_u *name)
1953{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001954 return find_internal_func_opt(name, TRUE) >= 0;
1955}
1956
1957 static int
1958has_internal_func_name(char_u *name)
1959{
1960 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001961}
1962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 char *
1964internal_func_name(int idx)
1965{
1966 return global_functions[idx].f_name;
1967}
1968
Bram Moolenaar94738d82020-10-21 14:25:07 +02001969/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001970 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001971 * Uses the list of types on the type stack: "types".
1972 * Return FAIL and gives an error message when a type is wrong.
1973 */
1974 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001975internal_func_check_arg_types(
1976 type_T **types,
1977 int idx,
1978 int argcount,
1979 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001980{
1981 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1982 int i;
1983
1984 if (argchecks != NULL)
1985 {
1986 argcontext_T context;
1987
1988 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001989 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001990 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001991 for (i = 0; i < argcount; ++i)
1992 if (argchecks[i] != NULL)
1993 {
1994 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001995 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001996 return FAIL;
1997 }
1998 }
1999 return OK;
2000}
2001
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002002/*
2003 * Call the "f_retfunc" function to obtain the return type of function "idx".
2004 * "argtypes" is the list of argument types or NULL when there are no
2005 * arguments.
2006 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002008internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002010 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011}
2012
2013/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002014 * Return TRUE if "idx" is for the map() function.
2015 */
2016 int
2017internal_func_is_map(int idx)
2018{
2019 return global_functions[idx].f_func == f_map;
2020}
2021
2022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002024 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2025 * first argument, 2 if method base is second argument, etc. 9 if method base
2026 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 */
2028 int
2029check_internal_func(int idx, int argcount)
2030{
2031 int res;
2032 char *name;
2033
2034 if (argcount < global_functions[idx].f_min_argc)
2035 res = FCERR_TOOFEW;
2036 else if (argcount > global_functions[idx].f_max_argc)
2037 res = FCERR_TOOMANY;
2038 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002039 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
2041 name = internal_func_name(idx);
2042 if (res == FCERR_TOOMANY)
2043 semsg(_(e_toomanyarg), name);
2044 else
2045 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002046 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047}
2048
Bram Moolenaarac92e252019-08-03 21:58:38 +02002049 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050call_internal_func(
2051 char_u *name,
2052 int argcount,
2053 typval_T *argvars,
2054 typval_T *rettv)
2055{
2056 int i;
2057
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002058 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002059 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002060 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002061 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002062 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002063 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002064 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002066 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002067 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002068}
2069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 void
2071call_internal_func_by_idx(
2072 int idx,
2073 typval_T *argvars,
2074 typval_T *rettv)
2075{
2076 global_functions[idx].f_func(argvars, rettv);
2077}
2078
Bram Moolenaarac92e252019-08-03 21:58:38 +02002079/*
2080 * Invoke a method for base->method().
2081 */
2082 int
2083call_internal_method(
2084 char_u *name,
2085 int argcount,
2086 typval_T *argvars,
2087 typval_T *rettv,
2088 typval_T *basetv)
2089{
2090 int i;
2091 int fi;
2092 typval_T argv[MAX_FUNC_ARGS + 1];
2093
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002094 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002095 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002096 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002097 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002098 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002099 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002100 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002101 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002103
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002104 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002105 {
2106 // base value goes last
2107 for (i = 0; i < argcount; ++i)
2108 argv[i] = argvars[i];
2109 argv[argcount] = *basetv;
2110 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002111 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002112 {
2113 // base value goes second
2114 argv[0] = argvars[0];
2115 argv[1] = *basetv;
2116 for (i = 1; i < argcount; ++i)
2117 argv[i + 1] = argvars[i];
2118 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002119 else if (global_functions[fi].f_argtype == FEARG_3)
2120 {
2121 // base value goes third
2122 argv[0] = argvars[0];
2123 argv[1] = argvars[1];
2124 argv[2] = *basetv;
2125 for (i = 2; i < argcount; ++i)
2126 argv[i + 1] = argvars[i];
2127 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002128 else if (global_functions[fi].f_argtype == FEARG_4)
2129 {
2130 // base value goes fourth
2131 argv[0] = argvars[0];
2132 argv[1] = argvars[1];
2133 argv[2] = argvars[2];
2134 argv[3] = *basetv;
2135 for (i = 3; i < argcount; ++i)
2136 argv[i + 1] = argvars[i];
2137 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002138 else
2139 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002140 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002141 argv[0] = *basetv;
2142 for (i = 0; i < argcount; ++i)
2143 argv[i + 1] = argvars[i];
2144 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002145 argv[argcount + 1].v_type = VAR_UNKNOWN;
2146
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002147 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002148 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149}
2150
2151/*
2152 * Return TRUE for a non-zero Number and a non-empty String.
2153 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002154 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155non_zero_arg(typval_T *argvars)
2156{
2157 return ((argvars[0].v_type == VAR_NUMBER
2158 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002159 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 && argvars[0].vval.v_number == VVAL_TRUE)
2161 || (argvars[0].v_type == VAR_STRING
2162 && argvars[0].vval.v_string != NULL
2163 && *argvars[0].vval.v_string != NUL));
2164}
2165
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167 * "and(expr, expr)" function
2168 */
2169 static void
2170f_and(typval_T *argvars, typval_T *rettv)
2171{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002172 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2173 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002174}
2175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002177 * "balloon_show()" function
2178 */
2179#ifdef FEAT_BEVAL
2180 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002181f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2182{
2183 rettv->v_type = VAR_STRING;
2184 if (balloonEval != NULL)
2185 {
2186 if (balloonEval->msg == NULL)
2187 rettv->vval.v_string = NULL;
2188 else
2189 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2190 }
2191}
2192
2193 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002194f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2195{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002196 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002197 {
2198 if (argvars[0].v_type == VAR_LIST
2199# ifdef FEAT_GUI
2200 && !gui.in_use
2201# endif
2202 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002203 {
2204 list_T *l = argvars[0].vval.v_list;
2205
2206 // empty list removes the balloon
2207 post_balloon(balloonEval, NULL,
2208 l == NULL || l->lv_len == 0 ? NULL : l);
2209 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002210 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002211 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002212 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002213
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002214 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2215 return;
2216
2217 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002218 if (mesg != NULL)
2219 // empty string removes the balloon
2220 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2221 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002222 }
2223}
2224
Bram Moolenaar669a8282017-11-19 20:13:05 +01002225# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002226 static void
2227f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2228{
2229 if (rettv_list_alloc(rettv) == OK)
2230 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002231 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002232
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002233 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2234 return;
2235 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002236 if (msg != NULL)
2237 {
2238 pumitem_T *array;
2239 int size = split_message(msg, &array);
2240 int i;
2241
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002242 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002243 for (i = 1; i < size - 1; ++i)
2244 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002245 while (size > 0)
2246 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002247 vim_free(array);
2248 }
2249 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002250}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002251# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002252#endif
2253
2254/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002255 * Get the buffer from "arg" and give an error and return NULL if it is not
2256 * valid.
2257 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002258 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002259get_buf_arg(typval_T *arg)
2260{
2261 buf_T *buf;
2262
2263 ++emsg_off;
2264 buf = tv_get_buf(arg, FALSE);
2265 --emsg_off;
2266 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002267 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002268 return buf;
2269}
2270
2271/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 * "byte2line(byte)" function
2273 */
2274 static void
2275f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2276{
2277#ifndef FEAT_BYTEOFF
2278 rettv->vval.v_number = -1;
2279#else
2280 long boff = 0;
2281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002282 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002283 if (boff < 0)
2284 rettv->vval.v_number = -1;
2285 else
2286 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2287 (linenr_T)0, &boff);
2288#endif
2289}
2290
2291 static void
2292byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2293{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295 char_u *str;
2296 varnumber_T idx;
2297
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002298 str = tv_get_string_chk(&argvars[0]);
2299 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300 rettv->vval.v_number = -1;
2301 if (str == NULL || idx < 0)
2302 return;
2303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304 t = str;
2305 for ( ; idx > 0; idx--)
2306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002307 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 return;
2309 if (enc_utf8 && comp)
2310 t += utf_ptr2len(t);
2311 else
2312 t += (*mb_ptr2len)(t);
2313 }
2314 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002315}
2316
2317/*
2318 * "byteidx()" function
2319 */
2320 static void
2321f_byteidx(typval_T *argvars, typval_T *rettv)
2322{
2323 byteidx(argvars, rettv, FALSE);
2324}
2325
2326/*
2327 * "byteidxcomp()" function
2328 */
2329 static void
2330f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2331{
2332 byteidx(argvars, rettv, TRUE);
2333}
2334
2335/*
2336 * "call(func, arglist [, dict])" function
2337 */
2338 static void
2339f_call(typval_T *argvars, typval_T *rettv)
2340{
2341 char_u *func;
2342 partial_T *partial = NULL;
2343 dict_T *selfdict = NULL;
2344
2345 if (argvars[1].v_type != VAR_LIST)
2346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002348 return;
2349 }
2350 if (argvars[1].vval.v_list == NULL)
2351 return;
2352
2353 if (argvars[0].v_type == VAR_FUNC)
2354 func = argvars[0].vval.v_string;
2355 else if (argvars[0].v_type == VAR_PARTIAL)
2356 {
2357 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002358 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 }
2360 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002361 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002362 if (func == NULL || *func == NUL)
2363 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364
2365 if (argvars[2].v_type != VAR_UNKNOWN)
2366 {
2367 if (argvars[2].v_type != VAR_DICT)
2368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 return;
2371 }
2372 selfdict = argvars[2].vval.v_dict;
2373 }
2374
2375 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2376}
2377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378/*
2379 * "changenr()" function
2380 */
2381 static void
2382f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2383{
2384 rettv->vval.v_number = curbuf->b_u_seq_cur;
2385}
2386
2387/*
2388 * "char2nr(string)" function
2389 */
2390 static void
2391f_char2nr(typval_T *argvars, typval_T *rettv)
2392{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002393 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2394 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 if (has_mbyte)
2396 {
2397 int utf8 = 0;
2398
2399 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002400 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002401
2402 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002403 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002404 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002405 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 }
2407 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002408 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409}
2410
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002411/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002412 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2413 * returns the character index of the column. Otherwise, returns the byte index
2414 * of the column.
2415 */
2416 static void
2417get_col(typval_T *argvars, typval_T *rettv, int charcol)
2418{
2419 colnr_T col = 0;
2420 pos_T *fp;
2421 int fnum = curbuf->b_fnum;
2422
2423 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2424 if (fp != NULL && fnum == curbuf->b_fnum)
2425 {
2426 if (fp->col == MAXCOL)
2427 {
2428 // '> can be MAXCOL, get the length of the line then
2429 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2430 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2431 else
2432 col = MAXCOL;
2433 }
2434 else
2435 {
2436 col = fp->col + 1;
2437 // col(".") when the cursor is on the NUL at the end of the line
2438 // because of "coladd" can be seen as an extra column.
2439 if (virtual_active() && fp == &curwin->w_cursor)
2440 {
2441 char_u *p = ml_get_cursor();
2442
2443 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2444 curwin->w_virtcol - curwin->w_cursor.coladd))
2445 {
2446 int l;
2447
2448 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2449 col += l;
2450 }
2451 }
2452 }
2453 }
2454 rettv->vval.v_number = col;
2455}
2456
2457/*
2458 * "charcol()" function
2459 */
2460 static void
2461f_charcol(typval_T *argvars, typval_T *rettv)
2462{
2463 get_col(argvars, rettv, TRUE);
2464}
2465
2466/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002467 * "charidx()" function
2468 */
2469 static void
2470f_charidx(typval_T *argvars, typval_T *rettv)
2471{
2472 char_u *str;
2473 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002474 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002475 char_u *p;
2476 int len;
2477 int (*ptr2len)(char_u *);
2478
2479 rettv->vval.v_number = -1;
2480
2481 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2482 || (argvars[2].v_type != VAR_UNKNOWN
2483 && argvars[2].v_type != VAR_NUMBER))
2484 {
2485 emsg(_(e_invarg));
2486 return;
2487 }
2488
2489 str = tv_get_string_chk(&argvars[0]);
2490 idx = tv_get_number_chk(&argvars[1], NULL);
2491 if (str == NULL || idx < 0)
2492 return;
2493
2494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002495 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002496 if (countcc < 0 || countcc > 1)
2497 {
2498 semsg(_(e_using_number_as_bool_nr), countcc);
2499 return;
2500 }
2501
2502 if (enc_utf8 && countcc)
2503 ptr2len = utf_ptr2len;
2504 else
2505 ptr2len = mb_ptr2len;
2506
2507 for (p = str, len = 0; p <= str + idx; len++)
2508 {
2509 if (*p == NUL)
2510 return;
2511 p += ptr2len(p);
2512 }
2513
2514 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2515}
2516
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002517 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002518get_optional_window(typval_T *argvars, int idx)
2519{
2520 win_T *win = curwin;
2521
2522 if (argvars[idx].v_type != VAR_UNKNOWN)
2523 {
2524 win = find_win_by_nr_or_id(&argvars[idx]);
2525 if (win == NULL)
2526 {
2527 emsg(_(e_invalwindow));
2528 return NULL;
2529 }
2530 }
2531 return win;
2532}
2533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 * "col(string)" function
2536 */
2537 static void
2538f_col(typval_T *argvars, typval_T *rettv)
2539{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002540 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541}
2542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543/*
2544 * "confirm(message, buttons[, default [, type]])" function
2545 */
2546 static void
2547f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2548{
2549#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2550 char_u *message;
2551 char_u *buttons = NULL;
2552 char_u buf[NUMBUFLEN];
2553 char_u buf2[NUMBUFLEN];
2554 int def = 1;
2555 int type = VIM_GENERIC;
2556 char_u *typestr;
2557 int error = FALSE;
2558
Bram Moolenaarc5809432021-03-27 21:23:30 +01002559 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2560 return;
2561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002562 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 if (message == NULL)
2564 error = TRUE;
2565 if (argvars[1].v_type != VAR_UNKNOWN)
2566 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002567 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2568 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002569 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002570 if (buttons == NULL)
2571 error = TRUE;
2572 if (argvars[2].v_type != VAR_UNKNOWN)
2573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002574 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002575 if (argvars[3].v_type != VAR_UNKNOWN)
2576 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002577 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2578 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002579 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002580 if (typestr == NULL)
2581 error = TRUE;
2582 else
2583 {
2584 switch (TOUPPER_ASC(*typestr))
2585 {
2586 case 'E': type = VIM_ERROR; break;
2587 case 'Q': type = VIM_QUESTION; break;
2588 case 'I': type = VIM_INFO; break;
2589 case 'W': type = VIM_WARNING; break;
2590 case 'G': type = VIM_GENERIC; break;
2591 }
2592 }
2593 }
2594 }
2595 }
2596
2597 if (buttons == NULL || *buttons == NUL)
2598 buttons = (char_u *)_("&Ok");
2599
2600 if (!error)
2601 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2602 def, NULL, FALSE);
2603#endif
2604}
2605
2606/*
2607 * "copy()" function
2608 */
2609 static void
2610f_copy(typval_T *argvars, typval_T *rettv)
2611{
2612 item_copy(&argvars[0], rettv, FALSE, 0);
2613}
2614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002615/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002616 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002617 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002618 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 */
2620 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002621set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002622{
2623 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002625 int set_curswant = TRUE;
2626
2627 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002628 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 {
2630 pos_T pos;
2631 colnr_T curswant = -1;
2632
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002633 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 return;
2637 }
2638 line = pos.lnum;
2639 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002640 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002641 if (curswant >= 0)
2642 {
2643 curwin->w_curswant = curswant - 1;
2644 set_curswant = FALSE;
2645 }
2646 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002647 else if ((argvars[0].v_type == VAR_NUMBER ||
2648 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002649 && (argvars[1].v_type == VAR_NUMBER ||
2650 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002652 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002653 if (line < 0)
2654 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002655 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002656 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002657 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002659 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002661 else
2662 {
2663 emsg(_(e_invarg));
2664 return;
2665 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002666 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002667 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 if (line > 0)
2669 curwin->w_cursor.lnum = line;
2670 if (col > 0)
2671 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002674 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002676 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 if (has_mbyte)
2678 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679
2680 curwin->w_set_curswant = set_curswant;
2681 rettv->vval.v_number = 0;
2682}
2683
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002684/*
2685 * "cursor(lnum, col)" function, or
2686 * "cursor(list)"
2687 *
2688 * Moves the cursor to the specified line and column.
2689 * Returns 0 when the position could be set, -1 otherwise.
2690 */
2691 static void
2692f_cursor(typval_T *argvars, typval_T *rettv)
2693{
2694 set_cursorpos(argvars, rettv, FALSE);
2695}
2696
Bram Moolenaar4f974752019-02-17 17:44:42 +01002697#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002698/*
2699 * "debugbreak()" function
2700 */
2701 static void
2702f_debugbreak(typval_T *argvars, typval_T *rettv)
2703{
2704 int pid;
2705
2706 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002707 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002708 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002709 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002710 else
2711 {
2712 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2713
2714 if (hProcess != NULL)
2715 {
2716 DebugBreakProcess(hProcess);
2717 CloseHandle(hProcess);
2718 rettv->vval.v_number = OK;
2719 }
2720 }
2721}
2722#endif
2723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002724/*
2725 * "deepcopy()" function
2726 */
2727 static void
2728f_deepcopy(typval_T *argvars, typval_T *rettv)
2729{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002730 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002731 int copyID;
2732
2733 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002734 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002735 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002736 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002737 else
2738 {
2739 copyID = get_copyID();
2740 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2741 }
2742}
2743
2744/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 * "did_filetype()" function
2746 */
2747 static void
2748f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2749{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002750 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751}
2752
2753/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002754 * "echoraw({expr})" function
2755 */
2756 static void
2757f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2758{
2759 char_u *str = tv_get_string_chk(&argvars[0]);
2760
2761 if (str != NULL && *str != NUL)
2762 {
2763 out_str(str);
2764 out_flush();
2765 }
2766}
2767
2768/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002769 * "empty({expr})" function
2770 */
2771 static void
2772f_empty(typval_T *argvars, typval_T *rettv)
2773{
2774 int n = FALSE;
2775
2776 switch (argvars[0].v_type)
2777 {
2778 case VAR_STRING:
2779 case VAR_FUNC:
2780 n = argvars[0].vval.v_string == NULL
2781 || *argvars[0].vval.v_string == NUL;
2782 break;
2783 case VAR_PARTIAL:
2784 n = FALSE;
2785 break;
2786 case VAR_NUMBER:
2787 n = argvars[0].vval.v_number == 0;
2788 break;
2789 case VAR_FLOAT:
2790#ifdef FEAT_FLOAT
2791 n = argvars[0].vval.v_float == 0.0;
2792 break;
2793#endif
2794 case VAR_LIST:
2795 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002796 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 break;
2798 case VAR_DICT:
2799 n = argvars[0].vval.v_dict == NULL
2800 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2801 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002802 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 case VAR_SPECIAL:
2804 n = argvars[0].vval.v_number != VVAL_TRUE;
2805 break;
2806
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002807 case VAR_BLOB:
2808 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002809 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2810 break;
2811
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002812 case VAR_JOB:
2813#ifdef FEAT_JOB_CHANNEL
2814 n = argvars[0].vval.v_job == NULL
2815 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2816 break;
2817#endif
2818 case VAR_CHANNEL:
2819#ifdef FEAT_JOB_CHANNEL
2820 n = argvars[0].vval.v_channel == NULL
2821 || !channel_is_open(argvars[0].vval.v_channel);
2822 break;
2823#endif
2824 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002825 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002827 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002828 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002829 n = TRUE;
2830 break;
2831 }
2832
2833 rettv->vval.v_number = n;
2834}
2835
2836/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002837 * "environ()" function
2838 */
2839 static void
2840f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2841{
2842#if !defined(AMIGA)
2843 int i = 0;
2844 char_u *entry, *value;
2845# ifdef MSWIN
2846 extern wchar_t **_wenviron;
2847# else
2848 extern char **environ;
2849# endif
2850
2851 if (rettv_dict_alloc(rettv) != OK)
2852 return;
2853
2854# ifdef MSWIN
2855 if (*_wenviron == NULL)
2856 return;
2857# else
2858 if (*environ == NULL)
2859 return;
2860# endif
2861
2862 for (i = 0; ; ++i)
2863 {
2864# ifdef MSWIN
2865 short_u *p;
2866
2867 if ((p = (short_u *)_wenviron[i]) == NULL)
2868 return;
2869 entry = utf16_to_enc(p, NULL);
2870# else
2871 if ((entry = (char_u *)environ[i]) == NULL)
2872 return;
2873 entry = vim_strsave(entry);
2874# endif
2875 if (entry == NULL) // out of memory
2876 return;
2877 if ((value = vim_strchr(entry, '=')) == NULL)
2878 {
2879 vim_free(entry);
2880 continue;
2881 }
2882 *value++ = NUL;
2883 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2884 vim_free(entry);
2885 }
2886#endif
2887}
2888
2889/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002890 * "escape({string}, {chars})" function
2891 */
2892 static void
2893f_escape(typval_T *argvars, typval_T *rettv)
2894{
2895 char_u buf[NUMBUFLEN];
2896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002897 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2898 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 rettv->v_type = VAR_STRING;
2900}
2901
2902/*
2903 * "eval()" function
2904 */
2905 static void
2906f_eval(typval_T *argvars, typval_T *rettv)
2907{
2908 char_u *s, *p;
2909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002910 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002911 if (s != NULL)
2912 s = skipwhite(s);
2913
2914 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002916 {
2917 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002918 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 need_clr_eos = FALSE;
2920 rettv->v_type = VAR_NUMBER;
2921 rettv->vval.v_number = 0;
2922 }
2923 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002924 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925}
2926
2927/*
2928 * "eventhandler()" function
2929 */
2930 static void
2931f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2932{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002933 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002934}
2935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936static garray_T redir_execute_ga;
2937
2938/*
2939 * Append "value[value_len]" to the execute() output.
2940 */
2941 void
2942execute_redir_str(char_u *value, int value_len)
2943{
2944 int len;
2945
2946 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002947 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002949 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950 if (ga_grow(&redir_execute_ga, len) == OK)
2951 {
2952 mch_memmove((char *)redir_execute_ga.ga_data
2953 + redir_execute_ga.ga_len, value, len);
2954 redir_execute_ga.ga_len += len;
2955 }
2956}
2957
2958/*
2959 * Get next line from a list.
2960 * Called by do_cmdline() to get the next line.
2961 * Returns allocated string, or NULL for end of function.
2962 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 static char_u *
2964get_list_line(
2965 int c UNUSED,
2966 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002967 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002968 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969{
2970 listitem_T **p = (listitem_T **)cookie;
2971 listitem_T *item = *p;
2972 char_u buf[NUMBUFLEN];
2973 char_u *s;
2974
2975 if (item == NULL)
2976 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 *p = item->li_next;
2979 return s == NULL ? NULL : vim_strsave(s);
2980}
2981
2982/*
2983 * "execute()" function
2984 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002985 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002986execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987{
2988 char_u *cmd = NULL;
2989 list_T *list = NULL;
2990 int save_msg_silent = msg_silent;
2991 int save_emsg_silent = emsg_silent;
2992 int save_emsg_noredir = emsg_noredir;
2993 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002994 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002996 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002997 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998
2999 rettv->vval.v_string = NULL;
3000 rettv->v_type = VAR_STRING;
3001
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003002 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003004 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003005 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003006 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007 return;
3008 ++list->lv_refcount;
3009 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003010 else if (argvars[arg_off].v_type == VAR_JOB
3011 || argvars[arg_off].v_type == VAR_CHANNEL)
3012 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003013 semsg(_(e_using_invalid_value_as_string_str),
3014 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003015 return;
3016 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 else
3018 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003019 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 if (cmd == NULL)
3021 return;
3022 }
3023
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003024 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 {
3026 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003027 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3028 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029
3030 if (s == NULL)
3031 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003032 if (*s == NUL)
3033 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034 if (STRNCMP(s, "silent", 6) == 0)
3035 ++msg_silent;
3036 if (STRCMP(s, "silent!") == 0)
3037 {
3038 emsg_silent = TRUE;
3039 emsg_noredir = TRUE;
3040 }
3041 }
3042 else
3043 ++msg_silent;
3044
3045 if (redir_execute)
3046 save_ga = redir_execute_ga;
3047 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3048 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003049 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003050 if (!echo_output)
3051 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052
3053 if (cmd != NULL)
3054 do_cmdline_cmd(cmd);
3055 else
3056 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003057 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003059 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003060 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003061 do_cmdline(NULL, get_list_line, (void *)&item,
3062 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3063 --list->lv_refcount;
3064 }
3065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003066 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003067 if (ga_grow(&redir_execute_ga, 1) == OK)
3068 {
3069 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3070 rettv->vval.v_string = redir_execute_ga.ga_data;
3071 }
3072 else
3073 {
3074 ga_clear(&redir_execute_ga);
3075 rettv->vval.v_string = NULL;
3076 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077 msg_silent = save_msg_silent;
3078 emsg_silent = save_emsg_silent;
3079 emsg_noredir = save_emsg_noredir;
3080
3081 redir_execute = save_redir_execute;
3082 if (redir_execute)
3083 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003084 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003086 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003087 if (echo_output)
3088 // When not working silently: put it in column zero. A following
3089 // "echon" will overwrite the message, unavoidably.
3090 msg_col = 0;
3091 else
3092 // When working silently: Put it back where it was, since nothing
3093 // should have been written.
3094 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095}
3096
3097/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003098 * "execute()" function
3099 */
3100 static void
3101f_execute(typval_T *argvars, typval_T *rettv)
3102{
3103 execute_common(argvars, rettv, 0);
3104}
3105
3106/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 * "exists()" function
3108 */
3109 static void
3110f_exists(typval_T *argvars, typval_T *rettv)
3111{
3112 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003116 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 if (mch_getenv(p + 1) != NULL)
3120 n = TRUE;
3121 else
3122 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003123 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 p = expand_env_save(p);
3125 if (p != NULL && *p != '$')
3126 n = TRUE;
3127 vim_free(p);
3128 }
3129 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003130 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003131 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003132 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003134 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003136 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003138 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003140 else if (*p == '?') // internal function only
3141 {
3142 n = has_internal_func_name(p + 1);
3143 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144 else if (*p == ':')
3145 {
3146 n = cmd_exists(p + 1);
3147 }
3148 else if (*p == '#')
3149 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 if (p[1] == '#')
3151 n = autocmd_supported(p + 2);
3152 else
3153 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003154 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003155 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003157 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 }
3159
3160 rettv->vval.v_number = n;
3161}
3162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163/*
3164 * "expand()" function
3165 */
3166 static void
3167f_expand(typval_T *argvars, typval_T *rettv)
3168{
3169 char_u *s;
3170 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003171 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3173 expand_T xpc;
3174 int error = FALSE;
3175 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003176#ifdef BACKSLASH_IN_FILENAME
3177 char_u *p_csl_save = p_csl;
3178
3179 // avoid using 'completeslash' here
3180 p_csl = empty_option;
3181#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003182
3183 rettv->v_type = VAR_STRING;
3184 if (argvars[1].v_type != VAR_UNKNOWN
3185 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003186 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003188 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003190 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191 if (*s == '%' || *s == '#' || *s == '<')
3192 {
3193 ++emsg_off;
3194 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3195 --emsg_off;
3196 if (rettv->v_type == VAR_LIST)
3197 {
3198 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3199 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003200 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003201 }
3202 else
3203 rettv->vval.v_string = result;
3204 }
3205 else
3206 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003207 // When the optional second argument is non-zero, don't remove matches
3208 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003210 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003211 options |= WILD_KEEP_ALL;
3212 if (!error)
3213 {
3214 ExpandInit(&xpc);
3215 xpc.xp_context = EXPAND_FILES;
3216 if (p_wic)
3217 options += WILD_ICASE;
3218 if (rettv->v_type == VAR_STRING)
3219 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3220 options, WILD_ALL);
3221 else if (rettv_list_alloc(rettv) != FAIL)
3222 {
3223 int i;
3224
3225 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3226 for (i = 0; i < xpc.xp_numfiles; i++)
3227 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3228 ExpandCleanup(&xpc);
3229 }
3230 }
3231 else
3232 rettv->vval.v_string = NULL;
3233 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003234#ifdef BACKSLASH_IN_FILENAME
3235 p_csl = p_csl_save;
3236#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237}
3238
3239/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003240 * "expandcmd()" function
3241 * Expand all the special characters in a command string.
3242 */
3243 static void
3244f_expandcmd(typval_T *argvars, typval_T *rettv)
3245{
3246 exarg_T eap;
3247 char_u *cmdstr;
3248 char *errormsg = NULL;
3249
3250 rettv->v_type = VAR_STRING;
3251 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3252
3253 memset(&eap, 0, sizeof(eap));
3254 eap.cmd = cmdstr;
3255 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003256 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003257 eap.usefilter = FALSE;
3258 eap.nextcmd = NULL;
3259 eap.cmdidx = CMD_USER;
3260
3261 expand_filename(&eap, &cmdstr, &errormsg);
3262 if (errormsg != NULL && *errormsg != NUL)
3263 emsg(errormsg);
3264
3265 rettv->vval.v_string = cmdstr;
3266}
3267
3268/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269 * "feedkeys()" function
3270 */
3271 static void
3272f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3273{
3274 int remap = TRUE;
3275 int insert = FALSE;
3276 char_u *keys, *flags;
3277 char_u nbuf[NUMBUFLEN];
3278 int typed = FALSE;
3279 int execute = FALSE;
3280 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003281 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 char_u *keys_esc;
3283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003284 // This is not allowed in the sandbox. If the commands would still be
3285 // executed in the sandbox it would be OK, but it probably happens later,
3286 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 if (check_secure())
3288 return;
3289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003290 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003291
3292 if (argvars[1].v_type != VAR_UNKNOWN)
3293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003294 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 for ( ; *flags != NUL; ++flags)
3296 {
3297 switch (*flags)
3298 {
3299 case 'n': remap = FALSE; break;
3300 case 'm': remap = TRUE; break;
3301 case 't': typed = TRUE; break;
3302 case 'i': insert = TRUE; break;
3303 case 'x': execute = TRUE; break;
3304 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003305 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003306 }
3307 }
3308 }
3309
3310 if (*keys != NUL || execute)
3311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003312 // Need to escape K_SPECIAL and CSI before putting the string in the
3313 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 keys_esc = vim_strsave_escape_csi(keys);
3315 if (keys_esc != NULL)
3316 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003317 if (lowlevel)
3318 {
3319#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003320 int idx;
3321 int len = (int)STRLEN(keys);
3322
3323 for (idx = 0; idx < len; ++idx)
3324 {
3325 // if a CTRL-C was typed, set got_int, similar to what
3326 // happens in fill_input_buf()
3327 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3328 got_int = TRUE;
3329 add_to_input_buf(keys + idx, 1);
3330 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003331#else
3332 emsg(_("E980: lowlevel input not supported"));
3333#endif
3334 }
3335 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003336 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003337 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003339 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003340#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003341 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003342#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003343 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003344 typebuf_was_filled = TRUE;
3345 }
3346 vim_free(keys_esc);
3347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 if (execute)
3349 {
3350 int save_msg_scroll = msg_scroll;
3351
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003353 msg_scroll = FALSE;
3354
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003355 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003356 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003357 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003358 ++in_feedkeys;
3359 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003360 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003361 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003362 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003363 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003364 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003365 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 msg_scroll |= save_msg_scroll;
3368 }
3369 }
3370 }
3371}
3372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003373/*
3374 * "fnameescape({string})" function
3375 */
3376 static void
3377f_fnameescape(typval_T *argvars, typval_T *rettv)
3378{
3379 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003380 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381 rettv->v_type = VAR_STRING;
3382}
3383
3384/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003385 * "foreground()" function
3386 */
3387 static void
3388f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3389{
3390#ifdef FEAT_GUI
3391 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003392 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003394 return;
3395 }
3396#endif
3397#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399#endif
3400}
3401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003402 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003403common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404{
3405 char_u *s;
3406 char_u *name;
3407 int use_string = FALSE;
3408 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003409 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003410 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003411
3412 if (argvars[0].v_type == VAR_FUNC)
3413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003414 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003415 s = argvars[0].vval.v_string;
3416 }
3417 else if (argvars[0].v_type == VAR_PARTIAL
3418 && argvars[0].vval.v_partial != NULL)
3419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003420 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003422 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423 }
3424 else
3425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003426 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003427 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003428 use_string = TRUE;
3429 }
3430
Bram Moolenaar843b8842016-08-21 14:36:15 +02003431 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003432 {
3433 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003434 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003435 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3436 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003437 if (*name != NUL)
3438 s = NULL;
3439 }
3440
Bram Moolenaar843b8842016-08-21 14:36:15 +02003441 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3442 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003443 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003444 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003445 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003446 ? find_func(trans_name, is_global, NULL) == NULL
3447 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003448 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 else
3450 {
3451 int dict_idx = 0;
3452 int arg_idx = 0;
3453 list_T *list = NULL;
3454
3455 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3456 {
3457 char sid_buf[25];
3458 int off = *s == 's' ? 2 : 5;
3459
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003460 // Expand s: and <SID> into <SNR>nr_, so that the function can
3461 // also be called from another script. Using trans_function_name()
3462 // would also work, but some plugins depend on the name being
3463 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003464 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003465 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003466 if (name != NULL)
3467 {
3468 STRCPY(name, sid_buf);
3469 STRCAT(name, s + off);
3470 }
3471 }
3472 else
3473 name = vim_strsave(s);
3474
3475 if (argvars[1].v_type != VAR_UNKNOWN)
3476 {
3477 if (argvars[2].v_type != VAR_UNKNOWN)
3478 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003479 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003480 arg_idx = 1;
3481 dict_idx = 2;
3482 }
3483 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003484 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 dict_idx = 1;
3486 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 arg_idx = 1;
3489 if (dict_idx > 0)
3490 {
3491 if (argvars[dict_idx].v_type != VAR_DICT)
3492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003493 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003495 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 }
3497 if (argvars[dict_idx].vval.v_dict == NULL)
3498 dict_idx = 0;
3499 }
3500 if (arg_idx > 0)
3501 {
3502 if (argvars[arg_idx].v_type != VAR_LIST)
3503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003504 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003506 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003507 }
3508 list = argvars[arg_idx].vval.v_list;
3509 if (list == NULL || list->lv_len == 0)
3510 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003511 else if (list->lv_len > MAX_FUNC_ARGS)
3512 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003513 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003514 vim_free(name);
3515 goto theend;
3516 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003517 }
3518 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003519 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003520 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003521 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003523 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524 if (pt == NULL)
3525 vim_free(name);
3526 else
3527 {
3528 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3529 {
3530 listitem_T *li;
3531 int i = 0;
3532 int arg_len = 0;
3533 int lv_len = 0;
3534
3535 if (arg_pt != NULL)
3536 arg_len = arg_pt->pt_argc;
3537 if (list != NULL)
3538 lv_len = list->lv_len;
3539 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003540 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 if (pt->pt_argv == NULL)
3542 {
3543 vim_free(pt);
3544 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003545 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003547 for (i = 0; i < arg_len; i++)
3548 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3549 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003550 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003551 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003552 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003553 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555 }
3556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 // For "function(dict.func, [], dict)" and "func" is a partial
3558 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003559 if (dict_idx > 0)
3560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3563 ++pt->pt_dict->dv_refcount;
3564 }
3565 else if (arg_pt != NULL)
3566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 // If the dict was bound automatically the result is also
3568 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569 pt->pt_dict = arg_pt->pt_dict;
3570 pt->pt_auto = arg_pt->pt_auto;
3571 if (pt->pt_dict != NULL)
3572 ++pt->pt_dict->dv_refcount;
3573 }
3574
3575 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003576 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3577 {
3578 pt->pt_func = arg_pt->pt_func;
3579 func_ptr_ref(pt->pt_func);
3580 vim_free(name);
3581 }
3582 else if (is_funcref)
3583 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003584 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003585 func_ptr_ref(pt->pt_func);
3586 vim_free(name);
3587 }
3588 else
3589 {
3590 pt->pt_name = name;
3591 func_ref(name);
3592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 }
3594 rettv->v_type = VAR_PARTIAL;
3595 rettv->vval.v_partial = pt;
3596 }
3597 else
3598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003599 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600 rettv->v_type = VAR_FUNC;
3601 rettv->vval.v_string = name;
3602 func_ref(name);
3603 }
3604 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003605theend:
3606 vim_free(trans_name);
3607}
3608
3609/*
3610 * "funcref()" function
3611 */
3612 static void
3613f_funcref(typval_T *argvars, typval_T *rettv)
3614{
3615 common_function(argvars, rettv, TRUE);
3616}
3617
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003618 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003619ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003620{
3621 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3622 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003623 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003624}
3625
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003626/*
3627 * "function()" function
3628 */
3629 static void
3630f_function(typval_T *argvars, typval_T *rettv)
3631{
3632 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633}
3634
3635/*
3636 * "garbagecollect()" function
3637 */
3638 static void
3639f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3640{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003641 // This is postponed until we are back at the toplevel, because we may be
3642 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 want_garbage_collect = TRUE;
3644
Bram Moolenaar2df47312020-09-05 17:30:44 +02003645 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646 garbage_collect_at_exit = TRUE;
3647}
3648
3649/*
3650 * "get()" function
3651 */
3652 static void
3653f_get(typval_T *argvars, typval_T *rettv)
3654{
3655 listitem_T *li;
3656 list_T *l;
3657 dictitem_T *di;
3658 dict_T *d;
3659 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003660 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003662 if (argvars[0].v_type == VAR_BLOB)
3663 {
3664 int error = FALSE;
3665 int idx = tv_get_number_chk(&argvars[1], &error);
3666
3667 if (!error)
3668 {
3669 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003670 if (idx < 0)
3671 idx = blob_len(argvars[0].vval.v_blob) + idx;
3672 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3673 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003674 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003675 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003676 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003677 tv = rettv;
3678 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003679 }
3680 }
3681 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 {
3683 if ((l = argvars[0].vval.v_list) != NULL)
3684 {
3685 int error = FALSE;
3686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003687 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 if (!error && li != NULL)
3689 tv = &li->li_tv;
3690 }
3691 }
3692 else if (argvars[0].v_type == VAR_DICT)
3693 {
3694 if ((d = argvars[0].vval.v_dict) != NULL)
3695 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003696 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 if (di != NULL)
3698 tv = &di->di_tv;
3699 }
3700 }
3701 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3702 {
3703 partial_T *pt;
3704 partial_T fref_pt;
3705
3706 if (argvars[0].v_type == VAR_PARTIAL)
3707 pt = argvars[0].vval.v_partial;
3708 else
3709 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003710 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 fref_pt.pt_name = argvars[0].vval.v_string;
3712 pt = &fref_pt;
3713 }
3714
3715 if (pt != NULL)
3716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003717 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003718 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003719
3720 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3721 {
3722 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723 n = partial_name(pt);
3724 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 rettv->vval.v_string = NULL;
3726 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003727 {
3728 rettv->vval.v_string = vim_strsave(n);
3729 if (rettv->v_type == VAR_FUNC)
3730 func_ref(rettv->vval.v_string);
3731 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 }
3733 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003734 {
3735 what_is_dict = TRUE;
3736 if (pt->pt_dict != NULL)
3737 rettv_dict_set(rettv, pt->pt_dict);
3738 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 else if (STRCMP(what, "args") == 0)
3740 {
3741 rettv->v_type = VAR_LIST;
3742 if (rettv_list_alloc(rettv) == OK)
3743 {
3744 int i;
3745
3746 for (i = 0; i < pt->pt_argc; ++i)
3747 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3748 }
3749 }
3750 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003751 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003752
3753 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3754 // third argument
3755 if (!what_is_dict)
3756 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 }
3758 }
3759 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003760 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761
3762 if (tv == NULL)
3763 {
3764 if (argvars[2].v_type != VAR_UNKNOWN)
3765 copy_tv(&argvars[2], rettv);
3766 }
3767 else
3768 copy_tv(tv, rettv);
3769}
3770
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003771/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003772 * "getchangelist()" function
3773 */
3774 static void
3775f_getchangelist(typval_T *argvars, typval_T *rettv)
3776{
3777#ifdef FEAT_JUMPLIST
3778 buf_T *buf;
3779 int i;
3780 list_T *l;
3781 dict_T *d;
3782#endif
3783
3784 if (rettv_list_alloc(rettv) != OK)
3785 return;
3786
3787#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003788 if (argvars[0].v_type == VAR_UNKNOWN)
3789 buf = curbuf;
3790 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003791 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003792 if (buf == NULL)
3793 return;
3794
3795 l = list_alloc();
3796 if (l == NULL)
3797 return;
3798
3799 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3800 return;
3801 /*
3802 * The current window change list index tracks only the position in the
3803 * current buffer change list. For other buffers, use the change list
3804 * length as the current index.
3805 */
3806 list_append_number(rettv->vval.v_list,
3807 (varnumber_T)((buf == curwin->w_buffer)
3808 ? curwin->w_changelistidx : buf->b_changelistlen));
3809
3810 for (i = 0; i < buf->b_changelistlen; ++i)
3811 {
3812 if (buf->b_changelist[i].lnum == 0)
3813 continue;
3814 if ((d = dict_alloc()) == NULL)
3815 return;
3816 if (list_append_dict(l, d) == FAIL)
3817 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003818 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3819 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003820 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003821 }
3822#endif
3823}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003825 static void
3826getpos_both(
3827 typval_T *argvars,
3828 typval_T *rettv,
3829 int getcurpos,
3830 int charcol)
3831{
3832 pos_T *fp = NULL;
3833 pos_T pos;
3834 win_T *wp = curwin;
3835 list_T *l;
3836 int fnum = -1;
3837
3838 if (rettv_list_alloc(rettv) == OK)
3839 {
3840 l = rettv->vval.v_list;
3841 if (getcurpos)
3842 {
3843 if (argvars[0].v_type != VAR_UNKNOWN)
3844 {
3845 wp = find_win_by_nr_or_id(&argvars[0]);
3846 if (wp != NULL)
3847 fp = &wp->w_cursor;
3848 }
3849 else
3850 fp = &curwin->w_cursor;
3851 if (fp != NULL && charcol)
3852 {
3853 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003854 pos.col =
3855 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003856 fp = &pos;
3857 }
3858 }
3859 else
3860 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3861 if (fnum != -1)
3862 list_append_number(l, (varnumber_T)fnum);
3863 else
3864 list_append_number(l, (varnumber_T)0);
3865 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3866 : (varnumber_T)0);
3867 list_append_number(l, (fp != NULL)
3868 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3869 : (varnumber_T)0);
3870 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3871 (varnumber_T)0);
3872 if (getcurpos)
3873 {
3874 int save_set_curswant = curwin->w_set_curswant;
3875 colnr_T save_curswant = curwin->w_curswant;
3876 colnr_T save_virtcol = curwin->w_virtcol;
3877
3878 if (wp == curwin)
3879 update_curswant();
3880 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3881 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3882
3883 // Do not change "curswant", as it is unexpected that a get
3884 // function has a side effect.
3885 if (wp == curwin && save_set_curswant)
3886 {
3887 curwin->w_set_curswant = save_set_curswant;
3888 curwin->w_curswant = save_curswant;
3889 curwin->w_virtcol = save_virtcol;
3890 curwin->w_valid &= ~VALID_VIRTCOL;
3891 }
3892 }
3893 }
3894 else
3895 rettv->vval.v_number = FALSE;
3896}
3897
3898/*
3899 * "getcharpos()" function
3900 */
3901 static void
3902f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3903{
3904 getpos_both(argvars, rettv, FALSE, TRUE);
3905}
3906
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003907/*
3908 * "getcharsearch()" function
3909 */
3910 static void
3911f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3912{
3913 if (rettv_dict_alloc(rettv) != FAIL)
3914 {
3915 dict_T *dict = rettv->vval.v_dict;
3916
Bram Moolenaare0be1672018-07-08 16:50:37 +02003917 dict_add_string(dict, "char", last_csearch());
3918 dict_add_number(dict, "forward", last_csearch_forward());
3919 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 }
3921}
3922
3923/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003924 * "getenv()" function
3925 */
3926 static void
3927f_getenv(typval_T *argvars, typval_T *rettv)
3928{
3929 int mustfree = FALSE;
3930 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3931
3932 if (p == NULL)
3933 {
3934 rettv->v_type = VAR_SPECIAL;
3935 rettv->vval.v_number = VVAL_NULL;
3936 return;
3937 }
3938 if (!mustfree)
3939 p = vim_strsave(p);
3940 rettv->vval.v_string = p;
3941 rettv->v_type = VAR_STRING;
3942}
3943
3944/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 * "getfontname()" function
3946 */
3947 static void
3948f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3949{
3950 rettv->v_type = VAR_STRING;
3951 rettv->vval.v_string = NULL;
3952#ifdef FEAT_GUI
3953 if (gui.in_use)
3954 {
3955 GuiFont font;
3956 char_u *name = NULL;
3957
3958 if (argvars[0].v_type == VAR_UNKNOWN)
3959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003960 // Get the "Normal" font. Either the name saved by
3961 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 font = gui.norm_font;
3963 name = hl_get_font_name();
3964 }
3965 else
3966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003967 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003968 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969 return;
3970 font = gui_mch_get_font(name, FALSE);
3971 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003972 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003973 }
3974 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3975 if (argvars[0].v_type != VAR_UNKNOWN)
3976 gui_mch_free_font(font);
3977 }
3978#endif
3979}
3980
3981/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003982 * "getjumplist()" function
3983 */
3984 static void
3985f_getjumplist(typval_T *argvars, typval_T *rettv)
3986{
3987#ifdef FEAT_JUMPLIST
3988 win_T *wp;
3989 int i;
3990 list_T *l;
3991 dict_T *d;
3992#endif
3993
3994 if (rettv_list_alloc(rettv) != OK)
3995 return;
3996
3997#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003998 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003999 if (wp == NULL)
4000 return;
4001
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004002 cleanup_jumplist(wp, TRUE);
4003
Bram Moolenaar4f505882018-02-10 21:06:32 +01004004 l = list_alloc();
4005 if (l == NULL)
4006 return;
4007
4008 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4009 return;
4010 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4011
4012 for (i = 0; i < wp->w_jumplistlen; ++i)
4013 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004014 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4015 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004016 if ((d = dict_alloc()) == NULL)
4017 return;
4018 if (list_append_dict(l, d) == FAIL)
4019 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004020 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4021 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004022 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004023 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004024 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004025 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004026 }
4027#endif
4028}
4029
4030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 * "getpid()" function
4032 */
4033 static void
4034f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4035{
4036 rettv->vval.v_number = mch_get_pid();
4037}
4038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039/*
4040 * "getcurpos()" function
4041 */
4042 static void
4043f_getcurpos(typval_T *argvars, typval_T *rettv)
4044{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004045 getpos_both(argvars, rettv, TRUE, FALSE);
4046}
4047
4048 static void
4049f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4050{
4051 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052}
4053
4054/*
4055 * "getpos(string)" function
4056 */
4057 static void
4058f_getpos(typval_T *argvars, typval_T *rettv)
4059{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004060 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061}
4062
4063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004064 * "getreg()" function
4065 */
4066 static void
4067f_getreg(typval_T *argvars, typval_T *rettv)
4068{
4069 char_u *strregname;
4070 int regname;
4071 int arg2 = FALSE;
4072 int return_list = FALSE;
4073 int error = FALSE;
4074
4075 if (argvars[0].v_type != VAR_UNKNOWN)
4076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004077 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004078 if (strregname == NULL)
4079 error = TRUE;
4080 else if (in_vim9script() && STRLEN(strregname) > 1)
4081 {
4082 semsg(_(e_register_name_must_be_one_char_str), strregname);
4083 error = TRUE;
4084 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004085 if (argvars[1].v_type != VAR_UNKNOWN)
4086 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004087 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004089 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090 }
4091 }
4092 else
4093 strregname = get_vim_var_str(VV_REG);
4094
4095 if (error)
4096 return;
4097
4098 regname = (strregname == NULL ? '"' : *strregname);
4099 if (regname == 0)
4100 regname = '"';
4101
4102 if (return_list)
4103 {
4104 rettv->v_type = VAR_LIST;
4105 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4106 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4107 if (rettv->vval.v_list == NULL)
4108 (void)rettv_list_alloc(rettv);
4109 else
4110 ++rettv->vval.v_list->lv_refcount;
4111 }
4112 else
4113 {
4114 rettv->v_type = VAR_STRING;
4115 rettv->vval.v_string = get_reg_contents(regname,
4116 arg2 ? GREG_EXPR_SRC : 0);
4117 }
4118}
4119
4120/*
4121 * "getregtype()" function
4122 */
4123 static void
4124f_getregtype(typval_T *argvars, typval_T *rettv)
4125{
4126 char_u *strregname;
4127 int regname;
4128 char_u buf[NUMBUFLEN + 2];
4129 long reglen = 0;
4130
4131 if (argvars[0].v_type != VAR_UNKNOWN)
4132 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004133 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004134 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4135 {
4136 semsg(_(e_register_name_must_be_one_char_str), strregname);
4137 strregname = NULL;
4138 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004139 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 {
4141 rettv->v_type = VAR_STRING;
4142 rettv->vval.v_string = NULL;
4143 return;
4144 }
4145 }
4146 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 strregname = get_vim_var_str(VV_REG);
4149
4150 regname = (strregname == NULL ? '"' : *strregname);
4151 if (regname == 0)
4152 regname = '"';
4153
4154 buf[0] = NUL;
4155 buf[1] = NUL;
4156 switch (get_reg_type(regname, &reglen))
4157 {
4158 case MLINE: buf[0] = 'V'; break;
4159 case MCHAR: buf[0] = 'v'; break;
4160 case MBLOCK:
4161 buf[0] = Ctrl_V;
4162 sprintf((char *)buf + 1, "%ld", reglen + 1);
4163 break;
4164 }
4165 rettv->v_type = VAR_STRING;
4166 rettv->vval.v_string = vim_strsave(buf);
4167}
4168
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004169/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004170 * "gettagstack()" function
4171 */
4172 static void
4173f_gettagstack(typval_T *argvars, typval_T *rettv)
4174{
4175 win_T *wp = curwin; // default is current window
4176
4177 if (rettv_dict_alloc(rettv) != OK)
4178 return;
4179
4180 if (argvars[0].v_type != VAR_UNKNOWN)
4181 {
4182 wp = find_win_by_nr_or_id(&argvars[0]);
4183 if (wp == NULL)
4184 return;
4185 }
4186
4187 get_tagstack(wp, rettv->vval.v_dict);
4188}
4189
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004190/*
4191 * "gettext()" function
4192 */
4193 static void
4194f_gettext(typval_T *argvars, typval_T *rettv)
4195{
4196 if (argvars[0].v_type != VAR_STRING
4197 || argvars[0].vval.v_string == NULL
4198 || *argvars[0].vval.v_string == NUL)
4199 {
4200 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4201 }
4202 else
4203 {
4204 rettv->v_type = VAR_STRING;
4205 rettv->vval.v_string = vim_strsave(
4206 (char_u *)_(argvars[0].vval.v_string));
4207 }
4208}
4209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004210// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211#include "version.h"
4212
4213/*
4214 * "has()" function
4215 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004216 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217f_has(typval_T *argvars, typval_T *rettv)
4218{
4219 int i;
4220 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004221 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004223 typedef struct {
4224 char *name;
4225 short present;
4226 } has_item_T;
4227 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004229 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004231 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004232#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004233 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004234#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004235 },
4236 {"arp",
4237#if defined(AMIGA) && defined(FEAT_ARP)
4238 1
4239#else
4240 0
4241#endif
4242 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004243 {"haiku",
4244#ifdef __HAIKU__
4245 1
4246#else
4247 0
4248#endif
4249 },
4250 {"bsd",
4251#if defined(BSD) && !defined(MACOS_X)
4252 1
4253#else
4254 0
4255#endif
4256 },
4257 {"hpux",
4258#ifdef hpux
4259 1
4260#else
4261 0
4262#endif
4263 },
4264 {"linux",
4265#ifdef __linux__
4266 1
4267#else
4268 0
4269#endif
4270 },
4271 {"mac", // Mac OS X (and, once, Mac OS Classic)
4272#ifdef MACOS_X
4273 1
4274#else
4275 0
4276#endif
4277 },
4278 {"osx", // Mac OS X
4279#ifdef MACOS_X
4280 1
4281#else
4282 0
4283#endif
4284 },
4285 {"macunix", // Mac OS X, with the darwin feature
4286#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4287 1
4288#else
4289 0
4290#endif
4291 },
4292 {"osxdarwin", // synonym for macunix
4293#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4294 1
4295#else
4296 0
4297#endif
4298 },
4299 {"qnx",
4300#ifdef __QNX__
4301 1
4302#else
4303 0
4304#endif
4305 },
4306 {"sun",
4307#ifdef SUN_SYSTEM
4308 1
4309#else
4310 0
4311#endif
4312 },
4313 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004314#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004315 1
4316#else
4317 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004318#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004319 },
4320 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004322 1
4323#else
4324 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004326 },
4327 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004328#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004329 1
4330#else
4331 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004332#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004333 },
4334 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004335#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004336 1
4337#else
4338 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004340 },
4341 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004342#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004343 1
4344#else
4345 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004346#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004347 },
4348 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004350 1
4351#else
4352 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004354 },
4355 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004356#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004357 1
4358#else
4359 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004361 },
4362 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004364 1
4365#else
4366 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004367#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004368 },
4369 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004371 1
4372#else
4373 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004375 },
4376 {"autocmd", 1},
4377 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004378#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004379 1
4380#else
4381 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004382#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004383 },
4384 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004385#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004386 1
4387#else
4388 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004389#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004390 },
4391 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004392#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004393 1
4394#else
4395 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004397 },
4398 {"balloon_multiline",
4399#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4400 // MS-Windows requires runtime check, see below
4401 1
4402#else
4403 0
4404#endif
4405 },
4406 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004407#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004408 1
4409#else
4410 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004411#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004412 },
4413 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004415 1
4416#else
4417 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004418#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004419 },
4420 {"all_builtin_terms",
4421#if defined(ALL_BUILTIN_TCAPS)
4422 1
4423#else
4424 0
4425#endif
4426 },
4427 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004428#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004429 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004430 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004431 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004433 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004435 },
4436 {"byte_offset",
4437#ifdef FEAT_BYTEOFF
4438 1
4439#else
4440 0
4441#endif
4442 },
4443 {"channel",
4444#ifdef FEAT_JOB_CHANNEL
4445 1
4446#else
4447 0
4448#endif
4449 },
4450 {"cindent",
4451#ifdef FEAT_CINDENT
4452 1
4453#else
4454 0
4455#endif
4456 },
4457 {"clientserver",
4458#ifdef FEAT_CLIENTSERVER
4459 1
4460#else
4461 0
4462#endif
4463 },
4464 {"clipboard",
4465#ifdef FEAT_CLIPBOARD
4466 1
4467#else
4468 0
4469#endif
4470 },
4471 {"cmdline_compl", 1},
4472 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004473 {"cmdwin",
4474#ifdef FEAT_CMDWIN
4475 1
4476#else
4477 0
4478#endif
4479 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004480 {"comments", 1},
4481 {"conceal",
4482#ifdef FEAT_CONCEAL
4483 1
4484#else
4485 0
4486#endif
4487 },
4488 {"cryptv",
4489#ifdef FEAT_CRYPT
4490 1
4491#else
4492 0
4493#endif
4494 },
4495 {"crypt-blowfish",
4496#ifdef FEAT_CRYPT
4497 1
4498#else
4499 0
4500#endif
4501 },
4502 {"crypt-blowfish2",
4503#ifdef FEAT_CRYPT
4504 1
4505#else
4506 0
4507#endif
4508 },
4509 {"cscope",
4510#ifdef FEAT_CSCOPE
4511 1
4512#else
4513 0
4514#endif
4515 },
4516 {"cursorbind", 1},
4517 {"cursorshape",
4518#ifdef CURSOR_SHAPE
4519 1
4520#else
4521 0
4522#endif
4523 },
4524 {"debug",
4525#ifdef DEBUG
4526 1
4527#else
4528 0
4529#endif
4530 },
4531 {"dialog_con",
4532#ifdef FEAT_CON_DIALOG
4533 1
4534#else
4535 0
4536#endif
4537 },
4538 {"dialog_gui",
4539#ifdef FEAT_GUI_DIALOG
4540 1
4541#else
4542 0
4543#endif
4544 },
4545 {"diff",
4546#ifdef FEAT_DIFF
4547 1
4548#else
4549 0
4550#endif
4551 },
4552 {"digraphs",
4553#ifdef FEAT_DIGRAPHS
4554 1
4555#else
4556 0
4557#endif
4558 },
4559 {"directx",
4560#ifdef FEAT_DIRECTX
4561 1
4562#else
4563 0
4564#endif
4565 },
4566 {"dnd",
4567#ifdef FEAT_DND
4568 1
4569#else
4570 0
4571#endif
4572 },
4573 {"emacs_tags",
4574#ifdef FEAT_EMACS_TAGS
4575 1
4576#else
4577 0
4578#endif
4579 },
4580 {"eval", 1}, // always present, of course!
4581 {"ex_extra", 1}, // graduated feature
4582 {"extra_search",
4583#ifdef FEAT_SEARCH_EXTRA
4584 1
4585#else
4586 0
4587#endif
4588 },
4589 {"file_in_path",
4590#ifdef FEAT_SEARCHPATH
4591 1
4592#else
4593 0
4594#endif
4595 },
4596 {"filterpipe",
4597#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4598 1
4599#else
4600 0
4601#endif
4602 },
4603 {"find_in_path",
4604#ifdef FEAT_FIND_ID
4605 1
4606#else
4607 0
4608#endif
4609 },
4610 {"float",
4611#ifdef FEAT_FLOAT
4612 1
4613#else
4614 0
4615#endif
4616 },
4617 {"folding",
4618#ifdef FEAT_FOLDING
4619 1
4620#else
4621 0
4622#endif
4623 },
4624 {"footer",
4625#ifdef FEAT_FOOTER
4626 1
4627#else
4628 0
4629#endif
4630 },
4631 {"fork",
4632#if !defined(USE_SYSTEM) && defined(UNIX)
4633 1
4634#else
4635 0
4636#endif
4637 },
4638 {"gettext",
4639#ifdef FEAT_GETTEXT
4640 1
4641#else
4642 0
4643#endif
4644 },
4645 {"gui",
4646#ifdef FEAT_GUI
4647 1
4648#else
4649 0
4650#endif
4651 },
4652 {"gui_neXtaw",
4653#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4654 1
4655#else
4656 0
4657#endif
4658 },
4659 {"gui_athena",
4660#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4661 1
4662#else
4663 0
4664#endif
4665 },
4666 {"gui_gtk",
4667#ifdef FEAT_GUI_GTK
4668 1
4669#else
4670 0
4671#endif
4672 },
4673 {"gui_gtk2",
4674#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4675 1
4676#else
4677 0
4678#endif
4679 },
4680 {"gui_gtk3",
4681#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4682 1
4683#else
4684 0
4685#endif
4686 },
4687 {"gui_gnome",
4688#ifdef FEAT_GUI_GNOME
4689 1
4690#else
4691 0
4692#endif
4693 },
4694 {"gui_haiku",
4695#ifdef FEAT_GUI_HAIKU
4696 1
4697#else
4698 0
4699#endif
4700 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004701 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004702 {"gui_motif",
4703#ifdef FEAT_GUI_MOTIF
4704 1
4705#else
4706 0
4707#endif
4708 },
4709 {"gui_photon",
4710#ifdef FEAT_GUI_PHOTON
4711 1
4712#else
4713 0
4714#endif
4715 },
4716 {"gui_win32",
4717#ifdef FEAT_GUI_MSWIN
4718 1
4719#else
4720 0
4721#endif
4722 },
4723 {"iconv",
4724#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4725 1
4726#else
4727 0
4728#endif
4729 },
4730 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004731 {"ipv6",
4732#ifdef FEAT_IPV6
4733 1
4734#else
4735 0
4736#endif
4737 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004738 {"job",
4739#ifdef FEAT_JOB_CHANNEL
4740 1
4741#else
4742 0
4743#endif
4744 },
4745 {"jumplist",
4746#ifdef FEAT_JUMPLIST
4747 1
4748#else
4749 0
4750#endif
4751 },
4752 {"keymap",
4753#ifdef FEAT_KEYMAP
4754 1
4755#else
4756 0
4757#endif
4758 },
4759 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4760 {"langmap",
4761#ifdef FEAT_LANGMAP
4762 1
4763#else
4764 0
4765#endif
4766 },
4767 {"libcall",
4768#ifdef FEAT_LIBCALL
4769 1
4770#else
4771 0
4772#endif
4773 },
4774 {"linebreak",
4775#ifdef FEAT_LINEBREAK
4776 1
4777#else
4778 0
4779#endif
4780 },
4781 {"lispindent",
4782#ifdef FEAT_LISP
4783 1
4784#else
4785 0
4786#endif
4787 },
4788 {"listcmds", 1},
4789 {"localmap", 1},
4790 {"lua",
4791#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4792 1
4793#else
4794 0
4795#endif
4796 },
4797 {"menu",
4798#ifdef FEAT_MENU
4799 1
4800#else
4801 0
4802#endif
4803 },
4804 {"mksession",
4805#ifdef FEAT_SESSION
4806 1
4807#else
4808 0
4809#endif
4810 },
4811 {"modify_fname", 1},
4812 {"mouse", 1},
4813 {"mouseshape",
4814#ifdef FEAT_MOUSESHAPE
4815 1
4816#else
4817 0
4818#endif
4819 },
4820 {"mouse_dec",
4821#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4822 1
4823#else
4824 0
4825#endif
4826 },
4827 {"mouse_gpm",
4828#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4829 1
4830#else
4831 0
4832#endif
4833 },
4834 {"mouse_jsbterm",
4835#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4836 1
4837#else
4838 0
4839#endif
4840 },
4841 {"mouse_netterm",
4842#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4843 1
4844#else
4845 0
4846#endif
4847 },
4848 {"mouse_pterm",
4849#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4850 1
4851#else
4852 0
4853#endif
4854 },
4855 {"mouse_sgr",
4856#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4857 1
4858#else
4859 0
4860#endif
4861 },
4862 {"mouse_sysmouse",
4863#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4864 1
4865#else
4866 0
4867#endif
4868 },
4869 {"mouse_urxvt",
4870#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4871 1
4872#else
4873 0
4874#endif
4875 },
4876 {"mouse_xterm",
4877#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4878 1
4879#else
4880 0
4881#endif
4882 },
4883 {"multi_byte", 1},
4884 {"multi_byte_ime",
4885#ifdef FEAT_MBYTE_IME
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"multi_lang",
4892#ifdef FEAT_MULTI_LANG
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"mzscheme",
4899#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4900 1
4901#else
4902 0
4903#endif
4904 },
4905 {"num64", 1},
4906 {"ole",
4907#ifdef FEAT_OLE
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"packages",
4914#ifdef FEAT_EVAL
4915 1
4916#else
4917 0
4918#endif
4919 },
4920 {"path_extra",
4921#ifdef FEAT_PATH_EXTRA
4922 1
4923#else
4924 0
4925#endif
4926 },
4927 {"perl",
4928#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4929 1
4930#else
4931 0
4932#endif
4933 },
4934 {"persistent_undo",
4935#ifdef FEAT_PERSISTENT_UNDO
4936 1
4937#else
4938 0
4939#endif
4940 },
4941 {"python_compiled",
4942#if defined(FEAT_PYTHON)
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"python_dynamic",
4949#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"python",
4956#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"pythonx",
4963#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4964 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4965 1
4966#else
4967 0
4968#endif
4969 },
4970 {"python3_compiled",
4971#if defined(FEAT_PYTHON3)
4972 1
4973#else
4974 0
4975#endif
4976 },
4977 {"python3_dynamic",
4978#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4979 1
4980#else
4981 0
4982#endif
4983 },
4984 {"python3",
4985#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4986 1
4987#else
4988 0
4989#endif
4990 },
4991 {"popupwin",
4992#ifdef FEAT_PROP_POPUP
4993 1
4994#else
4995 0
4996#endif
4997 },
4998 {"postscript",
4999#ifdef FEAT_POSTSCRIPT
5000 1
5001#else
5002 0
5003#endif
5004 },
5005 {"printer",
5006#ifdef FEAT_PRINTER
5007 1
5008#else
5009 0
5010#endif
5011 },
5012 {"profile",
5013#ifdef FEAT_PROFILE
5014 1
5015#else
5016 0
5017#endif
5018 },
5019 {"reltime",
5020#ifdef FEAT_RELTIME
5021 1
5022#else
5023 0
5024#endif
5025 },
5026 {"quickfix",
5027#ifdef FEAT_QUICKFIX
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"rightleft",
5034#ifdef FEAT_RIGHTLEFT
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"ruby",
5041#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"scrollbind", 1},
5048 {"showcmd",
5049#ifdef FEAT_CMDL_INFO
5050 1
5051#else
5052 0
5053#endif
5054 },
5055 {"cmdline_info",
5056#ifdef FEAT_CMDL_INFO
5057 1
5058#else
5059 0
5060#endif
5061 },
5062 {"signs",
5063#ifdef FEAT_SIGNS
5064 1
5065#else
5066 0
5067#endif
5068 },
5069 {"smartindent",
5070#ifdef FEAT_SMARTINDENT
5071 1
5072#else
5073 0
5074#endif
5075 },
5076 {"startuptime",
5077#ifdef STARTUPTIME
5078 1
5079#else
5080 0
5081#endif
5082 },
5083 {"statusline",
5084#ifdef FEAT_STL_OPT
5085 1
5086#else
5087 0
5088#endif
5089 },
5090 {"netbeans_intg",
5091#ifdef FEAT_NETBEANS_INTG
5092 1
5093#else
5094 0
5095#endif
5096 },
Christian Brabandtf573c6e2021-06-20 14:02:16 +02005097 {"sodium",
5098#ifdef FEAT_SODIUM
5099 1
5100#else
5101 0
5102#endif
5103 },
Bram Moolenaar79296512020-03-22 16:17:14 +01005104 {"sound",
5105#ifdef FEAT_SOUND
5106 1
5107#else
5108 0
5109#endif
5110 },
5111 {"spell",
5112#ifdef FEAT_SPELL
5113 1
5114#else
5115 0
5116#endif
5117 },
5118 {"syntax",
5119#ifdef FEAT_SYN_HL
5120 1
5121#else
5122 0
5123#endif
5124 },
5125 {"system",
5126#if defined(USE_SYSTEM) || !defined(UNIX)
5127 1
5128#else
5129 0
5130#endif
5131 },
5132 {"tag_binary",
5133#ifdef FEAT_TAG_BINS
5134 1
5135#else
5136 0
5137#endif
5138 },
5139 {"tcl",
5140#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5141 1
5142#else
5143 0
5144#endif
5145 },
5146 {"termguicolors",
5147#ifdef FEAT_TERMGUICOLORS
5148 1
5149#else
5150 0
5151#endif
5152 },
5153 {"terminal",
5154#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5155 1
5156#else
5157 0
5158#endif
5159 },
5160 {"terminfo",
5161#ifdef TERMINFO
5162 1
5163#else
5164 0
5165#endif
5166 },
5167 {"termresponse",
5168#ifdef FEAT_TERMRESPONSE
5169 1
5170#else
5171 0
5172#endif
5173 },
5174 {"textobjects",
5175#ifdef FEAT_TEXTOBJ
5176 1
5177#else
5178 0
5179#endif
5180 },
5181 {"textprop",
5182#ifdef FEAT_PROP_POPUP
5183 1
5184#else
5185 0
5186#endif
5187 },
5188 {"tgetent",
5189#ifdef HAVE_TGETENT
5190 1
5191#else
5192 0
5193#endif
5194 },
5195 {"timers",
5196#ifdef FEAT_TIMERS
5197 1
5198#else
5199 0
5200#endif
5201 },
5202 {"title",
5203#ifdef FEAT_TITLE
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"toolbar",
5210#ifdef FEAT_TOOLBAR
5211 1
5212#else
5213 0
5214#endif
5215 },
5216 {"unnamedplus",
5217#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"user-commands", 1}, // was accidentally included in 5.4
5224 {"user_commands", 1},
5225 {"vartabs",
5226#ifdef FEAT_VARTABS
5227 1
5228#else
5229 0
5230#endif
5231 },
5232 {"vertsplit", 1},
5233 {"viminfo",
5234#ifdef FEAT_VIMINFO
5235 1
5236#else
5237 0
5238#endif
5239 },
5240 {"vimscript-1", 1},
5241 {"vimscript-2", 1},
5242 {"vimscript-3", 1},
5243 {"vimscript-4", 1},
5244 {"virtualedit", 1},
5245 {"visual", 1},
5246 {"visualextra", 1},
5247 {"vreplace", 1},
5248 {"vtp",
5249#ifdef FEAT_VTP
5250 1
5251#else
5252 0
5253#endif
5254 },
5255 {"wildignore",
5256#ifdef FEAT_WILDIGN
5257 1
5258#else
5259 0
5260#endif
5261 },
5262 {"wildmenu",
5263#ifdef FEAT_WILDMENU
5264 1
5265#else
5266 0
5267#endif
5268 },
5269 {"windows", 1},
5270 {"winaltkeys",
5271#ifdef FEAT_WAK
5272 1
5273#else
5274 0
5275#endif
5276 },
5277 {"writebackup",
5278#ifdef FEAT_WRITEBACKUP
5279 1
5280#else
5281 0
5282#endif
5283 },
5284 {"xim",
5285#ifdef FEAT_XIM
5286 1
5287#else
5288 0
5289#endif
5290 },
5291 {"xfontset",
5292#ifdef FEAT_XFONTSET
5293 1
5294#else
5295 0
5296#endif
5297 },
5298 {"xpm",
5299#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5300 1
5301#else
5302 0
5303#endif
5304 },
5305 {"xpm_w32", // for backward compatibility
5306#ifdef FEAT_XPM_W32
5307 1
5308#else
5309 0
5310#endif
5311 },
5312 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005314 1
5315#else
5316 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005318 },
5319 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005320#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005321 1
5322#else
5323 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005325 },
5326 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005328 1
5329#else
5330 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005332 },
5333 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005335 1
5336#else
5337 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005339 },
5340 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005342 1
5343#else
5344 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005346 },
5347 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005348 };
5349
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005350 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005351 for (i = 0; has_list[i].name != NULL; ++i)
5352 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005354 x = TRUE;
5355 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356 break;
5357 }
5358
Bram Moolenaar79296512020-03-22 16:17:14 +01005359 // features also in has_list[] but sometimes enabled at runtime
5360 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005361 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005362 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005363 {
5364 // intentionally empty
5365 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005366#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367 else if (STRICMP(name, "balloon_multiline") == 0)
5368 n = multiline_balloon_available();
5369#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005370#ifdef VIMDLL
5371 else if (STRICMP(name, "filterpipe") == 0)
5372 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373#endif
5374#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5375 else if (STRICMP(name, "iconv") == 0)
5376 n = iconv_enabled(FALSE);
5377#endif
5378#ifdef DYNAMIC_LUA
5379 else if (STRICMP(name, "lua") == 0)
5380 n = lua_enabled(FALSE);
5381#endif
5382#ifdef DYNAMIC_MZSCHEME
5383 else if (STRICMP(name, "mzscheme") == 0)
5384 n = mzscheme_enabled(FALSE);
5385#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005386#ifdef DYNAMIC_PERL
5387 else if (STRICMP(name, "perl") == 0)
5388 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005389#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390#ifdef DYNAMIC_PYTHON
5391 else if (STRICMP(name, "python") == 0)
5392 n = python_enabled(FALSE);
5393#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394#ifdef DYNAMIC_PYTHON3
5395 else if (STRICMP(name, "python3") == 0)
5396 n = python3_enabled(FALSE);
5397#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005398#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5399 else if (STRICMP(name, "pythonx") == 0)
5400 {
5401# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5402 if (p_pyx == 0)
5403 n = python3_enabled(FALSE) || python_enabled(FALSE);
5404 else if (p_pyx == 3)
5405 n = python3_enabled(FALSE);
5406 else if (p_pyx == 2)
5407 n = python_enabled(FALSE);
5408# elif defined(DYNAMIC_PYTHON)
5409 n = python_enabled(FALSE);
5410# elif defined(DYNAMIC_PYTHON3)
5411 n = python3_enabled(FALSE);
5412# endif
5413 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005414#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005415#ifdef DYNAMIC_RUBY
5416 else if (STRICMP(name, "ruby") == 0)
5417 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005419#ifdef DYNAMIC_TCL
5420 else if (STRICMP(name, "tcl") == 0)
5421 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005422#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005423#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005424 else if (STRICMP(name, "terminal") == 0)
5425 n = terminal_enabled();
5426#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005427 }
5428
Bram Moolenaar79296512020-03-22 16:17:14 +01005429 // features not in has_list[]
5430 if (x == FALSE)
5431 {
5432 if (STRNICMP(name, "patch", 5) == 0)
5433 {
5434 x = TRUE;
5435 if (name[5] == '-'
5436 && STRLEN(name) >= 11
5437 && vim_isdigit(name[6])
5438 && vim_isdigit(name[8])
5439 && vim_isdigit(name[10]))
5440 {
5441 int major = atoi((char *)name + 6);
5442 int minor = atoi((char *)name + 8);
5443
5444 // Expect "patch-9.9.01234".
5445 n = (major < VIM_VERSION_MAJOR
5446 || (major == VIM_VERSION_MAJOR
5447 && (minor < VIM_VERSION_MINOR
5448 || (minor == VIM_VERSION_MINOR
5449 && has_patch(atoi((char *)name + 10))))));
5450 }
5451 else
5452 n = has_patch(atoi((char *)name + 5));
5453 }
5454 else if (STRICMP(name, "vim_starting") == 0)
5455 {
5456 x = TRUE;
5457 n = (starting != 0);
5458 }
5459 else if (STRICMP(name, "ttyin") == 0)
5460 {
5461 x = TRUE;
5462 n = mch_input_isatty();
5463 }
5464 else if (STRICMP(name, "ttyout") == 0)
5465 {
5466 x = TRUE;
5467 n = stdout_isatty;
5468 }
5469 else if (STRICMP(name, "multi_byte_encoding") == 0)
5470 {
5471 x = TRUE;
5472 n = has_mbyte;
5473 }
5474 else if (STRICMP(name, "gui_running") == 0)
5475 {
5476 x = TRUE;
5477#ifdef FEAT_GUI
5478 n = (gui.in_use || gui.starting);
5479#endif
5480 }
5481 else if (STRICMP(name, "browse") == 0)
5482 {
5483 x = TRUE;
5484#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5485 n = gui.in_use; // gui_mch_browse() works when GUI is running
5486#endif
5487 }
5488 else if (STRICMP(name, "syntax_items") == 0)
5489 {
5490 x = TRUE;
5491#ifdef FEAT_SYN_HL
5492 n = syntax_present(curwin);
5493#endif
5494 }
5495 else if (STRICMP(name, "vcon") == 0)
5496 {
5497 x = TRUE;
5498#ifdef FEAT_VTP
5499 n = is_term_win32() && has_vtp_working();
5500#endif
5501 }
5502 else if (STRICMP(name, "netbeans_enabled") == 0)
5503 {
5504 x = TRUE;
5505#ifdef FEAT_NETBEANS_INTG
5506 n = netbeans_active();
5507#endif
5508 }
5509 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5510 {
5511 x = TRUE;
5512#ifdef FEAT_MOUSE_GPM
5513 n = gpm_enabled();
5514#endif
5515 }
5516 else if (STRICMP(name, "conpty") == 0)
5517 {
5518 x = TRUE;
5519#if defined(FEAT_TERMINAL) && defined(MSWIN)
5520 n = use_conpty();
5521#endif
5522 }
5523 else if (STRICMP(name, "clipboard_working") == 0)
5524 {
5525 x = TRUE;
5526#ifdef FEAT_CLIPBOARD
5527 n = clip_star.available;
5528#endif
5529 }
5530 }
5531
Bram Moolenaar04637e22020-09-05 18:45:29 +02005532 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005533 // return whether feature could ever be enabled
5534 rettv->vval.v_number = x;
5535 else
5536 // return whether feature is enabled
5537 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538}
5539
5540/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005541 * Return TRUE if "feature" can change later.
5542 * Also when checking for the feature has side effects, such as loading a DLL.
5543 */
5544 int
5545dynamic_feature(char_u *feature)
5546{
5547 return (feature == NULL
5548#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5549 || STRICMP(feature, "balloon_multiline") == 0
5550#endif
5551#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5552 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5553#endif
5554#ifdef VIMDLL
5555 || STRICMP(feature, "filterpipe") == 0
5556#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005557#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005558 // this can only change on Unix where the ":gui" command could be
5559 // used.
5560 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5561#endif
5562#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5563 || STRICMP(feature, "iconv") == 0
5564#endif
5565#ifdef DYNAMIC_LUA
5566 || STRICMP(feature, "lua") == 0
5567#endif
5568#ifdef FEAT_MOUSE_GPM
5569 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5570#endif
5571#ifdef DYNAMIC_MZSCHEME
5572 || STRICMP(feature, "mzscheme") == 0
5573#endif
5574#ifdef FEAT_NETBEANS_INTG
5575 || STRICMP(feature, "netbeans_enabled") == 0
5576#endif
5577#ifdef DYNAMIC_PERL
5578 || STRICMP(feature, "perl") == 0
5579#endif
5580#ifdef DYNAMIC_PYTHON
5581 || STRICMP(feature, "python") == 0
5582#endif
5583#ifdef DYNAMIC_PYTHON3
5584 || STRICMP(feature, "python3") == 0
5585#endif
5586#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5587 || STRICMP(feature, "pythonx") == 0
5588#endif
5589#ifdef DYNAMIC_RUBY
5590 || STRICMP(feature, "ruby") == 0
5591#endif
5592#ifdef FEAT_SYN_HL
5593 || STRICMP(feature, "syntax_items") == 0
5594#endif
5595#ifdef DYNAMIC_TCL
5596 || STRICMP(feature, "tcl") == 0
5597#endif
5598 // once "starting" is zero it will stay that way
5599 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5600 || STRICMP(feature, "multi_byte_encoding") == 0
5601#if defined(FEAT_TERMINAL) && defined(MSWIN)
5602 || STRICMP(feature, "conpty") == 0
5603#endif
5604 );
5605}
5606
5607/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 * "haslocaldir()" function
5609 */
5610 static void
5611f_haslocaldir(typval_T *argvars, typval_T *rettv)
5612{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005613 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 win_T *wp = NULL;
5615
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005616 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5617
5618 // Check for window-local and tab-local directories
5619 if (wp != NULL && wp->w_localdir != NULL)
5620 rettv->vval.v_number = 1;
5621 else if (tp != NULL && tp->tp_localdir != NULL)
5622 rettv->vval.v_number = 2;
5623 else
5624 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625}
5626
5627/*
5628 * "hasmapto()" function
5629 */
5630 static void
5631f_hasmapto(typval_T *argvars, typval_T *rettv)
5632{
5633 char_u *name;
5634 char_u *mode;
5635 char_u buf[NUMBUFLEN];
5636 int abbr = FALSE;
5637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005638 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639 if (argvars[1].v_type == VAR_UNKNOWN)
5640 mode = (char_u *)"nvo";
5641 else
5642 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005643 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005644 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005645 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 }
5647
5648 if (map_to_exists(name, mode, abbr))
5649 rettv->vval.v_number = TRUE;
5650 else
5651 rettv->vval.v_number = FALSE;
5652}
5653
5654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005655 * "highlightID(name)" function
5656 */
5657 static void
5658f_hlID(typval_T *argvars, typval_T *rettv)
5659{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005661}
5662
5663/*
5664 * "highlight_exists()" function
5665 */
5666 static void
5667f_hlexists(typval_T *argvars, typval_T *rettv)
5668{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005669 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005670}
5671
5672/*
5673 * "hostname()" function
5674 */
5675 static void
5676f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5677{
5678 char_u hostname[256];
5679
5680 mch_get_host_name(hostname, 256);
5681 rettv->v_type = VAR_STRING;
5682 rettv->vval.v_string = vim_strsave(hostname);
5683}
5684
5685/*
5686 * iconv() function
5687 */
5688 static void
5689f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5690{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005691 char_u buf1[NUMBUFLEN];
5692 char_u buf2[NUMBUFLEN];
5693 char_u *from, *to, *str;
5694 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695
5696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = NULL;
5698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005699 str = tv_get_string(&argvars[0]);
5700 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5701 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 vimconv.vc_type = CONV_NONE;
5703 convert_setup(&vimconv, from, to);
5704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005705 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706 if (vimconv.vc_type == CONV_NONE)
5707 rettv->vval.v_string = vim_strsave(str);
5708 else
5709 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5710
5711 convert_setup(&vimconv, NULL, NULL);
5712 vim_free(from);
5713 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714}
5715
5716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 * "index()" function
5718 */
5719 static void
5720f_index(typval_T *argvars, typval_T *rettv)
5721{
5722 list_T *l;
5723 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005724 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 long idx = 0;
5726 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005727 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728
5729 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005730 if (argvars[0].v_type == VAR_BLOB)
5731 {
5732 typval_T tv;
5733 int start = 0;
5734
5735 if (argvars[2].v_type != VAR_UNKNOWN)
5736 {
5737 start = tv_get_number_chk(&argvars[2], &error);
5738 if (error)
5739 return;
5740 }
5741 b = argvars[0].vval.v_blob;
5742 if (b == NULL)
5743 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005744 if (start < 0)
5745 {
5746 start = blob_len(b) + start;
5747 if (start < 0)
5748 start = 0;
5749 }
5750
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005751 for (idx = start; idx < blob_len(b); ++idx)
5752 {
5753 tv.v_type = VAR_NUMBER;
5754 tv.vval.v_number = blob_get(b, idx);
5755 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5756 {
5757 rettv->vval.v_number = idx;
5758 return;
5759 }
5760 }
5761 return;
5762 }
5763 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005765 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766 return;
5767 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005768
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769 l = argvars[0].vval.v_list;
5770 if (l != NULL)
5771 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005772 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773 item = l->lv_first;
5774 if (argvars[2].v_type != VAR_UNKNOWN)
5775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005776 // Start at specified item. Use the cached index that list_find()
5777 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005778 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005779 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005781 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 if (error)
5783 item = NULL;
5784 }
5785
5786 for ( ; item != NULL; item = item->li_next, ++idx)
5787 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5788 {
5789 rettv->vval.v_number = idx;
5790 break;
5791 }
5792 }
5793}
5794
5795static int inputsecret_flag = 0;
5796
5797/*
5798 * "input()" function
5799 * Also handles inputsecret() when inputsecret is set.
5800 */
5801 static void
5802f_input(typval_T *argvars, typval_T *rettv)
5803{
5804 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5805}
5806
5807/*
5808 * "inputdialog()" function
5809 */
5810 static void
5811f_inputdialog(typval_T *argvars, typval_T *rettv)
5812{
5813#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005815 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5816 {
5817 char_u *message;
5818 char_u buf[NUMBUFLEN];
5819 char_u *defstr = (char_u *)"";
5820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005821 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005822 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005823 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5825 else
5826 IObuff[0] = NUL;
5827 if (message != NULL && defstr != NULL
5828 && do_dialog(VIM_QUESTION, NULL, message,
5829 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5830 rettv->vval.v_string = vim_strsave(IObuff);
5831 else
5832 {
5833 if (message != NULL && defstr != NULL
5834 && argvars[1].v_type != VAR_UNKNOWN
5835 && argvars[2].v_type != VAR_UNKNOWN)
5836 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 else
5839 rettv->vval.v_string = NULL;
5840 }
5841 rettv->v_type = VAR_STRING;
5842 }
5843 else
5844#endif
5845 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5846}
5847
5848/*
5849 * "inputlist()" function
5850 */
5851 static void
5852f_inputlist(typval_T *argvars, typval_T *rettv)
5853{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005854 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855 listitem_T *li;
5856 int selected;
5857 int mouse_used;
5858
5859#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005860 // While starting up, there is no place to enter text. When running tests
5861 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005862 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 return;
5864#endif
5865 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005867 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868 return;
5869 }
5870
5871 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 msg_row = Rows - 1; // for when 'cmdheight' > 1
5873 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005874 msg_scroll = TRUE;
5875 msg_clr_eos();
5876
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005877 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005878 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005879 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005881 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 msg_putchar('\n');
5883 }
5884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005885 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 selected = prompt_for_number(&mouse_used);
5887 if (mouse_used)
5888 selected -= lines_left;
5889
5890 rettv->vval.v_number = selected;
5891}
5892
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005893static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5894
5895/*
5896 * "inputrestore()" function
5897 */
5898 static void
5899f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5900{
5901 if (ga_userinput.ga_len > 0)
5902 {
5903 --ga_userinput.ga_len;
5904 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005905 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005906 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005907 }
5908 else if (p_verbose > 1)
5909 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005910 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005911 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005912 }
5913}
5914
5915/*
5916 * "inputsave()" function
5917 */
5918 static void
5919f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5920{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005922 if (ga_grow(&ga_userinput, 1) == OK)
5923 {
5924 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5925 + ga_userinput.ga_len);
5926 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 }
5929 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931}
5932
5933/*
5934 * "inputsecret()" function
5935 */
5936 static void
5937f_inputsecret(typval_T *argvars, typval_T *rettv)
5938{
5939 ++cmdline_star;
5940 ++inputsecret_flag;
5941 f_input(argvars, rettv);
5942 --cmdline_star;
5943 --inputsecret_flag;
5944}
5945
5946/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005947 * "interrupt()" function
5948 */
5949 static void
5950f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5951{
5952 got_int = TRUE;
5953}
5954
5955/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 * "invert(expr)" function
5957 */
5958 static void
5959f_invert(typval_T *argvars, typval_T *rettv)
5960{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005961 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005962}
5963
5964/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 * "islocked()" function
5966 */
5967 static void
5968f_islocked(typval_T *argvars, typval_T *rettv)
5969{
5970 lval_T lv;
5971 char_u *end;
5972 dictitem_T *di;
5973
5974 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005975 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005976 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 if (end != NULL && lv.ll_name != NULL)
5978 {
5979 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005980 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981 else
5982 {
5983 if (lv.ll_tv == NULL)
5984 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005985 di = find_var(lv.ll_name, NULL, TRUE);
5986 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005988 // Consider a variable locked when:
5989 // 1. the variable itself is locked
5990 // 2. the value of the variable is locked.
5991 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005992 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5993 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994 }
5995 }
5996 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005997 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005998 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005999 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006001 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6003 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6006 }
6007 }
6008
6009 clear_lval(&lv);
6010}
6011
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 * "last_buffer_nr()" function.
6014 */
6015 static void
6016f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6017{
6018 int n = 0;
6019 buf_T *buf;
6020
Bram Moolenaar29323592016-07-24 22:04:11 +02006021 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 if (n < buf->b_fnum)
6023 n = buf->b_fnum;
6024
6025 rettv->vval.v_number = n;
6026}
6027
6028/*
6029 * "len()" function
6030 */
6031 static void
6032f_len(typval_T *argvars, typval_T *rettv)
6033{
6034 switch (argvars[0].v_type)
6035 {
6036 case VAR_STRING:
6037 case VAR_NUMBER:
6038 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006039 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006041 case VAR_BLOB:
6042 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6043 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 case VAR_LIST:
6045 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6046 break;
6047 case VAR_DICT:
6048 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6049 break;
6050 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006051 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006053 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 case VAR_SPECIAL:
6055 case VAR_FLOAT:
6056 case VAR_FUNC:
6057 case VAR_PARTIAL:
6058 case VAR_JOB:
6059 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006060 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006061 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 break;
6063 }
6064}
6065
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006067libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068{
6069#ifdef FEAT_LIBCALL
6070 char_u *string_in;
6071 char_u **string_result;
6072 int nr_result;
6073#endif
6074
6075 rettv->v_type = type;
6076 if (type != VAR_NUMBER)
6077 rettv->vval.v_string = NULL;
6078
6079 if (check_restricted() || check_secure())
6080 return;
6081
6082#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006083 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6085 {
6086 string_in = NULL;
6087 if (argvars[2].v_type == VAR_STRING)
6088 string_in = argvars[2].vval.v_string;
6089 if (type == VAR_NUMBER)
6090 string_result = NULL;
6091 else
6092 string_result = &rettv->vval.v_string;
6093 if (mch_libcall(argvars[0].vval.v_string,
6094 argvars[1].vval.v_string,
6095 string_in,
6096 argvars[2].vval.v_number,
6097 string_result,
6098 &nr_result) == OK
6099 && type == VAR_NUMBER)
6100 rettv->vval.v_number = nr_result;
6101 }
6102#endif
6103}
6104
6105/*
6106 * "libcall()" function
6107 */
6108 static void
6109f_libcall(typval_T *argvars, typval_T *rettv)
6110{
6111 libcall_common(argvars, rettv, VAR_STRING);
6112}
6113
6114/*
6115 * "libcallnr()" function
6116 */
6117 static void
6118f_libcallnr(typval_T *argvars, typval_T *rettv)
6119{
6120 libcall_common(argvars, rettv, VAR_NUMBER);
6121}
6122
6123/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006124 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006125 */
6126 static void
6127f_line(typval_T *argvars, typval_T *rettv)
6128{
6129 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006130 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006132 int id;
6133 tabpage_T *tp;
6134 win_T *wp;
6135 win_T *save_curwin;
6136 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006138 if (argvars[1].v_type != VAR_UNKNOWN)
6139 {
6140 // use window specified in the second argument
6141 id = (int)tv_get_number(&argvars[1]);
6142 wp = win_id2wp_tp(id, &tp);
6143 if (wp != NULL && tp != NULL)
6144 {
6145 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6146 == OK)
6147 {
6148 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006149 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006150 }
6151 restore_win_noblock(save_curwin, save_curtab, TRUE);
6152 }
6153 }
6154 else
6155 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006156 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158 if (fp != NULL)
6159 lnum = fp->lnum;
6160 rettv->vval.v_number = lnum;
6161}
6162
6163/*
6164 * "line2byte(lnum)" function
6165 */
6166 static void
6167f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6168{
6169#ifndef FEAT_BYTEOFF
6170 rettv->vval.v_number = -1;
6171#else
6172 linenr_T lnum;
6173
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006174 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6176 rettv->vval.v_number = -1;
6177 else
6178 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6179 if (rettv->vval.v_number >= 0)
6180 ++rettv->vval.v_number;
6181#endif
6182}
6183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184#ifdef FEAT_LUA
6185/*
6186 * "luaeval()" function
6187 */
6188 static void
6189f_luaeval(typval_T *argvars, typval_T *rettv)
6190{
6191 char_u *str;
6192 char_u buf[NUMBUFLEN];
6193
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006194 if (check_restricted() || check_secure())
6195 return;
6196
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006197 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006198 do_luaeval(str, argvars + 1, rettv);
6199}
6200#endif
6201
6202/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203 * "maparg()" function
6204 */
6205 static void
6206f_maparg(typval_T *argvars, typval_T *rettv)
6207{
6208 get_maparg(argvars, rettv, TRUE);
6209}
6210
6211/*
6212 * "mapcheck()" function
6213 */
6214 static void
6215f_mapcheck(typval_T *argvars, typval_T *rettv)
6216{
6217 get_maparg(argvars, rettv, FALSE);
6218}
6219
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006220typedef enum
6221{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006222 MATCH_END, // matchend()
6223 MATCH_MATCH, // match()
6224 MATCH_STR, // matchstr()
6225 MATCH_LIST, // matchlist()
6226 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006227} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228
6229 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006230find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231{
6232 char_u *str = NULL;
6233 long len = 0;
6234 char_u *expr = NULL;
6235 char_u *pat;
6236 regmatch_T regmatch;
6237 char_u patbuf[NUMBUFLEN];
6238 char_u strbuf[NUMBUFLEN];
6239 char_u *save_cpo;
6240 long start = 0;
6241 long nth = 1;
6242 colnr_T startcol = 0;
6243 int match = 0;
6244 list_T *l = NULL;
6245 listitem_T *li = NULL;
6246 long idx = 0;
6247 char_u *tofree = NULL;
6248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006251 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252
6253 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006254 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006256 // type MATCH_LIST: return empty list when there are no matches.
6257 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 if (rettv_list_alloc(rettv) == FAIL)
6259 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006260 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 && (list_append_string(rettv->vval.v_list,
6262 (char_u *)"", 0) == FAIL
6263 || list_append_number(rettv->vval.v_list,
6264 (varnumber_T)-1) == 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 {
6270 list_free(rettv->vval.v_list);
6271 rettv->vval.v_list = NULL;
6272 goto theend;
6273 }
6274 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006275 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 {
6277 rettv->v_type = VAR_STRING;
6278 rettv->vval.v_string = NULL;
6279 }
6280
6281 if (argvars[0].v_type == VAR_LIST)
6282 {
6283 if ((l = argvars[0].vval.v_list) == NULL)
6284 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006285 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 li = l->lv_first;
6287 }
6288 else
6289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006290 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 len = (long)STRLEN(str);
6292 }
6293
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006294 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 if (pat == NULL)
6296 goto theend;
6297
6298 if (argvars[2].v_type != VAR_UNKNOWN)
6299 {
6300 int error = FALSE;
6301
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006302 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 if (error)
6304 goto theend;
6305 if (l != NULL)
6306 {
6307 li = list_find(l, start);
6308 if (li == NULL)
6309 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006310 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311 }
6312 else
6313 {
6314 if (start < 0)
6315 start = 0;
6316 if (start > len)
6317 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006318 // When "count" argument is there ignore matches before "start",
6319 // otherwise skip part of the string. Differs when pattern is "^"
6320 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 if (argvars[3].v_type != VAR_UNKNOWN)
6322 startcol = start;
6323 else
6324 {
6325 str += start;
6326 len -= start;
6327 }
6328 }
6329
6330 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332 if (error)
6333 goto theend;
6334 }
6335
6336 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6337 if (regmatch.regprog != NULL)
6338 {
6339 regmatch.rm_ic = p_ic;
6340
6341 for (;;)
6342 {
6343 if (l != NULL)
6344 {
6345 if (li == NULL)
6346 {
6347 match = FALSE;
6348 break;
6349 }
6350 vim_free(tofree);
6351 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6352 if (str == NULL)
6353 break;
6354 }
6355
6356 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6357
6358 if (match && --nth <= 0)
6359 break;
6360 if (l == NULL && !match)
6361 break;
6362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006363 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364 if (l != NULL)
6365 {
6366 li = li->li_next;
6367 ++idx;
6368 }
6369 else
6370 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 startcol = (colnr_T)(regmatch.startp[0]
6372 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 if (startcol > (colnr_T)len
6374 || str + startcol <= regmatch.startp[0])
6375 {
6376 match = FALSE;
6377 break;
6378 }
6379 }
6380 }
6381
6382 if (match)
6383 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006384 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385 {
6386 listitem_T *li1 = rettv->vval.v_list->lv_first;
6387 listitem_T *li2 = li1->li_next;
6388 listitem_T *li3 = li2->li_next;
6389 listitem_T *li4 = li3->li_next;
6390
6391 vim_free(li1->li_tv.vval.v_string);
6392 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006393 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394 li3->li_tv.vval.v_number =
6395 (varnumber_T)(regmatch.startp[0] - expr);
6396 li4->li_tv.vval.v_number =
6397 (varnumber_T)(regmatch.endp[0] - expr);
6398 if (l != NULL)
6399 li2->li_tv.vval.v_number = (varnumber_T)idx;
6400 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006401 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 {
6403 int i;
6404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006405 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 for (i = 0; i < NSUBEXP; ++i)
6407 {
6408 if (regmatch.endp[i] == NULL)
6409 {
6410 if (list_append_string(rettv->vval.v_list,
6411 (char_u *)"", 0) == FAIL)
6412 break;
6413 }
6414 else if (list_append_string(rettv->vval.v_list,
6415 regmatch.startp[i],
6416 (int)(regmatch.endp[i] - regmatch.startp[i]))
6417 == FAIL)
6418 break;
6419 }
6420 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006421 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006423 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424 if (l != NULL)
6425 copy_tv(&li->li_tv, rettv);
6426 else
6427 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006428 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 }
6430 else if (l != NULL)
6431 rettv->vval.v_number = idx;
6432 else
6433 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006434 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 rettv->vval.v_number =
6436 (varnumber_T)(regmatch.startp[0] - str);
6437 else
6438 rettv->vval.v_number =
6439 (varnumber_T)(regmatch.endp[0] - str);
6440 rettv->vval.v_number += (varnumber_T)(str - expr);
6441 }
6442 }
6443 vim_regfree(regmatch.regprog);
6444 }
6445
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006446theend:
6447 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449 listitem_remove(rettv->vval.v_list,
6450 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451 vim_free(tofree);
6452 p_cpo = save_cpo;
6453}
6454
6455/*
6456 * "match()" function
6457 */
6458 static void
6459f_match(typval_T *argvars, typval_T *rettv)
6460{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006461 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006462}
6463
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006464/*
6465 * "matchend()" function
6466 */
6467 static void
6468f_matchend(typval_T *argvars, typval_T *rettv)
6469{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006470 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471}
6472
6473/*
6474 * "matchlist()" function
6475 */
6476 static void
6477f_matchlist(typval_T *argvars, typval_T *rettv)
6478{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006479 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480}
6481
6482/*
6483 * "matchstr()" function
6484 */
6485 static void
6486f_matchstr(typval_T *argvars, typval_T *rettv)
6487{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006488 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489}
6490
6491/*
6492 * "matchstrpos()" function
6493 */
6494 static void
6495f_matchstrpos(typval_T *argvars, typval_T *rettv)
6496{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006497 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498}
6499
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500 static void
6501max_min(typval_T *argvars, typval_T *rettv, int domax)
6502{
6503 varnumber_T n = 0;
6504 varnumber_T i;
6505 int error = FALSE;
6506
6507 if (argvars[0].v_type == VAR_LIST)
6508 {
6509 list_T *l;
6510 listitem_T *li;
6511
6512 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006513 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006515 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006517 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6518 n = l->lv_u.nonmat.lv_start;
6519 else
6520 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6521 * l->lv_u.nonmat.lv_stride;
6522 }
6523 else
6524 {
6525 li = l->lv_first;
6526 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006528 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006529 if (error)
6530 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006531 for (;;)
6532 {
6533 li = li->li_next;
6534 if (li == NULL)
6535 break;
6536 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006537 if (error)
6538 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006539 if (domax ? i > n : i < n)
6540 n = i;
6541 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006542 }
6543 }
6544 }
6545 }
6546 else if (argvars[0].v_type == VAR_DICT)
6547 {
6548 dict_T *d;
6549 int first = TRUE;
6550 hashitem_T *hi;
6551 int todo;
6552
6553 d = argvars[0].vval.v_dict;
6554 if (d != NULL)
6555 {
6556 todo = (int)d->dv_hashtab.ht_used;
6557 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6558 {
6559 if (!HASHITEM_EMPTY(hi))
6560 {
6561 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006562 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006563 if (error)
6564 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 if (first)
6566 {
6567 n = i;
6568 first = FALSE;
6569 }
6570 else if (domax ? i > n : i < n)
6571 n = i;
6572 }
6573 }
6574 }
6575 }
6576 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006577 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006578
6579 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006580}
6581
6582/*
6583 * "max()" function
6584 */
6585 static void
6586f_max(typval_T *argvars, typval_T *rettv)
6587{
6588 max_min(argvars, rettv, TRUE);
6589}
6590
6591/*
6592 * "min()" function
6593 */
6594 static void
6595f_min(typval_T *argvars, typval_T *rettv)
6596{
6597 max_min(argvars, rettv, FALSE);
6598}
6599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600#if defined(FEAT_MZSCHEME) || defined(PROTO)
6601/*
6602 * "mzeval()" function
6603 */
6604 static void
6605f_mzeval(typval_T *argvars, typval_T *rettv)
6606{
6607 char_u *str;
6608 char_u buf[NUMBUFLEN];
6609
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006610 if (check_restricted() || check_secure())
6611 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006612 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613 do_mzeval(str, rettv);
6614}
6615
6616 void
6617mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6618{
6619 typval_T argvars[3];
6620
6621 argvars[0].v_type = VAR_STRING;
6622 argvars[0].vval.v_string = name;
6623 copy_tv(args, &argvars[1]);
6624 argvars[2].v_type = VAR_UNKNOWN;
6625 f_call(argvars, rettv);
6626 clear_tv(&argvars[1]);
6627}
6628#endif
6629
6630/*
6631 * "nextnonblank()" function
6632 */
6633 static void
6634f_nextnonblank(typval_T *argvars, typval_T *rettv)
6635{
6636 linenr_T lnum;
6637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006638 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 {
6640 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6641 {
6642 lnum = 0;
6643 break;
6644 }
6645 if (*skipwhite(ml_get(lnum)) != NUL)
6646 break;
6647 }
6648 rettv->vval.v_number = lnum;
6649}
6650
6651/*
6652 * "nr2char()" function
6653 */
6654 static void
6655f_nr2char(typval_T *argvars, typval_T *rettv)
6656{
6657 char_u buf[NUMBUFLEN];
6658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659 if (has_mbyte)
6660 {
6661 int utf8 = 0;
6662
6663 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006664 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006666 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006668 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 }
6670 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006672 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 buf[1] = NUL;
6674 }
6675 rettv->v_type = VAR_STRING;
6676 rettv->vval.v_string = vim_strsave(buf);
6677}
6678
6679/*
6680 * "or(expr, expr)" function
6681 */
6682 static void
6683f_or(typval_T *argvars, typval_T *rettv)
6684{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006685 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6686 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687}
6688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689#ifdef FEAT_PERL
6690/*
6691 * "perleval()" function
6692 */
6693 static void
6694f_perleval(typval_T *argvars, typval_T *rettv)
6695{
6696 char_u *str;
6697 char_u buf[NUMBUFLEN];
6698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006699 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700 do_perleval(str, rettv);
6701}
6702#endif
6703
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704/*
6705 * "prevnonblank()" function
6706 */
6707 static void
6708f_prevnonblank(typval_T *argvars, typval_T *rettv)
6709{
6710 linenr_T lnum;
6711
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006712 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6714 lnum = 0;
6715 else
6716 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6717 --lnum;
6718 rettv->vval.v_number = lnum;
6719}
6720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006721// This dummy va_list is here because:
6722// - passing a NULL pointer doesn't work when va_list isn't a pointer
6723// - locally in the function results in a "used before set" warning
6724// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725static va_list ap;
6726
6727/*
6728 * "printf()" function
6729 */
6730 static void
6731f_printf(typval_T *argvars, typval_T *rettv)
6732{
6733 char_u buf[NUMBUFLEN];
6734 int len;
6735 char_u *s;
6736 int saved_did_emsg = did_emsg;
6737 char *fmt;
6738
6739 rettv->v_type = VAR_STRING;
6740 rettv->vval.v_string = NULL;
6741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006742 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006744 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006745 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746 if (!did_emsg)
6747 {
6748 s = alloc(len + 1);
6749 if (s != NULL)
6750 {
6751 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006752 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6753 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 }
6755 }
6756 did_emsg |= saved_did_emsg;
6757}
6758
6759/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006760 * "pum_getpos()" function
6761 */
6762 static void
6763f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6764{
6765 if (rettv_dict_alloc(rettv) != OK)
6766 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006767 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006768}
6769
6770/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771 * "pumvisible()" function
6772 */
6773 static void
6774f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6775{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006776 if (pum_visible())
6777 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778}
6779
6780#ifdef FEAT_PYTHON3
6781/*
6782 * "py3eval()" function
6783 */
6784 static void
6785f_py3eval(typval_T *argvars, typval_T *rettv)
6786{
6787 char_u *str;
6788 char_u buf[NUMBUFLEN];
6789
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006790 if (check_restricted() || check_secure())
6791 return;
6792
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006793 if (p_pyx == 0)
6794 p_pyx = 3;
6795
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006796 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797 do_py3eval(str, rettv);
6798}
6799#endif
6800
6801#ifdef FEAT_PYTHON
6802/*
6803 * "pyeval()" function
6804 */
6805 static void
6806f_pyeval(typval_T *argvars, typval_T *rettv)
6807{
6808 char_u *str;
6809 char_u buf[NUMBUFLEN];
6810
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006811 if (check_restricted() || check_secure())
6812 return;
6813
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006814 if (p_pyx == 0)
6815 p_pyx = 2;
6816
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006817 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818 do_pyeval(str, rettv);
6819}
6820#endif
6821
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006822#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6823/*
6824 * "pyxeval()" function
6825 */
6826 static void
6827f_pyxeval(typval_T *argvars, typval_T *rettv)
6828{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006829 if (check_restricted() || check_secure())
6830 return;
6831
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006832# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6833 init_pyxversion();
6834 if (p_pyx == 2)
6835 f_pyeval(argvars, rettv);
6836 else
6837 f_py3eval(argvars, rettv);
6838# elif defined(FEAT_PYTHON)
6839 f_pyeval(argvars, rettv);
6840# elif defined(FEAT_PYTHON3)
6841 f_py3eval(argvars, rettv);
6842# endif
6843}
6844#endif
6845
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006846static UINT32_T srand_seed_for_testing = 0;
6847static int srand_seed_for_testing_is_used = FALSE;
6848
6849 static void
6850f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6851{
6852 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006853 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006854 else
6855 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006856 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6857 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006858 }
6859}
6860
6861 static void
6862init_srand(UINT32_T *x)
6863{
6864#ifndef MSWIN
6865 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6866#endif
6867
6868 if (srand_seed_for_testing_is_used)
6869 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006870 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006871 return;
6872 }
6873#ifndef MSWIN
6874 if (dev_urandom_state != FAIL)
6875 {
6876 int fd = open("/dev/urandom", O_RDONLY);
6877 struct {
6878 union {
6879 UINT32_T number;
6880 char bytes[sizeof(UINT32_T)];
6881 } contents;
6882 } buf;
6883
6884 // Attempt reading /dev/urandom.
6885 if (fd == -1)
6886 dev_urandom_state = FAIL;
6887 else
6888 {
6889 buf.contents.number = 0;
6890 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6891 != sizeof(UINT32_T))
6892 dev_urandom_state = FAIL;
6893 else
6894 {
6895 dev_urandom_state = OK;
6896 *x = buf.contents.number;
6897 }
6898 close(fd);
6899 }
6900 }
6901 if (dev_urandom_state != OK)
6902 // Reading /dev/urandom doesn't work, fall back to time().
6903#endif
6904 *x = vim_time();
6905}
6906
6907#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6908#define SPLITMIX32(x, z) ( \
6909 z = (x += 0x9e3779b9), \
6910 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6911 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6912 z ^ (z >> 16) \
6913 )
6914#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6915 result = ROTL(y * 5, 7) * 9; \
6916 t = y << 9; \
6917 z ^= x; \
6918 w ^= y; \
6919 y ^= z, x ^= w; \
6920 z ^= t; \
6921 w = ROTL(w, 11);
6922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006924 * "rand()" function
6925 */
6926 static void
6927f_rand(typval_T *argvars, typval_T *rettv)
6928{
6929 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006930 static UINT32_T gx, gy, gz, gw;
6931 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006932 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006933 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006934
6935 if (argvars[0].v_type == VAR_UNKNOWN)
6936 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006937 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006938 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006939 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006940 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006941 init_srand(&x);
6942
6943 gx = SPLITMIX32(x, z);
6944 gy = SPLITMIX32(x, z);
6945 gz = SPLITMIX32(x, z);
6946 gw = SPLITMIX32(x, z);
6947 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006948 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006949
6950 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006951 }
6952 else if (argvars[0].v_type == VAR_LIST)
6953 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006954 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006955 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006956 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006957
6958 lx = list_find(l, 0L);
6959 ly = list_find(l, 1L);
6960 lz = list_find(l, 2L);
6961 lw = list_find(l, 3L);
6962 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6963 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6964 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6965 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6966 x = (UINT32_T)lx->li_tv.vval.v_number;
6967 y = (UINT32_T)ly->li_tv.vval.v_number;
6968 z = (UINT32_T)lz->li_tv.vval.v_number;
6969 w = (UINT32_T)lw->li_tv.vval.v_number;
6970
6971 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6972
6973 lx->li_tv.vval.v_number = (varnumber_T)x;
6974 ly->li_tv.vval.v_number = (varnumber_T)y;
6975 lz->li_tv.vval.v_number = (varnumber_T)z;
6976 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006977 }
6978 else
6979 goto theend;
6980
6981 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006982 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006983 return;
6984
6985theend:
6986 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006987 rettv->v_type = VAR_NUMBER;
6988 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006989}
6990
6991/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006992 * "srand()" function
6993 */
6994 static void
6995f_srand(typval_T *argvars, typval_T *rettv)
6996{
6997 UINT32_T x = 0, z;
6998
6999 if (rettv_list_alloc(rettv) == FAIL)
7000 return;
7001 if (argvars[0].v_type == VAR_UNKNOWN)
7002 {
7003 init_srand(&x);
7004 }
7005 else
7006 {
7007 int error = FALSE;
7008
7009 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7010 if (error)
7011 return;
7012 }
7013
7014 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7015 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
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}
7019
7020#undef ROTL
7021#undef SPLITMIX32
7022#undef SHUFFLE_XOSHIRO128STARSTAR
7023
7024/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007025 * "range()" function
7026 */
7027 static void
7028f_range(typval_T *argvars, typval_T *rettv)
7029{
7030 varnumber_T start;
7031 varnumber_T end;
7032 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033 int error = FALSE;
7034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007035 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 if (argvars[1].v_type == VAR_UNKNOWN)
7037 {
7038 end = start - 1;
7039 start = 0;
7040 }
7041 else
7042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007045 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 }
7047
7048 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007049 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007051 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007053 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007055 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007056 list_T *list = rettv->vval.v_list;
7057
7058 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007059 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 // be called.
7061 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007062 list->lv_u.nonmat.lv_start = start;
7063 list->lv_u.nonmat.lv_end = end;
7064 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007065 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 }
7067}
7068
7069/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007070 * Materialize "list".
7071 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 */
7073 void
7074range_list_materialize(list_T *list)
7075{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007076 varnumber_T start = list->lv_u.nonmat.lv_start;
7077 varnumber_T end = list->lv_u.nonmat.lv_end;
7078 int stride = list->lv_u.nonmat.lv_stride;
7079 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007081 list->lv_first = NULL;
7082 list->lv_u.mat.lv_last = NULL;
7083 list->lv_len = 0;
7084 list->lv_u.mat.lv_idx_item = NULL;
7085 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7086 if (list_append_number(list, (varnumber_T)i) == FAIL)
7087 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088}
7089
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007090/*
7091 * "getreginfo()" function
7092 */
7093 static void
7094f_getreginfo(typval_T *argvars, typval_T *rettv)
7095{
7096 char_u *strregname;
7097 int regname;
7098 char_u buf[NUMBUFLEN + 2];
7099 long reglen = 0;
7100 dict_T *dict;
7101 list_T *list;
7102
7103 if (argvars[0].v_type != VAR_UNKNOWN)
7104 {
7105 strregname = tv_get_string_chk(&argvars[0]);
7106 if (strregname == NULL)
7107 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007108 if (in_vim9script() && STRLEN(strregname) > 1)
7109 {
7110 semsg(_(e_register_name_must_be_one_char_str), strregname);
7111 return;
7112 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007113 }
7114 else
7115 strregname = get_vim_var_str(VV_REG);
7116
7117 regname = (strregname == NULL ? '"' : *strregname);
7118 if (regname == 0 || regname == '@')
7119 regname = '"';
7120
7121 if (rettv_dict_alloc(rettv) == FAIL)
7122 return;
7123 dict = rettv->vval.v_dict;
7124
7125 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7126 if (list == NULL)
7127 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007128 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007129
7130 buf[0] = NUL;
7131 buf[1] = NUL;
7132 switch (get_reg_type(regname, &reglen))
7133 {
7134 case MLINE: buf[0] = 'V'; break;
7135 case MCHAR: buf[0] = 'v'; break;
7136 case MBLOCK:
7137 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7138 reglen + 1);
7139 break;
7140 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007141 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007142
7143 buf[0] = get_register_name(get_unname_register());
7144 buf[1] = NUL;
7145 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007146 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007147 else
7148 {
7149 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7150
7151 if (item != NULL)
7152 {
7153 item->di_tv.v_type = VAR_SPECIAL;
7154 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007155 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007156 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007157 }
7158 }
7159}
7160
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007161 static void
7162return_register(int regname, typval_T *rettv)
7163{
7164 char_u buf[2] = {0, 0};
7165
7166 buf[0] = (char_u)regname;
7167 rettv->v_type = VAR_STRING;
7168 rettv->vval.v_string = vim_strsave(buf);
7169}
7170
7171/*
7172 * "reg_executing()" function
7173 */
7174 static void
7175f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7176{
7177 return_register(reg_executing, rettv);
7178}
7179
7180/*
7181 * "reg_recording()" function
7182 */
7183 static void
7184f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7185{
7186 return_register(reg_recording, rettv);
7187}
7188
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007189/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 * "rename({from}, {to})" function
7191 */
7192 static void
7193f_rename(typval_T *argvars, typval_T *rettv)
7194{
7195 char_u buf[NUMBUFLEN];
7196
7197 if (check_restricted() || check_secure())
7198 rettv->vval.v_number = -1;
7199 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007200 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7201 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202}
7203
7204/*
7205 * "repeat()" function
7206 */
7207 static void
7208f_repeat(typval_T *argvars, typval_T *rettv)
7209{
7210 char_u *p;
7211 int n;
7212 int slen;
7213 int len;
7214 char_u *r;
7215 int i;
7216
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007217 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007218 if (argvars[0].v_type == VAR_LIST)
7219 {
7220 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7221 while (n-- > 0)
7222 if (list_extend(rettv->vval.v_list,
7223 argvars[0].vval.v_list, NULL) == FAIL)
7224 break;
7225 }
7226 else
7227 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007228 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 rettv->v_type = VAR_STRING;
7230 rettv->vval.v_string = NULL;
7231
7232 slen = (int)STRLEN(p);
7233 len = slen * n;
7234 if (len <= 0)
7235 return;
7236
7237 r = alloc(len + 1);
7238 if (r != NULL)
7239 {
7240 for (i = 0; i < n; i++)
7241 mch_memmove(r + i * slen, p, (size_t)slen);
7242 r[len] = NUL;
7243 }
7244
7245 rettv->vval.v_string = r;
7246 }
7247}
7248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007249#define SP_NOMOVE 0x01 // don't move cursor
7250#define SP_REPEAT 0x02 // repeat to find outer pair
7251#define SP_RETCOUNT 0x04 // return matchcount
7252#define SP_SETPCMARK 0x08 // set previous context mark
7253#define SP_START 0x10 // accept match at start position
7254#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7255#define SP_END 0x40 // leave cursor at end of match
7256#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258/*
7259 * Get flags for a search function.
7260 * Possibly sets "p_ws".
7261 * Returns BACKWARD, FORWARD or zero (for an error).
7262 */
7263 static int
7264get_search_arg(typval_T *varp, int *flagsp)
7265{
7266 int dir = FORWARD;
7267 char_u *flags;
7268 char_u nbuf[NUMBUFLEN];
7269 int mask;
7270
7271 if (varp->v_type != VAR_UNKNOWN)
7272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007273 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007275 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 while (*flags != NUL)
7277 {
7278 switch (*flags)
7279 {
7280 case 'b': dir = BACKWARD; break;
7281 case 'w': p_ws = TRUE; break;
7282 case 'W': p_ws = FALSE; break;
7283 default: mask = 0;
7284 if (flagsp != NULL)
7285 switch (*flags)
7286 {
7287 case 'c': mask = SP_START; break;
7288 case 'e': mask = SP_END; break;
7289 case 'm': mask = SP_RETCOUNT; break;
7290 case 'n': mask = SP_NOMOVE; break;
7291 case 'p': mask = SP_SUBPAT; break;
7292 case 'r': mask = SP_REPEAT; break;
7293 case 's': mask = SP_SETPCMARK; break;
7294 case 'z': mask = SP_COLUMN; break;
7295 }
7296 if (mask == 0)
7297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007298 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 dir = 0;
7300 }
7301 else
7302 *flagsp |= mask;
7303 }
7304 if (dir == 0)
7305 break;
7306 ++flags;
7307 }
7308 }
7309 return dir;
7310}
7311
7312/*
7313 * Shared by search() and searchpos() functions.
7314 */
7315 static int
7316search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7317{
7318 int flags;
7319 char_u *pat;
7320 pos_T pos;
7321 pos_T save_cursor;
7322 int save_p_ws = p_ws;
7323 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007324 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007327 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 long time_limit = 0;
7329#endif
7330 int options = SEARCH_KEEP;
7331 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007332 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007333 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007334 pos_T firstpos;
7335
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007336 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007337 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 if (dir == 0)
7339 goto theend;
7340 flags = *flagsp;
7341 if (flags & SP_START)
7342 options |= SEARCH_START;
7343 if (flags & SP_END)
7344 options |= SEARCH_END;
7345 if (flags & SP_COLUMN)
7346 options |= SEARCH_COL;
7347
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007348 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 if (lnum_stop < 0)
7353 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 if (argvars[3].v_type != VAR_UNKNOWN)
7355 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007356#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 if (time_limit < 0)
7359 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007361 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007362 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 }
7364
7365#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007366 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367 profile_setlimit(time_limit, &tm);
7368#endif
7369
7370 /*
7371 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7372 * Check to make sure only those flags are set.
7373 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7374 * flags cannot be set. Check for that condition also.
7375 */
7376 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7377 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 goto theend;
7381 }
7382
7383 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007384 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007385 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007386 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7387#ifdef FEAT_RELTIME
7388 sia.sa_tm = &tm;
7389#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007390
7391 // Repeat until {skip} returns FALSE.
7392 for (;;)
7393 {
7394 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007395 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007396 // finding the first match again means there is no match where {skip}
7397 // evaluates to zero.
7398 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7399 subpatnum = FAIL;
7400
Bram Moolenaara9c01042020-06-07 14:50:50 +02007401 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007402 // didn't find it or no skip argument
7403 break;
7404 firstpos = pos;
7405
Bram Moolenaara9c01042020-06-07 14:50:50 +02007406 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007407 {
7408 int do_skip;
7409 int err;
7410 pos_T save_pos = curwin->w_cursor;
7411
7412 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007413 err = FALSE;
7414 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007415 curwin->w_cursor = save_pos;
7416 if (err)
7417 {
7418 // Evaluating {skip} caused an error, break here.
7419 subpatnum = FAIL;
7420 break;
7421 }
7422 if (!do_skip)
7423 break;
7424 }
7425 }
7426
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 if (subpatnum != FAIL)
7428 {
7429 if (flags & SP_SUBPAT)
7430 retval = subpatnum;
7431 else
7432 retval = pos.lnum;
7433 if (flags & SP_SETPCMARK)
7434 setpcmark();
7435 curwin->w_cursor = pos;
7436 if (match_pos != NULL)
7437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007438 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 match_pos->lnum = pos.lnum;
7440 match_pos->col = pos.col + 1;
7441 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007442 // "/$" will put the cursor after the end of the line, may need to
7443 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 check_cursor();
7445 }
7446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007447 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007448 if (flags & SP_NOMOVE)
7449 curwin->w_cursor = save_cursor;
7450 else
7451 curwin->w_set_curswant = TRUE;
7452theend:
7453 p_ws = save_p_ws;
7454
7455 return retval;
7456}
7457
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007458#ifdef FEAT_RUBY
7459/*
7460 * "rubyeval()" function
7461 */
7462 static void
7463f_rubyeval(typval_T *argvars, typval_T *rettv)
7464{
7465 char_u *str;
7466 char_u buf[NUMBUFLEN];
7467
7468 str = tv_get_string_buf(&argvars[0], buf);
7469 do_rubyeval(str, rettv);
7470}
7471#endif
7472
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473/*
7474 * "screenattr()" function
7475 */
7476 static void
7477f_screenattr(typval_T *argvars, typval_T *rettv)
7478{
7479 int row;
7480 int col;
7481 int c;
7482
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007483 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7484 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 if (row < 0 || row >= screen_Rows
7486 || col < 0 || col >= screen_Columns)
7487 c = -1;
7488 else
7489 c = ScreenAttrs[LineOffset[row] + col];
7490 rettv->vval.v_number = c;
7491}
7492
7493/*
7494 * "screenchar()" function
7495 */
7496 static void
7497f_screenchar(typval_T *argvars, typval_T *rettv)
7498{
7499 int row;
7500 int col;
7501 int off;
7502 int c;
7503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007504 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7505 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007506 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507 c = -1;
7508 else
7509 {
7510 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 if (enc_utf8 && ScreenLinesUC[off] != 0)
7512 c = ScreenLinesUC[off];
7513 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 c = ScreenLines[off];
7515 }
7516 rettv->vval.v_number = c;
7517}
7518
7519/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007520 * "screenchars()" function
7521 */
7522 static void
7523f_screenchars(typval_T *argvars, typval_T *rettv)
7524{
7525 int row;
7526 int col;
7527 int off;
7528 int c;
7529 int i;
7530
7531 if (rettv_list_alloc(rettv) == FAIL)
7532 return;
7533 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7534 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7535 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7536 return;
7537
7538 off = LineOffset[row] + col;
7539 if (enc_utf8 && ScreenLinesUC[off] != 0)
7540 c = ScreenLinesUC[off];
7541 else
7542 c = ScreenLines[off];
7543 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7544
7545 if (enc_utf8)
7546
7547 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7548 list_append_number(rettv->vval.v_list,
7549 (varnumber_T)ScreenLinesC[i][off]);
7550}
7551
7552/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553 * "screencol()" function
7554 *
7555 * First column is 1 to be consistent with virtcol().
7556 */
7557 static void
7558f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7559{
7560 rettv->vval.v_number = screen_screencol() + 1;
7561}
7562
7563/*
7564 * "screenrow()" function
7565 */
7566 static void
7567f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7568{
7569 rettv->vval.v_number = screen_screenrow() + 1;
7570}
7571
7572/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007573 * "screenstring()" function
7574 */
7575 static void
7576f_screenstring(typval_T *argvars, typval_T *rettv)
7577{
7578 int row;
7579 int col;
7580 int off;
7581 int c;
7582 int i;
7583 char_u buf[MB_MAXBYTES + 1];
7584 int buflen = 0;
7585
7586 rettv->vval.v_string = NULL;
7587 rettv->v_type = VAR_STRING;
7588
7589 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7590 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7591 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7592 return;
7593
7594 off = LineOffset[row] + col;
7595 if (enc_utf8 && ScreenLinesUC[off] != 0)
7596 c = ScreenLinesUC[off];
7597 else
7598 c = ScreenLines[off];
7599 buflen += mb_char2bytes(c, buf);
7600
Bram Moolenaarf1387282021-03-22 17:11:15 +01007601 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007602 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7603 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7604
7605 buf[buflen] = NUL;
7606 rettv->vval.v_string = vim_strsave(buf);
7607}
7608
7609/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 * "search()" function
7611 */
7612 static void
7613f_search(typval_T *argvars, typval_T *rettv)
7614{
7615 int flags = 0;
7616
7617 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7618}
7619
7620/*
7621 * "searchdecl()" function
7622 */
7623 static void
7624f_searchdecl(typval_T *argvars, typval_T *rettv)
7625{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007626 int locally = TRUE;
7627 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 int error = FALSE;
7629 char_u *name;
7630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007631 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007633 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 if (argvars[1].v_type != VAR_UNKNOWN)
7635 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007636 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007638 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 }
7640 if (!error && name != NULL)
7641 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7642 locally, thisblock, SEARCH_KEEP) == FAIL;
7643}
7644
7645/*
7646 * Used by searchpair() and searchpairpos()
7647 */
7648 static int
7649searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7650{
7651 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007652 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 int save_p_ws = p_ws;
7654 int dir;
7655 int flags = 0;
7656 char_u nbuf1[NUMBUFLEN];
7657 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007658 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 long lnum_stop = 0;
7660 long time_limit = 0;
7661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007662 // Get the three pattern arguments: start, middle, end. Will result in an
7663 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007664 spat = tv_get_string_chk(&argvars[0]);
7665 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7666 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007668 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 // Handle the optional fourth argument: flags
7671 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 if (dir == 0)
7673 goto theend;
7674
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007675 // Don't accept SP_END or SP_SUBPAT.
7676 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 if ((flags & (SP_END | SP_SUBPAT)) != 0
7678 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 goto theend;
7682 }
7683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007684 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 if (flags & SP_REPEAT)
7686 p_ws = FALSE;
7687
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007688 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689 if (argvars[3].v_type == VAR_UNKNOWN
7690 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007691 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 else
7693 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007694 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007695 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 if (argvars[5].v_type != VAR_UNKNOWN)
7698 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007699 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007702 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007704 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705#ifdef FEAT_RELTIME
7706 if (argvars[6].v_type != VAR_UNKNOWN)
7707 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007708 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007711 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714 }
7715#endif
7716 }
7717 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718
7719 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7720 match_pos, lnum_stop, time_limit);
7721
7722theend:
7723 p_ws = save_p_ws;
7724
7725 return retval;
7726}
7727
7728/*
7729 * "searchpair()" function
7730 */
7731 static void
7732f_searchpair(typval_T *argvars, typval_T *rettv)
7733{
7734 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7735}
7736
7737/*
7738 * "searchpairpos()" function
7739 */
7740 static void
7741f_searchpairpos(typval_T *argvars, typval_T *rettv)
7742{
7743 pos_T match_pos;
7744 int lnum = 0;
7745 int col = 0;
7746
7747 if (rettv_list_alloc(rettv) == FAIL)
7748 return;
7749
7750 if (searchpair_cmn(argvars, &match_pos) > 0)
7751 {
7752 lnum = match_pos.lnum;
7753 col = match_pos.col;
7754 }
7755
7756 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7757 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7758}
7759
7760/*
7761 * Search for a start/middle/end thing.
7762 * Used by searchpair(), see its documentation for the details.
7763 * Returns 0 or -1 for no match,
7764 */
7765 long
7766do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007767 char_u *spat, // start pattern
7768 char_u *mpat, // middle pattern
7769 char_u *epat, // end pattern
7770 int dir, // BACKWARD or FORWARD
7771 typval_T *skip, // skip expression
7772 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007773 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007774 linenr_T lnum_stop, // stop at this line if not zero
7775 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776{
7777 char_u *save_cpo;
7778 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7779 long retval = 0;
7780 pos_T pos;
7781 pos_T firstpos;
7782 pos_T foundpos;
7783 pos_T save_cursor;
7784 pos_T save_pos;
7785 int n;
7786 int r;
7787 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007788 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 int err;
7790 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007791#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007793#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007795 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796 save_cpo = p_cpo;
7797 p_cpo = empty_option;
7798
7799#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007800 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801 profile_setlimit(time_limit, &tm);
7802#endif
7803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007804 // Make two search patterns: start/end (pat2, for in nested pairs) and
7805 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007806 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7807 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 if (pat2 == NULL || pat3 == NULL)
7809 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007810 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 if (*mpat == NUL)
7812 STRCPY(pat3, pat2);
7813 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007814 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815 spat, epat, mpat);
7816 if (flags & SP_START)
7817 options |= SEARCH_START;
7818
Bram Moolenaar48570482017-10-30 21:48:41 +01007819 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007820 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007821
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 save_cursor = curwin->w_cursor;
7823 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007824 CLEAR_POS(&firstpos);
7825 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 pat = pat3;
7827 for (;;)
7828 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007829 searchit_arg_T sia;
7830
Bram Moolenaara80faa82020-04-12 19:37:17 +02007831 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007832 sia.sa_stop_lnum = lnum_stop;
7833#ifdef FEAT_RELTIME
7834 sia.sa_tm = &tm;
7835#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007836 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007837 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007838 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007839 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007840 break;
7841
7842 if (firstpos.lnum == 0)
7843 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007844 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007846 // Found the same position again. Can happen with a pattern that
7847 // has "\zs" at the end and searching backwards. Advance one
7848 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007849 if (dir == BACKWARD)
7850 decl(&pos);
7851 else
7852 incl(&pos);
7853 }
7854 foundpos = pos;
7855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007856 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 options &= ~SEARCH_START;
7858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007859 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007860 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007861 {
7862 save_pos = curwin->w_cursor;
7863 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007864 err = FALSE;
7865 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 curwin->w_cursor = save_pos;
7867 if (err)
7868 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007869 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870 curwin->w_cursor = save_cursor;
7871 retval = -1;
7872 break;
7873 }
7874 if (r)
7875 continue;
7876 }
7877
7878 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007880 // Found end when searching backwards or start when searching
7881 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007882 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007883 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 }
7885 else
7886 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007887 // Found end when searching forward or start when searching
7888 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007890 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 }
7892
7893 if (nest == 0)
7894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007895 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007896 if (flags & SP_RETCOUNT)
7897 ++retval;
7898 else
7899 retval = pos.lnum;
7900 if (flags & SP_SETPCMARK)
7901 setpcmark();
7902 curwin->w_cursor = pos;
7903 if (!(flags & SP_REPEAT))
7904 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007905 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 }
7907 }
7908
7909 if (match_pos != NULL)
7910 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007911 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 match_pos->lnum = curwin->w_cursor.lnum;
7913 match_pos->col = curwin->w_cursor.col + 1;
7914 }
7915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007916 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917 if ((flags & SP_NOMOVE) || retval == 0)
7918 curwin->w_cursor = save_cursor;
7919
7920theend:
7921 vim_free(pat2);
7922 vim_free(pat3);
7923 if (p_cpo == empty_option)
7924 p_cpo = save_cpo;
7925 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007927 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007928 // If it's still empty it was changed and restored, need to restore in
7929 // the complicated way.
7930 if (*p_cpo == NUL)
7931 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007933 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934
7935 return retval;
7936}
7937
7938/*
7939 * "searchpos()" function
7940 */
7941 static void
7942f_searchpos(typval_T *argvars, typval_T *rettv)
7943{
7944 pos_T match_pos;
7945 int lnum = 0;
7946 int col = 0;
7947 int n;
7948 int flags = 0;
7949
7950 if (rettv_list_alloc(rettv) == FAIL)
7951 return;
7952
7953 n = search_cmn(argvars, &match_pos, &flags);
7954 if (n > 0)
7955 {
7956 lnum = match_pos.lnum;
7957 col = match_pos.col;
7958 }
7959
7960 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7961 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7962 if (flags & SP_SUBPAT)
7963 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7964}
7965
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007966/*
7967 * Set the cursor or mark position.
7968 * If 'charpos' is TRUE, then use the column number as a character offet.
7969 * Otherwise use the column number as a byte offset.
7970 */
7971 static void
7972set_position(typval_T *argvars, typval_T *rettv, int charpos)
7973{
7974 pos_T pos;
7975 int fnum;
7976 char_u *name;
7977 colnr_T curswant = -1;
7978
7979 rettv->vval.v_number = -1;
7980
7981 name = tv_get_string_chk(argvars);
7982 if (name != NULL)
7983 {
7984 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7985 {
7986 if (pos.col != MAXCOL && --pos.col < 0)
7987 pos.col = 0;
7988 if ((name[0] == '.' && name[1] == NUL))
7989 {
7990 // set cursor; "fnum" is ignored
7991 curwin->w_cursor = pos;
7992 if (curswant >= 0)
7993 {
7994 curwin->w_curswant = curswant - 1;
7995 curwin->w_set_curswant = FALSE;
7996 }
7997 check_cursor();
7998 rettv->vval.v_number = 0;
7999 }
8000 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8001 {
8002 // set mark
8003 if (setmark_pos(name[1], &pos, fnum) == OK)
8004 rettv->vval.v_number = 0;
8005 }
8006 else
8007 emsg(_(e_invarg));
8008 }
8009 }
8010}
8011/*
8012 * "setcharpos()" function
8013 */
8014 static void
8015f_setcharpos(typval_T *argvars, typval_T *rettv)
8016{
8017 set_position(argvars, rettv, TRUE);
8018}
8019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8022{
8023 dict_T *d;
8024 dictitem_T *di;
8025 char_u *csearch;
8026
8027 if (argvars[0].v_type != VAR_DICT)
8028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008029 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 return;
8031 }
8032
8033 if ((d = argvars[0].vval.v_dict) != NULL)
8034 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008035 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 if (csearch != NULL)
8037 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 if (enc_utf8)
8039 {
8040 int pcc[MAX_MCO];
8041 int c = utfc_ptr2char(csearch, pcc);
8042
8043 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8044 }
8045 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008047 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 }
8049
8050 di = dict_find(d, (char_u *)"forward", -1);
8051 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008052 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 ? FORWARD : BACKWARD);
8054
8055 di = dict_find(d, (char_u *)"until", -1);
8056 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 }
8059}
8060
8061/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008062 * "setcursorcharpos" function
8063 */
8064 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008065f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008066{
8067 set_cursorpos(argvars, rettv, TRUE);
8068}
8069
8070/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008071 * "setenv()" function
8072 */
8073 static void
8074f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8075{
8076 char_u namebuf[NUMBUFLEN];
8077 char_u valbuf[NUMBUFLEN];
8078 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8079
8080 if (argvars[1].v_type == VAR_SPECIAL
8081 && argvars[1].vval.v_number == VVAL_NULL)
8082 vim_unsetenv(name);
8083 else
8084 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8085}
8086
8087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 * "setfperm({fname}, {mode})" function
8089 */
8090 static void
8091f_setfperm(typval_T *argvars, typval_T *rettv)
8092{
8093 char_u *fname;
8094 char_u modebuf[NUMBUFLEN];
8095 char_u *mode_str;
8096 int i;
8097 int mask;
8098 int mode = 0;
8099
8100 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008101 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 if (fname == NULL)
8103 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008104 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 if (mode_str == NULL)
8106 return;
8107 if (STRLEN(mode_str) != 9)
8108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008109 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 return;
8111 }
8112
8113 mask = 1;
8114 for (i = 8; i >= 0; --i)
8115 {
8116 if (mode_str[i] != '-')
8117 mode |= mask;
8118 mask = mask << 1;
8119 }
8120 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8121}
8122
8123/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 * "setpos()" function
8125 */
8126 static void
8127f_setpos(typval_T *argvars, typval_T *rettv)
8128{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008129 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130}
8131
8132/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008133 * Translate a register type string to the yank type and block length
8134 */
8135 static int
8136get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8137{
8138 char_u *stropt = *pp;
8139 switch (*stropt)
8140 {
8141 case 'v': case 'c': // character-wise selection
8142 *yank_type = MCHAR;
8143 break;
8144 case 'V': case 'l': // line-wise selection
8145 *yank_type = MLINE;
8146 break;
8147 case 'b': case Ctrl_V: // block-wise selection
8148 *yank_type = MBLOCK;
8149 if (VIM_ISDIGIT(stropt[1]))
8150 {
8151 ++stropt;
8152 *block_len = getdigits(&stropt) - 1;
8153 --stropt;
8154 }
8155 break;
8156 default:
8157 return FAIL;
8158 }
8159 *pp = stropt;
8160 return OK;
8161}
8162
8163/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 * "setreg()" function
8165 */
8166 static void
8167f_setreg(typval_T *argvars, typval_T *rettv)
8168{
8169 int regname;
8170 char_u *strregname;
8171 char_u *stropt;
8172 char_u *strval;
8173 int append;
8174 char_u yank_type;
8175 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008176 typval_T *regcontents;
8177 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008179 pointreg = 0;
8180 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 block_len = -1;
8182 yank_type = MAUTO;
8183 append = FALSE;
8184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008185 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008186 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187
8188 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008189 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008190 if (in_vim9script() && STRLEN(strregname) > 1)
8191 {
8192 semsg(_(e_register_name_must_be_one_char_str), strregname);
8193 return;
8194 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195 regname = *strregname;
8196 if (regname == 0 || regname == '@')
8197 regname = '"';
8198
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008199 if (argvars[1].v_type == VAR_DICT)
8200 {
8201 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008202 dictitem_T *di;
8203
8204 if (d == NULL || d->dv_hashtab.ht_used == 0)
8205 {
8206 // Empty dict, clear the register (like setreg(0, []))
8207 char_u *lstval[2] = {NULL, NULL};
8208 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8209 return;
8210 }
8211
8212 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008213 if (di != NULL)
8214 regcontents = &di->di_tv;
8215
8216 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8217 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008218 {
8219 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8220
8221 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008222 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008223 semsg(_(e_invargval), "value");
8224 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008225 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008226 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008227
8228 if (regname == '"')
8229 {
8230 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8231 if (stropt != NULL)
8232 {
8233 pointreg = *stropt;
8234 regname = pointreg;
8235 }
8236 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008237 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008238 pointreg = regname;
8239 }
8240 else
8241 regcontents = &argvars[1];
8242
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 if (argvars[2].v_type != VAR_UNKNOWN)
8244 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008245 if (yank_type != MAUTO)
8246 {
8247 semsg(_(e_toomanyarg), "setreg");
8248 return;
8249 }
8250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008251 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008253 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008254 for (; *stropt != NUL; ++stropt)
8255 switch (*stropt)
8256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008257 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 append = TRUE;
8259 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008260 default:
8261 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 }
8263 }
8264
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008265 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 {
8267 char_u **lstval;
8268 char_u **allocval;
8269 char_u buf[NUMBUFLEN];
8270 char_u **curval;
8271 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008272 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008273 listitem_T *li;
8274 int len;
8275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008276 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 len = ll == NULL ? 0 : ll->lv_len;
8278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008279 // First half: use for pointers to result lines; second half: use for
8280 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008281 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 if (lstval == NULL)
8283 return;
8284 curval = lstval;
8285 allocval = lstval + len + 2;
8286 curallocval = allocval;
8287
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008288 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008290 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008291 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008293 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008294 if (strval == NULL)
8295 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008296 if (strval == buf)
8297 {
8298 // Need to make a copy, next tv_get_string_buf_chk() will
8299 // overwrite the string.
8300 strval = vim_strsave(buf);
8301 if (strval == NULL)
8302 goto free_lstval;
8303 *curallocval++ = strval;
8304 }
8305 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 }
8308 *curval++ = NULL;
8309
8310 write_reg_contents_lst(regname, lstval, -1,
8311 append, yank_type, block_len);
8312free_lstval:
8313 while (curallocval > allocval)
8314 vim_free(*--curallocval);
8315 vim_free(lstval);
8316 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008317 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008319 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 if (strval == NULL)
8321 return;
8322 write_reg_contents_ex(regname, strval, -1,
8323 append, yank_type, block_len);
8324 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008325 if (pointreg != 0)
8326 get_yank_register(pointreg, TRUE);
8327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 rettv->vval.v_number = 0;
8329}
8330
8331/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008332 * "settagstack()" function
8333 */
8334 static void
8335f_settagstack(typval_T *argvars, typval_T *rettv)
8336{
8337 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8338 win_T *wp;
8339 dict_T *d;
8340 int action = 'r';
8341
8342 rettv->vval.v_number = -1;
8343
8344 // first argument: window number or id
8345 wp = find_win_by_nr_or_id(&argvars[0]);
8346 if (wp == NULL)
8347 return;
8348
8349 // second argument: dict with items to set in the tag stack
8350 if (argvars[1].v_type != VAR_DICT)
8351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008352 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008353 return;
8354 }
8355 d = argvars[1].vval.v_dict;
8356 if (d == NULL)
8357 return;
8358
8359 // third argument: action - 'a' for append and 'r' for replace.
8360 // default is to replace the stack.
8361 if (argvars[2].v_type == VAR_UNKNOWN)
8362 action = 'r';
8363 else if (argvars[2].v_type == VAR_STRING)
8364 {
8365 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008367 if (actstr == NULL)
8368 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008369 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8370 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008371 action = *actstr;
8372 else
8373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008374 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008375 return;
8376 }
8377 }
8378 else
8379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008380 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008381 return;
8382 }
8383
8384 if (set_tagstack(wp, d, action) == OK)
8385 rettv->vval.v_number = 0;
8386}
8387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388#ifdef FEAT_CRYPT
8389/*
8390 * "sha256({string})" function
8391 */
8392 static void
8393f_sha256(typval_T *argvars, typval_T *rettv)
8394{
8395 char_u *p;
8396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 rettv->vval.v_string = vim_strsave(
8399 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8400 rettv->v_type = VAR_STRING;
8401}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008402#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403
8404/*
8405 * "shellescape({string})" function
8406 */
8407 static void
8408f_shellescape(typval_T *argvars, typval_T *rettv)
8409{
Bram Moolenaar20615522017-06-05 18:46:26 +02008410 int do_special = non_zero_arg(&argvars[1]);
8411
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008413 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008414 rettv->v_type = VAR_STRING;
8415}
8416
8417/*
8418 * shiftwidth() function
8419 */
8420 static void
8421f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8422{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008423 rettv->vval.v_number = 0;
8424
8425 if (argvars[0].v_type != VAR_UNKNOWN)
8426 {
8427 long col;
8428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008429 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008430 if (col < 0)
8431 return; // type error; errmsg already given
8432#ifdef FEAT_VARTABS
8433 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8434 return;
8435#endif
8436 }
8437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 rettv->vval.v_number = get_sw_value(curbuf);
8439}
8440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008441/*
8442 * "soundfold({word})" function
8443 */
8444 static void
8445f_soundfold(typval_T *argvars, typval_T *rettv)
8446{
8447 char_u *s;
8448
8449 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008450 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451#ifdef FEAT_SPELL
8452 rettv->vval.v_string = eval_soundfold(s);
8453#else
8454 rettv->vval.v_string = vim_strsave(s);
8455#endif
8456}
8457
8458/*
8459 * "spellbadword()" function
8460 */
8461 static void
8462f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8463{
8464 char_u *word = (char_u *)"";
8465 hlf_T attr = HLF_COUNT;
8466 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008467#ifdef FEAT_SPELL
8468 int wo_spell_save = curwin->w_p_spell;
8469
8470 if (!curwin->w_p_spell)
8471 {
8472 did_set_spelllang(curwin);
8473 curwin->w_p_spell = TRUE;
8474 }
8475
8476 if (*curwin->w_s->b_p_spl == NUL)
8477 {
8478 emsg(_(e_no_spell));
8479 curwin->w_p_spell = wo_spell_save;
8480 return;
8481 }
8482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008483
8484 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008485 {
8486#ifdef FEAT_SPELL
8487 curwin->w_p_spell = wo_spell_save;
8488#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491
8492#ifdef FEAT_SPELL
8493 if (argvars[0].v_type == VAR_UNKNOWN)
8494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008495 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008496 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8497 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008498 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008500 curwin->w_set_curswant = TRUE;
8501 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008503 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008505 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008506 int capcol = -1;
8507
8508 if (str != NULL)
8509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008510 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008511 while (*str != NUL)
8512 {
8513 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8514 if (attr != HLF_COUNT)
8515 {
8516 word = str;
8517 break;
8518 }
8519 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008520 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008521 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 }
8523 }
8524 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008525 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526#endif
8527
8528 list_append_string(rettv->vval.v_list, word, len);
8529 list_append_string(rettv->vval.v_list, (char_u *)(
8530 attr == HLF_SPB ? "bad" :
8531 attr == HLF_SPR ? "rare" :
8532 attr == HLF_SPL ? "local" :
8533 attr == HLF_SPC ? "caps" :
8534 ""), -1);
8535}
8536
8537/*
8538 * "spellsuggest()" function
8539 */
8540 static void
8541f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8542{
8543#ifdef FEAT_SPELL
8544 char_u *str;
8545 int typeerr = FALSE;
8546 int maxcount;
8547 garray_T ga;
8548 int i;
8549 listitem_T *li;
8550 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008551 int wo_spell_save = curwin->w_p_spell;
8552
8553 if (!curwin->w_p_spell)
8554 {
8555 did_set_spelllang(curwin);
8556 curwin->w_p_spell = TRUE;
8557 }
8558
8559 if (*curwin->w_s->b_p_spl == NUL)
8560 {
8561 emsg(_(e_no_spell));
8562 curwin->w_p_spell = wo_spell_save;
8563 return;
8564 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565#endif
8566
8567 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008568 {
8569#ifdef FEAT_SPELL
8570 curwin->w_p_spell = wo_spell_save;
8571#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008573 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574
8575#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008576 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579 if (argvars[1].v_type != VAR_UNKNOWN)
8580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008581 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 if (maxcount <= 0)
8583 return;
8584 if (argvars[2].v_type != VAR_UNKNOWN)
8585 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008586 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 if (typeerr)
8588 return;
8589 }
8590 }
8591 else
8592 maxcount = 25;
8593
8594 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8595
8596 for (i = 0; i < ga.ga_len; ++i)
8597 {
8598 str = ((char_u **)ga.ga_data)[i];
8599
8600 li = listitem_alloc();
8601 if (li == NULL)
8602 vim_free(str);
8603 else
8604 {
8605 li->li_tv.v_type = VAR_STRING;
8606 li->li_tv.v_lock = 0;
8607 li->li_tv.vval.v_string = str;
8608 list_append(rettv->vval.v_list, li);
8609 }
8610 }
8611 ga_clear(&ga);
8612 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008613 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614#endif
8615}
8616
8617 static void
8618f_split(typval_T *argvars, typval_T *rettv)
8619{
8620 char_u *str;
8621 char_u *end;
8622 char_u *pat = NULL;
8623 regmatch_T regmatch;
8624 char_u patbuf[NUMBUFLEN];
8625 char_u *save_cpo;
8626 int match;
8627 colnr_T col = 0;
8628 int keepempty = FALSE;
8629 int typeerr = FALSE;
8630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008631 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008633 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008635 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 if (argvars[1].v_type != VAR_UNKNOWN)
8637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008638 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 if (pat == NULL)
8640 typeerr = TRUE;
8641 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008642 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 }
8644 if (pat == NULL || *pat == NUL)
8645 pat = (char_u *)"[\\x01- ]\\+";
8646
8647 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008648 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008650 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651
8652 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8653 if (regmatch.regprog != NULL)
8654 {
8655 regmatch.rm_ic = FALSE;
8656 while (*str != NUL || keepempty)
8657 {
8658 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008659 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 else
8661 match = vim_regexec_nl(&regmatch, str, col);
8662 if (match)
8663 end = regmatch.startp[0];
8664 else
8665 end = str + STRLEN(str);
8666 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8667 && *str != NUL && match && end < regmatch.endp[0]))
8668 {
8669 if (list_append_string(rettv->vval.v_list, str,
8670 (int)(end - str)) == FAIL)
8671 break;
8672 }
8673 if (!match)
8674 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008675 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676 if (regmatch.endp[0] > str)
8677 col = 0;
8678 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008679 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 str = regmatch.endp[0];
8682 }
8683
8684 vim_regfree(regmatch.regprog);
8685 }
8686
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008687theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 p_cpo = save_cpo;
8689}
8690
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008692 * "str2list()" function
8693 */
8694 static void
8695f_str2list(typval_T *argvars, typval_T *rettv)
8696{
8697 char_u *p;
8698 int utf8 = FALSE;
8699
8700 if (rettv_list_alloc(rettv) == FAIL)
8701 return;
8702
8703 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008704 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008705
8706 p = tv_get_string(&argvars[0]);
8707
8708 if (has_mbyte || utf8)
8709 {
8710 int (*ptr2len)(char_u *);
8711 int (*ptr2char)(char_u *);
8712
8713 if (utf8 || enc_utf8)
8714 {
8715 ptr2len = utf_ptr2len;
8716 ptr2char = utf_ptr2char;
8717 }
8718 else
8719 {
8720 ptr2len = mb_ptr2len;
8721 ptr2char = mb_ptr2char;
8722 }
8723
8724 for ( ; *p != NUL; p += (*ptr2len)(p))
8725 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8726 }
8727 else
8728 for ( ; *p != NUL; ++p)
8729 list_append_number(rettv->vval.v_list, *p);
8730}
8731
8732/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 * "str2nr()" function
8734 */
8735 static void
8736f_str2nr(typval_T *argvars, typval_T *rettv)
8737{
8738 int base = 10;
8739 char_u *p;
8740 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008741 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008742 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743
8744 if (argvars[1].v_type != VAR_UNKNOWN)
8745 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008746 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 if (base != 2 && base != 8 && base != 10 && base != 16)
8748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008749 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 return;
8751 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008752 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008753 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 }
8755
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008756 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008757 isneg = (*p == '-');
8758 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 p = skipwhite(p + 1);
8760 switch (base)
8761 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008762 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008763 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008764 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008766 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8767 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008768 if (isneg)
8769 rettv->vval.v_number = -n;
8770 else
8771 rettv->vval.v_number = n;
8772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773}
8774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775/*
8776 * "strgetchar()" function
8777 */
8778 static void
8779f_strgetchar(typval_T *argvars, typval_T *rettv)
8780{
8781 char_u *str;
8782 int len;
8783 int error = FALSE;
8784 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008785 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786
8787 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008788 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 if (str == NULL)
8790 return;
8791 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008792 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 if (error)
8794 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795
Bram Moolenaar13505972019-01-24 15:04:48 +01008796 while (charidx >= 0 && byteidx < len)
8797 {
8798 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008800 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8801 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008803 --charidx;
8804 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806}
8807
8808/*
8809 * "stridx()" function
8810 */
8811 static void
8812f_stridx(typval_T *argvars, typval_T *rettv)
8813{
8814 char_u buf[NUMBUFLEN];
8815 char_u *needle;
8816 char_u *haystack;
8817 char_u *save_haystack;
8818 char_u *pos;
8819 int start_idx;
8820
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008821 needle = tv_get_string_chk(&argvars[1]);
8822 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008823 rettv->vval.v_number = -1;
8824 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008825 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008826
8827 if (argvars[2].v_type != VAR_UNKNOWN)
8828 {
8829 int error = FALSE;
8830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008831 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 if (error || start_idx >= (int)STRLEN(haystack))
8833 return;
8834 if (start_idx >= 0)
8835 haystack += start_idx;
8836 }
8837
8838 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8839 if (pos != NULL)
8840 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8841}
8842
8843/*
8844 * "string()" function
8845 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008846 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847f_string(typval_T *argvars, typval_T *rettv)
8848{
8849 char_u *tofree;
8850 char_u numbuf[NUMBUFLEN];
8851
8852 rettv->v_type = VAR_STRING;
8853 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8854 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008855 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 if (rettv->vval.v_string != NULL && tofree == NULL)
8857 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8858}
8859
8860/*
8861 * "strlen()" function
8862 */
8863 static void
8864f_strlen(typval_T *argvars, typval_T *rettv)
8865{
8866 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008867 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868}
8869
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008870 static void
8871strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8872{
8873 char_u *s = tv_get_string(&argvars[0]);
8874 varnumber_T len = 0;
8875 int (*func_mb_ptr2char_adv)(char_u **pp);
8876
8877 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8878 while (*s != NUL)
8879 {
8880 func_mb_ptr2char_adv(&s);
8881 ++len;
8882 }
8883 rettv->vval.v_number = len;
8884}
8885
8886/*
8887 * "strcharlen()" function
8888 */
8889 static void
8890f_strcharlen(typval_T *argvars, typval_T *rettv)
8891{
8892 strchar_common(argvars, rettv, TRUE);
8893}
8894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895/*
8896 * "strchars()" function
8897 */
8898 static void
8899f_strchars(typval_T *argvars, typval_T *rettv)
8900{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008901 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902
8903 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008904 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008906 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008908 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008909}
8910
8911/*
8912 * "strdisplaywidth()" function
8913 */
8914 static void
8915f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8916{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008917 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 int col = 0;
8919
8920 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008921 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922
8923 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8924}
8925
8926/*
8927 * "strwidth()" function
8928 */
8929 static void
8930f_strwidth(typval_T *argvars, typval_T *rettv)
8931{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008932 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933
Bram Moolenaar13505972019-01-24 15:04:48 +01008934 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935}
8936
8937/*
8938 * "strcharpart()" function
8939 */
8940 static void
8941f_strcharpart(typval_T *argvars, typval_T *rettv)
8942{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 char_u *p;
8944 int nchar;
8945 int nbyte = 0;
8946 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008947 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 int len = 0;
8949 int slen;
8950 int error = FALSE;
8951
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008952 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953 slen = (int)STRLEN(p);
8954
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008955 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 if (!error)
8957 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008958 if (argvars[2].v_type != VAR_UNKNOWN
8959 && argvars[3].v_type != VAR_UNKNOWN)
8960 {
8961 skipcc = tv_get_bool(&argvars[3]);
8962 if (skipcc < 0 || skipcc > 1)
8963 {
8964 semsg(_(e_using_number_as_bool_nr), skipcc);
8965 return;
8966 }
8967 }
8968
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 if (nchar > 0)
8970 while (nchar > 0 && nbyte < slen)
8971 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008972 if (skipcc)
8973 nbyte += mb_ptr2len(p + nbyte);
8974 else
8975 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 --nchar;
8977 }
8978 else
8979 nbyte = nchar;
8980 if (argvars[2].v_type != VAR_UNKNOWN)
8981 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008982 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983 while (charlen > 0 && nbyte + len < slen)
8984 {
8985 int off = nbyte + len;
8986
8987 if (off < 0)
8988 len += 1;
8989 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008990 {
8991 if (skipcc)
8992 len += mb_ptr2len(p + off);
8993 else
8994 len += MB_CPTR2LEN(p + off);
8995 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 --charlen;
8997 }
8998 }
8999 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009000 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009001 }
9002
9003 /*
9004 * Only return the overlap between the specified part and the actual
9005 * string.
9006 */
9007 if (nbyte < 0)
9008 {
9009 len += nbyte;
9010 nbyte = 0;
9011 }
9012 else if (nbyte > slen)
9013 nbyte = slen;
9014 if (len < 0)
9015 len = 0;
9016 else if (nbyte + len > slen)
9017 len = slen - nbyte;
9018
9019 rettv->v_type = VAR_STRING;
9020 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021}
9022
9023/*
9024 * "strpart()" function
9025 */
9026 static void
9027f_strpart(typval_T *argvars, typval_T *rettv)
9028{
9029 char_u *p;
9030 int n;
9031 int len;
9032 int slen;
9033 int error = FALSE;
9034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009035 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 slen = (int)STRLEN(p);
9037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009038 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 if (error)
9040 len = 0;
9041 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009042 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009044 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009046 // Only return the overlap between the specified part and the actual
9047 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 if (n < 0)
9049 {
9050 len += n;
9051 n = 0;
9052 }
9053 else if (n > slen)
9054 n = slen;
9055 if (len < 0)
9056 len = 0;
9057 else if (n + len > slen)
9058 len = slen - n;
9059
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009060 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9061 {
9062 int off;
9063
9064 // length in characters
9065 for (off = n; off < slen && len > 0; --len)
9066 off += mb_ptr2len(p + off);
9067 len = off - n;
9068 }
9069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 rettv->v_type = VAR_STRING;
9071 rettv->vval.v_string = vim_strnsave(p + n, len);
9072}
9073
9074/*
9075 * "strridx()" function
9076 */
9077 static void
9078f_strridx(typval_T *argvars, typval_T *rettv)
9079{
9080 char_u buf[NUMBUFLEN];
9081 char_u *needle;
9082 char_u *haystack;
9083 char_u *rest;
9084 char_u *lastmatch = NULL;
9085 int haystack_len, end_idx;
9086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009087 needle = tv_get_string_chk(&argvars[1]);
9088 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089
9090 rettv->vval.v_number = -1;
9091 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009092 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093
9094 haystack_len = (int)STRLEN(haystack);
9095 if (argvars[2].v_type != VAR_UNKNOWN)
9096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009097 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009098 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009100 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101 }
9102 else
9103 end_idx = haystack_len;
9104
9105 if (*needle == NUL)
9106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009107 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108 lastmatch = haystack + end_idx;
9109 }
9110 else
9111 {
9112 for (rest = haystack; *rest != '\0'; ++rest)
9113 {
9114 rest = (char_u *)strstr((char *)rest, (char *)needle);
9115 if (rest == NULL || rest > haystack + end_idx)
9116 break;
9117 lastmatch = rest;
9118 }
9119 }
9120
9121 if (lastmatch == NULL)
9122 rettv->vval.v_number = -1;
9123 else
9124 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9125}
9126
9127/*
9128 * "strtrans()" function
9129 */
9130 static void
9131f_strtrans(typval_T *argvars, typval_T *rettv)
9132{
9133 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009134 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135}
9136
9137/*
9138 * "submatch()" function
9139 */
9140 static void
9141f_submatch(typval_T *argvars, typval_T *rettv)
9142{
9143 int error = FALSE;
9144 int no;
9145 int retList = 0;
9146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009147 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 if (error)
9149 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009150 if (no < 0 || no >= NSUBEXP)
9151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009152 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009153 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009154 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009156 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 if (error)
9158 return;
9159
9160 if (retList == 0)
9161 {
9162 rettv->v_type = VAR_STRING;
9163 rettv->vval.v_string = reg_submatch(no);
9164 }
9165 else
9166 {
9167 rettv->v_type = VAR_LIST;
9168 rettv->vval.v_list = reg_submatch_list(no);
9169 }
9170}
9171
9172/*
9173 * "substitute()" function
9174 */
9175 static void
9176f_substitute(typval_T *argvars, typval_T *rettv)
9177{
9178 char_u patbuf[NUMBUFLEN];
9179 char_u subbuf[NUMBUFLEN];
9180 char_u flagsbuf[NUMBUFLEN];
9181
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009182 char_u *str = tv_get_string_chk(&argvars[0]);
9183 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009184 char_u *sub = NULL;
9185 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009186 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009188 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9189 expr = &argvars[2];
9190 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009191 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009192
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009194 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9195 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 rettv->vval.v_string = NULL;
9197 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009198 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199}
9200
9201/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009202 * "swapinfo(swap_filename)" function
9203 */
9204 static void
9205f_swapinfo(typval_T *argvars, typval_T *rettv)
9206{
9207 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009208 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009209}
9210
9211/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009212 * "swapname(expr)" function
9213 */
9214 static void
9215f_swapname(typval_T *argvars, typval_T *rettv)
9216{
9217 buf_T *buf;
9218
9219 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009220 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009221 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9222 || buf->b_ml.ml_mfp->mf_fname == NULL)
9223 rettv->vval.v_string = NULL;
9224 else
9225 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9226}
9227
9228/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229 * "synID(lnum, col, trans)" function
9230 */
9231 static void
9232f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9233{
9234 int id = 0;
9235#ifdef FEAT_SYN_HL
9236 linenr_T lnum;
9237 colnr_T col;
9238 int trans;
9239 int transerr = FALSE;
9240
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009241 lnum = tv_get_lnum(argvars); // -1 on type error
9242 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009243 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244
9245 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9246 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9247 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9248#endif
9249
9250 rettv->vval.v_number = id;
9251}
9252
9253/*
9254 * "synIDattr(id, what [, mode])" function
9255 */
9256 static void
9257f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9258{
9259 char_u *p = NULL;
9260#ifdef FEAT_SYN_HL
9261 int id;
9262 char_u *what;
9263 char_u *mode;
9264 char_u modebuf[NUMBUFLEN];
9265 int modec;
9266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009267 id = (int)tv_get_number(&argvars[0]);
9268 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 if (argvars[2].v_type != VAR_UNKNOWN)
9270 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009271 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 modec = TOLOWER_ASC(mode[0]);
9273 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009274 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 }
9276 else
9277 {
9278#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9279 if (USE_24BIT)
9280 modec = 'g';
9281 else
9282#endif
9283 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009284 modec = 'c';
9285 else
9286 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 }
9288
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 switch (TOLOWER_ASC(what[0]))
9290 {
9291 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009292 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009294 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 p = highlight_has_attr(id, HL_BOLD, modec);
9296 break;
9297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009298 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299 p = highlight_color(id, what, modec);
9300 break;
9301
9302 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009303 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009305 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 p = highlight_has_attr(id, HL_ITALIC, modec);
9307 break;
9308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009309 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009310 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 break;
9312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009313 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314 p = highlight_has_attr(id, HL_INVERSE, modec);
9315 break;
9316
9317 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009318 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009320 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009321 else if (TOLOWER_ASC(what[1]) == 't' &&
9322 TOLOWER_ASC(what[2]) == 'r')
9323 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009324 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 p = highlight_has_attr(id, HL_STANDOUT, modec);
9326 break;
9327
9328 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009329 if (TOLOWER_ASC(what[1]) == 'l') // ul
9330 p = highlight_color(id, what, modec);
9331 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009332 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9334 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009335 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009336 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9337 break;
9338 }
9339
9340 if (p != NULL)
9341 p = vim_strsave(p);
9342#endif
9343 rettv->v_type = VAR_STRING;
9344 rettv->vval.v_string = p;
9345}
9346
9347/*
9348 * "synIDtrans(id)" function
9349 */
9350 static void
9351f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9352{
9353 int id;
9354
9355#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009356 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357
9358 if (id > 0)
9359 id = syn_get_final_id(id);
9360 else
9361#endif
9362 id = 0;
9363
9364 rettv->vval.v_number = id;
9365}
9366
9367/*
9368 * "synconcealed(lnum, col)" function
9369 */
9370 static void
9371f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9372{
9373#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9374 linenr_T lnum;
9375 colnr_T col;
9376 int syntax_flags = 0;
9377 int cchar;
9378 int matchid = 0;
9379 char_u str[NUMBUFLEN];
9380#endif
9381
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009382 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383
9384#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009385 lnum = tv_get_lnum(argvars); // -1 on type error
9386 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387
Bram Moolenaara80faa82020-04-12 19:37:17 +02009388 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389
9390 if (rettv_list_alloc(rettv) != FAIL)
9391 {
9392 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9393 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9394 && curwin->w_p_cole > 0)
9395 {
9396 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9397 syntax_flags = get_syntax_info(&matchid);
9398
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009399 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009400 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9401 {
9402 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009403 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009404 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9405 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009406 if (cchar != NUL)
9407 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 if (has_mbyte)
9409 (*mb_char2bytes)(cchar, str);
9410 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 str[0] = cchar;
9412 }
9413 }
9414 }
9415
9416 list_append_number(rettv->vval.v_list,
9417 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009418 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 list_append_string(rettv->vval.v_list, str, -1);
9420 list_append_number(rettv->vval.v_list, matchid);
9421 }
9422#endif
9423}
9424
9425/*
9426 * "synstack(lnum, col)" function
9427 */
9428 static void
9429f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9430{
9431#ifdef FEAT_SYN_HL
9432 linenr_T lnum;
9433 colnr_T col;
9434 int i;
9435 int id;
9436#endif
9437
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009438 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439
9440#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009441 lnum = tv_get_lnum(argvars); // -1 on type error
9442 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443
9444 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9445 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9446 && rettv_list_alloc(rettv) != FAIL)
9447 {
9448 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9449 for (i = 0; ; ++i)
9450 {
9451 id = syn_get_stack_item(i);
9452 if (id < 0)
9453 break;
9454 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9455 break;
9456 }
9457 }
9458#endif
9459}
9460
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461/*
9462 * "tabpagebuflist()" function
9463 */
9464 static void
9465f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9466{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467 tabpage_T *tp;
9468 win_T *wp = NULL;
9469
9470 if (argvars[0].v_type == VAR_UNKNOWN)
9471 wp = firstwin;
9472 else
9473 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009474 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 if (tp != NULL)
9476 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9477 }
9478 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9479 {
9480 for (; wp != NULL; wp = wp->w_next)
9481 if (list_append_number(rettv->vval.v_list,
9482 wp->w_buffer->b_fnum) == FAIL)
9483 break;
9484 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485}
9486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 * "tagfiles()" function
9489 */
9490 static void
9491f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9492{
9493 char_u *fname;
9494 tagname_T tn;
9495 int first;
9496
9497 if (rettv_list_alloc(rettv) == FAIL)
9498 return;
9499 fname = alloc(MAXPATHL);
9500 if (fname == NULL)
9501 return;
9502
9503 for (first = TRUE; ; first = FALSE)
9504 if (get_tagfname(&tn, first, fname) == FAIL
9505 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9506 break;
9507 tagname_free(&tn);
9508 vim_free(fname);
9509}
9510
9511/*
9512 * "taglist()" function
9513 */
9514 static void
9515f_taglist(typval_T *argvars, typval_T *rettv)
9516{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009517 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518 char_u *tag_pattern;
9519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009520 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521
9522 rettv->vval.v_number = FALSE;
9523 if (*tag_pattern == NUL)
9524 return;
9525
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009526 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009529 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530}
9531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009532/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533 * "tolower(string)" function
9534 */
9535 static void
9536f_tolower(typval_T *argvars, typval_T *rettv)
9537{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009539 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540}
9541
9542/*
9543 * "toupper(string)" function
9544 */
9545 static void
9546f_toupper(typval_T *argvars, typval_T *rettv)
9547{
9548 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009549 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550}
9551
9552/*
9553 * "tr(string, fromstr, tostr)" function
9554 */
9555 static void
9556f_tr(typval_T *argvars, typval_T *rettv)
9557{
9558 char_u *in_str;
9559 char_u *fromstr;
9560 char_u *tostr;
9561 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009562 int inlen;
9563 int fromlen;
9564 int tolen;
9565 int idx;
9566 char_u *cpstr;
9567 int cplen;
9568 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 char_u buf[NUMBUFLEN];
9570 char_u buf2[NUMBUFLEN];
9571 garray_T ga;
9572
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009573 in_str = tv_get_string(&argvars[0]);
9574 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9575 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009577 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009578 rettv->v_type = VAR_STRING;
9579 rettv->vval.v_string = NULL;
9580 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009581 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 ga_init2(&ga, (int)sizeof(char), 80);
9583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009585 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 if (STRLEN(fromstr) != STRLEN(tostr))
9587 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009589 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 ga_clear(&ga);
9591 return;
9592 }
9593
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009594 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009595 while (*in_str != NUL)
9596 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 if (has_mbyte)
9598 {
9599 inlen = (*mb_ptr2len)(in_str);
9600 cpstr = in_str;
9601 cplen = inlen;
9602 idx = 0;
9603 for (p = fromstr; *p != NUL; p += fromlen)
9604 {
9605 fromlen = (*mb_ptr2len)(p);
9606 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9607 {
9608 for (p = tostr; *p != NUL; p += tolen)
9609 {
9610 tolen = (*mb_ptr2len)(p);
9611 if (idx-- == 0)
9612 {
9613 cplen = tolen;
9614 cpstr = p;
9615 break;
9616 }
9617 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009618 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619 goto error;
9620 break;
9621 }
9622 ++idx;
9623 }
9624
9625 if (first && cpstr == in_str)
9626 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009627 // Check that fromstr and tostr have the same number of
9628 // (multi-byte) characters. Done only once when a character
9629 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 first = FALSE;
9631 for (p = tostr; *p != NUL; p += tolen)
9632 {
9633 tolen = (*mb_ptr2len)(p);
9634 --idx;
9635 }
9636 if (idx != 0)
9637 goto error;
9638 }
9639
9640 (void)ga_grow(&ga, cplen);
9641 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9642 ga.ga_len += cplen;
9643
9644 in_str += inlen;
9645 }
9646 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009648 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 p = vim_strchr(fromstr, *in_str);
9650 if (p != NULL)
9651 ga_append(&ga, tostr[p - fromstr]);
9652 else
9653 ga_append(&ga, *in_str);
9654 ++in_str;
9655 }
9656 }
9657
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009658 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 (void)ga_grow(&ga, 1);
9660 ga_append(&ga, NUL);
9661
9662 rettv->vval.v_string = ga.ga_data;
9663}
9664
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009665/*
9666 * "trim({expr})" function
9667 */
9668 static void
9669f_trim(typval_T *argvars, typval_T *rettv)
9670{
9671 char_u buf1[NUMBUFLEN];
9672 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009673 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009674 char_u *mask = NULL;
9675 char_u *tail;
9676 char_u *prev;
9677 char_u *p;
9678 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009679 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009680
9681 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009682 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009683 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009684 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009685
9686 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009688 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009689
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009690 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009691 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009692 int error = 0;
9693
9694 // leading or trailing characters to trim
9695 dir = (int)tv_get_number_chk(&argvars[2], &error);
9696 if (error)
9697 return;
9698 if (dir < 0 || dir > 2)
9699 {
9700 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9701 return;
9702 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009703 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009704 }
9705
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009706 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009707 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009708 // Trim leading characters
9709 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009710 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009711 c1 = PTR2CHAR(head);
9712 if (mask == NULL)
9713 {
9714 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009715 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009716 }
9717 else
9718 {
9719 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9720 if (c1 == PTR2CHAR(p))
9721 break;
9722 if (*p == NUL)
9723 break;
9724 }
9725 MB_PTR_ADV(head);
9726 }
9727 }
9728
9729 tail = head + STRLEN(head);
9730 if (dir == 0 || dir == 2)
9731 {
9732 // Trim trailing characters
9733 for (; tail > head; tail = prev)
9734 {
9735 prev = tail;
9736 MB_PTR_BACK(head, prev);
9737 c1 = PTR2CHAR(prev);
9738 if (mask == NULL)
9739 {
9740 if (c1 > ' ' && c1 != 0xa0)
9741 break;
9742 }
9743 else
9744 {
9745 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9746 if (c1 == PTR2CHAR(p))
9747 break;
9748 if (*p == NUL)
9749 break;
9750 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009751 }
9752 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009753 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009754}
9755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756/*
9757 * "type(expr)" function
9758 */
9759 static void
9760f_type(typval_T *argvars, typval_T *rettv)
9761{
9762 int n = -1;
9763
9764 switch (argvars[0].v_type)
9765 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009766 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9767 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009769 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9770 case VAR_LIST: n = VAR_TYPE_LIST; break;
9771 case VAR_DICT: n = VAR_TYPE_DICT; break;
9772 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9773 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9774 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009775 case VAR_JOB: n = VAR_TYPE_JOB; break;
9776 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009777 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009778 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009780 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009781 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009782 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 n = -1;
9784 break;
9785 }
9786 rettv->vval.v_number = n;
9787}
9788
9789/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 * "virtcol(string)" function
9791 */
9792 static void
9793f_virtcol(typval_T *argvars, typval_T *rettv)
9794{
9795 colnr_T vcol = 0;
9796 pos_T *fp;
9797 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009798 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009800 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9802 && fnum == curbuf->b_fnum)
9803 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009804 // Limit the column to a valid value, getvvcol() doesn't check.
9805 if (fp->col < 0)
9806 fp->col = 0;
9807 else
9808 {
9809 len = (int)STRLEN(ml_get(fp->lnum));
9810 if (fp->col > len)
9811 fp->col = len;
9812 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009813 getvvcol(curwin, fp, NULL, NULL, &vcol);
9814 ++vcol;
9815 }
9816
9817 rettv->vval.v_number = vcol;
9818}
9819
9820/*
9821 * "visualmode()" function
9822 */
9823 static void
9824f_visualmode(typval_T *argvars, typval_T *rettv)
9825{
9826 char_u str[2];
9827
9828 rettv->v_type = VAR_STRING;
9829 str[0] = curbuf->b_visual_mode_eval;
9830 str[1] = NUL;
9831 rettv->vval.v_string = vim_strsave(str);
9832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009833 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 if (non_zero_arg(&argvars[0]))
9835 curbuf->b_visual_mode_eval = NUL;
9836}
9837
9838/*
9839 * "wildmenumode()" function
9840 */
9841 static void
9842f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9843{
9844#ifdef FEAT_WILDMENU
9845 if (wild_menu_showing)
9846 rettv->vval.v_number = 1;
9847#endif
9848}
9849
9850/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009851 * "windowsversion()" function
9852 */
9853 static void
9854f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9855{
9856 rettv->v_type = VAR_STRING;
9857 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9858}
9859
9860/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009861 * "wordcount()" function
9862 */
9863 static void
9864f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9865{
9866 if (rettv_dict_alloc(rettv) == FAIL)
9867 return;
9868 cursor_pos_info(rettv->vval.v_dict);
9869}
9870
9871/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009872 * "xor(expr, expr)" function
9873 */
9874 static void
9875f_xor(typval_T *argvars, typval_T *rettv)
9876{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9878 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879}
9880
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009881#endif // FEAT_EVAL