blob: 07f158d6d7337a4afbbd13d3d548c9fa6a8b83b1 [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,
948 ret_number, f_getchar},
949 {"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},
955 {"getcmdline", 0, 0, 0, NULL,
956 ret_string, f_getcmdline},
957 {"getcmdpos", 0, 0, 0, NULL,
958 ret_number, f_getcmdpos},
959 {"getcmdtype", 0, 0, 0, NULL,
960 ret_string, f_getcmdtype},
961 {"getcmdwintype", 0, 0, 0, NULL,
962 ret_string, f_getcmdwintype},
963 {"getcompletion", 2, 3, FEARG_1, NULL,
964 ret_list_string, f_getcompletion},
965 {"getcurpos", 0, 1, FEARG_1, NULL,
966 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100967 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
968 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200969 {"getcwd", 0, 2, FEARG_1, NULL,
970 ret_string, f_getcwd},
971 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100972 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200973 {"getfontname", 0, 1, 0, NULL,
974 ret_string, f_getfontname},
975 {"getfperm", 1, 1, FEARG_1, NULL,
976 ret_string, f_getfperm},
977 {"getfsize", 1, 1, FEARG_1, NULL,
978 ret_number, f_getfsize},
979 {"getftime", 1, 1, FEARG_1, NULL,
980 ret_number, f_getftime},
981 {"getftype", 1, 1, FEARG_1, NULL,
982 ret_string, f_getftype},
983 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100984 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200985 {"getjumplist", 0, 2, FEARG_1, NULL,
986 ret_list_any, f_getjumplist},
987 {"getline", 1, 2, FEARG_1, NULL,
988 ret_f_getline, f_getline},
989 {"getloclist", 1, 2, 0, NULL,
990 ret_list_or_dict_1, f_getloclist},
991 {"getmarklist", 0, 1, FEARG_1, NULL,
992 ret_list_dict_any, f_getmarklist},
993 {"getmatches", 0, 1, 0, NULL,
994 ret_list_dict_any, f_getmatches},
995 {"getmousepos", 0, 0, 0, NULL,
996 ret_dict_number, f_getmousepos},
997 {"getpid", 0, 0, 0, NULL,
998 ret_number, f_getpid},
999 {"getpos", 1, 1, FEARG_1, NULL,
1000 ret_list_number, f_getpos},
1001 {"getqflist", 0, 1, 0, NULL,
1002 ret_list_or_dict_0, f_getqflist},
1003 {"getreg", 0, 3, FEARG_1, NULL,
1004 ret_getreg, f_getreg},
1005 {"getreginfo", 0, 1, FEARG_1, NULL,
1006 ret_dict_any, f_getreginfo},
1007 {"getregtype", 0, 1, FEARG_1, NULL,
1008 ret_string, f_getregtype},
1009 {"gettabinfo", 0, 1, FEARG_1, NULL,
1010 ret_list_dict_any, f_gettabinfo},
1011 {"gettabvar", 2, 3, FEARG_1, NULL,
1012 ret_any, f_gettabvar},
1013 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1014 ret_any, f_gettabwinvar},
1015 {"gettagstack", 0, 1, FEARG_1, NULL,
1016 ret_dict_any, f_gettagstack},
1017 {"gettext", 1, 1, FEARG_1, NULL,
1018 ret_string, f_gettext},
1019 {"getwininfo", 0, 1, FEARG_1, NULL,
1020 ret_list_dict_any, f_getwininfo},
1021 {"getwinpos", 0, 1, FEARG_1, NULL,
1022 ret_list_number, f_getwinpos},
1023 {"getwinposx", 0, 0, 0, NULL,
1024 ret_number, f_getwinposx},
1025 {"getwinposy", 0, 0, 0, NULL,
1026 ret_number, f_getwinposy},
1027 {"getwinvar", 2, 3, FEARG_1, NULL,
1028 ret_any, f_getwinvar},
1029 {"glob", 1, 4, FEARG_1, NULL,
1030 ret_any, f_glob},
1031 {"glob2regpat", 1, 1, FEARG_1, NULL,
1032 ret_string, f_glob2regpat},
1033 {"globpath", 2, 5, FEARG_2, NULL,
1034 ret_any, f_globpath},
1035 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001036 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001037 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001038 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001039 {"haslocaldir", 0, 2, FEARG_1, NULL,
1040 ret_number, f_haslocaldir},
1041 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001042 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001043 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1044 ret_number, f_hlID},
1045 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001046 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001047 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001048 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001049 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001050 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001051 {"histget", 1, 2, FEARG_1, NULL,
1052 ret_string, f_histget},
1053 {"histnr", 1, 1, FEARG_1, NULL,
1054 ret_number, f_histnr},
1055 {"hlID", 1, 1, FEARG_1, NULL,
1056 ret_number, f_hlID},
1057 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001058 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001059 {"hostname", 0, 0, 0, NULL,
1060 ret_string, f_hostname},
1061 {"iconv", 3, 3, FEARG_1, NULL,
1062 ret_string, f_iconv},
1063 {"indent", 1, 1, FEARG_1, NULL,
1064 ret_number, f_indent},
1065 {"index", 2, 4, FEARG_1, NULL,
1066 ret_number, f_index},
1067 {"input", 1, 3, FEARG_1, NULL,
1068 ret_string, f_input},
1069 {"inputdialog", 1, 3, FEARG_1, NULL,
1070 ret_string, f_inputdialog},
1071 {"inputlist", 1, 1, FEARG_1, NULL,
1072 ret_number, f_inputlist},
1073 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001074 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001075 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 {"inputsecret", 1, 2, FEARG_1, NULL,
1078 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001079 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001080 ret_first_arg, f_insert},
1081 {"interrupt", 0, 0, 0, NULL,
1082 ret_void, f_interrupt},
1083 {"invert", 1, 1, FEARG_1, NULL,
1084 ret_number, f_invert},
1085 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"isinf", 1, 1, FEARG_1, NULL,
1088 ret_number, MATH_FUNC(f_isinf)},
1089 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001090 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001091 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001093 {"items", 1, 1, FEARG_1, NULL,
1094 ret_list_any, f_items},
1095 {"job_getchannel", 1, 1, FEARG_1, NULL,
1096 ret_channel, JOB_FUNC(f_job_getchannel)},
1097 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001098 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"job_setoptions", 2, 2, FEARG_1, NULL,
1100 ret_void, JOB_FUNC(f_job_setoptions)},
1101 {"job_start", 1, 2, FEARG_1, NULL,
1102 ret_job, JOB_FUNC(f_job_start)},
1103 {"job_status", 1, 1, FEARG_1, NULL,
1104 ret_string, JOB_FUNC(f_job_status)},
1105 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001106 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001107 {"join", 1, 2, FEARG_1, NULL,
1108 ret_string, f_join},
1109 {"js_decode", 1, 1, FEARG_1, NULL,
1110 ret_any, f_js_decode},
1111 {"js_encode", 1, 1, FEARG_1, NULL,
1112 ret_string, f_js_encode},
1113 {"json_decode", 1, 1, FEARG_1, NULL,
1114 ret_any, f_json_decode},
1115 {"json_encode", 1, 1, FEARG_1, NULL,
1116 ret_string, f_json_encode},
1117 {"keys", 1, 1, FEARG_1, NULL,
1118 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001119 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1120 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001121 {"len", 1, 1, FEARG_1, NULL,
1122 ret_number, f_len},
1123 {"libcall", 3, 3, FEARG_3, NULL,
1124 ret_string, f_libcall},
1125 {"libcallnr", 3, 3, FEARG_3, NULL,
1126 ret_number, f_libcallnr},
1127 {"line", 1, 2, FEARG_1, NULL,
1128 ret_number, f_line},
1129 {"line2byte", 1, 1, FEARG_1, NULL,
1130 ret_number, f_line2byte},
1131 {"lispindent", 1, 1, FEARG_1, NULL,
1132 ret_number, f_lispindent},
1133 {"list2str", 1, 2, FEARG_1, NULL,
1134 ret_string, f_list2str},
1135 {"listener_add", 1, 2, FEARG_2, NULL,
1136 ret_number, f_listener_add},
1137 {"listener_flush", 0, 1, FEARG_1, NULL,
1138 ret_void, f_listener_flush},
1139 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"localtime", 0, 0, 0, NULL,
1142 ret_number, f_localtime},
1143 {"log", 1, 1, FEARG_1, NULL,
1144 ret_float, FLOAT_FUNC(f_log)},
1145 {"log10", 1, 1, FEARG_1, NULL,
1146 ret_float, FLOAT_FUNC(f_log10)},
1147 {"luaeval", 1, 2, FEARG_1, NULL,
1148 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001149#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001150 f_luaeval
1151#else
1152 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001153#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001154 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001155 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001156 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"maparg", 1, 4, FEARG_1, NULL,
1158 ret_maparg, f_maparg},
1159 {"mapcheck", 1, 3, FEARG_1, NULL,
1160 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001161 {"mapnew", 2, 2, FEARG_1, NULL,
1162 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001163 {"mapset", 3, 3, FEARG_1, NULL,
1164 ret_void, f_mapset},
1165 {"match", 2, 4, FEARG_1, NULL,
1166 ret_any, f_match},
1167 {"matchadd", 2, 5, FEARG_1, NULL,
1168 ret_number, f_matchadd},
1169 {"matchaddpos", 2, 5, FEARG_1, NULL,
1170 ret_number, f_matchaddpos},
1171 {"matcharg", 1, 1, FEARG_1, NULL,
1172 ret_list_string, f_matcharg},
1173 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001174 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001175 {"matchend", 2, 4, FEARG_1, NULL,
1176 ret_number, f_matchend},
1177 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1178 ret_list_string, f_matchfuzzy},
1179 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1180 ret_list_any, f_matchfuzzypos},
1181 {"matchlist", 2, 4, FEARG_1, NULL,
1182 ret_list_string, f_matchlist},
1183 {"matchstr", 2, 4, FEARG_1, NULL,
1184 ret_string, f_matchstr},
1185 {"matchstrpos", 2, 4, FEARG_1, NULL,
1186 ret_list_any, f_matchstrpos},
1187 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001188 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001189 {"menu_info", 1, 2, FEARG_1, NULL,
1190 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001191#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001192 f_menu_info
1193#else
1194 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001195#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001196 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001197 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001198 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001199 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001200 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001201 {"mode", 0, 1, FEARG_1, NULL,
1202 ret_string, f_mode},
1203 {"mzeval", 1, 1, FEARG_1, NULL,
1204 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001206 f_mzeval
1207#else
1208 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001210 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001211 {"nextnonblank", 1, 1, FEARG_1, NULL,
1212 ret_number, f_nextnonblank},
1213 {"nr2char", 1, 2, FEARG_1, NULL,
1214 ret_string, f_nr2char},
1215 {"or", 2, 2, FEARG_1, NULL,
1216 ret_number, f_or},
1217 {"pathshorten", 1, 2, FEARG_1, NULL,
1218 ret_string, f_pathshorten},
1219 {"perleval", 1, 1, FEARG_1, NULL,
1220 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001222 f_perleval
1223#else
1224 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001225#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001226 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001227 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1228 ret_number, PROP_FUNC(f_popup_atcursor)},
1229 {"popup_beval", 2, 2, FEARG_1, NULL,
1230 ret_number, PROP_FUNC(f_popup_beval)},
1231 {"popup_clear", 0, 1, 0, NULL,
1232 ret_void, PROP_FUNC(f_popup_clear)},
1233 {"popup_close", 1, 2, FEARG_1, NULL,
1234 ret_void, PROP_FUNC(f_popup_close)},
1235 {"popup_create", 2, 2, FEARG_1, NULL,
1236 ret_number, PROP_FUNC(f_popup_create)},
1237 {"popup_dialog", 2, 2, FEARG_1, NULL,
1238 ret_number, PROP_FUNC(f_popup_dialog)},
1239 {"popup_filter_menu", 2, 2, 0, NULL,
1240 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1241 {"popup_filter_yesno", 2, 2, 0, NULL,
1242 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1243 {"popup_findinfo", 0, 0, 0, NULL,
1244 ret_number, PROP_FUNC(f_popup_findinfo)},
1245 {"popup_findpreview", 0, 0, 0, NULL,
1246 ret_number, PROP_FUNC(f_popup_findpreview)},
1247 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1248 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1249 {"popup_getpos", 1, 1, FEARG_1, NULL,
1250 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1251 {"popup_hide", 1, 1, FEARG_1, NULL,
1252 ret_void, PROP_FUNC(f_popup_hide)},
1253 {"popup_list", 0, 0, 0, NULL,
1254 ret_list_number, PROP_FUNC(f_popup_list)},
1255 {"popup_locate", 2, 2, 0, NULL,
1256 ret_number, PROP_FUNC(f_popup_locate)},
1257 {"popup_menu", 2, 2, FEARG_1, NULL,
1258 ret_number, PROP_FUNC(f_popup_menu)},
1259 {"popup_move", 2, 2, FEARG_1, NULL,
1260 ret_void, PROP_FUNC(f_popup_move)},
1261 {"popup_notification", 2, 2, FEARG_1, NULL,
1262 ret_number, PROP_FUNC(f_popup_notification)},
1263 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1264 ret_void, PROP_FUNC(f_popup_setoptions)},
1265 {"popup_settext", 2, 2, FEARG_1, NULL,
1266 ret_void, PROP_FUNC(f_popup_settext)},
1267 {"popup_show", 1, 1, FEARG_1, NULL,
1268 ret_void, PROP_FUNC(f_popup_show)},
1269 {"pow", 2, 2, FEARG_1, NULL,
1270 ret_float, FLOAT_FUNC(f_pow)},
1271 {"prevnonblank", 1, 1, FEARG_1, NULL,
1272 ret_number, f_prevnonblank},
1273 {"printf", 1, 19, FEARG_2, NULL,
1274 ret_string, f_printf},
1275 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1276 ret_string, JOB_FUNC(f_prompt_getprompt)},
1277 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1278 ret_void, JOB_FUNC(f_prompt_setcallback)},
1279 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1280 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1281 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1282 ret_void, JOB_FUNC(f_prompt_setprompt)},
1283 {"prop_add", 3, 3, FEARG_1, NULL,
1284 ret_void, PROP_FUNC(f_prop_add)},
1285 {"prop_clear", 1, 3, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_prop_clear)},
1287 {"prop_find", 1, 2, FEARG_1, NULL,
1288 ret_dict_any, PROP_FUNC(f_prop_find)},
1289 {"prop_list", 1, 2, FEARG_1, NULL,
1290 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1291 {"prop_remove", 1, 3, FEARG_1, NULL,
1292 ret_number, PROP_FUNC(f_prop_remove)},
1293 {"prop_type_add", 2, 2, FEARG_1, NULL,
1294 ret_void, PROP_FUNC(f_prop_type_add)},
1295 {"prop_type_change", 2, 2, FEARG_1, NULL,
1296 ret_void, PROP_FUNC(f_prop_type_change)},
1297 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1298 ret_void, PROP_FUNC(f_prop_type_delete)},
1299 {"prop_type_get", 1, 2, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1301 {"prop_type_list", 0, 1, FEARG_1, NULL,
1302 ret_list_string, PROP_FUNC(f_prop_type_list)},
1303 {"pum_getpos", 0, 0, 0, NULL,
1304 ret_dict_number, f_pum_getpos},
1305 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001306 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001307 {"py3eval", 1, 1, FEARG_1, NULL,
1308 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001310 f_py3eval
1311#else
1312 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001314 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001315 {"pyeval", 1, 1, FEARG_1, NULL,
1316 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001318 f_pyeval
1319#else
1320 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001321#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001322 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001323 {"pyxeval", 1, 1, FEARG_1, NULL,
1324 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001325#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001326 f_pyxeval
1327#else
1328 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001329#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001330 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001331 {"rand", 0, 1, FEARG_1, NULL,
1332 ret_number, f_rand},
1333 {"range", 1, 3, FEARG_1, NULL,
1334 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001335 {"readblob", 1, 1, FEARG_1, NULL,
1336 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001337 {"readdir", 1, 3, FEARG_1, NULL,
1338 ret_list_string, f_readdir},
1339 {"readdirex", 1, 3, FEARG_1, NULL,
1340 ret_list_dict_any, f_readdirex},
1341 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001342 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001343 {"reduce", 2, 3, FEARG_1, NULL,
1344 ret_any, f_reduce},
1345 {"reg_executing", 0, 0, 0, NULL,
1346 ret_string, f_reg_executing},
1347 {"reg_recording", 0, 0, 0, NULL,
1348 ret_string, f_reg_recording},
1349 {"reltime", 0, 2, FEARG_1, NULL,
1350 ret_list_any, f_reltime},
1351 {"reltimefloat", 1, 1, FEARG_1, NULL,
1352 ret_float, FLOAT_FUNC(f_reltimefloat)},
1353 {"reltimestr", 1, 1, FEARG_1, NULL,
1354 ret_string, f_reltimestr},
1355 {"remote_expr", 2, 4, FEARG_1, NULL,
1356 ret_string, f_remote_expr},
1357 {"remote_foreground", 1, 1, FEARG_1, NULL,
1358 ret_string, f_remote_foreground},
1359 {"remote_peek", 1, 2, FEARG_1, NULL,
1360 ret_number, f_remote_peek},
1361 {"remote_read", 1, 2, FEARG_1, NULL,
1362 ret_string, f_remote_read},
1363 {"remote_send", 2, 3, FEARG_1, NULL,
1364 ret_string, f_remote_send},
1365 {"remote_startserver", 1, 1, FEARG_1, NULL,
1366 ret_void, f_remote_startserver},
1367 {"remove", 2, 3, FEARG_1, NULL,
1368 ret_remove, f_remove},
1369 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001370 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 {"repeat", 2, 2, FEARG_1, NULL,
1372 ret_first_arg, f_repeat},
1373 {"resolve", 1, 1, FEARG_1, NULL,
1374 ret_string, f_resolve},
1375 {"reverse", 1, 1, FEARG_1, NULL,
1376 ret_first_arg, f_reverse},
1377 {"round", 1, 1, FEARG_1, NULL,
1378 ret_float, FLOAT_FUNC(f_round)},
1379 {"rubyeval", 1, 1, FEARG_1, NULL,
1380 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001381#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001382 f_rubyeval
1383#else
1384 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001385#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001386 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001387 {"screenattr", 2, 2, FEARG_1, NULL,
1388 ret_number, f_screenattr},
1389 {"screenchar", 2, 2, FEARG_1, NULL,
1390 ret_number, f_screenchar},
1391 {"screenchars", 2, 2, FEARG_1, NULL,
1392 ret_list_number, f_screenchars},
1393 {"screencol", 0, 0, 0, NULL,
1394 ret_number, f_screencol},
1395 {"screenpos", 3, 3, FEARG_1, NULL,
1396 ret_dict_number, f_screenpos},
1397 {"screenrow", 0, 0, 0, NULL,
1398 ret_number, f_screenrow},
1399 {"screenstring", 2, 2, FEARG_1, NULL,
1400 ret_string, f_screenstring},
1401 {"search", 1, 5, FEARG_1, NULL,
1402 ret_number, f_search},
1403 {"searchcount", 0, 1, FEARG_1, NULL,
1404 ret_dict_any, f_searchcount},
1405 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001406 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001407 {"searchpair", 3, 7, 0, NULL,
1408 ret_number, f_searchpair},
1409 {"searchpairpos", 3, 7, 0, NULL,
1410 ret_list_number, f_searchpairpos},
1411 {"searchpos", 1, 5, FEARG_1, NULL,
1412 ret_list_number, f_searchpos},
1413 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001414 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001415 {"serverlist", 0, 0, 0, NULL,
1416 ret_string, f_serverlist},
1417 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001418 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001419 {"setbufvar", 3, 3, FEARG_3, NULL,
1420 ret_void, f_setbufvar},
1421 {"setcellwidths", 1, 1, FEARG_1, NULL,
1422 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001423 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001424 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001425 {"setcharsearch", 1, 1, FEARG_1, NULL,
1426 ret_void, f_setcharsearch},
1427 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001428 ret_number_bool, f_setcmdpos},
1429 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1430 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001431 {"setenv", 2, 2, FEARG_2, NULL,
1432 ret_void, f_setenv},
1433 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001434 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001435 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001436 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001438 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001440 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001441 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001444 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001445 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001446 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001447 {"settabvar", 3, 3, FEARG_3, NULL,
1448 ret_void, f_settabvar},
1449 {"settabwinvar", 4, 4, FEARG_4, NULL,
1450 ret_void, f_settabwinvar},
1451 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001452 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001453 {"setwinvar", 3, 3, FEARG_3, NULL,
1454 ret_void, f_setwinvar},
1455 {"sha256", 1, 1, FEARG_1, NULL,
1456 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001457#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001458 f_sha256
1459#else
1460 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001462 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 {"shellescape", 1, 2, FEARG_1, NULL,
1464 ret_string, f_shellescape},
1465 {"shiftwidth", 0, 1, FEARG_1, NULL,
1466 ret_number, f_shiftwidth},
1467 {"sign_define", 1, 2, FEARG_1, NULL,
1468 ret_any, SIGN_FUNC(f_sign_define)},
1469 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1470 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1471 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1472 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1473 {"sign_jump", 3, 3, FEARG_1, NULL,
1474 ret_number, SIGN_FUNC(f_sign_jump)},
1475 {"sign_place", 4, 5, FEARG_1, NULL,
1476 ret_number, SIGN_FUNC(f_sign_place)},
1477 {"sign_placelist", 1, 1, FEARG_1, NULL,
1478 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1479 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001482 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1484 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1485 {"simplify", 1, 1, FEARG_1, NULL,
1486 ret_string, f_simplify},
1487 {"sin", 1, 1, FEARG_1, NULL,
1488 ret_float, FLOAT_FUNC(f_sin)},
1489 {"sinh", 1, 1, FEARG_1, NULL,
1490 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001491 {"slice", 2, 3, FEARG_1, NULL,
1492 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"sort", 1, 3, FEARG_1, NULL,
1494 ret_first_arg, f_sort},
1495 {"sound_clear", 0, 0, 0, NULL,
1496 ret_void, SOUND_FUNC(f_sound_clear)},
1497 {"sound_playevent", 1, 2, FEARG_1, NULL,
1498 ret_number, SOUND_FUNC(f_sound_playevent)},
1499 {"sound_playfile", 1, 2, FEARG_1, NULL,
1500 ret_number, SOUND_FUNC(f_sound_playfile)},
1501 {"sound_stop", 1, 1, FEARG_1, NULL,
1502 ret_void, SOUND_FUNC(f_sound_stop)},
1503 {"soundfold", 1, 1, FEARG_1, NULL,
1504 ret_string, f_soundfold},
1505 {"spellbadword", 0, 1, FEARG_1, NULL,
1506 ret_list_string, f_spellbadword},
1507 {"spellsuggest", 1, 3, FEARG_1, NULL,
1508 ret_list_string, f_spellsuggest},
1509 {"split", 1, 3, FEARG_1, NULL,
1510 ret_list_string, f_split},
1511 {"sqrt", 1, 1, FEARG_1, NULL,
1512 ret_float, FLOAT_FUNC(f_sqrt)},
1513 {"srand", 0, 1, FEARG_1, NULL,
1514 ret_list_number, f_srand},
1515 {"state", 0, 1, FEARG_1, NULL,
1516 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001517 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 ret_float, FLOAT_FUNC(f_str2float)},
1519 {"str2list", 1, 2, FEARG_1, NULL,
1520 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001521 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001522 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001523 {"strcharlen", 1, 1, FEARG_1, NULL,
1524 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001525 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001526 ret_string, f_strcharpart},
1527 {"strchars", 1, 2, FEARG_1, NULL,
1528 ret_number, f_strchars},
1529 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1530 ret_number, f_strdisplaywidth},
1531 {"strftime", 1, 2, FEARG_1, NULL,
1532 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001534 f_strftime
1535#else
1536 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001538 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001539 {"strgetchar", 2, 2, FEARG_1, NULL,
1540 ret_number, f_strgetchar},
1541 {"stridx", 2, 3, FEARG_1, NULL,
1542 ret_number, f_stridx},
1543 {"string", 1, 1, FEARG_1, NULL,
1544 ret_string, f_string},
1545 {"strlen", 1, 1, FEARG_1, NULL,
1546 ret_number, f_strlen},
1547 {"strpart", 2, 4, FEARG_1, NULL,
1548 ret_string, f_strpart},
1549 {"strptime", 2, 2, FEARG_1, NULL,
1550 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001551#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001552 f_strptime
1553#else
1554 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001555#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001556 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001557 {"strridx", 2, 3, FEARG_1, NULL,
1558 ret_number, f_strridx},
1559 {"strtrans", 1, 1, FEARG_1, NULL,
1560 ret_string, f_strtrans},
1561 {"strwidth", 1, 1, FEARG_1, NULL,
1562 ret_number, f_strwidth},
1563 {"submatch", 1, 2, FEARG_1, NULL,
1564 ret_string, f_submatch},
1565 {"substitute", 4, 4, FEARG_1, NULL,
1566 ret_string, f_substitute},
1567 {"swapinfo", 1, 1, FEARG_1, NULL,
1568 ret_dict_any, f_swapinfo},
1569 {"swapname", 1, 1, FEARG_1, NULL,
1570 ret_string, f_swapname},
1571 {"synID", 3, 3, 0, NULL,
1572 ret_number, f_synID},
1573 {"synIDattr", 2, 3, FEARG_1, NULL,
1574 ret_string, f_synIDattr},
1575 {"synIDtrans", 1, 1, FEARG_1, NULL,
1576 ret_number, f_synIDtrans},
1577 {"synconcealed", 2, 2, 0, NULL,
1578 ret_list_any, f_synconcealed},
1579 {"synstack", 2, 2, 0, NULL,
1580 ret_list_number, f_synstack},
1581 {"system", 1, 2, FEARG_1, NULL,
1582 ret_string, f_system},
1583 {"systemlist", 1, 2, FEARG_1, NULL,
1584 ret_list_string, f_systemlist},
1585 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1586 ret_list_number, f_tabpagebuflist},
1587 {"tabpagenr", 0, 1, 0, NULL,
1588 ret_number, f_tabpagenr},
1589 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1590 ret_number, f_tabpagewinnr},
1591 {"tagfiles", 0, 0, 0, NULL,
1592 ret_list_string, f_tagfiles},
1593 {"taglist", 1, 2, FEARG_1, NULL,
1594 ret_list_dict_any, f_taglist},
1595 {"tan", 1, 1, FEARG_1, NULL,
1596 ret_float, FLOAT_FUNC(f_tan)},
1597 {"tanh", 1, 1, FEARG_1, NULL,
1598 ret_float, FLOAT_FUNC(f_tanh)},
1599 {"tempname", 0, 0, 0, NULL,
1600 ret_string, f_tempname},
1601 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1602 ret_number, TERM_FUNC(f_term_dumpdiff)},
1603 {"term_dumpload", 1, 2, FEARG_1, NULL,
1604 ret_number, TERM_FUNC(f_term_dumpload)},
1605 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1606 ret_void, TERM_FUNC(f_term_dumpwrite)},
1607 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1608 ret_number, TERM_FUNC(f_term_getaltscreen)},
1609 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1610 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001611#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001612 f_term_getansicolors
1613#else
1614 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001615#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001616 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001617 {"term_getattr", 2, 2, FEARG_1, NULL,
1618 ret_number, TERM_FUNC(f_term_getattr)},
1619 {"term_getcursor", 1, 1, FEARG_1, NULL,
1620 ret_list_any, TERM_FUNC(f_term_getcursor)},
1621 {"term_getjob", 1, 1, FEARG_1, NULL,
1622 ret_job, TERM_FUNC(f_term_getjob)},
1623 {"term_getline", 2, 2, FEARG_1, NULL,
1624 ret_string, TERM_FUNC(f_term_getline)},
1625 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1626 ret_number, TERM_FUNC(f_term_getscrolled)},
1627 {"term_getsize", 1, 1, FEARG_1, NULL,
1628 ret_list_number, TERM_FUNC(f_term_getsize)},
1629 {"term_getstatus", 1, 1, FEARG_1, NULL,
1630 ret_string, TERM_FUNC(f_term_getstatus)},
1631 {"term_gettitle", 1, 1, FEARG_1, NULL,
1632 ret_string, TERM_FUNC(f_term_gettitle)},
1633 {"term_gettty", 1, 2, FEARG_1, NULL,
1634 ret_string, TERM_FUNC(f_term_gettty)},
1635 {"term_list", 0, 0, 0, NULL,
1636 ret_list_number, TERM_FUNC(f_term_list)},
1637 {"term_scrape", 2, 2, FEARG_1, NULL,
1638 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1639 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1640 ret_void, TERM_FUNC(f_term_sendkeys)},
1641 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1642 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001643#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001644 f_term_setansicolors
1645#else
1646 NULL
1647#endif
1648 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001649 {"term_setapi", 2, 2, FEARG_1, NULL,
1650 ret_void, TERM_FUNC(f_term_setapi)},
1651 {"term_setkill", 2, 2, FEARG_1, NULL,
1652 ret_void, TERM_FUNC(f_term_setkill)},
1653 {"term_setrestore", 2, 2, FEARG_1, NULL,
1654 ret_void, TERM_FUNC(f_term_setrestore)},
1655 {"term_setsize", 3, 3, FEARG_1, NULL,
1656 ret_void, TERM_FUNC(f_term_setsize)},
1657 {"term_start", 1, 2, FEARG_1, NULL,
1658 ret_number, TERM_FUNC(f_term_start)},
1659 {"term_wait", 1, 2, FEARG_1, NULL,
1660 ret_void, TERM_FUNC(f_term_wait)},
1661 {"terminalprops", 0, 0, 0, NULL,
1662 ret_dict_string, f_terminalprops},
1663 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1664 ret_void, f_test_alloc_fail},
1665 {"test_autochdir", 0, 0, 0, NULL,
1666 ret_void, f_test_autochdir},
1667 {"test_feedinput", 1, 1, FEARG_1, NULL,
1668 ret_void, f_test_feedinput},
1669 {"test_garbagecollect_now", 0, 0, 0, NULL,
1670 ret_void, f_test_garbagecollect_now},
1671 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1672 ret_void, f_test_garbagecollect_soon},
1673 {"test_getvalue", 1, 1, FEARG_1, NULL,
1674 ret_number, f_test_getvalue},
1675 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1676 ret_void, f_test_ignore_error},
1677 {"test_null_blob", 0, 0, 0, NULL,
1678 ret_blob, f_test_null_blob},
1679 {"test_null_channel", 0, 0, 0, NULL,
1680 ret_channel, JOB_FUNC(f_test_null_channel)},
1681 {"test_null_dict", 0, 0, 0, NULL,
1682 ret_dict_any, f_test_null_dict},
1683 {"test_null_function", 0, 0, 0, NULL,
1684 ret_func_any, f_test_null_function},
1685 {"test_null_job", 0, 0, 0, NULL,
1686 ret_job, JOB_FUNC(f_test_null_job)},
1687 {"test_null_list", 0, 0, 0, NULL,
1688 ret_list_any, f_test_null_list},
1689 {"test_null_partial", 0, 0, 0, NULL,
1690 ret_func_any, f_test_null_partial},
1691 {"test_null_string", 0, 0, 0, NULL,
1692 ret_string, f_test_null_string},
1693 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1694 ret_void, f_test_option_not_set},
1695 {"test_override", 2, 2, FEARG_2, NULL,
1696 ret_void, f_test_override},
1697 {"test_refcount", 1, 1, FEARG_1, NULL,
1698 ret_number, f_test_refcount},
1699 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1700 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001701#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001702 f_test_scrollbar
1703#else
1704 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001705#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001706 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001707 {"test_setmouse", 2, 2, 0, NULL,
1708 ret_void, f_test_setmouse},
1709 {"test_settime", 1, 1, FEARG_1, NULL,
1710 ret_void, f_test_settime},
1711 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1712 ret_void, f_test_srand_seed},
1713 {"test_unknown", 0, 0, 0, NULL,
1714 ret_any, f_test_unknown},
1715 {"test_void", 0, 0, 0, NULL,
1716 ret_void, f_test_void},
1717 {"timer_info", 0, 1, FEARG_1, NULL,
1718 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1719 {"timer_pause", 2, 2, FEARG_1, NULL,
1720 ret_void, TIMER_FUNC(f_timer_pause)},
1721 {"timer_start", 2, 3, FEARG_1, NULL,
1722 ret_number, TIMER_FUNC(f_timer_start)},
1723 {"timer_stop", 1, 1, FEARG_1, NULL,
1724 ret_void, TIMER_FUNC(f_timer_stop)},
1725 {"timer_stopall", 0, 0, 0, NULL,
1726 ret_void, TIMER_FUNC(f_timer_stopall)},
1727 {"tolower", 1, 1, FEARG_1, NULL,
1728 ret_string, f_tolower},
1729 {"toupper", 1, 1, FEARG_1, NULL,
1730 ret_string, f_toupper},
1731 {"tr", 3, 3, FEARG_1, NULL,
1732 ret_string, f_tr},
1733 {"trim", 1, 3, FEARG_1, NULL,
1734 ret_string, f_trim},
1735 {"trunc", 1, 1, FEARG_1, NULL,
1736 ret_float, FLOAT_FUNC(f_trunc)},
1737 {"type", 1, 1, FEARG_1, NULL,
1738 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001739 {"typename", 1, 1, FEARG_1, NULL,
1740 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001741 {"undofile", 1, 1, FEARG_1, NULL,
1742 ret_string, f_undofile},
1743 {"undotree", 0, 0, 0, NULL,
1744 ret_dict_any, f_undotree},
1745 {"uniq", 1, 3, FEARG_1, NULL,
1746 ret_list_any, f_uniq},
1747 {"values", 1, 1, FEARG_1, NULL,
1748 ret_list_any, f_values},
1749 {"virtcol", 1, 1, FEARG_1, NULL,
1750 ret_number, f_virtcol},
1751 {"visualmode", 0, 1, 0, NULL,
1752 ret_string, f_visualmode},
1753 {"wildmenumode", 0, 0, 0, NULL,
1754 ret_number, f_wildmenumode},
1755 {"win_execute", 2, 3, FEARG_2, NULL,
1756 ret_string, f_win_execute},
1757 {"win_findbuf", 1, 1, FEARG_1, NULL,
1758 ret_list_number, f_win_findbuf},
1759 {"win_getid", 0, 2, FEARG_1, NULL,
1760 ret_number, f_win_getid},
1761 {"win_gettype", 0, 1, FEARG_1, NULL,
1762 ret_string, f_win_gettype},
1763 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001764 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001765 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1766 ret_list_number, f_win_id2tabwin},
1767 {"win_id2win", 1, 1, FEARG_1, NULL,
1768 ret_number, f_win_id2win},
1769 {"win_screenpos", 1, 1, FEARG_1, NULL,
1770 ret_list_number, f_win_screenpos},
1771 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001772 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773 {"winbufnr", 1, 1, FEARG_1, NULL,
1774 ret_number, f_winbufnr},
1775 {"wincol", 0, 0, 0, NULL,
1776 ret_number, f_wincol},
1777 {"windowsversion", 0, 0, 0, NULL,
1778 ret_string, f_windowsversion},
1779 {"winheight", 1, 1, FEARG_1, NULL,
1780 ret_number, f_winheight},
1781 {"winlayout", 0, 1, FEARG_1, NULL,
1782 ret_list_any, f_winlayout},
1783 {"winline", 0, 0, 0, NULL,
1784 ret_number, f_winline},
1785 {"winnr", 0, 1, FEARG_1, NULL,
1786 ret_number, f_winnr},
1787 {"winrestcmd", 0, 0, 0, NULL,
1788 ret_string, f_winrestcmd},
1789 {"winrestview", 1, 1, FEARG_1, NULL,
1790 ret_void, f_winrestview},
1791 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001792 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001793 {"winwidth", 1, 1, FEARG_1, NULL,
1794 ret_number, f_winwidth},
1795 {"wordcount", 0, 0, 0, NULL,
1796 ret_dict_number, f_wordcount},
1797 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001798 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001799 {"xor", 2, 2, FEARG_1, NULL,
1800 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001801};
1802
K.Takataeecf2b32021-06-02 14:56:39 +02001803#if defined(EBCDIC) || defined(PROTO)
1804/*
1805 * Compare funcentry_T by function name.
1806 */
1807 static int
1808compare_func_name(const void *s1, const void *s2)
1809{
1810 funcentry_T *p1 = (funcentry_T *)s1;
1811 funcentry_T *p2 = (funcentry_T *)s2;
1812
1813 return STRCMP(p1->f_name, p2->f_name);
1814}
1815
1816/*
1817 * Sort the function table by function name.
1818 * The sorting of the table above is ASCII dependent.
1819 * On machines using EBCDIC we have to sort it.
1820 */
1821 void
1822sortFunctions(void)
1823{
1824 size_t funcCnt = ARRAY_LENGTH(global_functions);
1825
1826 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1827}
1828#endif
1829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001830/*
1831 * Function given to ExpandGeneric() to obtain the list of internal
1832 * or user defined function names.
1833 */
1834 char_u *
1835get_function_name(expand_T *xp, int idx)
1836{
1837 static int intidx = -1;
1838 char_u *name;
1839
1840 if (idx == 0)
1841 intidx = -1;
1842 if (intidx < 0)
1843 {
1844 name = get_user_func_name(xp, idx);
1845 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001846 {
1847 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1848 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 }
K.Takataeeec2542021-06-02 13:28:16 +02001852 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, ")");
1858 return IObuff;
1859 }
1860
1861 return NULL;
1862}
1863
1864/*
1865 * Function given to ExpandGeneric() to obtain the list of internal or
1866 * user defined variable or function names.
1867 */
1868 char_u *
1869get_expr_name(expand_T *xp, int idx)
1870{
1871 static int intidx = -1;
1872 char_u *name;
1873
1874 if (idx == 0)
1875 intidx = -1;
1876 if (intidx < 0)
1877 {
1878 name = get_function_name(xp, idx);
1879 if (name != NULL)
1880 return name;
1881 }
1882 return get_user_var_name(xp, ++intidx);
1883}
1884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001886 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001887 * Return index, or -1 if not found or "implemented" is TRUE and the function
1888 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001890 static int
1891find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892{
1893 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001894 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 int cmp;
1896 int x;
1897
K.Takataeeec2542021-06-02 13:28:16 +02001898 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001899
1900 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 while (first <= last)
1902 {
1903 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001904 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (cmp < 0)
1906 last = x - 1;
1907 else if (cmp > 0)
1908 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001909 else if (implemented && global_functions[x].f_func == NULL)
1910 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 else
1912 return x;
1913 }
1914 return -1;
1915}
1916
Bram Moolenaar15c47602020-03-26 22:16:48 +01001917/*
1918 * Find internal function "name" in table "global_functions".
1919 * Return index, or -1 if not found or the function is not implemented.
1920 */
1921 int
1922find_internal_func(char_u *name)
1923{
1924 return find_internal_func_opt(name, TRUE);
1925}
1926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001928has_internal_func(char_u *name)
1929{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 return find_internal_func_opt(name, TRUE) >= 0;
1931}
1932
1933 static int
1934has_internal_func_name(char_u *name)
1935{
1936 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001937}
1938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 char *
1940internal_func_name(int idx)
1941{
1942 return global_functions[idx].f_name;
1943}
1944
Bram Moolenaar94738d82020-10-21 14:25:07 +02001945/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001946 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001947 * Uses the list of types on the type stack: "types".
1948 * Return FAIL and gives an error message when a type is wrong.
1949 */
1950 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001951internal_func_check_arg_types(
1952 type_T **types,
1953 int idx,
1954 int argcount,
1955 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001956{
1957 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1958 int i;
1959
1960 if (argchecks != NULL)
1961 {
1962 argcontext_T context;
1963
1964 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001965 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001966 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001967 for (i = 0; i < argcount; ++i)
1968 if (argchecks[i] != NULL)
1969 {
1970 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001971 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001972 return FAIL;
1973 }
1974 }
1975 return OK;
1976}
1977
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001978/*
1979 * Call the "f_retfunc" function to obtain the return type of function "idx".
1980 * "argtypes" is the list of argument types or NULL when there are no
1981 * arguments.
1982 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001984internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987}
1988
1989/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001990 * Return TRUE if "idx" is for the map() function.
1991 */
1992 int
1993internal_func_is_map(int idx)
1994{
1995 return global_functions[idx].f_func == f_map;
1996}
1997
1998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002000 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2001 * first argument, 2 if method base is second argument, etc. 9 if method base
2002 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 int
2005check_internal_func(int idx, int argcount)
2006{
2007 int res;
2008 char *name;
2009
2010 if (argcount < global_functions[idx].f_min_argc)
2011 res = FCERR_TOOFEW;
2012 else if (argcount > global_functions[idx].f_max_argc)
2013 res = FCERR_TOOMANY;
2014 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002015 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
2017 name = internal_func_name(idx);
2018 if (res == FCERR_TOOMANY)
2019 semsg(_(e_toomanyarg), name);
2020 else
2021 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002022 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023}
2024
Bram Moolenaarac92e252019-08-03 21:58:38 +02002025 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026call_internal_func(
2027 char_u *name,
2028 int argcount,
2029 typval_T *argvars,
2030 typval_T *rettv)
2031{
2032 int i;
2033
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002034 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002036 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002037 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002042 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002043 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044}
2045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 void
2047call_internal_func_by_idx(
2048 int idx,
2049 typval_T *argvars,
2050 typval_T *rettv)
2051{
2052 global_functions[idx].f_func(argvars, rettv);
2053}
2054
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055/*
2056 * Invoke a method for base->method().
2057 */
2058 int
2059call_internal_method(
2060 char_u *name,
2061 int argcount,
2062 typval_T *argvars,
2063 typval_T *rettv,
2064 typval_T *basetv)
2065{
2066 int i;
2067 int fi;
2068 typval_T argv[MAX_FUNC_ARGS + 1];
2069
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002070 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002071 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002072 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002075 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002079
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002080 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002081 {
2082 // base value goes last
2083 for (i = 0; i < argcount; ++i)
2084 argv[i] = argvars[i];
2085 argv[argcount] = *basetv;
2086 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002087 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002088 {
2089 // base value goes second
2090 argv[0] = argvars[0];
2091 argv[1] = *basetv;
2092 for (i = 1; i < argcount; ++i)
2093 argv[i + 1] = argvars[i];
2094 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002095 else if (global_functions[fi].f_argtype == FEARG_3)
2096 {
2097 // base value goes third
2098 argv[0] = argvars[0];
2099 argv[1] = argvars[1];
2100 argv[2] = *basetv;
2101 for (i = 2; i < argcount; ++i)
2102 argv[i + 1] = argvars[i];
2103 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002104 else if (global_functions[fi].f_argtype == FEARG_4)
2105 {
2106 // base value goes fourth
2107 argv[0] = argvars[0];
2108 argv[1] = argvars[1];
2109 argv[2] = argvars[2];
2110 argv[3] = *basetv;
2111 for (i = 3; i < argcount; ++i)
2112 argv[i + 1] = argvars[i];
2113 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 else
2115 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002116 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002117 argv[0] = *basetv;
2118 for (i = 0; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002121 argv[argcount + 1].v_type = VAR_UNKNOWN;
2122
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002123 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002124 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125}
2126
2127/*
2128 * Return TRUE for a non-zero Number and a non-empty String.
2129 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002130 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131non_zero_arg(typval_T *argvars)
2132{
2133 return ((argvars[0].v_type == VAR_NUMBER
2134 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002135 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 && argvars[0].vval.v_number == VVAL_TRUE)
2137 || (argvars[0].v_type == VAR_STRING
2138 && argvars[0].vval.v_string != NULL
2139 && *argvars[0].vval.v_string != NUL));
2140}
2141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 * "and(expr, expr)" function
2144 */
2145 static void
2146f_and(typval_T *argvars, typval_T *rettv)
2147{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2149 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002150}
2151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002153 * "balloon_show()" function
2154 */
2155#ifdef FEAT_BEVAL
2156 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002157f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2158{
2159 rettv->v_type = VAR_STRING;
2160 if (balloonEval != NULL)
2161 {
2162 if (balloonEval->msg == NULL)
2163 rettv->vval.v_string = NULL;
2164 else
2165 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2166 }
2167}
2168
2169 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002170f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2171{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002172 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002173 {
2174 if (argvars[0].v_type == VAR_LIST
2175# ifdef FEAT_GUI
2176 && !gui.in_use
2177# endif
2178 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002179 {
2180 list_T *l = argvars[0].vval.v_list;
2181
2182 // empty list removes the balloon
2183 post_balloon(balloonEval, NULL,
2184 l == NULL || l->lv_len == 0 ? NULL : l);
2185 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002186 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002187 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002188 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002189
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002190 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2191 return;
2192
2193 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002194 if (mesg != NULL)
2195 // empty string removes the balloon
2196 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2197 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002198 }
2199}
2200
Bram Moolenaar669a8282017-11-19 20:13:05 +01002201# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002202 static void
2203f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2204{
2205 if (rettv_list_alloc(rettv) == OK)
2206 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002207 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002208
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002209 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2210 return;
2211 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002212 if (msg != NULL)
2213 {
2214 pumitem_T *array;
2215 int size = split_message(msg, &array);
2216 int i;
2217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002218 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002219 for (i = 1; i < size - 1; ++i)
2220 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002221 while (size > 0)
2222 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002223 vim_free(array);
2224 }
2225 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002226}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002227# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002228#endif
2229
2230/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002231 * Get the buffer from "arg" and give an error and return NULL if it is not
2232 * valid.
2233 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002234 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002235get_buf_arg(typval_T *arg)
2236{
2237 buf_T *buf;
2238
2239 ++emsg_off;
2240 buf = tv_get_buf(arg, FALSE);
2241 --emsg_off;
2242 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002244 return buf;
2245}
2246
2247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002248 * "byte2line(byte)" function
2249 */
2250 static void
2251f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2252{
2253#ifndef FEAT_BYTEOFF
2254 rettv->vval.v_number = -1;
2255#else
2256 long boff = 0;
2257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002258 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 if (boff < 0)
2260 rettv->vval.v_number = -1;
2261 else
2262 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2263 (linenr_T)0, &boff);
2264#endif
2265}
2266
2267 static void
2268byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2269{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 char_u *str;
2272 varnumber_T idx;
2273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002274 str = tv_get_string_chk(&argvars[0]);
2275 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276 rettv->vval.v_number = -1;
2277 if (str == NULL || idx < 0)
2278 return;
2279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 t = str;
2281 for ( ; idx > 0; idx--)
2282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002283 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284 return;
2285 if (enc_utf8 && comp)
2286 t += utf_ptr2len(t);
2287 else
2288 t += (*mb_ptr2len)(t);
2289 }
2290 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002291}
2292
2293/*
2294 * "byteidx()" function
2295 */
2296 static void
2297f_byteidx(typval_T *argvars, typval_T *rettv)
2298{
2299 byteidx(argvars, rettv, FALSE);
2300}
2301
2302/*
2303 * "byteidxcomp()" function
2304 */
2305 static void
2306f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2307{
2308 byteidx(argvars, rettv, TRUE);
2309}
2310
2311/*
2312 * "call(func, arglist [, dict])" function
2313 */
2314 static void
2315f_call(typval_T *argvars, typval_T *rettv)
2316{
2317 char_u *func;
2318 partial_T *partial = NULL;
2319 dict_T *selfdict = NULL;
2320
2321 if (argvars[1].v_type != VAR_LIST)
2322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002324 return;
2325 }
2326 if (argvars[1].vval.v_list == NULL)
2327 return;
2328
2329 if (argvars[0].v_type == VAR_FUNC)
2330 func = argvars[0].vval.v_string;
2331 else if (argvars[0].v_type == VAR_PARTIAL)
2332 {
2333 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002334 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335 }
2336 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002337 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002338 if (func == NULL || *func == NUL)
2339 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002340
2341 if (argvars[2].v_type != VAR_UNKNOWN)
2342 {
2343 if (argvars[2].v_type != VAR_DICT)
2344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346 return;
2347 }
2348 selfdict = argvars[2].vval.v_dict;
2349 }
2350
2351 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2352}
2353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354/*
2355 * "changenr()" function
2356 */
2357 static void
2358f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2359{
2360 rettv->vval.v_number = curbuf->b_u_seq_cur;
2361}
2362
2363/*
2364 * "char2nr(string)" function
2365 */
2366 static void
2367f_char2nr(typval_T *argvars, typval_T *rettv)
2368{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002369 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2370 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (has_mbyte)
2372 {
2373 int utf8 = 0;
2374
2375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002376 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377
2378 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002379 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002381 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 }
2383 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002384 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385}
2386
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002387/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002388 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2389 * returns the character index of the column. Otherwise, returns the byte index
2390 * of the column.
2391 */
2392 static void
2393get_col(typval_T *argvars, typval_T *rettv, int charcol)
2394{
2395 colnr_T col = 0;
2396 pos_T *fp;
2397 int fnum = curbuf->b_fnum;
2398
2399 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2400 if (fp != NULL && fnum == curbuf->b_fnum)
2401 {
2402 if (fp->col == MAXCOL)
2403 {
2404 // '> can be MAXCOL, get the length of the line then
2405 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2406 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2407 else
2408 col = MAXCOL;
2409 }
2410 else
2411 {
2412 col = fp->col + 1;
2413 // col(".") when the cursor is on the NUL at the end of the line
2414 // because of "coladd" can be seen as an extra column.
2415 if (virtual_active() && fp == &curwin->w_cursor)
2416 {
2417 char_u *p = ml_get_cursor();
2418
2419 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2420 curwin->w_virtcol - curwin->w_cursor.coladd))
2421 {
2422 int l;
2423
2424 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2425 col += l;
2426 }
2427 }
2428 }
2429 }
2430 rettv->vval.v_number = col;
2431}
2432
2433/*
2434 * "charcol()" function
2435 */
2436 static void
2437f_charcol(typval_T *argvars, typval_T *rettv)
2438{
2439 get_col(argvars, rettv, TRUE);
2440}
2441
2442/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002443 * "charidx()" function
2444 */
2445 static void
2446f_charidx(typval_T *argvars, typval_T *rettv)
2447{
2448 char_u *str;
2449 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002450 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002451 char_u *p;
2452 int len;
2453 int (*ptr2len)(char_u *);
2454
2455 rettv->vval.v_number = -1;
2456
2457 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2458 || (argvars[2].v_type != VAR_UNKNOWN
2459 && argvars[2].v_type != VAR_NUMBER))
2460 {
2461 emsg(_(e_invarg));
2462 return;
2463 }
2464
2465 str = tv_get_string_chk(&argvars[0]);
2466 idx = tv_get_number_chk(&argvars[1], NULL);
2467 if (str == NULL || idx < 0)
2468 return;
2469
2470 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002471 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002472 if (countcc < 0 || countcc > 1)
2473 {
2474 semsg(_(e_using_number_as_bool_nr), countcc);
2475 return;
2476 }
2477
2478 if (enc_utf8 && countcc)
2479 ptr2len = utf_ptr2len;
2480 else
2481 ptr2len = mb_ptr2len;
2482
2483 for (p = str, len = 0; p <= str + idx; len++)
2484 {
2485 if (*p == NUL)
2486 return;
2487 p += ptr2len(p);
2488 }
2489
2490 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2491}
2492
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002493 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002494get_optional_window(typval_T *argvars, int idx)
2495{
2496 win_T *win = curwin;
2497
2498 if (argvars[idx].v_type != VAR_UNKNOWN)
2499 {
2500 win = find_win_by_nr_or_id(&argvars[idx]);
2501 if (win == NULL)
2502 {
2503 emsg(_(e_invalwindow));
2504 return NULL;
2505 }
2506 }
2507 return win;
2508}
2509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511 * "col(string)" function
2512 */
2513 static void
2514f_col(typval_T *argvars, typval_T *rettv)
2515{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002516 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517}
2518
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519/*
2520 * "confirm(message, buttons[, default [, type]])" function
2521 */
2522 static void
2523f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2524{
2525#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2526 char_u *message;
2527 char_u *buttons = NULL;
2528 char_u buf[NUMBUFLEN];
2529 char_u buf2[NUMBUFLEN];
2530 int def = 1;
2531 int type = VIM_GENERIC;
2532 char_u *typestr;
2533 int error = FALSE;
2534
Bram Moolenaarc5809432021-03-27 21:23:30 +01002535 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2536 return;
2537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 if (message == NULL)
2540 error = TRUE;
2541 if (argvars[1].v_type != VAR_UNKNOWN)
2542 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002543 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2544 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002545 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546 if (buttons == NULL)
2547 error = TRUE;
2548 if (argvars[2].v_type != VAR_UNKNOWN)
2549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002550 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 if (argvars[3].v_type != VAR_UNKNOWN)
2552 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002553 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2554 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002555 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 if (typestr == NULL)
2557 error = TRUE;
2558 else
2559 {
2560 switch (TOUPPER_ASC(*typestr))
2561 {
2562 case 'E': type = VIM_ERROR; break;
2563 case 'Q': type = VIM_QUESTION; break;
2564 case 'I': type = VIM_INFO; break;
2565 case 'W': type = VIM_WARNING; break;
2566 case 'G': type = VIM_GENERIC; break;
2567 }
2568 }
2569 }
2570 }
2571 }
2572
2573 if (buttons == NULL || *buttons == NUL)
2574 buttons = (char_u *)_("&Ok");
2575
2576 if (!error)
2577 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2578 def, NULL, FALSE);
2579#endif
2580}
2581
2582/*
2583 * "copy()" function
2584 */
2585 static void
2586f_copy(typval_T *argvars, typval_T *rettv)
2587{
2588 item_copy(&argvars[0], rettv, FALSE, 0);
2589}
2590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002592 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002593 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002594 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002595 */
2596 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002597set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598{
2599 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002600 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002601 int set_curswant = TRUE;
2602
2603 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002604 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002605 {
2606 pos_T pos;
2607 colnr_T curswant = -1;
2608
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002609 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002611 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 return;
2613 }
2614 line = pos.lnum;
2615 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617 if (curswant >= 0)
2618 {
2619 curwin->w_curswant = curswant - 1;
2620 set_curswant = FALSE;
2621 }
2622 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002623 else if ((argvars[0].v_type == VAR_NUMBER ||
2624 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002625 && (argvars[1].v_type == VAR_NUMBER ||
2626 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002629 if (line < 0)
2630 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002631 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002632 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002633 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002635 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002637 else
2638 {
2639 emsg(_(e_invarg));
2640 return;
2641 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002642 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002643 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 if (line > 0)
2645 curwin->w_cursor.lnum = line;
2646 if (col > 0)
2647 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002650 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 if (has_mbyte)
2654 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655
2656 curwin->w_set_curswant = set_curswant;
2657 rettv->vval.v_number = 0;
2658}
2659
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002660/*
2661 * "cursor(lnum, col)" function, or
2662 * "cursor(list)"
2663 *
2664 * Moves the cursor to the specified line and column.
2665 * Returns 0 when the position could be set, -1 otherwise.
2666 */
2667 static void
2668f_cursor(typval_T *argvars, typval_T *rettv)
2669{
2670 set_cursorpos(argvars, rettv, FALSE);
2671}
2672
Bram Moolenaar4f974752019-02-17 17:44:42 +01002673#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002674/*
2675 * "debugbreak()" function
2676 */
2677 static void
2678f_debugbreak(typval_T *argvars, typval_T *rettv)
2679{
2680 int pid;
2681
2682 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002684 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002685 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002686 else
2687 {
2688 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2689
2690 if (hProcess != NULL)
2691 {
2692 DebugBreakProcess(hProcess);
2693 CloseHandle(hProcess);
2694 rettv->vval.v_number = OK;
2695 }
2696 }
2697}
2698#endif
2699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700/*
2701 * "deepcopy()" function
2702 */
2703 static void
2704f_deepcopy(typval_T *argvars, typval_T *rettv)
2705{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002706 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 int copyID;
2708
2709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002710 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002712 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 else
2714 {
2715 copyID = get_copyID();
2716 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2717 }
2718}
2719
2720/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 * "did_filetype()" function
2722 */
2723 static void
2724f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2725{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727}
2728
2729/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002730 * "echoraw({expr})" function
2731 */
2732 static void
2733f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2734{
2735 char_u *str = tv_get_string_chk(&argvars[0]);
2736
2737 if (str != NULL && *str != NUL)
2738 {
2739 out_str(str);
2740 out_flush();
2741 }
2742}
2743
2744/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 * "empty({expr})" function
2746 */
2747 static void
2748f_empty(typval_T *argvars, typval_T *rettv)
2749{
2750 int n = FALSE;
2751
2752 switch (argvars[0].v_type)
2753 {
2754 case VAR_STRING:
2755 case VAR_FUNC:
2756 n = argvars[0].vval.v_string == NULL
2757 || *argvars[0].vval.v_string == NUL;
2758 break;
2759 case VAR_PARTIAL:
2760 n = FALSE;
2761 break;
2762 case VAR_NUMBER:
2763 n = argvars[0].vval.v_number == 0;
2764 break;
2765 case VAR_FLOAT:
2766#ifdef FEAT_FLOAT
2767 n = argvars[0].vval.v_float == 0.0;
2768 break;
2769#endif
2770 case VAR_LIST:
2771 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002772 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 break;
2774 case VAR_DICT:
2775 n = argvars[0].vval.v_dict == NULL
2776 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2777 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002778 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 case VAR_SPECIAL:
2780 n = argvars[0].vval.v_number != VVAL_TRUE;
2781 break;
2782
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002783 case VAR_BLOB:
2784 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002785 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2786 break;
2787
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 case VAR_JOB:
2789#ifdef FEAT_JOB_CHANNEL
2790 n = argvars[0].vval.v_job == NULL
2791 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2792 break;
2793#endif
2794 case VAR_CHANNEL:
2795#ifdef FEAT_JOB_CHANNEL
2796 n = argvars[0].vval.v_channel == NULL
2797 || !channel_is_open(argvars[0].vval.v_channel);
2798 break;
2799#endif
2800 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002801 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002803 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002804 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 n = TRUE;
2806 break;
2807 }
2808
2809 rettv->vval.v_number = n;
2810}
2811
2812/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002813 * "environ()" function
2814 */
2815 static void
2816f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2817{
2818#if !defined(AMIGA)
2819 int i = 0;
2820 char_u *entry, *value;
2821# ifdef MSWIN
2822 extern wchar_t **_wenviron;
2823# else
2824 extern char **environ;
2825# endif
2826
2827 if (rettv_dict_alloc(rettv) != OK)
2828 return;
2829
2830# ifdef MSWIN
2831 if (*_wenviron == NULL)
2832 return;
2833# else
2834 if (*environ == NULL)
2835 return;
2836# endif
2837
2838 for (i = 0; ; ++i)
2839 {
2840# ifdef MSWIN
2841 short_u *p;
2842
2843 if ((p = (short_u *)_wenviron[i]) == NULL)
2844 return;
2845 entry = utf16_to_enc(p, NULL);
2846# else
2847 if ((entry = (char_u *)environ[i]) == NULL)
2848 return;
2849 entry = vim_strsave(entry);
2850# endif
2851 if (entry == NULL) // out of memory
2852 return;
2853 if ((value = vim_strchr(entry, '=')) == NULL)
2854 {
2855 vim_free(entry);
2856 continue;
2857 }
2858 *value++ = NUL;
2859 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2860 vim_free(entry);
2861 }
2862#endif
2863}
2864
2865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 * "escape({string}, {chars})" function
2867 */
2868 static void
2869f_escape(typval_T *argvars, typval_T *rettv)
2870{
2871 char_u buf[NUMBUFLEN];
2872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002873 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2874 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 rettv->v_type = VAR_STRING;
2876}
2877
2878/*
2879 * "eval()" function
2880 */
2881 static void
2882f_eval(typval_T *argvars, typval_T *rettv)
2883{
2884 char_u *s, *p;
2885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002886 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 if (s != NULL)
2888 s = skipwhite(s);
2889
2890 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 {
2893 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002894 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895 need_clr_eos = FALSE;
2896 rettv->v_type = VAR_NUMBER;
2897 rettv->vval.v_number = 0;
2898 }
2899 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002900 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901}
2902
2903/*
2904 * "eventhandler()" function
2905 */
2906 static void
2907f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2908{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002909 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910}
2911
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912static garray_T redir_execute_ga;
2913
2914/*
2915 * Append "value[value_len]" to the execute() output.
2916 */
2917 void
2918execute_redir_str(char_u *value, int value_len)
2919{
2920 int len;
2921
2922 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002923 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 if (ga_grow(&redir_execute_ga, len) == OK)
2927 {
2928 mch_memmove((char *)redir_execute_ga.ga_data
2929 + redir_execute_ga.ga_len, value, len);
2930 redir_execute_ga.ga_len += len;
2931 }
2932}
2933
2934/*
2935 * Get next line from a list.
2936 * Called by do_cmdline() to get the next line.
2937 * Returns allocated string, or NULL for end of function.
2938 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 static char_u *
2940get_list_line(
2941 int c UNUSED,
2942 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002943 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002944 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945{
2946 listitem_T **p = (listitem_T **)cookie;
2947 listitem_T *item = *p;
2948 char_u buf[NUMBUFLEN];
2949 char_u *s;
2950
2951 if (item == NULL)
2952 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002953 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 *p = item->li_next;
2955 return s == NULL ? NULL : vim_strsave(s);
2956}
2957
2958/*
2959 * "execute()" function
2960 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002961 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002962execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963{
2964 char_u *cmd = NULL;
2965 list_T *list = NULL;
2966 int save_msg_silent = msg_silent;
2967 int save_emsg_silent = emsg_silent;
2968 int save_emsg_noredir = emsg_noredir;
2969 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002970 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002972 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002973 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974
2975 rettv->vval.v_string = NULL;
2976 rettv->v_type = VAR_STRING;
2977
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002978 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002980 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002981 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002982 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 return;
2984 ++list->lv_refcount;
2985 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002986 else if (argvars[arg_off].v_type == VAR_JOB
2987 || argvars[arg_off].v_type == VAR_CHANNEL)
2988 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02002989 semsg(_(e_using_invalid_value_as_string_str),
2990 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002991 return;
2992 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 else
2994 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002995 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 if (cmd == NULL)
2997 return;
2998 }
2999
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003000 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 {
3002 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003003 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3004 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005
3006 if (s == NULL)
3007 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003008 if (*s == NUL)
3009 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 if (STRNCMP(s, "silent", 6) == 0)
3011 ++msg_silent;
3012 if (STRCMP(s, "silent!") == 0)
3013 {
3014 emsg_silent = TRUE;
3015 emsg_noredir = TRUE;
3016 }
3017 }
3018 else
3019 ++msg_silent;
3020
3021 if (redir_execute)
3022 save_ga = redir_execute_ga;
3023 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3024 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003025 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003026 if (!echo_output)
3027 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028
3029 if (cmd != NULL)
3030 do_cmdline_cmd(cmd);
3031 else
3032 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003033 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003035 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003036 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 do_cmdline(NULL, get_list_line, (void *)&item,
3038 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3039 --list->lv_refcount;
3040 }
3041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003042 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003043 if (ga_grow(&redir_execute_ga, 1) == OK)
3044 {
3045 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3046 rettv->vval.v_string = redir_execute_ga.ga_data;
3047 }
3048 else
3049 {
3050 ga_clear(&redir_execute_ga);
3051 rettv->vval.v_string = NULL;
3052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053 msg_silent = save_msg_silent;
3054 emsg_silent = save_emsg_silent;
3055 emsg_noredir = save_emsg_noredir;
3056
3057 redir_execute = save_redir_execute;
3058 if (redir_execute)
3059 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003060 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003061
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003062 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003063 if (echo_output)
3064 // When not working silently: put it in column zero. A following
3065 // "echon" will overwrite the message, unavoidably.
3066 msg_col = 0;
3067 else
3068 // When working silently: Put it back where it was, since nothing
3069 // should have been written.
3070 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003071}
3072
3073/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003074 * "execute()" function
3075 */
3076 static void
3077f_execute(typval_T *argvars, typval_T *rettv)
3078{
3079 execute_common(argvars, rettv, 0);
3080}
3081
3082/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 * "exists()" function
3084 */
3085 static void
3086f_exists(typval_T *argvars, typval_T *rettv)
3087{
3088 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003091 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003092 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003094 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095 if (mch_getenv(p + 1) != NULL)
3096 n = TRUE;
3097 else
3098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 p = expand_env_save(p);
3101 if (p != NULL && *p != '$')
3102 n = TRUE;
3103 vim_free(p);
3104 }
3105 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003106 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003108 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003110 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003112 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003114 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003116 else if (*p == '?') // internal function only
3117 {
3118 n = has_internal_func_name(p + 1);
3119 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120 else if (*p == ':')
3121 {
3122 n = cmd_exists(p + 1);
3123 }
3124 else if (*p == '#')
3125 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 if (p[1] == '#')
3127 n = autocmd_supported(p + 2);
3128 else
3129 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003131 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003133 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 }
3135
3136 rettv->vval.v_number = n;
3137}
3138
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139/*
3140 * "expand()" function
3141 */
3142 static void
3143f_expand(typval_T *argvars, typval_T *rettv)
3144{
3145 char_u *s;
3146 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003147 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003148 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3149 expand_T xpc;
3150 int error = FALSE;
3151 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003152#ifdef BACKSLASH_IN_FILENAME
3153 char_u *p_csl_save = p_csl;
3154
3155 // avoid using 'completeslash' here
3156 p_csl = empty_option;
3157#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158
3159 rettv->v_type = VAR_STRING;
3160 if (argvars[1].v_type != VAR_UNKNOWN
3161 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003162 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003163 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003164 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003166 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167 if (*s == '%' || *s == '#' || *s == '<')
3168 {
3169 ++emsg_off;
3170 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3171 --emsg_off;
3172 if (rettv->v_type == VAR_LIST)
3173 {
3174 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3175 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003176 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003177 }
3178 else
3179 rettv->vval.v_string = result;
3180 }
3181 else
3182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003183 // When the optional second argument is non-zero, don't remove matches
3184 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003185 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003186 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 options |= WILD_KEEP_ALL;
3188 if (!error)
3189 {
3190 ExpandInit(&xpc);
3191 xpc.xp_context = EXPAND_FILES;
3192 if (p_wic)
3193 options += WILD_ICASE;
3194 if (rettv->v_type == VAR_STRING)
3195 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3196 options, WILD_ALL);
3197 else if (rettv_list_alloc(rettv) != FAIL)
3198 {
3199 int i;
3200
3201 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3202 for (i = 0; i < xpc.xp_numfiles; i++)
3203 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3204 ExpandCleanup(&xpc);
3205 }
3206 }
3207 else
3208 rettv->vval.v_string = NULL;
3209 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003210#ifdef BACKSLASH_IN_FILENAME
3211 p_csl = p_csl_save;
3212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003213}
3214
3215/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003216 * "expandcmd()" function
3217 * Expand all the special characters in a command string.
3218 */
3219 static void
3220f_expandcmd(typval_T *argvars, typval_T *rettv)
3221{
3222 exarg_T eap;
3223 char_u *cmdstr;
3224 char *errormsg = NULL;
3225
3226 rettv->v_type = VAR_STRING;
3227 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3228
3229 memset(&eap, 0, sizeof(eap));
3230 eap.cmd = cmdstr;
3231 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003232 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003233 eap.usefilter = FALSE;
3234 eap.nextcmd = NULL;
3235 eap.cmdidx = CMD_USER;
3236
3237 expand_filename(&eap, &cmdstr, &errormsg);
3238 if (errormsg != NULL && *errormsg != NUL)
3239 emsg(errormsg);
3240
3241 rettv->vval.v_string = cmdstr;
3242}
3243
3244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245 * "feedkeys()" function
3246 */
3247 static void
3248f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3249{
3250 int remap = TRUE;
3251 int insert = FALSE;
3252 char_u *keys, *flags;
3253 char_u nbuf[NUMBUFLEN];
3254 int typed = FALSE;
3255 int execute = FALSE;
3256 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003257 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 char_u *keys_esc;
3259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 // This is not allowed in the sandbox. If the commands would still be
3261 // executed in the sandbox it would be OK, but it probably happens later,
3262 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 if (check_secure())
3264 return;
3265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003266 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003267
3268 if (argvars[1].v_type != VAR_UNKNOWN)
3269 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003270 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 for ( ; *flags != NUL; ++flags)
3272 {
3273 switch (*flags)
3274 {
3275 case 'n': remap = FALSE; break;
3276 case 'm': remap = TRUE; break;
3277 case 't': typed = TRUE; break;
3278 case 'i': insert = TRUE; break;
3279 case 'x': execute = TRUE; break;
3280 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003281 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 }
3283 }
3284 }
3285
3286 if (*keys != NUL || execute)
3287 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003288 // Need to escape K_SPECIAL and CSI before putting the string in the
3289 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 keys_esc = vim_strsave_escape_csi(keys);
3291 if (keys_esc != NULL)
3292 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003293 if (lowlevel)
3294 {
3295#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003296 int idx;
3297 int len = (int)STRLEN(keys);
3298
3299 for (idx = 0; idx < len; ++idx)
3300 {
3301 // if a CTRL-C was typed, set got_int, similar to what
3302 // happens in fill_input_buf()
3303 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3304 got_int = TRUE;
3305 add_to_input_buf(keys + idx, 1);
3306 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003307#else
3308 emsg(_("E980: lowlevel input not supported"));
3309#endif
3310 }
3311 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003312 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003313 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003315 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003316#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003317 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003318#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003319 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003320 typebuf_was_filled = TRUE;
3321 }
3322 vim_free(keys_esc);
3323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003324 if (execute)
3325 {
3326 int save_msg_scroll = msg_scroll;
3327
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329 msg_scroll = FALSE;
3330
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003331 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003332 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003333 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003334 ++in_feedkeys;
3335 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003336 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003337 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003338 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003339 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003340 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003341 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003342
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003343 msg_scroll |= save_msg_scroll;
3344 }
3345 }
3346 }
3347}
3348
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349/*
3350 * "fnameescape({string})" function
3351 */
3352 static void
3353f_fnameescape(typval_T *argvars, typval_T *rettv)
3354{
3355 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003356 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357 rettv->v_type = VAR_STRING;
3358}
3359
3360/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003361 * "foreground()" function
3362 */
3363 static void
3364f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3365{
3366#ifdef FEAT_GUI
3367 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003368 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003370 return;
3371 }
3372#endif
3373#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003375#endif
3376}
3377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003378 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003379common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380{
3381 char_u *s;
3382 char_u *name;
3383 int use_string = FALSE;
3384 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003385 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003386 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387
3388 if (argvars[0].v_type == VAR_FUNC)
3389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003390 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 s = argvars[0].vval.v_string;
3392 }
3393 else if (argvars[0].v_type == VAR_PARTIAL
3394 && argvars[0].vval.v_partial != NULL)
3395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003396 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003397 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003398 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 }
3400 else
3401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003402 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003403 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 use_string = TRUE;
3405 }
3406
Bram Moolenaar843b8842016-08-21 14:36:15 +02003407 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003408 {
3409 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003410 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003411 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3412 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003413 if (*name != NUL)
3414 s = NULL;
3415 }
3416
Bram Moolenaar843b8842016-08-21 14:36:15 +02003417 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3418 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003419 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003420 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003421 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003422 ? find_func(trans_name, is_global, NULL) == NULL
3423 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003424 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 else
3426 {
3427 int dict_idx = 0;
3428 int arg_idx = 0;
3429 list_T *list = NULL;
3430
3431 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3432 {
3433 char sid_buf[25];
3434 int off = *s == 's' ? 2 : 5;
3435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003436 // Expand s: and <SID> into <SNR>nr_, so that the function can
3437 // also be called from another script. Using trans_function_name()
3438 // would also work, but some plugins depend on the name being
3439 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003440 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003441 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442 if (name != NULL)
3443 {
3444 STRCPY(name, sid_buf);
3445 STRCAT(name, s + off);
3446 }
3447 }
3448 else
3449 name = vim_strsave(s);
3450
3451 if (argvars[1].v_type != VAR_UNKNOWN)
3452 {
3453 if (argvars[2].v_type != VAR_UNKNOWN)
3454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003455 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 arg_idx = 1;
3457 dict_idx = 2;
3458 }
3459 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003460 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461 dict_idx = 1;
3462 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003463 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 arg_idx = 1;
3465 if (dict_idx > 0)
3466 {
3467 if (argvars[dict_idx].v_type != VAR_DICT)
3468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003469 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003471 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 }
3473 if (argvars[dict_idx].vval.v_dict == NULL)
3474 dict_idx = 0;
3475 }
3476 if (arg_idx > 0)
3477 {
3478 if (argvars[arg_idx].v_type != VAR_LIST)
3479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003480 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003482 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 }
3484 list = argvars[arg_idx].vval.v_list;
3485 if (list == NULL || list->lv_len == 0)
3486 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003487 else if (list->lv_len > MAX_FUNC_ARGS)
3488 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003489 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003490 vim_free(name);
3491 goto theend;
3492 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493 }
3494 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003495 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003497 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500 if (pt == NULL)
3501 vim_free(name);
3502 else
3503 {
3504 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3505 {
3506 listitem_T *li;
3507 int i = 0;
3508 int arg_len = 0;
3509 int lv_len = 0;
3510
3511 if (arg_pt != NULL)
3512 arg_len = arg_pt->pt_argc;
3513 if (list != NULL)
3514 lv_len = list->lv_len;
3515 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003516 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003517 if (pt->pt_argv == NULL)
3518 {
3519 vim_free(pt);
3520 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003521 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003523 for (i = 0; i < arg_len; i++)
3524 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3525 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003526 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003527 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003528 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003529 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531 }
3532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003533 // For "function(dict.func, [], dict)" and "func" is a partial
3534 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 if (dict_idx > 0)
3536 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003537 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3539 ++pt->pt_dict->dv_refcount;
3540 }
3541 else if (arg_pt != NULL)
3542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003543 // If the dict was bound automatically the result is also
3544 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 pt->pt_dict = arg_pt->pt_dict;
3546 pt->pt_auto = arg_pt->pt_auto;
3547 if (pt->pt_dict != NULL)
3548 ++pt->pt_dict->dv_refcount;
3549 }
3550
3551 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003552 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3553 {
3554 pt->pt_func = arg_pt->pt_func;
3555 func_ptr_ref(pt->pt_func);
3556 vim_free(name);
3557 }
3558 else if (is_funcref)
3559 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003560 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003561 func_ptr_ref(pt->pt_func);
3562 vim_free(name);
3563 }
3564 else
3565 {
3566 pt->pt_name = name;
3567 func_ref(name);
3568 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569 }
3570 rettv->v_type = VAR_PARTIAL;
3571 rettv->vval.v_partial = pt;
3572 }
3573 else
3574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003575 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576 rettv->v_type = VAR_FUNC;
3577 rettv->vval.v_string = name;
3578 func_ref(name);
3579 }
3580 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003581theend:
3582 vim_free(trans_name);
3583}
3584
3585/*
3586 * "funcref()" function
3587 */
3588 static void
3589f_funcref(typval_T *argvars, typval_T *rettv)
3590{
3591 common_function(argvars, rettv, TRUE);
3592}
3593
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003594 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003595ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003596{
3597 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3598 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003599 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003600}
3601
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003602/*
3603 * "function()" function
3604 */
3605 static void
3606f_function(typval_T *argvars, typval_T *rettv)
3607{
3608 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609}
3610
3611/*
3612 * "garbagecollect()" function
3613 */
3614 static void
3615f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3616{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003617 // This is postponed until we are back at the toplevel, because we may be
3618 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003619 want_garbage_collect = TRUE;
3620
Bram Moolenaar2df47312020-09-05 17:30:44 +02003621 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 garbage_collect_at_exit = TRUE;
3623}
3624
3625/*
3626 * "get()" function
3627 */
3628 static void
3629f_get(typval_T *argvars, typval_T *rettv)
3630{
3631 listitem_T *li;
3632 list_T *l;
3633 dictitem_T *di;
3634 dict_T *d;
3635 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003636 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003638 if (argvars[0].v_type == VAR_BLOB)
3639 {
3640 int error = FALSE;
3641 int idx = tv_get_number_chk(&argvars[1], &error);
3642
3643 if (!error)
3644 {
3645 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003646 if (idx < 0)
3647 idx = blob_len(argvars[0].vval.v_blob) + idx;
3648 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3649 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003650 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003651 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003652 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003653 tv = rettv;
3654 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003655 }
3656 }
3657 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 {
3659 if ((l = argvars[0].vval.v_list) != NULL)
3660 {
3661 int error = FALSE;
3662
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003663 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 if (!error && li != NULL)
3665 tv = &li->li_tv;
3666 }
3667 }
3668 else if (argvars[0].v_type == VAR_DICT)
3669 {
3670 if ((d = argvars[0].vval.v_dict) != NULL)
3671 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673 if (di != NULL)
3674 tv = &di->di_tv;
3675 }
3676 }
3677 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3678 {
3679 partial_T *pt;
3680 partial_T fref_pt;
3681
3682 if (argvars[0].v_type == VAR_PARTIAL)
3683 pt = argvars[0].vval.v_partial;
3684 else
3685 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003686 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003687 fref_pt.pt_name = argvars[0].vval.v_string;
3688 pt = &fref_pt;
3689 }
3690
3691 if (pt != NULL)
3692 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003693 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003694 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695
3696 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3697 {
3698 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003699 n = partial_name(pt);
3700 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 rettv->vval.v_string = NULL;
3702 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003703 {
3704 rettv->vval.v_string = vim_strsave(n);
3705 if (rettv->v_type == VAR_FUNC)
3706 func_ref(rettv->vval.v_string);
3707 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708 }
3709 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003710 {
3711 what_is_dict = TRUE;
3712 if (pt->pt_dict != NULL)
3713 rettv_dict_set(rettv, pt->pt_dict);
3714 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 else if (STRCMP(what, "args") == 0)
3716 {
3717 rettv->v_type = VAR_LIST;
3718 if (rettv_list_alloc(rettv) == OK)
3719 {
3720 int i;
3721
3722 for (i = 0; i < pt->pt_argc; ++i)
3723 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3724 }
3725 }
3726 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003727 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003728
3729 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3730 // third argument
3731 if (!what_is_dict)
3732 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 }
3734 }
3735 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003736 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737
3738 if (tv == NULL)
3739 {
3740 if (argvars[2].v_type != VAR_UNKNOWN)
3741 copy_tv(&argvars[2], rettv);
3742 }
3743 else
3744 copy_tv(tv, rettv);
3745}
3746
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003747/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003748 * "getchangelist()" function
3749 */
3750 static void
3751f_getchangelist(typval_T *argvars, typval_T *rettv)
3752{
3753#ifdef FEAT_JUMPLIST
3754 buf_T *buf;
3755 int i;
3756 list_T *l;
3757 dict_T *d;
3758#endif
3759
3760 if (rettv_list_alloc(rettv) != OK)
3761 return;
3762
3763#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003764 if (argvars[0].v_type == VAR_UNKNOWN)
3765 buf = curbuf;
3766 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003767 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003768 if (buf == NULL)
3769 return;
3770
3771 l = list_alloc();
3772 if (l == NULL)
3773 return;
3774
3775 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3776 return;
3777 /*
3778 * The current window change list index tracks only the position in the
3779 * current buffer change list. For other buffers, use the change list
3780 * length as the current index.
3781 */
3782 list_append_number(rettv->vval.v_list,
3783 (varnumber_T)((buf == curwin->w_buffer)
3784 ? curwin->w_changelistidx : buf->b_changelistlen));
3785
3786 for (i = 0; i < buf->b_changelistlen; ++i)
3787 {
3788 if (buf->b_changelist[i].lnum == 0)
3789 continue;
3790 if ((d = dict_alloc()) == NULL)
3791 return;
3792 if (list_append_dict(l, d) == FAIL)
3793 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003794 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3795 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003796 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003797 }
3798#endif
3799}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003800
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003801 static void
3802getpos_both(
3803 typval_T *argvars,
3804 typval_T *rettv,
3805 int getcurpos,
3806 int charcol)
3807{
3808 pos_T *fp = NULL;
3809 pos_T pos;
3810 win_T *wp = curwin;
3811 list_T *l;
3812 int fnum = -1;
3813
3814 if (rettv_list_alloc(rettv) == OK)
3815 {
3816 l = rettv->vval.v_list;
3817 if (getcurpos)
3818 {
3819 if (argvars[0].v_type != VAR_UNKNOWN)
3820 {
3821 wp = find_win_by_nr_or_id(&argvars[0]);
3822 if (wp != NULL)
3823 fp = &wp->w_cursor;
3824 }
3825 else
3826 fp = &curwin->w_cursor;
3827 if (fp != NULL && charcol)
3828 {
3829 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003830 pos.col =
3831 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003832 fp = &pos;
3833 }
3834 }
3835 else
3836 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3837 if (fnum != -1)
3838 list_append_number(l, (varnumber_T)fnum);
3839 else
3840 list_append_number(l, (varnumber_T)0);
3841 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3842 : (varnumber_T)0);
3843 list_append_number(l, (fp != NULL)
3844 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3845 : (varnumber_T)0);
3846 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3847 (varnumber_T)0);
3848 if (getcurpos)
3849 {
3850 int save_set_curswant = curwin->w_set_curswant;
3851 colnr_T save_curswant = curwin->w_curswant;
3852 colnr_T save_virtcol = curwin->w_virtcol;
3853
3854 if (wp == curwin)
3855 update_curswant();
3856 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3857 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3858
3859 // Do not change "curswant", as it is unexpected that a get
3860 // function has a side effect.
3861 if (wp == curwin && save_set_curswant)
3862 {
3863 curwin->w_set_curswant = save_set_curswant;
3864 curwin->w_curswant = save_curswant;
3865 curwin->w_virtcol = save_virtcol;
3866 curwin->w_valid &= ~VALID_VIRTCOL;
3867 }
3868 }
3869 }
3870 else
3871 rettv->vval.v_number = FALSE;
3872}
3873
3874/*
3875 * "getcharpos()" function
3876 */
3877 static void
3878f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3879{
3880 getpos_both(argvars, rettv, FALSE, TRUE);
3881}
3882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883/*
3884 * "getcharsearch()" function
3885 */
3886 static void
3887f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3888{
3889 if (rettv_dict_alloc(rettv) != FAIL)
3890 {
3891 dict_T *dict = rettv->vval.v_dict;
3892
Bram Moolenaare0be1672018-07-08 16:50:37 +02003893 dict_add_string(dict, "char", last_csearch());
3894 dict_add_number(dict, "forward", last_csearch_forward());
3895 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 }
3897}
3898
3899/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003900 * "getenv()" function
3901 */
3902 static void
3903f_getenv(typval_T *argvars, typval_T *rettv)
3904{
3905 int mustfree = FALSE;
3906 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3907
3908 if (p == NULL)
3909 {
3910 rettv->v_type = VAR_SPECIAL;
3911 rettv->vval.v_number = VVAL_NULL;
3912 return;
3913 }
3914 if (!mustfree)
3915 p = vim_strsave(p);
3916 rettv->vval.v_string = p;
3917 rettv->v_type = VAR_STRING;
3918}
3919
3920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 * "getfontname()" function
3922 */
3923 static void
3924f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3925{
3926 rettv->v_type = VAR_STRING;
3927 rettv->vval.v_string = NULL;
3928#ifdef FEAT_GUI
3929 if (gui.in_use)
3930 {
3931 GuiFont font;
3932 char_u *name = NULL;
3933
3934 if (argvars[0].v_type == VAR_UNKNOWN)
3935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003936 // Get the "Normal" font. Either the name saved by
3937 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 font = gui.norm_font;
3939 name = hl_get_font_name();
3940 }
3941 else
3942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003943 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 return;
3946 font = gui_mch_get_font(name, FALSE);
3947 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3951 if (argvars[0].v_type != VAR_UNKNOWN)
3952 gui_mch_free_font(font);
3953 }
3954#endif
3955}
3956
3957/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003958 * "getjumplist()" function
3959 */
3960 static void
3961f_getjumplist(typval_T *argvars, typval_T *rettv)
3962{
3963#ifdef FEAT_JUMPLIST
3964 win_T *wp;
3965 int i;
3966 list_T *l;
3967 dict_T *d;
3968#endif
3969
3970 if (rettv_list_alloc(rettv) != OK)
3971 return;
3972
3973#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003974 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003975 if (wp == NULL)
3976 return;
3977
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003978 cleanup_jumplist(wp, TRUE);
3979
Bram Moolenaar4f505882018-02-10 21:06:32 +01003980 l = list_alloc();
3981 if (l == NULL)
3982 return;
3983
3984 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3985 return;
3986 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3987
3988 for (i = 0; i < wp->w_jumplistlen; ++i)
3989 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003990 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3991 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003992 if ((d = dict_alloc()) == NULL)
3993 return;
3994 if (list_append_dict(l, d) == FAIL)
3995 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003996 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3997 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003998 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003999 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004000 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004001 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004002 }
4003#endif
4004}
4005
4006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004007 * "getpid()" function
4008 */
4009 static void
4010f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4011{
4012 rettv->vval.v_number = mch_get_pid();
4013}
4014
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015/*
4016 * "getcurpos()" function
4017 */
4018 static void
4019f_getcurpos(typval_T *argvars, typval_T *rettv)
4020{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004021 getpos_both(argvars, rettv, TRUE, FALSE);
4022}
4023
4024 static void
4025f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4026{
4027 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028}
4029
4030/*
4031 * "getpos(string)" function
4032 */
4033 static void
4034f_getpos(typval_T *argvars, typval_T *rettv)
4035{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004036 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037}
4038
4039/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040 * "getreg()" function
4041 */
4042 static void
4043f_getreg(typval_T *argvars, typval_T *rettv)
4044{
4045 char_u *strregname;
4046 int regname;
4047 int arg2 = FALSE;
4048 int return_list = FALSE;
4049 int error = FALSE;
4050
4051 if (argvars[0].v_type != VAR_UNKNOWN)
4052 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004053 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004054 if (strregname == NULL)
4055 error = TRUE;
4056 else if (in_vim9script() && STRLEN(strregname) > 1)
4057 {
4058 semsg(_(e_register_name_must_be_one_char_str), strregname);
4059 error = TRUE;
4060 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061 if (argvars[1].v_type != VAR_UNKNOWN)
4062 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004063 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004064 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004065 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 }
4067 }
4068 else
4069 strregname = get_vim_var_str(VV_REG);
4070
4071 if (error)
4072 return;
4073
4074 regname = (strregname == NULL ? '"' : *strregname);
4075 if (regname == 0)
4076 regname = '"';
4077
4078 if (return_list)
4079 {
4080 rettv->v_type = VAR_LIST;
4081 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4082 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4083 if (rettv->vval.v_list == NULL)
4084 (void)rettv_list_alloc(rettv);
4085 else
4086 ++rettv->vval.v_list->lv_refcount;
4087 }
4088 else
4089 {
4090 rettv->v_type = VAR_STRING;
4091 rettv->vval.v_string = get_reg_contents(regname,
4092 arg2 ? GREG_EXPR_SRC : 0);
4093 }
4094}
4095
4096/*
4097 * "getregtype()" function
4098 */
4099 static void
4100f_getregtype(typval_T *argvars, typval_T *rettv)
4101{
4102 char_u *strregname;
4103 int regname;
4104 char_u buf[NUMBUFLEN + 2];
4105 long reglen = 0;
4106
4107 if (argvars[0].v_type != VAR_UNKNOWN)
4108 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004109 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004110 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4111 {
4112 semsg(_(e_register_name_must_be_one_char_str), strregname);
4113 strregname = NULL;
4114 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004116 {
4117 rettv->v_type = VAR_STRING;
4118 rettv->vval.v_string = NULL;
4119 return;
4120 }
4121 }
4122 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004123 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004124 strregname = get_vim_var_str(VV_REG);
4125
4126 regname = (strregname == NULL ? '"' : *strregname);
4127 if (regname == 0)
4128 regname = '"';
4129
4130 buf[0] = NUL;
4131 buf[1] = NUL;
4132 switch (get_reg_type(regname, &reglen))
4133 {
4134 case MLINE: buf[0] = 'V'; break;
4135 case MCHAR: buf[0] = 'v'; break;
4136 case MBLOCK:
4137 buf[0] = Ctrl_V;
4138 sprintf((char *)buf + 1, "%ld", reglen + 1);
4139 break;
4140 }
4141 rettv->v_type = VAR_STRING;
4142 rettv->vval.v_string = vim_strsave(buf);
4143}
4144
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004145/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004146 * "gettagstack()" function
4147 */
4148 static void
4149f_gettagstack(typval_T *argvars, typval_T *rettv)
4150{
4151 win_T *wp = curwin; // default is current window
4152
4153 if (rettv_dict_alloc(rettv) != OK)
4154 return;
4155
4156 if (argvars[0].v_type != VAR_UNKNOWN)
4157 {
4158 wp = find_win_by_nr_or_id(&argvars[0]);
4159 if (wp == NULL)
4160 return;
4161 }
4162
4163 get_tagstack(wp, rettv->vval.v_dict);
4164}
4165
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004166/*
4167 * "gettext()" function
4168 */
4169 static void
4170f_gettext(typval_T *argvars, typval_T *rettv)
4171{
4172 if (argvars[0].v_type != VAR_STRING
4173 || argvars[0].vval.v_string == NULL
4174 || *argvars[0].vval.v_string == NUL)
4175 {
4176 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4177 }
4178 else
4179 {
4180 rettv->v_type = VAR_STRING;
4181 rettv->vval.v_string = vim_strsave(
4182 (char_u *)_(argvars[0].vval.v_string));
4183 }
4184}
4185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004186// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187#include "version.h"
4188
4189/*
4190 * "has()" function
4191 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004192 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004193f_has(typval_T *argvars, typval_T *rettv)
4194{
4195 int i;
4196 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004197 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004199 typedef struct {
4200 char *name;
4201 short present;
4202 } has_item_T;
4203 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004204 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004205 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004207 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004208#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004209 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004210#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004211 },
4212 {"arp",
4213#if defined(AMIGA) && defined(FEAT_ARP)
4214 1
4215#else
4216 0
4217#endif
4218 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004219 {"haiku",
4220#ifdef __HAIKU__
4221 1
4222#else
4223 0
4224#endif
4225 },
4226 {"bsd",
4227#if defined(BSD) && !defined(MACOS_X)
4228 1
4229#else
4230 0
4231#endif
4232 },
4233 {"hpux",
4234#ifdef hpux
4235 1
4236#else
4237 0
4238#endif
4239 },
4240 {"linux",
4241#ifdef __linux__
4242 1
4243#else
4244 0
4245#endif
4246 },
4247 {"mac", // Mac OS X (and, once, Mac OS Classic)
4248#ifdef MACOS_X
4249 1
4250#else
4251 0
4252#endif
4253 },
4254 {"osx", // Mac OS X
4255#ifdef MACOS_X
4256 1
4257#else
4258 0
4259#endif
4260 },
4261 {"macunix", // Mac OS X, with the darwin feature
4262#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4263 1
4264#else
4265 0
4266#endif
4267 },
4268 {"osxdarwin", // synonym for macunix
4269#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4270 1
4271#else
4272 0
4273#endif
4274 },
4275 {"qnx",
4276#ifdef __QNX__
4277 1
4278#else
4279 0
4280#endif
4281 },
4282 {"sun",
4283#ifdef SUN_SYSTEM
4284 1
4285#else
4286 0
4287#endif
4288 },
4289 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004291 1
4292#else
4293 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004295 },
4296 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004297#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004298 1
4299#else
4300 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004302 },
4303 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004304#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004305 1
4306#else
4307 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004308#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004309 },
4310 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004311#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004312 1
4313#else
4314 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004316 },
4317 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004318#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004319 1
4320#else
4321 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004323 },
4324 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004326 1
4327#else
4328 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004330 },
4331 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004332#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004333 1
4334#else
4335 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004337 },
4338 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004340 1
4341#else
4342 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004343#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004344 },
4345 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004346#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004347 1
4348#else
4349 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004351 },
4352 {"autocmd", 1},
4353 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004354#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004355 1
4356#else
4357 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004358#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004359 },
4360 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004361#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004362 1
4363#else
4364 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004365#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004366 },
4367 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004368#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004369 1
4370#else
4371 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004372#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004373 },
4374 {"balloon_multiline",
4375#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4376 // MS-Windows requires runtime check, see below
4377 1
4378#else
4379 0
4380#endif
4381 },
4382 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004383#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004384 1
4385#else
4386 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004387#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004388 },
4389 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004391 1
4392#else
4393 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004394#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004395 },
4396 {"all_builtin_terms",
4397#if defined(ALL_BUILTIN_TCAPS)
4398 1
4399#else
4400 0
4401#endif
4402 },
4403 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004405 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004407 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004409 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004411 },
4412 {"byte_offset",
4413#ifdef FEAT_BYTEOFF
4414 1
4415#else
4416 0
4417#endif
4418 },
4419 {"channel",
4420#ifdef FEAT_JOB_CHANNEL
4421 1
4422#else
4423 0
4424#endif
4425 },
4426 {"cindent",
4427#ifdef FEAT_CINDENT
4428 1
4429#else
4430 0
4431#endif
4432 },
4433 {"clientserver",
4434#ifdef FEAT_CLIENTSERVER
4435 1
4436#else
4437 0
4438#endif
4439 },
4440 {"clipboard",
4441#ifdef FEAT_CLIPBOARD
4442 1
4443#else
4444 0
4445#endif
4446 },
4447 {"cmdline_compl", 1},
4448 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004449 {"cmdwin",
4450#ifdef FEAT_CMDWIN
4451 1
4452#else
4453 0
4454#endif
4455 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004456 {"comments", 1},
4457 {"conceal",
4458#ifdef FEAT_CONCEAL
4459 1
4460#else
4461 0
4462#endif
4463 },
4464 {"cryptv",
4465#ifdef FEAT_CRYPT
4466 1
4467#else
4468 0
4469#endif
4470 },
4471 {"crypt-blowfish",
4472#ifdef FEAT_CRYPT
4473 1
4474#else
4475 0
4476#endif
4477 },
4478 {"crypt-blowfish2",
4479#ifdef FEAT_CRYPT
4480 1
4481#else
4482 0
4483#endif
4484 },
4485 {"cscope",
4486#ifdef FEAT_CSCOPE
4487 1
4488#else
4489 0
4490#endif
4491 },
4492 {"cursorbind", 1},
4493 {"cursorshape",
4494#ifdef CURSOR_SHAPE
4495 1
4496#else
4497 0
4498#endif
4499 },
4500 {"debug",
4501#ifdef DEBUG
4502 1
4503#else
4504 0
4505#endif
4506 },
4507 {"dialog_con",
4508#ifdef FEAT_CON_DIALOG
4509 1
4510#else
4511 0
4512#endif
4513 },
4514 {"dialog_gui",
4515#ifdef FEAT_GUI_DIALOG
4516 1
4517#else
4518 0
4519#endif
4520 },
4521 {"diff",
4522#ifdef FEAT_DIFF
4523 1
4524#else
4525 0
4526#endif
4527 },
4528 {"digraphs",
4529#ifdef FEAT_DIGRAPHS
4530 1
4531#else
4532 0
4533#endif
4534 },
4535 {"directx",
4536#ifdef FEAT_DIRECTX
4537 1
4538#else
4539 0
4540#endif
4541 },
4542 {"dnd",
4543#ifdef FEAT_DND
4544 1
4545#else
4546 0
4547#endif
4548 },
4549 {"emacs_tags",
4550#ifdef FEAT_EMACS_TAGS
4551 1
4552#else
4553 0
4554#endif
4555 },
4556 {"eval", 1}, // always present, of course!
4557 {"ex_extra", 1}, // graduated feature
4558 {"extra_search",
4559#ifdef FEAT_SEARCH_EXTRA
4560 1
4561#else
4562 0
4563#endif
4564 },
4565 {"file_in_path",
4566#ifdef FEAT_SEARCHPATH
4567 1
4568#else
4569 0
4570#endif
4571 },
4572 {"filterpipe",
4573#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4574 1
4575#else
4576 0
4577#endif
4578 },
4579 {"find_in_path",
4580#ifdef FEAT_FIND_ID
4581 1
4582#else
4583 0
4584#endif
4585 },
4586 {"float",
4587#ifdef FEAT_FLOAT
4588 1
4589#else
4590 0
4591#endif
4592 },
4593 {"folding",
4594#ifdef FEAT_FOLDING
4595 1
4596#else
4597 0
4598#endif
4599 },
4600 {"footer",
4601#ifdef FEAT_FOOTER
4602 1
4603#else
4604 0
4605#endif
4606 },
4607 {"fork",
4608#if !defined(USE_SYSTEM) && defined(UNIX)
4609 1
4610#else
4611 0
4612#endif
4613 },
4614 {"gettext",
4615#ifdef FEAT_GETTEXT
4616 1
4617#else
4618 0
4619#endif
4620 },
4621 {"gui",
4622#ifdef FEAT_GUI
4623 1
4624#else
4625 0
4626#endif
4627 },
4628 {"gui_neXtaw",
4629#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4630 1
4631#else
4632 0
4633#endif
4634 },
4635 {"gui_athena",
4636#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4637 1
4638#else
4639 0
4640#endif
4641 },
4642 {"gui_gtk",
4643#ifdef FEAT_GUI_GTK
4644 1
4645#else
4646 0
4647#endif
4648 },
4649 {"gui_gtk2",
4650#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4651 1
4652#else
4653 0
4654#endif
4655 },
4656 {"gui_gtk3",
4657#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4658 1
4659#else
4660 0
4661#endif
4662 },
4663 {"gui_gnome",
4664#ifdef FEAT_GUI_GNOME
4665 1
4666#else
4667 0
4668#endif
4669 },
4670 {"gui_haiku",
4671#ifdef FEAT_GUI_HAIKU
4672 1
4673#else
4674 0
4675#endif
4676 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004677 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004678 {"gui_motif",
4679#ifdef FEAT_GUI_MOTIF
4680 1
4681#else
4682 0
4683#endif
4684 },
4685 {"gui_photon",
4686#ifdef FEAT_GUI_PHOTON
4687 1
4688#else
4689 0
4690#endif
4691 },
4692 {"gui_win32",
4693#ifdef FEAT_GUI_MSWIN
4694 1
4695#else
4696 0
4697#endif
4698 },
4699 {"iconv",
4700#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4701 1
4702#else
4703 0
4704#endif
4705 },
4706 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004707 {"ipv6",
4708#ifdef FEAT_IPV6
4709 1
4710#else
4711 0
4712#endif
4713 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004714 {"job",
4715#ifdef FEAT_JOB_CHANNEL
4716 1
4717#else
4718 0
4719#endif
4720 },
4721 {"jumplist",
4722#ifdef FEAT_JUMPLIST
4723 1
4724#else
4725 0
4726#endif
4727 },
4728 {"keymap",
4729#ifdef FEAT_KEYMAP
4730 1
4731#else
4732 0
4733#endif
4734 },
4735 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4736 {"langmap",
4737#ifdef FEAT_LANGMAP
4738 1
4739#else
4740 0
4741#endif
4742 },
4743 {"libcall",
4744#ifdef FEAT_LIBCALL
4745 1
4746#else
4747 0
4748#endif
4749 },
4750 {"linebreak",
4751#ifdef FEAT_LINEBREAK
4752 1
4753#else
4754 0
4755#endif
4756 },
4757 {"lispindent",
4758#ifdef FEAT_LISP
4759 1
4760#else
4761 0
4762#endif
4763 },
4764 {"listcmds", 1},
4765 {"localmap", 1},
4766 {"lua",
4767#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4768 1
4769#else
4770 0
4771#endif
4772 },
4773 {"menu",
4774#ifdef FEAT_MENU
4775 1
4776#else
4777 0
4778#endif
4779 },
4780 {"mksession",
4781#ifdef FEAT_SESSION
4782 1
4783#else
4784 0
4785#endif
4786 },
4787 {"modify_fname", 1},
4788 {"mouse", 1},
4789 {"mouseshape",
4790#ifdef FEAT_MOUSESHAPE
4791 1
4792#else
4793 0
4794#endif
4795 },
4796 {"mouse_dec",
4797#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4798 1
4799#else
4800 0
4801#endif
4802 },
4803 {"mouse_gpm",
4804#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4805 1
4806#else
4807 0
4808#endif
4809 },
4810 {"mouse_jsbterm",
4811#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4812 1
4813#else
4814 0
4815#endif
4816 },
4817 {"mouse_netterm",
4818#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4819 1
4820#else
4821 0
4822#endif
4823 },
4824 {"mouse_pterm",
4825#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4826 1
4827#else
4828 0
4829#endif
4830 },
4831 {"mouse_sgr",
4832#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4833 1
4834#else
4835 0
4836#endif
4837 },
4838 {"mouse_sysmouse",
4839#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4840 1
4841#else
4842 0
4843#endif
4844 },
4845 {"mouse_urxvt",
4846#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4847 1
4848#else
4849 0
4850#endif
4851 },
4852 {"mouse_xterm",
4853#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4854 1
4855#else
4856 0
4857#endif
4858 },
4859 {"multi_byte", 1},
4860 {"multi_byte_ime",
4861#ifdef FEAT_MBYTE_IME
4862 1
4863#else
4864 0
4865#endif
4866 },
4867 {"multi_lang",
4868#ifdef FEAT_MULTI_LANG
4869 1
4870#else
4871 0
4872#endif
4873 },
4874 {"mzscheme",
4875#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4876 1
4877#else
4878 0
4879#endif
4880 },
4881 {"num64", 1},
4882 {"ole",
4883#ifdef FEAT_OLE
4884 1
4885#else
4886 0
4887#endif
4888 },
4889 {"packages",
4890#ifdef FEAT_EVAL
4891 1
4892#else
4893 0
4894#endif
4895 },
4896 {"path_extra",
4897#ifdef FEAT_PATH_EXTRA
4898 1
4899#else
4900 0
4901#endif
4902 },
4903 {"perl",
4904#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4905 1
4906#else
4907 0
4908#endif
4909 },
4910 {"persistent_undo",
4911#ifdef FEAT_PERSISTENT_UNDO
4912 1
4913#else
4914 0
4915#endif
4916 },
4917 {"python_compiled",
4918#if defined(FEAT_PYTHON)
4919 1
4920#else
4921 0
4922#endif
4923 },
4924 {"python_dynamic",
4925#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4926 1
4927#else
4928 0
4929#endif
4930 },
4931 {"python",
4932#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4933 1
4934#else
4935 0
4936#endif
4937 },
4938 {"pythonx",
4939#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4940 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4941 1
4942#else
4943 0
4944#endif
4945 },
4946 {"python3_compiled",
4947#if defined(FEAT_PYTHON3)
4948 1
4949#else
4950 0
4951#endif
4952 },
4953 {"python3_dynamic",
4954#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4955 1
4956#else
4957 0
4958#endif
4959 },
4960 {"python3",
4961#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4962 1
4963#else
4964 0
4965#endif
4966 },
4967 {"popupwin",
4968#ifdef FEAT_PROP_POPUP
4969 1
4970#else
4971 0
4972#endif
4973 },
4974 {"postscript",
4975#ifdef FEAT_POSTSCRIPT
4976 1
4977#else
4978 0
4979#endif
4980 },
4981 {"printer",
4982#ifdef FEAT_PRINTER
4983 1
4984#else
4985 0
4986#endif
4987 },
4988 {"profile",
4989#ifdef FEAT_PROFILE
4990 1
4991#else
4992 0
4993#endif
4994 },
4995 {"reltime",
4996#ifdef FEAT_RELTIME
4997 1
4998#else
4999 0
5000#endif
5001 },
5002 {"quickfix",
5003#ifdef FEAT_QUICKFIX
5004 1
5005#else
5006 0
5007#endif
5008 },
5009 {"rightleft",
5010#ifdef FEAT_RIGHTLEFT
5011 1
5012#else
5013 0
5014#endif
5015 },
5016 {"ruby",
5017#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5018 1
5019#else
5020 0
5021#endif
5022 },
5023 {"scrollbind", 1},
5024 {"showcmd",
5025#ifdef FEAT_CMDL_INFO
5026 1
5027#else
5028 0
5029#endif
5030 },
5031 {"cmdline_info",
5032#ifdef FEAT_CMDL_INFO
5033 1
5034#else
5035 0
5036#endif
5037 },
5038 {"signs",
5039#ifdef FEAT_SIGNS
5040 1
5041#else
5042 0
5043#endif
5044 },
5045 {"smartindent",
5046#ifdef FEAT_SMARTINDENT
5047 1
5048#else
5049 0
5050#endif
5051 },
5052 {"startuptime",
5053#ifdef STARTUPTIME
5054 1
5055#else
5056 0
5057#endif
5058 },
5059 {"statusline",
5060#ifdef FEAT_STL_OPT
5061 1
5062#else
5063 0
5064#endif
5065 },
5066 {"netbeans_intg",
5067#ifdef FEAT_NETBEANS_INTG
5068 1
5069#else
5070 0
5071#endif
5072 },
5073 {"sound",
5074#ifdef FEAT_SOUND
5075 1
5076#else
5077 0
5078#endif
5079 },
5080 {"spell",
5081#ifdef FEAT_SPELL
5082 1
5083#else
5084 0
5085#endif
5086 },
5087 {"syntax",
5088#ifdef FEAT_SYN_HL
5089 1
5090#else
5091 0
5092#endif
5093 },
5094 {"system",
5095#if defined(USE_SYSTEM) || !defined(UNIX)
5096 1
5097#else
5098 0
5099#endif
5100 },
5101 {"tag_binary",
5102#ifdef FEAT_TAG_BINS
5103 1
5104#else
5105 0
5106#endif
5107 },
5108 {"tcl",
5109#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5110 1
5111#else
5112 0
5113#endif
5114 },
5115 {"termguicolors",
5116#ifdef FEAT_TERMGUICOLORS
5117 1
5118#else
5119 0
5120#endif
5121 },
5122 {"terminal",
5123#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5124 1
5125#else
5126 0
5127#endif
5128 },
5129 {"terminfo",
5130#ifdef TERMINFO
5131 1
5132#else
5133 0
5134#endif
5135 },
5136 {"termresponse",
5137#ifdef FEAT_TERMRESPONSE
5138 1
5139#else
5140 0
5141#endif
5142 },
5143 {"textobjects",
5144#ifdef FEAT_TEXTOBJ
5145 1
5146#else
5147 0
5148#endif
5149 },
5150 {"textprop",
5151#ifdef FEAT_PROP_POPUP
5152 1
5153#else
5154 0
5155#endif
5156 },
5157 {"tgetent",
5158#ifdef HAVE_TGETENT
5159 1
5160#else
5161 0
5162#endif
5163 },
5164 {"timers",
5165#ifdef FEAT_TIMERS
5166 1
5167#else
5168 0
5169#endif
5170 },
5171 {"title",
5172#ifdef FEAT_TITLE
5173 1
5174#else
5175 0
5176#endif
5177 },
5178 {"toolbar",
5179#ifdef FEAT_TOOLBAR
5180 1
5181#else
5182 0
5183#endif
5184 },
5185 {"unnamedplus",
5186#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5187 1
5188#else
5189 0
5190#endif
5191 },
5192 {"user-commands", 1}, // was accidentally included in 5.4
5193 {"user_commands", 1},
5194 {"vartabs",
5195#ifdef FEAT_VARTABS
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"vertsplit", 1},
5202 {"viminfo",
5203#ifdef FEAT_VIMINFO
5204 1
5205#else
5206 0
5207#endif
5208 },
5209 {"vimscript-1", 1},
5210 {"vimscript-2", 1},
5211 {"vimscript-3", 1},
5212 {"vimscript-4", 1},
5213 {"virtualedit", 1},
5214 {"visual", 1},
5215 {"visualextra", 1},
5216 {"vreplace", 1},
5217 {"vtp",
5218#ifdef FEAT_VTP
5219 1
5220#else
5221 0
5222#endif
5223 },
5224 {"wildignore",
5225#ifdef FEAT_WILDIGN
5226 1
5227#else
5228 0
5229#endif
5230 },
5231 {"wildmenu",
5232#ifdef FEAT_WILDMENU
5233 1
5234#else
5235 0
5236#endif
5237 },
5238 {"windows", 1},
5239 {"winaltkeys",
5240#ifdef FEAT_WAK
5241 1
5242#else
5243 0
5244#endif
5245 },
5246 {"writebackup",
5247#ifdef FEAT_WRITEBACKUP
5248 1
5249#else
5250 0
5251#endif
5252 },
5253 {"xim",
5254#ifdef FEAT_XIM
5255 1
5256#else
5257 0
5258#endif
5259 },
5260 {"xfontset",
5261#ifdef FEAT_XFONTSET
5262 1
5263#else
5264 0
5265#endif
5266 },
5267 {"xpm",
5268#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5269 1
5270#else
5271 0
5272#endif
5273 },
5274 {"xpm_w32", // for backward compatibility
5275#ifdef FEAT_XPM_W32
5276 1
5277#else
5278 0
5279#endif
5280 },
5281 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005283 1
5284#else
5285 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005286#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005287 },
5288 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005290 1
5291#else
5292 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005294 },
5295 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005297 1
5298#else
5299 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005301 },
5302 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005304 1
5305#else
5306 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005308 },
5309 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005311 1
5312#else
5313 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005315 },
5316 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317 };
5318
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005319 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005320 for (i = 0; has_list[i].name != NULL; ++i)
5321 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005323 x = TRUE;
5324 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 break;
5326 }
5327
Bram Moolenaar79296512020-03-22 16:17:14 +01005328 // features also in has_list[] but sometimes enabled at runtime
5329 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005331 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005332 {
5333 // intentionally empty
5334 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005335#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336 else if (STRICMP(name, "balloon_multiline") == 0)
5337 n = multiline_balloon_available();
5338#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005339#ifdef VIMDLL
5340 else if (STRICMP(name, "filterpipe") == 0)
5341 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342#endif
5343#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5344 else if (STRICMP(name, "iconv") == 0)
5345 n = iconv_enabled(FALSE);
5346#endif
5347#ifdef DYNAMIC_LUA
5348 else if (STRICMP(name, "lua") == 0)
5349 n = lua_enabled(FALSE);
5350#endif
5351#ifdef DYNAMIC_MZSCHEME
5352 else if (STRICMP(name, "mzscheme") == 0)
5353 n = mzscheme_enabled(FALSE);
5354#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005355#ifdef DYNAMIC_PERL
5356 else if (STRICMP(name, "perl") == 0)
5357 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359#ifdef DYNAMIC_PYTHON
5360 else if (STRICMP(name, "python") == 0)
5361 n = python_enabled(FALSE);
5362#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363#ifdef DYNAMIC_PYTHON3
5364 else if (STRICMP(name, "python3") == 0)
5365 n = python3_enabled(FALSE);
5366#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005367#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5368 else if (STRICMP(name, "pythonx") == 0)
5369 {
5370# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5371 if (p_pyx == 0)
5372 n = python3_enabled(FALSE) || python_enabled(FALSE);
5373 else if (p_pyx == 3)
5374 n = python3_enabled(FALSE);
5375 else if (p_pyx == 2)
5376 n = python_enabled(FALSE);
5377# elif defined(DYNAMIC_PYTHON)
5378 n = python_enabled(FALSE);
5379# elif defined(DYNAMIC_PYTHON3)
5380 n = python3_enabled(FALSE);
5381# endif
5382 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005383#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005384#ifdef DYNAMIC_RUBY
5385 else if (STRICMP(name, "ruby") == 0)
5386 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005387#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005388#ifdef DYNAMIC_TCL
5389 else if (STRICMP(name, "tcl") == 0)
5390 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005391#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005392#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005393 else if (STRICMP(name, "terminal") == 0)
5394 n = terminal_enabled();
5395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396 }
5397
Bram Moolenaar79296512020-03-22 16:17:14 +01005398 // features not in has_list[]
5399 if (x == FALSE)
5400 {
5401 if (STRNICMP(name, "patch", 5) == 0)
5402 {
5403 x = TRUE;
5404 if (name[5] == '-'
5405 && STRLEN(name) >= 11
5406 && vim_isdigit(name[6])
5407 && vim_isdigit(name[8])
5408 && vim_isdigit(name[10]))
5409 {
5410 int major = atoi((char *)name + 6);
5411 int minor = atoi((char *)name + 8);
5412
5413 // Expect "patch-9.9.01234".
5414 n = (major < VIM_VERSION_MAJOR
5415 || (major == VIM_VERSION_MAJOR
5416 && (minor < VIM_VERSION_MINOR
5417 || (minor == VIM_VERSION_MINOR
5418 && has_patch(atoi((char *)name + 10))))));
5419 }
5420 else
5421 n = has_patch(atoi((char *)name + 5));
5422 }
5423 else if (STRICMP(name, "vim_starting") == 0)
5424 {
5425 x = TRUE;
5426 n = (starting != 0);
5427 }
5428 else if (STRICMP(name, "ttyin") == 0)
5429 {
5430 x = TRUE;
5431 n = mch_input_isatty();
5432 }
5433 else if (STRICMP(name, "ttyout") == 0)
5434 {
5435 x = TRUE;
5436 n = stdout_isatty;
5437 }
5438 else if (STRICMP(name, "multi_byte_encoding") == 0)
5439 {
5440 x = TRUE;
5441 n = has_mbyte;
5442 }
5443 else if (STRICMP(name, "gui_running") == 0)
5444 {
5445 x = TRUE;
5446#ifdef FEAT_GUI
5447 n = (gui.in_use || gui.starting);
5448#endif
5449 }
5450 else if (STRICMP(name, "browse") == 0)
5451 {
5452 x = TRUE;
5453#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5454 n = gui.in_use; // gui_mch_browse() works when GUI is running
5455#endif
5456 }
5457 else if (STRICMP(name, "syntax_items") == 0)
5458 {
5459 x = TRUE;
5460#ifdef FEAT_SYN_HL
5461 n = syntax_present(curwin);
5462#endif
5463 }
5464 else if (STRICMP(name, "vcon") == 0)
5465 {
5466 x = TRUE;
5467#ifdef FEAT_VTP
5468 n = is_term_win32() && has_vtp_working();
5469#endif
5470 }
5471 else if (STRICMP(name, "netbeans_enabled") == 0)
5472 {
5473 x = TRUE;
5474#ifdef FEAT_NETBEANS_INTG
5475 n = netbeans_active();
5476#endif
5477 }
5478 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5479 {
5480 x = TRUE;
5481#ifdef FEAT_MOUSE_GPM
5482 n = gpm_enabled();
5483#endif
5484 }
5485 else if (STRICMP(name, "conpty") == 0)
5486 {
5487 x = TRUE;
5488#if defined(FEAT_TERMINAL) && defined(MSWIN)
5489 n = use_conpty();
5490#endif
5491 }
5492 else if (STRICMP(name, "clipboard_working") == 0)
5493 {
5494 x = TRUE;
5495#ifdef FEAT_CLIPBOARD
5496 n = clip_star.available;
5497#endif
5498 }
5499 }
5500
Bram Moolenaar04637e22020-09-05 18:45:29 +02005501 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005502 // return whether feature could ever be enabled
5503 rettv->vval.v_number = x;
5504 else
5505 // return whether feature is enabled
5506 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507}
5508
5509/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005510 * Return TRUE if "feature" can change later.
5511 * Also when checking for the feature has side effects, such as loading a DLL.
5512 */
5513 int
5514dynamic_feature(char_u *feature)
5515{
5516 return (feature == NULL
5517#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5518 || STRICMP(feature, "balloon_multiline") == 0
5519#endif
5520#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5521 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5522#endif
5523#ifdef VIMDLL
5524 || STRICMP(feature, "filterpipe") == 0
5525#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005526#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005527 // this can only change on Unix where the ":gui" command could be
5528 // used.
5529 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5530#endif
5531#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5532 || STRICMP(feature, "iconv") == 0
5533#endif
5534#ifdef DYNAMIC_LUA
5535 || STRICMP(feature, "lua") == 0
5536#endif
5537#ifdef FEAT_MOUSE_GPM
5538 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5539#endif
5540#ifdef DYNAMIC_MZSCHEME
5541 || STRICMP(feature, "mzscheme") == 0
5542#endif
5543#ifdef FEAT_NETBEANS_INTG
5544 || STRICMP(feature, "netbeans_enabled") == 0
5545#endif
5546#ifdef DYNAMIC_PERL
5547 || STRICMP(feature, "perl") == 0
5548#endif
5549#ifdef DYNAMIC_PYTHON
5550 || STRICMP(feature, "python") == 0
5551#endif
5552#ifdef DYNAMIC_PYTHON3
5553 || STRICMP(feature, "python3") == 0
5554#endif
5555#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5556 || STRICMP(feature, "pythonx") == 0
5557#endif
5558#ifdef DYNAMIC_RUBY
5559 || STRICMP(feature, "ruby") == 0
5560#endif
5561#ifdef FEAT_SYN_HL
5562 || STRICMP(feature, "syntax_items") == 0
5563#endif
5564#ifdef DYNAMIC_TCL
5565 || STRICMP(feature, "tcl") == 0
5566#endif
5567 // once "starting" is zero it will stay that way
5568 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5569 || STRICMP(feature, "multi_byte_encoding") == 0
5570#if defined(FEAT_TERMINAL) && defined(MSWIN)
5571 || STRICMP(feature, "conpty") == 0
5572#endif
5573 );
5574}
5575
5576/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577 * "haslocaldir()" function
5578 */
5579 static void
5580f_haslocaldir(typval_T *argvars, typval_T *rettv)
5581{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005582 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 win_T *wp = NULL;
5584
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005585 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5586
5587 // Check for window-local and tab-local directories
5588 if (wp != NULL && wp->w_localdir != NULL)
5589 rettv->vval.v_number = 1;
5590 else if (tp != NULL && tp->tp_localdir != NULL)
5591 rettv->vval.v_number = 2;
5592 else
5593 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594}
5595
5596/*
5597 * "hasmapto()" function
5598 */
5599 static void
5600f_hasmapto(typval_T *argvars, typval_T *rettv)
5601{
5602 char_u *name;
5603 char_u *mode;
5604 char_u buf[NUMBUFLEN];
5605 int abbr = FALSE;
5606
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005607 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 if (argvars[1].v_type == VAR_UNKNOWN)
5609 mode = (char_u *)"nvo";
5610 else
5611 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005612 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005614 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 }
5616
5617 if (map_to_exists(name, mode, abbr))
5618 rettv->vval.v_number = TRUE;
5619 else
5620 rettv->vval.v_number = FALSE;
5621}
5622
5623/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005624 * "highlightID(name)" function
5625 */
5626 static void
5627f_hlID(typval_T *argvars, typval_T *rettv)
5628{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005629 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630}
5631
5632/*
5633 * "highlight_exists()" function
5634 */
5635 static void
5636f_hlexists(typval_T *argvars, typval_T *rettv)
5637{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005638 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639}
5640
5641/*
5642 * "hostname()" function
5643 */
5644 static void
5645f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5646{
5647 char_u hostname[256];
5648
5649 mch_get_host_name(hostname, 256);
5650 rettv->v_type = VAR_STRING;
5651 rettv->vval.v_string = vim_strsave(hostname);
5652}
5653
5654/*
5655 * iconv() function
5656 */
5657 static void
5658f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5659{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005660 char_u buf1[NUMBUFLEN];
5661 char_u buf2[NUMBUFLEN];
5662 char_u *from, *to, *str;
5663 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664
5665 rettv->v_type = VAR_STRING;
5666 rettv->vval.v_string = NULL;
5667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005668 str = tv_get_string(&argvars[0]);
5669 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5670 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671 vimconv.vc_type = CONV_NONE;
5672 convert_setup(&vimconv, from, to);
5673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 if (vimconv.vc_type == CONV_NONE)
5676 rettv->vval.v_string = vim_strsave(str);
5677 else
5678 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5679
5680 convert_setup(&vimconv, NULL, NULL);
5681 vim_free(from);
5682 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005683}
5684
5685/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005686 * "index()" function
5687 */
5688 static void
5689f_index(typval_T *argvars, typval_T *rettv)
5690{
5691 list_T *l;
5692 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005693 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005694 long idx = 0;
5695 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005696 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005697
5698 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005699 if (argvars[0].v_type == VAR_BLOB)
5700 {
5701 typval_T tv;
5702 int start = 0;
5703
5704 if (argvars[2].v_type != VAR_UNKNOWN)
5705 {
5706 start = tv_get_number_chk(&argvars[2], &error);
5707 if (error)
5708 return;
5709 }
5710 b = argvars[0].vval.v_blob;
5711 if (b == NULL)
5712 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005713 if (start < 0)
5714 {
5715 start = blob_len(b) + start;
5716 if (start < 0)
5717 start = 0;
5718 }
5719
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005720 for (idx = start; idx < blob_len(b); ++idx)
5721 {
5722 tv.v_type = VAR_NUMBER;
5723 tv.vval.v_number = blob_get(b, idx);
5724 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5725 {
5726 rettv->vval.v_number = idx;
5727 return;
5728 }
5729 }
5730 return;
5731 }
5732 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005733 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005734 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 return;
5736 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 l = argvars[0].vval.v_list;
5739 if (l != NULL)
5740 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005741 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005742 item = l->lv_first;
5743 if (argvars[2].v_type != VAR_UNKNOWN)
5744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005745 // Start at specified item. Use the cached index that list_find()
5746 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005747 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005748 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005749 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005750 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 if (error)
5752 item = NULL;
5753 }
5754
5755 for ( ; item != NULL; item = item->li_next, ++idx)
5756 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5757 {
5758 rettv->vval.v_number = idx;
5759 break;
5760 }
5761 }
5762}
5763
5764static int inputsecret_flag = 0;
5765
5766/*
5767 * "input()" function
5768 * Also handles inputsecret() when inputsecret is set.
5769 */
5770 static void
5771f_input(typval_T *argvars, typval_T *rettv)
5772{
5773 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5774}
5775
5776/*
5777 * "inputdialog()" function
5778 */
5779 static void
5780f_inputdialog(typval_T *argvars, typval_T *rettv)
5781{
5782#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005783 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5785 {
5786 char_u *message;
5787 char_u buf[NUMBUFLEN];
5788 char_u *defstr = (char_u *)"";
5789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005790 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005791 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005792 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5794 else
5795 IObuff[0] = NUL;
5796 if (message != NULL && defstr != NULL
5797 && do_dialog(VIM_QUESTION, NULL, message,
5798 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5799 rettv->vval.v_string = vim_strsave(IObuff);
5800 else
5801 {
5802 if (message != NULL && defstr != NULL
5803 && argvars[1].v_type != VAR_UNKNOWN
5804 && argvars[2].v_type != VAR_UNKNOWN)
5805 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005806 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005807 else
5808 rettv->vval.v_string = NULL;
5809 }
5810 rettv->v_type = VAR_STRING;
5811 }
5812 else
5813#endif
5814 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5815}
5816
5817/*
5818 * "inputlist()" function
5819 */
5820 static void
5821f_inputlist(typval_T *argvars, typval_T *rettv)
5822{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005823 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 listitem_T *li;
5825 int selected;
5826 int mouse_used;
5827
5828#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005829 // While starting up, there is no place to enter text. When running tests
5830 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005831 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 return;
5833#endif
5834 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005837 return;
5838 }
5839
5840 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005841 msg_row = Rows - 1; // for when 'cmdheight' > 1
5842 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005843 msg_scroll = TRUE;
5844 msg_clr_eos();
5845
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005846 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005847 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005848 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005850 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 msg_putchar('\n');
5852 }
5853
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855 selected = prompt_for_number(&mouse_used);
5856 if (mouse_used)
5857 selected -= lines_left;
5858
5859 rettv->vval.v_number = selected;
5860}
5861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5863
5864/*
5865 * "inputrestore()" function
5866 */
5867 static void
5868f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5869{
5870 if (ga_userinput.ga_len > 0)
5871 {
5872 --ga_userinput.ga_len;
5873 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5874 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005875 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876 }
5877 else if (p_verbose > 1)
5878 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005879 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005881 }
5882}
5883
5884/*
5885 * "inputsave()" function
5886 */
5887 static void
5888f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5889{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005890 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005891 if (ga_grow(&ga_userinput, 1) == OK)
5892 {
5893 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5894 + ga_userinput.ga_len);
5895 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005896 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005897 }
5898 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005899 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005900}
5901
5902/*
5903 * "inputsecret()" function
5904 */
5905 static void
5906f_inputsecret(typval_T *argvars, typval_T *rettv)
5907{
5908 ++cmdline_star;
5909 ++inputsecret_flag;
5910 f_input(argvars, rettv);
5911 --cmdline_star;
5912 --inputsecret_flag;
5913}
5914
5915/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005916 * "interrupt()" function
5917 */
5918 static void
5919f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5920{
5921 got_int = TRUE;
5922}
5923
5924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925 * "invert(expr)" function
5926 */
5927 static void
5928f_invert(typval_T *argvars, typval_T *rettv)
5929{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005930 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931}
5932
5933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 * "islocked()" function
5935 */
5936 static void
5937f_islocked(typval_T *argvars, typval_T *rettv)
5938{
5939 lval_T lv;
5940 char_u *end;
5941 dictitem_T *di;
5942
5943 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005944 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005945 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005946 if (end != NULL && lv.ll_name != NULL)
5947 {
5948 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005949 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950 else
5951 {
5952 if (lv.ll_tv == NULL)
5953 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005954 di = find_var(lv.ll_name, NULL, TRUE);
5955 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005957 // Consider a variable locked when:
5958 // 1. the variable itself is locked
5959 // 2. the value of the variable is locked.
5960 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005961 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5962 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005963 }
5964 }
5965 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005966 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005968 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005970 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5972 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005973 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5975 }
5976 }
5977
5978 clear_lval(&lv);
5979}
5980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005982 * "last_buffer_nr()" function.
5983 */
5984 static void
5985f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5986{
5987 int n = 0;
5988 buf_T *buf;
5989
Bram Moolenaar29323592016-07-24 22:04:11 +02005990 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005991 if (n < buf->b_fnum)
5992 n = buf->b_fnum;
5993
5994 rettv->vval.v_number = n;
5995}
5996
5997/*
5998 * "len()" function
5999 */
6000 static void
6001f_len(typval_T *argvars, typval_T *rettv)
6002{
6003 switch (argvars[0].v_type)
6004 {
6005 case VAR_STRING:
6006 case VAR_NUMBER:
6007 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006008 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006010 case VAR_BLOB:
6011 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6012 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 case VAR_LIST:
6014 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6015 break;
6016 case VAR_DICT:
6017 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6018 break;
6019 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006020 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006022 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006023 case VAR_SPECIAL:
6024 case VAR_FLOAT:
6025 case VAR_FUNC:
6026 case VAR_PARTIAL:
6027 case VAR_JOB:
6028 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006029 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006030 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 break;
6032 }
6033}
6034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006036libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037{
6038#ifdef FEAT_LIBCALL
6039 char_u *string_in;
6040 char_u **string_result;
6041 int nr_result;
6042#endif
6043
6044 rettv->v_type = type;
6045 if (type != VAR_NUMBER)
6046 rettv->vval.v_string = NULL;
6047
6048 if (check_restricted() || check_secure())
6049 return;
6050
6051#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006052 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6054 {
6055 string_in = NULL;
6056 if (argvars[2].v_type == VAR_STRING)
6057 string_in = argvars[2].vval.v_string;
6058 if (type == VAR_NUMBER)
6059 string_result = NULL;
6060 else
6061 string_result = &rettv->vval.v_string;
6062 if (mch_libcall(argvars[0].vval.v_string,
6063 argvars[1].vval.v_string,
6064 string_in,
6065 argvars[2].vval.v_number,
6066 string_result,
6067 &nr_result) == OK
6068 && type == VAR_NUMBER)
6069 rettv->vval.v_number = nr_result;
6070 }
6071#endif
6072}
6073
6074/*
6075 * "libcall()" function
6076 */
6077 static void
6078f_libcall(typval_T *argvars, typval_T *rettv)
6079{
6080 libcall_common(argvars, rettv, VAR_STRING);
6081}
6082
6083/*
6084 * "libcallnr()" function
6085 */
6086 static void
6087f_libcallnr(typval_T *argvars, typval_T *rettv)
6088{
6089 libcall_common(argvars, rettv, VAR_NUMBER);
6090}
6091
6092/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006093 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 */
6095 static void
6096f_line(typval_T *argvars, typval_T *rettv)
6097{
6098 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006099 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006101 int id;
6102 tabpage_T *tp;
6103 win_T *wp;
6104 win_T *save_curwin;
6105 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006107 if (argvars[1].v_type != VAR_UNKNOWN)
6108 {
6109 // use window specified in the second argument
6110 id = (int)tv_get_number(&argvars[1]);
6111 wp = win_id2wp_tp(id, &tp);
6112 if (wp != NULL && tp != NULL)
6113 {
6114 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6115 == OK)
6116 {
6117 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006118 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006119 }
6120 restore_win_noblock(save_curwin, save_curtab, TRUE);
6121 }
6122 }
6123 else
6124 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006125 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006126
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006127 if (fp != NULL)
6128 lnum = fp->lnum;
6129 rettv->vval.v_number = lnum;
6130}
6131
6132/*
6133 * "line2byte(lnum)" function
6134 */
6135 static void
6136f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6137{
6138#ifndef FEAT_BYTEOFF
6139 rettv->vval.v_number = -1;
6140#else
6141 linenr_T lnum;
6142
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006143 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6145 rettv->vval.v_number = -1;
6146 else
6147 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6148 if (rettv->vval.v_number >= 0)
6149 ++rettv->vval.v_number;
6150#endif
6151}
6152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006153#ifdef FEAT_LUA
6154/*
6155 * "luaeval()" function
6156 */
6157 static void
6158f_luaeval(typval_T *argvars, typval_T *rettv)
6159{
6160 char_u *str;
6161 char_u buf[NUMBUFLEN];
6162
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006163 if (check_restricted() || check_secure())
6164 return;
6165
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006166 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167 do_luaeval(str, argvars + 1, rettv);
6168}
6169#endif
6170
6171/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006172 * "maparg()" function
6173 */
6174 static void
6175f_maparg(typval_T *argvars, typval_T *rettv)
6176{
6177 get_maparg(argvars, rettv, TRUE);
6178}
6179
6180/*
6181 * "mapcheck()" function
6182 */
6183 static void
6184f_mapcheck(typval_T *argvars, typval_T *rettv)
6185{
6186 get_maparg(argvars, rettv, FALSE);
6187}
6188
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006189typedef enum
6190{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 MATCH_END, // matchend()
6192 MATCH_MATCH, // match()
6193 MATCH_STR, // matchstr()
6194 MATCH_LIST, // matchlist()
6195 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006196} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197
6198 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006199find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200{
6201 char_u *str = NULL;
6202 long len = 0;
6203 char_u *expr = NULL;
6204 char_u *pat;
6205 regmatch_T regmatch;
6206 char_u patbuf[NUMBUFLEN];
6207 char_u strbuf[NUMBUFLEN];
6208 char_u *save_cpo;
6209 long start = 0;
6210 long nth = 1;
6211 colnr_T startcol = 0;
6212 int match = 0;
6213 list_T *l = NULL;
6214 listitem_T *li = NULL;
6215 long idx = 0;
6216 char_u *tofree = NULL;
6217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006218 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006220 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006221
6222 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006223 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006225 // type MATCH_LIST: return empty list when there are no matches.
6226 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227 if (rettv_list_alloc(rettv) == FAIL)
6228 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006229 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230 && (list_append_string(rettv->vval.v_list,
6231 (char_u *)"", 0) == FAIL
6232 || list_append_number(rettv->vval.v_list,
6233 (varnumber_T)-1) == FAIL
6234 || list_append_number(rettv->vval.v_list,
6235 (varnumber_T)-1) == FAIL
6236 || list_append_number(rettv->vval.v_list,
6237 (varnumber_T)-1) == FAIL))
6238 {
6239 list_free(rettv->vval.v_list);
6240 rettv->vval.v_list = NULL;
6241 goto theend;
6242 }
6243 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006244 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006245 {
6246 rettv->v_type = VAR_STRING;
6247 rettv->vval.v_string = NULL;
6248 }
6249
6250 if (argvars[0].v_type == VAR_LIST)
6251 {
6252 if ((l = argvars[0].vval.v_list) == NULL)
6253 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006254 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 li = l->lv_first;
6256 }
6257 else
6258 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006259 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 len = (long)STRLEN(str);
6261 }
6262
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006263 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 if (pat == NULL)
6265 goto theend;
6266
6267 if (argvars[2].v_type != VAR_UNKNOWN)
6268 {
6269 int error = FALSE;
6270
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006271 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 if (error)
6273 goto theend;
6274 if (l != NULL)
6275 {
6276 li = list_find(l, start);
6277 if (li == NULL)
6278 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006279 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006280 }
6281 else
6282 {
6283 if (start < 0)
6284 start = 0;
6285 if (start > len)
6286 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006287 // When "count" argument is there ignore matches before "start",
6288 // otherwise skip part of the string. Differs when pattern is "^"
6289 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 if (argvars[3].v_type != VAR_UNKNOWN)
6291 startcol = start;
6292 else
6293 {
6294 str += start;
6295 len -= start;
6296 }
6297 }
6298
6299 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006300 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 if (error)
6302 goto theend;
6303 }
6304
6305 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6306 if (regmatch.regprog != NULL)
6307 {
6308 regmatch.rm_ic = p_ic;
6309
6310 for (;;)
6311 {
6312 if (l != NULL)
6313 {
6314 if (li == NULL)
6315 {
6316 match = FALSE;
6317 break;
6318 }
6319 vim_free(tofree);
6320 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6321 if (str == NULL)
6322 break;
6323 }
6324
6325 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6326
6327 if (match && --nth <= 0)
6328 break;
6329 if (l == NULL && !match)
6330 break;
6331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006332 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 if (l != NULL)
6334 {
6335 li = li->li_next;
6336 ++idx;
6337 }
6338 else
6339 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 startcol = (colnr_T)(regmatch.startp[0]
6341 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 if (startcol > (colnr_T)len
6343 || str + startcol <= regmatch.startp[0])
6344 {
6345 match = FALSE;
6346 break;
6347 }
6348 }
6349 }
6350
6351 if (match)
6352 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006353 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006354 {
6355 listitem_T *li1 = rettv->vval.v_list->lv_first;
6356 listitem_T *li2 = li1->li_next;
6357 listitem_T *li3 = li2->li_next;
6358 listitem_T *li4 = li3->li_next;
6359
6360 vim_free(li1->li_tv.vval.v_string);
6361 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006362 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363 li3->li_tv.vval.v_number =
6364 (varnumber_T)(regmatch.startp[0] - expr);
6365 li4->li_tv.vval.v_number =
6366 (varnumber_T)(regmatch.endp[0] - expr);
6367 if (l != NULL)
6368 li2->li_tv.vval.v_number = (varnumber_T)idx;
6369 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006370 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 {
6372 int i;
6373
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006374 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006375 for (i = 0; i < NSUBEXP; ++i)
6376 {
6377 if (regmatch.endp[i] == NULL)
6378 {
6379 if (list_append_string(rettv->vval.v_list,
6380 (char_u *)"", 0) == FAIL)
6381 break;
6382 }
6383 else if (list_append_string(rettv->vval.v_list,
6384 regmatch.startp[i],
6385 (int)(regmatch.endp[i] - regmatch.startp[i]))
6386 == FAIL)
6387 break;
6388 }
6389 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006390 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006392 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 if (l != NULL)
6394 copy_tv(&li->li_tv, rettv);
6395 else
6396 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006397 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 }
6399 else if (l != NULL)
6400 rettv->vval.v_number = idx;
6401 else
6402 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006403 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 rettv->vval.v_number =
6405 (varnumber_T)(regmatch.startp[0] - str);
6406 else
6407 rettv->vval.v_number =
6408 (varnumber_T)(regmatch.endp[0] - str);
6409 rettv->vval.v_number += (varnumber_T)(str - expr);
6410 }
6411 }
6412 vim_regfree(regmatch.regprog);
6413 }
6414
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006415theend:
6416 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006417 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 listitem_remove(rettv->vval.v_list,
6419 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 vim_free(tofree);
6421 p_cpo = save_cpo;
6422}
6423
6424/*
6425 * "match()" function
6426 */
6427 static void
6428f_match(typval_T *argvars, typval_T *rettv)
6429{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006430 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431}
6432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433/*
6434 * "matchend()" function
6435 */
6436 static void
6437f_matchend(typval_T *argvars, typval_T *rettv)
6438{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006439 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440}
6441
6442/*
6443 * "matchlist()" function
6444 */
6445 static void
6446f_matchlist(typval_T *argvars, typval_T *rettv)
6447{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006448 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449}
6450
6451/*
6452 * "matchstr()" function
6453 */
6454 static void
6455f_matchstr(typval_T *argvars, typval_T *rettv)
6456{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006457 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458}
6459
6460/*
6461 * "matchstrpos()" function
6462 */
6463 static void
6464f_matchstrpos(typval_T *argvars, typval_T *rettv)
6465{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006466 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467}
6468
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 static void
6470max_min(typval_T *argvars, typval_T *rettv, int domax)
6471{
6472 varnumber_T n = 0;
6473 varnumber_T i;
6474 int error = FALSE;
6475
6476 if (argvars[0].v_type == VAR_LIST)
6477 {
6478 list_T *l;
6479 listitem_T *li;
6480
6481 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006482 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006484 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006486 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6487 n = l->lv_u.nonmat.lv_start;
6488 else
6489 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6490 * l->lv_u.nonmat.lv_stride;
6491 }
6492 else
6493 {
6494 li = l->lv_first;
6495 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006497 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006498 if (error)
6499 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006500 for (;;)
6501 {
6502 li = li->li_next;
6503 if (li == NULL)
6504 break;
6505 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006506 if (error)
6507 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006508 if (domax ? i > n : i < n)
6509 n = i;
6510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 }
6512 }
6513 }
6514 }
6515 else if (argvars[0].v_type == VAR_DICT)
6516 {
6517 dict_T *d;
6518 int first = TRUE;
6519 hashitem_T *hi;
6520 int todo;
6521
6522 d = argvars[0].vval.v_dict;
6523 if (d != NULL)
6524 {
6525 todo = (int)d->dv_hashtab.ht_used;
6526 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6527 {
6528 if (!HASHITEM_EMPTY(hi))
6529 {
6530 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006531 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006532 if (error)
6533 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006534 if (first)
6535 {
6536 n = i;
6537 first = FALSE;
6538 }
6539 else if (domax ? i > n : i < n)
6540 n = i;
6541 }
6542 }
6543 }
6544 }
6545 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006546 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006547
6548 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549}
6550
6551/*
6552 * "max()" function
6553 */
6554 static void
6555f_max(typval_T *argvars, typval_T *rettv)
6556{
6557 max_min(argvars, rettv, TRUE);
6558}
6559
6560/*
6561 * "min()" function
6562 */
6563 static void
6564f_min(typval_T *argvars, typval_T *rettv)
6565{
6566 max_min(argvars, rettv, FALSE);
6567}
6568
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#if defined(FEAT_MZSCHEME) || defined(PROTO)
6570/*
6571 * "mzeval()" function
6572 */
6573 static void
6574f_mzeval(typval_T *argvars, typval_T *rettv)
6575{
6576 char_u *str;
6577 char_u buf[NUMBUFLEN];
6578
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006579 if (check_restricted() || check_secure())
6580 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006581 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 do_mzeval(str, rettv);
6583}
6584
6585 void
6586mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6587{
6588 typval_T argvars[3];
6589
6590 argvars[0].v_type = VAR_STRING;
6591 argvars[0].vval.v_string = name;
6592 copy_tv(args, &argvars[1]);
6593 argvars[2].v_type = VAR_UNKNOWN;
6594 f_call(argvars, rettv);
6595 clear_tv(&argvars[1]);
6596}
6597#endif
6598
6599/*
6600 * "nextnonblank()" function
6601 */
6602 static void
6603f_nextnonblank(typval_T *argvars, typval_T *rettv)
6604{
6605 linenr_T lnum;
6606
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006607 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 {
6609 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6610 {
6611 lnum = 0;
6612 break;
6613 }
6614 if (*skipwhite(ml_get(lnum)) != NUL)
6615 break;
6616 }
6617 rettv->vval.v_number = lnum;
6618}
6619
6620/*
6621 * "nr2char()" function
6622 */
6623 static void
6624f_nr2char(typval_T *argvars, typval_T *rettv)
6625{
6626 char_u buf[NUMBUFLEN];
6627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628 if (has_mbyte)
6629 {
6630 int utf8 = 0;
6631
6632 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006633 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006634 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006635 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006637 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 }
6639 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006641 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 buf[1] = NUL;
6643 }
6644 rettv->v_type = VAR_STRING;
6645 rettv->vval.v_string = vim_strsave(buf);
6646}
6647
6648/*
6649 * "or(expr, expr)" function
6650 */
6651 static void
6652f_or(typval_T *argvars, typval_T *rettv)
6653{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006654 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6655 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656}
6657
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658#ifdef FEAT_PERL
6659/*
6660 * "perleval()" function
6661 */
6662 static void
6663f_perleval(typval_T *argvars, typval_T *rettv)
6664{
6665 char_u *str;
6666 char_u buf[NUMBUFLEN];
6667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006668 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006669 do_perleval(str, rettv);
6670}
6671#endif
6672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673/*
6674 * "prevnonblank()" function
6675 */
6676 static void
6677f_prevnonblank(typval_T *argvars, typval_T *rettv)
6678{
6679 linenr_T lnum;
6680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006681 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6683 lnum = 0;
6684 else
6685 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6686 --lnum;
6687 rettv->vval.v_number = lnum;
6688}
6689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006690// This dummy va_list is here because:
6691// - passing a NULL pointer doesn't work when va_list isn't a pointer
6692// - locally in the function results in a "used before set" warning
6693// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694static va_list ap;
6695
6696/*
6697 * "printf()" function
6698 */
6699 static void
6700f_printf(typval_T *argvars, typval_T *rettv)
6701{
6702 char_u buf[NUMBUFLEN];
6703 int len;
6704 char_u *s;
6705 int saved_did_emsg = did_emsg;
6706 char *fmt;
6707
6708 rettv->v_type = VAR_STRING;
6709 rettv->vval.v_string = NULL;
6710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006711 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006713 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006714 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715 if (!did_emsg)
6716 {
6717 s = alloc(len + 1);
6718 if (s != NULL)
6719 {
6720 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006721 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6722 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723 }
6724 }
6725 did_emsg |= saved_did_emsg;
6726}
6727
6728/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006729 * "pum_getpos()" function
6730 */
6731 static void
6732f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6733{
6734 if (rettv_dict_alloc(rettv) != OK)
6735 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006736 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006737}
6738
6739/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 * "pumvisible()" function
6741 */
6742 static void
6743f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6744{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745 if (pum_visible())
6746 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747}
6748
6749#ifdef FEAT_PYTHON3
6750/*
6751 * "py3eval()" function
6752 */
6753 static void
6754f_py3eval(typval_T *argvars, typval_T *rettv)
6755{
6756 char_u *str;
6757 char_u buf[NUMBUFLEN];
6758
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006759 if (check_restricted() || check_secure())
6760 return;
6761
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006762 if (p_pyx == 0)
6763 p_pyx = 3;
6764
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006765 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 do_py3eval(str, rettv);
6767}
6768#endif
6769
6770#ifdef FEAT_PYTHON
6771/*
6772 * "pyeval()" function
6773 */
6774 static void
6775f_pyeval(typval_T *argvars, typval_T *rettv)
6776{
6777 char_u *str;
6778 char_u buf[NUMBUFLEN];
6779
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006780 if (check_restricted() || check_secure())
6781 return;
6782
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006783 if (p_pyx == 0)
6784 p_pyx = 2;
6785
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006786 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787 do_pyeval(str, rettv);
6788}
6789#endif
6790
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006791#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6792/*
6793 * "pyxeval()" function
6794 */
6795 static void
6796f_pyxeval(typval_T *argvars, typval_T *rettv)
6797{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006798 if (check_restricted() || check_secure())
6799 return;
6800
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006801# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6802 init_pyxversion();
6803 if (p_pyx == 2)
6804 f_pyeval(argvars, rettv);
6805 else
6806 f_py3eval(argvars, rettv);
6807# elif defined(FEAT_PYTHON)
6808 f_pyeval(argvars, rettv);
6809# elif defined(FEAT_PYTHON3)
6810 f_py3eval(argvars, rettv);
6811# endif
6812}
6813#endif
6814
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006815static UINT32_T srand_seed_for_testing = 0;
6816static int srand_seed_for_testing_is_used = FALSE;
6817
6818 static void
6819f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6820{
6821 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006822 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006823 else
6824 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006825 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6826 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006827 }
6828}
6829
6830 static void
6831init_srand(UINT32_T *x)
6832{
6833#ifndef MSWIN
6834 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6835#endif
6836
6837 if (srand_seed_for_testing_is_used)
6838 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006839 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006840 return;
6841 }
6842#ifndef MSWIN
6843 if (dev_urandom_state != FAIL)
6844 {
6845 int fd = open("/dev/urandom", O_RDONLY);
6846 struct {
6847 union {
6848 UINT32_T number;
6849 char bytes[sizeof(UINT32_T)];
6850 } contents;
6851 } buf;
6852
6853 // Attempt reading /dev/urandom.
6854 if (fd == -1)
6855 dev_urandom_state = FAIL;
6856 else
6857 {
6858 buf.contents.number = 0;
6859 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6860 != sizeof(UINT32_T))
6861 dev_urandom_state = FAIL;
6862 else
6863 {
6864 dev_urandom_state = OK;
6865 *x = buf.contents.number;
6866 }
6867 close(fd);
6868 }
6869 }
6870 if (dev_urandom_state != OK)
6871 // Reading /dev/urandom doesn't work, fall back to time().
6872#endif
6873 *x = vim_time();
6874}
6875
6876#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6877#define SPLITMIX32(x, z) ( \
6878 z = (x += 0x9e3779b9), \
6879 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6880 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6881 z ^ (z >> 16) \
6882 )
6883#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6884 result = ROTL(y * 5, 7) * 9; \
6885 t = y << 9; \
6886 z ^= x; \
6887 w ^= y; \
6888 y ^= z, x ^= w; \
6889 z ^= t; \
6890 w = ROTL(w, 11);
6891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006893 * "rand()" function
6894 */
6895 static void
6896f_rand(typval_T *argvars, typval_T *rettv)
6897{
6898 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006899 static UINT32_T gx, gy, gz, gw;
6900 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006901 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006902 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006903
6904 if (argvars[0].v_type == VAR_UNKNOWN)
6905 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006906 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006907 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006908 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006909 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006910 init_srand(&x);
6911
6912 gx = SPLITMIX32(x, z);
6913 gy = SPLITMIX32(x, z);
6914 gz = SPLITMIX32(x, z);
6915 gw = SPLITMIX32(x, z);
6916 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006917 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006918
6919 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006920 }
6921 else if (argvars[0].v_type == VAR_LIST)
6922 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006923 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006924 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006925 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006926
6927 lx = list_find(l, 0L);
6928 ly = list_find(l, 1L);
6929 lz = list_find(l, 2L);
6930 lw = list_find(l, 3L);
6931 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6932 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6933 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6934 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6935 x = (UINT32_T)lx->li_tv.vval.v_number;
6936 y = (UINT32_T)ly->li_tv.vval.v_number;
6937 z = (UINT32_T)lz->li_tv.vval.v_number;
6938 w = (UINT32_T)lw->li_tv.vval.v_number;
6939
6940 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6941
6942 lx->li_tv.vval.v_number = (varnumber_T)x;
6943 ly->li_tv.vval.v_number = (varnumber_T)y;
6944 lz->li_tv.vval.v_number = (varnumber_T)z;
6945 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006946 }
6947 else
6948 goto theend;
6949
6950 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006951 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006952 return;
6953
6954theend:
6955 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006956 rettv->v_type = VAR_NUMBER;
6957 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006958}
6959
6960/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006961 * "srand()" function
6962 */
6963 static void
6964f_srand(typval_T *argvars, typval_T *rettv)
6965{
6966 UINT32_T x = 0, z;
6967
6968 if (rettv_list_alloc(rettv) == FAIL)
6969 return;
6970 if (argvars[0].v_type == VAR_UNKNOWN)
6971 {
6972 init_srand(&x);
6973 }
6974 else
6975 {
6976 int error = FALSE;
6977
6978 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6979 if (error)
6980 return;
6981 }
6982
6983 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6984 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6985 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6986 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6987}
6988
6989#undef ROTL
6990#undef SPLITMIX32
6991#undef SHUFFLE_XOSHIRO128STARSTAR
6992
6993/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 * "range()" function
6995 */
6996 static void
6997f_range(typval_T *argvars, typval_T *rettv)
6998{
6999 varnumber_T start;
7000 varnumber_T end;
7001 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002 int error = FALSE;
7003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007004 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 if (argvars[1].v_type == VAR_UNKNOWN)
7006 {
7007 end = start - 1;
7008 start = 0;
7009 }
7010 else
7011 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007012 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007014 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015 }
7016
7017 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007018 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007020 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007022 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007023 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 list_T *list = rettv->vval.v_list;
7026
7027 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007028 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007029 // be called.
7030 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007031 list->lv_u.nonmat.lv_start = start;
7032 list->lv_u.nonmat.lv_end = end;
7033 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007034 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 }
7036}
7037
7038/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007039 * Materialize "list".
7040 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 */
7042 void
7043range_list_materialize(list_T *list)
7044{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007045 varnumber_T start = list->lv_u.nonmat.lv_start;
7046 varnumber_T end = list->lv_u.nonmat.lv_end;
7047 int stride = list->lv_u.nonmat.lv_stride;
7048 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007049
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007050 list->lv_first = NULL;
7051 list->lv_u.mat.lv_last = NULL;
7052 list->lv_len = 0;
7053 list->lv_u.mat.lv_idx_item = NULL;
7054 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7055 if (list_append_number(list, (varnumber_T)i) == FAIL)
7056 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057}
7058
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007059/*
7060 * "getreginfo()" function
7061 */
7062 static void
7063f_getreginfo(typval_T *argvars, typval_T *rettv)
7064{
7065 char_u *strregname;
7066 int regname;
7067 char_u buf[NUMBUFLEN + 2];
7068 long reglen = 0;
7069 dict_T *dict;
7070 list_T *list;
7071
7072 if (argvars[0].v_type != VAR_UNKNOWN)
7073 {
7074 strregname = tv_get_string_chk(&argvars[0]);
7075 if (strregname == NULL)
7076 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007077 if (in_vim9script() && STRLEN(strregname) > 1)
7078 {
7079 semsg(_(e_register_name_must_be_one_char_str), strregname);
7080 return;
7081 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007082 }
7083 else
7084 strregname = get_vim_var_str(VV_REG);
7085
7086 regname = (strregname == NULL ? '"' : *strregname);
7087 if (regname == 0 || regname == '@')
7088 regname = '"';
7089
7090 if (rettv_dict_alloc(rettv) == FAIL)
7091 return;
7092 dict = rettv->vval.v_dict;
7093
7094 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7095 if (list == NULL)
7096 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007097 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007098
7099 buf[0] = NUL;
7100 buf[1] = NUL;
7101 switch (get_reg_type(regname, &reglen))
7102 {
7103 case MLINE: buf[0] = 'V'; break;
7104 case MCHAR: buf[0] = 'v'; break;
7105 case MBLOCK:
7106 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7107 reglen + 1);
7108 break;
7109 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007110 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007111
7112 buf[0] = get_register_name(get_unname_register());
7113 buf[1] = NUL;
7114 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007115 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007116 else
7117 {
7118 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7119
7120 if (item != NULL)
7121 {
7122 item->di_tv.v_type = VAR_SPECIAL;
7123 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007124 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007125 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007126 }
7127 }
7128}
7129
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007130 static void
7131return_register(int regname, typval_T *rettv)
7132{
7133 char_u buf[2] = {0, 0};
7134
7135 buf[0] = (char_u)regname;
7136 rettv->v_type = VAR_STRING;
7137 rettv->vval.v_string = vim_strsave(buf);
7138}
7139
7140/*
7141 * "reg_executing()" function
7142 */
7143 static void
7144f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7145{
7146 return_register(reg_executing, rettv);
7147}
7148
7149/*
7150 * "reg_recording()" function
7151 */
7152 static void
7153f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7154{
7155 return_register(reg_recording, rettv);
7156}
7157
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007158/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 * "rename({from}, {to})" function
7160 */
7161 static void
7162f_rename(typval_T *argvars, typval_T *rettv)
7163{
7164 char_u buf[NUMBUFLEN];
7165
7166 if (check_restricted() || check_secure())
7167 rettv->vval.v_number = -1;
7168 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007169 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7170 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171}
7172
7173/*
7174 * "repeat()" function
7175 */
7176 static void
7177f_repeat(typval_T *argvars, typval_T *rettv)
7178{
7179 char_u *p;
7180 int n;
7181 int slen;
7182 int len;
7183 char_u *r;
7184 int i;
7185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 if (argvars[0].v_type == VAR_LIST)
7188 {
7189 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7190 while (n-- > 0)
7191 if (list_extend(rettv->vval.v_list,
7192 argvars[0].vval.v_list, NULL) == FAIL)
7193 break;
7194 }
7195 else
7196 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007197 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 rettv->v_type = VAR_STRING;
7199 rettv->vval.v_string = NULL;
7200
7201 slen = (int)STRLEN(p);
7202 len = slen * n;
7203 if (len <= 0)
7204 return;
7205
7206 r = alloc(len + 1);
7207 if (r != NULL)
7208 {
7209 for (i = 0; i < n; i++)
7210 mch_memmove(r + i * slen, p, (size_t)slen);
7211 r[len] = NUL;
7212 }
7213
7214 rettv->vval.v_string = r;
7215 }
7216}
7217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007218#define SP_NOMOVE 0x01 // don't move cursor
7219#define SP_REPEAT 0x02 // repeat to find outer pair
7220#define SP_RETCOUNT 0x04 // return matchcount
7221#define SP_SETPCMARK 0x08 // set previous context mark
7222#define SP_START 0x10 // accept match at start position
7223#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7224#define SP_END 0x40 // leave cursor at end of match
7225#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227/*
7228 * Get flags for a search function.
7229 * Possibly sets "p_ws".
7230 * Returns BACKWARD, FORWARD or zero (for an error).
7231 */
7232 static int
7233get_search_arg(typval_T *varp, int *flagsp)
7234{
7235 int dir = FORWARD;
7236 char_u *flags;
7237 char_u nbuf[NUMBUFLEN];
7238 int mask;
7239
7240 if (varp->v_type != VAR_UNKNOWN)
7241 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007242 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007243 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007244 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 while (*flags != NUL)
7246 {
7247 switch (*flags)
7248 {
7249 case 'b': dir = BACKWARD; break;
7250 case 'w': p_ws = TRUE; break;
7251 case 'W': p_ws = FALSE; break;
7252 default: mask = 0;
7253 if (flagsp != NULL)
7254 switch (*flags)
7255 {
7256 case 'c': mask = SP_START; break;
7257 case 'e': mask = SP_END; break;
7258 case 'm': mask = SP_RETCOUNT; break;
7259 case 'n': mask = SP_NOMOVE; break;
7260 case 'p': mask = SP_SUBPAT; break;
7261 case 'r': mask = SP_REPEAT; break;
7262 case 's': mask = SP_SETPCMARK; break;
7263 case 'z': mask = SP_COLUMN; break;
7264 }
7265 if (mask == 0)
7266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007267 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007268 dir = 0;
7269 }
7270 else
7271 *flagsp |= mask;
7272 }
7273 if (dir == 0)
7274 break;
7275 ++flags;
7276 }
7277 }
7278 return dir;
7279}
7280
7281/*
7282 * Shared by search() and searchpos() functions.
7283 */
7284 static int
7285search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7286{
7287 int flags;
7288 char_u *pat;
7289 pos_T pos;
7290 pos_T save_cursor;
7291 int save_p_ws = p_ws;
7292 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007293 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007296 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 long time_limit = 0;
7298#endif
7299 int options = SEARCH_KEEP;
7300 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007301 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007302 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007303 pos_T firstpos;
7304
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007305 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007306 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 if (dir == 0)
7308 goto theend;
7309 flags = *flagsp;
7310 if (flags & SP_START)
7311 options |= SEARCH_START;
7312 if (flags & SP_END)
7313 options |= SEARCH_END;
7314 if (flags & SP_COLUMN)
7315 options |= SEARCH_COL;
7316
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007317 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007320 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321 if (lnum_stop < 0)
7322 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 if (argvars[3].v_type != VAR_UNKNOWN)
7324 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007325#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007326 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 if (time_limit < 0)
7328 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007330 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007331 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332 }
7333
7334#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007335 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 profile_setlimit(time_limit, &tm);
7337#endif
7338
7339 /*
7340 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7341 * Check to make sure only those flags are set.
7342 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7343 * flags cannot be set. Check for that condition also.
7344 */
7345 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7346 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007348 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 goto theend;
7350 }
7351
7352 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007353 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007354 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007355 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7356#ifdef FEAT_RELTIME
7357 sia.sa_tm = &tm;
7358#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007359
7360 // Repeat until {skip} returns FALSE.
7361 for (;;)
7362 {
7363 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007364 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007365 // finding the first match again means there is no match where {skip}
7366 // evaluates to zero.
7367 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7368 subpatnum = FAIL;
7369
Bram Moolenaara9c01042020-06-07 14:50:50 +02007370 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007371 // didn't find it or no skip argument
7372 break;
7373 firstpos = pos;
7374
Bram Moolenaara9c01042020-06-07 14:50:50 +02007375 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007376 {
7377 int do_skip;
7378 int err;
7379 pos_T save_pos = curwin->w_cursor;
7380
7381 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007382 err = FALSE;
7383 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007384 curwin->w_cursor = save_pos;
7385 if (err)
7386 {
7387 // Evaluating {skip} caused an error, break here.
7388 subpatnum = FAIL;
7389 break;
7390 }
7391 if (!do_skip)
7392 break;
7393 }
7394 }
7395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396 if (subpatnum != FAIL)
7397 {
7398 if (flags & SP_SUBPAT)
7399 retval = subpatnum;
7400 else
7401 retval = pos.lnum;
7402 if (flags & SP_SETPCMARK)
7403 setpcmark();
7404 curwin->w_cursor = pos;
7405 if (match_pos != NULL)
7406 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007407 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007408 match_pos->lnum = pos.lnum;
7409 match_pos->col = pos.col + 1;
7410 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007411 // "/$" will put the cursor after the end of the line, may need to
7412 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 check_cursor();
7414 }
7415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007416 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417 if (flags & SP_NOMOVE)
7418 curwin->w_cursor = save_cursor;
7419 else
7420 curwin->w_set_curswant = TRUE;
7421theend:
7422 p_ws = save_p_ws;
7423
7424 return retval;
7425}
7426
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007427#ifdef FEAT_RUBY
7428/*
7429 * "rubyeval()" function
7430 */
7431 static void
7432f_rubyeval(typval_T *argvars, typval_T *rettv)
7433{
7434 char_u *str;
7435 char_u buf[NUMBUFLEN];
7436
7437 str = tv_get_string_buf(&argvars[0], buf);
7438 do_rubyeval(str, rettv);
7439}
7440#endif
7441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442/*
7443 * "screenattr()" function
7444 */
7445 static void
7446f_screenattr(typval_T *argvars, typval_T *rettv)
7447{
7448 int row;
7449 int col;
7450 int c;
7451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007452 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7453 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454 if (row < 0 || row >= screen_Rows
7455 || col < 0 || col >= screen_Columns)
7456 c = -1;
7457 else
7458 c = ScreenAttrs[LineOffset[row] + col];
7459 rettv->vval.v_number = c;
7460}
7461
7462/*
7463 * "screenchar()" function
7464 */
7465 static void
7466f_screenchar(typval_T *argvars, typval_T *rettv)
7467{
7468 int row;
7469 int col;
7470 int off;
7471 int c;
7472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007473 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7474 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007475 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476 c = -1;
7477 else
7478 {
7479 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480 if (enc_utf8 && ScreenLinesUC[off] != 0)
7481 c = ScreenLinesUC[off];
7482 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 c = ScreenLines[off];
7484 }
7485 rettv->vval.v_number = c;
7486}
7487
7488/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007489 * "screenchars()" function
7490 */
7491 static void
7492f_screenchars(typval_T *argvars, typval_T *rettv)
7493{
7494 int row;
7495 int col;
7496 int off;
7497 int c;
7498 int i;
7499
7500 if (rettv_list_alloc(rettv) == FAIL)
7501 return;
7502 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7503 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7504 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7505 return;
7506
7507 off = LineOffset[row] + col;
7508 if (enc_utf8 && ScreenLinesUC[off] != 0)
7509 c = ScreenLinesUC[off];
7510 else
7511 c = ScreenLines[off];
7512 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7513
7514 if (enc_utf8)
7515
7516 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7517 list_append_number(rettv->vval.v_list,
7518 (varnumber_T)ScreenLinesC[i][off]);
7519}
7520
7521/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 * "screencol()" function
7523 *
7524 * First column is 1 to be consistent with virtcol().
7525 */
7526 static void
7527f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7528{
7529 rettv->vval.v_number = screen_screencol() + 1;
7530}
7531
7532/*
7533 * "screenrow()" function
7534 */
7535 static void
7536f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7537{
7538 rettv->vval.v_number = screen_screenrow() + 1;
7539}
7540
7541/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007542 * "screenstring()" function
7543 */
7544 static void
7545f_screenstring(typval_T *argvars, typval_T *rettv)
7546{
7547 int row;
7548 int col;
7549 int off;
7550 int c;
7551 int i;
7552 char_u buf[MB_MAXBYTES + 1];
7553 int buflen = 0;
7554
7555 rettv->vval.v_string = NULL;
7556 rettv->v_type = VAR_STRING;
7557
7558 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7559 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7560 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7561 return;
7562
7563 off = LineOffset[row] + col;
7564 if (enc_utf8 && ScreenLinesUC[off] != 0)
7565 c = ScreenLinesUC[off];
7566 else
7567 c = ScreenLines[off];
7568 buflen += mb_char2bytes(c, buf);
7569
Bram Moolenaarf1387282021-03-22 17:11:15 +01007570 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007571 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7572 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7573
7574 buf[buflen] = NUL;
7575 rettv->vval.v_string = vim_strsave(buf);
7576}
7577
7578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579 * "search()" function
7580 */
7581 static void
7582f_search(typval_T *argvars, typval_T *rettv)
7583{
7584 int flags = 0;
7585
7586 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7587}
7588
7589/*
7590 * "searchdecl()" function
7591 */
7592 static void
7593f_searchdecl(typval_T *argvars, typval_T *rettv)
7594{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007595 int locally = TRUE;
7596 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 int error = FALSE;
7598 char_u *name;
7599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007600 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007602 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 if (argvars[1].v_type != VAR_UNKNOWN)
7604 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007605 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007607 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 }
7609 if (!error && name != NULL)
7610 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7611 locally, thisblock, SEARCH_KEEP) == FAIL;
7612}
7613
7614/*
7615 * Used by searchpair() and searchpairpos()
7616 */
7617 static int
7618searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7619{
7620 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007621 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 int save_p_ws = p_ws;
7623 int dir;
7624 int flags = 0;
7625 char_u nbuf1[NUMBUFLEN];
7626 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007627 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 long lnum_stop = 0;
7629 long time_limit = 0;
7630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007631 // Get the three pattern arguments: start, middle, end. Will result in an
7632 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007633 spat = tv_get_string_chk(&argvars[0]);
7634 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7635 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007637 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007639 // Handle the optional fourth argument: flags
7640 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 if (dir == 0)
7642 goto theend;
7643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007644 // Don't accept SP_END or SP_SUBPAT.
7645 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007646 if ((flags & (SP_END | SP_SUBPAT)) != 0
7647 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7648 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007649 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 goto theend;
7651 }
7652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007653 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 if (flags & SP_REPEAT)
7655 p_ws = FALSE;
7656
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007657 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 if (argvars[3].v_type == VAR_UNKNOWN
7659 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007660 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 else
7662 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007663 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007664 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 if (argvars[5].v_type != VAR_UNKNOWN)
7667 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007668 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007669 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007671 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007673 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674#ifdef FEAT_RELTIME
7675 if (argvars[6].v_type != VAR_UNKNOWN)
7676 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007677 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
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 }
7684#endif
7685 }
7686 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687
7688 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7689 match_pos, lnum_stop, time_limit);
7690
7691theend:
7692 p_ws = save_p_ws;
7693
7694 return retval;
7695}
7696
7697/*
7698 * "searchpair()" function
7699 */
7700 static void
7701f_searchpair(typval_T *argvars, typval_T *rettv)
7702{
7703 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7704}
7705
7706/*
7707 * "searchpairpos()" function
7708 */
7709 static void
7710f_searchpairpos(typval_T *argvars, typval_T *rettv)
7711{
7712 pos_T match_pos;
7713 int lnum = 0;
7714 int col = 0;
7715
7716 if (rettv_list_alloc(rettv) == FAIL)
7717 return;
7718
7719 if (searchpair_cmn(argvars, &match_pos) > 0)
7720 {
7721 lnum = match_pos.lnum;
7722 col = match_pos.col;
7723 }
7724
7725 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7726 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7727}
7728
7729/*
7730 * Search for a start/middle/end thing.
7731 * Used by searchpair(), see its documentation for the details.
7732 * Returns 0 or -1 for no match,
7733 */
7734 long
7735do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007736 char_u *spat, // start pattern
7737 char_u *mpat, // middle pattern
7738 char_u *epat, // end pattern
7739 int dir, // BACKWARD or FORWARD
7740 typval_T *skip, // skip expression
7741 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007743 linenr_T lnum_stop, // stop at this line if not zero
7744 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745{
7746 char_u *save_cpo;
7747 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7748 long retval = 0;
7749 pos_T pos;
7750 pos_T firstpos;
7751 pos_T foundpos;
7752 pos_T save_cursor;
7753 pos_T save_pos;
7754 int n;
7755 int r;
7756 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007757 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 int err;
7759 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007760#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007764 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765 save_cpo = p_cpo;
7766 p_cpo = empty_option;
7767
7768#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007769 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 profile_setlimit(time_limit, &tm);
7771#endif
7772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007773 // Make two search patterns: start/end (pat2, for in nested pairs) and
7774 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007775 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7776 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 if (pat2 == NULL || pat3 == NULL)
7778 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007779 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780 if (*mpat == NUL)
7781 STRCPY(pat3, pat2);
7782 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007783 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784 spat, epat, mpat);
7785 if (flags & SP_START)
7786 options |= SEARCH_START;
7787
Bram Moolenaar48570482017-10-30 21:48:41 +01007788 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007789 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791 save_cursor = curwin->w_cursor;
7792 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007793 CLEAR_POS(&firstpos);
7794 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795 pat = pat3;
7796 for (;;)
7797 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007798 searchit_arg_T sia;
7799
Bram Moolenaara80faa82020-04-12 19:37:17 +02007800 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007801 sia.sa_stop_lnum = lnum_stop;
7802#ifdef FEAT_RELTIME
7803 sia.sa_tm = &tm;
7804#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007805 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007806 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007807 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007808 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 break;
7810
7811 if (firstpos.lnum == 0)
7812 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007813 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007815 // Found the same position again. Can happen with a pattern that
7816 // has "\zs" at the end and searching backwards. Advance one
7817 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007818 if (dir == BACKWARD)
7819 decl(&pos);
7820 else
7821 incl(&pos);
7822 }
7823 foundpos = pos;
7824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007825 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 options &= ~SEARCH_START;
7827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007828 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007829 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 {
7831 save_pos = curwin->w_cursor;
7832 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007833 err = FALSE;
7834 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 curwin->w_cursor = save_pos;
7836 if (err)
7837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007838 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 curwin->w_cursor = save_cursor;
7840 retval = -1;
7841 break;
7842 }
7843 if (r)
7844 continue;
7845 }
7846
7847 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7848 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007849 // Found end when searching backwards or start when searching
7850 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007852 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 }
7854 else
7855 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007856 // Found end when searching forward or start when searching
7857 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007859 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860 }
7861
7862 if (nest == 0)
7863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007864 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865 if (flags & SP_RETCOUNT)
7866 ++retval;
7867 else
7868 retval = pos.lnum;
7869 if (flags & SP_SETPCMARK)
7870 setpcmark();
7871 curwin->w_cursor = pos;
7872 if (!(flags & SP_REPEAT))
7873 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007874 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 }
7876 }
7877
7878 if (match_pos != NULL)
7879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007880 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 match_pos->lnum = curwin->w_cursor.lnum;
7882 match_pos->col = curwin->w_cursor.col + 1;
7883 }
7884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007885 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 if ((flags & SP_NOMOVE) || retval == 0)
7887 curwin->w_cursor = save_cursor;
7888
7889theend:
7890 vim_free(pat2);
7891 vim_free(pat3);
7892 if (p_cpo == empty_option)
7893 p_cpo = save_cpo;
7894 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007896 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007897 // If it's still empty it was changed and restored, need to restore in
7898 // the complicated way.
7899 if (*p_cpo == NUL)
7900 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007902 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903
7904 return retval;
7905}
7906
7907/*
7908 * "searchpos()" function
7909 */
7910 static void
7911f_searchpos(typval_T *argvars, typval_T *rettv)
7912{
7913 pos_T match_pos;
7914 int lnum = 0;
7915 int col = 0;
7916 int n;
7917 int flags = 0;
7918
7919 if (rettv_list_alloc(rettv) == FAIL)
7920 return;
7921
7922 n = search_cmn(argvars, &match_pos, &flags);
7923 if (n > 0)
7924 {
7925 lnum = match_pos.lnum;
7926 col = match_pos.col;
7927 }
7928
7929 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7930 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7931 if (flags & SP_SUBPAT)
7932 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7933}
7934
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007935/*
7936 * Set the cursor or mark position.
7937 * If 'charpos' is TRUE, then use the column number as a character offet.
7938 * Otherwise use the column number as a byte offset.
7939 */
7940 static void
7941set_position(typval_T *argvars, typval_T *rettv, int charpos)
7942{
7943 pos_T pos;
7944 int fnum;
7945 char_u *name;
7946 colnr_T curswant = -1;
7947
7948 rettv->vval.v_number = -1;
7949
7950 name = tv_get_string_chk(argvars);
7951 if (name != NULL)
7952 {
7953 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7954 {
7955 if (pos.col != MAXCOL && --pos.col < 0)
7956 pos.col = 0;
7957 if ((name[0] == '.' && name[1] == NUL))
7958 {
7959 // set cursor; "fnum" is ignored
7960 curwin->w_cursor = pos;
7961 if (curswant >= 0)
7962 {
7963 curwin->w_curswant = curswant - 1;
7964 curwin->w_set_curswant = FALSE;
7965 }
7966 check_cursor();
7967 rettv->vval.v_number = 0;
7968 }
7969 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7970 {
7971 // set mark
7972 if (setmark_pos(name[1], &pos, fnum) == OK)
7973 rettv->vval.v_number = 0;
7974 }
7975 else
7976 emsg(_(e_invarg));
7977 }
7978 }
7979}
7980/*
7981 * "setcharpos()" function
7982 */
7983 static void
7984f_setcharpos(typval_T *argvars, typval_T *rettv)
7985{
7986 set_position(argvars, rettv, TRUE);
7987}
7988
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7991{
7992 dict_T *d;
7993 dictitem_T *di;
7994 char_u *csearch;
7995
7996 if (argvars[0].v_type != VAR_DICT)
7997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007998 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 return;
8000 }
8001
8002 if ((d = argvars[0].vval.v_dict) != NULL)
8003 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008004 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 if (csearch != NULL)
8006 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 if (enc_utf8)
8008 {
8009 int pcc[MAX_MCO];
8010 int c = utfc_ptr2char(csearch, pcc);
8011
8012 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8013 }
8014 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008016 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 }
8018
8019 di = dict_find(d, (char_u *)"forward", -1);
8020 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008021 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 ? FORWARD : BACKWARD);
8023
8024 di = dict_find(d, (char_u *)"until", -1);
8025 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008026 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 }
8028}
8029
8030/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008031 * "setcursorcharpos" function
8032 */
8033 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008034f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008035{
8036 set_cursorpos(argvars, rettv, TRUE);
8037}
8038
8039/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008040 * "setenv()" function
8041 */
8042 static void
8043f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8044{
8045 char_u namebuf[NUMBUFLEN];
8046 char_u valbuf[NUMBUFLEN];
8047 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8048
8049 if (argvars[1].v_type == VAR_SPECIAL
8050 && argvars[1].vval.v_number == VVAL_NULL)
8051 vim_unsetenv(name);
8052 else
8053 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8054}
8055
8056/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 * "setfperm({fname}, {mode})" function
8058 */
8059 static void
8060f_setfperm(typval_T *argvars, typval_T *rettv)
8061{
8062 char_u *fname;
8063 char_u modebuf[NUMBUFLEN];
8064 char_u *mode_str;
8065 int i;
8066 int mask;
8067 int mode = 0;
8068
8069 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008070 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 if (fname == NULL)
8072 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008073 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 if (mode_str == NULL)
8075 return;
8076 if (STRLEN(mode_str) != 9)
8077 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008078 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008079 return;
8080 }
8081
8082 mask = 1;
8083 for (i = 8; i >= 0; --i)
8084 {
8085 if (mode_str[i] != '-')
8086 mode |= mask;
8087 mask = mask << 1;
8088 }
8089 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8090}
8091
8092/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008093 * "setpos()" function
8094 */
8095 static void
8096f_setpos(typval_T *argvars, typval_T *rettv)
8097{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008098 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099}
8100
8101/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008102 * Translate a register type string to the yank type and block length
8103 */
8104 static int
8105get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8106{
8107 char_u *stropt = *pp;
8108 switch (*stropt)
8109 {
8110 case 'v': case 'c': // character-wise selection
8111 *yank_type = MCHAR;
8112 break;
8113 case 'V': case 'l': // line-wise selection
8114 *yank_type = MLINE;
8115 break;
8116 case 'b': case Ctrl_V: // block-wise selection
8117 *yank_type = MBLOCK;
8118 if (VIM_ISDIGIT(stropt[1]))
8119 {
8120 ++stropt;
8121 *block_len = getdigits(&stropt) - 1;
8122 --stropt;
8123 }
8124 break;
8125 default:
8126 return FAIL;
8127 }
8128 *pp = stropt;
8129 return OK;
8130}
8131
8132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 * "setreg()" function
8134 */
8135 static void
8136f_setreg(typval_T *argvars, typval_T *rettv)
8137{
8138 int regname;
8139 char_u *strregname;
8140 char_u *stropt;
8141 char_u *strval;
8142 int append;
8143 char_u yank_type;
8144 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008145 typval_T *regcontents;
8146 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008147
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008148 pointreg = 0;
8149 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 block_len = -1;
8151 yank_type = MAUTO;
8152 append = FALSE;
8153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008154 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008155 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156
8157 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008158 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008159 if (in_vim9script() && STRLEN(strregname) > 1)
8160 {
8161 semsg(_(e_register_name_must_be_one_char_str), strregname);
8162 return;
8163 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 regname = *strregname;
8165 if (regname == 0 || regname == '@')
8166 regname = '"';
8167
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008168 if (argvars[1].v_type == VAR_DICT)
8169 {
8170 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008171 dictitem_T *di;
8172
8173 if (d == NULL || d->dv_hashtab.ht_used == 0)
8174 {
8175 // Empty dict, clear the register (like setreg(0, []))
8176 char_u *lstval[2] = {NULL, NULL};
8177 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8178 return;
8179 }
8180
8181 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008182 if (di != NULL)
8183 regcontents = &di->di_tv;
8184
8185 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8186 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008187 {
8188 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8189
8190 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008191 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008192 semsg(_(e_invargval), "value");
8193 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008194 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008195 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008196
8197 if (regname == '"')
8198 {
8199 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8200 if (stropt != NULL)
8201 {
8202 pointreg = *stropt;
8203 regname = pointreg;
8204 }
8205 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008206 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008207 pointreg = regname;
8208 }
8209 else
8210 regcontents = &argvars[1];
8211
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 if (argvars[2].v_type != VAR_UNKNOWN)
8213 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008214 if (yank_type != MAUTO)
8215 {
8216 semsg(_(e_toomanyarg), "setreg");
8217 return;
8218 }
8219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008220 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008222 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 for (; *stropt != NUL; ++stropt)
8224 switch (*stropt)
8225 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008226 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227 append = TRUE;
8228 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008229 default:
8230 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 }
8232 }
8233
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008234 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235 {
8236 char_u **lstval;
8237 char_u **allocval;
8238 char_u buf[NUMBUFLEN];
8239 char_u **curval;
8240 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008241 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008242 listitem_T *li;
8243 int len;
8244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008245 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246 len = ll == NULL ? 0 : ll->lv_len;
8247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008248 // First half: use for pointers to result lines; second half: use for
8249 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008250 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251 if (lstval == NULL)
8252 return;
8253 curval = lstval;
8254 allocval = lstval + len + 2;
8255 curallocval = allocval;
8256
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008257 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008259 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008260 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008262 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 if (strval == NULL)
8264 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008265 if (strval == buf)
8266 {
8267 // Need to make a copy, next tv_get_string_buf_chk() will
8268 // overwrite the string.
8269 strval = vim_strsave(buf);
8270 if (strval == NULL)
8271 goto free_lstval;
8272 *curallocval++ = strval;
8273 }
8274 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 }
8277 *curval++ = NULL;
8278
8279 write_reg_contents_lst(regname, lstval, -1,
8280 append, yank_type, block_len);
8281free_lstval:
8282 while (curallocval > allocval)
8283 vim_free(*--curallocval);
8284 vim_free(lstval);
8285 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008286 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008288 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (strval == NULL)
8290 return;
8291 write_reg_contents_ex(regname, strval, -1,
8292 append, yank_type, block_len);
8293 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008294 if (pointreg != 0)
8295 get_yank_register(pointreg, TRUE);
8296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 rettv->vval.v_number = 0;
8298}
8299
8300/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008301 * "settagstack()" function
8302 */
8303 static void
8304f_settagstack(typval_T *argvars, typval_T *rettv)
8305{
8306 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8307 win_T *wp;
8308 dict_T *d;
8309 int action = 'r';
8310
8311 rettv->vval.v_number = -1;
8312
8313 // first argument: window number or id
8314 wp = find_win_by_nr_or_id(&argvars[0]);
8315 if (wp == NULL)
8316 return;
8317
8318 // second argument: dict with items to set in the tag stack
8319 if (argvars[1].v_type != VAR_DICT)
8320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008321 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008322 return;
8323 }
8324 d = argvars[1].vval.v_dict;
8325 if (d == NULL)
8326 return;
8327
8328 // third argument: action - 'a' for append and 'r' for replace.
8329 // default is to replace the stack.
8330 if (argvars[2].v_type == VAR_UNKNOWN)
8331 action = 'r';
8332 else if (argvars[2].v_type == VAR_STRING)
8333 {
8334 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008335 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008336 if (actstr == NULL)
8337 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008338 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8339 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008340 action = *actstr;
8341 else
8342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008343 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008344 return;
8345 }
8346 }
8347 else
8348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008349 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008350 return;
8351 }
8352
8353 if (set_tagstack(wp, d, action) == OK)
8354 rettv->vval.v_number = 0;
8355}
8356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357#ifdef FEAT_CRYPT
8358/*
8359 * "sha256({string})" function
8360 */
8361 static void
8362f_sha256(typval_T *argvars, typval_T *rettv)
8363{
8364 char_u *p;
8365
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 rettv->vval.v_string = vim_strsave(
8368 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8369 rettv->v_type = VAR_STRING;
8370}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008371#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372
8373/*
8374 * "shellescape({string})" function
8375 */
8376 static void
8377f_shellescape(typval_T *argvars, typval_T *rettv)
8378{
Bram Moolenaar20615522017-06-05 18:46:26 +02008379 int do_special = non_zero_arg(&argvars[1]);
8380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008382 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 rettv->v_type = VAR_STRING;
8384}
8385
8386/*
8387 * shiftwidth() function
8388 */
8389 static void
8390f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8391{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008392 rettv->vval.v_number = 0;
8393
8394 if (argvars[0].v_type != VAR_UNKNOWN)
8395 {
8396 long col;
8397
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008399 if (col < 0)
8400 return; // type error; errmsg already given
8401#ifdef FEAT_VARTABS
8402 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8403 return;
8404#endif
8405 }
8406
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 rettv->vval.v_number = get_sw_value(curbuf);
8408}
8409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410/*
8411 * "soundfold({word})" function
8412 */
8413 static void
8414f_soundfold(typval_T *argvars, typval_T *rettv)
8415{
8416 char_u *s;
8417
8418 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420#ifdef FEAT_SPELL
8421 rettv->vval.v_string = eval_soundfold(s);
8422#else
8423 rettv->vval.v_string = vim_strsave(s);
8424#endif
8425}
8426
8427/*
8428 * "spellbadword()" function
8429 */
8430 static void
8431f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8432{
8433 char_u *word = (char_u *)"";
8434 hlf_T attr = HLF_COUNT;
8435 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008436#ifdef FEAT_SPELL
8437 int wo_spell_save = curwin->w_p_spell;
8438
8439 if (!curwin->w_p_spell)
8440 {
8441 did_set_spelllang(curwin);
8442 curwin->w_p_spell = TRUE;
8443 }
8444
8445 if (*curwin->w_s->b_p_spl == NUL)
8446 {
8447 emsg(_(e_no_spell));
8448 curwin->w_p_spell = wo_spell_save;
8449 return;
8450 }
8451#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452
8453 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008454 {
8455#ifdef FEAT_SPELL
8456 curwin->w_p_spell = wo_spell_save;
8457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008458 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460
8461#ifdef FEAT_SPELL
8462 if (argvars[0].v_type == VAR_UNKNOWN)
8463 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008464 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8466 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008467 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008469 curwin->w_set_curswant = TRUE;
8470 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008471 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008472 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008474 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 int capcol = -1;
8476
8477 if (str != NULL)
8478 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008479 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 while (*str != NUL)
8481 {
8482 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8483 if (attr != HLF_COUNT)
8484 {
8485 word = str;
8486 break;
8487 }
8488 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008489 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008490 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 }
8492 }
8493 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008494 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495#endif
8496
8497 list_append_string(rettv->vval.v_list, word, len);
8498 list_append_string(rettv->vval.v_list, (char_u *)(
8499 attr == HLF_SPB ? "bad" :
8500 attr == HLF_SPR ? "rare" :
8501 attr == HLF_SPL ? "local" :
8502 attr == HLF_SPC ? "caps" :
8503 ""), -1);
8504}
8505
8506/*
8507 * "spellsuggest()" function
8508 */
8509 static void
8510f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8511{
8512#ifdef FEAT_SPELL
8513 char_u *str;
8514 int typeerr = FALSE;
8515 int maxcount;
8516 garray_T ga;
8517 int i;
8518 listitem_T *li;
8519 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008520 int wo_spell_save = curwin->w_p_spell;
8521
8522 if (!curwin->w_p_spell)
8523 {
8524 did_set_spelllang(curwin);
8525 curwin->w_p_spell = TRUE;
8526 }
8527
8528 if (*curwin->w_s->b_p_spl == NUL)
8529 {
8530 emsg(_(e_no_spell));
8531 curwin->w_p_spell = wo_spell_save;
8532 return;
8533 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534#endif
8535
8536 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008537 {
8538#ifdef FEAT_SPELL
8539 curwin->w_p_spell = wo_spell_save;
8540#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543
8544#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008545 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008547 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 if (argvars[1].v_type != VAR_UNKNOWN)
8549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008550 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 if (maxcount <= 0)
8552 return;
8553 if (argvars[2].v_type != VAR_UNKNOWN)
8554 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008555 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556 if (typeerr)
8557 return;
8558 }
8559 }
8560 else
8561 maxcount = 25;
8562
8563 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8564
8565 for (i = 0; i < ga.ga_len; ++i)
8566 {
8567 str = ((char_u **)ga.ga_data)[i];
8568
8569 li = listitem_alloc();
8570 if (li == NULL)
8571 vim_free(str);
8572 else
8573 {
8574 li->li_tv.v_type = VAR_STRING;
8575 li->li_tv.v_lock = 0;
8576 li->li_tv.vval.v_string = str;
8577 list_append(rettv->vval.v_list, li);
8578 }
8579 }
8580 ga_clear(&ga);
8581 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008582 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583#endif
8584}
8585
8586 static void
8587f_split(typval_T *argvars, typval_T *rettv)
8588{
8589 char_u *str;
8590 char_u *end;
8591 char_u *pat = NULL;
8592 regmatch_T regmatch;
8593 char_u patbuf[NUMBUFLEN];
8594 char_u *save_cpo;
8595 int match;
8596 colnr_T col = 0;
8597 int keepempty = FALSE;
8598 int typeerr = FALSE;
8599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008600 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008602 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008604 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605 if (argvars[1].v_type != VAR_UNKNOWN)
8606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008607 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 if (pat == NULL)
8609 typeerr = TRUE;
8610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008611 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 }
8613 if (pat == NULL || *pat == NUL)
8614 pat = (char_u *)"[\\x01- ]\\+";
8615
8616 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008617 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008619 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620
8621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8622 if (regmatch.regprog != NULL)
8623 {
8624 regmatch.rm_ic = FALSE;
8625 while (*str != NUL || keepempty)
8626 {
8627 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008628 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 else
8630 match = vim_regexec_nl(&regmatch, str, col);
8631 if (match)
8632 end = regmatch.startp[0];
8633 else
8634 end = str + STRLEN(str);
8635 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8636 && *str != NUL && match && end < regmatch.endp[0]))
8637 {
8638 if (list_append_string(rettv->vval.v_list, str,
8639 (int)(end - str)) == FAIL)
8640 break;
8641 }
8642 if (!match)
8643 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008644 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if (regmatch.endp[0] > str)
8646 col = 0;
8647 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008648 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 str = regmatch.endp[0];
8651 }
8652
8653 vim_regfree(regmatch.regprog);
8654 }
8655
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008656theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 p_cpo = save_cpo;
8658}
8659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008661 * "str2list()" function
8662 */
8663 static void
8664f_str2list(typval_T *argvars, typval_T *rettv)
8665{
8666 char_u *p;
8667 int utf8 = FALSE;
8668
8669 if (rettv_list_alloc(rettv) == FAIL)
8670 return;
8671
8672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008673 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008674
8675 p = tv_get_string(&argvars[0]);
8676
8677 if (has_mbyte || utf8)
8678 {
8679 int (*ptr2len)(char_u *);
8680 int (*ptr2char)(char_u *);
8681
8682 if (utf8 || enc_utf8)
8683 {
8684 ptr2len = utf_ptr2len;
8685 ptr2char = utf_ptr2char;
8686 }
8687 else
8688 {
8689 ptr2len = mb_ptr2len;
8690 ptr2char = mb_ptr2char;
8691 }
8692
8693 for ( ; *p != NUL; p += (*ptr2len)(p))
8694 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8695 }
8696 else
8697 for ( ; *p != NUL; ++p)
8698 list_append_number(rettv->vval.v_list, *p);
8699}
8700
8701/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 * "str2nr()" function
8703 */
8704 static void
8705f_str2nr(typval_T *argvars, typval_T *rettv)
8706{
8707 int base = 10;
8708 char_u *p;
8709 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008710 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008711 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712
8713 if (argvars[1].v_type != VAR_UNKNOWN)
8714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008715 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 if (base != 2 && base != 8 && base != 10 && base != 16)
8717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 return;
8720 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008721 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008722 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 }
8724
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008725 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008726 isneg = (*p == '-');
8727 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 p = skipwhite(p + 1);
8729 switch (base)
8730 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008731 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008732 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008733 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008735 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8736 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008737 if (isneg)
8738 rettv->vval.v_number = -n;
8739 else
8740 rettv->vval.v_number = n;
8741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742}
8743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744/*
8745 * "strgetchar()" function
8746 */
8747 static void
8748f_strgetchar(typval_T *argvars, typval_T *rettv)
8749{
8750 char_u *str;
8751 int len;
8752 int error = FALSE;
8753 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008754 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755
8756 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 if (str == NULL)
8759 return;
8760 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008761 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 if (error)
8763 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764
Bram Moolenaar13505972019-01-24 15:04:48 +01008765 while (charidx >= 0 && byteidx < len)
8766 {
8767 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008769 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8770 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008772 --charidx;
8773 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775}
8776
8777/*
8778 * "stridx()" function
8779 */
8780 static void
8781f_stridx(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u buf[NUMBUFLEN];
8784 char_u *needle;
8785 char_u *haystack;
8786 char_u *save_haystack;
8787 char_u *pos;
8788 int start_idx;
8789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008790 needle = tv_get_string_chk(&argvars[1]);
8791 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792 rettv->vval.v_number = -1;
8793 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008794 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795
8796 if (argvars[2].v_type != VAR_UNKNOWN)
8797 {
8798 int error = FALSE;
8799
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008800 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 if (error || start_idx >= (int)STRLEN(haystack))
8802 return;
8803 if (start_idx >= 0)
8804 haystack += start_idx;
8805 }
8806
8807 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8808 if (pos != NULL)
8809 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8810}
8811
8812/*
8813 * "string()" function
8814 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008815 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816f_string(typval_T *argvars, typval_T *rettv)
8817{
8818 char_u *tofree;
8819 char_u numbuf[NUMBUFLEN];
8820
8821 rettv->v_type = VAR_STRING;
8822 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8823 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008824 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 if (rettv->vval.v_string != NULL && tofree == NULL)
8826 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8827}
8828
8829/*
8830 * "strlen()" function
8831 */
8832 static void
8833f_strlen(typval_T *argvars, typval_T *rettv)
8834{
8835 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837}
8838
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008839 static void
8840strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8841{
8842 char_u *s = tv_get_string(&argvars[0]);
8843 varnumber_T len = 0;
8844 int (*func_mb_ptr2char_adv)(char_u **pp);
8845
8846 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8847 while (*s != NUL)
8848 {
8849 func_mb_ptr2char_adv(&s);
8850 ++len;
8851 }
8852 rettv->vval.v_number = len;
8853}
8854
8855/*
8856 * "strcharlen()" function
8857 */
8858 static void
8859f_strcharlen(typval_T *argvars, typval_T *rettv)
8860{
8861 strchar_common(argvars, rettv, TRUE);
8862}
8863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864/*
8865 * "strchars()" function
8866 */
8867 static void
8868f_strchars(typval_T *argvars, typval_T *rettv)
8869{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008870 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008871
8872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008873 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008875 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008877 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878}
8879
8880/*
8881 * "strdisplaywidth()" function
8882 */
8883 static void
8884f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8885{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008886 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 int col = 0;
8888
8889 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891
8892 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8893}
8894
8895/*
8896 * "strwidth()" function
8897 */
8898 static void
8899f_strwidth(typval_T *argvars, typval_T *rettv)
8900{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008901 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902
Bram Moolenaar13505972019-01-24 15:04:48 +01008903 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904}
8905
8906/*
8907 * "strcharpart()" function
8908 */
8909 static void
8910f_strcharpart(typval_T *argvars, typval_T *rettv)
8911{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912 char_u *p;
8913 int nchar;
8914 int nbyte = 0;
8915 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008916 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 int len = 0;
8918 int slen;
8919 int error = FALSE;
8920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008921 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 slen = (int)STRLEN(p);
8923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008924 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 if (!error)
8926 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008927 if (argvars[2].v_type != VAR_UNKNOWN
8928 && argvars[3].v_type != VAR_UNKNOWN)
8929 {
8930 skipcc = tv_get_bool(&argvars[3]);
8931 if (skipcc < 0 || skipcc > 1)
8932 {
8933 semsg(_(e_using_number_as_bool_nr), skipcc);
8934 return;
8935 }
8936 }
8937
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008938 if (nchar > 0)
8939 while (nchar > 0 && nbyte < slen)
8940 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008941 if (skipcc)
8942 nbyte += mb_ptr2len(p + nbyte);
8943 else
8944 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008945 --nchar;
8946 }
8947 else
8948 nbyte = nchar;
8949 if (argvars[2].v_type != VAR_UNKNOWN)
8950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008951 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952 while (charlen > 0 && nbyte + len < slen)
8953 {
8954 int off = nbyte + len;
8955
8956 if (off < 0)
8957 len += 1;
8958 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008959 {
8960 if (skipcc)
8961 len += mb_ptr2len(p + off);
8962 else
8963 len += MB_CPTR2LEN(p + off);
8964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 --charlen;
8966 }
8967 }
8968 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008969 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 }
8971
8972 /*
8973 * Only return the overlap between the specified part and the actual
8974 * string.
8975 */
8976 if (nbyte < 0)
8977 {
8978 len += nbyte;
8979 nbyte = 0;
8980 }
8981 else if (nbyte > slen)
8982 nbyte = slen;
8983 if (len < 0)
8984 len = 0;
8985 else if (nbyte + len > slen)
8986 len = slen - nbyte;
8987
8988 rettv->v_type = VAR_STRING;
8989 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008990}
8991
8992/*
8993 * "strpart()" function
8994 */
8995 static void
8996f_strpart(typval_T *argvars, typval_T *rettv)
8997{
8998 char_u *p;
8999 int n;
9000 int len;
9001 int slen;
9002 int error = FALSE;
9003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009004 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 slen = (int)STRLEN(p);
9006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009007 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 if (error)
9009 len = 0;
9010 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009011 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009013 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009015 // Only return the overlap between the specified part and the actual
9016 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 if (n < 0)
9018 {
9019 len += n;
9020 n = 0;
9021 }
9022 else if (n > slen)
9023 n = slen;
9024 if (len < 0)
9025 len = 0;
9026 else if (n + len > slen)
9027 len = slen - n;
9028
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009029 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9030 {
9031 int off;
9032
9033 // length in characters
9034 for (off = n; off < slen && len > 0; --len)
9035 off += mb_ptr2len(p + off);
9036 len = off - n;
9037 }
9038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 rettv->v_type = VAR_STRING;
9040 rettv->vval.v_string = vim_strnsave(p + n, len);
9041}
9042
9043/*
9044 * "strridx()" function
9045 */
9046 static void
9047f_strridx(typval_T *argvars, typval_T *rettv)
9048{
9049 char_u buf[NUMBUFLEN];
9050 char_u *needle;
9051 char_u *haystack;
9052 char_u *rest;
9053 char_u *lastmatch = NULL;
9054 int haystack_len, end_idx;
9055
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009056 needle = tv_get_string_chk(&argvars[1]);
9057 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058
9059 rettv->vval.v_number = -1;
9060 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009061 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062
9063 haystack_len = (int)STRLEN(haystack);
9064 if (argvars[2].v_type != VAR_UNKNOWN)
9065 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009066 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009067 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009069 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 }
9071 else
9072 end_idx = haystack_len;
9073
9074 if (*needle == NUL)
9075 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009076 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 lastmatch = haystack + end_idx;
9078 }
9079 else
9080 {
9081 for (rest = haystack; *rest != '\0'; ++rest)
9082 {
9083 rest = (char_u *)strstr((char *)rest, (char *)needle);
9084 if (rest == NULL || rest > haystack + end_idx)
9085 break;
9086 lastmatch = rest;
9087 }
9088 }
9089
9090 if (lastmatch == NULL)
9091 rettv->vval.v_number = -1;
9092 else
9093 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9094}
9095
9096/*
9097 * "strtrans()" function
9098 */
9099 static void
9100f_strtrans(typval_T *argvars, typval_T *rettv)
9101{
9102 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009103 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104}
9105
9106/*
9107 * "submatch()" function
9108 */
9109 static void
9110f_submatch(typval_T *argvars, typval_T *rettv)
9111{
9112 int error = FALSE;
9113 int no;
9114 int retList = 0;
9115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 if (error)
9118 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009119 if (no < 0 || no >= NSUBEXP)
9120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009121 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009122 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009123 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009125 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 if (error)
9127 return;
9128
9129 if (retList == 0)
9130 {
9131 rettv->v_type = VAR_STRING;
9132 rettv->vval.v_string = reg_submatch(no);
9133 }
9134 else
9135 {
9136 rettv->v_type = VAR_LIST;
9137 rettv->vval.v_list = reg_submatch_list(no);
9138 }
9139}
9140
9141/*
9142 * "substitute()" function
9143 */
9144 static void
9145f_substitute(typval_T *argvars, typval_T *rettv)
9146{
9147 char_u patbuf[NUMBUFLEN];
9148 char_u subbuf[NUMBUFLEN];
9149 char_u flagsbuf[NUMBUFLEN];
9150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009151 char_u *str = tv_get_string_chk(&argvars[0]);
9152 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009153 char_u *sub = NULL;
9154 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009155 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009157 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9158 expr = &argvars[2];
9159 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009160 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009162 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009163 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9164 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 rettv->vval.v_string = NULL;
9166 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009167 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009168}
9169
9170/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009171 * "swapinfo(swap_filename)" function
9172 */
9173 static void
9174f_swapinfo(typval_T *argvars, typval_T *rettv)
9175{
9176 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009177 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009178}
9179
9180/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009181 * "swapname(expr)" function
9182 */
9183 static void
9184f_swapname(typval_T *argvars, typval_T *rettv)
9185{
9186 buf_T *buf;
9187
9188 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009189 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009190 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9191 || buf->b_ml.ml_mfp->mf_fname == NULL)
9192 rettv->vval.v_string = NULL;
9193 else
9194 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9195}
9196
9197/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009198 * "synID(lnum, col, trans)" function
9199 */
9200 static void
9201f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9202{
9203 int id = 0;
9204#ifdef FEAT_SYN_HL
9205 linenr_T lnum;
9206 colnr_T col;
9207 int trans;
9208 int transerr = FALSE;
9209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009210 lnum = tv_get_lnum(argvars); // -1 on type error
9211 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009212 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009213
9214 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9215 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9216 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9217#endif
9218
9219 rettv->vval.v_number = id;
9220}
9221
9222/*
9223 * "synIDattr(id, what [, mode])" function
9224 */
9225 static void
9226f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9227{
9228 char_u *p = NULL;
9229#ifdef FEAT_SYN_HL
9230 int id;
9231 char_u *what;
9232 char_u *mode;
9233 char_u modebuf[NUMBUFLEN];
9234 int modec;
9235
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009236 id = (int)tv_get_number(&argvars[0]);
9237 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238 if (argvars[2].v_type != VAR_UNKNOWN)
9239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009240 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241 modec = TOLOWER_ASC(mode[0]);
9242 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009243 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 }
9245 else
9246 {
9247#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9248 if (USE_24BIT)
9249 modec = 'g';
9250 else
9251#endif
9252 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009253 modec = 'c';
9254 else
9255 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009256 }
9257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 switch (TOLOWER_ASC(what[0]))
9259 {
9260 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009261 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009262 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009263 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 p = highlight_has_attr(id, HL_BOLD, modec);
9265 break;
9266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009267 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 p = highlight_color(id, what, modec);
9269 break;
9270
9271 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009272 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009273 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009274 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 p = highlight_has_attr(id, HL_ITALIC, modec);
9276 break;
9277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009278 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009279 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280 break;
9281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009282 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 p = highlight_has_attr(id, HL_INVERSE, modec);
9284 break;
9285
9286 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009287 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009289 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009290 else if (TOLOWER_ASC(what[1]) == 't' &&
9291 TOLOWER_ASC(what[2]) == 'r')
9292 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009293 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 p = highlight_has_attr(id, HL_STANDOUT, modec);
9295 break;
9296
9297 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009298 if (TOLOWER_ASC(what[1]) == 'l') // ul
9299 p = highlight_color(id, what, modec);
9300 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009301 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9303 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009304 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9306 break;
9307 }
9308
9309 if (p != NULL)
9310 p = vim_strsave(p);
9311#endif
9312 rettv->v_type = VAR_STRING;
9313 rettv->vval.v_string = p;
9314}
9315
9316/*
9317 * "synIDtrans(id)" function
9318 */
9319 static void
9320f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9321{
9322 int id;
9323
9324#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009325 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009326
9327 if (id > 0)
9328 id = syn_get_final_id(id);
9329 else
9330#endif
9331 id = 0;
9332
9333 rettv->vval.v_number = id;
9334}
9335
9336/*
9337 * "synconcealed(lnum, col)" function
9338 */
9339 static void
9340f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9341{
9342#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9343 linenr_T lnum;
9344 colnr_T col;
9345 int syntax_flags = 0;
9346 int cchar;
9347 int matchid = 0;
9348 char_u str[NUMBUFLEN];
9349#endif
9350
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009351 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352
9353#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009354 lnum = tv_get_lnum(argvars); // -1 on type error
9355 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009356
Bram Moolenaara80faa82020-04-12 19:37:17 +02009357 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358
9359 if (rettv_list_alloc(rettv) != FAIL)
9360 {
9361 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9362 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9363 && curwin->w_p_cole > 0)
9364 {
9365 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9366 syntax_flags = get_syntax_info(&matchid);
9367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009368 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9370 {
9371 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009372 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009373 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9374 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 if (cchar != NUL)
9376 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 if (has_mbyte)
9378 (*mb_char2bytes)(cchar, str);
9379 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009380 str[0] = cchar;
9381 }
9382 }
9383 }
9384
9385 list_append_number(rettv->vval.v_list,
9386 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009387 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 list_append_string(rettv->vval.v_list, str, -1);
9389 list_append_number(rettv->vval.v_list, matchid);
9390 }
9391#endif
9392}
9393
9394/*
9395 * "synstack(lnum, col)" function
9396 */
9397 static void
9398f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9399{
9400#ifdef FEAT_SYN_HL
9401 linenr_T lnum;
9402 colnr_T col;
9403 int i;
9404 int id;
9405#endif
9406
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009407 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408
9409#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009410 lnum = tv_get_lnum(argvars); // -1 on type error
9411 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412
9413 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9414 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9415 && rettv_list_alloc(rettv) != FAIL)
9416 {
9417 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9418 for (i = 0; ; ++i)
9419 {
9420 id = syn_get_stack_item(i);
9421 if (id < 0)
9422 break;
9423 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9424 break;
9425 }
9426 }
9427#endif
9428}
9429
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430/*
9431 * "tabpagebuflist()" function
9432 */
9433 static void
9434f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9435{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 tabpage_T *tp;
9437 win_T *wp = NULL;
9438
9439 if (argvars[0].v_type == VAR_UNKNOWN)
9440 wp = firstwin;
9441 else
9442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009443 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444 if (tp != NULL)
9445 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9446 }
9447 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9448 {
9449 for (; wp != NULL; wp = wp->w_next)
9450 if (list_append_number(rettv->vval.v_list,
9451 wp->w_buffer->b_fnum) == FAIL)
9452 break;
9453 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454}
9455
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457 * "tagfiles()" function
9458 */
9459 static void
9460f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9461{
9462 char_u *fname;
9463 tagname_T tn;
9464 int first;
9465
9466 if (rettv_list_alloc(rettv) == FAIL)
9467 return;
9468 fname = alloc(MAXPATHL);
9469 if (fname == NULL)
9470 return;
9471
9472 for (first = TRUE; ; first = FALSE)
9473 if (get_tagfname(&tn, first, fname) == FAIL
9474 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9475 break;
9476 tagname_free(&tn);
9477 vim_free(fname);
9478}
9479
9480/*
9481 * "taglist()" function
9482 */
9483 static void
9484f_taglist(typval_T *argvars, typval_T *rettv)
9485{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009486 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 char_u *tag_pattern;
9488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009489 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490
9491 rettv->vval.v_number = FALSE;
9492 if (*tag_pattern == NUL)
9493 return;
9494
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009495 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009496 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009497 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009498 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499}
9500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502 * "tolower(string)" function
9503 */
9504 static void
9505f_tolower(typval_T *argvars, typval_T *rettv)
9506{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009507 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009508 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509}
9510
9511/*
9512 * "toupper(string)" function
9513 */
9514 static void
9515f_toupper(typval_T *argvars, typval_T *rettv)
9516{
9517 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519}
9520
9521/*
9522 * "tr(string, fromstr, tostr)" function
9523 */
9524 static void
9525f_tr(typval_T *argvars, typval_T *rettv)
9526{
9527 char_u *in_str;
9528 char_u *fromstr;
9529 char_u *tostr;
9530 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 int inlen;
9532 int fromlen;
9533 int tolen;
9534 int idx;
9535 char_u *cpstr;
9536 int cplen;
9537 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 char_u buf[NUMBUFLEN];
9539 char_u buf2[NUMBUFLEN];
9540 garray_T ga;
9541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009542 in_str = tv_get_string(&argvars[0]);
9543 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9544 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009546 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 rettv->v_type = VAR_STRING;
9548 rettv->vval.v_string = NULL;
9549 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009550 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 ga_init2(&ga, (int)sizeof(char), 80);
9552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009554 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 if (STRLEN(fromstr) != STRLEN(tostr))
9556 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009558 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559 ga_clear(&ga);
9560 return;
9561 }
9562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009563 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 while (*in_str != NUL)
9565 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 if (has_mbyte)
9567 {
9568 inlen = (*mb_ptr2len)(in_str);
9569 cpstr = in_str;
9570 cplen = inlen;
9571 idx = 0;
9572 for (p = fromstr; *p != NUL; p += fromlen)
9573 {
9574 fromlen = (*mb_ptr2len)(p);
9575 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9576 {
9577 for (p = tostr; *p != NUL; p += tolen)
9578 {
9579 tolen = (*mb_ptr2len)(p);
9580 if (idx-- == 0)
9581 {
9582 cplen = tolen;
9583 cpstr = p;
9584 break;
9585 }
9586 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009587 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 goto error;
9589 break;
9590 }
9591 ++idx;
9592 }
9593
9594 if (first && cpstr == in_str)
9595 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009596 // Check that fromstr and tostr have the same number of
9597 // (multi-byte) characters. Done only once when a character
9598 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 first = FALSE;
9600 for (p = tostr; *p != NUL; p += tolen)
9601 {
9602 tolen = (*mb_ptr2len)(p);
9603 --idx;
9604 }
9605 if (idx != 0)
9606 goto error;
9607 }
9608
9609 (void)ga_grow(&ga, cplen);
9610 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9611 ga.ga_len += cplen;
9612
9613 in_str += inlen;
9614 }
9615 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009617 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 p = vim_strchr(fromstr, *in_str);
9619 if (p != NULL)
9620 ga_append(&ga, tostr[p - fromstr]);
9621 else
9622 ga_append(&ga, *in_str);
9623 ++in_str;
9624 }
9625 }
9626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009627 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 (void)ga_grow(&ga, 1);
9629 ga_append(&ga, NUL);
9630
9631 rettv->vval.v_string = ga.ga_data;
9632}
9633
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009634/*
9635 * "trim({expr})" function
9636 */
9637 static void
9638f_trim(typval_T *argvars, typval_T *rettv)
9639{
9640 char_u buf1[NUMBUFLEN];
9641 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009642 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009643 char_u *mask = NULL;
9644 char_u *tail;
9645 char_u *prev;
9646 char_u *p;
9647 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009648 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009649
9650 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009651 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009652 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009653 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009654
9655 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009656 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009657 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009658
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009660 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009661 int error = 0;
9662
9663 // leading or trailing characters to trim
9664 dir = (int)tv_get_number_chk(&argvars[2], &error);
9665 if (error)
9666 return;
9667 if (dir < 0 || dir > 2)
9668 {
9669 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9670 return;
9671 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009672 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009673 }
9674
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009675 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009676 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009677 // Trim leading characters
9678 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009679 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009680 c1 = PTR2CHAR(head);
9681 if (mask == NULL)
9682 {
9683 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009684 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009685 }
9686 else
9687 {
9688 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9689 if (c1 == PTR2CHAR(p))
9690 break;
9691 if (*p == NUL)
9692 break;
9693 }
9694 MB_PTR_ADV(head);
9695 }
9696 }
9697
9698 tail = head + STRLEN(head);
9699 if (dir == 0 || dir == 2)
9700 {
9701 // Trim trailing characters
9702 for (; tail > head; tail = prev)
9703 {
9704 prev = tail;
9705 MB_PTR_BACK(head, prev);
9706 c1 = PTR2CHAR(prev);
9707 if (mask == NULL)
9708 {
9709 if (c1 > ' ' && c1 != 0xa0)
9710 break;
9711 }
9712 else
9713 {
9714 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9715 if (c1 == PTR2CHAR(p))
9716 break;
9717 if (*p == NUL)
9718 break;
9719 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009720 }
9721 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009722 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009723}
9724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009725/*
9726 * "type(expr)" function
9727 */
9728 static void
9729f_type(typval_T *argvars, typval_T *rettv)
9730{
9731 int n = -1;
9732
9733 switch (argvars[0].v_type)
9734 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009735 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9736 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009737 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009738 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9739 case VAR_LIST: n = VAR_TYPE_LIST; break;
9740 case VAR_DICT: n = VAR_TYPE_DICT; break;
9741 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9742 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9743 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009744 case VAR_JOB: n = VAR_TYPE_JOB; break;
9745 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009746 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009747 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009749 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009750 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009751 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009752 n = -1;
9753 break;
9754 }
9755 rettv->vval.v_number = n;
9756}
9757
9758/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759 * "virtcol(string)" function
9760 */
9761 static void
9762f_virtcol(typval_T *argvars, typval_T *rettv)
9763{
9764 colnr_T vcol = 0;
9765 pos_T *fp;
9766 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009767 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009769 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9771 && fnum == curbuf->b_fnum)
9772 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009773 // Limit the column to a valid value, getvvcol() doesn't check.
9774 if (fp->col < 0)
9775 fp->col = 0;
9776 else
9777 {
9778 len = (int)STRLEN(ml_get(fp->lnum));
9779 if (fp->col > len)
9780 fp->col = len;
9781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782 getvvcol(curwin, fp, NULL, NULL, &vcol);
9783 ++vcol;
9784 }
9785
9786 rettv->vval.v_number = vcol;
9787}
9788
9789/*
9790 * "visualmode()" function
9791 */
9792 static void
9793f_visualmode(typval_T *argvars, typval_T *rettv)
9794{
9795 char_u str[2];
9796
9797 rettv->v_type = VAR_STRING;
9798 str[0] = curbuf->b_visual_mode_eval;
9799 str[1] = NUL;
9800 rettv->vval.v_string = vim_strsave(str);
9801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009802 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009803 if (non_zero_arg(&argvars[0]))
9804 curbuf->b_visual_mode_eval = NUL;
9805}
9806
9807/*
9808 * "wildmenumode()" function
9809 */
9810 static void
9811f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9812{
9813#ifdef FEAT_WILDMENU
9814 if (wild_menu_showing)
9815 rettv->vval.v_number = 1;
9816#endif
9817}
9818
9819/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009820 * "windowsversion()" function
9821 */
9822 static void
9823f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9824{
9825 rettv->v_type = VAR_STRING;
9826 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9827}
9828
9829/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 * "wordcount()" function
9831 */
9832 static void
9833f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9834{
9835 if (rettv_dict_alloc(rettv) == FAIL)
9836 return;
9837 cursor_pos_info(rettv->vval.v_dict);
9838}
9839
9840/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009841 * "xor(expr, expr)" function
9842 */
9843 static void
9844f_xor(typval_T *argvars, typval_T *rettv)
9845{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9847 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009848}
9849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009850#endif // FEAT_EVAL