blob: 9267117daa70f80fd4d0ef300964b445ddb8d9a7 [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 Moolenaarfbcbffe2020-10-31 19:33:38 +0100305 * Check "type" is a list or a dict.
306 */
307 static int
308arg_list_or_dict(type_T *type, argcontext_T *context)
309{
310 if (type->tt_type == VAR_ANY
311 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
312 return OK;
313 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
314 return FAIL;
315}
316
317/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100318 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100319 * Must not be used for the first argcheck_T entry.
320 */
321 static int
322arg_same_as_prev(type_T *type, argcontext_T *context)
323{
324 type_T *prev_type = context->arg_types[context->arg_idx - 1];
325
Bram Moolenaar351ead02021-01-16 16:07:01 +0100326 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100327}
328
329/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100330 * Check "type" is the same basic type as the previous argument, checks list or
331 * dict vs other type, but not member type.
332 * Must not be used for the first argcheck_T entry.
333 */
334 static int
335arg_same_struct_as_prev(type_T *type, argcontext_T *context)
336{
337 type_T *prev_type = context->arg_types[context->arg_idx - 1];
338
339 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100340 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100341 return OK;
342}
343
344/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100345 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200346 * Must not be used for the first argcheck_T entry.
347 */
348 static int
349arg_item_of_prev(type_T *type, argcontext_T *context)
350{
351 type_T *prev_type = context->arg_types[context->arg_idx - 1];
352 type_T *expected;
353
354 if (prev_type->tt_type == VAR_LIST)
355 expected = prev_type->tt_member;
356 else if (prev_type->tt_type == VAR_BLOB)
357 expected = &t_number;
358 else
359 // probably VAR_ANY, can't check
360 return OK;
361
Bram Moolenaar351ead02021-01-16 16:07:01 +0100362 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200363}
364
Bram Moolenaar94738d82020-10-21 14:25:07 +0200365/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100366 * Check "type" which is the third argument of extend().
367 */
368 static int
369arg_extend3(type_T *type, argcontext_T *context)
370{
371 type_T *first_type = context->arg_types[context->arg_idx - 2];
372
373 if (first_type->tt_type == VAR_LIST)
374 return arg_number(type, context);
375 if (first_type->tt_type == VAR_DICT)
376 return arg_string(type, context);
377 return OK;
378}
379
380
381/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200382 * Lists of functions that check the argument types of a builtin function.
383 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100384argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100385argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200386argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
388argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100389argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200390argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200391
392/*
393 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200394 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200395 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100396 static type_T *
397ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
398{
399 return &t_void;
400}
401 static type_T *
402ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
403{
404 return &t_any;
405}
406 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200407ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
408{
409 return &t_bool;
410}
411 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100412ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
413{
414 return &t_number_bool;
415}
416 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100417ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
418{
419 return &t_number;
420}
421 static type_T *
422ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
423{
424 return &t_float;
425}
426 static type_T *
427ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
428{
429 return &t_string;
430}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200431 static type_T *
432ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100433{
434 return &t_list_any;
435}
436 static type_T *
437ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_list_number;
440}
441 static type_T *
442ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_list_string;
445}
446 static type_T *
447ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_list_dict_any;
450}
451 static type_T *
452ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_dict_any;
455}
456 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100457ret_job_info(int argcount, type_T **argtypes UNUSED)
458{
459 if (argcount == 0)
460 return &t_list_job;
461 return &t_dict_any;
462}
463 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100464ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
465{
466 return &t_dict_number;
467}
468 static type_T *
469ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
470{
471 return &t_dict_string;
472}
473 static type_T *
474ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
475{
476 return &t_blob;
477}
478 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200479ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100480{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200481 return &t_func_any;
482}
483 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100484ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
485{
486 return &t_channel;
487}
488 static type_T *
489ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
490{
491 return &t_job;
492}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200493 static type_T *
494ret_first_arg(int argcount, type_T **argtypes)
495{
496 if (argcount > 0)
497 return argtypes[0];
498 return &t_void;
499}
Bram Moolenaarea696852020-11-09 18:31:39 +0100500// for map(): returns first argument but item type may differ
501 static type_T *
502ret_first_cont(int argcount UNUSED, type_T **argtypes)
503{
504 if (argtypes[0]->tt_type == VAR_LIST)
505 return &t_list_any;
506 if (argtypes[0]->tt_type == VAR_DICT)
507 return &t_dict_any;
508 if (argtypes[0]->tt_type == VAR_BLOB)
509 return argtypes[0];
510 return &t_any;
511}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200512
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200513/*
514 * Used for getqflist(): returns list if there is no argument, dict if there is
515 * one.
516 */
517 static type_T *
518ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
519{
520 if (argcount > 0)
521 return &t_dict_any;
522 return &t_list_dict_any;
523}
524
525/*
526 * Used for getloclist(): returns list if there is one argument, dict if there
527 * are two.
528 */
529 static type_T *
530ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
531{
532 if (argcount > 1)
533 return &t_dict_any;
534 return &t_list_dict_any;
535}
536
Bram Moolenaar846178a2020-07-05 17:04:13 +0200537 static type_T *
538ret_argv(int argcount, type_T **argtypes UNUSED)
539{
540 // argv() returns list of strings
541 if (argcount == 0)
542 return &t_list_string;
543
544 // argv(0) returns a string, but argv(-1] returns a list
545 return &t_any;
546}
547
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200548 static type_T *
549ret_remove(int argcount UNUSED, type_T **argtypes)
550{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200551 if (argtypes != NULL)
552 {
553 if (argtypes[0]->tt_type == VAR_LIST
554 || argtypes[0]->tt_type == VAR_DICT)
555 return argtypes[0]->tt_member;
556 if (argtypes[0]->tt_type == VAR_BLOB)
557 return &t_number;
558 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200559 return &t_any;
560}
561
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200562 static type_T *
563ret_getreg(int argcount, type_T **argtypes UNUSED)
564{
565 // Assume that if the third argument is passed it's non-zero
566 if (argcount == 3)
567 return &t_list_string;
568 return &t_string;
569}
570
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200571 static type_T *
572ret_maparg(int argcount, type_T **argtypes UNUSED)
573{
574 // Assume that if the fourth argument is passed it's non-zero
575 if (argcount == 4)
576 return &t_dict_any;
577 return &t_string;
578}
579
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580static type_T *ret_f_function(int argcount, type_T **argtypes);
581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582/*
583 * Array with names and number of arguments of all internal functions
584 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
585 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200586typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200588 char *f_name; // function name
589 char f_min_argc; // minimal number of arguments
590 char f_max_argc; // maximal number of arguments
591 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200592 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100593 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
594 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200596 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200597} funcentry_T;
598
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200599// values for f_argtype; zero means it cannot be used as a method
600#define FEARG_1 1 // base is the first argument
601#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200602#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200603#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200604#define FEARG_LAST 9 // base is the last argument
605
Bram Moolenaar15c47602020-03-26 22:16:48 +0100606#ifdef FEAT_FLOAT
607# define FLOAT_FUNC(name) name
608#else
609# define FLOAT_FUNC(name) NULL
610#endif
611#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
612# define MATH_FUNC(name) name
613#else
614# define MATH_FUNC(name) NULL
615#endif
616#ifdef FEAT_TIMERS
617# define TIMER_FUNC(name) name
618#else
619# define TIMER_FUNC(name) NULL
620#endif
621#ifdef FEAT_JOB_CHANNEL
622# define JOB_FUNC(name) name
623#else
624# define JOB_FUNC(name) NULL
625#endif
626#ifdef FEAT_PROP_POPUP
627# define PROP_FUNC(name) name
628#else
629# define PROP_FUNC(name) NULL
630#endif
631#ifdef FEAT_SIGNS
632# define SIGN_FUNC(name) name
633#else
634# define SIGN_FUNC(name) NULL
635#endif
636#ifdef FEAT_SOUND
637# define SOUND_FUNC(name) name
638#else
639# define SOUND_FUNC(name) NULL
640#endif
641#ifdef FEAT_TERMINAL
642# define TERM_FUNC(name) name
643#else
644# define TERM_FUNC(name) NULL
645#endif
646
Bram Moolenaarac92e252019-08-03 21:58:38 +0200647static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200649 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
650 ret_any, FLOAT_FUNC(f_abs)},
651 {"acos", 1, 1, FEARG_1, NULL,
652 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100653 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200654 ret_first_arg, f_add},
655 {"and", 2, 2, FEARG_1, NULL,
656 ret_number, f_and},
657 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100658 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200659 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100660 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200661 {"argc", 0, 1, 0, NULL,
662 ret_number, f_argc},
663 {"argidx", 0, 0, 0, NULL,
664 ret_number, f_argidx},
665 {"arglistid", 0, 2, 0, NULL,
666 ret_number, f_arglistid},
667 {"argv", 0, 2, 0, NULL,
668 ret_argv, f_argv},
669 {"asin", 1, 1, FEARG_1, NULL,
670 ret_float, FLOAT_FUNC(f_asin)},
671 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100672 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200673 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100674 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200675 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100676 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200677 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100678 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200679 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100680 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200681 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100682 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200683 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100684 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200685 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100686 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200687 {"assert_nobeep", 1, 2, FEARG_1, NULL,
688 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100690 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200691 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100692 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200693 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100694 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200695 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100696 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200697 {"atan", 1, 1, FEARG_1, NULL,
698 ret_float, FLOAT_FUNC(f_atan)},
699 {"atan2", 2, 2, FEARG_1, NULL,
700 ret_float, FLOAT_FUNC(f_atan2)},
701 {"balloon_gettext", 0, 0, 0, NULL,
702 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100703#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100704 f_balloon_gettext
705#else
706 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100707#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 {"balloon_show", 1, 1, FEARG_1, NULL,
710 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100711#ifdef FEAT_BEVAL
712 f_balloon_show
713#else
714 NULL
715#endif
716 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"balloon_split", 1, 1, FEARG_1, NULL,
718 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100719#if defined(FEAT_BEVAL_TERM)
720 f_balloon_split
721#else
722 NULL
723#endif
724 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200725 {"browse", 4, 4, 0, NULL,
726 ret_string, f_browse},
727 {"browsedir", 2, 2, 0, NULL,
728 ret_string, f_browsedir},
729 {"bufadd", 1, 1, FEARG_1, NULL,
730 ret_number, f_bufadd},
731 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200733 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200735 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
736 ret_string, f_bufname},
737 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
738 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"bufload", 1, 1, FEARG_1, NULL,
742 ret_void, f_bufload},
743 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"bufname", 0, 1, FEARG_1, NULL,
746 ret_string, f_bufname},
747 {"bufnr", 0, 2, FEARG_1, NULL,
748 ret_number, f_bufnr},
749 {"bufwinid", 1, 1, FEARG_1, NULL,
750 ret_number, f_bufwinid},
751 {"bufwinnr", 1, 1, FEARG_1, NULL,
752 ret_number, f_bufwinnr},
753 {"byte2line", 1, 1, FEARG_1, NULL,
754 ret_number, f_byte2line},
755 {"byteidx", 2, 2, FEARG_1, NULL,
756 ret_number, f_byteidx},
757 {"byteidxcomp", 2, 2, FEARG_1, NULL,
758 ret_number, f_byteidxcomp},
759 {"call", 2, 3, FEARG_1, NULL,
760 ret_any, f_call},
761 {"ceil", 1, 1, FEARG_1, NULL,
762 ret_float, FLOAT_FUNC(f_ceil)},
763 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100764 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200765 {"ch_close", 1, 1, FEARG_1, NULL,
766 ret_void, JOB_FUNC(f_ch_close)},
767 {"ch_close_in", 1, 1, FEARG_1, NULL,
768 ret_void, JOB_FUNC(f_ch_close_in)},
769 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
770 ret_any, JOB_FUNC(f_ch_evalexpr)},
771 {"ch_evalraw", 2, 3, FEARG_1, NULL,
772 ret_any, JOB_FUNC(f_ch_evalraw)},
773 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
774 ret_number, JOB_FUNC(f_ch_getbufnr)},
775 {"ch_getjob", 1, 1, FEARG_1, NULL,
776 ret_job, JOB_FUNC(f_ch_getjob)},
777 {"ch_info", 1, 1, FEARG_1, NULL,
778 ret_dict_any, JOB_FUNC(f_ch_info)},
779 {"ch_log", 1, 2, FEARG_1, NULL,
780 ret_void, JOB_FUNC(f_ch_log)},
781 {"ch_logfile", 1, 2, FEARG_1, NULL,
782 ret_void, JOB_FUNC(f_ch_logfile)},
783 {"ch_open", 1, 2, FEARG_1, NULL,
784 ret_channel, JOB_FUNC(f_ch_open)},
785 {"ch_read", 1, 2, FEARG_1, NULL,
786 ret_string, JOB_FUNC(f_ch_read)},
787 {"ch_readblob", 1, 2, FEARG_1, NULL,
788 ret_blob, JOB_FUNC(f_ch_readblob)},
789 {"ch_readraw", 1, 2, FEARG_1, NULL,
790 ret_string, JOB_FUNC(f_ch_readraw)},
791 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
792 ret_void, JOB_FUNC(f_ch_sendexpr)},
793 {"ch_sendraw", 2, 3, FEARG_1, NULL,
794 ret_void, JOB_FUNC(f_ch_sendraw)},
795 {"ch_setoptions", 2, 2, FEARG_1, NULL,
796 ret_void, JOB_FUNC(f_ch_setoptions)},
797 {"ch_status", 1, 2, FEARG_1, NULL,
798 ret_string, JOB_FUNC(f_ch_status)},
799 {"changenr", 0, 0, 0, NULL,
800 ret_number, f_changenr},
801 {"char2nr", 1, 2, FEARG_1, NULL,
802 ret_number, f_char2nr},
803 {"charclass", 1, 1, FEARG_1, NULL,
804 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100805 {"charcol", 1, 1, FEARG_1, NULL,
806 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100807 {"charidx", 2, 3, FEARG_1, NULL,
808 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200809 {"chdir", 1, 1, FEARG_1, NULL,
810 ret_string, f_chdir},
811 {"cindent", 1, 1, FEARG_1, NULL,
812 ret_number, f_cindent},
813 {"clearmatches", 0, 1, FEARG_1, NULL,
814 ret_void, f_clearmatches},
815 {"col", 1, 1, FEARG_1, NULL,
816 ret_number, f_col},
817 {"complete", 2, 2, FEARG_2, NULL,
818 ret_void, f_complete},
819 {"complete_add", 1, 1, FEARG_1, NULL,
820 ret_number, f_complete_add},
821 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100822 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200823 {"complete_info", 0, 1, FEARG_1, NULL,
824 ret_dict_any, f_complete_info},
825 {"confirm", 1, 4, FEARG_1, NULL,
826 ret_number, f_confirm},
827 {"copy", 1, 1, FEARG_1, NULL,
828 ret_first_arg, f_copy},
829 {"cos", 1, 1, FEARG_1, NULL,
830 ret_float, FLOAT_FUNC(f_cos)},
831 {"cosh", 1, 1, FEARG_1, NULL,
832 ret_float, FLOAT_FUNC(f_cosh)},
833 {"count", 2, 4, FEARG_1, NULL,
834 ret_number, f_count},
835 {"cscope_connection",0,3, 0, NULL,
836 ret_number, f_cscope_connection},
837 {"cursor", 1, 3, FEARG_1, NULL,
838 ret_number, f_cursor},
839 {"debugbreak", 1, 1, FEARG_1, NULL,
840 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 f_debugbreak
843#else
844 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200845#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200847 {"deepcopy", 1, 2, FEARG_1, NULL,
848 ret_first_arg, f_deepcopy},
849 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100850 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200851 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100852 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200853 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100854 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200855 {"diff_filler", 1, 1, FEARG_1, NULL,
856 ret_number, f_diff_filler},
857 {"diff_hlID", 2, 2, FEARG_1, NULL,
858 ret_number, f_diff_hlID},
859 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100860 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100862 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200863 {"environ", 0, 0, 0, NULL,
864 ret_dict_string, f_environ},
865 {"escape", 2, 2, FEARG_1, NULL,
866 ret_string, f_escape},
867 {"eval", 1, 1, FEARG_1, NULL,
868 ret_any, f_eval},
869 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100870 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200871 {"executable", 1, 1, FEARG_1, NULL,
872 ret_number, f_executable},
873 {"execute", 1, 2, FEARG_1, NULL,
874 ret_string, f_execute},
875 {"exepath", 1, 1, FEARG_1, NULL,
876 ret_string, f_exepath},
877 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100878 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200879 {"exp", 1, 1, FEARG_1, NULL,
880 ret_float, FLOAT_FUNC(f_exp)},
881 {"expand", 1, 3, FEARG_1, NULL,
882 ret_any, f_expand},
883 {"expandcmd", 1, 1, FEARG_1, NULL,
884 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100885 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200886 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100887 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
888 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200889 {"feedkeys", 1, 2, FEARG_1, NULL,
890 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200891 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100892 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200893 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100894 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200895 {"filewritable", 1, 1, FEARG_1, NULL,
896 ret_number, f_filewritable},
897 {"filter", 2, 2, FEARG_1, NULL,
898 ret_first_arg, f_filter},
899 {"finddir", 1, 3, FEARG_1, NULL,
900 ret_string, f_finddir},
901 {"findfile", 1, 3, FEARG_1, NULL,
902 ret_string, f_findfile},
903 {"flatten", 1, 2, FEARG_1, NULL,
904 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100905 {"flattennew", 1, 2, FEARG_1, NULL,
906 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"float2nr", 1, 1, FEARG_1, NULL,
908 ret_number, FLOAT_FUNC(f_float2nr)},
909 {"floor", 1, 1, FEARG_1, NULL,
910 ret_float, FLOAT_FUNC(f_floor)},
911 {"fmod", 2, 2, FEARG_1, NULL,
912 ret_float, FLOAT_FUNC(f_fmod)},
913 {"fnameescape", 1, 1, FEARG_1, NULL,
914 ret_string, f_fnameescape},
915 {"fnamemodify", 2, 2, FEARG_1, NULL,
916 ret_string, f_fnamemodify},
917 {"foldclosed", 1, 1, FEARG_1, NULL,
918 ret_number, f_foldclosed},
919 {"foldclosedend", 1, 1, FEARG_1, NULL,
920 ret_number, f_foldclosedend},
921 {"foldlevel", 1, 1, FEARG_1, NULL,
922 ret_number, f_foldlevel},
923 {"foldtext", 0, 0, 0, NULL,
924 ret_string, f_foldtext},
925 {"foldtextresult", 1, 1, FEARG_1, NULL,
926 ret_string, f_foldtextresult},
927 {"foreground", 0, 0, 0, NULL,
928 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100929 {"fullcommand", 1, 1, FEARG_1, arg1_string,
930 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"funcref", 1, 3, FEARG_1, NULL,
932 ret_func_any, f_funcref},
933 {"function", 1, 3, FEARG_1, NULL,
934 ret_f_function, f_function},
935 {"garbagecollect", 0, 1, 0, NULL,
936 ret_void, f_garbagecollect},
937 {"get", 2, 3, FEARG_1, NULL,
938 ret_any, f_get},
939 {"getbufinfo", 0, 1, FEARG_1, NULL,
940 ret_list_dict_any, f_getbufinfo},
941 {"getbufline", 2, 3, FEARG_1, NULL,
942 ret_list_string, f_getbufline},
943 {"getbufvar", 2, 3, FEARG_1, NULL,
944 ret_any, f_getbufvar},
945 {"getchangelist", 0, 1, FEARG_1, NULL,
946 ret_list_any, f_getchangelist},
947 {"getchar", 0, 1, 0, NULL,
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200948 ret_any, f_getchar},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200949 {"getcharmod", 0, 0, 0, NULL,
950 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100951 {"getcharpos", 1, 1, FEARG_1, NULL,
952 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200953 {"getcharsearch", 0, 0, 0, NULL,
954 ret_dict_any, f_getcharsearch},
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200955 {"getcharstr", 0, 1, 0, NULL,
956 ret_string, f_getcharstr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200957 {"getcmdline", 0, 0, 0, NULL,
958 ret_string, f_getcmdline},
959 {"getcmdpos", 0, 0, 0, NULL,
960 ret_number, f_getcmdpos},
961 {"getcmdtype", 0, 0, 0, NULL,
962 ret_string, f_getcmdtype},
963 {"getcmdwintype", 0, 0, 0, NULL,
964 ret_string, f_getcmdwintype},
965 {"getcompletion", 2, 3, FEARG_1, NULL,
966 ret_list_string, f_getcompletion},
967 {"getcurpos", 0, 1, FEARG_1, NULL,
968 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100969 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
970 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200971 {"getcwd", 0, 2, FEARG_1, NULL,
972 ret_string, f_getcwd},
973 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100974 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200975 {"getfontname", 0, 1, 0, NULL,
976 ret_string, f_getfontname},
977 {"getfperm", 1, 1, FEARG_1, NULL,
978 ret_string, f_getfperm},
979 {"getfsize", 1, 1, FEARG_1, NULL,
980 ret_number, f_getfsize},
981 {"getftime", 1, 1, FEARG_1, NULL,
982 ret_number, f_getftime},
983 {"getftype", 1, 1, FEARG_1, NULL,
984 ret_string, f_getftype},
985 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100986 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200987 {"getjumplist", 0, 2, FEARG_1, NULL,
988 ret_list_any, f_getjumplist},
989 {"getline", 1, 2, FEARG_1, NULL,
990 ret_f_getline, f_getline},
991 {"getloclist", 1, 2, 0, NULL,
992 ret_list_or_dict_1, f_getloclist},
993 {"getmarklist", 0, 1, FEARG_1, NULL,
994 ret_list_dict_any, f_getmarklist},
995 {"getmatches", 0, 1, 0, NULL,
996 ret_list_dict_any, f_getmatches},
997 {"getmousepos", 0, 0, 0, NULL,
998 ret_dict_number, f_getmousepos},
999 {"getpid", 0, 0, 0, NULL,
1000 ret_number, f_getpid},
1001 {"getpos", 1, 1, FEARG_1, NULL,
1002 ret_list_number, f_getpos},
1003 {"getqflist", 0, 1, 0, NULL,
1004 ret_list_or_dict_0, f_getqflist},
1005 {"getreg", 0, 3, FEARG_1, NULL,
1006 ret_getreg, f_getreg},
1007 {"getreginfo", 0, 1, FEARG_1, NULL,
1008 ret_dict_any, f_getreginfo},
1009 {"getregtype", 0, 1, FEARG_1, NULL,
1010 ret_string, f_getregtype},
1011 {"gettabinfo", 0, 1, FEARG_1, NULL,
1012 ret_list_dict_any, f_gettabinfo},
1013 {"gettabvar", 2, 3, FEARG_1, NULL,
1014 ret_any, f_gettabvar},
1015 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1016 ret_any, f_gettabwinvar},
1017 {"gettagstack", 0, 1, FEARG_1, NULL,
1018 ret_dict_any, f_gettagstack},
1019 {"gettext", 1, 1, FEARG_1, NULL,
1020 ret_string, f_gettext},
1021 {"getwininfo", 0, 1, FEARG_1, NULL,
1022 ret_list_dict_any, f_getwininfo},
1023 {"getwinpos", 0, 1, FEARG_1, NULL,
1024 ret_list_number, f_getwinpos},
1025 {"getwinposx", 0, 0, 0, NULL,
1026 ret_number, f_getwinposx},
1027 {"getwinposy", 0, 0, 0, NULL,
1028 ret_number, f_getwinposy},
1029 {"getwinvar", 2, 3, FEARG_1, NULL,
1030 ret_any, f_getwinvar},
1031 {"glob", 1, 4, FEARG_1, NULL,
1032 ret_any, f_glob},
1033 {"glob2regpat", 1, 1, FEARG_1, NULL,
1034 ret_string, f_glob2regpat},
1035 {"globpath", 2, 5, FEARG_2, NULL,
1036 ret_any, f_globpath},
1037 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001038 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001039 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001040 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001041 {"haslocaldir", 0, 2, FEARG_1, NULL,
1042 ret_number, f_haslocaldir},
1043 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001044 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001045 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1046 ret_number, f_hlID},
1047 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001048 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001049 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001050 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001051 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001052 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001053 {"histget", 1, 2, FEARG_1, NULL,
1054 ret_string, f_histget},
1055 {"histnr", 1, 1, FEARG_1, NULL,
1056 ret_number, f_histnr},
1057 {"hlID", 1, 1, FEARG_1, NULL,
1058 ret_number, f_hlID},
1059 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001060 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001061 {"hostname", 0, 0, 0, NULL,
1062 ret_string, f_hostname},
1063 {"iconv", 3, 3, FEARG_1, NULL,
1064 ret_string, f_iconv},
1065 {"indent", 1, 1, FEARG_1, NULL,
1066 ret_number, f_indent},
1067 {"index", 2, 4, FEARG_1, NULL,
1068 ret_number, f_index},
1069 {"input", 1, 3, FEARG_1, NULL,
1070 ret_string, f_input},
1071 {"inputdialog", 1, 3, FEARG_1, NULL,
1072 ret_string, f_inputdialog},
1073 {"inputlist", 1, 1, FEARG_1, NULL,
1074 ret_number, f_inputlist},
1075 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001078 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001079 {"inputsecret", 1, 2, FEARG_1, NULL,
1080 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001081 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001082 ret_first_arg, f_insert},
1083 {"interrupt", 0, 0, 0, NULL,
1084 ret_void, f_interrupt},
1085 {"invert", 1, 1, FEARG_1, NULL,
1086 ret_number, f_invert},
1087 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"isinf", 1, 1, FEARG_1, NULL,
1090 ret_number, MATH_FUNC(f_isinf)},
1091 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001093 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001094 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001095 {"items", 1, 1, FEARG_1, NULL,
1096 ret_list_any, f_items},
1097 {"job_getchannel", 1, 1, FEARG_1, NULL,
1098 ret_channel, JOB_FUNC(f_job_getchannel)},
1099 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001100 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"job_setoptions", 2, 2, FEARG_1, NULL,
1102 ret_void, JOB_FUNC(f_job_setoptions)},
1103 {"job_start", 1, 2, FEARG_1, NULL,
1104 ret_job, JOB_FUNC(f_job_start)},
1105 {"job_status", 1, 1, FEARG_1, NULL,
1106 ret_string, JOB_FUNC(f_job_status)},
1107 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001108 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 {"join", 1, 2, FEARG_1, NULL,
1110 ret_string, f_join},
1111 {"js_decode", 1, 1, FEARG_1, NULL,
1112 ret_any, f_js_decode},
1113 {"js_encode", 1, 1, FEARG_1, NULL,
1114 ret_string, f_js_encode},
1115 {"json_decode", 1, 1, FEARG_1, NULL,
1116 ret_any, f_json_decode},
1117 {"json_encode", 1, 1, FEARG_1, NULL,
1118 ret_string, f_json_encode},
1119 {"keys", 1, 1, FEARG_1, NULL,
1120 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001121 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1122 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001123 {"len", 1, 1, FEARG_1, NULL,
1124 ret_number, f_len},
1125 {"libcall", 3, 3, FEARG_3, NULL,
1126 ret_string, f_libcall},
1127 {"libcallnr", 3, 3, FEARG_3, NULL,
1128 ret_number, f_libcallnr},
1129 {"line", 1, 2, FEARG_1, NULL,
1130 ret_number, f_line},
1131 {"line2byte", 1, 1, FEARG_1, NULL,
1132 ret_number, f_line2byte},
1133 {"lispindent", 1, 1, FEARG_1, NULL,
1134 ret_number, f_lispindent},
1135 {"list2str", 1, 2, FEARG_1, NULL,
1136 ret_string, f_list2str},
1137 {"listener_add", 1, 2, FEARG_2, NULL,
1138 ret_number, f_listener_add},
1139 {"listener_flush", 0, 1, FEARG_1, NULL,
1140 ret_void, f_listener_flush},
1141 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"localtime", 0, 0, 0, NULL,
1144 ret_number, f_localtime},
1145 {"log", 1, 1, FEARG_1, NULL,
1146 ret_float, FLOAT_FUNC(f_log)},
1147 {"log10", 1, 1, FEARG_1, NULL,
1148 ret_float, FLOAT_FUNC(f_log10)},
1149 {"luaeval", 1, 2, FEARG_1, NULL,
1150 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001151#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001152 f_luaeval
1153#else
1154 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001155#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001156 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001158 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001159 {"maparg", 1, 4, FEARG_1, NULL,
1160 ret_maparg, f_maparg},
1161 {"mapcheck", 1, 3, FEARG_1, NULL,
1162 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001163 {"mapnew", 2, 2, FEARG_1, NULL,
1164 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001165 {"mapset", 3, 3, FEARG_1, NULL,
1166 ret_void, f_mapset},
1167 {"match", 2, 4, FEARG_1, NULL,
1168 ret_any, f_match},
1169 {"matchadd", 2, 5, FEARG_1, NULL,
1170 ret_number, f_matchadd},
1171 {"matchaddpos", 2, 5, FEARG_1, NULL,
1172 ret_number, f_matchaddpos},
1173 {"matcharg", 1, 1, FEARG_1, NULL,
1174 ret_list_string, f_matcharg},
1175 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001176 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001177 {"matchend", 2, 4, FEARG_1, NULL,
1178 ret_number, f_matchend},
1179 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1180 ret_list_string, f_matchfuzzy},
1181 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1182 ret_list_any, f_matchfuzzypos},
1183 {"matchlist", 2, 4, FEARG_1, NULL,
1184 ret_list_string, f_matchlist},
1185 {"matchstr", 2, 4, FEARG_1, NULL,
1186 ret_string, f_matchstr},
1187 {"matchstrpos", 2, 4, FEARG_1, NULL,
1188 ret_list_any, f_matchstrpos},
1189 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001190 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"menu_info", 1, 2, FEARG_1, NULL,
1192 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001193#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001194 f_menu_info
1195#else
1196 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001197#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001198 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001199 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001200 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001201 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001202 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001203 {"mode", 0, 1, FEARG_1, NULL,
1204 ret_string, f_mode},
1205 {"mzeval", 1, 1, FEARG_1, NULL,
1206 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001207#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001208 f_mzeval
1209#else
1210 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001212 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001213 {"nextnonblank", 1, 1, FEARG_1, NULL,
1214 ret_number, f_nextnonblank},
1215 {"nr2char", 1, 2, FEARG_1, NULL,
1216 ret_string, f_nr2char},
1217 {"or", 2, 2, FEARG_1, NULL,
1218 ret_number, f_or},
1219 {"pathshorten", 1, 2, FEARG_1, NULL,
1220 ret_string, f_pathshorten},
1221 {"perleval", 1, 1, FEARG_1, NULL,
1222 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001223#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001224 f_perleval
1225#else
1226 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001228 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001229 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1230 ret_number, PROP_FUNC(f_popup_atcursor)},
1231 {"popup_beval", 2, 2, FEARG_1, NULL,
1232 ret_number, PROP_FUNC(f_popup_beval)},
1233 {"popup_clear", 0, 1, 0, NULL,
1234 ret_void, PROP_FUNC(f_popup_clear)},
1235 {"popup_close", 1, 2, FEARG_1, NULL,
1236 ret_void, PROP_FUNC(f_popup_close)},
1237 {"popup_create", 2, 2, FEARG_1, NULL,
1238 ret_number, PROP_FUNC(f_popup_create)},
1239 {"popup_dialog", 2, 2, FEARG_1, NULL,
1240 ret_number, PROP_FUNC(f_popup_dialog)},
1241 {"popup_filter_menu", 2, 2, 0, NULL,
1242 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1243 {"popup_filter_yesno", 2, 2, 0, NULL,
1244 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1245 {"popup_findinfo", 0, 0, 0, NULL,
1246 ret_number, PROP_FUNC(f_popup_findinfo)},
1247 {"popup_findpreview", 0, 0, 0, NULL,
1248 ret_number, PROP_FUNC(f_popup_findpreview)},
1249 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1250 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1251 {"popup_getpos", 1, 1, FEARG_1, NULL,
1252 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1253 {"popup_hide", 1, 1, FEARG_1, NULL,
1254 ret_void, PROP_FUNC(f_popup_hide)},
1255 {"popup_list", 0, 0, 0, NULL,
1256 ret_list_number, PROP_FUNC(f_popup_list)},
1257 {"popup_locate", 2, 2, 0, NULL,
1258 ret_number, PROP_FUNC(f_popup_locate)},
1259 {"popup_menu", 2, 2, FEARG_1, NULL,
1260 ret_number, PROP_FUNC(f_popup_menu)},
1261 {"popup_move", 2, 2, FEARG_1, NULL,
1262 ret_void, PROP_FUNC(f_popup_move)},
1263 {"popup_notification", 2, 2, FEARG_1, NULL,
1264 ret_number, PROP_FUNC(f_popup_notification)},
1265 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1266 ret_void, PROP_FUNC(f_popup_setoptions)},
1267 {"popup_settext", 2, 2, FEARG_1, NULL,
1268 ret_void, PROP_FUNC(f_popup_settext)},
1269 {"popup_show", 1, 1, FEARG_1, NULL,
1270 ret_void, PROP_FUNC(f_popup_show)},
1271 {"pow", 2, 2, FEARG_1, NULL,
1272 ret_float, FLOAT_FUNC(f_pow)},
1273 {"prevnonblank", 1, 1, FEARG_1, NULL,
1274 ret_number, f_prevnonblank},
1275 {"printf", 1, 19, FEARG_2, NULL,
1276 ret_string, f_printf},
1277 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1278 ret_string, JOB_FUNC(f_prompt_getprompt)},
1279 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1280 ret_void, JOB_FUNC(f_prompt_setcallback)},
1281 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1282 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1283 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1284 ret_void, JOB_FUNC(f_prompt_setprompt)},
1285 {"prop_add", 3, 3, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_prop_add)},
1287 {"prop_clear", 1, 3, FEARG_1, NULL,
1288 ret_void, PROP_FUNC(f_prop_clear)},
1289 {"prop_find", 1, 2, FEARG_1, NULL,
1290 ret_dict_any, PROP_FUNC(f_prop_find)},
1291 {"prop_list", 1, 2, FEARG_1, NULL,
1292 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1293 {"prop_remove", 1, 3, FEARG_1, NULL,
1294 ret_number, PROP_FUNC(f_prop_remove)},
1295 {"prop_type_add", 2, 2, FEARG_1, NULL,
1296 ret_void, PROP_FUNC(f_prop_type_add)},
1297 {"prop_type_change", 2, 2, FEARG_1, NULL,
1298 ret_void, PROP_FUNC(f_prop_type_change)},
1299 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1300 ret_void, PROP_FUNC(f_prop_type_delete)},
1301 {"prop_type_get", 1, 2, FEARG_1, NULL,
1302 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1303 {"prop_type_list", 0, 1, FEARG_1, NULL,
1304 ret_list_string, PROP_FUNC(f_prop_type_list)},
1305 {"pum_getpos", 0, 0, 0, NULL,
1306 ret_dict_number, f_pum_getpos},
1307 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001308 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001309 {"py3eval", 1, 1, FEARG_1, NULL,
1310 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001312 f_py3eval
1313#else
1314 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001316 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001317 {"pyeval", 1, 1, FEARG_1, NULL,
1318 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001320 f_pyeval
1321#else
1322 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001324 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001325 {"pyxeval", 1, 1, FEARG_1, NULL,
1326 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001327#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001328 f_pyxeval
1329#else
1330 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001331#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001332 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001333 {"rand", 0, 1, FEARG_1, NULL,
1334 ret_number, f_rand},
1335 {"range", 1, 3, FEARG_1, NULL,
1336 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001337 {"readblob", 1, 1, FEARG_1, NULL,
1338 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001339 {"readdir", 1, 3, FEARG_1, NULL,
1340 ret_list_string, f_readdir},
1341 {"readdirex", 1, 3, FEARG_1, NULL,
1342 ret_list_dict_any, f_readdirex},
1343 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001344 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001345 {"reduce", 2, 3, FEARG_1, NULL,
1346 ret_any, f_reduce},
1347 {"reg_executing", 0, 0, 0, NULL,
1348 ret_string, f_reg_executing},
1349 {"reg_recording", 0, 0, 0, NULL,
1350 ret_string, f_reg_recording},
1351 {"reltime", 0, 2, FEARG_1, NULL,
1352 ret_list_any, f_reltime},
1353 {"reltimefloat", 1, 1, FEARG_1, NULL,
1354 ret_float, FLOAT_FUNC(f_reltimefloat)},
1355 {"reltimestr", 1, 1, FEARG_1, NULL,
1356 ret_string, f_reltimestr},
1357 {"remote_expr", 2, 4, FEARG_1, NULL,
1358 ret_string, f_remote_expr},
1359 {"remote_foreground", 1, 1, FEARG_1, NULL,
1360 ret_string, f_remote_foreground},
1361 {"remote_peek", 1, 2, FEARG_1, NULL,
1362 ret_number, f_remote_peek},
1363 {"remote_read", 1, 2, FEARG_1, NULL,
1364 ret_string, f_remote_read},
1365 {"remote_send", 2, 3, FEARG_1, NULL,
1366 ret_string, f_remote_send},
1367 {"remote_startserver", 1, 1, FEARG_1, NULL,
1368 ret_void, f_remote_startserver},
1369 {"remove", 2, 3, FEARG_1, NULL,
1370 ret_remove, f_remove},
1371 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001372 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001373 {"repeat", 2, 2, FEARG_1, NULL,
1374 ret_first_arg, f_repeat},
1375 {"resolve", 1, 1, FEARG_1, NULL,
1376 ret_string, f_resolve},
1377 {"reverse", 1, 1, FEARG_1, NULL,
1378 ret_first_arg, f_reverse},
1379 {"round", 1, 1, FEARG_1, NULL,
1380 ret_float, FLOAT_FUNC(f_round)},
1381 {"rubyeval", 1, 1, FEARG_1, NULL,
1382 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001383#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001384 f_rubyeval
1385#else
1386 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001387#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001388 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001389 {"screenattr", 2, 2, FEARG_1, NULL,
1390 ret_number, f_screenattr},
1391 {"screenchar", 2, 2, FEARG_1, NULL,
1392 ret_number, f_screenchar},
1393 {"screenchars", 2, 2, FEARG_1, NULL,
1394 ret_list_number, f_screenchars},
1395 {"screencol", 0, 0, 0, NULL,
1396 ret_number, f_screencol},
1397 {"screenpos", 3, 3, FEARG_1, NULL,
1398 ret_dict_number, f_screenpos},
1399 {"screenrow", 0, 0, 0, NULL,
1400 ret_number, f_screenrow},
1401 {"screenstring", 2, 2, FEARG_1, NULL,
1402 ret_string, f_screenstring},
1403 {"search", 1, 5, FEARG_1, NULL,
1404 ret_number, f_search},
1405 {"searchcount", 0, 1, FEARG_1, NULL,
1406 ret_dict_any, f_searchcount},
1407 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001408 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001409 {"searchpair", 3, 7, 0, NULL,
1410 ret_number, f_searchpair},
1411 {"searchpairpos", 3, 7, 0, NULL,
1412 ret_list_number, f_searchpairpos},
1413 {"searchpos", 1, 5, FEARG_1, NULL,
1414 ret_list_number, f_searchpos},
1415 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001416 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001417 {"serverlist", 0, 0, 0, NULL,
1418 ret_string, f_serverlist},
1419 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001420 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001421 {"setbufvar", 3, 3, FEARG_3, NULL,
1422 ret_void, f_setbufvar},
1423 {"setcellwidths", 1, 1, FEARG_1, NULL,
1424 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001425 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001426 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001427 {"setcharsearch", 1, 1, FEARG_1, NULL,
1428 ret_void, f_setcharsearch},
1429 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001430 ret_number_bool, f_setcmdpos},
1431 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1432 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001433 {"setenv", 2, 2, FEARG_2, NULL,
1434 ret_void, f_setenv},
1435 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001436 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001438 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001440 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001441 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001444 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001445 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001446 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001447 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001448 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001449 {"settabvar", 3, 3, FEARG_3, NULL,
1450 ret_void, f_settabvar},
1451 {"settabwinvar", 4, 4, FEARG_4, NULL,
1452 ret_void, f_settabwinvar},
1453 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001454 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001455 {"setwinvar", 3, 3, FEARG_3, NULL,
1456 ret_void, f_setwinvar},
1457 {"sha256", 1, 1, FEARG_1, NULL,
1458 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001459#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001460 f_sha256
1461#else
1462 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001463#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001464 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"shellescape", 1, 2, FEARG_1, NULL,
1466 ret_string, f_shellescape},
1467 {"shiftwidth", 0, 1, FEARG_1, NULL,
1468 ret_number, f_shiftwidth},
1469 {"sign_define", 1, 2, FEARG_1, NULL,
1470 ret_any, SIGN_FUNC(f_sign_define)},
1471 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1472 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1473 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1474 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1475 {"sign_jump", 3, 3, FEARG_1, NULL,
1476 ret_number, SIGN_FUNC(f_sign_jump)},
1477 {"sign_place", 4, 5, FEARG_1, NULL,
1478 ret_number, SIGN_FUNC(f_sign_place)},
1479 {"sign_placelist", 1, 1, FEARG_1, NULL,
1480 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1481 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001482 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001484 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1486 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1487 {"simplify", 1, 1, FEARG_1, NULL,
1488 ret_string, f_simplify},
1489 {"sin", 1, 1, FEARG_1, NULL,
1490 ret_float, FLOAT_FUNC(f_sin)},
1491 {"sinh", 1, 1, FEARG_1, NULL,
1492 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001493 {"slice", 2, 3, FEARG_1, NULL,
1494 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"sort", 1, 3, FEARG_1, NULL,
1496 ret_first_arg, f_sort},
1497 {"sound_clear", 0, 0, 0, NULL,
1498 ret_void, SOUND_FUNC(f_sound_clear)},
1499 {"sound_playevent", 1, 2, FEARG_1, NULL,
1500 ret_number, SOUND_FUNC(f_sound_playevent)},
1501 {"sound_playfile", 1, 2, FEARG_1, NULL,
1502 ret_number, SOUND_FUNC(f_sound_playfile)},
1503 {"sound_stop", 1, 1, FEARG_1, NULL,
1504 ret_void, SOUND_FUNC(f_sound_stop)},
1505 {"soundfold", 1, 1, FEARG_1, NULL,
1506 ret_string, f_soundfold},
1507 {"spellbadword", 0, 1, FEARG_1, NULL,
1508 ret_list_string, f_spellbadword},
1509 {"spellsuggest", 1, 3, FEARG_1, NULL,
1510 ret_list_string, f_spellsuggest},
1511 {"split", 1, 3, FEARG_1, NULL,
1512 ret_list_string, f_split},
1513 {"sqrt", 1, 1, FEARG_1, NULL,
1514 ret_float, FLOAT_FUNC(f_sqrt)},
1515 {"srand", 0, 1, FEARG_1, NULL,
1516 ret_list_number, f_srand},
1517 {"state", 0, 1, FEARG_1, NULL,
1518 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001519 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001520 ret_float, FLOAT_FUNC(f_str2float)},
1521 {"str2list", 1, 2, FEARG_1, NULL,
1522 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001523 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001524 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001525 {"strcharlen", 1, 1, FEARG_1, NULL,
1526 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001527 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001528 ret_string, f_strcharpart},
1529 {"strchars", 1, 2, FEARG_1, NULL,
1530 ret_number, f_strchars},
1531 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1532 ret_number, f_strdisplaywidth},
1533 {"strftime", 1, 2, FEARG_1, NULL,
1534 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001536 f_strftime
1537#else
1538 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001540 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 {"strgetchar", 2, 2, FEARG_1, NULL,
1542 ret_number, f_strgetchar},
1543 {"stridx", 2, 3, FEARG_1, NULL,
1544 ret_number, f_stridx},
1545 {"string", 1, 1, FEARG_1, NULL,
1546 ret_string, f_string},
1547 {"strlen", 1, 1, FEARG_1, NULL,
1548 ret_number, f_strlen},
1549 {"strpart", 2, 4, FEARG_1, NULL,
1550 ret_string, f_strpart},
1551 {"strptime", 2, 2, FEARG_1, NULL,
1552 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001553#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001554 f_strptime
1555#else
1556 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001557#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001558 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001559 {"strridx", 2, 3, FEARG_1, NULL,
1560 ret_number, f_strridx},
1561 {"strtrans", 1, 1, FEARG_1, NULL,
1562 ret_string, f_strtrans},
1563 {"strwidth", 1, 1, FEARG_1, NULL,
1564 ret_number, f_strwidth},
1565 {"submatch", 1, 2, FEARG_1, NULL,
1566 ret_string, f_submatch},
1567 {"substitute", 4, 4, FEARG_1, NULL,
1568 ret_string, f_substitute},
1569 {"swapinfo", 1, 1, FEARG_1, NULL,
1570 ret_dict_any, f_swapinfo},
1571 {"swapname", 1, 1, FEARG_1, NULL,
1572 ret_string, f_swapname},
1573 {"synID", 3, 3, 0, NULL,
1574 ret_number, f_synID},
1575 {"synIDattr", 2, 3, FEARG_1, NULL,
1576 ret_string, f_synIDattr},
1577 {"synIDtrans", 1, 1, FEARG_1, NULL,
1578 ret_number, f_synIDtrans},
1579 {"synconcealed", 2, 2, 0, NULL,
1580 ret_list_any, f_synconcealed},
1581 {"synstack", 2, 2, 0, NULL,
1582 ret_list_number, f_synstack},
1583 {"system", 1, 2, FEARG_1, NULL,
1584 ret_string, f_system},
1585 {"systemlist", 1, 2, FEARG_1, NULL,
1586 ret_list_string, f_systemlist},
1587 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1588 ret_list_number, f_tabpagebuflist},
1589 {"tabpagenr", 0, 1, 0, NULL,
1590 ret_number, f_tabpagenr},
1591 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1592 ret_number, f_tabpagewinnr},
1593 {"tagfiles", 0, 0, 0, NULL,
1594 ret_list_string, f_tagfiles},
1595 {"taglist", 1, 2, FEARG_1, NULL,
1596 ret_list_dict_any, f_taglist},
1597 {"tan", 1, 1, FEARG_1, NULL,
1598 ret_float, FLOAT_FUNC(f_tan)},
1599 {"tanh", 1, 1, FEARG_1, NULL,
1600 ret_float, FLOAT_FUNC(f_tanh)},
1601 {"tempname", 0, 0, 0, NULL,
1602 ret_string, f_tempname},
1603 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1604 ret_number, TERM_FUNC(f_term_dumpdiff)},
1605 {"term_dumpload", 1, 2, FEARG_1, NULL,
1606 ret_number, TERM_FUNC(f_term_dumpload)},
1607 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1608 ret_void, TERM_FUNC(f_term_dumpwrite)},
1609 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1610 ret_number, TERM_FUNC(f_term_getaltscreen)},
1611 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1612 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001613#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001614 f_term_getansicolors
1615#else
1616 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001617#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001618 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001619 {"term_getattr", 2, 2, FEARG_1, NULL,
1620 ret_number, TERM_FUNC(f_term_getattr)},
1621 {"term_getcursor", 1, 1, FEARG_1, NULL,
1622 ret_list_any, TERM_FUNC(f_term_getcursor)},
1623 {"term_getjob", 1, 1, FEARG_1, NULL,
1624 ret_job, TERM_FUNC(f_term_getjob)},
1625 {"term_getline", 2, 2, FEARG_1, NULL,
1626 ret_string, TERM_FUNC(f_term_getline)},
1627 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1628 ret_number, TERM_FUNC(f_term_getscrolled)},
1629 {"term_getsize", 1, 1, FEARG_1, NULL,
1630 ret_list_number, TERM_FUNC(f_term_getsize)},
1631 {"term_getstatus", 1, 1, FEARG_1, NULL,
1632 ret_string, TERM_FUNC(f_term_getstatus)},
1633 {"term_gettitle", 1, 1, FEARG_1, NULL,
1634 ret_string, TERM_FUNC(f_term_gettitle)},
1635 {"term_gettty", 1, 2, FEARG_1, NULL,
1636 ret_string, TERM_FUNC(f_term_gettty)},
1637 {"term_list", 0, 0, 0, NULL,
1638 ret_list_number, TERM_FUNC(f_term_list)},
1639 {"term_scrape", 2, 2, FEARG_1, NULL,
1640 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1641 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1642 ret_void, TERM_FUNC(f_term_sendkeys)},
1643 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1644 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001645#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001646 f_term_setansicolors
1647#else
1648 NULL
1649#endif
1650 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001651 {"term_setapi", 2, 2, FEARG_1, NULL,
1652 ret_void, TERM_FUNC(f_term_setapi)},
1653 {"term_setkill", 2, 2, FEARG_1, NULL,
1654 ret_void, TERM_FUNC(f_term_setkill)},
1655 {"term_setrestore", 2, 2, FEARG_1, NULL,
1656 ret_void, TERM_FUNC(f_term_setrestore)},
1657 {"term_setsize", 3, 3, FEARG_1, NULL,
1658 ret_void, TERM_FUNC(f_term_setsize)},
1659 {"term_start", 1, 2, FEARG_1, NULL,
1660 ret_number, TERM_FUNC(f_term_start)},
1661 {"term_wait", 1, 2, FEARG_1, NULL,
1662 ret_void, TERM_FUNC(f_term_wait)},
1663 {"terminalprops", 0, 0, 0, NULL,
1664 ret_dict_string, f_terminalprops},
1665 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1666 ret_void, f_test_alloc_fail},
1667 {"test_autochdir", 0, 0, 0, NULL,
1668 ret_void, f_test_autochdir},
1669 {"test_feedinput", 1, 1, FEARG_1, NULL,
1670 ret_void, f_test_feedinput},
1671 {"test_garbagecollect_now", 0, 0, 0, NULL,
1672 ret_void, f_test_garbagecollect_now},
1673 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1674 ret_void, f_test_garbagecollect_soon},
1675 {"test_getvalue", 1, 1, FEARG_1, NULL,
1676 ret_number, f_test_getvalue},
1677 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1678 ret_void, f_test_ignore_error},
1679 {"test_null_blob", 0, 0, 0, NULL,
1680 ret_blob, f_test_null_blob},
1681 {"test_null_channel", 0, 0, 0, NULL,
1682 ret_channel, JOB_FUNC(f_test_null_channel)},
1683 {"test_null_dict", 0, 0, 0, NULL,
1684 ret_dict_any, f_test_null_dict},
1685 {"test_null_function", 0, 0, 0, NULL,
1686 ret_func_any, f_test_null_function},
1687 {"test_null_job", 0, 0, 0, NULL,
1688 ret_job, JOB_FUNC(f_test_null_job)},
1689 {"test_null_list", 0, 0, 0, NULL,
1690 ret_list_any, f_test_null_list},
1691 {"test_null_partial", 0, 0, 0, NULL,
1692 ret_func_any, f_test_null_partial},
1693 {"test_null_string", 0, 0, 0, NULL,
1694 ret_string, f_test_null_string},
1695 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1696 ret_void, f_test_option_not_set},
1697 {"test_override", 2, 2, FEARG_2, NULL,
1698 ret_void, f_test_override},
1699 {"test_refcount", 1, 1, FEARG_1, NULL,
1700 ret_number, f_test_refcount},
1701 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1702 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001703#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001704 f_test_scrollbar
1705#else
1706 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001707#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001708 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001709 {"test_setmouse", 2, 2, 0, NULL,
1710 ret_void, f_test_setmouse},
1711 {"test_settime", 1, 1, FEARG_1, NULL,
1712 ret_void, f_test_settime},
1713 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1714 ret_void, f_test_srand_seed},
1715 {"test_unknown", 0, 0, 0, NULL,
1716 ret_any, f_test_unknown},
1717 {"test_void", 0, 0, 0, NULL,
1718 ret_void, f_test_void},
1719 {"timer_info", 0, 1, FEARG_1, NULL,
1720 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1721 {"timer_pause", 2, 2, FEARG_1, NULL,
1722 ret_void, TIMER_FUNC(f_timer_pause)},
1723 {"timer_start", 2, 3, FEARG_1, NULL,
1724 ret_number, TIMER_FUNC(f_timer_start)},
1725 {"timer_stop", 1, 1, FEARG_1, NULL,
1726 ret_void, TIMER_FUNC(f_timer_stop)},
1727 {"timer_stopall", 0, 0, 0, NULL,
1728 ret_void, TIMER_FUNC(f_timer_stopall)},
1729 {"tolower", 1, 1, FEARG_1, NULL,
1730 ret_string, f_tolower},
1731 {"toupper", 1, 1, FEARG_1, NULL,
1732 ret_string, f_toupper},
1733 {"tr", 3, 3, FEARG_1, NULL,
1734 ret_string, f_tr},
1735 {"trim", 1, 3, FEARG_1, NULL,
1736 ret_string, f_trim},
1737 {"trunc", 1, 1, FEARG_1, NULL,
1738 ret_float, FLOAT_FUNC(f_trunc)},
1739 {"type", 1, 1, FEARG_1, NULL,
1740 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001741 {"typename", 1, 1, FEARG_1, NULL,
1742 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001743 {"undofile", 1, 1, FEARG_1, NULL,
1744 ret_string, f_undofile},
1745 {"undotree", 0, 0, 0, NULL,
1746 ret_dict_any, f_undotree},
1747 {"uniq", 1, 3, FEARG_1, NULL,
1748 ret_list_any, f_uniq},
1749 {"values", 1, 1, FEARG_1, NULL,
1750 ret_list_any, f_values},
1751 {"virtcol", 1, 1, FEARG_1, NULL,
1752 ret_number, f_virtcol},
1753 {"visualmode", 0, 1, 0, NULL,
1754 ret_string, f_visualmode},
1755 {"wildmenumode", 0, 0, 0, NULL,
1756 ret_number, f_wildmenumode},
1757 {"win_execute", 2, 3, FEARG_2, NULL,
1758 ret_string, f_win_execute},
1759 {"win_findbuf", 1, 1, FEARG_1, NULL,
1760 ret_list_number, f_win_findbuf},
1761 {"win_getid", 0, 2, FEARG_1, NULL,
1762 ret_number, f_win_getid},
1763 {"win_gettype", 0, 1, FEARG_1, NULL,
1764 ret_string, f_win_gettype},
1765 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001766 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001767 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1768 ret_list_number, f_win_id2tabwin},
1769 {"win_id2win", 1, 1, FEARG_1, NULL,
1770 ret_number, f_win_id2win},
1771 {"win_screenpos", 1, 1, FEARG_1, NULL,
1772 ret_list_number, f_win_screenpos},
1773 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001774 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001775 {"winbufnr", 1, 1, FEARG_1, NULL,
1776 ret_number, f_winbufnr},
1777 {"wincol", 0, 0, 0, NULL,
1778 ret_number, f_wincol},
1779 {"windowsversion", 0, 0, 0, NULL,
1780 ret_string, f_windowsversion},
1781 {"winheight", 1, 1, FEARG_1, NULL,
1782 ret_number, f_winheight},
1783 {"winlayout", 0, 1, FEARG_1, NULL,
1784 ret_list_any, f_winlayout},
1785 {"winline", 0, 0, 0, NULL,
1786 ret_number, f_winline},
1787 {"winnr", 0, 1, FEARG_1, NULL,
1788 ret_number, f_winnr},
1789 {"winrestcmd", 0, 0, 0, NULL,
1790 ret_string, f_winrestcmd},
1791 {"winrestview", 1, 1, FEARG_1, NULL,
1792 ret_void, f_winrestview},
1793 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001794 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001795 {"winwidth", 1, 1, FEARG_1, NULL,
1796 ret_number, f_winwidth},
1797 {"wordcount", 0, 0, 0, NULL,
1798 ret_dict_number, f_wordcount},
1799 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001800 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001801 {"xor", 2, 2, FEARG_1, NULL,
1802 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001803};
1804
K.Takataeecf2b32021-06-02 14:56:39 +02001805#if defined(EBCDIC) || defined(PROTO)
1806/*
1807 * Compare funcentry_T by function name.
1808 */
1809 static int
1810compare_func_name(const void *s1, const void *s2)
1811{
1812 funcentry_T *p1 = (funcentry_T *)s1;
1813 funcentry_T *p2 = (funcentry_T *)s2;
1814
1815 return STRCMP(p1->f_name, p2->f_name);
1816}
1817
1818/*
1819 * Sort the function table by function name.
1820 * The sorting of the table above is ASCII dependent.
1821 * On machines using EBCDIC we have to sort it.
1822 */
1823 void
1824sortFunctions(void)
1825{
1826 size_t funcCnt = ARRAY_LENGTH(global_functions);
1827
1828 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1829}
1830#endif
1831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832/*
1833 * Function given to ExpandGeneric() to obtain the list of internal
1834 * or user defined function names.
1835 */
1836 char_u *
1837get_function_name(expand_T *xp, int idx)
1838{
1839 static int intidx = -1;
1840 char_u *name;
1841
1842 if (idx == 0)
1843 intidx = -1;
1844 if (intidx < 0)
1845 {
1846 name = get_user_func_name(xp, idx);
1847 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001848 {
1849 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1850 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 }
K.Takataeeec2542021-06-02 13:28:16 +02001854 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001858 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001859 STRCAT(IObuff, ")");
1860 return IObuff;
1861 }
1862
1863 return NULL;
1864}
1865
1866/*
1867 * Function given to ExpandGeneric() to obtain the list of internal or
1868 * user defined variable or function names.
1869 */
1870 char_u *
1871get_expr_name(expand_T *xp, int idx)
1872{
1873 static int intidx = -1;
1874 char_u *name;
1875
1876 if (idx == 0)
1877 intidx = -1;
1878 if (intidx < 0)
1879 {
1880 name = get_function_name(xp, idx);
1881 if (name != NULL)
1882 return name;
1883 }
1884 return get_user_var_name(xp, ++intidx);
1885}
1886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001888 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001889 * Return index, or -1 if not found or "implemented" is TRUE and the function
1890 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001892 static int
1893find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894{
1895 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001896 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897 int cmp;
1898 int x;
1899
K.Takataeeec2542021-06-02 13:28:16 +02001900 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001901
1902 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903 while (first <= last)
1904 {
1905 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001906 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 if (cmp < 0)
1908 last = x - 1;
1909 else if (cmp > 0)
1910 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001911 else if (implemented && global_functions[x].f_func == NULL)
1912 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 else
1914 return x;
1915 }
1916 return -1;
1917}
1918
Bram Moolenaar15c47602020-03-26 22:16:48 +01001919/*
1920 * Find internal function "name" in table "global_functions".
1921 * Return index, or -1 if not found or the function is not implemented.
1922 */
1923 int
1924find_internal_func(char_u *name)
1925{
1926 return find_internal_func_opt(name, TRUE);
1927}
1928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001930has_internal_func(char_u *name)
1931{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001932 return find_internal_func_opt(name, TRUE) >= 0;
1933}
1934
1935 static int
1936has_internal_func_name(char_u *name)
1937{
1938 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001939}
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 char *
1942internal_func_name(int idx)
1943{
1944 return global_functions[idx].f_name;
1945}
1946
Bram Moolenaar94738d82020-10-21 14:25:07 +02001947/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001948 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001949 * Uses the list of types on the type stack: "types".
1950 * Return FAIL and gives an error message when a type is wrong.
1951 */
1952 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001953internal_func_check_arg_types(
1954 type_T **types,
1955 int idx,
1956 int argcount,
1957 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001958{
1959 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1960 int i;
1961
1962 if (argchecks != NULL)
1963 {
1964 argcontext_T context;
1965
1966 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001967 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001968 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001969 for (i = 0; i < argcount; ++i)
1970 if (argchecks[i] != NULL)
1971 {
1972 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001973 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001974 return FAIL;
1975 }
1976 }
1977 return OK;
1978}
1979
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001980/*
1981 * Call the "f_retfunc" function to obtain the return type of function "idx".
1982 * "argtypes" is the list of argument types or NULL when there are no
1983 * arguments.
1984 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001988 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989}
1990
1991/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001992 * Return TRUE if "idx" is for the map() function.
1993 */
1994 int
1995internal_func_is_map(int idx)
1996{
1997 return global_functions[idx].f_func == f_map;
1998}
1999
2000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002002 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2003 * first argument, 2 if method base is second argument, etc. 9 if method base
2004 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 */
2006 int
2007check_internal_func(int idx, int argcount)
2008{
2009 int res;
2010 char *name;
2011
2012 if (argcount < global_functions[idx].f_min_argc)
2013 res = FCERR_TOOFEW;
2014 else if (argcount > global_functions[idx].f_max_argc)
2015 res = FCERR_TOOMANY;
2016 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002017 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018
2019 name = internal_func_name(idx);
2020 if (res == FCERR_TOOMANY)
2021 semsg(_(e_toomanyarg), name);
2022 else
2023 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002024 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025}
2026
Bram Moolenaarac92e252019-08-03 21:58:38 +02002027 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002028call_internal_func(
2029 char_u *name,
2030 int argcount,
2031 typval_T *argvars,
2032 typval_T *rettv)
2033{
2034 int i;
2035
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002036 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002041 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002042 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002045 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002046}
2047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 void
2049call_internal_func_by_idx(
2050 int idx,
2051 typval_T *argvars,
2052 typval_T *rettv)
2053{
2054 global_functions[idx].f_func(argvars, rettv);
2055}
2056
Bram Moolenaarac92e252019-08-03 21:58:38 +02002057/*
2058 * Invoke a method for base->method().
2059 */
2060 int
2061call_internal_method(
2062 char_u *name,
2063 int argcount,
2064 typval_T *argvars,
2065 typval_T *rettv,
2066 typval_T *basetv)
2067{
2068 int i;
2069 int fi;
2070 typval_T argv[MAX_FUNC_ARGS + 1];
2071
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002072 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002075 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002079 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002080 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002081
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002082 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002083 {
2084 // base value goes last
2085 for (i = 0; i < argcount; ++i)
2086 argv[i] = argvars[i];
2087 argv[argcount] = *basetv;
2088 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002089 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002090 {
2091 // base value goes second
2092 argv[0] = argvars[0];
2093 argv[1] = *basetv;
2094 for (i = 1; i < argcount; ++i)
2095 argv[i + 1] = argvars[i];
2096 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002097 else if (global_functions[fi].f_argtype == FEARG_3)
2098 {
2099 // base value goes third
2100 argv[0] = argvars[0];
2101 argv[1] = argvars[1];
2102 argv[2] = *basetv;
2103 for (i = 2; i < argcount; ++i)
2104 argv[i + 1] = argvars[i];
2105 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002106 else if (global_functions[fi].f_argtype == FEARG_4)
2107 {
2108 // base value goes fourth
2109 argv[0] = argvars[0];
2110 argv[1] = argvars[1];
2111 argv[2] = argvars[2];
2112 argv[3] = *basetv;
2113 for (i = 3; i < argcount; ++i)
2114 argv[i + 1] = argvars[i];
2115 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002116 else
2117 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002118 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002119 argv[0] = *basetv;
2120 for (i = 0; i < argcount; ++i)
2121 argv[i + 1] = argvars[i];
2122 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002123 argv[argcount + 1].v_type = VAR_UNKNOWN;
2124
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002125 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002126 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002127}
2128
2129/*
2130 * Return TRUE for a non-zero Number and a non-empty String.
2131 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002132 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002133non_zero_arg(typval_T *argvars)
2134{
2135 return ((argvars[0].v_type == VAR_NUMBER
2136 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002137 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 && argvars[0].vval.v_number == VVAL_TRUE)
2139 || (argvars[0].v_type == VAR_STRING
2140 && argvars[0].vval.v_string != NULL
2141 && *argvars[0].vval.v_string != NUL));
2142}
2143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 * "and(expr, expr)" function
2146 */
2147 static void
2148f_and(typval_T *argvars, typval_T *rettv)
2149{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002150 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2151 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002152}
2153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002155 * "balloon_show()" function
2156 */
2157#ifdef FEAT_BEVAL
2158 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002159f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2160{
2161 rettv->v_type = VAR_STRING;
2162 if (balloonEval != NULL)
2163 {
2164 if (balloonEval->msg == NULL)
2165 rettv->vval.v_string = NULL;
2166 else
2167 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2168 }
2169}
2170
2171 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002172f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2173{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002174 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002175 {
2176 if (argvars[0].v_type == VAR_LIST
2177# ifdef FEAT_GUI
2178 && !gui.in_use
2179# endif
2180 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002181 {
2182 list_T *l = argvars[0].vval.v_list;
2183
2184 // empty list removes the balloon
2185 post_balloon(balloonEval, NULL,
2186 l == NULL || l->lv_len == 0 ? NULL : l);
2187 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002188 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002189 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002190 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002191
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002192 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2193 return;
2194
2195 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002196 if (mesg != NULL)
2197 // empty string removes the balloon
2198 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2199 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002200 }
2201}
2202
Bram Moolenaar669a8282017-11-19 20:13:05 +01002203# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002204 static void
2205f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2206{
2207 if (rettv_list_alloc(rettv) == OK)
2208 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002209 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002210
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002211 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2212 return;
2213 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002214 if (msg != NULL)
2215 {
2216 pumitem_T *array;
2217 int size = split_message(msg, &array);
2218 int i;
2219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002220 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002221 for (i = 1; i < size - 1; ++i)
2222 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002223 while (size > 0)
2224 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002225 vim_free(array);
2226 }
2227 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002228}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002229# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002230#endif
2231
2232/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002233 * Get the buffer from "arg" and give an error and return NULL if it is not
2234 * valid.
2235 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002236 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002237get_buf_arg(typval_T *arg)
2238{
2239 buf_T *buf;
2240
2241 ++emsg_off;
2242 buf = tv_get_buf(arg, FALSE);
2243 --emsg_off;
2244 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002245 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002246 return buf;
2247}
2248
2249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250 * "byte2line(byte)" function
2251 */
2252 static void
2253f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2254{
2255#ifndef FEAT_BYTEOFF
2256 rettv->vval.v_number = -1;
2257#else
2258 long boff = 0;
2259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 if (boff < 0)
2262 rettv->vval.v_number = -1;
2263 else
2264 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2265 (linenr_T)0, &boff);
2266#endif
2267}
2268
2269 static void
2270byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2271{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 char_u *str;
2274 varnumber_T idx;
2275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002276 str = tv_get_string_chk(&argvars[0]);
2277 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278 rettv->vval.v_number = -1;
2279 if (str == NULL || idx < 0)
2280 return;
2281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 t = str;
2283 for ( ; idx > 0; idx--)
2284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002285 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 return;
2287 if (enc_utf8 && comp)
2288 t += utf_ptr2len(t);
2289 else
2290 t += (*mb_ptr2len)(t);
2291 }
2292 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002293}
2294
2295/*
2296 * "byteidx()" function
2297 */
2298 static void
2299f_byteidx(typval_T *argvars, typval_T *rettv)
2300{
2301 byteidx(argvars, rettv, FALSE);
2302}
2303
2304/*
2305 * "byteidxcomp()" function
2306 */
2307 static void
2308f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2309{
2310 byteidx(argvars, rettv, TRUE);
2311}
2312
2313/*
2314 * "call(func, arglist [, dict])" function
2315 */
2316 static void
2317f_call(typval_T *argvars, typval_T *rettv)
2318{
2319 char_u *func;
2320 partial_T *partial = NULL;
2321 dict_T *selfdict = NULL;
2322
2323 if (argvars[1].v_type != VAR_LIST)
2324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 return;
2327 }
2328 if (argvars[1].vval.v_list == NULL)
2329 return;
2330
2331 if (argvars[0].v_type == VAR_FUNC)
2332 func = argvars[0].vval.v_string;
2333 else if (argvars[0].v_type == VAR_PARTIAL)
2334 {
2335 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002336 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 }
2338 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002339 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002340 if (func == NULL || *func == NUL)
2341 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342
2343 if (argvars[2].v_type != VAR_UNKNOWN)
2344 {
2345 if (argvars[2].v_type != VAR_DICT)
2346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002348 return;
2349 }
2350 selfdict = argvars[2].vval.v_dict;
2351 }
2352
2353 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2354}
2355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356/*
2357 * "changenr()" function
2358 */
2359 static void
2360f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2361{
2362 rettv->vval.v_number = curbuf->b_u_seq_cur;
2363}
2364
2365/*
2366 * "char2nr(string)" function
2367 */
2368 static void
2369f_char2nr(typval_T *argvars, typval_T *rettv)
2370{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002371 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2372 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 if (has_mbyte)
2374 {
2375 int utf8 = 0;
2376
2377 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002378 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379
2380 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002381 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002383 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384 }
2385 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002386 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387}
2388
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002389/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002390 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2391 * returns the character index of the column. Otherwise, returns the byte index
2392 * of the column.
2393 */
2394 static void
2395get_col(typval_T *argvars, typval_T *rettv, int charcol)
2396{
2397 colnr_T col = 0;
2398 pos_T *fp;
2399 int fnum = curbuf->b_fnum;
2400
2401 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2402 if (fp != NULL && fnum == curbuf->b_fnum)
2403 {
2404 if (fp->col == MAXCOL)
2405 {
2406 // '> can be MAXCOL, get the length of the line then
2407 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2408 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2409 else
2410 col = MAXCOL;
2411 }
2412 else
2413 {
2414 col = fp->col + 1;
2415 // col(".") when the cursor is on the NUL at the end of the line
2416 // because of "coladd" can be seen as an extra column.
2417 if (virtual_active() && fp == &curwin->w_cursor)
2418 {
2419 char_u *p = ml_get_cursor();
2420
2421 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2422 curwin->w_virtcol - curwin->w_cursor.coladd))
2423 {
2424 int l;
2425
2426 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2427 col += l;
2428 }
2429 }
2430 }
2431 }
2432 rettv->vval.v_number = col;
2433}
2434
2435/*
2436 * "charcol()" function
2437 */
2438 static void
2439f_charcol(typval_T *argvars, typval_T *rettv)
2440{
2441 get_col(argvars, rettv, TRUE);
2442}
2443
2444/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002445 * "charidx()" function
2446 */
2447 static void
2448f_charidx(typval_T *argvars, typval_T *rettv)
2449{
2450 char_u *str;
2451 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002452 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002453 char_u *p;
2454 int len;
2455 int (*ptr2len)(char_u *);
2456
2457 rettv->vval.v_number = -1;
2458
2459 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2460 || (argvars[2].v_type != VAR_UNKNOWN
2461 && argvars[2].v_type != VAR_NUMBER))
2462 {
2463 emsg(_(e_invarg));
2464 return;
2465 }
2466
2467 str = tv_get_string_chk(&argvars[0]);
2468 idx = tv_get_number_chk(&argvars[1], NULL);
2469 if (str == NULL || idx < 0)
2470 return;
2471
2472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002473 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002474 if (countcc < 0 || countcc > 1)
2475 {
2476 semsg(_(e_using_number_as_bool_nr), countcc);
2477 return;
2478 }
2479
2480 if (enc_utf8 && countcc)
2481 ptr2len = utf_ptr2len;
2482 else
2483 ptr2len = mb_ptr2len;
2484
2485 for (p = str, len = 0; p <= str + idx; len++)
2486 {
2487 if (*p == NUL)
2488 return;
2489 p += ptr2len(p);
2490 }
2491
2492 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2493}
2494
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002495 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002496get_optional_window(typval_T *argvars, int idx)
2497{
2498 win_T *win = curwin;
2499
2500 if (argvars[idx].v_type != VAR_UNKNOWN)
2501 {
2502 win = find_win_by_nr_or_id(&argvars[idx]);
2503 if (win == NULL)
2504 {
2505 emsg(_(e_invalwindow));
2506 return NULL;
2507 }
2508 }
2509 return win;
2510}
2511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 * "col(string)" function
2514 */
2515 static void
2516f_col(typval_T *argvars, typval_T *rettv)
2517{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002518 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519}
2520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521/*
2522 * "confirm(message, buttons[, default [, type]])" function
2523 */
2524 static void
2525f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2526{
2527#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2528 char_u *message;
2529 char_u *buttons = NULL;
2530 char_u buf[NUMBUFLEN];
2531 char_u buf2[NUMBUFLEN];
2532 int def = 1;
2533 int type = VIM_GENERIC;
2534 char_u *typestr;
2535 int error = FALSE;
2536
Bram Moolenaarc5809432021-03-27 21:23:30 +01002537 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2538 return;
2539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (message == NULL)
2542 error = TRUE;
2543 if (argvars[1].v_type != VAR_UNKNOWN)
2544 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002545 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2546 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548 if (buttons == NULL)
2549 error = TRUE;
2550 if (argvars[2].v_type != VAR_UNKNOWN)
2551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002552 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002553 if (argvars[3].v_type != VAR_UNKNOWN)
2554 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002555 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2556 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002557 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002558 if (typestr == NULL)
2559 error = TRUE;
2560 else
2561 {
2562 switch (TOUPPER_ASC(*typestr))
2563 {
2564 case 'E': type = VIM_ERROR; break;
2565 case 'Q': type = VIM_QUESTION; break;
2566 case 'I': type = VIM_INFO; break;
2567 case 'W': type = VIM_WARNING; break;
2568 case 'G': type = VIM_GENERIC; break;
2569 }
2570 }
2571 }
2572 }
2573 }
2574
2575 if (buttons == NULL || *buttons == NUL)
2576 buttons = (char_u *)_("&Ok");
2577
2578 if (!error)
2579 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2580 def, NULL, FALSE);
2581#endif
2582}
2583
2584/*
2585 * "copy()" function
2586 */
2587 static void
2588f_copy(typval_T *argvars, typval_T *rettv)
2589{
2590 item_copy(&argvars[0], rettv, FALSE, 0);
2591}
2592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002594 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002595 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002596 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 */
2598 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002599set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002600{
2601 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 int set_curswant = TRUE;
2604
2605 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002606 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 {
2608 pos_T pos;
2609 colnr_T curswant = -1;
2610
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002611 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 return;
2615 }
2616 line = pos.lnum;
2617 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (curswant >= 0)
2620 {
2621 curwin->w_curswant = curswant - 1;
2622 set_curswant = FALSE;
2623 }
2624 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002625 else if ((argvars[0].v_type == VAR_NUMBER ||
2626 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002627 && (argvars[1].v_type == VAR_NUMBER ||
2628 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002630 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002631 if (line < 0)
2632 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002633 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002634 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002635 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002637 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002639 else
2640 {
2641 emsg(_(e_invarg));
2642 return;
2643 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002644 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002645 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646 if (line > 0)
2647 curwin->w_cursor.lnum = line;
2648 if (col > 0)
2649 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002654 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 if (has_mbyte)
2656 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657
2658 curwin->w_set_curswant = set_curswant;
2659 rettv->vval.v_number = 0;
2660}
2661
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002662/*
2663 * "cursor(lnum, col)" function, or
2664 * "cursor(list)"
2665 *
2666 * Moves the cursor to the specified line and column.
2667 * Returns 0 when the position could be set, -1 otherwise.
2668 */
2669 static void
2670f_cursor(typval_T *argvars, typval_T *rettv)
2671{
2672 set_cursorpos(argvars, rettv, FALSE);
2673}
2674
Bram Moolenaar4f974752019-02-17 17:44:42 +01002675#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002676/*
2677 * "debugbreak()" function
2678 */
2679 static void
2680f_debugbreak(typval_T *argvars, typval_T *rettv)
2681{
2682 int pid;
2683
2684 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002685 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002686 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002688 else
2689 {
2690 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2691
2692 if (hProcess != NULL)
2693 {
2694 DebugBreakProcess(hProcess);
2695 CloseHandle(hProcess);
2696 rettv->vval.v_number = OK;
2697 }
2698 }
2699}
2700#endif
2701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702/*
2703 * "deepcopy()" function
2704 */
2705 static void
2706f_deepcopy(typval_T *argvars, typval_T *rettv)
2707{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002708 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 int copyID;
2710
2711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002712 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002714 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 else
2716 {
2717 copyID = get_copyID();
2718 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2719 }
2720}
2721
2722/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 * "did_filetype()" function
2724 */
2725 static void
2726f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2727{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729}
2730
2731/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002732 * "echoraw({expr})" function
2733 */
2734 static void
2735f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2736{
2737 char_u *str = tv_get_string_chk(&argvars[0]);
2738
2739 if (str != NULL && *str != NUL)
2740 {
2741 out_str(str);
2742 out_flush();
2743 }
2744}
2745
2746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 * "empty({expr})" function
2748 */
2749 static void
2750f_empty(typval_T *argvars, typval_T *rettv)
2751{
2752 int n = FALSE;
2753
2754 switch (argvars[0].v_type)
2755 {
2756 case VAR_STRING:
2757 case VAR_FUNC:
2758 n = argvars[0].vval.v_string == NULL
2759 || *argvars[0].vval.v_string == NUL;
2760 break;
2761 case VAR_PARTIAL:
2762 n = FALSE;
2763 break;
2764 case VAR_NUMBER:
2765 n = argvars[0].vval.v_number == 0;
2766 break;
2767 case VAR_FLOAT:
2768#ifdef FEAT_FLOAT
2769 n = argvars[0].vval.v_float == 0.0;
2770 break;
2771#endif
2772 case VAR_LIST:
2773 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002774 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 break;
2776 case VAR_DICT:
2777 n = argvars[0].vval.v_dict == NULL
2778 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2779 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002780 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 case VAR_SPECIAL:
2782 n = argvars[0].vval.v_number != VVAL_TRUE;
2783 break;
2784
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002785 case VAR_BLOB:
2786 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002787 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2788 break;
2789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 case VAR_JOB:
2791#ifdef FEAT_JOB_CHANNEL
2792 n = argvars[0].vval.v_job == NULL
2793 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2794 break;
2795#endif
2796 case VAR_CHANNEL:
2797#ifdef FEAT_JOB_CHANNEL
2798 n = argvars[0].vval.v_channel == NULL
2799 || !channel_is_open(argvars[0].vval.v_channel);
2800 break;
2801#endif
2802 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002803 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002805 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002806 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 n = TRUE;
2808 break;
2809 }
2810
2811 rettv->vval.v_number = n;
2812}
2813
2814/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002815 * "environ()" function
2816 */
2817 static void
2818f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2819{
2820#if !defined(AMIGA)
2821 int i = 0;
2822 char_u *entry, *value;
2823# ifdef MSWIN
2824 extern wchar_t **_wenviron;
2825# else
2826 extern char **environ;
2827# endif
2828
2829 if (rettv_dict_alloc(rettv) != OK)
2830 return;
2831
2832# ifdef MSWIN
2833 if (*_wenviron == NULL)
2834 return;
2835# else
2836 if (*environ == NULL)
2837 return;
2838# endif
2839
2840 for (i = 0; ; ++i)
2841 {
2842# ifdef MSWIN
2843 short_u *p;
2844
2845 if ((p = (short_u *)_wenviron[i]) == NULL)
2846 return;
2847 entry = utf16_to_enc(p, NULL);
2848# else
2849 if ((entry = (char_u *)environ[i]) == NULL)
2850 return;
2851 entry = vim_strsave(entry);
2852# endif
2853 if (entry == NULL) // out of memory
2854 return;
2855 if ((value = vim_strchr(entry, '=')) == NULL)
2856 {
2857 vim_free(entry);
2858 continue;
2859 }
2860 *value++ = NUL;
2861 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2862 vim_free(entry);
2863 }
2864#endif
2865}
2866
2867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 * "escape({string}, {chars})" function
2869 */
2870 static void
2871f_escape(typval_T *argvars, typval_T *rettv)
2872{
2873 char_u buf[NUMBUFLEN];
2874
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002875 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2876 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 rettv->v_type = VAR_STRING;
2878}
2879
2880/*
2881 * "eval()" function
2882 */
2883 static void
2884f_eval(typval_T *argvars, typval_T *rettv)
2885{
2886 char_u *s, *p;
2887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002888 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 if (s != NULL)
2890 s = skipwhite(s);
2891
2892 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 {
2895 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002897 need_clr_eos = FALSE;
2898 rettv->v_type = VAR_NUMBER;
2899 rettv->vval.v_number = 0;
2900 }
2901 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002902 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903}
2904
2905/*
2906 * "eventhandler()" function
2907 */
2908 static void
2909f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2910{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002911 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912}
2913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914static garray_T redir_execute_ga;
2915
2916/*
2917 * Append "value[value_len]" to the execute() output.
2918 */
2919 void
2920execute_redir_str(char_u *value, int value_len)
2921{
2922 int len;
2923
2924 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002927 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002928 if (ga_grow(&redir_execute_ga, len) == OK)
2929 {
2930 mch_memmove((char *)redir_execute_ga.ga_data
2931 + redir_execute_ga.ga_len, value, len);
2932 redir_execute_ga.ga_len += len;
2933 }
2934}
2935
2936/*
2937 * Get next line from a list.
2938 * Called by do_cmdline() to get the next line.
2939 * Returns allocated string, or NULL for end of function.
2940 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 static char_u *
2942get_list_line(
2943 int c UNUSED,
2944 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002945 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002946 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947{
2948 listitem_T **p = (listitem_T **)cookie;
2949 listitem_T *item = *p;
2950 char_u buf[NUMBUFLEN];
2951 char_u *s;
2952
2953 if (item == NULL)
2954 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002955 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 *p = item->li_next;
2957 return s == NULL ? NULL : vim_strsave(s);
2958}
2959
2960/*
2961 * "execute()" function
2962 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002963 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002964execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965{
2966 char_u *cmd = NULL;
2967 list_T *list = NULL;
2968 int save_msg_silent = msg_silent;
2969 int save_emsg_silent = emsg_silent;
2970 int save_emsg_noredir = emsg_noredir;
2971 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002972 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002974 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002975 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002976
2977 rettv->vval.v_string = NULL;
2978 rettv->v_type = VAR_STRING;
2979
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002980 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002982 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002983 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002984 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 return;
2986 ++list->lv_refcount;
2987 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002988 else if (argvars[arg_off].v_type == VAR_JOB
2989 || argvars[arg_off].v_type == VAR_CHANNEL)
2990 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02002991 semsg(_(e_using_invalid_value_as_string_str),
2992 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002993 return;
2994 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 else
2996 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002997 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 if (cmd == NULL)
2999 return;
3000 }
3001
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003002 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 {
3004 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003005 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3006 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007
3008 if (s == NULL)
3009 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003010 if (*s == NUL)
3011 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 if (STRNCMP(s, "silent", 6) == 0)
3013 ++msg_silent;
3014 if (STRCMP(s, "silent!") == 0)
3015 {
3016 emsg_silent = TRUE;
3017 emsg_noredir = TRUE;
3018 }
3019 }
3020 else
3021 ++msg_silent;
3022
3023 if (redir_execute)
3024 save_ga = redir_execute_ga;
3025 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3026 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003027 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003028 if (!echo_output)
3029 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030
3031 if (cmd != NULL)
3032 do_cmdline_cmd(cmd);
3033 else
3034 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003035 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003037 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003038 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 do_cmdline(NULL, get_list_line, (void *)&item,
3040 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3041 --list->lv_refcount;
3042 }
3043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003044 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003045 if (ga_grow(&redir_execute_ga, 1) == OK)
3046 {
3047 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3048 rettv->vval.v_string = redir_execute_ga.ga_data;
3049 }
3050 else
3051 {
3052 ga_clear(&redir_execute_ga);
3053 rettv->vval.v_string = NULL;
3054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 msg_silent = save_msg_silent;
3056 emsg_silent = save_emsg_silent;
3057 emsg_noredir = save_emsg_noredir;
3058
3059 redir_execute = save_redir_execute;
3060 if (redir_execute)
3061 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003062 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003064 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003065 if (echo_output)
3066 // When not working silently: put it in column zero. A following
3067 // "echon" will overwrite the message, unavoidably.
3068 msg_col = 0;
3069 else
3070 // When working silently: Put it back where it was, since nothing
3071 // should have been written.
3072 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073}
3074
3075/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003076 * "execute()" function
3077 */
3078 static void
3079f_execute(typval_T *argvars, typval_T *rettv)
3080{
3081 execute_common(argvars, rettv, 0);
3082}
3083
3084/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 * "exists()" function
3086 */
3087 static void
3088f_exists(typval_T *argvars, typval_T *rettv)
3089{
3090 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003093 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003094 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 if (mch_getenv(p + 1) != NULL)
3098 n = TRUE;
3099 else
3100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 p = expand_env_save(p);
3103 if (p != NULL && *p != '$')
3104 n = TRUE;
3105 vim_free(p);
3106 }
3107 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003108 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003110 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003112 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003116 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003118 else if (*p == '?') // internal function only
3119 {
3120 n = has_internal_func_name(p + 1);
3121 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122 else if (*p == ':')
3123 {
3124 n = cmd_exists(p + 1);
3125 }
3126 else if (*p == '#')
3127 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 if (p[1] == '#')
3129 n = autocmd_supported(p + 2);
3130 else
3131 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003133 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003135 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 }
3137
3138 rettv->vval.v_number = n;
3139}
3140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141/*
3142 * "expand()" function
3143 */
3144 static void
3145f_expand(typval_T *argvars, typval_T *rettv)
3146{
3147 char_u *s;
3148 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3151 expand_T xpc;
3152 int error = FALSE;
3153 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003154#ifdef BACKSLASH_IN_FILENAME
3155 char_u *p_csl_save = p_csl;
3156
3157 // avoid using 'completeslash' here
3158 p_csl = empty_option;
3159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160
3161 rettv->v_type = VAR_STRING;
3162 if (argvars[1].v_type != VAR_UNKNOWN
3163 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003164 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003166 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003168 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 if (*s == '%' || *s == '#' || *s == '<')
3170 {
3171 ++emsg_off;
3172 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3173 --emsg_off;
3174 if (rettv->v_type == VAR_LIST)
3175 {
3176 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3177 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003178 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 }
3180 else
3181 rettv->vval.v_string = result;
3182 }
3183 else
3184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // When the optional second argument is non-zero, don't remove matches
3186 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003188 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 options |= WILD_KEEP_ALL;
3190 if (!error)
3191 {
3192 ExpandInit(&xpc);
3193 xpc.xp_context = EXPAND_FILES;
3194 if (p_wic)
3195 options += WILD_ICASE;
3196 if (rettv->v_type == VAR_STRING)
3197 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3198 options, WILD_ALL);
3199 else if (rettv_list_alloc(rettv) != FAIL)
3200 {
3201 int i;
3202
3203 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3204 for (i = 0; i < xpc.xp_numfiles; i++)
3205 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3206 ExpandCleanup(&xpc);
3207 }
3208 }
3209 else
3210 rettv->vval.v_string = NULL;
3211 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003212#ifdef BACKSLASH_IN_FILENAME
3213 p_csl = p_csl_save;
3214#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215}
3216
3217/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003218 * "expandcmd()" function
3219 * Expand all the special characters in a command string.
3220 */
3221 static void
3222f_expandcmd(typval_T *argvars, typval_T *rettv)
3223{
3224 exarg_T eap;
3225 char_u *cmdstr;
3226 char *errormsg = NULL;
3227
3228 rettv->v_type = VAR_STRING;
3229 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3230
3231 memset(&eap, 0, sizeof(eap));
3232 eap.cmd = cmdstr;
3233 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003234 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003235 eap.usefilter = FALSE;
3236 eap.nextcmd = NULL;
3237 eap.cmdidx = CMD_USER;
3238
3239 expand_filename(&eap, &cmdstr, &errormsg);
3240 if (errormsg != NULL && *errormsg != NUL)
3241 emsg(errormsg);
3242
3243 rettv->vval.v_string = cmdstr;
3244}
3245
3246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 * "feedkeys()" function
3248 */
3249 static void
3250f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3251{
3252 int remap = TRUE;
3253 int insert = FALSE;
3254 char_u *keys, *flags;
3255 char_u nbuf[NUMBUFLEN];
3256 int typed = FALSE;
3257 int execute = FALSE;
3258 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003259 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 char_u *keys_esc;
3261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // This is not allowed in the sandbox. If the commands would still be
3263 // executed in the sandbox it would be OK, but it probably happens later,
3264 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 if (check_secure())
3266 return;
3267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003268 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269
3270 if (argvars[1].v_type != VAR_UNKNOWN)
3271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003272 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 for ( ; *flags != NUL; ++flags)
3274 {
3275 switch (*flags)
3276 {
3277 case 'n': remap = FALSE; break;
3278 case 'm': remap = TRUE; break;
3279 case 't': typed = TRUE; break;
3280 case 'i': insert = TRUE; break;
3281 case 'x': execute = TRUE; break;
3282 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003283 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 }
3285 }
3286 }
3287
3288 if (*keys != NUL || execute)
3289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003290 // Need to escape K_SPECIAL and CSI before putting the string in the
3291 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 keys_esc = vim_strsave_escape_csi(keys);
3293 if (keys_esc != NULL)
3294 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003295 if (lowlevel)
3296 {
3297#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003298 int idx;
3299 int len = (int)STRLEN(keys);
3300
3301 for (idx = 0; idx < len; ++idx)
3302 {
3303 // if a CTRL-C was typed, set got_int, similar to what
3304 // happens in fill_input_buf()
3305 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3306 got_int = TRUE;
3307 add_to_input_buf(keys + idx, 1);
3308 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003309#else
3310 emsg(_("E980: lowlevel input not supported"));
3311#endif
3312 }
3313 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003314 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003315 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003317 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003318#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003319 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003320#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003321 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003322 typebuf_was_filled = TRUE;
3323 }
3324 vim_free(keys_esc);
3325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 if (execute)
3327 {
3328 int save_msg_scroll = msg_scroll;
3329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331 msg_scroll = FALSE;
3332
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003333 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003334 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003335 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003336 ++in_feedkeys;
3337 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003338 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003339 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003340 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003341 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003342 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003343 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 msg_scroll |= save_msg_scroll;
3346 }
3347 }
3348 }
3349}
3350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351/*
3352 * "fnameescape({string})" function
3353 */
3354 static void
3355f_fnameescape(typval_T *argvars, typval_T *rettv)
3356{
3357 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 rettv->v_type = VAR_STRING;
3360}
3361
3362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 * "foreground()" function
3364 */
3365 static void
3366f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3367{
3368#ifdef FEAT_GUI
3369 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003370 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003372 return;
3373 }
3374#endif
3375#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003376 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377#endif
3378}
3379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003381common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382{
3383 char_u *s;
3384 char_u *name;
3385 int use_string = FALSE;
3386 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003387 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003388 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389
3390 if (argvars[0].v_type == VAR_FUNC)
3391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003392 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 s = argvars[0].vval.v_string;
3394 }
3395 else if (argvars[0].v_type == VAR_PARTIAL
3396 && argvars[0].vval.v_partial != NULL)
3397 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003400 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 }
3402 else
3403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003405 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406 use_string = TRUE;
3407 }
3408
Bram Moolenaar843b8842016-08-21 14:36:15 +02003409 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003410 {
3411 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003412 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003413 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3414 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003415 if (*name != NUL)
3416 s = NULL;
3417 }
3418
Bram Moolenaar843b8842016-08-21 14:36:15 +02003419 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3420 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003421 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003423 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003424 ? find_func(trans_name, is_global, NULL) == NULL
3425 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003426 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 else
3428 {
3429 int dict_idx = 0;
3430 int arg_idx = 0;
3431 list_T *list = NULL;
3432
3433 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3434 {
3435 char sid_buf[25];
3436 int off = *s == 's' ? 2 : 5;
3437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // Expand s: and <SID> into <SNR>nr_, so that the function can
3439 // also be called from another script. Using trans_function_name()
3440 // would also work, but some plugins depend on the name being
3441 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003442 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003443 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 if (name != NULL)
3445 {
3446 STRCPY(name, sid_buf);
3447 STRCAT(name, s + off);
3448 }
3449 }
3450 else
3451 name = vim_strsave(s);
3452
3453 if (argvars[1].v_type != VAR_UNKNOWN)
3454 {
3455 if (argvars[2].v_type != VAR_UNKNOWN)
3456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003457 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 arg_idx = 1;
3459 dict_idx = 2;
3460 }
3461 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 dict_idx = 1;
3464 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003465 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003466 arg_idx = 1;
3467 if (dict_idx > 0)
3468 {
3469 if (argvars[dict_idx].v_type != VAR_DICT)
3470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003473 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 }
3475 if (argvars[dict_idx].vval.v_dict == NULL)
3476 dict_idx = 0;
3477 }
3478 if (arg_idx > 0)
3479 {
3480 if (argvars[arg_idx].v_type != VAR_LIST)
3481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003482 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003484 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 }
3486 list = argvars[arg_idx].vval.v_list;
3487 if (list == NULL || list->lv_len == 0)
3488 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003489 else if (list->lv_len > MAX_FUNC_ARGS)
3490 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003491 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003492 vim_free(name);
3493 goto theend;
3494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 }
3496 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003497 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003499 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003501 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 if (pt == NULL)
3503 vim_free(name);
3504 else
3505 {
3506 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3507 {
3508 listitem_T *li;
3509 int i = 0;
3510 int arg_len = 0;
3511 int lv_len = 0;
3512
3513 if (arg_pt != NULL)
3514 arg_len = arg_pt->pt_argc;
3515 if (list != NULL)
3516 lv_len = list->lv_len;
3517 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003518 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 if (pt->pt_argv == NULL)
3520 {
3521 vim_free(pt);
3522 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003523 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003525 for (i = 0; i < arg_len; i++)
3526 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3527 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003528 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003529 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003530 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003531 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 }
3534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 // For "function(dict.func, [], dict)" and "func" is a partial
3536 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537 if (dict_idx > 0)
3538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3541 ++pt->pt_dict->dv_refcount;
3542 }
3543 else if (arg_pt != NULL)
3544 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 // If the dict was bound automatically the result is also
3546 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 pt->pt_dict = arg_pt->pt_dict;
3548 pt->pt_auto = arg_pt->pt_auto;
3549 if (pt->pt_dict != NULL)
3550 ++pt->pt_dict->dv_refcount;
3551 }
3552
3553 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003554 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3555 {
3556 pt->pt_func = arg_pt->pt_func;
3557 func_ptr_ref(pt->pt_func);
3558 vim_free(name);
3559 }
3560 else if (is_funcref)
3561 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003562 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003563 func_ptr_ref(pt->pt_func);
3564 vim_free(name);
3565 }
3566 else
3567 {
3568 pt->pt_name = name;
3569 func_ref(name);
3570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 }
3572 rettv->v_type = VAR_PARTIAL;
3573 rettv->vval.v_partial = pt;
3574 }
3575 else
3576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578 rettv->v_type = VAR_FUNC;
3579 rettv->vval.v_string = name;
3580 func_ref(name);
3581 }
3582 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003583theend:
3584 vim_free(trans_name);
3585}
3586
3587/*
3588 * "funcref()" function
3589 */
3590 static void
3591f_funcref(typval_T *argvars, typval_T *rettv)
3592{
3593 common_function(argvars, rettv, TRUE);
3594}
3595
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003596 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003597ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003598{
3599 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3600 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003601 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003602}
3603
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003604/*
3605 * "function()" function
3606 */
3607 static void
3608f_function(typval_T *argvars, typval_T *rettv)
3609{
3610 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611}
3612
3613/*
3614 * "garbagecollect()" function
3615 */
3616 static void
3617f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3618{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 // This is postponed until we are back at the toplevel, because we may be
3620 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621 want_garbage_collect = TRUE;
3622
Bram Moolenaar2df47312020-09-05 17:30:44 +02003623 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 garbage_collect_at_exit = TRUE;
3625}
3626
3627/*
3628 * "get()" function
3629 */
3630 static void
3631f_get(typval_T *argvars, typval_T *rettv)
3632{
3633 listitem_T *li;
3634 list_T *l;
3635 dictitem_T *di;
3636 dict_T *d;
3637 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003638 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003640 if (argvars[0].v_type == VAR_BLOB)
3641 {
3642 int error = FALSE;
3643 int idx = tv_get_number_chk(&argvars[1], &error);
3644
3645 if (!error)
3646 {
3647 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003648 if (idx < 0)
3649 idx = blob_len(argvars[0].vval.v_blob) + idx;
3650 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3651 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003652 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003654 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003655 tv = rettv;
3656 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003657 }
3658 }
3659 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 {
3661 if ((l = argvars[0].vval.v_list) != NULL)
3662 {
3663 int error = FALSE;
3664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003665 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 if (!error && li != NULL)
3667 tv = &li->li_tv;
3668 }
3669 }
3670 else if (argvars[0].v_type == VAR_DICT)
3671 {
3672 if ((d = argvars[0].vval.v_dict) != NULL)
3673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 if (di != NULL)
3676 tv = &di->di_tv;
3677 }
3678 }
3679 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3680 {
3681 partial_T *pt;
3682 partial_T fref_pt;
3683
3684 if (argvars[0].v_type == VAR_PARTIAL)
3685 pt = argvars[0].vval.v_partial;
3686 else
3687 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003688 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 fref_pt.pt_name = argvars[0].vval.v_string;
3690 pt = &fref_pt;
3691 }
3692
3693 if (pt != NULL)
3694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003695 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697
3698 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3699 {
3700 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003701 n = partial_name(pt);
3702 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703 rettv->vval.v_string = NULL;
3704 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 {
3706 rettv->vval.v_string = vim_strsave(n);
3707 if (rettv->v_type == VAR_FUNC)
3708 func_ref(rettv->vval.v_string);
3709 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 }
3711 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003712 {
3713 what_is_dict = TRUE;
3714 if (pt->pt_dict != NULL)
3715 rettv_dict_set(rettv, pt->pt_dict);
3716 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 else if (STRCMP(what, "args") == 0)
3718 {
3719 rettv->v_type = VAR_LIST;
3720 if (rettv_list_alloc(rettv) == OK)
3721 {
3722 int i;
3723
3724 for (i = 0; i < pt->pt_argc; ++i)
3725 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3726 }
3727 }
3728 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003729 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003730
3731 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3732 // third argument
3733 if (!what_is_dict)
3734 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 }
3736 }
3737 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003738 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
3740 if (tv == NULL)
3741 {
3742 if (argvars[2].v_type != VAR_UNKNOWN)
3743 copy_tv(&argvars[2], rettv);
3744 }
3745 else
3746 copy_tv(tv, rettv);
3747}
3748
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003749/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003750 * "getchangelist()" function
3751 */
3752 static void
3753f_getchangelist(typval_T *argvars, typval_T *rettv)
3754{
3755#ifdef FEAT_JUMPLIST
3756 buf_T *buf;
3757 int i;
3758 list_T *l;
3759 dict_T *d;
3760#endif
3761
3762 if (rettv_list_alloc(rettv) != OK)
3763 return;
3764
3765#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003766 if (argvars[0].v_type == VAR_UNKNOWN)
3767 buf = curbuf;
3768 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003769 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003770 if (buf == NULL)
3771 return;
3772
3773 l = list_alloc();
3774 if (l == NULL)
3775 return;
3776
3777 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3778 return;
3779 /*
3780 * The current window change list index tracks only the position in the
3781 * current buffer change list. For other buffers, use the change list
3782 * length as the current index.
3783 */
3784 list_append_number(rettv->vval.v_list,
3785 (varnumber_T)((buf == curwin->w_buffer)
3786 ? curwin->w_changelistidx : buf->b_changelistlen));
3787
3788 for (i = 0; i < buf->b_changelistlen; ++i)
3789 {
3790 if (buf->b_changelist[i].lnum == 0)
3791 continue;
3792 if ((d = dict_alloc()) == NULL)
3793 return;
3794 if (list_append_dict(l, d) == FAIL)
3795 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003796 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3797 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003798 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003799 }
3800#endif
3801}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003803 static void
3804getpos_both(
3805 typval_T *argvars,
3806 typval_T *rettv,
3807 int getcurpos,
3808 int charcol)
3809{
3810 pos_T *fp = NULL;
3811 pos_T pos;
3812 win_T *wp = curwin;
3813 list_T *l;
3814 int fnum = -1;
3815
3816 if (rettv_list_alloc(rettv) == OK)
3817 {
3818 l = rettv->vval.v_list;
3819 if (getcurpos)
3820 {
3821 if (argvars[0].v_type != VAR_UNKNOWN)
3822 {
3823 wp = find_win_by_nr_or_id(&argvars[0]);
3824 if (wp != NULL)
3825 fp = &wp->w_cursor;
3826 }
3827 else
3828 fp = &curwin->w_cursor;
3829 if (fp != NULL && charcol)
3830 {
3831 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003832 pos.col =
3833 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003834 fp = &pos;
3835 }
3836 }
3837 else
3838 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3839 if (fnum != -1)
3840 list_append_number(l, (varnumber_T)fnum);
3841 else
3842 list_append_number(l, (varnumber_T)0);
3843 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3844 : (varnumber_T)0);
3845 list_append_number(l, (fp != NULL)
3846 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3847 : (varnumber_T)0);
3848 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3849 (varnumber_T)0);
3850 if (getcurpos)
3851 {
3852 int save_set_curswant = curwin->w_set_curswant;
3853 colnr_T save_curswant = curwin->w_curswant;
3854 colnr_T save_virtcol = curwin->w_virtcol;
3855
3856 if (wp == curwin)
3857 update_curswant();
3858 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3859 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3860
3861 // Do not change "curswant", as it is unexpected that a get
3862 // function has a side effect.
3863 if (wp == curwin && save_set_curswant)
3864 {
3865 curwin->w_set_curswant = save_set_curswant;
3866 curwin->w_curswant = save_curswant;
3867 curwin->w_virtcol = save_virtcol;
3868 curwin->w_valid &= ~VALID_VIRTCOL;
3869 }
3870 }
3871 }
3872 else
3873 rettv->vval.v_number = FALSE;
3874}
3875
3876/*
3877 * "getcharpos()" function
3878 */
3879 static void
3880f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3881{
3882 getpos_both(argvars, rettv, FALSE, TRUE);
3883}
3884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885/*
3886 * "getcharsearch()" function
3887 */
3888 static void
3889f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3890{
3891 if (rettv_dict_alloc(rettv) != FAIL)
3892 {
3893 dict_T *dict = rettv->vval.v_dict;
3894
Bram Moolenaare0be1672018-07-08 16:50:37 +02003895 dict_add_string(dict, "char", last_csearch());
3896 dict_add_number(dict, "forward", last_csearch_forward());
3897 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 }
3899}
3900
3901/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003902 * "getenv()" function
3903 */
3904 static void
3905f_getenv(typval_T *argvars, typval_T *rettv)
3906{
3907 int mustfree = FALSE;
3908 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3909
3910 if (p == NULL)
3911 {
3912 rettv->v_type = VAR_SPECIAL;
3913 rettv->vval.v_number = VVAL_NULL;
3914 return;
3915 }
3916 if (!mustfree)
3917 p = vim_strsave(p);
3918 rettv->vval.v_string = p;
3919 rettv->v_type = VAR_STRING;
3920}
3921
3922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923 * "getfontname()" function
3924 */
3925 static void
3926f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3927{
3928 rettv->v_type = VAR_STRING;
3929 rettv->vval.v_string = NULL;
3930#ifdef FEAT_GUI
3931 if (gui.in_use)
3932 {
3933 GuiFont font;
3934 char_u *name = NULL;
3935
3936 if (argvars[0].v_type == VAR_UNKNOWN)
3937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003938 // Get the "Normal" font. Either the name saved by
3939 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 font = gui.norm_font;
3941 name = hl_get_font_name();
3942 }
3943 else
3944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003946 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 return;
3948 font = gui_mch_get_font(name, FALSE);
3949 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003950 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 }
3952 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3953 if (argvars[0].v_type != VAR_UNKNOWN)
3954 gui_mch_free_font(font);
3955 }
3956#endif
3957}
3958
3959/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003960 * "getjumplist()" function
3961 */
3962 static void
3963f_getjumplist(typval_T *argvars, typval_T *rettv)
3964{
3965#ifdef FEAT_JUMPLIST
3966 win_T *wp;
3967 int i;
3968 list_T *l;
3969 dict_T *d;
3970#endif
3971
3972 if (rettv_list_alloc(rettv) != OK)
3973 return;
3974
3975#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003976 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003977 if (wp == NULL)
3978 return;
3979
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003980 cleanup_jumplist(wp, TRUE);
3981
Bram Moolenaar4f505882018-02-10 21:06:32 +01003982 l = list_alloc();
3983 if (l == NULL)
3984 return;
3985
3986 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3987 return;
3988 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3989
3990 for (i = 0; i < wp->w_jumplistlen; ++i)
3991 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003992 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3993 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003994 if ((d = dict_alloc()) == NULL)
3995 return;
3996 if (list_append_dict(l, d) == FAIL)
3997 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003998 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3999 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004000 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004001 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004002 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004003 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004004 }
4005#endif
4006}
4007
4008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 * "getpid()" function
4010 */
4011 static void
4012f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4013{
4014 rettv->vval.v_number = mch_get_pid();
4015}
4016
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017/*
4018 * "getcurpos()" function
4019 */
4020 static void
4021f_getcurpos(typval_T *argvars, typval_T *rettv)
4022{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004023 getpos_both(argvars, rettv, TRUE, FALSE);
4024}
4025
4026 static void
4027f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4028{
4029 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030}
4031
4032/*
4033 * "getpos(string)" function
4034 */
4035 static void
4036f_getpos(typval_T *argvars, typval_T *rettv)
4037{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004038 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039}
4040
4041/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 * "getreg()" function
4043 */
4044 static void
4045f_getreg(typval_T *argvars, typval_T *rettv)
4046{
4047 char_u *strregname;
4048 int regname;
4049 int arg2 = FALSE;
4050 int return_list = FALSE;
4051 int error = FALSE;
4052
4053 if (argvars[0].v_type != VAR_UNKNOWN)
4054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004055 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004056 if (strregname == NULL)
4057 error = TRUE;
4058 else if (in_vim9script() && STRLEN(strregname) > 1)
4059 {
4060 semsg(_(e_register_name_must_be_one_char_str), strregname);
4061 error = TRUE;
4062 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (argvars[1].v_type != VAR_UNKNOWN)
4064 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004065 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004067 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004068 }
4069 }
4070 else
4071 strregname = get_vim_var_str(VV_REG);
4072
4073 if (error)
4074 return;
4075
4076 regname = (strregname == NULL ? '"' : *strregname);
4077 if (regname == 0)
4078 regname = '"';
4079
4080 if (return_list)
4081 {
4082 rettv->v_type = VAR_LIST;
4083 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4084 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4085 if (rettv->vval.v_list == NULL)
4086 (void)rettv_list_alloc(rettv);
4087 else
4088 ++rettv->vval.v_list->lv_refcount;
4089 }
4090 else
4091 {
4092 rettv->v_type = VAR_STRING;
4093 rettv->vval.v_string = get_reg_contents(regname,
4094 arg2 ? GREG_EXPR_SRC : 0);
4095 }
4096}
4097
4098/*
4099 * "getregtype()" function
4100 */
4101 static void
4102f_getregtype(typval_T *argvars, typval_T *rettv)
4103{
4104 char_u *strregname;
4105 int regname;
4106 char_u buf[NUMBUFLEN + 2];
4107 long reglen = 0;
4108
4109 if (argvars[0].v_type != VAR_UNKNOWN)
4110 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004111 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004112 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4113 {
4114 semsg(_(e_register_name_must_be_one_char_str), strregname);
4115 strregname = NULL;
4116 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004117 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118 {
4119 rettv->v_type = VAR_STRING;
4120 rettv->vval.v_string = NULL;
4121 return;
4122 }
4123 }
4124 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 strregname = get_vim_var_str(VV_REG);
4127
4128 regname = (strregname == NULL ? '"' : *strregname);
4129 if (regname == 0)
4130 regname = '"';
4131
4132 buf[0] = NUL;
4133 buf[1] = NUL;
4134 switch (get_reg_type(regname, &reglen))
4135 {
4136 case MLINE: buf[0] = 'V'; break;
4137 case MCHAR: buf[0] = 'v'; break;
4138 case MBLOCK:
4139 buf[0] = Ctrl_V;
4140 sprintf((char *)buf + 1, "%ld", reglen + 1);
4141 break;
4142 }
4143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = vim_strsave(buf);
4145}
4146
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004147/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004148 * "gettagstack()" function
4149 */
4150 static void
4151f_gettagstack(typval_T *argvars, typval_T *rettv)
4152{
4153 win_T *wp = curwin; // default is current window
4154
4155 if (rettv_dict_alloc(rettv) != OK)
4156 return;
4157
4158 if (argvars[0].v_type != VAR_UNKNOWN)
4159 {
4160 wp = find_win_by_nr_or_id(&argvars[0]);
4161 if (wp == NULL)
4162 return;
4163 }
4164
4165 get_tagstack(wp, rettv->vval.v_dict);
4166}
4167
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004168/*
4169 * "gettext()" function
4170 */
4171 static void
4172f_gettext(typval_T *argvars, typval_T *rettv)
4173{
4174 if (argvars[0].v_type != VAR_STRING
4175 || argvars[0].vval.v_string == NULL
4176 || *argvars[0].vval.v_string == NUL)
4177 {
4178 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4179 }
4180 else
4181 {
4182 rettv->v_type = VAR_STRING;
4183 rettv->vval.v_string = vim_strsave(
4184 (char_u *)_(argvars[0].vval.v_string));
4185 }
4186}
4187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189#include "version.h"
4190
4191/*
4192 * "has()" function
4193 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004194 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004195f_has(typval_T *argvars, typval_T *rettv)
4196{
4197 int i;
4198 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004199 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004200 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004201 typedef struct {
4202 char *name;
4203 short present;
4204 } has_item_T;
4205 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004207 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004209 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004210#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004211 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004212#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004213 },
4214 {"arp",
4215#if defined(AMIGA) && defined(FEAT_ARP)
4216 1
4217#else
4218 0
4219#endif
4220 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004221 {"haiku",
4222#ifdef __HAIKU__
4223 1
4224#else
4225 0
4226#endif
4227 },
4228 {"bsd",
4229#if defined(BSD) && !defined(MACOS_X)
4230 1
4231#else
4232 0
4233#endif
4234 },
4235 {"hpux",
4236#ifdef hpux
4237 1
4238#else
4239 0
4240#endif
4241 },
4242 {"linux",
4243#ifdef __linux__
4244 1
4245#else
4246 0
4247#endif
4248 },
4249 {"mac", // Mac OS X (and, once, Mac OS Classic)
4250#ifdef MACOS_X
4251 1
4252#else
4253 0
4254#endif
4255 },
4256 {"osx", // Mac OS X
4257#ifdef MACOS_X
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"macunix", // Mac OS X, with the darwin feature
4264#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"osxdarwin", // synonym for macunix
4271#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"qnx",
4278#ifdef __QNX__
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"sun",
4285#ifdef SUN_SYSTEM
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004293 1
4294#else
4295 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004297 },
4298 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004300 1
4301#else
4302 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004304 },
4305 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004306#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004307 1
4308#else
4309 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004311 },
4312 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004313#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004314 1
4315#else
4316 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004318 },
4319 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004320#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004321 1
4322#else
4323 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004325 },
4326 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004328 1
4329#else
4330 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004332 },
4333 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004335 1
4336#else
4337 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004339 },
4340 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004342 1
4343#else
4344 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004346 },
4347 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004349 1
4350#else
4351 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004353 },
4354 {"autocmd", 1},
4355 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004356#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004357 1
4358#else
4359 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004360#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004361 },
4362 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004363#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004364 1
4365#else
4366 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004367#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004368 },
4369 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004370#ifdef FEAT_BEVAL_GUI
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 {"balloon_multiline",
4377#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4378 // MS-Windows requires runtime check, see below
4379 1
4380#else
4381 0
4382#endif
4383 },
4384 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004385#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004386 1
4387#else
4388 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004389#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004390 },
4391 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
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 {"all_builtin_terms",
4399#if defined(ALL_BUILTIN_TCAPS)
4400 1
4401#else
4402 0
4403#endif
4404 },
4405 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004407 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004409 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004411 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004413 },
4414 {"byte_offset",
4415#ifdef FEAT_BYTEOFF
4416 1
4417#else
4418 0
4419#endif
4420 },
4421 {"channel",
4422#ifdef FEAT_JOB_CHANNEL
4423 1
4424#else
4425 0
4426#endif
4427 },
4428 {"cindent",
4429#ifdef FEAT_CINDENT
4430 1
4431#else
4432 0
4433#endif
4434 },
4435 {"clientserver",
4436#ifdef FEAT_CLIENTSERVER
4437 1
4438#else
4439 0
4440#endif
4441 },
4442 {"clipboard",
4443#ifdef FEAT_CLIPBOARD
4444 1
4445#else
4446 0
4447#endif
4448 },
4449 {"cmdline_compl", 1},
4450 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004451 {"cmdwin",
4452#ifdef FEAT_CMDWIN
4453 1
4454#else
4455 0
4456#endif
4457 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004458 {"comments", 1},
4459 {"conceal",
4460#ifdef FEAT_CONCEAL
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"cryptv",
4467#ifdef FEAT_CRYPT
4468 1
4469#else
4470 0
4471#endif
4472 },
4473 {"crypt-blowfish",
4474#ifdef FEAT_CRYPT
4475 1
4476#else
4477 0
4478#endif
4479 },
4480 {"crypt-blowfish2",
4481#ifdef FEAT_CRYPT
4482 1
4483#else
4484 0
4485#endif
4486 },
4487 {"cscope",
4488#ifdef FEAT_CSCOPE
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"cursorbind", 1},
4495 {"cursorshape",
4496#ifdef CURSOR_SHAPE
4497 1
4498#else
4499 0
4500#endif
4501 },
4502 {"debug",
4503#ifdef DEBUG
4504 1
4505#else
4506 0
4507#endif
4508 },
4509 {"dialog_con",
4510#ifdef FEAT_CON_DIALOG
4511 1
4512#else
4513 0
4514#endif
4515 },
4516 {"dialog_gui",
4517#ifdef FEAT_GUI_DIALOG
4518 1
4519#else
4520 0
4521#endif
4522 },
4523 {"diff",
4524#ifdef FEAT_DIFF
4525 1
4526#else
4527 0
4528#endif
4529 },
4530 {"digraphs",
4531#ifdef FEAT_DIGRAPHS
4532 1
4533#else
4534 0
4535#endif
4536 },
4537 {"directx",
4538#ifdef FEAT_DIRECTX
4539 1
4540#else
4541 0
4542#endif
4543 },
4544 {"dnd",
4545#ifdef FEAT_DND
4546 1
4547#else
4548 0
4549#endif
4550 },
4551 {"emacs_tags",
4552#ifdef FEAT_EMACS_TAGS
4553 1
4554#else
4555 0
4556#endif
4557 },
4558 {"eval", 1}, // always present, of course!
4559 {"ex_extra", 1}, // graduated feature
4560 {"extra_search",
4561#ifdef FEAT_SEARCH_EXTRA
4562 1
4563#else
4564 0
4565#endif
4566 },
4567 {"file_in_path",
4568#ifdef FEAT_SEARCHPATH
4569 1
4570#else
4571 0
4572#endif
4573 },
4574 {"filterpipe",
4575#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4576 1
4577#else
4578 0
4579#endif
4580 },
4581 {"find_in_path",
4582#ifdef FEAT_FIND_ID
4583 1
4584#else
4585 0
4586#endif
4587 },
4588 {"float",
4589#ifdef FEAT_FLOAT
4590 1
4591#else
4592 0
4593#endif
4594 },
4595 {"folding",
4596#ifdef FEAT_FOLDING
4597 1
4598#else
4599 0
4600#endif
4601 },
4602 {"footer",
4603#ifdef FEAT_FOOTER
4604 1
4605#else
4606 0
4607#endif
4608 },
4609 {"fork",
4610#if !defined(USE_SYSTEM) && defined(UNIX)
4611 1
4612#else
4613 0
4614#endif
4615 },
4616 {"gettext",
4617#ifdef FEAT_GETTEXT
4618 1
4619#else
4620 0
4621#endif
4622 },
4623 {"gui",
4624#ifdef FEAT_GUI
4625 1
4626#else
4627 0
4628#endif
4629 },
4630 {"gui_neXtaw",
4631#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4632 1
4633#else
4634 0
4635#endif
4636 },
4637 {"gui_athena",
4638#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4639 1
4640#else
4641 0
4642#endif
4643 },
4644 {"gui_gtk",
4645#ifdef FEAT_GUI_GTK
4646 1
4647#else
4648 0
4649#endif
4650 },
4651 {"gui_gtk2",
4652#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4653 1
4654#else
4655 0
4656#endif
4657 },
4658 {"gui_gtk3",
4659#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4660 1
4661#else
4662 0
4663#endif
4664 },
4665 {"gui_gnome",
4666#ifdef FEAT_GUI_GNOME
4667 1
4668#else
4669 0
4670#endif
4671 },
4672 {"gui_haiku",
4673#ifdef FEAT_GUI_HAIKU
4674 1
4675#else
4676 0
4677#endif
4678 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004679 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004680 {"gui_motif",
4681#ifdef FEAT_GUI_MOTIF
4682 1
4683#else
4684 0
4685#endif
4686 },
4687 {"gui_photon",
4688#ifdef FEAT_GUI_PHOTON
4689 1
4690#else
4691 0
4692#endif
4693 },
4694 {"gui_win32",
4695#ifdef FEAT_GUI_MSWIN
4696 1
4697#else
4698 0
4699#endif
4700 },
4701 {"iconv",
4702#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4703 1
4704#else
4705 0
4706#endif
4707 },
4708 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004709 {"ipv6",
4710#ifdef FEAT_IPV6
4711 1
4712#else
4713 0
4714#endif
4715 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004716 {"job",
4717#ifdef FEAT_JOB_CHANNEL
4718 1
4719#else
4720 0
4721#endif
4722 },
4723 {"jumplist",
4724#ifdef FEAT_JUMPLIST
4725 1
4726#else
4727 0
4728#endif
4729 },
4730 {"keymap",
4731#ifdef FEAT_KEYMAP
4732 1
4733#else
4734 0
4735#endif
4736 },
4737 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4738 {"langmap",
4739#ifdef FEAT_LANGMAP
4740 1
4741#else
4742 0
4743#endif
4744 },
4745 {"libcall",
4746#ifdef FEAT_LIBCALL
4747 1
4748#else
4749 0
4750#endif
4751 },
4752 {"linebreak",
4753#ifdef FEAT_LINEBREAK
4754 1
4755#else
4756 0
4757#endif
4758 },
4759 {"lispindent",
4760#ifdef FEAT_LISP
4761 1
4762#else
4763 0
4764#endif
4765 },
4766 {"listcmds", 1},
4767 {"localmap", 1},
4768 {"lua",
4769#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4770 1
4771#else
4772 0
4773#endif
4774 },
4775 {"menu",
4776#ifdef FEAT_MENU
4777 1
4778#else
4779 0
4780#endif
4781 },
4782 {"mksession",
4783#ifdef FEAT_SESSION
4784 1
4785#else
4786 0
4787#endif
4788 },
4789 {"modify_fname", 1},
4790 {"mouse", 1},
4791 {"mouseshape",
4792#ifdef FEAT_MOUSESHAPE
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"mouse_dec",
4799#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"mouse_gpm",
4806#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"mouse_jsbterm",
4813#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"mouse_netterm",
4820#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"mouse_pterm",
4827#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"mouse_sgr",
4834#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"mouse_sysmouse",
4841#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"mouse_urxvt",
4848#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"mouse_xterm",
4855#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"multi_byte", 1},
4862 {"multi_byte_ime",
4863#ifdef FEAT_MBYTE_IME
4864 1
4865#else
4866 0
4867#endif
4868 },
4869 {"multi_lang",
4870#ifdef FEAT_MULTI_LANG
4871 1
4872#else
4873 0
4874#endif
4875 },
4876 {"mzscheme",
4877#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4878 1
4879#else
4880 0
4881#endif
4882 },
4883 {"num64", 1},
4884 {"ole",
4885#ifdef FEAT_OLE
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"packages",
4892#ifdef FEAT_EVAL
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"path_extra",
4899#ifdef FEAT_PATH_EXTRA
4900 1
4901#else
4902 0
4903#endif
4904 },
4905 {"perl",
4906#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4907 1
4908#else
4909 0
4910#endif
4911 },
4912 {"persistent_undo",
4913#ifdef FEAT_PERSISTENT_UNDO
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"python_compiled",
4920#if defined(FEAT_PYTHON)
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"python_dynamic",
4927#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"python",
4934#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"pythonx",
4941#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4942 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"python3_compiled",
4949#if defined(FEAT_PYTHON3)
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"python3_dynamic",
4956#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"python3",
4963#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"popupwin",
4970#ifdef FEAT_PROP_POPUP
4971 1
4972#else
4973 0
4974#endif
4975 },
4976 {"postscript",
4977#ifdef FEAT_POSTSCRIPT
4978 1
4979#else
4980 0
4981#endif
4982 },
4983 {"printer",
4984#ifdef FEAT_PRINTER
4985 1
4986#else
4987 0
4988#endif
4989 },
4990 {"profile",
4991#ifdef FEAT_PROFILE
4992 1
4993#else
4994 0
4995#endif
4996 },
4997 {"reltime",
4998#ifdef FEAT_RELTIME
4999 1
5000#else
5001 0
5002#endif
5003 },
5004 {"quickfix",
5005#ifdef FEAT_QUICKFIX
5006 1
5007#else
5008 0
5009#endif
5010 },
5011 {"rightleft",
5012#ifdef FEAT_RIGHTLEFT
5013 1
5014#else
5015 0
5016#endif
5017 },
5018 {"ruby",
5019#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5020 1
5021#else
5022 0
5023#endif
5024 },
5025 {"scrollbind", 1},
5026 {"showcmd",
5027#ifdef FEAT_CMDL_INFO
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"cmdline_info",
5034#ifdef FEAT_CMDL_INFO
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"signs",
5041#ifdef FEAT_SIGNS
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"smartindent",
5048#ifdef FEAT_SMARTINDENT
5049 1
5050#else
5051 0
5052#endif
5053 },
5054 {"startuptime",
5055#ifdef STARTUPTIME
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"statusline",
5062#ifdef FEAT_STL_OPT
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"netbeans_intg",
5069#ifdef FEAT_NETBEANS_INTG
5070 1
5071#else
5072 0
5073#endif
5074 },
Christian Brabandtf573c6e2021-06-20 14:02:16 +02005075 {"sodium",
5076#ifdef FEAT_SODIUM
5077 1
5078#else
5079 0
5080#endif
5081 },
Bram Moolenaar79296512020-03-22 16:17:14 +01005082 {"sound",
5083#ifdef FEAT_SOUND
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"spell",
5090#ifdef FEAT_SPELL
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"syntax",
5097#ifdef FEAT_SYN_HL
5098 1
5099#else
5100 0
5101#endif
5102 },
5103 {"system",
5104#if defined(USE_SYSTEM) || !defined(UNIX)
5105 1
5106#else
5107 0
5108#endif
5109 },
5110 {"tag_binary",
5111#ifdef FEAT_TAG_BINS
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"tcl",
5118#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"termguicolors",
5125#ifdef FEAT_TERMGUICOLORS
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"terminal",
5132#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"terminfo",
5139#ifdef TERMINFO
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"termresponse",
5146#ifdef FEAT_TERMRESPONSE
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"textobjects",
5153#ifdef FEAT_TEXTOBJ
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"textprop",
5160#ifdef FEAT_PROP_POPUP
5161 1
5162#else
5163 0
5164#endif
5165 },
5166 {"tgetent",
5167#ifdef HAVE_TGETENT
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"timers",
5174#ifdef FEAT_TIMERS
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"title",
5181#ifdef FEAT_TITLE
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"toolbar",
5188#ifdef FEAT_TOOLBAR
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"unnamedplus",
5195#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"user-commands", 1}, // was accidentally included in 5.4
5202 {"user_commands", 1},
5203 {"vartabs",
5204#ifdef FEAT_VARTABS
5205 1
5206#else
5207 0
5208#endif
5209 },
5210 {"vertsplit", 1},
5211 {"viminfo",
5212#ifdef FEAT_VIMINFO
5213 1
5214#else
5215 0
5216#endif
5217 },
5218 {"vimscript-1", 1},
5219 {"vimscript-2", 1},
5220 {"vimscript-3", 1},
5221 {"vimscript-4", 1},
5222 {"virtualedit", 1},
5223 {"visual", 1},
5224 {"visualextra", 1},
5225 {"vreplace", 1},
5226 {"vtp",
5227#ifdef FEAT_VTP
5228 1
5229#else
5230 0
5231#endif
5232 },
5233 {"wildignore",
5234#ifdef FEAT_WILDIGN
5235 1
5236#else
5237 0
5238#endif
5239 },
5240 {"wildmenu",
5241#ifdef FEAT_WILDMENU
5242 1
5243#else
5244 0
5245#endif
5246 },
5247 {"windows", 1},
5248 {"winaltkeys",
5249#ifdef FEAT_WAK
5250 1
5251#else
5252 0
5253#endif
5254 },
5255 {"writebackup",
5256#ifdef FEAT_WRITEBACKUP
5257 1
5258#else
5259 0
5260#endif
5261 },
5262 {"xim",
5263#ifdef FEAT_XIM
5264 1
5265#else
5266 0
5267#endif
5268 },
5269 {"xfontset",
5270#ifdef FEAT_XFONTSET
5271 1
5272#else
5273 0
5274#endif
5275 },
5276 {"xpm",
5277#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5278 1
5279#else
5280 0
5281#endif
5282 },
5283 {"xpm_w32", // for backward compatibility
5284#ifdef FEAT_XPM_W32
5285 1
5286#else
5287 0
5288#endif
5289 },
5290 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005292 1
5293#else
5294 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005296 },
5297 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005299 1
5300#else
5301 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005303 },
5304 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005306 1
5307#else
5308 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005310 },
5311 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005313 1
5314#else
5315 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005317 },
5318 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005320 1
5321#else
5322 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005324 },
5325 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326 };
5327
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005328 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005329 for (i = 0; has_list[i].name != NULL; ++i)
5330 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005331 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005332 x = TRUE;
5333 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334 break;
5335 }
5336
Bram Moolenaar79296512020-03-22 16:17:14 +01005337 // features also in has_list[] but sometimes enabled at runtime
5338 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005340 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005341 {
5342 // intentionally empty
5343 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005344#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345 else if (STRICMP(name, "balloon_multiline") == 0)
5346 n = multiline_balloon_available();
5347#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005348#ifdef VIMDLL
5349 else if (STRICMP(name, "filterpipe") == 0)
5350 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351#endif
5352#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5353 else if (STRICMP(name, "iconv") == 0)
5354 n = iconv_enabled(FALSE);
5355#endif
5356#ifdef DYNAMIC_LUA
5357 else if (STRICMP(name, "lua") == 0)
5358 n = lua_enabled(FALSE);
5359#endif
5360#ifdef DYNAMIC_MZSCHEME
5361 else if (STRICMP(name, "mzscheme") == 0)
5362 n = mzscheme_enabled(FALSE);
5363#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005364#ifdef DYNAMIC_PERL
5365 else if (STRICMP(name, "perl") == 0)
5366 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368#ifdef DYNAMIC_PYTHON
5369 else if (STRICMP(name, "python") == 0)
5370 n = python_enabled(FALSE);
5371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372#ifdef DYNAMIC_PYTHON3
5373 else if (STRICMP(name, "python3") == 0)
5374 n = python3_enabled(FALSE);
5375#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005376#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5377 else if (STRICMP(name, "pythonx") == 0)
5378 {
5379# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5380 if (p_pyx == 0)
5381 n = python3_enabled(FALSE) || python_enabled(FALSE);
5382 else if (p_pyx == 3)
5383 n = python3_enabled(FALSE);
5384 else if (p_pyx == 2)
5385 n = python_enabled(FALSE);
5386# elif defined(DYNAMIC_PYTHON)
5387 n = python_enabled(FALSE);
5388# elif defined(DYNAMIC_PYTHON3)
5389 n = python3_enabled(FALSE);
5390# endif
5391 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005392#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005393#ifdef DYNAMIC_RUBY
5394 else if (STRICMP(name, "ruby") == 0)
5395 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005397#ifdef DYNAMIC_TCL
5398 else if (STRICMP(name, "tcl") == 0)
5399 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005400#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005401#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005402 else if (STRICMP(name, "terminal") == 0)
5403 n = terminal_enabled();
5404#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005405 }
5406
Bram Moolenaar79296512020-03-22 16:17:14 +01005407 // features not in has_list[]
5408 if (x == FALSE)
5409 {
5410 if (STRNICMP(name, "patch", 5) == 0)
5411 {
5412 x = TRUE;
5413 if (name[5] == '-'
5414 && STRLEN(name) >= 11
5415 && vim_isdigit(name[6])
5416 && vim_isdigit(name[8])
5417 && vim_isdigit(name[10]))
5418 {
5419 int major = atoi((char *)name + 6);
5420 int minor = atoi((char *)name + 8);
5421
5422 // Expect "patch-9.9.01234".
5423 n = (major < VIM_VERSION_MAJOR
5424 || (major == VIM_VERSION_MAJOR
5425 && (minor < VIM_VERSION_MINOR
5426 || (minor == VIM_VERSION_MINOR
5427 && has_patch(atoi((char *)name + 10))))));
5428 }
5429 else
5430 n = has_patch(atoi((char *)name + 5));
5431 }
5432 else if (STRICMP(name, "vim_starting") == 0)
5433 {
5434 x = TRUE;
5435 n = (starting != 0);
5436 }
5437 else if (STRICMP(name, "ttyin") == 0)
5438 {
5439 x = TRUE;
5440 n = mch_input_isatty();
5441 }
5442 else if (STRICMP(name, "ttyout") == 0)
5443 {
5444 x = TRUE;
5445 n = stdout_isatty;
5446 }
5447 else if (STRICMP(name, "multi_byte_encoding") == 0)
5448 {
5449 x = TRUE;
5450 n = has_mbyte;
5451 }
5452 else if (STRICMP(name, "gui_running") == 0)
5453 {
5454 x = TRUE;
5455#ifdef FEAT_GUI
5456 n = (gui.in_use || gui.starting);
5457#endif
5458 }
5459 else if (STRICMP(name, "browse") == 0)
5460 {
5461 x = TRUE;
5462#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5463 n = gui.in_use; // gui_mch_browse() works when GUI is running
5464#endif
5465 }
5466 else if (STRICMP(name, "syntax_items") == 0)
5467 {
5468 x = TRUE;
5469#ifdef FEAT_SYN_HL
5470 n = syntax_present(curwin);
5471#endif
5472 }
5473 else if (STRICMP(name, "vcon") == 0)
5474 {
5475 x = TRUE;
5476#ifdef FEAT_VTP
5477 n = is_term_win32() && has_vtp_working();
5478#endif
5479 }
5480 else if (STRICMP(name, "netbeans_enabled") == 0)
5481 {
5482 x = TRUE;
5483#ifdef FEAT_NETBEANS_INTG
5484 n = netbeans_active();
5485#endif
5486 }
5487 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5488 {
5489 x = TRUE;
5490#ifdef FEAT_MOUSE_GPM
5491 n = gpm_enabled();
5492#endif
5493 }
5494 else if (STRICMP(name, "conpty") == 0)
5495 {
5496 x = TRUE;
5497#if defined(FEAT_TERMINAL) && defined(MSWIN)
5498 n = use_conpty();
5499#endif
5500 }
5501 else if (STRICMP(name, "clipboard_working") == 0)
5502 {
5503 x = TRUE;
5504#ifdef FEAT_CLIPBOARD
5505 n = clip_star.available;
5506#endif
5507 }
5508 }
5509
Bram Moolenaar04637e22020-09-05 18:45:29 +02005510 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005511 // return whether feature could ever be enabled
5512 rettv->vval.v_number = x;
5513 else
5514 // return whether feature is enabled
5515 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005516}
5517
5518/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005519 * Return TRUE if "feature" can change later.
5520 * Also when checking for the feature has side effects, such as loading a DLL.
5521 */
5522 int
5523dynamic_feature(char_u *feature)
5524{
5525 return (feature == NULL
5526#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5527 || STRICMP(feature, "balloon_multiline") == 0
5528#endif
5529#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5530 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5531#endif
5532#ifdef VIMDLL
5533 || STRICMP(feature, "filterpipe") == 0
5534#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005535#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005536 // this can only change on Unix where the ":gui" command could be
5537 // used.
5538 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5539#endif
5540#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5541 || STRICMP(feature, "iconv") == 0
5542#endif
5543#ifdef DYNAMIC_LUA
5544 || STRICMP(feature, "lua") == 0
5545#endif
5546#ifdef FEAT_MOUSE_GPM
5547 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5548#endif
5549#ifdef DYNAMIC_MZSCHEME
5550 || STRICMP(feature, "mzscheme") == 0
5551#endif
5552#ifdef FEAT_NETBEANS_INTG
5553 || STRICMP(feature, "netbeans_enabled") == 0
5554#endif
5555#ifdef DYNAMIC_PERL
5556 || STRICMP(feature, "perl") == 0
5557#endif
5558#ifdef DYNAMIC_PYTHON
5559 || STRICMP(feature, "python") == 0
5560#endif
5561#ifdef DYNAMIC_PYTHON3
5562 || STRICMP(feature, "python3") == 0
5563#endif
5564#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5565 || STRICMP(feature, "pythonx") == 0
5566#endif
5567#ifdef DYNAMIC_RUBY
5568 || STRICMP(feature, "ruby") == 0
5569#endif
5570#ifdef FEAT_SYN_HL
5571 || STRICMP(feature, "syntax_items") == 0
5572#endif
5573#ifdef DYNAMIC_TCL
5574 || STRICMP(feature, "tcl") == 0
5575#endif
5576 // once "starting" is zero it will stay that way
5577 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5578 || STRICMP(feature, "multi_byte_encoding") == 0
5579#if defined(FEAT_TERMINAL) && defined(MSWIN)
5580 || STRICMP(feature, "conpty") == 0
5581#endif
5582 );
5583}
5584
5585/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005586 * "haslocaldir()" function
5587 */
5588 static void
5589f_haslocaldir(typval_T *argvars, typval_T *rettv)
5590{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005591 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005592 win_T *wp = NULL;
5593
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005594 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5595
5596 // Check for window-local and tab-local directories
5597 if (wp != NULL && wp->w_localdir != NULL)
5598 rettv->vval.v_number = 1;
5599 else if (tp != NULL && tp->tp_localdir != NULL)
5600 rettv->vval.v_number = 2;
5601 else
5602 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603}
5604
5605/*
5606 * "hasmapto()" function
5607 */
5608 static void
5609f_hasmapto(typval_T *argvars, typval_T *rettv)
5610{
5611 char_u *name;
5612 char_u *mode;
5613 char_u buf[NUMBUFLEN];
5614 int abbr = FALSE;
5615
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005616 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617 if (argvars[1].v_type == VAR_UNKNOWN)
5618 mode = (char_u *)"nvo";
5619 else
5620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005623 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 }
5625
5626 if (map_to_exists(name, mode, abbr))
5627 rettv->vval.v_number = TRUE;
5628 else
5629 rettv->vval.v_number = FALSE;
5630}
5631
5632/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633 * "highlightID(name)" function
5634 */
5635 static void
5636f_hlID(typval_T *argvars, typval_T *rettv)
5637{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005638 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639}
5640
5641/*
5642 * "highlight_exists()" function
5643 */
5644 static void
5645f_hlexists(typval_T *argvars, typval_T *rettv)
5646{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005647 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005648}
5649
5650/*
5651 * "hostname()" function
5652 */
5653 static void
5654f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5655{
5656 char_u hostname[256];
5657
5658 mch_get_host_name(hostname, 256);
5659 rettv->v_type = VAR_STRING;
5660 rettv->vval.v_string = vim_strsave(hostname);
5661}
5662
5663/*
5664 * iconv() function
5665 */
5666 static void
5667f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5668{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669 char_u buf1[NUMBUFLEN];
5670 char_u buf2[NUMBUFLEN];
5671 char_u *from, *to, *str;
5672 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673
5674 rettv->v_type = VAR_STRING;
5675 rettv->vval.v_string = NULL;
5676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005677 str = tv_get_string(&argvars[0]);
5678 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5679 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 vimconv.vc_type = CONV_NONE;
5681 convert_setup(&vimconv, from, to);
5682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005683 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684 if (vimconv.vc_type == CONV_NONE)
5685 rettv->vval.v_string = vim_strsave(str);
5686 else
5687 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5688
5689 convert_setup(&vimconv, NULL, NULL);
5690 vim_free(from);
5691 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692}
5693
5694/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695 * "index()" function
5696 */
5697 static void
5698f_index(typval_T *argvars, typval_T *rettv)
5699{
5700 list_T *l;
5701 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005702 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 long idx = 0;
5704 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005705 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706
5707 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005708 if (argvars[0].v_type == VAR_BLOB)
5709 {
5710 typval_T tv;
5711 int start = 0;
5712
5713 if (argvars[2].v_type != VAR_UNKNOWN)
5714 {
5715 start = tv_get_number_chk(&argvars[2], &error);
5716 if (error)
5717 return;
5718 }
5719 b = argvars[0].vval.v_blob;
5720 if (b == NULL)
5721 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005722 if (start < 0)
5723 {
5724 start = blob_len(b) + start;
5725 if (start < 0)
5726 start = 0;
5727 }
5728
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005729 for (idx = start; idx < blob_len(b); ++idx)
5730 {
5731 tv.v_type = VAR_NUMBER;
5732 tv.vval.v_number = blob_get(b, idx);
5733 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5734 {
5735 rettv->vval.v_number = idx;
5736 return;
5737 }
5738 }
5739 return;
5740 }
5741 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005742 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005743 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744 return;
5745 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005747 l = argvars[0].vval.v_list;
5748 if (l != NULL)
5749 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005750 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 item = l->lv_first;
5752 if (argvars[2].v_type != VAR_UNKNOWN)
5753 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005754 // Start at specified item. Use the cached index that list_find()
5755 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005756 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005757 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005759 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005760 if (error)
5761 item = NULL;
5762 }
5763
5764 for ( ; item != NULL; item = item->li_next, ++idx)
5765 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5766 {
5767 rettv->vval.v_number = idx;
5768 break;
5769 }
5770 }
5771}
5772
5773static int inputsecret_flag = 0;
5774
5775/*
5776 * "input()" function
5777 * Also handles inputsecret() when inputsecret is set.
5778 */
5779 static void
5780f_input(typval_T *argvars, typval_T *rettv)
5781{
5782 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5783}
5784
5785/*
5786 * "inputdialog()" function
5787 */
5788 static void
5789f_inputdialog(typval_T *argvars, typval_T *rettv)
5790{
5791#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5794 {
5795 char_u *message;
5796 char_u buf[NUMBUFLEN];
5797 char_u *defstr = (char_u *)"";
5798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005799 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005801 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5803 else
5804 IObuff[0] = NUL;
5805 if (message != NULL && defstr != NULL
5806 && do_dialog(VIM_QUESTION, NULL, message,
5807 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5808 rettv->vval.v_string = vim_strsave(IObuff);
5809 else
5810 {
5811 if (message != NULL && defstr != NULL
5812 && argvars[1].v_type != VAR_UNKNOWN
5813 && argvars[2].v_type != VAR_UNKNOWN)
5814 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005815 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005816 else
5817 rettv->vval.v_string = NULL;
5818 }
5819 rettv->v_type = VAR_STRING;
5820 }
5821 else
5822#endif
5823 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5824}
5825
5826/*
5827 * "inputlist()" function
5828 */
5829 static void
5830f_inputlist(typval_T *argvars, typval_T *rettv)
5831{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005832 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005833 listitem_T *li;
5834 int selected;
5835 int mouse_used;
5836
5837#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005838 // While starting up, there is no place to enter text. When running tests
5839 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005840 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005841 return;
5842#endif
5843 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005845 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005846 return;
5847 }
5848
5849 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005850 msg_row = Rows - 1; // for when 'cmdheight' > 1
5851 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 msg_scroll = TRUE;
5853 msg_clr_eos();
5854
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005855 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005856 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005857 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005859 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 msg_putchar('\n');
5861 }
5862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005863 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 selected = prompt_for_number(&mouse_used);
5865 if (mouse_used)
5866 selected -= lines_left;
5867
5868 rettv->vval.v_number = selected;
5869}
5870
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5872
5873/*
5874 * "inputrestore()" function
5875 */
5876 static void
5877f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5878{
5879 if (ga_userinput.ga_len > 0)
5880 {
5881 --ga_userinput.ga_len;
5882 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005883 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005884 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005885 }
5886 else if (p_verbose > 1)
5887 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005888 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 }
5891}
5892
5893/*
5894 * "inputsave()" function
5895 */
5896 static void
5897f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5898{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005899 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900 if (ga_grow(&ga_userinput, 1) == OK)
5901 {
5902 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5903 + ga_userinput.ga_len);
5904 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005905 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906 }
5907 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005908 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005909}
5910
5911/*
5912 * "inputsecret()" function
5913 */
5914 static void
5915f_inputsecret(typval_T *argvars, typval_T *rettv)
5916{
5917 ++cmdline_star;
5918 ++inputsecret_flag;
5919 f_input(argvars, rettv);
5920 --cmdline_star;
5921 --inputsecret_flag;
5922}
5923
5924/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005925 * "interrupt()" function
5926 */
5927 static void
5928f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5929{
5930 got_int = TRUE;
5931}
5932
5933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 * "invert(expr)" function
5935 */
5936 static void
5937f_invert(typval_T *argvars, typval_T *rettv)
5938{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005939 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005940}
5941
5942/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005943 * "islocked()" function
5944 */
5945 static void
5946f_islocked(typval_T *argvars, typval_T *rettv)
5947{
5948 lval_T lv;
5949 char_u *end;
5950 dictitem_T *di;
5951
5952 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005953 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005954 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955 if (end != NULL && lv.ll_name != NULL)
5956 {
5957 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005958 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 else
5960 {
5961 if (lv.ll_tv == NULL)
5962 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005963 di = find_var(lv.ll_name, NULL, TRUE);
5964 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // Consider a variable locked when:
5967 // 1. the variable itself is locked
5968 // 2. the value of the variable is locked.
5969 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005970 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5971 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 }
5973 }
5974 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005975 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005977 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005980 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5981 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005982 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5984 }
5985 }
5986
5987 clear_lval(&lv);
5988}
5989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 * "last_buffer_nr()" function.
5992 */
5993 static void
5994f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5995{
5996 int n = 0;
5997 buf_T *buf;
5998
Bram Moolenaar29323592016-07-24 22:04:11 +02005999 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 if (n < buf->b_fnum)
6001 n = buf->b_fnum;
6002
6003 rettv->vval.v_number = n;
6004}
6005
6006/*
6007 * "len()" function
6008 */
6009 static void
6010f_len(typval_T *argvars, typval_T *rettv)
6011{
6012 switch (argvars[0].v_type)
6013 {
6014 case VAR_STRING:
6015 case VAR_NUMBER:
6016 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006017 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006019 case VAR_BLOB:
6020 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6021 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 case VAR_LIST:
6023 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6024 break;
6025 case VAR_DICT:
6026 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6027 break;
6028 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006029 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006030 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006031 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006032 case VAR_SPECIAL:
6033 case VAR_FLOAT:
6034 case VAR_FUNC:
6035 case VAR_PARTIAL:
6036 case VAR_JOB:
6037 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006038 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006039 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006040 break;
6041 }
6042}
6043
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006044 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006045libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046{
6047#ifdef FEAT_LIBCALL
6048 char_u *string_in;
6049 char_u **string_result;
6050 int nr_result;
6051#endif
6052
6053 rettv->v_type = type;
6054 if (type != VAR_NUMBER)
6055 rettv->vval.v_string = NULL;
6056
6057 if (check_restricted() || check_secure())
6058 return;
6059
6060#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006061 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6063 {
6064 string_in = NULL;
6065 if (argvars[2].v_type == VAR_STRING)
6066 string_in = argvars[2].vval.v_string;
6067 if (type == VAR_NUMBER)
6068 string_result = NULL;
6069 else
6070 string_result = &rettv->vval.v_string;
6071 if (mch_libcall(argvars[0].vval.v_string,
6072 argvars[1].vval.v_string,
6073 string_in,
6074 argvars[2].vval.v_number,
6075 string_result,
6076 &nr_result) == OK
6077 && type == VAR_NUMBER)
6078 rettv->vval.v_number = nr_result;
6079 }
6080#endif
6081}
6082
6083/*
6084 * "libcall()" function
6085 */
6086 static void
6087f_libcall(typval_T *argvars, typval_T *rettv)
6088{
6089 libcall_common(argvars, rettv, VAR_STRING);
6090}
6091
6092/*
6093 * "libcallnr()" function
6094 */
6095 static void
6096f_libcallnr(typval_T *argvars, typval_T *rettv)
6097{
6098 libcall_common(argvars, rettv, VAR_NUMBER);
6099}
6100
6101/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006102 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103 */
6104 static void
6105f_line(typval_T *argvars, typval_T *rettv)
6106{
6107 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006108 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006110 int id;
6111 tabpage_T *tp;
6112 win_T *wp;
6113 win_T *save_curwin;
6114 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006115
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006116 if (argvars[1].v_type != VAR_UNKNOWN)
6117 {
6118 // use window specified in the second argument
6119 id = (int)tv_get_number(&argvars[1]);
6120 wp = win_id2wp_tp(id, &tp);
6121 if (wp != NULL && tp != NULL)
6122 {
6123 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6124 == OK)
6125 {
6126 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006127 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006128 }
6129 restore_win_noblock(save_curwin, save_curtab, TRUE);
6130 }
6131 }
6132 else
6133 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006134 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006136 if (fp != NULL)
6137 lnum = fp->lnum;
6138 rettv->vval.v_number = lnum;
6139}
6140
6141/*
6142 * "line2byte(lnum)" function
6143 */
6144 static void
6145f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6146{
6147#ifndef FEAT_BYTEOFF
6148 rettv->vval.v_number = -1;
6149#else
6150 linenr_T lnum;
6151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006152 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006153 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6154 rettv->vval.v_number = -1;
6155 else
6156 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6157 if (rettv->vval.v_number >= 0)
6158 ++rettv->vval.v_number;
6159#endif
6160}
6161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006162#ifdef FEAT_LUA
6163/*
6164 * "luaeval()" function
6165 */
6166 static void
6167f_luaeval(typval_T *argvars, typval_T *rettv)
6168{
6169 char_u *str;
6170 char_u buf[NUMBUFLEN];
6171
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006172 if (check_restricted() || check_secure())
6173 return;
6174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006175 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176 do_luaeval(str, argvars + 1, rettv);
6177}
6178#endif
6179
6180/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 * "maparg()" function
6182 */
6183 static void
6184f_maparg(typval_T *argvars, typval_T *rettv)
6185{
6186 get_maparg(argvars, rettv, TRUE);
6187}
6188
6189/*
6190 * "mapcheck()" function
6191 */
6192 static void
6193f_mapcheck(typval_T *argvars, typval_T *rettv)
6194{
6195 get_maparg(argvars, rettv, FALSE);
6196}
6197
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006198typedef enum
6199{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006200 MATCH_END, // matchend()
6201 MATCH_MATCH, // match()
6202 MATCH_STR, // matchstr()
6203 MATCH_LIST, // matchlist()
6204 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006205} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206
6207 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006208find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209{
6210 char_u *str = NULL;
6211 long len = 0;
6212 char_u *expr = NULL;
6213 char_u *pat;
6214 regmatch_T regmatch;
6215 char_u patbuf[NUMBUFLEN];
6216 char_u strbuf[NUMBUFLEN];
6217 char_u *save_cpo;
6218 long start = 0;
6219 long nth = 1;
6220 colnr_T startcol = 0;
6221 int match = 0;
6222 list_T *l = NULL;
6223 listitem_T *li = NULL;
6224 long idx = 0;
6225 char_u *tofree = NULL;
6226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006227 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006229 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230
6231 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006232 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006234 // type MATCH_LIST: return empty list when there are no matches.
6235 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236 if (rettv_list_alloc(rettv) == FAIL)
6237 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006238 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239 && (list_append_string(rettv->vval.v_list,
6240 (char_u *)"", 0) == FAIL
6241 || list_append_number(rettv->vval.v_list,
6242 (varnumber_T)-1) == FAIL
6243 || list_append_number(rettv->vval.v_list,
6244 (varnumber_T)-1) == FAIL
6245 || list_append_number(rettv->vval.v_list,
6246 (varnumber_T)-1) == FAIL))
6247 {
6248 list_free(rettv->vval.v_list);
6249 rettv->vval.v_list = NULL;
6250 goto theend;
6251 }
6252 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006253 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 {
6255 rettv->v_type = VAR_STRING;
6256 rettv->vval.v_string = NULL;
6257 }
6258
6259 if (argvars[0].v_type == VAR_LIST)
6260 {
6261 if ((l = argvars[0].vval.v_list) == NULL)
6262 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006263 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 li = l->lv_first;
6265 }
6266 else
6267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006268 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 len = (long)STRLEN(str);
6270 }
6271
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006272 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006273 if (pat == NULL)
6274 goto theend;
6275
6276 if (argvars[2].v_type != VAR_UNKNOWN)
6277 {
6278 int error = FALSE;
6279
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006280 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 if (error)
6282 goto theend;
6283 if (l != NULL)
6284 {
6285 li = list_find(l, start);
6286 if (li == NULL)
6287 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006288 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 }
6290 else
6291 {
6292 if (start < 0)
6293 start = 0;
6294 if (start > len)
6295 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006296 // When "count" argument is there ignore matches before "start",
6297 // otherwise skip part of the string. Differs when pattern is "^"
6298 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 if (argvars[3].v_type != VAR_UNKNOWN)
6300 startcol = start;
6301 else
6302 {
6303 str += start;
6304 len -= start;
6305 }
6306 }
6307
6308 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006309 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006310 if (error)
6311 goto theend;
6312 }
6313
6314 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6315 if (regmatch.regprog != NULL)
6316 {
6317 regmatch.rm_ic = p_ic;
6318
6319 for (;;)
6320 {
6321 if (l != NULL)
6322 {
6323 if (li == NULL)
6324 {
6325 match = FALSE;
6326 break;
6327 }
6328 vim_free(tofree);
6329 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6330 if (str == NULL)
6331 break;
6332 }
6333
6334 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6335
6336 if (match && --nth <= 0)
6337 break;
6338 if (l == NULL && !match)
6339 break;
6340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006341 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 if (l != NULL)
6343 {
6344 li = li->li_next;
6345 ++idx;
6346 }
6347 else
6348 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006349 startcol = (colnr_T)(regmatch.startp[0]
6350 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351 if (startcol > (colnr_T)len
6352 || str + startcol <= regmatch.startp[0])
6353 {
6354 match = FALSE;
6355 break;
6356 }
6357 }
6358 }
6359
6360 if (match)
6361 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006362 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363 {
6364 listitem_T *li1 = rettv->vval.v_list->lv_first;
6365 listitem_T *li2 = li1->li_next;
6366 listitem_T *li3 = li2->li_next;
6367 listitem_T *li4 = li3->li_next;
6368
6369 vim_free(li1->li_tv.vval.v_string);
6370 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006371 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 li3->li_tv.vval.v_number =
6373 (varnumber_T)(regmatch.startp[0] - expr);
6374 li4->li_tv.vval.v_number =
6375 (varnumber_T)(regmatch.endp[0] - expr);
6376 if (l != NULL)
6377 li2->li_tv.vval.v_number = (varnumber_T)idx;
6378 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006379 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006380 {
6381 int i;
6382
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006383 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384 for (i = 0; i < NSUBEXP; ++i)
6385 {
6386 if (regmatch.endp[i] == NULL)
6387 {
6388 if (list_append_string(rettv->vval.v_list,
6389 (char_u *)"", 0) == FAIL)
6390 break;
6391 }
6392 else if (list_append_string(rettv->vval.v_list,
6393 regmatch.startp[i],
6394 (int)(regmatch.endp[i] - regmatch.startp[i]))
6395 == FAIL)
6396 break;
6397 }
6398 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006399 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006401 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 if (l != NULL)
6403 copy_tv(&li->li_tv, rettv);
6404 else
6405 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006406 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 }
6408 else if (l != NULL)
6409 rettv->vval.v_number = idx;
6410 else
6411 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006412 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413 rettv->vval.v_number =
6414 (varnumber_T)(regmatch.startp[0] - str);
6415 else
6416 rettv->vval.v_number =
6417 (varnumber_T)(regmatch.endp[0] - str);
6418 rettv->vval.v_number += (varnumber_T)(str - expr);
6419 }
6420 }
6421 vim_regfree(regmatch.regprog);
6422 }
6423
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006424theend:
6425 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006426 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427 listitem_remove(rettv->vval.v_list,
6428 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 vim_free(tofree);
6430 p_cpo = save_cpo;
6431}
6432
6433/*
6434 * "match()" function
6435 */
6436 static void
6437f_match(typval_T *argvars, typval_T *rettv)
6438{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006439 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440}
6441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442/*
6443 * "matchend()" function
6444 */
6445 static void
6446f_matchend(typval_T *argvars, typval_T *rettv)
6447{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006448 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449}
6450
6451/*
6452 * "matchlist()" function
6453 */
6454 static void
6455f_matchlist(typval_T *argvars, typval_T *rettv)
6456{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006457 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458}
6459
6460/*
6461 * "matchstr()" function
6462 */
6463 static void
6464f_matchstr(typval_T *argvars, typval_T *rettv)
6465{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006466 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467}
6468
6469/*
6470 * "matchstrpos()" function
6471 */
6472 static void
6473f_matchstrpos(typval_T *argvars, typval_T *rettv)
6474{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006475 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476}
6477
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 static void
6479max_min(typval_T *argvars, typval_T *rettv, int domax)
6480{
6481 varnumber_T n = 0;
6482 varnumber_T i;
6483 int error = FALSE;
6484
6485 if (argvars[0].v_type == VAR_LIST)
6486 {
6487 list_T *l;
6488 listitem_T *li;
6489
6490 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006491 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006493 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006495 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6496 n = l->lv_u.nonmat.lv_start;
6497 else
6498 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6499 * l->lv_u.nonmat.lv_stride;
6500 }
6501 else
6502 {
6503 li = l->lv_first;
6504 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006506 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006507 if (error)
6508 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006509 for (;;)
6510 {
6511 li = li->li_next;
6512 if (li == NULL)
6513 break;
6514 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006515 if (error)
6516 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006517 if (domax ? i > n : i < n)
6518 n = i;
6519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 }
6521 }
6522 }
6523 }
6524 else if (argvars[0].v_type == VAR_DICT)
6525 {
6526 dict_T *d;
6527 int first = TRUE;
6528 hashitem_T *hi;
6529 int todo;
6530
6531 d = argvars[0].vval.v_dict;
6532 if (d != NULL)
6533 {
6534 todo = (int)d->dv_hashtab.ht_used;
6535 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6536 {
6537 if (!HASHITEM_EMPTY(hi))
6538 {
6539 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006540 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006541 if (error)
6542 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543 if (first)
6544 {
6545 n = i;
6546 first = FALSE;
6547 }
6548 else if (domax ? i > n : i < n)
6549 n = i;
6550 }
6551 }
6552 }
6553 }
6554 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006556
6557 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558}
6559
6560/*
6561 * "max()" function
6562 */
6563 static void
6564f_max(typval_T *argvars, typval_T *rettv)
6565{
6566 max_min(argvars, rettv, TRUE);
6567}
6568
6569/*
6570 * "min()" function
6571 */
6572 static void
6573f_min(typval_T *argvars, typval_T *rettv)
6574{
6575 max_min(argvars, rettv, FALSE);
6576}
6577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006578#if defined(FEAT_MZSCHEME) || defined(PROTO)
6579/*
6580 * "mzeval()" function
6581 */
6582 static void
6583f_mzeval(typval_T *argvars, typval_T *rettv)
6584{
6585 char_u *str;
6586 char_u buf[NUMBUFLEN];
6587
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006588 if (check_restricted() || check_secure())
6589 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006590 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 do_mzeval(str, rettv);
6592}
6593
6594 void
6595mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6596{
6597 typval_T argvars[3];
6598
6599 argvars[0].v_type = VAR_STRING;
6600 argvars[0].vval.v_string = name;
6601 copy_tv(args, &argvars[1]);
6602 argvars[2].v_type = VAR_UNKNOWN;
6603 f_call(argvars, rettv);
6604 clear_tv(&argvars[1]);
6605}
6606#endif
6607
6608/*
6609 * "nextnonblank()" function
6610 */
6611 static void
6612f_nextnonblank(typval_T *argvars, typval_T *rettv)
6613{
6614 linenr_T lnum;
6615
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006616 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617 {
6618 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6619 {
6620 lnum = 0;
6621 break;
6622 }
6623 if (*skipwhite(ml_get(lnum)) != NUL)
6624 break;
6625 }
6626 rettv->vval.v_number = lnum;
6627}
6628
6629/*
6630 * "nr2char()" function
6631 */
6632 static void
6633f_nr2char(typval_T *argvars, typval_T *rettv)
6634{
6635 char_u buf[NUMBUFLEN];
6636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 if (has_mbyte)
6638 {
6639 int utf8 = 0;
6640
6641 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006642 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006643 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006644 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006646 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 }
6648 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006649 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006650 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 buf[1] = NUL;
6652 }
6653 rettv->v_type = VAR_STRING;
6654 rettv->vval.v_string = vim_strsave(buf);
6655}
6656
6657/*
6658 * "or(expr, expr)" function
6659 */
6660 static void
6661f_or(typval_T *argvars, typval_T *rettv)
6662{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006663 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6664 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665}
6666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667#ifdef FEAT_PERL
6668/*
6669 * "perleval()" function
6670 */
6671 static void
6672f_perleval(typval_T *argvars, typval_T *rettv)
6673{
6674 char_u *str;
6675 char_u buf[NUMBUFLEN];
6676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006677 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 do_perleval(str, rettv);
6679}
6680#endif
6681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682/*
6683 * "prevnonblank()" function
6684 */
6685 static void
6686f_prevnonblank(typval_T *argvars, typval_T *rettv)
6687{
6688 linenr_T lnum;
6689
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006690 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6692 lnum = 0;
6693 else
6694 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6695 --lnum;
6696 rettv->vval.v_number = lnum;
6697}
6698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006699// This dummy va_list is here because:
6700// - passing a NULL pointer doesn't work when va_list isn't a pointer
6701// - locally in the function results in a "used before set" warning
6702// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703static va_list ap;
6704
6705/*
6706 * "printf()" function
6707 */
6708 static void
6709f_printf(typval_T *argvars, typval_T *rettv)
6710{
6711 char_u buf[NUMBUFLEN];
6712 int len;
6713 char_u *s;
6714 int saved_did_emsg = did_emsg;
6715 char *fmt;
6716
6717 rettv->v_type = VAR_STRING;
6718 rettv->vval.v_string = NULL;
6719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006720 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006722 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006723 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006724 if (!did_emsg)
6725 {
6726 s = alloc(len + 1);
6727 if (s != NULL)
6728 {
6729 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006730 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6731 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 }
6733 }
6734 did_emsg |= saved_did_emsg;
6735}
6736
6737/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006738 * "pum_getpos()" function
6739 */
6740 static void
6741f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6742{
6743 if (rettv_dict_alloc(rettv) != OK)
6744 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006745 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006746}
6747
6748/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749 * "pumvisible()" function
6750 */
6751 static void
6752f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6753{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 if (pum_visible())
6755 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756}
6757
6758#ifdef FEAT_PYTHON3
6759/*
6760 * "py3eval()" function
6761 */
6762 static void
6763f_py3eval(typval_T *argvars, typval_T *rettv)
6764{
6765 char_u *str;
6766 char_u buf[NUMBUFLEN];
6767
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006768 if (check_restricted() || check_secure())
6769 return;
6770
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006771 if (p_pyx == 0)
6772 p_pyx = 3;
6773
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006774 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775 do_py3eval(str, rettv);
6776}
6777#endif
6778
6779#ifdef FEAT_PYTHON
6780/*
6781 * "pyeval()" function
6782 */
6783 static void
6784f_pyeval(typval_T *argvars, typval_T *rettv)
6785{
6786 char_u *str;
6787 char_u buf[NUMBUFLEN];
6788
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006789 if (check_restricted() || check_secure())
6790 return;
6791
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006792 if (p_pyx == 0)
6793 p_pyx = 2;
6794
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006795 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 do_pyeval(str, rettv);
6797}
6798#endif
6799
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006800#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6801/*
6802 * "pyxeval()" function
6803 */
6804 static void
6805f_pyxeval(typval_T *argvars, typval_T *rettv)
6806{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006807 if (check_restricted() || check_secure())
6808 return;
6809
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006810# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6811 init_pyxversion();
6812 if (p_pyx == 2)
6813 f_pyeval(argvars, rettv);
6814 else
6815 f_py3eval(argvars, rettv);
6816# elif defined(FEAT_PYTHON)
6817 f_pyeval(argvars, rettv);
6818# elif defined(FEAT_PYTHON3)
6819 f_py3eval(argvars, rettv);
6820# endif
6821}
6822#endif
6823
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006824static UINT32_T srand_seed_for_testing = 0;
6825static int srand_seed_for_testing_is_used = FALSE;
6826
6827 static void
6828f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6829{
6830 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006831 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006832 else
6833 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006834 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6835 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006836 }
6837}
6838
6839 static void
6840init_srand(UINT32_T *x)
6841{
6842#ifndef MSWIN
6843 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6844#endif
6845
6846 if (srand_seed_for_testing_is_used)
6847 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006848 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006849 return;
6850 }
6851#ifndef MSWIN
6852 if (dev_urandom_state != FAIL)
6853 {
6854 int fd = open("/dev/urandom", O_RDONLY);
6855 struct {
6856 union {
6857 UINT32_T number;
6858 char bytes[sizeof(UINT32_T)];
6859 } contents;
6860 } buf;
6861
6862 // Attempt reading /dev/urandom.
6863 if (fd == -1)
6864 dev_urandom_state = FAIL;
6865 else
6866 {
6867 buf.contents.number = 0;
6868 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6869 != sizeof(UINT32_T))
6870 dev_urandom_state = FAIL;
6871 else
6872 {
6873 dev_urandom_state = OK;
6874 *x = buf.contents.number;
6875 }
6876 close(fd);
6877 }
6878 }
6879 if (dev_urandom_state != OK)
6880 // Reading /dev/urandom doesn't work, fall back to time().
6881#endif
6882 *x = vim_time();
6883}
6884
6885#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6886#define SPLITMIX32(x, z) ( \
6887 z = (x += 0x9e3779b9), \
6888 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6889 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6890 z ^ (z >> 16) \
6891 )
6892#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6893 result = ROTL(y * 5, 7) * 9; \
6894 t = y << 9; \
6895 z ^= x; \
6896 w ^= y; \
6897 y ^= z, x ^= w; \
6898 z ^= t; \
6899 w = ROTL(w, 11);
6900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006902 * "rand()" function
6903 */
6904 static void
6905f_rand(typval_T *argvars, typval_T *rettv)
6906{
6907 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006908 static UINT32_T gx, gy, gz, gw;
6909 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006910 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006911 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006912
6913 if (argvars[0].v_type == VAR_UNKNOWN)
6914 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006915 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006916 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006917 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006918 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006919 init_srand(&x);
6920
6921 gx = SPLITMIX32(x, z);
6922 gy = SPLITMIX32(x, z);
6923 gz = SPLITMIX32(x, z);
6924 gw = SPLITMIX32(x, z);
6925 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006926 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006927
6928 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006929 }
6930 else if (argvars[0].v_type == VAR_LIST)
6931 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006932 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006933 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006934 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006935
6936 lx = list_find(l, 0L);
6937 ly = list_find(l, 1L);
6938 lz = list_find(l, 2L);
6939 lw = list_find(l, 3L);
6940 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6941 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6942 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6943 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6944 x = (UINT32_T)lx->li_tv.vval.v_number;
6945 y = (UINT32_T)ly->li_tv.vval.v_number;
6946 z = (UINT32_T)lz->li_tv.vval.v_number;
6947 w = (UINT32_T)lw->li_tv.vval.v_number;
6948
6949 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6950
6951 lx->li_tv.vval.v_number = (varnumber_T)x;
6952 ly->li_tv.vval.v_number = (varnumber_T)y;
6953 lz->li_tv.vval.v_number = (varnumber_T)z;
6954 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006955 }
6956 else
6957 goto theend;
6958
6959 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006960 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006961 return;
6962
6963theend:
6964 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006965 rettv->v_type = VAR_NUMBER;
6966 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006967}
6968
6969/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006970 * "srand()" function
6971 */
6972 static void
6973f_srand(typval_T *argvars, typval_T *rettv)
6974{
6975 UINT32_T x = 0, z;
6976
6977 if (rettv_list_alloc(rettv) == FAIL)
6978 return;
6979 if (argvars[0].v_type == VAR_UNKNOWN)
6980 {
6981 init_srand(&x);
6982 }
6983 else
6984 {
6985 int error = FALSE;
6986
6987 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6988 if (error)
6989 return;
6990 }
6991
6992 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6993 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6994 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6995 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6996}
6997
6998#undef ROTL
6999#undef SPLITMIX32
7000#undef SHUFFLE_XOSHIRO128STARSTAR
7001
7002/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003 * "range()" function
7004 */
7005 static void
7006f_range(typval_T *argvars, typval_T *rettv)
7007{
7008 varnumber_T start;
7009 varnumber_T end;
7010 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011 int error = FALSE;
7012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007013 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014 if (argvars[1].v_type == VAR_UNKNOWN)
7015 {
7016 end = start - 1;
7017 start = 0;
7018 }
7019 else
7020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007021 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007023 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 }
7025
7026 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007027 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007029 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007031 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007032 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 list_T *list = rettv->vval.v_list;
7035
7036 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007037 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007038 // be called.
7039 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007040 list->lv_u.nonmat.lv_start = start;
7041 list->lv_u.nonmat.lv_end = end;
7042 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007043 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007044 }
7045}
7046
7047/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007048 * Materialize "list".
7049 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007050 */
7051 void
7052range_list_materialize(list_T *list)
7053{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007054 varnumber_T start = list->lv_u.nonmat.lv_start;
7055 varnumber_T end = list->lv_u.nonmat.lv_end;
7056 int stride = list->lv_u.nonmat.lv_stride;
7057 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007059 list->lv_first = NULL;
7060 list->lv_u.mat.lv_last = NULL;
7061 list->lv_len = 0;
7062 list->lv_u.mat.lv_idx_item = NULL;
7063 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7064 if (list_append_number(list, (varnumber_T)i) == FAIL)
7065 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066}
7067
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007068/*
7069 * "getreginfo()" function
7070 */
7071 static void
7072f_getreginfo(typval_T *argvars, typval_T *rettv)
7073{
7074 char_u *strregname;
7075 int regname;
7076 char_u buf[NUMBUFLEN + 2];
7077 long reglen = 0;
7078 dict_T *dict;
7079 list_T *list;
7080
7081 if (argvars[0].v_type != VAR_UNKNOWN)
7082 {
7083 strregname = tv_get_string_chk(&argvars[0]);
7084 if (strregname == NULL)
7085 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007086 if (in_vim9script() && STRLEN(strregname) > 1)
7087 {
7088 semsg(_(e_register_name_must_be_one_char_str), strregname);
7089 return;
7090 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007091 }
7092 else
7093 strregname = get_vim_var_str(VV_REG);
7094
7095 regname = (strregname == NULL ? '"' : *strregname);
7096 if (regname == 0 || regname == '@')
7097 regname = '"';
7098
7099 if (rettv_dict_alloc(rettv) == FAIL)
7100 return;
7101 dict = rettv->vval.v_dict;
7102
7103 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7104 if (list == NULL)
7105 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007106 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007107
7108 buf[0] = NUL;
7109 buf[1] = NUL;
7110 switch (get_reg_type(regname, &reglen))
7111 {
7112 case MLINE: buf[0] = 'V'; break;
7113 case MCHAR: buf[0] = 'v'; break;
7114 case MBLOCK:
7115 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7116 reglen + 1);
7117 break;
7118 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007119 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007120
7121 buf[0] = get_register_name(get_unname_register());
7122 buf[1] = NUL;
7123 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007124 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007125 else
7126 {
7127 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7128
7129 if (item != NULL)
7130 {
7131 item->di_tv.v_type = VAR_SPECIAL;
7132 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007133 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007134 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007135 }
7136 }
7137}
7138
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007139 static void
7140return_register(int regname, typval_T *rettv)
7141{
7142 char_u buf[2] = {0, 0};
7143
7144 buf[0] = (char_u)regname;
7145 rettv->v_type = VAR_STRING;
7146 rettv->vval.v_string = vim_strsave(buf);
7147}
7148
7149/*
7150 * "reg_executing()" function
7151 */
7152 static void
7153f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7154{
7155 return_register(reg_executing, rettv);
7156}
7157
7158/*
7159 * "reg_recording()" function
7160 */
7161 static void
7162f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7163{
7164 return_register(reg_recording, rettv);
7165}
7166
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007167/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 * "rename({from}, {to})" function
7169 */
7170 static void
7171f_rename(typval_T *argvars, typval_T *rettv)
7172{
7173 char_u buf[NUMBUFLEN];
7174
7175 if (check_restricted() || check_secure())
7176 rettv->vval.v_number = -1;
7177 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007178 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7179 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007180}
7181
7182/*
7183 * "repeat()" function
7184 */
7185 static void
7186f_repeat(typval_T *argvars, typval_T *rettv)
7187{
7188 char_u *p;
7189 int n;
7190 int slen;
7191 int len;
7192 char_u *r;
7193 int i;
7194
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007195 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 if (argvars[0].v_type == VAR_LIST)
7197 {
7198 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7199 while (n-- > 0)
7200 if (list_extend(rettv->vval.v_list,
7201 argvars[0].vval.v_list, NULL) == FAIL)
7202 break;
7203 }
7204 else
7205 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 rettv->v_type = VAR_STRING;
7208 rettv->vval.v_string = NULL;
7209
7210 slen = (int)STRLEN(p);
7211 len = slen * n;
7212 if (len <= 0)
7213 return;
7214
7215 r = alloc(len + 1);
7216 if (r != NULL)
7217 {
7218 for (i = 0; i < n; i++)
7219 mch_memmove(r + i * slen, p, (size_t)slen);
7220 r[len] = NUL;
7221 }
7222
7223 rettv->vval.v_string = r;
7224 }
7225}
7226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007227#define SP_NOMOVE 0x01 // don't move cursor
7228#define SP_REPEAT 0x02 // repeat to find outer pair
7229#define SP_RETCOUNT 0x04 // return matchcount
7230#define SP_SETPCMARK 0x08 // set previous context mark
7231#define SP_START 0x10 // accept match at start position
7232#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7233#define SP_END 0x40 // leave cursor at end of match
7234#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007236/*
7237 * Get flags for a search function.
7238 * Possibly sets "p_ws".
7239 * Returns BACKWARD, FORWARD or zero (for an error).
7240 */
7241 static int
7242get_search_arg(typval_T *varp, int *flagsp)
7243{
7244 int dir = FORWARD;
7245 char_u *flags;
7246 char_u nbuf[NUMBUFLEN];
7247 int mask;
7248
7249 if (varp->v_type != VAR_UNKNOWN)
7250 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007251 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007253 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 while (*flags != NUL)
7255 {
7256 switch (*flags)
7257 {
7258 case 'b': dir = BACKWARD; break;
7259 case 'w': p_ws = TRUE; break;
7260 case 'W': p_ws = FALSE; break;
7261 default: mask = 0;
7262 if (flagsp != NULL)
7263 switch (*flags)
7264 {
7265 case 'c': mask = SP_START; break;
7266 case 'e': mask = SP_END; break;
7267 case 'm': mask = SP_RETCOUNT; break;
7268 case 'n': mask = SP_NOMOVE; break;
7269 case 'p': mask = SP_SUBPAT; break;
7270 case 'r': mask = SP_REPEAT; break;
7271 case 's': mask = SP_SETPCMARK; break;
7272 case 'z': mask = SP_COLUMN; break;
7273 }
7274 if (mask == 0)
7275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007276 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007277 dir = 0;
7278 }
7279 else
7280 *flagsp |= mask;
7281 }
7282 if (dir == 0)
7283 break;
7284 ++flags;
7285 }
7286 }
7287 return dir;
7288}
7289
7290/*
7291 * Shared by search() and searchpos() functions.
7292 */
7293 static int
7294search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7295{
7296 int flags;
7297 char_u *pat;
7298 pos_T pos;
7299 pos_T save_cursor;
7300 int save_p_ws = p_ws;
7301 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007302 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007305 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 long time_limit = 0;
7307#endif
7308 int options = SEARCH_KEEP;
7309 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007310 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007311 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007312 pos_T firstpos;
7313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007314 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007315 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007316 if (dir == 0)
7317 goto theend;
7318 flags = *flagsp;
7319 if (flags & SP_START)
7320 options |= SEARCH_START;
7321 if (flags & SP_END)
7322 options |= SEARCH_END;
7323 if (flags & SP_COLUMN)
7324 options |= SEARCH_COL;
7325
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007326 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7328 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007329 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 if (lnum_stop < 0)
7331 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 if (argvars[3].v_type != VAR_UNKNOWN)
7333 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007334#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007335 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 if (time_limit < 0)
7337 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007339 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 }
7342
7343#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007344 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 profile_setlimit(time_limit, &tm);
7346#endif
7347
7348 /*
7349 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7350 * Check to make sure only those flags are set.
7351 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7352 * flags cannot be set. Check for that condition also.
7353 */
7354 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7355 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 goto theend;
7359 }
7360
7361 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007362 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007363 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007364 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7365#ifdef FEAT_RELTIME
7366 sia.sa_tm = &tm;
7367#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007368
7369 // Repeat until {skip} returns FALSE.
7370 for (;;)
7371 {
7372 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007373 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007374 // finding the first match again means there is no match where {skip}
7375 // evaluates to zero.
7376 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7377 subpatnum = FAIL;
7378
Bram Moolenaara9c01042020-06-07 14:50:50 +02007379 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007380 // didn't find it or no skip argument
7381 break;
7382 firstpos = pos;
7383
Bram Moolenaara9c01042020-06-07 14:50:50 +02007384 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007385 {
7386 int do_skip;
7387 int err;
7388 pos_T save_pos = curwin->w_cursor;
7389
7390 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007391 err = FALSE;
7392 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007393 curwin->w_cursor = save_pos;
7394 if (err)
7395 {
7396 // Evaluating {skip} caused an error, break here.
7397 subpatnum = FAIL;
7398 break;
7399 }
7400 if (!do_skip)
7401 break;
7402 }
7403 }
7404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007405 if (subpatnum != FAIL)
7406 {
7407 if (flags & SP_SUBPAT)
7408 retval = subpatnum;
7409 else
7410 retval = pos.lnum;
7411 if (flags & SP_SETPCMARK)
7412 setpcmark();
7413 curwin->w_cursor = pos;
7414 if (match_pos != NULL)
7415 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007416 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 match_pos->lnum = pos.lnum;
7418 match_pos->col = pos.col + 1;
7419 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007420 // "/$" will put the cursor after the end of the line, may need to
7421 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422 check_cursor();
7423 }
7424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007425 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 if (flags & SP_NOMOVE)
7427 curwin->w_cursor = save_cursor;
7428 else
7429 curwin->w_set_curswant = TRUE;
7430theend:
7431 p_ws = save_p_ws;
7432
7433 return retval;
7434}
7435
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007436#ifdef FEAT_RUBY
7437/*
7438 * "rubyeval()" function
7439 */
7440 static void
7441f_rubyeval(typval_T *argvars, typval_T *rettv)
7442{
7443 char_u *str;
7444 char_u buf[NUMBUFLEN];
7445
7446 str = tv_get_string_buf(&argvars[0], buf);
7447 do_rubyeval(str, rettv);
7448}
7449#endif
7450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451/*
7452 * "screenattr()" function
7453 */
7454 static void
7455f_screenattr(typval_T *argvars, typval_T *rettv)
7456{
7457 int row;
7458 int col;
7459 int c;
7460
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007461 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7462 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007463 if (row < 0 || row >= screen_Rows
7464 || col < 0 || col >= screen_Columns)
7465 c = -1;
7466 else
7467 c = ScreenAttrs[LineOffset[row] + col];
7468 rettv->vval.v_number = c;
7469}
7470
7471/*
7472 * "screenchar()" function
7473 */
7474 static void
7475f_screenchar(typval_T *argvars, typval_T *rettv)
7476{
7477 int row;
7478 int col;
7479 int off;
7480 int c;
7481
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007482 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7483 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007484 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 c = -1;
7486 else
7487 {
7488 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007489 if (enc_utf8 && ScreenLinesUC[off] != 0)
7490 c = ScreenLinesUC[off];
7491 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007492 c = ScreenLines[off];
7493 }
7494 rettv->vval.v_number = c;
7495}
7496
7497/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007498 * "screenchars()" function
7499 */
7500 static void
7501f_screenchars(typval_T *argvars, typval_T *rettv)
7502{
7503 int row;
7504 int col;
7505 int off;
7506 int c;
7507 int i;
7508
7509 if (rettv_list_alloc(rettv) == FAIL)
7510 return;
7511 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7512 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7513 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7514 return;
7515
7516 off = LineOffset[row] + col;
7517 if (enc_utf8 && ScreenLinesUC[off] != 0)
7518 c = ScreenLinesUC[off];
7519 else
7520 c = ScreenLines[off];
7521 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7522
7523 if (enc_utf8)
7524
7525 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7526 list_append_number(rettv->vval.v_list,
7527 (varnumber_T)ScreenLinesC[i][off]);
7528}
7529
7530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 * "screencol()" function
7532 *
7533 * First column is 1 to be consistent with virtcol().
7534 */
7535 static void
7536f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7537{
7538 rettv->vval.v_number = screen_screencol() + 1;
7539}
7540
7541/*
7542 * "screenrow()" function
7543 */
7544 static void
7545f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7546{
7547 rettv->vval.v_number = screen_screenrow() + 1;
7548}
7549
7550/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007551 * "screenstring()" function
7552 */
7553 static void
7554f_screenstring(typval_T *argvars, typval_T *rettv)
7555{
7556 int row;
7557 int col;
7558 int off;
7559 int c;
7560 int i;
7561 char_u buf[MB_MAXBYTES + 1];
7562 int buflen = 0;
7563
7564 rettv->vval.v_string = NULL;
7565 rettv->v_type = VAR_STRING;
7566
7567 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7568 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7569 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7570 return;
7571
7572 off = LineOffset[row] + col;
7573 if (enc_utf8 && ScreenLinesUC[off] != 0)
7574 c = ScreenLinesUC[off];
7575 else
7576 c = ScreenLines[off];
7577 buflen += mb_char2bytes(c, buf);
7578
Bram Moolenaarf1387282021-03-22 17:11:15 +01007579 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007580 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7581 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7582
7583 buf[buflen] = NUL;
7584 rettv->vval.v_string = vim_strsave(buf);
7585}
7586
7587/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007588 * "search()" function
7589 */
7590 static void
7591f_search(typval_T *argvars, typval_T *rettv)
7592{
7593 int flags = 0;
7594
7595 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7596}
7597
7598/*
7599 * "searchdecl()" function
7600 */
7601 static void
7602f_searchdecl(typval_T *argvars, typval_T *rettv)
7603{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007604 int locally = TRUE;
7605 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 int error = FALSE;
7607 char_u *name;
7608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007609 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 if (argvars[1].v_type != VAR_UNKNOWN)
7613 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007614 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007615 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007616 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617 }
7618 if (!error && name != NULL)
7619 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7620 locally, thisblock, SEARCH_KEEP) == FAIL;
7621}
7622
7623/*
7624 * Used by searchpair() and searchpairpos()
7625 */
7626 static int
7627searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7628{
7629 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007630 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631 int save_p_ws = p_ws;
7632 int dir;
7633 int flags = 0;
7634 char_u nbuf1[NUMBUFLEN];
7635 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007636 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637 long lnum_stop = 0;
7638 long time_limit = 0;
7639
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007640 // Get the three pattern arguments: start, middle, end. Will result in an
7641 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007642 spat = tv_get_string_chk(&argvars[0]);
7643 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7644 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007646 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007648 // Handle the optional fourth argument: flags
7649 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 if (dir == 0)
7651 goto theend;
7652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007653 // Don't accept SP_END or SP_SUBPAT.
7654 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 if ((flags & (SP_END | SP_SUBPAT)) != 0
7656 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007658 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 goto theend;
7660 }
7661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007662 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663 if (flags & SP_REPEAT)
7664 p_ws = FALSE;
7665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007666 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 if (argvars[3].v_type == VAR_UNKNOWN
7668 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007669 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 else
7671 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007672 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007673 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 if (argvars[5].v_type != VAR_UNKNOWN)
7676 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007677 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007682 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683#ifdef FEAT_RELTIME
7684 if (argvars[6].v_type != VAR_UNKNOWN)
7685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007686 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007689 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007691 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 }
7693#endif
7694 }
7695 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696
7697 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7698 match_pos, lnum_stop, time_limit);
7699
7700theend:
7701 p_ws = save_p_ws;
7702
7703 return retval;
7704}
7705
7706/*
7707 * "searchpair()" function
7708 */
7709 static void
7710f_searchpair(typval_T *argvars, typval_T *rettv)
7711{
7712 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7713}
7714
7715/*
7716 * "searchpairpos()" function
7717 */
7718 static void
7719f_searchpairpos(typval_T *argvars, typval_T *rettv)
7720{
7721 pos_T match_pos;
7722 int lnum = 0;
7723 int col = 0;
7724
7725 if (rettv_list_alloc(rettv) == FAIL)
7726 return;
7727
7728 if (searchpair_cmn(argvars, &match_pos) > 0)
7729 {
7730 lnum = match_pos.lnum;
7731 col = match_pos.col;
7732 }
7733
7734 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7735 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7736}
7737
7738/*
7739 * Search for a start/middle/end thing.
7740 * Used by searchpair(), see its documentation for the details.
7741 * Returns 0 or -1 for no match,
7742 */
7743 long
7744do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007745 char_u *spat, // start pattern
7746 char_u *mpat, // middle pattern
7747 char_u *epat, // end pattern
7748 int dir, // BACKWARD or FORWARD
7749 typval_T *skip, // skip expression
7750 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007752 linenr_T lnum_stop, // stop at this line if not zero
7753 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754{
7755 char_u *save_cpo;
7756 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7757 long retval = 0;
7758 pos_T pos;
7759 pos_T firstpos;
7760 pos_T foundpos;
7761 pos_T save_cursor;
7762 pos_T save_pos;
7763 int n;
7764 int r;
7765 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007766 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 int err;
7768 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007769#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007773 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007774 save_cpo = p_cpo;
7775 p_cpo = empty_option;
7776
7777#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007778 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 profile_setlimit(time_limit, &tm);
7780#endif
7781
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007782 // Make two search patterns: start/end (pat2, for in nested pairs) and
7783 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007784 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7785 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 if (pat2 == NULL || pat3 == NULL)
7787 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007788 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007789 if (*mpat == NUL)
7790 STRCPY(pat3, pat2);
7791 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007792 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793 spat, epat, mpat);
7794 if (flags & SP_START)
7795 options |= SEARCH_START;
7796
Bram Moolenaar48570482017-10-30 21:48:41 +01007797 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007798 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 save_cursor = curwin->w_cursor;
7801 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007802 CLEAR_POS(&firstpos);
7803 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 pat = pat3;
7805 for (;;)
7806 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007807 searchit_arg_T sia;
7808
Bram Moolenaara80faa82020-04-12 19:37:17 +02007809 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007810 sia.sa_stop_lnum = lnum_stop;
7811#ifdef FEAT_RELTIME
7812 sia.sa_tm = &tm;
7813#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007814 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007815 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007816 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007817 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818 break;
7819
7820 if (firstpos.lnum == 0)
7821 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007822 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007824 // Found the same position again. Can happen with a pattern that
7825 // has "\zs" at the end and searching backwards. Advance one
7826 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827 if (dir == BACKWARD)
7828 decl(&pos);
7829 else
7830 incl(&pos);
7831 }
7832 foundpos = pos;
7833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007834 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 options &= ~SEARCH_START;
7836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007837 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007838 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 {
7840 save_pos = curwin->w_cursor;
7841 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007842 err = FALSE;
7843 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844 curwin->w_cursor = save_pos;
7845 if (err)
7846 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007847 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 curwin->w_cursor = save_cursor;
7849 retval = -1;
7850 break;
7851 }
7852 if (r)
7853 continue;
7854 }
7855
7856 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007858 // Found end when searching backwards or start when searching
7859 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007861 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 }
7863 else
7864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007865 // Found end when searching forward or start when searching
7866 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007868 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 }
7870
7871 if (nest == 0)
7872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007873 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 if (flags & SP_RETCOUNT)
7875 ++retval;
7876 else
7877 retval = pos.lnum;
7878 if (flags & SP_SETPCMARK)
7879 setpcmark();
7880 curwin->w_cursor = pos;
7881 if (!(flags & SP_REPEAT))
7882 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007883 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 }
7885 }
7886
7887 if (match_pos != NULL)
7888 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007889 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 match_pos->lnum = curwin->w_cursor.lnum;
7891 match_pos->col = curwin->w_cursor.col + 1;
7892 }
7893
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007894 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 if ((flags & SP_NOMOVE) || retval == 0)
7896 curwin->w_cursor = save_cursor;
7897
7898theend:
7899 vim_free(pat2);
7900 vim_free(pat3);
7901 if (p_cpo == empty_option)
7902 p_cpo = save_cpo;
7903 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007905 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007906 // If it's still empty it was changed and restored, need to restore in
7907 // the complicated way.
7908 if (*p_cpo == NUL)
7909 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007911 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912
7913 return retval;
7914}
7915
7916/*
7917 * "searchpos()" function
7918 */
7919 static void
7920f_searchpos(typval_T *argvars, typval_T *rettv)
7921{
7922 pos_T match_pos;
7923 int lnum = 0;
7924 int col = 0;
7925 int n;
7926 int flags = 0;
7927
7928 if (rettv_list_alloc(rettv) == FAIL)
7929 return;
7930
7931 n = search_cmn(argvars, &match_pos, &flags);
7932 if (n > 0)
7933 {
7934 lnum = match_pos.lnum;
7935 col = match_pos.col;
7936 }
7937
7938 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7939 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7940 if (flags & SP_SUBPAT)
7941 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7942}
7943
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007944/*
7945 * Set the cursor or mark position.
7946 * If 'charpos' is TRUE, then use the column number as a character offet.
7947 * Otherwise use the column number as a byte offset.
7948 */
7949 static void
7950set_position(typval_T *argvars, typval_T *rettv, int charpos)
7951{
7952 pos_T pos;
7953 int fnum;
7954 char_u *name;
7955 colnr_T curswant = -1;
7956
7957 rettv->vval.v_number = -1;
7958
7959 name = tv_get_string_chk(argvars);
7960 if (name != NULL)
7961 {
7962 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7963 {
7964 if (pos.col != MAXCOL && --pos.col < 0)
7965 pos.col = 0;
7966 if ((name[0] == '.' && name[1] == NUL))
7967 {
7968 // set cursor; "fnum" is ignored
7969 curwin->w_cursor = pos;
7970 if (curswant >= 0)
7971 {
7972 curwin->w_curswant = curswant - 1;
7973 curwin->w_set_curswant = FALSE;
7974 }
7975 check_cursor();
7976 rettv->vval.v_number = 0;
7977 }
7978 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7979 {
7980 // set mark
7981 if (setmark_pos(name[1], &pos, fnum) == OK)
7982 rettv->vval.v_number = 0;
7983 }
7984 else
7985 emsg(_(e_invarg));
7986 }
7987 }
7988}
7989/*
7990 * "setcharpos()" function
7991 */
7992 static void
7993f_setcharpos(typval_T *argvars, typval_T *rettv)
7994{
7995 set_position(argvars, rettv, TRUE);
7996}
7997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8000{
8001 dict_T *d;
8002 dictitem_T *di;
8003 char_u *csearch;
8004
8005 if (argvars[0].v_type != VAR_DICT)
8006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 return;
8009 }
8010
8011 if ((d = argvars[0].vval.v_dict) != NULL)
8012 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008013 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 if (csearch != NULL)
8015 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 if (enc_utf8)
8017 {
8018 int pcc[MAX_MCO];
8019 int c = utfc_ptr2char(csearch, pcc);
8020
8021 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8022 }
8023 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008025 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 }
8027
8028 di = dict_find(d, (char_u *)"forward", -1);
8029 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008030 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008031 ? FORWARD : BACKWARD);
8032
8033 di = dict_find(d, (char_u *)"until", -1);
8034 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008035 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 }
8037}
8038
8039/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008040 * "setcursorcharpos" function
8041 */
8042 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008043f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008044{
8045 set_cursorpos(argvars, rettv, TRUE);
8046}
8047
8048/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008049 * "setenv()" function
8050 */
8051 static void
8052f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8053{
8054 char_u namebuf[NUMBUFLEN];
8055 char_u valbuf[NUMBUFLEN];
8056 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8057
8058 if (argvars[1].v_type == VAR_SPECIAL
8059 && argvars[1].vval.v_number == VVAL_NULL)
8060 vim_unsetenv(name);
8061 else
8062 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8063}
8064
8065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 * "setfperm({fname}, {mode})" function
8067 */
8068 static void
8069f_setfperm(typval_T *argvars, typval_T *rettv)
8070{
8071 char_u *fname;
8072 char_u modebuf[NUMBUFLEN];
8073 char_u *mode_str;
8074 int i;
8075 int mask;
8076 int mode = 0;
8077
8078 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008079 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008080 if (fname == NULL)
8081 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008082 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083 if (mode_str == NULL)
8084 return;
8085 if (STRLEN(mode_str) != 9)
8086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008087 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 return;
8089 }
8090
8091 mask = 1;
8092 for (i = 8; i >= 0; --i)
8093 {
8094 if (mode_str[i] != '-')
8095 mode |= mask;
8096 mask = mask << 1;
8097 }
8098 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8099}
8100
8101/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008102 * "setpos()" function
8103 */
8104 static void
8105f_setpos(typval_T *argvars, typval_T *rettv)
8106{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008107 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108}
8109
8110/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008111 * Translate a register type string to the yank type and block length
8112 */
8113 static int
8114get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8115{
8116 char_u *stropt = *pp;
8117 switch (*stropt)
8118 {
8119 case 'v': case 'c': // character-wise selection
8120 *yank_type = MCHAR;
8121 break;
8122 case 'V': case 'l': // line-wise selection
8123 *yank_type = MLINE;
8124 break;
8125 case 'b': case Ctrl_V: // block-wise selection
8126 *yank_type = MBLOCK;
8127 if (VIM_ISDIGIT(stropt[1]))
8128 {
8129 ++stropt;
8130 *block_len = getdigits(&stropt) - 1;
8131 --stropt;
8132 }
8133 break;
8134 default:
8135 return FAIL;
8136 }
8137 *pp = stropt;
8138 return OK;
8139}
8140
8141/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 * "setreg()" function
8143 */
8144 static void
8145f_setreg(typval_T *argvars, typval_T *rettv)
8146{
8147 int regname;
8148 char_u *strregname;
8149 char_u *stropt;
8150 char_u *strval;
8151 int append;
8152 char_u yank_type;
8153 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008154 typval_T *regcontents;
8155 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008157 pointreg = 0;
8158 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159 block_len = -1;
8160 yank_type = MAUTO;
8161 append = FALSE;
8162
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008163 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008164 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165
8166 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008167 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008168 if (in_vim9script() && STRLEN(strregname) > 1)
8169 {
8170 semsg(_(e_register_name_must_be_one_char_str), strregname);
8171 return;
8172 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 regname = *strregname;
8174 if (regname == 0 || regname == '@')
8175 regname = '"';
8176
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008177 if (argvars[1].v_type == VAR_DICT)
8178 {
8179 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008180 dictitem_T *di;
8181
8182 if (d == NULL || d->dv_hashtab.ht_used == 0)
8183 {
8184 // Empty dict, clear the register (like setreg(0, []))
8185 char_u *lstval[2] = {NULL, NULL};
8186 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8187 return;
8188 }
8189
8190 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008191 if (di != NULL)
8192 regcontents = &di->di_tv;
8193
8194 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8195 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008196 {
8197 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8198
8199 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008200 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008201 semsg(_(e_invargval), "value");
8202 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008203 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008204 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008205
8206 if (regname == '"')
8207 {
8208 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8209 if (stropt != NULL)
8210 {
8211 pointreg = *stropt;
8212 regname = pointreg;
8213 }
8214 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008215 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008216 pointreg = regname;
8217 }
8218 else
8219 regcontents = &argvars[1];
8220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 if (argvars[2].v_type != VAR_UNKNOWN)
8222 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008223 if (yank_type != MAUTO)
8224 {
8225 semsg(_(e_toomanyarg), "setreg");
8226 return;
8227 }
8228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008229 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008231 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008232 for (; *stropt != NUL; ++stropt)
8233 switch (*stropt)
8234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008235 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 append = TRUE;
8237 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008238 default:
8239 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008240 }
8241 }
8242
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008243 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 {
8245 char_u **lstval;
8246 char_u **allocval;
8247 char_u buf[NUMBUFLEN];
8248 char_u **curval;
8249 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008250 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251 listitem_T *li;
8252 int len;
8253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008254 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255 len = ll == NULL ? 0 : ll->lv_len;
8256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008257 // First half: use for pointers to result lines; second half: use for
8258 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008259 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 if (lstval == NULL)
8261 return;
8262 curval = lstval;
8263 allocval = lstval + len + 2;
8264 curallocval = allocval;
8265
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008266 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008268 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008269 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008271 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 if (strval == NULL)
8273 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008274 if (strval == buf)
8275 {
8276 // Need to make a copy, next tv_get_string_buf_chk() will
8277 // overwrite the string.
8278 strval = vim_strsave(buf);
8279 if (strval == NULL)
8280 goto free_lstval;
8281 *curallocval++ = strval;
8282 }
8283 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 }
8286 *curval++ = NULL;
8287
8288 write_reg_contents_lst(regname, lstval, -1,
8289 append, yank_type, block_len);
8290free_lstval:
8291 while (curallocval > allocval)
8292 vim_free(*--curallocval);
8293 vim_free(lstval);
8294 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008295 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008297 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 if (strval == NULL)
8299 return;
8300 write_reg_contents_ex(regname, strval, -1,
8301 append, yank_type, block_len);
8302 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008303 if (pointreg != 0)
8304 get_yank_register(pointreg, TRUE);
8305
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 rettv->vval.v_number = 0;
8307}
8308
8309/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008310 * "settagstack()" function
8311 */
8312 static void
8313f_settagstack(typval_T *argvars, typval_T *rettv)
8314{
8315 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8316 win_T *wp;
8317 dict_T *d;
8318 int action = 'r';
8319
8320 rettv->vval.v_number = -1;
8321
8322 // first argument: window number or id
8323 wp = find_win_by_nr_or_id(&argvars[0]);
8324 if (wp == NULL)
8325 return;
8326
8327 // second argument: dict with items to set in the tag stack
8328 if (argvars[1].v_type != VAR_DICT)
8329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008330 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008331 return;
8332 }
8333 d = argvars[1].vval.v_dict;
8334 if (d == NULL)
8335 return;
8336
8337 // third argument: action - 'a' for append and 'r' for replace.
8338 // default is to replace the stack.
8339 if (argvars[2].v_type == VAR_UNKNOWN)
8340 action = 'r';
8341 else if (argvars[2].v_type == VAR_STRING)
8342 {
8343 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008344 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008345 if (actstr == NULL)
8346 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008347 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8348 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008349 action = *actstr;
8350 else
8351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008352 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008353 return;
8354 }
8355 }
8356 else
8357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008358 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008359 return;
8360 }
8361
8362 if (set_tagstack(wp, d, action) == OK)
8363 rettv->vval.v_number = 0;
8364}
8365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366#ifdef FEAT_CRYPT
8367/*
8368 * "sha256({string})" function
8369 */
8370 static void
8371f_sha256(typval_T *argvars, typval_T *rettv)
8372{
8373 char_u *p;
8374
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008375 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 rettv->vval.v_string = vim_strsave(
8377 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8378 rettv->v_type = VAR_STRING;
8379}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008380#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381
8382/*
8383 * "shellescape({string})" function
8384 */
8385 static void
8386f_shellescape(typval_T *argvars, typval_T *rettv)
8387{
Bram Moolenaar20615522017-06-05 18:46:26 +02008388 int do_special = non_zero_arg(&argvars[1]);
8389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008391 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 rettv->v_type = VAR_STRING;
8393}
8394
8395/*
8396 * shiftwidth() function
8397 */
8398 static void
8399f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8400{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008401 rettv->vval.v_number = 0;
8402
8403 if (argvars[0].v_type != VAR_UNKNOWN)
8404 {
8405 long col;
8406
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008407 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008408 if (col < 0)
8409 return; // type error; errmsg already given
8410#ifdef FEAT_VARTABS
8411 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8412 return;
8413#endif
8414 }
8415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 rettv->vval.v_number = get_sw_value(curbuf);
8417}
8418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419/*
8420 * "soundfold({word})" function
8421 */
8422 static void
8423f_soundfold(typval_T *argvars, typval_T *rettv)
8424{
8425 char_u *s;
8426
8427 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008428 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008429#ifdef FEAT_SPELL
8430 rettv->vval.v_string = eval_soundfold(s);
8431#else
8432 rettv->vval.v_string = vim_strsave(s);
8433#endif
8434}
8435
8436/*
8437 * "spellbadword()" function
8438 */
8439 static void
8440f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8441{
8442 char_u *word = (char_u *)"";
8443 hlf_T attr = HLF_COUNT;
8444 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008445#ifdef FEAT_SPELL
8446 int wo_spell_save = curwin->w_p_spell;
8447
8448 if (!curwin->w_p_spell)
8449 {
8450 did_set_spelllang(curwin);
8451 curwin->w_p_spell = TRUE;
8452 }
8453
8454 if (*curwin->w_s->b_p_spl == NUL)
8455 {
8456 emsg(_(e_no_spell));
8457 curwin->w_p_spell = wo_spell_save;
8458 return;
8459 }
8460#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461
8462 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008463 {
8464#ifdef FEAT_SPELL
8465 curwin->w_p_spell = wo_spell_save;
8466#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008468 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469
8470#ifdef FEAT_SPELL
8471 if (argvars[0].v_type == VAR_UNKNOWN)
8472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008473 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8475 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008476 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008478 curwin->w_set_curswant = TRUE;
8479 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008481 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008483 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008484 int capcol = -1;
8485
8486 if (str != NULL)
8487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008488 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 while (*str != NUL)
8490 {
8491 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8492 if (attr != HLF_COUNT)
8493 {
8494 word = str;
8495 break;
8496 }
8497 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008498 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008499 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 }
8501 }
8502 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008503 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504#endif
8505
8506 list_append_string(rettv->vval.v_list, word, len);
8507 list_append_string(rettv->vval.v_list, (char_u *)(
8508 attr == HLF_SPB ? "bad" :
8509 attr == HLF_SPR ? "rare" :
8510 attr == HLF_SPL ? "local" :
8511 attr == HLF_SPC ? "caps" :
8512 ""), -1);
8513}
8514
8515/*
8516 * "spellsuggest()" function
8517 */
8518 static void
8519f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8520{
8521#ifdef FEAT_SPELL
8522 char_u *str;
8523 int typeerr = FALSE;
8524 int maxcount;
8525 garray_T ga;
8526 int i;
8527 listitem_T *li;
8528 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008529 int wo_spell_save = curwin->w_p_spell;
8530
8531 if (!curwin->w_p_spell)
8532 {
8533 did_set_spelllang(curwin);
8534 curwin->w_p_spell = TRUE;
8535 }
8536
8537 if (*curwin->w_s->b_p_spl == NUL)
8538 {
8539 emsg(_(e_no_spell));
8540 curwin->w_p_spell = wo_spell_save;
8541 return;
8542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543#endif
8544
8545 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008546 {
8547#ifdef FEAT_SPELL
8548 curwin->w_p_spell = wo_spell_save;
8549#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008551 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552
8553#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008554 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008556 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 if (argvars[1].v_type != VAR_UNKNOWN)
8558 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008559 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 if (maxcount <= 0)
8561 return;
8562 if (argvars[2].v_type != VAR_UNKNOWN)
8563 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008564 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 if (typeerr)
8566 return;
8567 }
8568 }
8569 else
8570 maxcount = 25;
8571
8572 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8573
8574 for (i = 0; i < ga.ga_len; ++i)
8575 {
8576 str = ((char_u **)ga.ga_data)[i];
8577
8578 li = listitem_alloc();
8579 if (li == NULL)
8580 vim_free(str);
8581 else
8582 {
8583 li->li_tv.v_type = VAR_STRING;
8584 li->li_tv.v_lock = 0;
8585 li->li_tv.vval.v_string = str;
8586 list_append(rettv->vval.v_list, li);
8587 }
8588 }
8589 ga_clear(&ga);
8590 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008591 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592#endif
8593}
8594
8595 static void
8596f_split(typval_T *argvars, typval_T *rettv)
8597{
8598 char_u *str;
8599 char_u *end;
8600 char_u *pat = NULL;
8601 regmatch_T regmatch;
8602 char_u patbuf[NUMBUFLEN];
8603 char_u *save_cpo;
8604 int match;
8605 colnr_T col = 0;
8606 int keepempty = FALSE;
8607 int typeerr = FALSE;
8608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008609 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008611 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008613 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 if (argvars[1].v_type != VAR_UNKNOWN)
8615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008616 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 if (pat == NULL)
8618 typeerr = TRUE;
8619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008620 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 }
8622 if (pat == NULL || *pat == NUL)
8623 pat = (char_u *)"[\\x01- ]\\+";
8624
8625 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008626 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008628 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629
8630 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8631 if (regmatch.regprog != NULL)
8632 {
8633 regmatch.rm_ic = FALSE;
8634 while (*str != NUL || keepempty)
8635 {
8636 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008637 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 else
8639 match = vim_regexec_nl(&regmatch, str, col);
8640 if (match)
8641 end = regmatch.startp[0];
8642 else
8643 end = str + STRLEN(str);
8644 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8645 && *str != NUL && match && end < regmatch.endp[0]))
8646 {
8647 if (list_append_string(rettv->vval.v_list, str,
8648 (int)(end - str)) == FAIL)
8649 break;
8650 }
8651 if (!match)
8652 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008653 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 if (regmatch.endp[0] > str)
8655 col = 0;
8656 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008657 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 str = regmatch.endp[0];
8660 }
8661
8662 vim_regfree(regmatch.regprog);
8663 }
8664
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008665theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 p_cpo = save_cpo;
8667}
8668
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008670 * "str2list()" function
8671 */
8672 static void
8673f_str2list(typval_T *argvars, typval_T *rettv)
8674{
8675 char_u *p;
8676 int utf8 = FALSE;
8677
8678 if (rettv_list_alloc(rettv) == FAIL)
8679 return;
8680
8681 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008682 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008683
8684 p = tv_get_string(&argvars[0]);
8685
8686 if (has_mbyte || utf8)
8687 {
8688 int (*ptr2len)(char_u *);
8689 int (*ptr2char)(char_u *);
8690
8691 if (utf8 || enc_utf8)
8692 {
8693 ptr2len = utf_ptr2len;
8694 ptr2char = utf_ptr2char;
8695 }
8696 else
8697 {
8698 ptr2len = mb_ptr2len;
8699 ptr2char = mb_ptr2char;
8700 }
8701
8702 for ( ; *p != NUL; p += (*ptr2len)(p))
8703 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8704 }
8705 else
8706 for ( ; *p != NUL; ++p)
8707 list_append_number(rettv->vval.v_list, *p);
8708}
8709
8710/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 * "str2nr()" function
8712 */
8713 static void
8714f_str2nr(typval_T *argvars, typval_T *rettv)
8715{
8716 int base = 10;
8717 char_u *p;
8718 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008719 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008720 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721
8722 if (argvars[1].v_type != VAR_UNKNOWN)
8723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 if (base != 2 && base != 8 && base != 10 && base != 16)
8726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008727 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 return;
8729 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008730 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008731 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 }
8733
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008734 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008735 isneg = (*p == '-');
8736 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008737 p = skipwhite(p + 1);
8738 switch (base)
8739 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008740 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008741 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008742 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008744 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8745 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008746 if (isneg)
8747 rettv->vval.v_number = -n;
8748 else
8749 rettv->vval.v_number = n;
8750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751}
8752
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753/*
8754 * "strgetchar()" function
8755 */
8756 static void
8757f_strgetchar(typval_T *argvars, typval_T *rettv)
8758{
8759 char_u *str;
8760 int len;
8761 int error = FALSE;
8762 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008763 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764
8765 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008766 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 if (str == NULL)
8768 return;
8769 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 if (error)
8772 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773
Bram Moolenaar13505972019-01-24 15:04:48 +01008774 while (charidx >= 0 && byteidx < len)
8775 {
8776 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008778 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8779 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008781 --charidx;
8782 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784}
8785
8786/*
8787 * "stridx()" function
8788 */
8789 static void
8790f_stridx(typval_T *argvars, typval_T *rettv)
8791{
8792 char_u buf[NUMBUFLEN];
8793 char_u *needle;
8794 char_u *haystack;
8795 char_u *save_haystack;
8796 char_u *pos;
8797 int start_idx;
8798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008799 needle = tv_get_string_chk(&argvars[1]);
8800 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 rettv->vval.v_number = -1;
8802 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008803 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804
8805 if (argvars[2].v_type != VAR_UNKNOWN)
8806 {
8807 int error = FALSE;
8808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008809 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810 if (error || start_idx >= (int)STRLEN(haystack))
8811 return;
8812 if (start_idx >= 0)
8813 haystack += start_idx;
8814 }
8815
8816 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8817 if (pos != NULL)
8818 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8819}
8820
8821/*
8822 * "string()" function
8823 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008824 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825f_string(typval_T *argvars, typval_T *rettv)
8826{
8827 char_u *tofree;
8828 char_u numbuf[NUMBUFLEN];
8829
8830 rettv->v_type = VAR_STRING;
8831 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8832 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008833 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 if (rettv->vval.v_string != NULL && tofree == NULL)
8835 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8836}
8837
8838/*
8839 * "strlen()" function
8840 */
8841 static void
8842f_strlen(typval_T *argvars, typval_T *rettv)
8843{
8844 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008845 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846}
8847
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008848 static void
8849strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8850{
8851 char_u *s = tv_get_string(&argvars[0]);
8852 varnumber_T len = 0;
8853 int (*func_mb_ptr2char_adv)(char_u **pp);
8854
8855 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8856 while (*s != NUL)
8857 {
8858 func_mb_ptr2char_adv(&s);
8859 ++len;
8860 }
8861 rettv->vval.v_number = len;
8862}
8863
8864/*
8865 * "strcharlen()" function
8866 */
8867 static void
8868f_strcharlen(typval_T *argvars, typval_T *rettv)
8869{
8870 strchar_common(argvars, rettv, TRUE);
8871}
8872
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873/*
8874 * "strchars()" function
8875 */
8876 static void
8877f_strchars(typval_T *argvars, typval_T *rettv)
8878{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008879 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880
8881 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008882 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008884 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008886 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887}
8888
8889/*
8890 * "strdisplaywidth()" function
8891 */
8892 static void
8893f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8894{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008895 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 int col = 0;
8897
8898 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008899 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900
8901 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8902}
8903
8904/*
8905 * "strwidth()" function
8906 */
8907 static void
8908f_strwidth(typval_T *argvars, typval_T *rettv)
8909{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008910 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911
Bram Moolenaar13505972019-01-24 15:04:48 +01008912 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913}
8914
8915/*
8916 * "strcharpart()" function
8917 */
8918 static void
8919f_strcharpart(typval_T *argvars, typval_T *rettv)
8920{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 char_u *p;
8922 int nchar;
8923 int nbyte = 0;
8924 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008925 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 int len = 0;
8927 int slen;
8928 int error = FALSE;
8929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008930 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 slen = (int)STRLEN(p);
8932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008933 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 if (!error)
8935 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008936 if (argvars[2].v_type != VAR_UNKNOWN
8937 && argvars[3].v_type != VAR_UNKNOWN)
8938 {
8939 skipcc = tv_get_bool(&argvars[3]);
8940 if (skipcc < 0 || skipcc > 1)
8941 {
8942 semsg(_(e_using_number_as_bool_nr), skipcc);
8943 return;
8944 }
8945 }
8946
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 if (nchar > 0)
8948 while (nchar > 0 && nbyte < slen)
8949 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008950 if (skipcc)
8951 nbyte += mb_ptr2len(p + nbyte);
8952 else
8953 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 --nchar;
8955 }
8956 else
8957 nbyte = nchar;
8958 if (argvars[2].v_type != VAR_UNKNOWN)
8959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008960 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 while (charlen > 0 && nbyte + len < slen)
8962 {
8963 int off = nbyte + len;
8964
8965 if (off < 0)
8966 len += 1;
8967 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008968 {
8969 if (skipcc)
8970 len += mb_ptr2len(p + off);
8971 else
8972 len += MB_CPTR2LEN(p + off);
8973 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 --charlen;
8975 }
8976 }
8977 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008978 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 }
8980
8981 /*
8982 * Only return the overlap between the specified part and the actual
8983 * string.
8984 */
8985 if (nbyte < 0)
8986 {
8987 len += nbyte;
8988 nbyte = 0;
8989 }
8990 else if (nbyte > slen)
8991 nbyte = slen;
8992 if (len < 0)
8993 len = 0;
8994 else if (nbyte + len > slen)
8995 len = slen - nbyte;
8996
8997 rettv->v_type = VAR_STRING;
8998 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999}
9000
9001/*
9002 * "strpart()" function
9003 */
9004 static void
9005f_strpart(typval_T *argvars, typval_T *rettv)
9006{
9007 char_u *p;
9008 int n;
9009 int len;
9010 int slen;
9011 int error = FALSE;
9012
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009013 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 slen = (int)STRLEN(p);
9015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009016 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 if (error)
9018 len = 0;
9019 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009020 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009022 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009024 // Only return the overlap between the specified part and the actual
9025 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (n < 0)
9027 {
9028 len += n;
9029 n = 0;
9030 }
9031 else if (n > slen)
9032 n = slen;
9033 if (len < 0)
9034 len = 0;
9035 else if (n + len > slen)
9036 len = slen - n;
9037
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009038 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9039 {
9040 int off;
9041
9042 // length in characters
9043 for (off = n; off < slen && len > 0; --len)
9044 off += mb_ptr2len(p + off);
9045 len = off - n;
9046 }
9047
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048 rettv->v_type = VAR_STRING;
9049 rettv->vval.v_string = vim_strnsave(p + n, len);
9050}
9051
9052/*
9053 * "strridx()" function
9054 */
9055 static void
9056f_strridx(typval_T *argvars, typval_T *rettv)
9057{
9058 char_u buf[NUMBUFLEN];
9059 char_u *needle;
9060 char_u *haystack;
9061 char_u *rest;
9062 char_u *lastmatch = NULL;
9063 int haystack_len, end_idx;
9064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009065 needle = tv_get_string_chk(&argvars[1]);
9066 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067
9068 rettv->vval.v_number = -1;
9069 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009070 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071
9072 haystack_len = (int)STRLEN(haystack);
9073 if (argvars[2].v_type != VAR_UNKNOWN)
9074 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009075 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009076 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009078 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 }
9080 else
9081 end_idx = haystack_len;
9082
9083 if (*needle == NUL)
9084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009085 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009086 lastmatch = haystack + end_idx;
9087 }
9088 else
9089 {
9090 for (rest = haystack; *rest != '\0'; ++rest)
9091 {
9092 rest = (char_u *)strstr((char *)rest, (char *)needle);
9093 if (rest == NULL || rest > haystack + end_idx)
9094 break;
9095 lastmatch = rest;
9096 }
9097 }
9098
9099 if (lastmatch == NULL)
9100 rettv->vval.v_number = -1;
9101 else
9102 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9103}
9104
9105/*
9106 * "strtrans()" function
9107 */
9108 static void
9109f_strtrans(typval_T *argvars, typval_T *rettv)
9110{
9111 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009112 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113}
9114
9115/*
9116 * "submatch()" function
9117 */
9118 static void
9119f_submatch(typval_T *argvars, typval_T *rettv)
9120{
9121 int error = FALSE;
9122 int no;
9123 int retList = 0;
9124
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009125 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 if (error)
9127 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009128 if (no < 0 || no >= NSUBEXP)
9129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009130 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009131 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009132 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009133 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009134 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135 if (error)
9136 return;
9137
9138 if (retList == 0)
9139 {
9140 rettv->v_type = VAR_STRING;
9141 rettv->vval.v_string = reg_submatch(no);
9142 }
9143 else
9144 {
9145 rettv->v_type = VAR_LIST;
9146 rettv->vval.v_list = reg_submatch_list(no);
9147 }
9148}
9149
9150/*
9151 * "substitute()" function
9152 */
9153 static void
9154f_substitute(typval_T *argvars, typval_T *rettv)
9155{
9156 char_u patbuf[NUMBUFLEN];
9157 char_u subbuf[NUMBUFLEN];
9158 char_u flagsbuf[NUMBUFLEN];
9159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 char_u *str = tv_get_string_chk(&argvars[0]);
9161 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009162 char_u *sub = NULL;
9163 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009164 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009166 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9167 expr = &argvars[2];
9168 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009169 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009172 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9173 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009174 rettv->vval.v_string = NULL;
9175 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009176 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009177}
9178
9179/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009180 * "swapinfo(swap_filename)" function
9181 */
9182 static void
9183f_swapinfo(typval_T *argvars, typval_T *rettv)
9184{
9185 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009186 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009187}
9188
9189/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009190 * "swapname(expr)" function
9191 */
9192 static void
9193f_swapname(typval_T *argvars, typval_T *rettv)
9194{
9195 buf_T *buf;
9196
9197 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009198 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009199 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9200 || buf->b_ml.ml_mfp->mf_fname == NULL)
9201 rettv->vval.v_string = NULL;
9202 else
9203 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9204}
9205
9206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009207 * "synID(lnum, col, trans)" function
9208 */
9209 static void
9210f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9211{
9212 int id = 0;
9213#ifdef FEAT_SYN_HL
9214 linenr_T lnum;
9215 colnr_T col;
9216 int trans;
9217 int transerr = FALSE;
9218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009219 lnum = tv_get_lnum(argvars); // -1 on type error
9220 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009221 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222
9223 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9224 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9225 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9226#endif
9227
9228 rettv->vval.v_number = id;
9229}
9230
9231/*
9232 * "synIDattr(id, what [, mode])" function
9233 */
9234 static void
9235f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9236{
9237 char_u *p = NULL;
9238#ifdef FEAT_SYN_HL
9239 int id;
9240 char_u *what;
9241 char_u *mode;
9242 char_u modebuf[NUMBUFLEN];
9243 int modec;
9244
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009245 id = (int)tv_get_number(&argvars[0]);
9246 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 if (argvars[2].v_type != VAR_UNKNOWN)
9248 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009249 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250 modec = TOLOWER_ASC(mode[0]);
9251 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009252 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009253 }
9254 else
9255 {
9256#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9257 if (USE_24BIT)
9258 modec = 'g';
9259 else
9260#endif
9261 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009262 modec = 'c';
9263 else
9264 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009265 }
9266
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 switch (TOLOWER_ASC(what[0]))
9268 {
9269 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009270 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009271 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009272 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 p = highlight_has_attr(id, HL_BOLD, modec);
9274 break;
9275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009276 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 p = highlight_color(id, what, modec);
9278 break;
9279
9280 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009281 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009283 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009284 p = highlight_has_attr(id, HL_ITALIC, modec);
9285 break;
9286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009287 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009288 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 break;
9290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009291 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009292 p = highlight_has_attr(id, HL_INVERSE, modec);
9293 break;
9294
9295 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009296 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009298 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009299 else if (TOLOWER_ASC(what[1]) == 't' &&
9300 TOLOWER_ASC(what[2]) == 'r')
9301 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009302 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 p = highlight_has_attr(id, HL_STANDOUT, modec);
9304 break;
9305
9306 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009307 if (TOLOWER_ASC(what[1]) == 'l') // ul
9308 p = highlight_color(id, what, modec);
9309 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009310 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9312 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009313 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9315 break;
9316 }
9317
9318 if (p != NULL)
9319 p = vim_strsave(p);
9320#endif
9321 rettv->v_type = VAR_STRING;
9322 rettv->vval.v_string = p;
9323}
9324
9325/*
9326 * "synIDtrans(id)" function
9327 */
9328 static void
9329f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9330{
9331 int id;
9332
9333#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009334 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009335
9336 if (id > 0)
9337 id = syn_get_final_id(id);
9338 else
9339#endif
9340 id = 0;
9341
9342 rettv->vval.v_number = id;
9343}
9344
9345/*
9346 * "synconcealed(lnum, col)" function
9347 */
9348 static void
9349f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9350{
9351#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9352 linenr_T lnum;
9353 colnr_T col;
9354 int syntax_flags = 0;
9355 int cchar;
9356 int matchid = 0;
9357 char_u str[NUMBUFLEN];
9358#endif
9359
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009360 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361
9362#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009363 lnum = tv_get_lnum(argvars); // -1 on type error
9364 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365
Bram Moolenaara80faa82020-04-12 19:37:17 +02009366 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009367
9368 if (rettv_list_alloc(rettv) != FAIL)
9369 {
9370 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9371 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9372 && curwin->w_p_cole > 0)
9373 {
9374 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9375 syntax_flags = get_syntax_info(&matchid);
9376
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009377 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9379 {
9380 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009381 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009382 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9383 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009384 if (cchar != NUL)
9385 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009386 if (has_mbyte)
9387 (*mb_char2bytes)(cchar, str);
9388 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 str[0] = cchar;
9390 }
9391 }
9392 }
9393
9394 list_append_number(rettv->vval.v_list,
9395 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009396 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397 list_append_string(rettv->vval.v_list, str, -1);
9398 list_append_number(rettv->vval.v_list, matchid);
9399 }
9400#endif
9401}
9402
9403/*
9404 * "synstack(lnum, col)" function
9405 */
9406 static void
9407f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9408{
9409#ifdef FEAT_SYN_HL
9410 linenr_T lnum;
9411 colnr_T col;
9412 int i;
9413 int id;
9414#endif
9415
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009416 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417
9418#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009419 lnum = tv_get_lnum(argvars); // -1 on type error
9420 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421
9422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9423 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9424 && rettv_list_alloc(rettv) != FAIL)
9425 {
9426 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9427 for (i = 0; ; ++i)
9428 {
9429 id = syn_get_stack_item(i);
9430 if (id < 0)
9431 break;
9432 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9433 break;
9434 }
9435 }
9436#endif
9437}
9438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009439/*
9440 * "tabpagebuflist()" function
9441 */
9442 static void
9443f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9444{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445 tabpage_T *tp;
9446 win_T *wp = NULL;
9447
9448 if (argvars[0].v_type == VAR_UNKNOWN)
9449 wp = firstwin;
9450 else
9451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 if (tp != NULL)
9454 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9455 }
9456 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9457 {
9458 for (; wp != NULL; wp = wp->w_next)
9459 if (list_append_number(rettv->vval.v_list,
9460 wp->w_buffer->b_fnum) == FAIL)
9461 break;
9462 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463}
9464
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 * "tagfiles()" function
9467 */
9468 static void
9469f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9470{
9471 char_u *fname;
9472 tagname_T tn;
9473 int first;
9474
9475 if (rettv_list_alloc(rettv) == FAIL)
9476 return;
9477 fname = alloc(MAXPATHL);
9478 if (fname == NULL)
9479 return;
9480
9481 for (first = TRUE; ; first = FALSE)
9482 if (get_tagfname(&tn, first, fname) == FAIL
9483 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9484 break;
9485 tagname_free(&tn);
9486 vim_free(fname);
9487}
9488
9489/*
9490 * "taglist()" function
9491 */
9492 static void
9493f_taglist(typval_T *argvars, typval_T *rettv)
9494{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009495 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 char_u *tag_pattern;
9497
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009498 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499
9500 rettv->vval.v_number = FALSE;
9501 if (*tag_pattern == NUL)
9502 return;
9503
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009505 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009507 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508}
9509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 * "tolower(string)" function
9512 */
9513 static void
9514f_tolower(typval_T *argvars, typval_T *rettv)
9515{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518}
9519
9520/*
9521 * "toupper(string)" function
9522 */
9523 static void
9524f_toupper(typval_T *argvars, typval_T *rettv)
9525{
9526 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009527 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528}
9529
9530/*
9531 * "tr(string, fromstr, tostr)" function
9532 */
9533 static void
9534f_tr(typval_T *argvars, typval_T *rettv)
9535{
9536 char_u *in_str;
9537 char_u *fromstr;
9538 char_u *tostr;
9539 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540 int inlen;
9541 int fromlen;
9542 int tolen;
9543 int idx;
9544 char_u *cpstr;
9545 int cplen;
9546 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 char_u buf[NUMBUFLEN];
9548 char_u buf2[NUMBUFLEN];
9549 garray_T ga;
9550
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009551 in_str = tv_get_string(&argvars[0]);
9552 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9553 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009555 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 rettv->v_type = VAR_STRING;
9557 rettv->vval.v_string = NULL;
9558 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009559 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009560 ga_init2(&ga, (int)sizeof(char), 80);
9561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009562 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009563 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 if (STRLEN(fromstr) != STRLEN(tostr))
9565 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009567 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 ga_clear(&ga);
9569 return;
9570 }
9571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009572 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 while (*in_str != NUL)
9574 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 if (has_mbyte)
9576 {
9577 inlen = (*mb_ptr2len)(in_str);
9578 cpstr = in_str;
9579 cplen = inlen;
9580 idx = 0;
9581 for (p = fromstr; *p != NUL; p += fromlen)
9582 {
9583 fromlen = (*mb_ptr2len)(p);
9584 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9585 {
9586 for (p = tostr; *p != NUL; p += tolen)
9587 {
9588 tolen = (*mb_ptr2len)(p);
9589 if (idx-- == 0)
9590 {
9591 cplen = tolen;
9592 cpstr = p;
9593 break;
9594 }
9595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009596 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 goto error;
9598 break;
9599 }
9600 ++idx;
9601 }
9602
9603 if (first && cpstr == in_str)
9604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009605 // Check that fromstr and tostr have the same number of
9606 // (multi-byte) characters. Done only once when a character
9607 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 first = FALSE;
9609 for (p = tostr; *p != NUL; p += tolen)
9610 {
9611 tolen = (*mb_ptr2len)(p);
9612 --idx;
9613 }
9614 if (idx != 0)
9615 goto error;
9616 }
9617
9618 (void)ga_grow(&ga, cplen);
9619 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9620 ga.ga_len += cplen;
9621
9622 in_str += inlen;
9623 }
9624 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009626 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 p = vim_strchr(fromstr, *in_str);
9628 if (p != NULL)
9629 ga_append(&ga, tostr[p - fromstr]);
9630 else
9631 ga_append(&ga, *in_str);
9632 ++in_str;
9633 }
9634 }
9635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009636 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637 (void)ga_grow(&ga, 1);
9638 ga_append(&ga, NUL);
9639
9640 rettv->vval.v_string = ga.ga_data;
9641}
9642
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009643/*
9644 * "trim({expr})" function
9645 */
9646 static void
9647f_trim(typval_T *argvars, typval_T *rettv)
9648{
9649 char_u buf1[NUMBUFLEN];
9650 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009651 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009652 char_u *mask = NULL;
9653 char_u *tail;
9654 char_u *prev;
9655 char_u *p;
9656 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009657 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009658
9659 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009660 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009661 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009662 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009663
9664 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009666 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009667
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009668 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009669 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009670 int error = 0;
9671
9672 // leading or trailing characters to trim
9673 dir = (int)tv_get_number_chk(&argvars[2], &error);
9674 if (error)
9675 return;
9676 if (dir < 0 || dir > 2)
9677 {
9678 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9679 return;
9680 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009681 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009682 }
9683
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009684 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009685 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009686 // Trim leading characters
9687 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009688 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009689 c1 = PTR2CHAR(head);
9690 if (mask == NULL)
9691 {
9692 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009693 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009694 }
9695 else
9696 {
9697 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9698 if (c1 == PTR2CHAR(p))
9699 break;
9700 if (*p == NUL)
9701 break;
9702 }
9703 MB_PTR_ADV(head);
9704 }
9705 }
9706
9707 tail = head + STRLEN(head);
9708 if (dir == 0 || dir == 2)
9709 {
9710 // Trim trailing characters
9711 for (; tail > head; tail = prev)
9712 {
9713 prev = tail;
9714 MB_PTR_BACK(head, prev);
9715 c1 = PTR2CHAR(prev);
9716 if (mask == NULL)
9717 {
9718 if (c1 > ' ' && c1 != 0xa0)
9719 break;
9720 }
9721 else
9722 {
9723 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9724 if (c1 == PTR2CHAR(p))
9725 break;
9726 if (*p == NUL)
9727 break;
9728 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009729 }
9730 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009731 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009732}
9733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734/*
9735 * "type(expr)" function
9736 */
9737 static void
9738f_type(typval_T *argvars, typval_T *rettv)
9739{
9740 int n = -1;
9741
9742 switch (argvars[0].v_type)
9743 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009744 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9745 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009746 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009747 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9748 case VAR_LIST: n = VAR_TYPE_LIST; break;
9749 case VAR_DICT: n = VAR_TYPE_DICT; break;
9750 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9751 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9752 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009753 case VAR_JOB: n = VAR_TYPE_JOB; break;
9754 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009755 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009756 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009758 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009759 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009760 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761 n = -1;
9762 break;
9763 }
9764 rettv->vval.v_number = n;
9765}
9766
9767/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 * "virtcol(string)" function
9769 */
9770 static void
9771f_virtcol(typval_T *argvars, typval_T *rettv)
9772{
9773 colnr_T vcol = 0;
9774 pos_T *fp;
9775 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009776 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009778 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9780 && fnum == curbuf->b_fnum)
9781 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009782 // Limit the column to a valid value, getvvcol() doesn't check.
9783 if (fp->col < 0)
9784 fp->col = 0;
9785 else
9786 {
9787 len = (int)STRLEN(ml_get(fp->lnum));
9788 if (fp->col > len)
9789 fp->col = len;
9790 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 getvvcol(curwin, fp, NULL, NULL, &vcol);
9792 ++vcol;
9793 }
9794
9795 rettv->vval.v_number = vcol;
9796}
9797
9798/*
9799 * "visualmode()" function
9800 */
9801 static void
9802f_visualmode(typval_T *argvars, typval_T *rettv)
9803{
9804 char_u str[2];
9805
9806 rettv->v_type = VAR_STRING;
9807 str[0] = curbuf->b_visual_mode_eval;
9808 str[1] = NUL;
9809 rettv->vval.v_string = vim_strsave(str);
9810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009811 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 if (non_zero_arg(&argvars[0]))
9813 curbuf->b_visual_mode_eval = NUL;
9814}
9815
9816/*
9817 * "wildmenumode()" function
9818 */
9819 static void
9820f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9821{
9822#ifdef FEAT_WILDMENU
9823 if (wild_menu_showing)
9824 rettv->vval.v_number = 1;
9825#endif
9826}
9827
9828/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009829 * "windowsversion()" function
9830 */
9831 static void
9832f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9833{
9834 rettv->v_type = VAR_STRING;
9835 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9836}
9837
9838/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 * "wordcount()" function
9840 */
9841 static void
9842f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9843{
9844 if (rettv_dict_alloc(rettv) == FAIL)
9845 return;
9846 cursor_pos_info(rettv->vval.v_dict);
9847}
9848
9849/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850 * "xor(expr, expr)" function
9851 */
9852 static void
9853f_xor(typval_T *argvars, typval_T *rettv)
9854{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009855 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9856 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009857}
9858
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009859#endif // FEAT_EVAL