blob: 97f7b61070388c1efc1e437dd52fe35afd27e479 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010024#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020025static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010026static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010027# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010028static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010029# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031static void f_byte2line(typval_T *argvars, typval_T *rettv);
32static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
33static void f_byteidx(typval_T *argvars, typval_T *rettv);
34static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
35static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020036static void f_changenr(typval_T *argvars, typval_T *rettv);
37static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010038static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010039static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020041static void f_confirm(typval_T *argvars, typval_T *rettv);
42static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010044#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020045static void f_debugbreak(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010049static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020051static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_escape(typval_T *argvars, typval_T *rettv);
53static void f_eval(typval_T *argvars, typval_T *rettv);
54static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020058static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020062static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_function(typval_T *argvars, typval_T *rettv);
64static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
65static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010066static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010067static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020069static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010071static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_getpid(typval_T *argvars, typval_T *rettv);
73static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010074static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020077static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010079static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +020080static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020081static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
82static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_hlID(typval_T *argvars, typval_T *rettv);
84static void f_hlexists(typval_T *argvars, typval_T *rettv);
85static void f_hostname(typval_T *argvars, typval_T *rettv);
86static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_index(typval_T *argvars, typval_T *rettv);
88static void f_input(typval_T *argvars, typval_T *rettv);
89static void f_inputdialog(typval_T *argvars, typval_T *rettv);
90static void f_inputlist(typval_T *argvars, typval_T *rettv);
91static void f_inputrestore(typval_T *argvars, typval_T *rettv);
92static void f_inputsave(typval_T *argvars, typval_T *rettv);
93static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +010094static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020095static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020097static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
98static void f_len(typval_T *argvars, typval_T *rettv);
99static void f_libcall(typval_T *argvars, typval_T *rettv);
100static void f_libcallnr(typval_T *argvars, typval_T *rettv);
101static void f_line(typval_T *argvars, typval_T *rettv);
102static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103#ifdef FEAT_LUA
104static void f_luaeval(typval_T *argvars, typval_T *rettv);
105#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_maparg(typval_T *argvars, typval_T *rettv);
107static void f_mapcheck(typval_T *argvars, typval_T *rettv);
108static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200109static void f_matchend(typval_T *argvars, typval_T *rettv);
110static void f_matchlist(typval_T *argvars, typval_T *rettv);
111static void f_matchstr(typval_T *argvars, typval_T *rettv);
112static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
113static void f_max(typval_T *argvars, typval_T *rettv);
114static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115#ifdef FEAT_MZSCHEME
116static void f_mzeval(typval_T *argvars, typval_T *rettv);
117#endif
118static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
119static void f_nr2char(typval_T *argvars, typval_T *rettv);
120static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121#ifdef FEAT_PERL
122static void f_perleval(typval_T *argvars, typval_T *rettv);
123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200124static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
125static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200126static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_pumvisible(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_PYTHON3
129static void f_py3eval(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_PYTHON
132static void f_pyeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100134#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
135static void f_pyxeval(typval_T *argvars, typval_T *rettv);
136#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100137static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100138static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200139static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200140static void f_reg_executing(typval_T *argvars, typval_T *rettv);
141static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142static void f_rename(typval_T *argvars, typval_T *rettv);
143static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100144#ifdef FEAT_RUBY
145static void f_rubyeval(typval_T *argvars, typval_T *rettv);
146#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147static void f_screenattr(typval_T *argvars, typval_T *rettv);
148static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100149static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200150static void f_screencol(typval_T *argvars, typval_T *rettv);
151static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100152static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_search(typval_T *argvars, typval_T *rettv);
154static void f_searchdecl(typval_T *argvars, typval_T *rettv);
155static void f_searchpair(typval_T *argvars, typval_T *rettv);
156static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
157static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100158static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100160static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200161static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100165static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166#ifdef FEAT_CRYPT
167static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_shellescape(typval_T *argvars, typval_T *rettv);
170static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_soundfold(typval_T *argvars, typval_T *rettv);
172static void f_spellbadword(typval_T *argvars, typval_T *rettv);
173static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
174static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100175static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200176static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_str2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100178static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_strgetchar(typval_T *argvars, typval_T *rettv);
181static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_strlen(typval_T *argvars, typval_T *rettv);
183static void f_strcharpart(typval_T *argvars, typval_T *rettv);
184static void f_strpart(typval_T *argvars, typval_T *rettv);
185static void f_strridx(typval_T *argvars, typval_T *rettv);
186static void f_strtrans(typval_T *argvars, typval_T *rettv);
187static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
188static void f_strwidth(typval_T *argvars, typval_T *rettv);
189static void f_submatch(typval_T *argvars, typval_T *rettv);
190static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200191static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200192static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_synID(typval_T *argvars, typval_T *rettv);
194static void f_synIDattr(typval_T *argvars, typval_T *rettv);
195static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
196static void f_synstack(typval_T *argvars, typval_T *rettv);
197static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_taglist(typval_T *argvars, typval_T *rettv);
200static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_tolower(typval_T *argvars, typval_T *rettv);
202static void f_toupper(typval_T *argvars, typval_T *rettv);
203static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100204static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_virtcol(typval_T *argvars, typval_T *rettv);
207static void f_visualmode(typval_T *argvars, typval_T *rettv);
208static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100209static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200210static void f_wordcount(typval_T *argvars, typval_T *rettv);
211static void f_xor(typval_T *argvars, typval_T *rettv);
212
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100213
Bram Moolenaar94738d82020-10-21 14:25:07 +0200214/*
215 * Functions that check the argument type of a builtin function.
216 * Each function returns FAIL and gives an error message if the type is wrong.
217 */
218
219// Context passed to an arg_ function.
220typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200221 int arg_count; // actual argument count
222 type_T **arg_types; // list of argument types
223 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100224 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200225} argcontext_T;
226
227// A function to check one argument type. The first argument is the type to
228// check. If needed, other argument types can be obtained with the context.
229// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
230typedef int (*argcheck_T)(type_T *, argcontext_T *);
231
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100232/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100233 * Call need_type() to check an argument type.
234 */
235 static int
236check_arg_type(
237 type_T *expected,
238 type_T *actual,
239 argcontext_T *context)
240{
241 // TODO: would be useful to know if "actual" is a constant and pass it to
242 // need_type() to get a compile time error if possible.
243 return need_type(actual, expected,
244 context->arg_idx - context->arg_count, context->arg_idx + 1,
245 context->arg_cctx, FALSE, FALSE);
246}
247
248/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100249 * Check "type" is a float or a number.
250 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200251 static int
252arg_float_or_nr(type_T *type, argcontext_T *context)
253{
Bram Moolenaarca174532020-10-21 16:42:22 +0200254 if (type->tt_type == VAR_ANY
255 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200256 return OK;
257 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
258 return FAIL;
259}
260
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100261/*
262 * Check "type" is a number.
263 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200264 static int
265arg_number(type_T *type, argcontext_T *context)
266{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100267 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200268}
269
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100270/*
271 * Check "type" is a string.
272 */
273 static int
274arg_string(type_T *type, argcontext_T *context)
275{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100276 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100277}
278
279/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100280 * Check "type" is a bool or number 0 or 1.
281 */
282 static int
283arg_bool(type_T *type, argcontext_T *context)
284{
285 if (type->tt_type == VAR_ANY
286 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
287 return OK;
288 return check_arg_type(&t_bool, type, context);
289}
290
291/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100292 * Check "type" is a list or a blob.
293 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200294 static int
295arg_list_or_blob(type_T *type, argcontext_T *context)
296{
297 if (type->tt_type == VAR_ANY
298 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
299 return OK;
300 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
301 return FAIL;
302}
303
304/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100305 * Check "type" is a list or a dict.
306 */
307 static int
308arg_list_or_dict(type_T *type, argcontext_T *context)
309{
310 if (type->tt_type == VAR_ANY
311 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
312 return OK;
313 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
314 return FAIL;
315}
316
317/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100318 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100319 * Must not be used for the first argcheck_T entry.
320 */
321 static int
322arg_same_as_prev(type_T *type, argcontext_T *context)
323{
324 type_T *prev_type = context->arg_types[context->arg_idx - 1];
325
Bram Moolenaar351ead02021-01-16 16:07:01 +0100326 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100327}
328
329/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100330 * Check "type" is the same basic type as the previous argument, checks list or
331 * dict vs other type, but not member type.
332 * Must not be used for the first argcheck_T entry.
333 */
334 static int
335arg_same_struct_as_prev(type_T *type, argcontext_T *context)
336{
337 type_T *prev_type = context->arg_types[context->arg_idx - 1];
338
339 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100340 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100341 return OK;
342}
343
344/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100345 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200346 * Must not be used for the first argcheck_T entry.
347 */
348 static int
349arg_item_of_prev(type_T *type, argcontext_T *context)
350{
351 type_T *prev_type = context->arg_types[context->arg_idx - 1];
352 type_T *expected;
353
354 if (prev_type->tt_type == VAR_LIST)
355 expected = prev_type->tt_member;
356 else if (prev_type->tt_type == VAR_BLOB)
357 expected = &t_number;
358 else
359 // probably VAR_ANY, can't check
360 return OK;
361
Bram Moolenaar351ead02021-01-16 16:07:01 +0100362 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200363}
364
Bram Moolenaar94738d82020-10-21 14:25:07 +0200365/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100366 * Check "type" which is the third argument of extend().
367 */
368 static int
369arg_extend3(type_T *type, argcontext_T *context)
370{
371 type_T *first_type = context->arg_types[context->arg_idx - 2];
372
373 if (first_type->tt_type == VAR_LIST)
374 return arg_number(type, context);
375 if (first_type->tt_type == VAR_DICT)
376 return arg_string(type, context);
377 return OK;
378}
379
380
381/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200382 * Lists of functions that check the argument types of a builtin function.
383 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100384argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100385argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200386argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
388argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100389argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200390argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200391
392/*
393 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200394 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200395 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100396 static type_T *
397ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
398{
399 return &t_void;
400}
401 static type_T *
402ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
403{
404 return &t_any;
405}
406 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200407ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
408{
409 return &t_bool;
410}
411 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100412ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
413{
414 return &t_number_bool;
415}
416 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100417ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
418{
419 return &t_number;
420}
421 static type_T *
422ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
423{
424 return &t_float;
425}
426 static type_T *
427ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
428{
429 return &t_string;
430}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200431 static type_T *
432ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100433{
434 return &t_list_any;
435}
436 static type_T *
437ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_list_number;
440}
441 static type_T *
442ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_list_string;
445}
446 static type_T *
447ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_list_dict_any;
450}
451 static type_T *
452ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_dict_any;
455}
456 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100457ret_job_info(int argcount, type_T **argtypes UNUSED)
458{
459 if (argcount == 0)
460 return &t_list_job;
461 return &t_dict_any;
462}
463 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100464ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
465{
466 return &t_dict_number;
467}
468 static type_T *
469ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
470{
471 return &t_dict_string;
472}
473 static type_T *
474ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
475{
476 return &t_blob;
477}
478 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200479ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100480{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200481 return &t_func_any;
482}
483 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100484ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
485{
486 return &t_channel;
487}
488 static type_T *
489ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
490{
491 return &t_job;
492}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200493 static type_T *
494ret_first_arg(int argcount, type_T **argtypes)
495{
496 if (argcount > 0)
497 return argtypes[0];
498 return &t_void;
499}
Bram Moolenaarea696852020-11-09 18:31:39 +0100500// for map(): returns first argument but item type may differ
501 static type_T *
502ret_first_cont(int argcount UNUSED, type_T **argtypes)
503{
504 if (argtypes[0]->tt_type == VAR_LIST)
505 return &t_list_any;
506 if (argtypes[0]->tt_type == VAR_DICT)
507 return &t_dict_any;
508 if (argtypes[0]->tt_type == VAR_BLOB)
509 return argtypes[0];
510 return &t_any;
511}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200512
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200513/*
514 * Used for getqflist(): returns list if there is no argument, dict if there is
515 * one.
516 */
517 static type_T *
518ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
519{
520 if (argcount > 0)
521 return &t_dict_any;
522 return &t_list_dict_any;
523}
524
525/*
526 * Used for getloclist(): returns list if there is one argument, dict if there
527 * are two.
528 */
529 static type_T *
530ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
531{
532 if (argcount > 1)
533 return &t_dict_any;
534 return &t_list_dict_any;
535}
536
Bram Moolenaar846178a2020-07-05 17:04:13 +0200537 static type_T *
538ret_argv(int argcount, type_T **argtypes UNUSED)
539{
540 // argv() returns list of strings
541 if (argcount == 0)
542 return &t_list_string;
543
544 // argv(0) returns a string, but argv(-1] returns a list
545 return &t_any;
546}
547
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200548 static type_T *
549ret_remove(int argcount UNUSED, type_T **argtypes)
550{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200551 if (argtypes != NULL)
552 {
553 if (argtypes[0]->tt_type == VAR_LIST
554 || argtypes[0]->tt_type == VAR_DICT)
555 return argtypes[0]->tt_member;
556 if (argtypes[0]->tt_type == VAR_BLOB)
557 return &t_number;
558 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200559 return &t_any;
560}
561
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200562 static type_T *
563ret_getreg(int argcount, type_T **argtypes UNUSED)
564{
565 // Assume that if the third argument is passed it's non-zero
566 if (argcount == 3)
567 return &t_list_string;
568 return &t_string;
569}
570
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200571 static type_T *
572ret_maparg(int argcount, type_T **argtypes UNUSED)
573{
574 // Assume that if the fourth argument is passed it's non-zero
575 if (argcount == 4)
576 return &t_dict_any;
577 return &t_string;
578}
579
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580static type_T *ret_f_function(int argcount, type_T **argtypes);
581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582/*
583 * Array with names and number of arguments of all internal functions
584 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
585 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200586typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200588 char *f_name; // function name
589 char f_min_argc; // minimal number of arguments
590 char f_max_argc; // maximal number of arguments
591 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200592 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100593 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
594 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200596 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200597} funcentry_T;
598
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200599// values for f_argtype; zero means it cannot be used as a method
600#define FEARG_1 1 // base is the first argument
601#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200602#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200603#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200604#define FEARG_LAST 9 // base is the last argument
605
Bram Moolenaar15c47602020-03-26 22:16:48 +0100606#ifdef FEAT_FLOAT
607# define FLOAT_FUNC(name) name
608#else
609# define FLOAT_FUNC(name) NULL
610#endif
611#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
612# define MATH_FUNC(name) name
613#else
614# define MATH_FUNC(name) NULL
615#endif
616#ifdef FEAT_TIMERS
617# define TIMER_FUNC(name) name
618#else
619# define TIMER_FUNC(name) NULL
620#endif
621#ifdef FEAT_JOB_CHANNEL
622# define JOB_FUNC(name) name
623#else
624# define JOB_FUNC(name) NULL
625#endif
626#ifdef FEAT_PROP_POPUP
627# define PROP_FUNC(name) name
628#else
629# define PROP_FUNC(name) NULL
630#endif
631#ifdef FEAT_SIGNS
632# define SIGN_FUNC(name) name
633#else
634# define SIGN_FUNC(name) NULL
635#endif
636#ifdef FEAT_SOUND
637# define SOUND_FUNC(name) name
638#else
639# define SOUND_FUNC(name) NULL
640#endif
641#ifdef FEAT_TERMINAL
642# define TERM_FUNC(name) name
643#else
644# define TERM_FUNC(name) NULL
645#endif
646
Bram Moolenaarac92e252019-08-03 21:58:38 +0200647static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200649 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
650 ret_any, FLOAT_FUNC(f_abs)},
651 {"acos", 1, 1, FEARG_1, NULL,
652 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100653 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200654 ret_first_arg, f_add},
655 {"and", 2, 2, FEARG_1, NULL,
656 ret_number, f_and},
657 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100658 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200659 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100660 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200661 {"argc", 0, 1, 0, NULL,
662 ret_number, f_argc},
663 {"argidx", 0, 0, 0, NULL,
664 ret_number, f_argidx},
665 {"arglistid", 0, 2, 0, NULL,
666 ret_number, f_arglistid},
667 {"argv", 0, 2, 0, NULL,
668 ret_argv, f_argv},
669 {"asin", 1, 1, FEARG_1, NULL,
670 ret_float, FLOAT_FUNC(f_asin)},
671 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100672 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200673 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100674 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200675 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100676 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200677 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100678 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200679 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100680 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200681 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100682 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200683 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100684 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200685 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100686 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200687 {"assert_nobeep", 1, 2, FEARG_1, NULL,
688 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100690 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200691 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100692 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200693 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100694 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200695 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100696 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200697 {"atan", 1, 1, FEARG_1, NULL,
698 ret_float, FLOAT_FUNC(f_atan)},
699 {"atan2", 2, 2, FEARG_1, NULL,
700 ret_float, FLOAT_FUNC(f_atan2)},
701 {"balloon_gettext", 0, 0, 0, NULL,
702 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100703#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100704 f_balloon_gettext
705#else
706 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100707#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 {"balloon_show", 1, 1, FEARG_1, NULL,
710 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100711#ifdef FEAT_BEVAL
712 f_balloon_show
713#else
714 NULL
715#endif
716 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"balloon_split", 1, 1, FEARG_1, NULL,
718 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100719#if defined(FEAT_BEVAL_TERM)
720 f_balloon_split
721#else
722 NULL
723#endif
724 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200725 {"browse", 4, 4, 0, NULL,
726 ret_string, f_browse},
727 {"browsedir", 2, 2, 0, NULL,
728 ret_string, f_browsedir},
729 {"bufadd", 1, 1, FEARG_1, NULL,
730 ret_number, f_bufadd},
731 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200733 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200735 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
736 ret_string, f_bufname},
737 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
738 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"bufload", 1, 1, FEARG_1, NULL,
742 ret_void, f_bufload},
743 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"bufname", 0, 1, FEARG_1, NULL,
746 ret_string, f_bufname},
747 {"bufnr", 0, 2, FEARG_1, NULL,
748 ret_number, f_bufnr},
749 {"bufwinid", 1, 1, FEARG_1, NULL,
750 ret_number, f_bufwinid},
751 {"bufwinnr", 1, 1, FEARG_1, NULL,
752 ret_number, f_bufwinnr},
753 {"byte2line", 1, 1, FEARG_1, NULL,
754 ret_number, f_byte2line},
755 {"byteidx", 2, 2, FEARG_1, NULL,
756 ret_number, f_byteidx},
757 {"byteidxcomp", 2, 2, FEARG_1, NULL,
758 ret_number, f_byteidxcomp},
759 {"call", 2, 3, FEARG_1, NULL,
760 ret_any, f_call},
761 {"ceil", 1, 1, FEARG_1, NULL,
762 ret_float, FLOAT_FUNC(f_ceil)},
763 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100764 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200765 {"ch_close", 1, 1, FEARG_1, NULL,
766 ret_void, JOB_FUNC(f_ch_close)},
767 {"ch_close_in", 1, 1, FEARG_1, NULL,
768 ret_void, JOB_FUNC(f_ch_close_in)},
769 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
770 ret_any, JOB_FUNC(f_ch_evalexpr)},
771 {"ch_evalraw", 2, 3, FEARG_1, NULL,
772 ret_any, JOB_FUNC(f_ch_evalraw)},
773 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
774 ret_number, JOB_FUNC(f_ch_getbufnr)},
775 {"ch_getjob", 1, 1, FEARG_1, NULL,
776 ret_job, JOB_FUNC(f_ch_getjob)},
777 {"ch_info", 1, 1, FEARG_1, NULL,
778 ret_dict_any, JOB_FUNC(f_ch_info)},
779 {"ch_log", 1, 2, FEARG_1, NULL,
780 ret_void, JOB_FUNC(f_ch_log)},
781 {"ch_logfile", 1, 2, FEARG_1, NULL,
782 ret_void, JOB_FUNC(f_ch_logfile)},
783 {"ch_open", 1, 2, FEARG_1, NULL,
784 ret_channel, JOB_FUNC(f_ch_open)},
785 {"ch_read", 1, 2, FEARG_1, NULL,
786 ret_string, JOB_FUNC(f_ch_read)},
787 {"ch_readblob", 1, 2, FEARG_1, NULL,
788 ret_blob, JOB_FUNC(f_ch_readblob)},
789 {"ch_readraw", 1, 2, FEARG_1, NULL,
790 ret_string, JOB_FUNC(f_ch_readraw)},
791 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
792 ret_void, JOB_FUNC(f_ch_sendexpr)},
793 {"ch_sendraw", 2, 3, FEARG_1, NULL,
794 ret_void, JOB_FUNC(f_ch_sendraw)},
795 {"ch_setoptions", 2, 2, FEARG_1, NULL,
796 ret_void, JOB_FUNC(f_ch_setoptions)},
797 {"ch_status", 1, 2, FEARG_1, NULL,
798 ret_string, JOB_FUNC(f_ch_status)},
799 {"changenr", 0, 0, 0, NULL,
800 ret_number, f_changenr},
801 {"char2nr", 1, 2, FEARG_1, NULL,
802 ret_number, f_char2nr},
803 {"charclass", 1, 1, FEARG_1, NULL,
804 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100805 {"charcol", 1, 1, FEARG_1, NULL,
806 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100807 {"charidx", 2, 3, FEARG_1, NULL,
808 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200809 {"chdir", 1, 1, FEARG_1, NULL,
810 ret_string, f_chdir},
811 {"cindent", 1, 1, FEARG_1, NULL,
812 ret_number, f_cindent},
813 {"clearmatches", 0, 1, FEARG_1, NULL,
814 ret_void, f_clearmatches},
815 {"col", 1, 1, FEARG_1, NULL,
816 ret_number, f_col},
817 {"complete", 2, 2, FEARG_2, NULL,
818 ret_void, f_complete},
819 {"complete_add", 1, 1, FEARG_1, NULL,
820 ret_number, f_complete_add},
821 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100822 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200823 {"complete_info", 0, 1, FEARG_1, NULL,
824 ret_dict_any, f_complete_info},
825 {"confirm", 1, 4, FEARG_1, NULL,
826 ret_number, f_confirm},
827 {"copy", 1, 1, FEARG_1, NULL,
828 ret_first_arg, f_copy},
829 {"cos", 1, 1, FEARG_1, NULL,
830 ret_float, FLOAT_FUNC(f_cos)},
831 {"cosh", 1, 1, FEARG_1, NULL,
832 ret_float, FLOAT_FUNC(f_cosh)},
833 {"count", 2, 4, FEARG_1, NULL,
834 ret_number, f_count},
835 {"cscope_connection",0,3, 0, NULL,
836 ret_number, f_cscope_connection},
837 {"cursor", 1, 3, FEARG_1, NULL,
838 ret_number, f_cursor},
839 {"debugbreak", 1, 1, FEARG_1, NULL,
840 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 f_debugbreak
843#else
844 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200845#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200847 {"deepcopy", 1, 2, FEARG_1, NULL,
848 ret_first_arg, f_deepcopy},
849 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100850 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200851 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100852 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200853 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100854 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200855 {"diff_filler", 1, 1, FEARG_1, NULL,
856 ret_number, f_diff_filler},
857 {"diff_hlID", 2, 2, FEARG_1, NULL,
858 ret_number, f_diff_hlID},
859 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100860 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100862 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200863 {"environ", 0, 0, 0, NULL,
864 ret_dict_string, f_environ},
865 {"escape", 2, 2, FEARG_1, NULL,
866 ret_string, f_escape},
867 {"eval", 1, 1, FEARG_1, NULL,
868 ret_any, f_eval},
869 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100870 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200871 {"executable", 1, 1, FEARG_1, NULL,
872 ret_number, f_executable},
873 {"execute", 1, 2, FEARG_1, NULL,
874 ret_string, f_execute},
875 {"exepath", 1, 1, FEARG_1, NULL,
876 ret_string, f_exepath},
877 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100878 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200879 {"exp", 1, 1, FEARG_1, NULL,
880 ret_float, FLOAT_FUNC(f_exp)},
881 {"expand", 1, 3, FEARG_1, NULL,
882 ret_any, f_expand},
883 {"expandcmd", 1, 1, FEARG_1, NULL,
884 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100885 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200886 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100887 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
888 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200889 {"feedkeys", 1, 2, FEARG_1, NULL,
890 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200891 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100892 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200893 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100894 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200895 {"filewritable", 1, 1, FEARG_1, NULL,
896 ret_number, f_filewritable},
897 {"filter", 2, 2, FEARG_1, NULL,
898 ret_first_arg, f_filter},
899 {"finddir", 1, 3, FEARG_1, NULL,
900 ret_string, f_finddir},
901 {"findfile", 1, 3, FEARG_1, NULL,
902 ret_string, f_findfile},
903 {"flatten", 1, 2, FEARG_1, NULL,
904 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100905 {"flattennew", 1, 2, FEARG_1, NULL,
906 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"float2nr", 1, 1, FEARG_1, NULL,
908 ret_number, FLOAT_FUNC(f_float2nr)},
909 {"floor", 1, 1, FEARG_1, NULL,
910 ret_float, FLOAT_FUNC(f_floor)},
911 {"fmod", 2, 2, FEARG_1, NULL,
912 ret_float, FLOAT_FUNC(f_fmod)},
913 {"fnameescape", 1, 1, FEARG_1, NULL,
914 ret_string, f_fnameescape},
915 {"fnamemodify", 2, 2, FEARG_1, NULL,
916 ret_string, f_fnamemodify},
917 {"foldclosed", 1, 1, FEARG_1, NULL,
918 ret_number, f_foldclosed},
919 {"foldclosedend", 1, 1, FEARG_1, NULL,
920 ret_number, f_foldclosedend},
921 {"foldlevel", 1, 1, FEARG_1, NULL,
922 ret_number, f_foldlevel},
923 {"foldtext", 0, 0, 0, NULL,
924 ret_string, f_foldtext},
925 {"foldtextresult", 1, 1, FEARG_1, NULL,
926 ret_string, f_foldtextresult},
927 {"foreground", 0, 0, 0, NULL,
928 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100929 {"fullcommand", 1, 1, FEARG_1, arg1_string,
930 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"funcref", 1, 3, FEARG_1, NULL,
932 ret_func_any, f_funcref},
933 {"function", 1, 3, FEARG_1, NULL,
934 ret_f_function, f_function},
935 {"garbagecollect", 0, 1, 0, NULL,
936 ret_void, f_garbagecollect},
937 {"get", 2, 3, FEARG_1, NULL,
938 ret_any, f_get},
939 {"getbufinfo", 0, 1, FEARG_1, NULL,
940 ret_list_dict_any, f_getbufinfo},
941 {"getbufline", 2, 3, FEARG_1, NULL,
942 ret_list_string, f_getbufline},
943 {"getbufvar", 2, 3, FEARG_1, NULL,
944 ret_any, f_getbufvar},
945 {"getchangelist", 0, 1, FEARG_1, NULL,
946 ret_list_any, f_getchangelist},
947 {"getchar", 0, 1, 0, NULL,
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200948 ret_any, f_getchar},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200949 {"getcharmod", 0, 0, 0, NULL,
950 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100951 {"getcharpos", 1, 1, FEARG_1, NULL,
952 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200953 {"getcharsearch", 0, 0, 0, NULL,
954 ret_dict_any, f_getcharsearch},
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200955 {"getcharstr", 0, 1, 0, NULL,
956 ret_string, f_getcharstr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200957 {"getcmdline", 0, 0, 0, NULL,
958 ret_string, f_getcmdline},
959 {"getcmdpos", 0, 0, 0, NULL,
960 ret_number, f_getcmdpos},
961 {"getcmdtype", 0, 0, 0, NULL,
962 ret_string, f_getcmdtype},
963 {"getcmdwintype", 0, 0, 0, NULL,
964 ret_string, f_getcmdwintype},
965 {"getcompletion", 2, 3, FEARG_1, NULL,
966 ret_list_string, f_getcompletion},
967 {"getcurpos", 0, 1, FEARG_1, NULL,
968 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100969 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
970 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200971 {"getcwd", 0, 2, FEARG_1, NULL,
972 ret_string, f_getcwd},
973 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100974 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200975 {"getfontname", 0, 1, 0, NULL,
976 ret_string, f_getfontname},
977 {"getfperm", 1, 1, FEARG_1, NULL,
978 ret_string, f_getfperm},
979 {"getfsize", 1, 1, FEARG_1, NULL,
980 ret_number, f_getfsize},
981 {"getftime", 1, 1, FEARG_1, NULL,
982 ret_number, f_getftime},
983 {"getftype", 1, 1, FEARG_1, NULL,
984 ret_string, f_getftype},
985 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100986 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200987 {"getjumplist", 0, 2, FEARG_1, NULL,
988 ret_list_any, f_getjumplist},
989 {"getline", 1, 2, FEARG_1, NULL,
990 ret_f_getline, f_getline},
991 {"getloclist", 1, 2, 0, NULL,
992 ret_list_or_dict_1, f_getloclist},
993 {"getmarklist", 0, 1, FEARG_1, NULL,
994 ret_list_dict_any, f_getmarklist},
995 {"getmatches", 0, 1, 0, NULL,
996 ret_list_dict_any, f_getmatches},
997 {"getmousepos", 0, 0, 0, NULL,
998 ret_dict_number, f_getmousepos},
999 {"getpid", 0, 0, 0, NULL,
1000 ret_number, f_getpid},
1001 {"getpos", 1, 1, FEARG_1, NULL,
1002 ret_list_number, f_getpos},
1003 {"getqflist", 0, 1, 0, NULL,
1004 ret_list_or_dict_0, f_getqflist},
1005 {"getreg", 0, 3, FEARG_1, NULL,
1006 ret_getreg, f_getreg},
1007 {"getreginfo", 0, 1, FEARG_1, NULL,
1008 ret_dict_any, f_getreginfo},
1009 {"getregtype", 0, 1, FEARG_1, NULL,
1010 ret_string, f_getregtype},
1011 {"gettabinfo", 0, 1, FEARG_1, NULL,
1012 ret_list_dict_any, f_gettabinfo},
1013 {"gettabvar", 2, 3, FEARG_1, NULL,
1014 ret_any, f_gettabvar},
1015 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1016 ret_any, f_gettabwinvar},
1017 {"gettagstack", 0, 1, FEARG_1, NULL,
1018 ret_dict_any, f_gettagstack},
1019 {"gettext", 1, 1, FEARG_1, NULL,
1020 ret_string, f_gettext},
1021 {"getwininfo", 0, 1, FEARG_1, NULL,
1022 ret_list_dict_any, f_getwininfo},
1023 {"getwinpos", 0, 1, FEARG_1, NULL,
1024 ret_list_number, f_getwinpos},
1025 {"getwinposx", 0, 0, 0, NULL,
1026 ret_number, f_getwinposx},
1027 {"getwinposy", 0, 0, 0, NULL,
1028 ret_number, f_getwinposy},
1029 {"getwinvar", 2, 3, FEARG_1, NULL,
1030 ret_any, f_getwinvar},
1031 {"glob", 1, 4, FEARG_1, NULL,
1032 ret_any, f_glob},
1033 {"glob2regpat", 1, 1, FEARG_1, NULL,
1034 ret_string, f_glob2regpat},
1035 {"globpath", 2, 5, FEARG_2, NULL,
1036 ret_any, f_globpath},
1037 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001038 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001039 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001040 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001041 {"haslocaldir", 0, 2, FEARG_1, NULL,
1042 ret_number, f_haslocaldir},
1043 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001044 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001045 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1046 ret_number, f_hlID},
1047 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001048 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001049 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001050 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001051 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001052 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001053 {"histget", 1, 2, FEARG_1, NULL,
1054 ret_string, f_histget},
1055 {"histnr", 1, 1, FEARG_1, NULL,
1056 ret_number, f_histnr},
1057 {"hlID", 1, 1, FEARG_1, NULL,
1058 ret_number, f_hlID},
1059 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001060 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001061 {"hostname", 0, 0, 0, NULL,
1062 ret_string, f_hostname},
1063 {"iconv", 3, 3, FEARG_1, NULL,
1064 ret_string, f_iconv},
1065 {"indent", 1, 1, FEARG_1, NULL,
1066 ret_number, f_indent},
1067 {"index", 2, 4, FEARG_1, NULL,
1068 ret_number, f_index},
1069 {"input", 1, 3, FEARG_1, NULL,
1070 ret_string, f_input},
1071 {"inputdialog", 1, 3, FEARG_1, NULL,
1072 ret_string, f_inputdialog},
1073 {"inputlist", 1, 1, FEARG_1, NULL,
1074 ret_number, f_inputlist},
1075 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001078 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001079 {"inputsecret", 1, 2, FEARG_1, NULL,
1080 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001081 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001082 ret_first_arg, f_insert},
1083 {"interrupt", 0, 0, 0, NULL,
1084 ret_void, f_interrupt},
1085 {"invert", 1, 1, FEARG_1, NULL,
1086 ret_number, f_invert},
1087 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001088 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001089 {"isinf", 1, 1, FEARG_1, NULL,
1090 ret_number, MATH_FUNC(f_isinf)},
1091 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001093 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001094 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001095 {"items", 1, 1, FEARG_1, NULL,
1096 ret_list_any, f_items},
1097 {"job_getchannel", 1, 1, FEARG_1, NULL,
1098 ret_channel, JOB_FUNC(f_job_getchannel)},
1099 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001100 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001101 {"job_setoptions", 2, 2, FEARG_1, NULL,
1102 ret_void, JOB_FUNC(f_job_setoptions)},
1103 {"job_start", 1, 2, FEARG_1, NULL,
1104 ret_job, JOB_FUNC(f_job_start)},
1105 {"job_status", 1, 1, FEARG_1, NULL,
1106 ret_string, JOB_FUNC(f_job_status)},
1107 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001108 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001109 {"join", 1, 2, FEARG_1, NULL,
1110 ret_string, f_join},
1111 {"js_decode", 1, 1, FEARG_1, NULL,
1112 ret_any, f_js_decode},
1113 {"js_encode", 1, 1, FEARG_1, NULL,
1114 ret_string, f_js_encode},
1115 {"json_decode", 1, 1, FEARG_1, NULL,
1116 ret_any, f_json_decode},
1117 {"json_encode", 1, 1, FEARG_1, NULL,
1118 ret_string, f_json_encode},
1119 {"keys", 1, 1, FEARG_1, NULL,
1120 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001121 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1122 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001123 {"len", 1, 1, FEARG_1, NULL,
1124 ret_number, f_len},
1125 {"libcall", 3, 3, FEARG_3, NULL,
1126 ret_string, f_libcall},
1127 {"libcallnr", 3, 3, FEARG_3, NULL,
1128 ret_number, f_libcallnr},
1129 {"line", 1, 2, FEARG_1, NULL,
1130 ret_number, f_line},
1131 {"line2byte", 1, 1, FEARG_1, NULL,
1132 ret_number, f_line2byte},
1133 {"lispindent", 1, 1, FEARG_1, NULL,
1134 ret_number, f_lispindent},
1135 {"list2str", 1, 2, FEARG_1, NULL,
1136 ret_string, f_list2str},
1137 {"listener_add", 1, 2, FEARG_2, NULL,
1138 ret_number, f_listener_add},
1139 {"listener_flush", 0, 1, FEARG_1, NULL,
1140 ret_void, f_listener_flush},
1141 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001142 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001143 {"localtime", 0, 0, 0, NULL,
1144 ret_number, f_localtime},
1145 {"log", 1, 1, FEARG_1, NULL,
1146 ret_float, FLOAT_FUNC(f_log)},
1147 {"log10", 1, 1, FEARG_1, NULL,
1148 ret_float, FLOAT_FUNC(f_log10)},
1149 {"luaeval", 1, 2, FEARG_1, NULL,
1150 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001151#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001152 f_luaeval
1153#else
1154 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001155#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001156 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001158 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001159 {"maparg", 1, 4, FEARG_1, NULL,
1160 ret_maparg, f_maparg},
1161 {"mapcheck", 1, 3, FEARG_1, NULL,
1162 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001163 {"mapnew", 2, 2, FEARG_1, NULL,
1164 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001165 {"mapset", 3, 3, FEARG_1, NULL,
1166 ret_void, f_mapset},
1167 {"match", 2, 4, FEARG_1, NULL,
1168 ret_any, f_match},
1169 {"matchadd", 2, 5, FEARG_1, NULL,
1170 ret_number, f_matchadd},
1171 {"matchaddpos", 2, 5, FEARG_1, NULL,
1172 ret_number, f_matchaddpos},
1173 {"matcharg", 1, 1, FEARG_1, NULL,
1174 ret_list_string, f_matcharg},
1175 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001176 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001177 {"matchend", 2, 4, FEARG_1, NULL,
1178 ret_number, f_matchend},
1179 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1180 ret_list_string, f_matchfuzzy},
1181 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1182 ret_list_any, f_matchfuzzypos},
1183 {"matchlist", 2, 4, FEARG_1, NULL,
1184 ret_list_string, f_matchlist},
1185 {"matchstr", 2, 4, FEARG_1, NULL,
1186 ret_string, f_matchstr},
1187 {"matchstrpos", 2, 4, FEARG_1, NULL,
1188 ret_list_any, f_matchstrpos},
1189 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001190 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"menu_info", 1, 2, FEARG_1, NULL,
1192 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001193#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001194 f_menu_info
1195#else
1196 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001197#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001198 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001199 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001200 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001201 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001202 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001203 {"mode", 0, 1, FEARG_1, NULL,
1204 ret_string, f_mode},
1205 {"mzeval", 1, 1, FEARG_1, NULL,
1206 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001207#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001208 f_mzeval
1209#else
1210 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001212 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001213 {"nextnonblank", 1, 1, FEARG_1, NULL,
1214 ret_number, f_nextnonblank},
1215 {"nr2char", 1, 2, FEARG_1, NULL,
1216 ret_string, f_nr2char},
1217 {"or", 2, 2, FEARG_1, NULL,
1218 ret_number, f_or},
1219 {"pathshorten", 1, 2, FEARG_1, NULL,
1220 ret_string, f_pathshorten},
1221 {"perleval", 1, 1, FEARG_1, NULL,
1222 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001223#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001224 f_perleval
1225#else
1226 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001228 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001229 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1230 ret_number, PROP_FUNC(f_popup_atcursor)},
1231 {"popup_beval", 2, 2, FEARG_1, NULL,
1232 ret_number, PROP_FUNC(f_popup_beval)},
1233 {"popup_clear", 0, 1, 0, NULL,
1234 ret_void, PROP_FUNC(f_popup_clear)},
1235 {"popup_close", 1, 2, FEARG_1, NULL,
1236 ret_void, PROP_FUNC(f_popup_close)},
1237 {"popup_create", 2, 2, FEARG_1, NULL,
1238 ret_number, PROP_FUNC(f_popup_create)},
1239 {"popup_dialog", 2, 2, FEARG_1, NULL,
1240 ret_number, PROP_FUNC(f_popup_dialog)},
1241 {"popup_filter_menu", 2, 2, 0, NULL,
1242 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1243 {"popup_filter_yesno", 2, 2, 0, NULL,
1244 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1245 {"popup_findinfo", 0, 0, 0, NULL,
1246 ret_number, PROP_FUNC(f_popup_findinfo)},
1247 {"popup_findpreview", 0, 0, 0, NULL,
1248 ret_number, PROP_FUNC(f_popup_findpreview)},
1249 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1250 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1251 {"popup_getpos", 1, 1, FEARG_1, NULL,
1252 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1253 {"popup_hide", 1, 1, FEARG_1, NULL,
1254 ret_void, PROP_FUNC(f_popup_hide)},
1255 {"popup_list", 0, 0, 0, NULL,
1256 ret_list_number, PROP_FUNC(f_popup_list)},
1257 {"popup_locate", 2, 2, 0, NULL,
1258 ret_number, PROP_FUNC(f_popup_locate)},
1259 {"popup_menu", 2, 2, FEARG_1, NULL,
1260 ret_number, PROP_FUNC(f_popup_menu)},
1261 {"popup_move", 2, 2, FEARG_1, NULL,
1262 ret_void, PROP_FUNC(f_popup_move)},
1263 {"popup_notification", 2, 2, FEARG_1, NULL,
1264 ret_number, PROP_FUNC(f_popup_notification)},
1265 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1266 ret_void, PROP_FUNC(f_popup_setoptions)},
1267 {"popup_settext", 2, 2, FEARG_1, NULL,
1268 ret_void, PROP_FUNC(f_popup_settext)},
1269 {"popup_show", 1, 1, FEARG_1, NULL,
1270 ret_void, PROP_FUNC(f_popup_show)},
1271 {"pow", 2, 2, FEARG_1, NULL,
1272 ret_float, FLOAT_FUNC(f_pow)},
1273 {"prevnonblank", 1, 1, FEARG_1, NULL,
1274 ret_number, f_prevnonblank},
1275 {"printf", 1, 19, FEARG_2, NULL,
1276 ret_string, f_printf},
1277 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1278 ret_string, JOB_FUNC(f_prompt_getprompt)},
1279 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1280 ret_void, JOB_FUNC(f_prompt_setcallback)},
1281 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1282 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1283 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1284 ret_void, JOB_FUNC(f_prompt_setprompt)},
1285 {"prop_add", 3, 3, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_prop_add)},
1287 {"prop_clear", 1, 3, FEARG_1, NULL,
1288 ret_void, PROP_FUNC(f_prop_clear)},
1289 {"prop_find", 1, 2, FEARG_1, NULL,
1290 ret_dict_any, PROP_FUNC(f_prop_find)},
1291 {"prop_list", 1, 2, FEARG_1, NULL,
1292 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1293 {"prop_remove", 1, 3, FEARG_1, NULL,
1294 ret_number, PROP_FUNC(f_prop_remove)},
1295 {"prop_type_add", 2, 2, FEARG_1, NULL,
1296 ret_void, PROP_FUNC(f_prop_type_add)},
1297 {"prop_type_change", 2, 2, FEARG_1, NULL,
1298 ret_void, PROP_FUNC(f_prop_type_change)},
1299 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1300 ret_void, PROP_FUNC(f_prop_type_delete)},
1301 {"prop_type_get", 1, 2, FEARG_1, NULL,
1302 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1303 {"prop_type_list", 0, 1, FEARG_1, NULL,
1304 ret_list_string, PROP_FUNC(f_prop_type_list)},
1305 {"pum_getpos", 0, 0, 0, NULL,
1306 ret_dict_number, f_pum_getpos},
1307 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001308 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001309 {"py3eval", 1, 1, FEARG_1, NULL,
1310 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001312 f_py3eval
1313#else
1314 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001316 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001317 {"pyeval", 1, 1, FEARG_1, NULL,
1318 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001320 f_pyeval
1321#else
1322 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001324 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001325 {"pyxeval", 1, 1, FEARG_1, NULL,
1326 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001327#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001328 f_pyxeval
1329#else
1330 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001331#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001332 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001333 {"rand", 0, 1, FEARG_1, NULL,
1334 ret_number, f_rand},
1335 {"range", 1, 3, FEARG_1, NULL,
1336 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001337 {"readblob", 1, 1, FEARG_1, NULL,
1338 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001339 {"readdir", 1, 3, FEARG_1, NULL,
1340 ret_list_string, f_readdir},
1341 {"readdirex", 1, 3, FEARG_1, NULL,
1342 ret_list_dict_any, f_readdirex},
1343 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001344 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001345 {"reduce", 2, 3, FEARG_1, NULL,
1346 ret_any, f_reduce},
1347 {"reg_executing", 0, 0, 0, NULL,
1348 ret_string, f_reg_executing},
1349 {"reg_recording", 0, 0, 0, NULL,
1350 ret_string, f_reg_recording},
1351 {"reltime", 0, 2, FEARG_1, NULL,
1352 ret_list_any, f_reltime},
1353 {"reltimefloat", 1, 1, FEARG_1, NULL,
1354 ret_float, FLOAT_FUNC(f_reltimefloat)},
1355 {"reltimestr", 1, 1, FEARG_1, NULL,
1356 ret_string, f_reltimestr},
1357 {"remote_expr", 2, 4, FEARG_1, NULL,
1358 ret_string, f_remote_expr},
1359 {"remote_foreground", 1, 1, FEARG_1, NULL,
1360 ret_string, f_remote_foreground},
1361 {"remote_peek", 1, 2, FEARG_1, NULL,
1362 ret_number, f_remote_peek},
1363 {"remote_read", 1, 2, FEARG_1, NULL,
1364 ret_string, f_remote_read},
1365 {"remote_send", 2, 3, FEARG_1, NULL,
1366 ret_string, f_remote_send},
1367 {"remote_startserver", 1, 1, FEARG_1, NULL,
1368 ret_void, f_remote_startserver},
1369 {"remove", 2, 3, FEARG_1, NULL,
1370 ret_remove, f_remove},
1371 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001372 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001373 {"repeat", 2, 2, FEARG_1, NULL,
1374 ret_first_arg, f_repeat},
1375 {"resolve", 1, 1, FEARG_1, NULL,
1376 ret_string, f_resolve},
1377 {"reverse", 1, 1, FEARG_1, NULL,
1378 ret_first_arg, f_reverse},
1379 {"round", 1, 1, FEARG_1, NULL,
1380 ret_float, FLOAT_FUNC(f_round)},
1381 {"rubyeval", 1, 1, FEARG_1, NULL,
1382 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001383#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001384 f_rubyeval
1385#else
1386 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001387#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001388 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001389 {"screenattr", 2, 2, FEARG_1, NULL,
1390 ret_number, f_screenattr},
1391 {"screenchar", 2, 2, FEARG_1, NULL,
1392 ret_number, f_screenchar},
1393 {"screenchars", 2, 2, FEARG_1, NULL,
1394 ret_list_number, f_screenchars},
1395 {"screencol", 0, 0, 0, NULL,
1396 ret_number, f_screencol},
1397 {"screenpos", 3, 3, FEARG_1, NULL,
1398 ret_dict_number, f_screenpos},
1399 {"screenrow", 0, 0, 0, NULL,
1400 ret_number, f_screenrow},
1401 {"screenstring", 2, 2, FEARG_1, NULL,
1402 ret_string, f_screenstring},
1403 {"search", 1, 5, FEARG_1, NULL,
1404 ret_number, f_search},
1405 {"searchcount", 0, 1, FEARG_1, NULL,
1406 ret_dict_any, f_searchcount},
1407 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001408 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001409 {"searchpair", 3, 7, 0, NULL,
1410 ret_number, f_searchpair},
1411 {"searchpairpos", 3, 7, 0, NULL,
1412 ret_list_number, f_searchpairpos},
1413 {"searchpos", 1, 5, FEARG_1, NULL,
1414 ret_list_number, f_searchpos},
1415 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001416 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001417 {"serverlist", 0, 0, 0, NULL,
1418 ret_string, f_serverlist},
1419 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001420 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001421 {"setbufvar", 3, 3, FEARG_3, NULL,
1422 ret_void, f_setbufvar},
1423 {"setcellwidths", 1, 1, FEARG_1, NULL,
1424 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001425 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001426 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001427 {"setcharsearch", 1, 1, FEARG_1, NULL,
1428 ret_void, f_setcharsearch},
1429 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001430 ret_number_bool, f_setcmdpos},
1431 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1432 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001433 {"setenv", 2, 2, FEARG_2, NULL,
1434 ret_void, f_setenv},
1435 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001436 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001438 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001440 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001441 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001444 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001445 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001446 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001447 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001448 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001449 {"settabvar", 3, 3, FEARG_3, NULL,
1450 ret_void, f_settabvar},
1451 {"settabwinvar", 4, 4, FEARG_4, NULL,
1452 ret_void, f_settabwinvar},
1453 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001454 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001455 {"setwinvar", 3, 3, FEARG_3, NULL,
1456 ret_void, f_setwinvar},
1457 {"sha256", 1, 1, FEARG_1, NULL,
1458 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001459#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001460 f_sha256
1461#else
1462 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001463#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001464 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"shellescape", 1, 2, FEARG_1, NULL,
1466 ret_string, f_shellescape},
1467 {"shiftwidth", 0, 1, FEARG_1, NULL,
1468 ret_number, f_shiftwidth},
1469 {"sign_define", 1, 2, FEARG_1, NULL,
1470 ret_any, SIGN_FUNC(f_sign_define)},
1471 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1472 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1473 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1474 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1475 {"sign_jump", 3, 3, FEARG_1, NULL,
1476 ret_number, SIGN_FUNC(f_sign_jump)},
1477 {"sign_place", 4, 5, FEARG_1, NULL,
1478 ret_number, SIGN_FUNC(f_sign_place)},
1479 {"sign_placelist", 1, 1, FEARG_1, NULL,
1480 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1481 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001482 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001484 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001485 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1486 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1487 {"simplify", 1, 1, FEARG_1, NULL,
1488 ret_string, f_simplify},
1489 {"sin", 1, 1, FEARG_1, NULL,
1490 ret_float, FLOAT_FUNC(f_sin)},
1491 {"sinh", 1, 1, FEARG_1, NULL,
1492 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001493 {"slice", 2, 3, FEARG_1, NULL,
1494 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001495 {"sort", 1, 3, FEARG_1, NULL,
1496 ret_first_arg, f_sort},
1497 {"sound_clear", 0, 0, 0, NULL,
1498 ret_void, SOUND_FUNC(f_sound_clear)},
1499 {"sound_playevent", 1, 2, FEARG_1, NULL,
1500 ret_number, SOUND_FUNC(f_sound_playevent)},
1501 {"sound_playfile", 1, 2, FEARG_1, NULL,
1502 ret_number, SOUND_FUNC(f_sound_playfile)},
1503 {"sound_stop", 1, 1, FEARG_1, NULL,
1504 ret_void, SOUND_FUNC(f_sound_stop)},
1505 {"soundfold", 1, 1, FEARG_1, NULL,
1506 ret_string, f_soundfold},
1507 {"spellbadword", 0, 1, FEARG_1, NULL,
1508 ret_list_string, f_spellbadword},
1509 {"spellsuggest", 1, 3, FEARG_1, NULL,
1510 ret_list_string, f_spellsuggest},
1511 {"split", 1, 3, FEARG_1, NULL,
1512 ret_list_string, f_split},
1513 {"sqrt", 1, 1, FEARG_1, NULL,
1514 ret_float, FLOAT_FUNC(f_sqrt)},
1515 {"srand", 0, 1, FEARG_1, NULL,
1516 ret_list_number, f_srand},
1517 {"state", 0, 1, FEARG_1, NULL,
1518 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001519 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001520 ret_float, FLOAT_FUNC(f_str2float)},
1521 {"str2list", 1, 2, FEARG_1, NULL,
1522 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001523 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001524 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001525 {"strcharlen", 1, 1, FEARG_1, NULL,
1526 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001527 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001528 ret_string, f_strcharpart},
1529 {"strchars", 1, 2, FEARG_1, NULL,
1530 ret_number, f_strchars},
1531 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1532 ret_number, f_strdisplaywidth},
1533 {"strftime", 1, 2, FEARG_1, NULL,
1534 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001536 f_strftime
1537#else
1538 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001540 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001541 {"strgetchar", 2, 2, FEARG_1, NULL,
1542 ret_number, f_strgetchar},
1543 {"stridx", 2, 3, FEARG_1, NULL,
1544 ret_number, f_stridx},
1545 {"string", 1, 1, FEARG_1, NULL,
1546 ret_string, f_string},
1547 {"strlen", 1, 1, FEARG_1, NULL,
1548 ret_number, f_strlen},
1549 {"strpart", 2, 4, FEARG_1, NULL,
1550 ret_string, f_strpart},
1551 {"strptime", 2, 2, FEARG_1, NULL,
1552 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001553#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001554 f_strptime
1555#else
1556 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001557#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001558 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001559 {"strridx", 2, 3, FEARG_1, NULL,
1560 ret_number, f_strridx},
1561 {"strtrans", 1, 1, FEARG_1, NULL,
1562 ret_string, f_strtrans},
1563 {"strwidth", 1, 1, FEARG_1, NULL,
1564 ret_number, f_strwidth},
1565 {"submatch", 1, 2, FEARG_1, NULL,
1566 ret_string, f_submatch},
1567 {"substitute", 4, 4, FEARG_1, NULL,
1568 ret_string, f_substitute},
1569 {"swapinfo", 1, 1, FEARG_1, NULL,
1570 ret_dict_any, f_swapinfo},
1571 {"swapname", 1, 1, FEARG_1, NULL,
1572 ret_string, f_swapname},
1573 {"synID", 3, 3, 0, NULL,
1574 ret_number, f_synID},
1575 {"synIDattr", 2, 3, FEARG_1, NULL,
1576 ret_string, f_synIDattr},
1577 {"synIDtrans", 1, 1, FEARG_1, NULL,
1578 ret_number, f_synIDtrans},
1579 {"synconcealed", 2, 2, 0, NULL,
1580 ret_list_any, f_synconcealed},
1581 {"synstack", 2, 2, 0, NULL,
1582 ret_list_number, f_synstack},
1583 {"system", 1, 2, FEARG_1, NULL,
1584 ret_string, f_system},
1585 {"systemlist", 1, 2, FEARG_1, NULL,
1586 ret_list_string, f_systemlist},
1587 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1588 ret_list_number, f_tabpagebuflist},
1589 {"tabpagenr", 0, 1, 0, NULL,
1590 ret_number, f_tabpagenr},
1591 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1592 ret_number, f_tabpagewinnr},
1593 {"tagfiles", 0, 0, 0, NULL,
1594 ret_list_string, f_tagfiles},
1595 {"taglist", 1, 2, FEARG_1, NULL,
1596 ret_list_dict_any, f_taglist},
1597 {"tan", 1, 1, FEARG_1, NULL,
1598 ret_float, FLOAT_FUNC(f_tan)},
1599 {"tanh", 1, 1, FEARG_1, NULL,
1600 ret_float, FLOAT_FUNC(f_tanh)},
1601 {"tempname", 0, 0, 0, NULL,
1602 ret_string, f_tempname},
1603 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1604 ret_number, TERM_FUNC(f_term_dumpdiff)},
1605 {"term_dumpload", 1, 2, FEARG_1, NULL,
1606 ret_number, TERM_FUNC(f_term_dumpload)},
1607 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1608 ret_void, TERM_FUNC(f_term_dumpwrite)},
1609 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1610 ret_number, TERM_FUNC(f_term_getaltscreen)},
1611 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1612 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001613#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001614 f_term_getansicolors
1615#else
1616 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001617#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001618 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001619 {"term_getattr", 2, 2, FEARG_1, NULL,
1620 ret_number, TERM_FUNC(f_term_getattr)},
1621 {"term_getcursor", 1, 1, FEARG_1, NULL,
1622 ret_list_any, TERM_FUNC(f_term_getcursor)},
1623 {"term_getjob", 1, 1, FEARG_1, NULL,
1624 ret_job, TERM_FUNC(f_term_getjob)},
1625 {"term_getline", 2, 2, FEARG_1, NULL,
1626 ret_string, TERM_FUNC(f_term_getline)},
1627 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1628 ret_number, TERM_FUNC(f_term_getscrolled)},
1629 {"term_getsize", 1, 1, FEARG_1, NULL,
1630 ret_list_number, TERM_FUNC(f_term_getsize)},
1631 {"term_getstatus", 1, 1, FEARG_1, NULL,
1632 ret_string, TERM_FUNC(f_term_getstatus)},
1633 {"term_gettitle", 1, 1, FEARG_1, NULL,
1634 ret_string, TERM_FUNC(f_term_gettitle)},
1635 {"term_gettty", 1, 2, FEARG_1, NULL,
1636 ret_string, TERM_FUNC(f_term_gettty)},
1637 {"term_list", 0, 0, 0, NULL,
1638 ret_list_number, TERM_FUNC(f_term_list)},
1639 {"term_scrape", 2, 2, FEARG_1, NULL,
1640 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1641 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1642 ret_void, TERM_FUNC(f_term_sendkeys)},
1643 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1644 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001645#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001646 f_term_setansicolors
1647#else
1648 NULL
1649#endif
1650 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001651 {"term_setapi", 2, 2, FEARG_1, NULL,
1652 ret_void, TERM_FUNC(f_term_setapi)},
1653 {"term_setkill", 2, 2, FEARG_1, NULL,
1654 ret_void, TERM_FUNC(f_term_setkill)},
1655 {"term_setrestore", 2, 2, FEARG_1, NULL,
1656 ret_void, TERM_FUNC(f_term_setrestore)},
1657 {"term_setsize", 3, 3, FEARG_1, NULL,
1658 ret_void, TERM_FUNC(f_term_setsize)},
1659 {"term_start", 1, 2, FEARG_1, NULL,
1660 ret_number, TERM_FUNC(f_term_start)},
1661 {"term_wait", 1, 2, FEARG_1, NULL,
1662 ret_void, TERM_FUNC(f_term_wait)},
1663 {"terminalprops", 0, 0, 0, NULL,
1664 ret_dict_string, f_terminalprops},
1665 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1666 ret_void, f_test_alloc_fail},
1667 {"test_autochdir", 0, 0, 0, NULL,
1668 ret_void, f_test_autochdir},
1669 {"test_feedinput", 1, 1, FEARG_1, NULL,
1670 ret_void, f_test_feedinput},
1671 {"test_garbagecollect_now", 0, 0, 0, NULL,
1672 ret_void, f_test_garbagecollect_now},
1673 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1674 ret_void, f_test_garbagecollect_soon},
1675 {"test_getvalue", 1, 1, FEARG_1, NULL,
1676 ret_number, f_test_getvalue},
1677 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1678 ret_void, f_test_ignore_error},
1679 {"test_null_blob", 0, 0, 0, NULL,
1680 ret_blob, f_test_null_blob},
1681 {"test_null_channel", 0, 0, 0, NULL,
1682 ret_channel, JOB_FUNC(f_test_null_channel)},
1683 {"test_null_dict", 0, 0, 0, NULL,
1684 ret_dict_any, f_test_null_dict},
1685 {"test_null_function", 0, 0, 0, NULL,
1686 ret_func_any, f_test_null_function},
1687 {"test_null_job", 0, 0, 0, NULL,
1688 ret_job, JOB_FUNC(f_test_null_job)},
1689 {"test_null_list", 0, 0, 0, NULL,
1690 ret_list_any, f_test_null_list},
1691 {"test_null_partial", 0, 0, 0, NULL,
1692 ret_func_any, f_test_null_partial},
1693 {"test_null_string", 0, 0, 0, NULL,
1694 ret_string, f_test_null_string},
1695 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1696 ret_void, f_test_option_not_set},
1697 {"test_override", 2, 2, FEARG_2, NULL,
1698 ret_void, f_test_override},
1699 {"test_refcount", 1, 1, FEARG_1, NULL,
1700 ret_number, f_test_refcount},
1701 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1702 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001703#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001704 f_test_scrollbar
1705#else
1706 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001707#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001708 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001709 {"test_setmouse", 2, 2, 0, NULL,
1710 ret_void, f_test_setmouse},
1711 {"test_settime", 1, 1, FEARG_1, NULL,
1712 ret_void, f_test_settime},
1713 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1714 ret_void, f_test_srand_seed},
1715 {"test_unknown", 0, 0, 0, NULL,
1716 ret_any, f_test_unknown},
1717 {"test_void", 0, 0, 0, NULL,
1718 ret_void, f_test_void},
1719 {"timer_info", 0, 1, FEARG_1, NULL,
1720 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1721 {"timer_pause", 2, 2, FEARG_1, NULL,
1722 ret_void, TIMER_FUNC(f_timer_pause)},
1723 {"timer_start", 2, 3, FEARG_1, NULL,
1724 ret_number, TIMER_FUNC(f_timer_start)},
1725 {"timer_stop", 1, 1, FEARG_1, NULL,
1726 ret_void, TIMER_FUNC(f_timer_stop)},
1727 {"timer_stopall", 0, 0, 0, NULL,
1728 ret_void, TIMER_FUNC(f_timer_stopall)},
1729 {"tolower", 1, 1, FEARG_1, NULL,
1730 ret_string, f_tolower},
1731 {"toupper", 1, 1, FEARG_1, NULL,
1732 ret_string, f_toupper},
1733 {"tr", 3, 3, FEARG_1, NULL,
1734 ret_string, f_tr},
1735 {"trim", 1, 3, FEARG_1, NULL,
1736 ret_string, f_trim},
1737 {"trunc", 1, 1, FEARG_1, NULL,
1738 ret_float, FLOAT_FUNC(f_trunc)},
1739 {"type", 1, 1, FEARG_1, NULL,
1740 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001741 {"typename", 1, 1, FEARG_1, NULL,
1742 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001743 {"undofile", 1, 1, FEARG_1, NULL,
1744 ret_string, f_undofile},
1745 {"undotree", 0, 0, 0, NULL,
1746 ret_dict_any, f_undotree},
1747 {"uniq", 1, 3, FEARG_1, NULL,
1748 ret_list_any, f_uniq},
1749 {"values", 1, 1, FEARG_1, NULL,
1750 ret_list_any, f_values},
1751 {"virtcol", 1, 1, FEARG_1, NULL,
1752 ret_number, f_virtcol},
1753 {"visualmode", 0, 1, 0, NULL,
1754 ret_string, f_visualmode},
1755 {"wildmenumode", 0, 0, 0, NULL,
1756 ret_number, f_wildmenumode},
1757 {"win_execute", 2, 3, FEARG_2, NULL,
1758 ret_string, f_win_execute},
1759 {"win_findbuf", 1, 1, FEARG_1, NULL,
1760 ret_list_number, f_win_findbuf},
1761 {"win_getid", 0, 2, FEARG_1, NULL,
1762 ret_number, f_win_getid},
1763 {"win_gettype", 0, 1, FEARG_1, NULL,
1764 ret_string, f_win_gettype},
1765 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001766 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001767 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1768 ret_list_number, f_win_id2tabwin},
1769 {"win_id2win", 1, 1, FEARG_1, NULL,
1770 ret_number, f_win_id2win},
1771 {"win_screenpos", 1, 1, FEARG_1, NULL,
1772 ret_list_number, f_win_screenpos},
1773 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001774 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001775 {"winbufnr", 1, 1, FEARG_1, NULL,
1776 ret_number, f_winbufnr},
1777 {"wincol", 0, 0, 0, NULL,
1778 ret_number, f_wincol},
1779 {"windowsversion", 0, 0, 0, NULL,
1780 ret_string, f_windowsversion},
1781 {"winheight", 1, 1, FEARG_1, NULL,
1782 ret_number, f_winheight},
1783 {"winlayout", 0, 1, FEARG_1, NULL,
1784 ret_list_any, f_winlayout},
1785 {"winline", 0, 0, 0, NULL,
1786 ret_number, f_winline},
1787 {"winnr", 0, 1, FEARG_1, NULL,
1788 ret_number, f_winnr},
1789 {"winrestcmd", 0, 0, 0, NULL,
1790 ret_string, f_winrestcmd},
1791 {"winrestview", 1, 1, FEARG_1, NULL,
1792 ret_void, f_winrestview},
1793 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001794 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001795 {"winwidth", 1, 1, FEARG_1, NULL,
1796 ret_number, f_winwidth},
1797 {"wordcount", 0, 0, 0, NULL,
1798 ret_dict_number, f_wordcount},
1799 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001800 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001801 {"xor", 2, 2, FEARG_1, NULL,
1802 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001803};
1804
K.Takataeecf2b32021-06-02 14:56:39 +02001805#if defined(EBCDIC) || defined(PROTO)
1806/*
1807 * Compare funcentry_T by function name.
1808 */
1809 static int
1810compare_func_name(const void *s1, const void *s2)
1811{
1812 funcentry_T *p1 = (funcentry_T *)s1;
1813 funcentry_T *p2 = (funcentry_T *)s2;
1814
1815 return STRCMP(p1->f_name, p2->f_name);
1816}
1817
1818/*
1819 * Sort the function table by function name.
1820 * The sorting of the table above is ASCII dependent.
1821 * On machines using EBCDIC we have to sort it.
1822 */
1823 void
1824sortFunctions(void)
1825{
1826 size_t funcCnt = ARRAY_LENGTH(global_functions);
1827
1828 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1829}
1830#endif
1831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832/*
1833 * Function given to ExpandGeneric() to obtain the list of internal
1834 * or user defined function names.
1835 */
1836 char_u *
1837get_function_name(expand_T *xp, int idx)
1838{
1839 static int intidx = -1;
1840 char_u *name;
1841
1842 if (idx == 0)
1843 intidx = -1;
1844 if (intidx < 0)
1845 {
1846 name = get_user_func_name(xp, idx);
1847 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001848 {
1849 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1850 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 }
K.Takataeeec2542021-06-02 13:28:16 +02001854 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001858 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001859 STRCAT(IObuff, ")");
1860 return IObuff;
1861 }
1862
1863 return NULL;
1864}
1865
1866/*
1867 * Function given to ExpandGeneric() to obtain the list of internal or
1868 * user defined variable or function names.
1869 */
1870 char_u *
1871get_expr_name(expand_T *xp, int idx)
1872{
1873 static int intidx = -1;
1874 char_u *name;
1875
1876 if (idx == 0)
1877 intidx = -1;
1878 if (intidx < 0)
1879 {
1880 name = get_function_name(xp, idx);
1881 if (name != NULL)
1882 return name;
1883 }
1884 return get_user_var_name(xp, ++intidx);
1885}
1886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001888 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001889 * Return index, or -1 if not found or "implemented" is TRUE and the function
1890 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001892 static int
1893find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894{
1895 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001896 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001897 int cmp;
1898 int x;
1899
K.Takataeeec2542021-06-02 13:28:16 +02001900 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001901
1902 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903 while (first <= last)
1904 {
1905 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001906 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 if (cmp < 0)
1908 last = x - 1;
1909 else if (cmp > 0)
1910 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001911 else if (implemented && global_functions[x].f_func == NULL)
1912 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913 else
1914 return x;
1915 }
1916 return -1;
1917}
1918
Bram Moolenaar15c47602020-03-26 22:16:48 +01001919/*
1920 * Find internal function "name" in table "global_functions".
1921 * Return index, or -1 if not found or the function is not implemented.
1922 */
1923 int
1924find_internal_func(char_u *name)
1925{
1926 return find_internal_func_opt(name, TRUE);
1927}
1928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001930has_internal_func(char_u *name)
1931{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001932 return find_internal_func_opt(name, TRUE) >= 0;
1933}
1934
1935 static int
1936has_internal_func_name(char_u *name)
1937{
1938 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001939}
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 char *
1942internal_func_name(int idx)
1943{
1944 return global_functions[idx].f_name;
1945}
1946
Bram Moolenaar94738d82020-10-21 14:25:07 +02001947/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001948 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001949 * Uses the list of types on the type stack: "types".
1950 * Return FAIL and gives an error message when a type is wrong.
1951 */
1952 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001953internal_func_check_arg_types(
1954 type_T **types,
1955 int idx,
1956 int argcount,
1957 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001958{
1959 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1960 int i;
1961
1962 if (argchecks != NULL)
1963 {
1964 argcontext_T context;
1965
1966 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001967 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001968 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001969 for (i = 0; i < argcount; ++i)
1970 if (argchecks[i] != NULL)
1971 {
1972 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001973 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001974 return FAIL;
1975 }
1976 }
1977 return OK;
1978}
1979
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001980/*
1981 * Call the "f_retfunc" function to obtain the return type of function "idx".
1982 * "argtypes" is the list of argument types or NULL when there are no
1983 * arguments.
1984 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001988 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989}
1990
1991/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001992 * Return TRUE if "idx" is for the map() function.
1993 */
1994 int
1995internal_func_is_map(int idx)
1996{
1997 return global_functions[idx].f_func == f_map;
1998}
1999
2000/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002002 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2003 * first argument, 2 if method base is second argument, etc. 9 if method base
2004 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 */
2006 int
2007check_internal_func(int idx, int argcount)
2008{
2009 int res;
2010 char *name;
2011
2012 if (argcount < global_functions[idx].f_min_argc)
2013 res = FCERR_TOOFEW;
2014 else if (argcount > global_functions[idx].f_max_argc)
2015 res = FCERR_TOOMANY;
2016 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002017 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018
2019 name = internal_func_name(idx);
2020 if (res == FCERR_TOOMANY)
2021 semsg(_(e_toomanyarg), name);
2022 else
2023 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002024 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025}
2026
Bram Moolenaarac92e252019-08-03 21:58:38 +02002027 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002028call_internal_func(
2029 char_u *name,
2030 int argcount,
2031 typval_T *argvars,
2032 typval_T *rettv)
2033{
2034 int i;
2035
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002036 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002037 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002041 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002042 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002045 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002046}
2047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 void
2049call_internal_func_by_idx(
2050 int idx,
2051 typval_T *argvars,
2052 typval_T *rettv)
2053{
2054 global_functions[idx].f_func(argvars, rettv);
2055}
2056
Bram Moolenaarac92e252019-08-03 21:58:38 +02002057/*
2058 * Invoke a method for base->method().
2059 */
2060 int
2061call_internal_method(
2062 char_u *name,
2063 int argcount,
2064 typval_T *argvars,
2065 typval_T *rettv,
2066 typval_T *basetv)
2067{
2068 int i;
2069 int fi;
2070 typval_T argv[MAX_FUNC_ARGS + 1];
2071
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002072 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002075 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002079 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002080 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002081
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002082 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002083 {
2084 // base value goes last
2085 for (i = 0; i < argcount; ++i)
2086 argv[i] = argvars[i];
2087 argv[argcount] = *basetv;
2088 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002089 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002090 {
2091 // base value goes second
2092 argv[0] = argvars[0];
2093 argv[1] = *basetv;
2094 for (i = 1; i < argcount; ++i)
2095 argv[i + 1] = argvars[i];
2096 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002097 else if (global_functions[fi].f_argtype == FEARG_3)
2098 {
2099 // base value goes third
2100 argv[0] = argvars[0];
2101 argv[1] = argvars[1];
2102 argv[2] = *basetv;
2103 for (i = 2; i < argcount; ++i)
2104 argv[i + 1] = argvars[i];
2105 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002106 else if (global_functions[fi].f_argtype == FEARG_4)
2107 {
2108 // base value goes fourth
2109 argv[0] = argvars[0];
2110 argv[1] = argvars[1];
2111 argv[2] = argvars[2];
2112 argv[3] = *basetv;
2113 for (i = 3; i < argcount; ++i)
2114 argv[i + 1] = argvars[i];
2115 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002116 else
2117 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002118 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002119 argv[0] = *basetv;
2120 for (i = 0; i < argcount; ++i)
2121 argv[i + 1] = argvars[i];
2122 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002123 argv[argcount + 1].v_type = VAR_UNKNOWN;
2124
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002125 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002126 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002127}
2128
2129/*
2130 * Return TRUE for a non-zero Number and a non-empty String.
2131 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002132 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002133non_zero_arg(typval_T *argvars)
2134{
2135 return ((argvars[0].v_type == VAR_NUMBER
2136 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002137 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002138 && argvars[0].vval.v_number == VVAL_TRUE)
2139 || (argvars[0].v_type == VAR_STRING
2140 && argvars[0].vval.v_string != NULL
2141 && *argvars[0].vval.v_string != NUL));
2142}
2143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 * "and(expr, expr)" function
2146 */
2147 static void
2148f_and(typval_T *argvars, typval_T *rettv)
2149{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002150 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2151 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002152}
2153
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002155 * "balloon_show()" function
2156 */
2157#ifdef FEAT_BEVAL
2158 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002159f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2160{
2161 rettv->v_type = VAR_STRING;
2162 if (balloonEval != NULL)
2163 {
2164 if (balloonEval->msg == NULL)
2165 rettv->vval.v_string = NULL;
2166 else
2167 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2168 }
2169}
2170
2171 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002172f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2173{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002174 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002175 {
2176 if (argvars[0].v_type == VAR_LIST
2177# ifdef FEAT_GUI
2178 && !gui.in_use
2179# endif
2180 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002181 {
2182 list_T *l = argvars[0].vval.v_list;
2183
2184 // empty list removes the balloon
2185 post_balloon(balloonEval, NULL,
2186 l == NULL || l->lv_len == 0 ? NULL : l);
2187 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002188 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002189 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002190 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002191
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002192 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2193 return;
2194
2195 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002196 if (mesg != NULL)
2197 // empty string removes the balloon
2198 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2199 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002200 }
2201}
2202
Bram Moolenaar669a8282017-11-19 20:13:05 +01002203# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002204 static void
2205f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2206{
2207 if (rettv_list_alloc(rettv) == OK)
2208 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002209 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002210
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002211 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2212 return;
2213 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002214 if (msg != NULL)
2215 {
2216 pumitem_T *array;
2217 int size = split_message(msg, &array);
2218 int i;
2219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002220 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002221 for (i = 1; i < size - 1; ++i)
2222 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002223 while (size > 0)
2224 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002225 vim_free(array);
2226 }
2227 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002228}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002229# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002230#endif
2231
2232/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002233 * Get the buffer from "arg" and give an error and return NULL if it is not
2234 * valid.
2235 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002236 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002237get_buf_arg(typval_T *arg)
2238{
2239 buf_T *buf;
2240
2241 ++emsg_off;
2242 buf = tv_get_buf(arg, FALSE);
2243 --emsg_off;
2244 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002245 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002246 return buf;
2247}
2248
2249/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002250 * "byte2line(byte)" function
2251 */
2252 static void
2253f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2254{
2255#ifndef FEAT_BYTEOFF
2256 rettv->vval.v_number = -1;
2257#else
2258 long boff = 0;
2259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 if (boff < 0)
2262 rettv->vval.v_number = -1;
2263 else
2264 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2265 (linenr_T)0, &boff);
2266#endif
2267}
2268
2269 static void
2270byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2271{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002272 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 char_u *str;
2274 varnumber_T idx;
2275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002276 str = tv_get_string_chk(&argvars[0]);
2277 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278 rettv->vval.v_number = -1;
2279 if (str == NULL || idx < 0)
2280 return;
2281
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 t = str;
2283 for ( ; idx > 0; idx--)
2284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002285 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 return;
2287 if (enc_utf8 && comp)
2288 t += utf_ptr2len(t);
2289 else
2290 t += (*mb_ptr2len)(t);
2291 }
2292 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002293}
2294
2295/*
2296 * "byteidx()" function
2297 */
2298 static void
2299f_byteidx(typval_T *argvars, typval_T *rettv)
2300{
2301 byteidx(argvars, rettv, FALSE);
2302}
2303
2304/*
2305 * "byteidxcomp()" function
2306 */
2307 static void
2308f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2309{
2310 byteidx(argvars, rettv, TRUE);
2311}
2312
2313/*
2314 * "call(func, arglist [, dict])" function
2315 */
2316 static void
2317f_call(typval_T *argvars, typval_T *rettv)
2318{
2319 char_u *func;
2320 partial_T *partial = NULL;
2321 dict_T *selfdict = NULL;
2322
2323 if (argvars[1].v_type != VAR_LIST)
2324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 return;
2327 }
2328 if (argvars[1].vval.v_list == NULL)
2329 return;
2330
2331 if (argvars[0].v_type == VAR_FUNC)
2332 func = argvars[0].vval.v_string;
2333 else if (argvars[0].v_type == VAR_PARTIAL)
2334 {
2335 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002336 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337 }
2338 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002339 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002340 if (func == NULL || *func == NUL)
2341 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342
2343 if (argvars[2].v_type != VAR_UNKNOWN)
2344 {
2345 if (argvars[2].v_type != VAR_DICT)
2346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002348 return;
2349 }
2350 selfdict = argvars[2].vval.v_dict;
2351 }
2352
2353 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2354}
2355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356/*
2357 * "changenr()" function
2358 */
2359 static void
2360f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2361{
2362 rettv->vval.v_number = curbuf->b_u_seq_cur;
2363}
2364
2365/*
2366 * "char2nr(string)" function
2367 */
2368 static void
2369f_char2nr(typval_T *argvars, typval_T *rettv)
2370{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002371 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2372 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 if (has_mbyte)
2374 {
2375 int utf8 = 0;
2376
2377 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002378 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379
2380 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002381 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002383 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384 }
2385 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002386 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387}
2388
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002389/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002390 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2391 * returns the character index of the column. Otherwise, returns the byte index
2392 * of the column.
2393 */
2394 static void
2395get_col(typval_T *argvars, typval_T *rettv, int charcol)
2396{
2397 colnr_T col = 0;
2398 pos_T *fp;
2399 int fnum = curbuf->b_fnum;
2400
2401 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2402 if (fp != NULL && fnum == curbuf->b_fnum)
2403 {
2404 if (fp->col == MAXCOL)
2405 {
2406 // '> can be MAXCOL, get the length of the line then
2407 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2408 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2409 else
2410 col = MAXCOL;
2411 }
2412 else
2413 {
2414 col = fp->col + 1;
2415 // col(".") when the cursor is on the NUL at the end of the line
2416 // because of "coladd" can be seen as an extra column.
2417 if (virtual_active() && fp == &curwin->w_cursor)
2418 {
2419 char_u *p = ml_get_cursor();
2420
2421 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2422 curwin->w_virtcol - curwin->w_cursor.coladd))
2423 {
2424 int l;
2425
2426 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2427 col += l;
2428 }
2429 }
2430 }
2431 }
2432 rettv->vval.v_number = col;
2433}
2434
2435/*
2436 * "charcol()" function
2437 */
2438 static void
2439f_charcol(typval_T *argvars, typval_T *rettv)
2440{
2441 get_col(argvars, rettv, TRUE);
2442}
2443
2444/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002445 * "charidx()" function
2446 */
2447 static void
2448f_charidx(typval_T *argvars, typval_T *rettv)
2449{
2450 char_u *str;
2451 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002452 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002453 char_u *p;
2454 int len;
2455 int (*ptr2len)(char_u *);
2456
2457 rettv->vval.v_number = -1;
2458
2459 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2460 || (argvars[2].v_type != VAR_UNKNOWN
2461 && argvars[2].v_type != VAR_NUMBER))
2462 {
2463 emsg(_(e_invarg));
2464 return;
2465 }
2466
2467 str = tv_get_string_chk(&argvars[0]);
2468 idx = tv_get_number_chk(&argvars[1], NULL);
2469 if (str == NULL || idx < 0)
2470 return;
2471
2472 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002473 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002474 if (countcc < 0 || countcc > 1)
2475 {
2476 semsg(_(e_using_number_as_bool_nr), countcc);
2477 return;
2478 }
2479
2480 if (enc_utf8 && countcc)
2481 ptr2len = utf_ptr2len;
2482 else
2483 ptr2len = mb_ptr2len;
2484
2485 for (p = str, len = 0; p <= str + idx; len++)
2486 {
2487 if (*p == NUL)
2488 return;
2489 p += ptr2len(p);
2490 }
2491
2492 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2493}
2494
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002495 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002496get_optional_window(typval_T *argvars, int idx)
2497{
2498 win_T *win = curwin;
2499
2500 if (argvars[idx].v_type != VAR_UNKNOWN)
2501 {
2502 win = find_win_by_nr_or_id(&argvars[idx]);
2503 if (win == NULL)
2504 {
2505 emsg(_(e_invalwindow));
2506 return NULL;
2507 }
2508 }
2509 return win;
2510}
2511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002512/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002513 * "col(string)" function
2514 */
2515 static void
2516f_col(typval_T *argvars, typval_T *rettv)
2517{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002518 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519}
2520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521/*
2522 * "confirm(message, buttons[, default [, type]])" function
2523 */
2524 static void
2525f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2526{
2527#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2528 char_u *message;
2529 char_u *buttons = NULL;
2530 char_u buf[NUMBUFLEN];
2531 char_u buf2[NUMBUFLEN];
2532 int def = 1;
2533 int type = VIM_GENERIC;
2534 char_u *typestr;
2535 int error = FALSE;
2536
Bram Moolenaarc5809432021-03-27 21:23:30 +01002537 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2538 return;
2539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (message == NULL)
2542 error = TRUE;
2543 if (argvars[1].v_type != VAR_UNKNOWN)
2544 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002545 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2546 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548 if (buttons == NULL)
2549 error = TRUE;
2550 if (argvars[2].v_type != VAR_UNKNOWN)
2551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002552 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002553 if (argvars[3].v_type != VAR_UNKNOWN)
2554 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002555 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2556 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002557 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002558 if (typestr == NULL)
2559 error = TRUE;
2560 else
2561 {
2562 switch (TOUPPER_ASC(*typestr))
2563 {
2564 case 'E': type = VIM_ERROR; break;
2565 case 'Q': type = VIM_QUESTION; break;
2566 case 'I': type = VIM_INFO; break;
2567 case 'W': type = VIM_WARNING; break;
2568 case 'G': type = VIM_GENERIC; break;
2569 }
2570 }
2571 }
2572 }
2573 }
2574
2575 if (buttons == NULL || *buttons == NUL)
2576 buttons = (char_u *)_("&Ok");
2577
2578 if (!error)
2579 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2580 def, NULL, FALSE);
2581#endif
2582}
2583
2584/*
2585 * "copy()" function
2586 */
2587 static void
2588f_copy(typval_T *argvars, typval_T *rettv)
2589{
2590 item_copy(&argvars[0], rettv, FALSE, 0);
2591}
2592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002594 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002595 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002596 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002597 */
2598 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002599set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002600{
2601 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 int set_curswant = TRUE;
2604
2605 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002606 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 {
2608 pos_T pos;
2609 colnr_T curswant = -1;
2610
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002611 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002613 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 return;
2615 }
2616 line = pos.lnum;
2617 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (curswant >= 0)
2620 {
2621 curwin->w_curswant = curswant - 1;
2622 set_curswant = FALSE;
2623 }
2624 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002625 else if ((argvars[0].v_type == VAR_NUMBER ||
2626 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002627 && (argvars[1].v_type == VAR_NUMBER ||
2628 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002630 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002631 if (line < 0)
2632 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002633 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002634 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002635 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002637 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002639 else
2640 {
2641 emsg(_(e_invarg));
2642 return;
2643 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002644 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002645 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646 if (line > 0)
2647 curwin->w_cursor.lnum = line;
2648 if (col > 0)
2649 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002654 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 if (has_mbyte)
2656 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657
2658 curwin->w_set_curswant = set_curswant;
2659 rettv->vval.v_number = 0;
2660}
2661
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002662/*
2663 * "cursor(lnum, col)" function, or
2664 * "cursor(list)"
2665 *
2666 * Moves the cursor to the specified line and column.
2667 * Returns 0 when the position could be set, -1 otherwise.
2668 */
2669 static void
2670f_cursor(typval_T *argvars, typval_T *rettv)
2671{
2672 set_cursorpos(argvars, rettv, FALSE);
2673}
2674
Bram Moolenaar4f974752019-02-17 17:44:42 +01002675#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002676/*
2677 * "debugbreak()" function
2678 */
2679 static void
2680f_debugbreak(typval_T *argvars, typval_T *rettv)
2681{
2682 int pid;
2683
2684 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002685 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002686 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002688 else
2689 {
2690 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2691
2692 if (hProcess != NULL)
2693 {
2694 DebugBreakProcess(hProcess);
2695 CloseHandle(hProcess);
2696 rettv->vval.v_number = OK;
2697 }
2698 }
2699}
2700#endif
2701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702/*
2703 * "deepcopy()" function
2704 */
2705 static void
2706f_deepcopy(typval_T *argvars, typval_T *rettv)
2707{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002708 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002709 int copyID;
2710
2711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002712 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002714 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 else
2716 {
2717 copyID = get_copyID();
2718 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2719 }
2720}
2721
2722/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 * "did_filetype()" function
2724 */
2725 static void
2726f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2727{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729}
2730
2731/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002732 * "echoraw({expr})" function
2733 */
2734 static void
2735f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2736{
2737 char_u *str = tv_get_string_chk(&argvars[0]);
2738
2739 if (str != NULL && *str != NUL)
2740 {
2741 out_str(str);
2742 out_flush();
2743 }
2744}
2745
2746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002747 * "empty({expr})" function
2748 */
2749 static void
2750f_empty(typval_T *argvars, typval_T *rettv)
2751{
2752 int n = FALSE;
2753
2754 switch (argvars[0].v_type)
2755 {
2756 case VAR_STRING:
2757 case VAR_FUNC:
2758 n = argvars[0].vval.v_string == NULL
2759 || *argvars[0].vval.v_string == NUL;
2760 break;
2761 case VAR_PARTIAL:
2762 n = FALSE;
2763 break;
2764 case VAR_NUMBER:
2765 n = argvars[0].vval.v_number == 0;
2766 break;
2767 case VAR_FLOAT:
2768#ifdef FEAT_FLOAT
2769 n = argvars[0].vval.v_float == 0.0;
2770 break;
2771#endif
2772 case VAR_LIST:
2773 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002774 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 break;
2776 case VAR_DICT:
2777 n = argvars[0].vval.v_dict == NULL
2778 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2779 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002780 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002781 case VAR_SPECIAL:
2782 n = argvars[0].vval.v_number != VVAL_TRUE;
2783 break;
2784
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002785 case VAR_BLOB:
2786 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002787 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2788 break;
2789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 case VAR_JOB:
2791#ifdef FEAT_JOB_CHANNEL
2792 n = argvars[0].vval.v_job == NULL
2793 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2794 break;
2795#endif
2796 case VAR_CHANNEL:
2797#ifdef FEAT_JOB_CHANNEL
2798 n = argvars[0].vval.v_channel == NULL
2799 || !channel_is_open(argvars[0].vval.v_channel);
2800 break;
2801#endif
2802 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002803 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002805 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002806 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807 n = TRUE;
2808 break;
2809 }
2810
2811 rettv->vval.v_number = n;
2812}
2813
2814/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002815 * "environ()" function
2816 */
2817 static void
2818f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2819{
2820#if !defined(AMIGA)
2821 int i = 0;
2822 char_u *entry, *value;
2823# ifdef MSWIN
2824 extern wchar_t **_wenviron;
2825# else
2826 extern char **environ;
2827# endif
2828
2829 if (rettv_dict_alloc(rettv) != OK)
2830 return;
2831
2832# ifdef MSWIN
2833 if (*_wenviron == NULL)
2834 return;
2835# else
2836 if (*environ == NULL)
2837 return;
2838# endif
2839
2840 for (i = 0; ; ++i)
2841 {
2842# ifdef MSWIN
2843 short_u *p;
2844
2845 if ((p = (short_u *)_wenviron[i]) == NULL)
2846 return;
2847 entry = utf16_to_enc(p, NULL);
2848# else
2849 if ((entry = (char_u *)environ[i]) == NULL)
2850 return;
2851 entry = vim_strsave(entry);
2852# endif
2853 if (entry == NULL) // out of memory
2854 return;
2855 if ((value = vim_strchr(entry, '=')) == NULL)
2856 {
2857 vim_free(entry);
2858 continue;
2859 }
2860 *value++ = NUL;
2861 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2862 vim_free(entry);
2863 }
2864#endif
2865}
2866
2867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 * "escape({string}, {chars})" function
2869 */
2870 static void
2871f_escape(typval_T *argvars, typval_T *rettv)
2872{
2873 char_u buf[NUMBUFLEN];
2874
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002875 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2876 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002877 rettv->v_type = VAR_STRING;
2878}
2879
2880/*
2881 * "eval()" function
2882 */
2883 static void
2884f_eval(typval_T *argvars, typval_T *rettv)
2885{
2886 char_u *s, *p;
2887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002888 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889 if (s != NULL)
2890 s = skipwhite(s);
2891
2892 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 {
2895 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002897 need_clr_eos = FALSE;
2898 rettv->v_type = VAR_NUMBER;
2899 rettv->vval.v_number = 0;
2900 }
2901 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002902 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002903}
2904
2905/*
2906 * "eventhandler()" function
2907 */
2908 static void
2909f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2910{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002911 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912}
2913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914static garray_T redir_execute_ga;
2915
2916/*
2917 * Append "value[value_len]" to the execute() output.
2918 */
2919 void
2920execute_redir_str(char_u *value, int value_len)
2921{
2922 int len;
2923
2924 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002927 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002928 if (ga_grow(&redir_execute_ga, len) == OK)
2929 {
2930 mch_memmove((char *)redir_execute_ga.ga_data
2931 + redir_execute_ga.ga_len, value, len);
2932 redir_execute_ga.ga_len += len;
2933 }
2934}
2935
2936/*
2937 * Get next line from a list.
2938 * Called by do_cmdline() to get the next line.
2939 * Returns allocated string, or NULL for end of function.
2940 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 static char_u *
2942get_list_line(
2943 int c UNUSED,
2944 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002945 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002946 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947{
2948 listitem_T **p = (listitem_T **)cookie;
2949 listitem_T *item = *p;
2950 char_u buf[NUMBUFLEN];
2951 char_u *s;
2952
2953 if (item == NULL)
2954 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002955 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 *p = item->li_next;
2957 return s == NULL ? NULL : vim_strsave(s);
2958}
2959
2960/*
2961 * "execute()" function
2962 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002963 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002964execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965{
2966 char_u *cmd = NULL;
2967 list_T *list = NULL;
2968 int save_msg_silent = msg_silent;
2969 int save_emsg_silent = emsg_silent;
2970 int save_emsg_noredir = emsg_noredir;
2971 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002972 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002974 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002975 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002976
2977 rettv->vval.v_string = NULL;
2978 rettv->v_type = VAR_STRING;
2979
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002980 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002982 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002983 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002984 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 return;
2986 ++list->lv_refcount;
2987 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002988 else if (argvars[arg_off].v_type == VAR_JOB
2989 || argvars[arg_off].v_type == VAR_CHANNEL)
2990 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02002991 semsg(_(e_using_invalid_value_as_string_str),
2992 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002993 return;
2994 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 else
2996 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002997 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 if (cmd == NULL)
2999 return;
3000 }
3001
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003002 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 {
3004 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003005 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3006 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007
3008 if (s == NULL)
3009 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003010 if (*s == NUL)
3011 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 if (STRNCMP(s, "silent", 6) == 0)
3013 ++msg_silent;
3014 if (STRCMP(s, "silent!") == 0)
3015 {
3016 emsg_silent = TRUE;
3017 emsg_noredir = TRUE;
3018 }
3019 }
3020 else
3021 ++msg_silent;
3022
3023 if (redir_execute)
3024 save_ga = redir_execute_ga;
3025 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3026 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003027 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003028 if (!echo_output)
3029 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030
3031 if (cmd != NULL)
3032 do_cmdline_cmd(cmd);
3033 else
3034 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003035 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003037 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003038 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 do_cmdline(NULL, get_list_line, (void *)&item,
3040 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3041 --list->lv_refcount;
3042 }
3043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003044 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003045 if (ga_grow(&redir_execute_ga, 1) == OK)
3046 {
3047 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3048 rettv->vval.v_string = redir_execute_ga.ga_data;
3049 }
3050 else
3051 {
3052 ga_clear(&redir_execute_ga);
3053 rettv->vval.v_string = NULL;
3054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 msg_silent = save_msg_silent;
3056 emsg_silent = save_emsg_silent;
3057 emsg_noredir = save_emsg_noredir;
3058
3059 redir_execute = save_redir_execute;
3060 if (redir_execute)
3061 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003062 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003064 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003065 if (echo_output)
3066 // When not working silently: put it in column zero. A following
3067 // "echon" will overwrite the message, unavoidably.
3068 msg_col = 0;
3069 else
3070 // When working silently: Put it back where it was, since nothing
3071 // should have been written.
3072 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073}
3074
3075/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003076 * "execute()" function
3077 */
3078 static void
3079f_execute(typval_T *argvars, typval_T *rettv)
3080{
3081 execute_common(argvars, rettv, 0);
3082}
3083
3084/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 * "exists()" function
3086 */
3087 static void
3088f_exists(typval_T *argvars, typval_T *rettv)
3089{
3090 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003093 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003094 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 if (mch_getenv(p + 1) != NULL)
3098 n = TRUE;
3099 else
3100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003101 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 p = expand_env_save(p);
3103 if (p != NULL && *p != '$')
3104 n = TRUE;
3105 vim_free(p);
3106 }
3107 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003108 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003109 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003110 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003112 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003116 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003118 else if (*p == '?') // internal function only
3119 {
3120 n = has_internal_func_name(p + 1);
3121 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122 else if (*p == ':')
3123 {
3124 n = cmd_exists(p + 1);
3125 }
3126 else if (*p == '#')
3127 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 if (p[1] == '#')
3129 n = autocmd_supported(p + 2);
3130 else
3131 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003133 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003135 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003136 }
3137
3138 rettv->vval.v_number = n;
3139}
3140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141/*
3142 * "expand()" function
3143 */
3144 static void
3145f_expand(typval_T *argvars, typval_T *rettv)
3146{
3147 char_u *s;
3148 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3151 expand_T xpc;
3152 int error = FALSE;
3153 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003154#ifdef BACKSLASH_IN_FILENAME
3155 char_u *p_csl_save = p_csl;
3156
3157 // avoid using 'completeslash' here
3158 p_csl = empty_option;
3159#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160
3161 rettv->v_type = VAR_STRING;
3162 if (argvars[1].v_type != VAR_UNKNOWN
3163 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003164 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003166 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003168 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 if (*s == '%' || *s == '#' || *s == '<')
3170 {
3171 ++emsg_off;
3172 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3173 --emsg_off;
3174 if (rettv->v_type == VAR_LIST)
3175 {
3176 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3177 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003178 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 }
3180 else
3181 rettv->vval.v_string = result;
3182 }
3183 else
3184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003185 // When the optional second argument is non-zero, don't remove matches
3186 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003187 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003188 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003189 options |= WILD_KEEP_ALL;
3190 if (!error)
3191 {
3192 ExpandInit(&xpc);
3193 xpc.xp_context = EXPAND_FILES;
3194 if (p_wic)
3195 options += WILD_ICASE;
3196 if (rettv->v_type == VAR_STRING)
3197 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3198 options, WILD_ALL);
3199 else if (rettv_list_alloc(rettv) != FAIL)
3200 {
3201 int i;
3202
3203 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3204 for (i = 0; i < xpc.xp_numfiles; i++)
3205 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3206 ExpandCleanup(&xpc);
3207 }
3208 }
3209 else
3210 rettv->vval.v_string = NULL;
3211 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003212#ifdef BACKSLASH_IN_FILENAME
3213 p_csl = p_csl_save;
3214#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215}
3216
3217/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003218 * "expandcmd()" function
3219 * Expand all the special characters in a command string.
3220 */
3221 static void
3222f_expandcmd(typval_T *argvars, typval_T *rettv)
3223{
3224 exarg_T eap;
3225 char_u *cmdstr;
3226 char *errormsg = NULL;
3227
3228 rettv->v_type = VAR_STRING;
3229 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3230
3231 memset(&eap, 0, sizeof(eap));
3232 eap.cmd = cmdstr;
3233 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003234 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003235 eap.usefilter = FALSE;
3236 eap.nextcmd = NULL;
3237 eap.cmdidx = CMD_USER;
3238
3239 expand_filename(&eap, &cmdstr, &errormsg);
3240 if (errormsg != NULL && *errormsg != NUL)
3241 emsg(errormsg);
3242
3243 rettv->vval.v_string = cmdstr;
3244}
3245
3246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 * "feedkeys()" function
3248 */
3249 static void
3250f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3251{
3252 int remap = TRUE;
3253 int insert = FALSE;
3254 char_u *keys, *flags;
3255 char_u nbuf[NUMBUFLEN];
3256 int typed = FALSE;
3257 int execute = FALSE;
3258 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003259 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 char_u *keys_esc;
3261
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // This is not allowed in the sandbox. If the commands would still be
3263 // executed in the sandbox it would be OK, but it probably happens later,
3264 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003265 if (check_secure())
3266 return;
3267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003268 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269
3270 if (argvars[1].v_type != VAR_UNKNOWN)
3271 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003272 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 for ( ; *flags != NUL; ++flags)
3274 {
3275 switch (*flags)
3276 {
3277 case 'n': remap = FALSE; break;
3278 case 'm': remap = TRUE; break;
3279 case 't': typed = TRUE; break;
3280 case 'i': insert = TRUE; break;
3281 case 'x': execute = TRUE; break;
3282 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003283 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 }
3285 }
3286 }
3287
3288 if (*keys != NUL || execute)
3289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003290 // Need to escape K_SPECIAL and CSI before putting the string in the
3291 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 keys_esc = vim_strsave_escape_csi(keys);
3293 if (keys_esc != NULL)
3294 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003295 if (lowlevel)
3296 {
3297#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003298 int idx;
3299 int len = (int)STRLEN(keys);
3300
3301 for (idx = 0; idx < len; ++idx)
3302 {
3303 // if a CTRL-C was typed, set got_int, similar to what
3304 // happens in fill_input_buf()
3305 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3306 got_int = TRUE;
3307 add_to_input_buf(keys + idx, 1);
3308 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003309#else
3310 emsg(_("E980: lowlevel input not supported"));
3311#endif
3312 }
3313 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003314 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003315 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003317 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003318#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003319 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003320#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003321 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003322 typebuf_was_filled = TRUE;
3323 }
3324 vim_free(keys_esc);
3325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003326 if (execute)
3327 {
3328 int save_msg_scroll = msg_scroll;
3329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003331 msg_scroll = FALSE;
3332
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003333 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003334 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003335 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003336 ++in_feedkeys;
3337 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003338 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003339 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003340 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003341 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003342 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003343 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003344
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 msg_scroll |= save_msg_scroll;
3346 }
3347 }
3348 }
3349}
3350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351/*
3352 * "fnameescape({string})" function
3353 */
3354 static void
3355f_fnameescape(typval_T *argvars, typval_T *rettv)
3356{
3357 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 rettv->v_type = VAR_STRING;
3360}
3361
3362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 * "foreground()" function
3364 */
3365 static void
3366f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3367{
3368#ifdef FEAT_GUI
3369 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003370 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003372 return;
3373 }
3374#endif
3375#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003376 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377#endif
3378}
3379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003381common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382{
3383 char_u *s;
3384 char_u *name;
3385 int use_string = FALSE;
3386 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003387 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003388 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389
3390 if (argvars[0].v_type == VAR_FUNC)
3391 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003392 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 s = argvars[0].vval.v_string;
3394 }
3395 else if (argvars[0].v_type == VAR_PARTIAL
3396 && argvars[0].vval.v_partial != NULL)
3397 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003398 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003400 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 }
3402 else
3403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003404 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003405 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406 use_string = TRUE;
3407 }
3408
Bram Moolenaar843b8842016-08-21 14:36:15 +02003409 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003410 {
3411 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003412 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003413 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3414 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003415 if (*name != NUL)
3416 s = NULL;
3417 }
3418
Bram Moolenaar843b8842016-08-21 14:36:15 +02003419 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3420 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003421 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003423 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003424 ? find_func(trans_name, is_global, NULL) == NULL
3425 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003426 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 else
3428 {
3429 int dict_idx = 0;
3430 int arg_idx = 0;
3431 list_T *list = NULL;
3432
3433 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3434 {
3435 char sid_buf[25];
3436 int off = *s == 's' ? 2 : 5;
3437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // Expand s: and <SID> into <SNR>nr_, so that the function can
3439 // also be called from another script. Using trans_function_name()
3440 // would also work, but some plugins depend on the name being
3441 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003442 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003443 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 if (name != NULL)
3445 {
3446 STRCPY(name, sid_buf);
3447 STRCAT(name, s + off);
3448 }
3449 }
3450 else
3451 name = vim_strsave(s);
3452
3453 if (argvars[1].v_type != VAR_UNKNOWN)
3454 {
3455 if (argvars[2].v_type != VAR_UNKNOWN)
3456 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003457 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 arg_idx = 1;
3459 dict_idx = 2;
3460 }
3461 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 dict_idx = 1;
3464 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003465 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003466 arg_idx = 1;
3467 if (dict_idx > 0)
3468 {
3469 if (argvars[dict_idx].v_type != VAR_DICT)
3470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003471 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003473 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 }
3475 if (argvars[dict_idx].vval.v_dict == NULL)
3476 dict_idx = 0;
3477 }
3478 if (arg_idx > 0)
3479 {
3480 if (argvars[arg_idx].v_type != VAR_LIST)
3481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003482 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003484 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485 }
3486 list = argvars[arg_idx].vval.v_list;
3487 if (list == NULL || list->lv_len == 0)
3488 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003489 else if (list->lv_len > MAX_FUNC_ARGS)
3490 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003491 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003492 vim_free(name);
3493 goto theend;
3494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 }
3496 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003497 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003499 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003501 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 if (pt == NULL)
3503 vim_free(name);
3504 else
3505 {
3506 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3507 {
3508 listitem_T *li;
3509 int i = 0;
3510 int arg_len = 0;
3511 int lv_len = 0;
3512
3513 if (arg_pt != NULL)
3514 arg_len = arg_pt->pt_argc;
3515 if (list != NULL)
3516 lv_len = list->lv_len;
3517 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003518 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003519 if (pt->pt_argv == NULL)
3520 {
3521 vim_free(pt);
3522 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003523 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003524 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003525 for (i = 0; i < arg_len; i++)
3526 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3527 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003528 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003529 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003530 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003531 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 }
3534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003535 // For "function(dict.func, [], dict)" and "func" is a partial
3536 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537 if (dict_idx > 0)
3538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3541 ++pt->pt_dict->dv_refcount;
3542 }
3543 else if (arg_pt != NULL)
3544 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 // If the dict was bound automatically the result is also
3546 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 pt->pt_dict = arg_pt->pt_dict;
3548 pt->pt_auto = arg_pt->pt_auto;
3549 if (pt->pt_dict != NULL)
3550 ++pt->pt_dict->dv_refcount;
3551 }
3552
3553 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003554 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3555 {
3556 pt->pt_func = arg_pt->pt_func;
3557 func_ptr_ref(pt->pt_func);
3558 vim_free(name);
3559 }
3560 else if (is_funcref)
3561 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003562 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003563 func_ptr_ref(pt->pt_func);
3564 vim_free(name);
3565 }
3566 else
3567 {
3568 pt->pt_name = name;
3569 func_ref(name);
3570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 }
3572 rettv->v_type = VAR_PARTIAL;
3573 rettv->vval.v_partial = pt;
3574 }
3575 else
3576 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003577 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003578 rettv->v_type = VAR_FUNC;
3579 rettv->vval.v_string = name;
3580 func_ref(name);
3581 }
3582 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003583theend:
3584 vim_free(trans_name);
3585}
3586
3587/*
3588 * "funcref()" function
3589 */
3590 static void
3591f_funcref(typval_T *argvars, typval_T *rettv)
3592{
3593 common_function(argvars, rettv, TRUE);
3594}
3595
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003596 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003597ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003598{
3599 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3600 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003601 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003602}
3603
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003604/*
3605 * "function()" function
3606 */
3607 static void
3608f_function(typval_T *argvars, typval_T *rettv)
3609{
3610 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611}
3612
3613/*
3614 * "garbagecollect()" function
3615 */
3616 static void
3617f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3618{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003619 // This is postponed until we are back at the toplevel, because we may be
3620 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621 want_garbage_collect = TRUE;
3622
Bram Moolenaar2df47312020-09-05 17:30:44 +02003623 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 garbage_collect_at_exit = TRUE;
3625}
3626
3627/*
3628 * "get()" function
3629 */
3630 static void
3631f_get(typval_T *argvars, typval_T *rettv)
3632{
3633 listitem_T *li;
3634 list_T *l;
3635 dictitem_T *di;
3636 dict_T *d;
3637 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003638 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003640 if (argvars[0].v_type == VAR_BLOB)
3641 {
3642 int error = FALSE;
3643 int idx = tv_get_number_chk(&argvars[1], &error);
3644
3645 if (!error)
3646 {
3647 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003648 if (idx < 0)
3649 idx = blob_len(argvars[0].vval.v_blob) + idx;
3650 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3651 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003652 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003654 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003655 tv = rettv;
3656 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003657 }
3658 }
3659 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 {
3661 if ((l = argvars[0].vval.v_list) != NULL)
3662 {
3663 int error = FALSE;
3664
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003665 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 if (!error && li != NULL)
3667 tv = &li->li_tv;
3668 }
3669 }
3670 else if (argvars[0].v_type == VAR_DICT)
3671 {
3672 if ((d = argvars[0].vval.v_dict) != NULL)
3673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003674 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 if (di != NULL)
3676 tv = &di->di_tv;
3677 }
3678 }
3679 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3680 {
3681 partial_T *pt;
3682 partial_T fref_pt;
3683
3684 if (argvars[0].v_type == VAR_PARTIAL)
3685 pt = argvars[0].vval.v_partial;
3686 else
3687 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003688 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 fref_pt.pt_name = argvars[0].vval.v_string;
3690 pt = &fref_pt;
3691 }
3692
3693 if (pt != NULL)
3694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003695 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003696 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697
3698 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3699 {
3700 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003701 n = partial_name(pt);
3702 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703 rettv->vval.v_string = NULL;
3704 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003705 {
3706 rettv->vval.v_string = vim_strsave(n);
3707 if (rettv->v_type == VAR_FUNC)
3708 func_ref(rettv->vval.v_string);
3709 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 }
3711 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003712 {
3713 what_is_dict = TRUE;
3714 if (pt->pt_dict != NULL)
3715 rettv_dict_set(rettv, pt->pt_dict);
3716 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 else if (STRCMP(what, "args") == 0)
3718 {
3719 rettv->v_type = VAR_LIST;
3720 if (rettv_list_alloc(rettv) == OK)
3721 {
3722 int i;
3723
3724 for (i = 0; i < pt->pt_argc; ++i)
3725 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3726 }
3727 }
3728 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003729 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003730
3731 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3732 // third argument
3733 if (!what_is_dict)
3734 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 }
3736 }
3737 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003738 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
3740 if (tv == NULL)
3741 {
3742 if (argvars[2].v_type != VAR_UNKNOWN)
3743 copy_tv(&argvars[2], rettv);
3744 }
3745 else
3746 copy_tv(tv, rettv);
3747}
3748
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003749/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003750 * "getchangelist()" function
3751 */
3752 static void
3753f_getchangelist(typval_T *argvars, typval_T *rettv)
3754{
3755#ifdef FEAT_JUMPLIST
3756 buf_T *buf;
3757 int i;
3758 list_T *l;
3759 dict_T *d;
3760#endif
3761
3762 if (rettv_list_alloc(rettv) != OK)
3763 return;
3764
3765#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003766 if (argvars[0].v_type == VAR_UNKNOWN)
3767 buf = curbuf;
3768 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003769 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003770 if (buf == NULL)
3771 return;
3772
3773 l = list_alloc();
3774 if (l == NULL)
3775 return;
3776
3777 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3778 return;
3779 /*
3780 * The current window change list index tracks only the position in the
3781 * current buffer change list. For other buffers, use the change list
3782 * length as the current index.
3783 */
3784 list_append_number(rettv->vval.v_list,
3785 (varnumber_T)((buf == curwin->w_buffer)
3786 ? curwin->w_changelistidx : buf->b_changelistlen));
3787
3788 for (i = 0; i < buf->b_changelistlen; ++i)
3789 {
3790 if (buf->b_changelist[i].lnum == 0)
3791 continue;
3792 if ((d = dict_alloc()) == NULL)
3793 return;
3794 if (list_append_dict(l, d) == FAIL)
3795 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003796 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3797 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003798 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003799 }
3800#endif
3801}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003803 static void
3804getpos_both(
3805 typval_T *argvars,
3806 typval_T *rettv,
3807 int getcurpos,
3808 int charcol)
3809{
3810 pos_T *fp = NULL;
3811 pos_T pos;
3812 win_T *wp = curwin;
3813 list_T *l;
3814 int fnum = -1;
3815
3816 if (rettv_list_alloc(rettv) == OK)
3817 {
3818 l = rettv->vval.v_list;
3819 if (getcurpos)
3820 {
3821 if (argvars[0].v_type != VAR_UNKNOWN)
3822 {
3823 wp = find_win_by_nr_or_id(&argvars[0]);
3824 if (wp != NULL)
3825 fp = &wp->w_cursor;
3826 }
3827 else
3828 fp = &curwin->w_cursor;
3829 if (fp != NULL && charcol)
3830 {
3831 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003832 pos.col =
3833 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003834 fp = &pos;
3835 }
3836 }
3837 else
3838 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3839 if (fnum != -1)
3840 list_append_number(l, (varnumber_T)fnum);
3841 else
3842 list_append_number(l, (varnumber_T)0);
3843 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3844 : (varnumber_T)0);
3845 list_append_number(l, (fp != NULL)
3846 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3847 : (varnumber_T)0);
3848 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3849 (varnumber_T)0);
3850 if (getcurpos)
3851 {
3852 int save_set_curswant = curwin->w_set_curswant;
3853 colnr_T save_curswant = curwin->w_curswant;
3854 colnr_T save_virtcol = curwin->w_virtcol;
3855
3856 if (wp == curwin)
3857 update_curswant();
3858 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3859 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3860
3861 // Do not change "curswant", as it is unexpected that a get
3862 // function has a side effect.
3863 if (wp == curwin && save_set_curswant)
3864 {
3865 curwin->w_set_curswant = save_set_curswant;
3866 curwin->w_curswant = save_curswant;
3867 curwin->w_virtcol = save_virtcol;
3868 curwin->w_valid &= ~VALID_VIRTCOL;
3869 }
3870 }
3871 }
3872 else
3873 rettv->vval.v_number = FALSE;
3874}
3875
3876/*
3877 * "getcharpos()" function
3878 */
3879 static void
3880f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3881{
3882 getpos_both(argvars, rettv, FALSE, TRUE);
3883}
3884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885/*
3886 * "getcharsearch()" function
3887 */
3888 static void
3889f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3890{
3891 if (rettv_dict_alloc(rettv) != FAIL)
3892 {
3893 dict_T *dict = rettv->vval.v_dict;
3894
Bram Moolenaare0be1672018-07-08 16:50:37 +02003895 dict_add_string(dict, "char", last_csearch());
3896 dict_add_number(dict, "forward", last_csearch_forward());
3897 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 }
3899}
3900
3901/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003902 * "getenv()" function
3903 */
3904 static void
3905f_getenv(typval_T *argvars, typval_T *rettv)
3906{
3907 int mustfree = FALSE;
3908 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3909
3910 if (p == NULL)
3911 {
3912 rettv->v_type = VAR_SPECIAL;
3913 rettv->vval.v_number = VVAL_NULL;
3914 return;
3915 }
3916 if (!mustfree)
3917 p = vim_strsave(p);
3918 rettv->vval.v_string = p;
3919 rettv->v_type = VAR_STRING;
3920}
3921
3922/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003923 * "getfontname()" function
3924 */
3925 static void
3926f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3927{
3928 rettv->v_type = VAR_STRING;
3929 rettv->vval.v_string = NULL;
3930#ifdef FEAT_GUI
3931 if (gui.in_use)
3932 {
3933 GuiFont font;
3934 char_u *name = NULL;
3935
3936 if (argvars[0].v_type == VAR_UNKNOWN)
3937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003938 // Get the "Normal" font. Either the name saved by
3939 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 font = gui.norm_font;
3941 name = hl_get_font_name();
3942 }
3943 else
3944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003946 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 return;
3948 font = gui_mch_get_font(name, FALSE);
3949 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003950 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 }
3952 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3953 if (argvars[0].v_type != VAR_UNKNOWN)
3954 gui_mch_free_font(font);
3955 }
3956#endif
3957}
3958
3959/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003960 * "getjumplist()" function
3961 */
3962 static void
3963f_getjumplist(typval_T *argvars, typval_T *rettv)
3964{
3965#ifdef FEAT_JUMPLIST
3966 win_T *wp;
3967 int i;
3968 list_T *l;
3969 dict_T *d;
3970#endif
3971
3972 if (rettv_list_alloc(rettv) != OK)
3973 return;
3974
3975#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003976 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003977 if (wp == NULL)
3978 return;
3979
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003980 cleanup_jumplist(wp, TRUE);
3981
Bram Moolenaar4f505882018-02-10 21:06:32 +01003982 l = list_alloc();
3983 if (l == NULL)
3984 return;
3985
3986 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3987 return;
3988 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3989
3990 for (i = 0; i < wp->w_jumplistlen; ++i)
3991 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003992 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3993 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003994 if ((d = dict_alloc()) == NULL)
3995 return;
3996 if (list_append_dict(l, d) == FAIL)
3997 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003998 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3999 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004000 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004001 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004002 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004003 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004004 }
4005#endif
4006}
4007
4008/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 * "getpid()" function
4010 */
4011 static void
4012f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4013{
4014 rettv->vval.v_number = mch_get_pid();
4015}
4016
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017/*
4018 * "getcurpos()" function
4019 */
4020 static void
4021f_getcurpos(typval_T *argvars, typval_T *rettv)
4022{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004023 getpos_both(argvars, rettv, TRUE, FALSE);
4024}
4025
4026 static void
4027f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4028{
4029 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004030}
4031
4032/*
4033 * "getpos(string)" function
4034 */
4035 static void
4036f_getpos(typval_T *argvars, typval_T *rettv)
4037{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004038 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039}
4040
4041/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042 * "getreg()" function
4043 */
4044 static void
4045f_getreg(typval_T *argvars, typval_T *rettv)
4046{
4047 char_u *strregname;
4048 int regname;
4049 int arg2 = FALSE;
4050 int return_list = FALSE;
4051 int error = FALSE;
4052
4053 if (argvars[0].v_type != VAR_UNKNOWN)
4054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004055 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004056 if (strregname == NULL)
4057 error = TRUE;
4058 else if (in_vim9script() && STRLEN(strregname) > 1)
4059 {
4060 semsg(_(e_register_name_must_be_one_char_str), strregname);
4061 error = TRUE;
4062 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (argvars[1].v_type != VAR_UNKNOWN)
4064 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004065 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004067 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004068 }
4069 }
4070 else
4071 strregname = get_vim_var_str(VV_REG);
4072
4073 if (error)
4074 return;
4075
4076 regname = (strregname == NULL ? '"' : *strregname);
4077 if (regname == 0)
4078 regname = '"';
4079
4080 if (return_list)
4081 {
4082 rettv->v_type = VAR_LIST;
4083 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4084 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4085 if (rettv->vval.v_list == NULL)
4086 (void)rettv_list_alloc(rettv);
4087 else
4088 ++rettv->vval.v_list->lv_refcount;
4089 }
4090 else
4091 {
4092 rettv->v_type = VAR_STRING;
4093 rettv->vval.v_string = get_reg_contents(regname,
4094 arg2 ? GREG_EXPR_SRC : 0);
4095 }
4096}
4097
4098/*
4099 * "getregtype()" function
4100 */
4101 static void
4102f_getregtype(typval_T *argvars, typval_T *rettv)
4103{
4104 char_u *strregname;
4105 int regname;
4106 char_u buf[NUMBUFLEN + 2];
4107 long reglen = 0;
4108
4109 if (argvars[0].v_type != VAR_UNKNOWN)
4110 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004111 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004112 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4113 {
4114 semsg(_(e_register_name_must_be_one_char_str), strregname);
4115 strregname = NULL;
4116 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004117 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118 {
4119 rettv->v_type = VAR_STRING;
4120 rettv->vval.v_string = NULL;
4121 return;
4122 }
4123 }
4124 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004125 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004126 strregname = get_vim_var_str(VV_REG);
4127
4128 regname = (strregname == NULL ? '"' : *strregname);
4129 if (regname == 0)
4130 regname = '"';
4131
4132 buf[0] = NUL;
4133 buf[1] = NUL;
4134 switch (get_reg_type(regname, &reglen))
4135 {
4136 case MLINE: buf[0] = 'V'; break;
4137 case MCHAR: buf[0] = 'v'; break;
4138 case MBLOCK:
4139 buf[0] = Ctrl_V;
4140 sprintf((char *)buf + 1, "%ld", reglen + 1);
4141 break;
4142 }
4143 rettv->v_type = VAR_STRING;
4144 rettv->vval.v_string = vim_strsave(buf);
4145}
4146
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004147/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004148 * "gettagstack()" function
4149 */
4150 static void
4151f_gettagstack(typval_T *argvars, typval_T *rettv)
4152{
4153 win_T *wp = curwin; // default is current window
4154
4155 if (rettv_dict_alloc(rettv) != OK)
4156 return;
4157
4158 if (argvars[0].v_type != VAR_UNKNOWN)
4159 {
4160 wp = find_win_by_nr_or_id(&argvars[0]);
4161 if (wp == NULL)
4162 return;
4163 }
4164
4165 get_tagstack(wp, rettv->vval.v_dict);
4166}
4167
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004168/*
4169 * "gettext()" function
4170 */
4171 static void
4172f_gettext(typval_T *argvars, typval_T *rettv)
4173{
4174 if (argvars[0].v_type != VAR_STRING
4175 || argvars[0].vval.v_string == NULL
4176 || *argvars[0].vval.v_string == NUL)
4177 {
4178 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4179 }
4180 else
4181 {
4182 rettv->v_type = VAR_STRING;
4183 rettv->vval.v_string = vim_strsave(
4184 (char_u *)_(argvars[0].vval.v_string));
4185 }
4186}
4187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004188// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189#include "version.h"
4190
4191/*
4192 * "has()" function
4193 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004194 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004195f_has(typval_T *argvars, typval_T *rettv)
4196{
4197 int i;
4198 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004199 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004200 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004201 typedef struct {
4202 char *name;
4203 short present;
4204 } has_item_T;
4205 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004206 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004207 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004209 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004210#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004211 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004212#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004213 },
4214 {"arp",
4215#if defined(AMIGA) && defined(FEAT_ARP)
4216 1
4217#else
4218 0
4219#endif
4220 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004221 {"haiku",
4222#ifdef __HAIKU__
4223 1
4224#else
4225 0
4226#endif
4227 },
4228 {"bsd",
4229#if defined(BSD) && !defined(MACOS_X)
4230 1
4231#else
4232 0
4233#endif
4234 },
4235 {"hpux",
4236#ifdef hpux
4237 1
4238#else
4239 0
4240#endif
4241 },
4242 {"linux",
4243#ifdef __linux__
4244 1
4245#else
4246 0
4247#endif
4248 },
4249 {"mac", // Mac OS X (and, once, Mac OS Classic)
4250#ifdef MACOS_X
4251 1
4252#else
4253 0
4254#endif
4255 },
4256 {"osx", // Mac OS X
4257#ifdef MACOS_X
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"macunix", // Mac OS X, with the darwin feature
4264#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"osxdarwin", // synonym for macunix
4271#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"qnx",
4278#ifdef __QNX__
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"sun",
4285#ifdef SUN_SYSTEM
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004293 1
4294#else
4295 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004297 },
4298 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004300 1
4301#else
4302 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004304 },
4305 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004306#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004307 1
4308#else
4309 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004311 },
4312 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004313#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004314 1
4315#else
4316 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004318 },
4319 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004320#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004321 1
4322#else
4323 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004325 },
4326 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004328 1
4329#else
4330 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004332 },
4333 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004335 1
4336#else
4337 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004339 },
4340 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004342 1
4343#else
4344 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004346 },
4347 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004349 1
4350#else
4351 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004353 },
4354 {"autocmd", 1},
4355 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004356#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004357 1
4358#else
4359 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004360#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004361 },
4362 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004363#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004364 1
4365#else
4366 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004367#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004368 },
4369 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004370#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004371 1
4372#else
4373 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004375 },
4376 {"balloon_multiline",
4377#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4378 // MS-Windows requires runtime check, see below
4379 1
4380#else
4381 0
4382#endif
4383 },
4384 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004385#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004386 1
4387#else
4388 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004389#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004390 },
4391 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004393 1
4394#else
4395 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004397 },
4398 {"all_builtin_terms",
4399#if defined(ALL_BUILTIN_TCAPS)
4400 1
4401#else
4402 0
4403#endif
4404 },
4405 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004407 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004409 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004411 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004413 },
4414 {"byte_offset",
4415#ifdef FEAT_BYTEOFF
4416 1
4417#else
4418 0
4419#endif
4420 },
4421 {"channel",
4422#ifdef FEAT_JOB_CHANNEL
4423 1
4424#else
4425 0
4426#endif
4427 },
4428 {"cindent",
4429#ifdef FEAT_CINDENT
4430 1
4431#else
4432 0
4433#endif
4434 },
4435 {"clientserver",
4436#ifdef FEAT_CLIENTSERVER
4437 1
4438#else
4439 0
4440#endif
4441 },
4442 {"clipboard",
4443#ifdef FEAT_CLIPBOARD
4444 1
4445#else
4446 0
4447#endif
4448 },
4449 {"cmdline_compl", 1},
4450 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004451 {"cmdwin",
4452#ifdef FEAT_CMDWIN
4453 1
4454#else
4455 0
4456#endif
4457 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004458 {"comments", 1},
4459 {"conceal",
4460#ifdef FEAT_CONCEAL
4461 1
4462#else
4463 0
4464#endif
4465 },
4466 {"cryptv",
4467#ifdef FEAT_CRYPT
4468 1
4469#else
4470 0
4471#endif
4472 },
4473 {"crypt-blowfish",
4474#ifdef FEAT_CRYPT
4475 1
4476#else
4477 0
4478#endif
4479 },
4480 {"crypt-blowfish2",
4481#ifdef FEAT_CRYPT
4482 1
4483#else
4484 0
4485#endif
4486 },
4487 {"cscope",
4488#ifdef FEAT_CSCOPE
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"cursorbind", 1},
4495 {"cursorshape",
4496#ifdef CURSOR_SHAPE
4497 1
4498#else
4499 0
4500#endif
4501 },
4502 {"debug",
4503#ifdef DEBUG
4504 1
4505#else
4506 0
4507#endif
4508 },
4509 {"dialog_con",
4510#ifdef FEAT_CON_DIALOG
4511 1
4512#else
4513 0
4514#endif
4515 },
4516 {"dialog_gui",
4517#ifdef FEAT_GUI_DIALOG
4518 1
4519#else
4520 0
4521#endif
4522 },
4523 {"diff",
4524#ifdef FEAT_DIFF
4525 1
4526#else
4527 0
4528#endif
4529 },
4530 {"digraphs",
4531#ifdef FEAT_DIGRAPHS
4532 1
4533#else
4534 0
4535#endif
4536 },
4537 {"directx",
4538#ifdef FEAT_DIRECTX
4539 1
4540#else
4541 0
4542#endif
4543 },
4544 {"dnd",
4545#ifdef FEAT_DND
4546 1
4547#else
4548 0
4549#endif
4550 },
4551 {"emacs_tags",
4552#ifdef FEAT_EMACS_TAGS
4553 1
4554#else
4555 0
4556#endif
4557 },
4558 {"eval", 1}, // always present, of course!
4559 {"ex_extra", 1}, // graduated feature
4560 {"extra_search",
4561#ifdef FEAT_SEARCH_EXTRA
4562 1
4563#else
4564 0
4565#endif
4566 },
4567 {"file_in_path",
4568#ifdef FEAT_SEARCHPATH
4569 1
4570#else
4571 0
4572#endif
4573 },
4574 {"filterpipe",
4575#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4576 1
4577#else
4578 0
4579#endif
4580 },
4581 {"find_in_path",
4582#ifdef FEAT_FIND_ID
4583 1
4584#else
4585 0
4586#endif
4587 },
4588 {"float",
4589#ifdef FEAT_FLOAT
4590 1
4591#else
4592 0
4593#endif
4594 },
4595 {"folding",
4596#ifdef FEAT_FOLDING
4597 1
4598#else
4599 0
4600#endif
4601 },
4602 {"footer",
4603#ifdef FEAT_FOOTER
4604 1
4605#else
4606 0
4607#endif
4608 },
4609 {"fork",
4610#if !defined(USE_SYSTEM) && defined(UNIX)
4611 1
4612#else
4613 0
4614#endif
4615 },
4616 {"gettext",
4617#ifdef FEAT_GETTEXT
4618 1
4619#else
4620 0
4621#endif
4622 },
4623 {"gui",
4624#ifdef FEAT_GUI
4625 1
4626#else
4627 0
4628#endif
4629 },
4630 {"gui_neXtaw",
4631#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4632 1
4633#else
4634 0
4635#endif
4636 },
4637 {"gui_athena",
4638#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4639 1
4640#else
4641 0
4642#endif
4643 },
4644 {"gui_gtk",
4645#ifdef FEAT_GUI_GTK
4646 1
4647#else
4648 0
4649#endif
4650 },
4651 {"gui_gtk2",
4652#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4653 1
4654#else
4655 0
4656#endif
4657 },
4658 {"gui_gtk3",
4659#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4660 1
4661#else
4662 0
4663#endif
4664 },
4665 {"gui_gnome",
4666#ifdef FEAT_GUI_GNOME
4667 1
4668#else
4669 0
4670#endif
4671 },
4672 {"gui_haiku",
4673#ifdef FEAT_GUI_HAIKU
4674 1
4675#else
4676 0
4677#endif
4678 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004679 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004680 {"gui_motif",
4681#ifdef FEAT_GUI_MOTIF
4682 1
4683#else
4684 0
4685#endif
4686 },
4687 {"gui_photon",
4688#ifdef FEAT_GUI_PHOTON
4689 1
4690#else
4691 0
4692#endif
4693 },
4694 {"gui_win32",
4695#ifdef FEAT_GUI_MSWIN
4696 1
4697#else
4698 0
4699#endif
4700 },
4701 {"iconv",
4702#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4703 1
4704#else
4705 0
4706#endif
4707 },
4708 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004709 {"ipv6",
4710#ifdef FEAT_IPV6
4711 1
4712#else
4713 0
4714#endif
4715 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004716 {"job",
4717#ifdef FEAT_JOB_CHANNEL
4718 1
4719#else
4720 0
4721#endif
4722 },
4723 {"jumplist",
4724#ifdef FEAT_JUMPLIST
4725 1
4726#else
4727 0
4728#endif
4729 },
4730 {"keymap",
4731#ifdef FEAT_KEYMAP
4732 1
4733#else
4734 0
4735#endif
4736 },
4737 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4738 {"langmap",
4739#ifdef FEAT_LANGMAP
4740 1
4741#else
4742 0
4743#endif
4744 },
4745 {"libcall",
4746#ifdef FEAT_LIBCALL
4747 1
4748#else
4749 0
4750#endif
4751 },
4752 {"linebreak",
4753#ifdef FEAT_LINEBREAK
4754 1
4755#else
4756 0
4757#endif
4758 },
4759 {"lispindent",
4760#ifdef FEAT_LISP
4761 1
4762#else
4763 0
4764#endif
4765 },
4766 {"listcmds", 1},
4767 {"localmap", 1},
4768 {"lua",
4769#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4770 1
4771#else
4772 0
4773#endif
4774 },
4775 {"menu",
4776#ifdef FEAT_MENU
4777 1
4778#else
4779 0
4780#endif
4781 },
4782 {"mksession",
4783#ifdef FEAT_SESSION
4784 1
4785#else
4786 0
4787#endif
4788 },
4789 {"modify_fname", 1},
4790 {"mouse", 1},
4791 {"mouseshape",
4792#ifdef FEAT_MOUSESHAPE
4793 1
4794#else
4795 0
4796#endif
4797 },
4798 {"mouse_dec",
4799#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"mouse_gpm",
4806#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"mouse_jsbterm",
4813#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"mouse_netterm",
4820#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"mouse_pterm",
4827#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"mouse_sgr",
4834#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"mouse_sysmouse",
4841#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"mouse_urxvt",
4848#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"mouse_xterm",
4855#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"multi_byte", 1},
4862 {"multi_byte_ime",
4863#ifdef FEAT_MBYTE_IME
4864 1
4865#else
4866 0
4867#endif
4868 },
4869 {"multi_lang",
4870#ifdef FEAT_MULTI_LANG
4871 1
4872#else
4873 0
4874#endif
4875 },
4876 {"mzscheme",
4877#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4878 1
4879#else
4880 0
4881#endif
4882 },
4883 {"num64", 1},
4884 {"ole",
4885#ifdef FEAT_OLE
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"packages",
4892#ifdef FEAT_EVAL
4893 1
4894#else
4895 0
4896#endif
4897 },
4898 {"path_extra",
4899#ifdef FEAT_PATH_EXTRA
4900 1
4901#else
4902 0
4903#endif
4904 },
4905 {"perl",
4906#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4907 1
4908#else
4909 0
4910#endif
4911 },
4912 {"persistent_undo",
4913#ifdef FEAT_PERSISTENT_UNDO
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"python_compiled",
4920#if defined(FEAT_PYTHON)
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"python_dynamic",
4927#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"python",
4934#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"pythonx",
4941#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4942 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4943 1
4944#else
4945 0
4946#endif
4947 },
4948 {"python3_compiled",
4949#if defined(FEAT_PYTHON3)
4950 1
4951#else
4952 0
4953#endif
4954 },
4955 {"python3_dynamic",
4956#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4957 1
4958#else
4959 0
4960#endif
4961 },
4962 {"python3",
4963#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4964 1
4965#else
4966 0
4967#endif
4968 },
4969 {"popupwin",
4970#ifdef FEAT_PROP_POPUP
4971 1
4972#else
4973 0
4974#endif
4975 },
4976 {"postscript",
4977#ifdef FEAT_POSTSCRIPT
4978 1
4979#else
4980 0
4981#endif
4982 },
4983 {"printer",
4984#ifdef FEAT_PRINTER
4985 1
4986#else
4987 0
4988#endif
4989 },
4990 {"profile",
4991#ifdef FEAT_PROFILE
4992 1
4993#else
4994 0
4995#endif
4996 },
4997 {"reltime",
4998#ifdef FEAT_RELTIME
4999 1
5000#else
5001 0
5002#endif
5003 },
5004 {"quickfix",
5005#ifdef FEAT_QUICKFIX
5006 1
5007#else
5008 0
5009#endif
5010 },
5011 {"rightleft",
5012#ifdef FEAT_RIGHTLEFT
5013 1
5014#else
5015 0
5016#endif
5017 },
5018 {"ruby",
5019#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5020 1
5021#else
5022 0
5023#endif
5024 },
5025 {"scrollbind", 1},
5026 {"showcmd",
5027#ifdef FEAT_CMDL_INFO
5028 1
5029#else
5030 0
5031#endif
5032 },
5033 {"cmdline_info",
5034#ifdef FEAT_CMDL_INFO
5035 1
5036#else
5037 0
5038#endif
5039 },
5040 {"signs",
5041#ifdef FEAT_SIGNS
5042 1
5043#else
5044 0
5045#endif
5046 },
5047 {"smartindent",
5048#ifdef FEAT_SMARTINDENT
5049 1
5050#else
5051 0
5052#endif
5053 },
5054 {"startuptime",
5055#ifdef STARTUPTIME
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"statusline",
5062#ifdef FEAT_STL_OPT
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"netbeans_intg",
5069#ifdef FEAT_NETBEANS_INTG
5070 1
5071#else
5072 0
5073#endif
5074 },
5075 {"sound",
5076#ifdef FEAT_SOUND
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"spell",
5083#ifdef FEAT_SPELL
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"syntax",
5090#ifdef FEAT_SYN_HL
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"system",
5097#if defined(USE_SYSTEM) || !defined(UNIX)
5098 1
5099#else
5100 0
5101#endif
5102 },
5103 {"tag_binary",
5104#ifdef FEAT_TAG_BINS
5105 1
5106#else
5107 0
5108#endif
5109 },
5110 {"tcl",
5111#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"termguicolors",
5118#ifdef FEAT_TERMGUICOLORS
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"terminal",
5125#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"terminfo",
5132#ifdef TERMINFO
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"termresponse",
5139#ifdef FEAT_TERMRESPONSE
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"textobjects",
5146#ifdef FEAT_TEXTOBJ
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"textprop",
5153#ifdef FEAT_PROP_POPUP
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"tgetent",
5160#ifdef HAVE_TGETENT
5161 1
5162#else
5163 0
5164#endif
5165 },
5166 {"timers",
5167#ifdef FEAT_TIMERS
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"title",
5174#ifdef FEAT_TITLE
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"toolbar",
5181#ifdef FEAT_TOOLBAR
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"unnamedplus",
5188#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"user-commands", 1}, // was accidentally included in 5.4
5195 {"user_commands", 1},
5196 {"vartabs",
5197#ifdef FEAT_VARTABS
5198 1
5199#else
5200 0
5201#endif
5202 },
5203 {"vertsplit", 1},
5204 {"viminfo",
5205#ifdef FEAT_VIMINFO
5206 1
5207#else
5208 0
5209#endif
5210 },
5211 {"vimscript-1", 1},
5212 {"vimscript-2", 1},
5213 {"vimscript-3", 1},
5214 {"vimscript-4", 1},
5215 {"virtualedit", 1},
5216 {"visual", 1},
5217 {"visualextra", 1},
5218 {"vreplace", 1},
5219 {"vtp",
5220#ifdef FEAT_VTP
5221 1
5222#else
5223 0
5224#endif
5225 },
5226 {"wildignore",
5227#ifdef FEAT_WILDIGN
5228 1
5229#else
5230 0
5231#endif
5232 },
5233 {"wildmenu",
5234#ifdef FEAT_WILDMENU
5235 1
5236#else
5237 0
5238#endif
5239 },
5240 {"windows", 1},
5241 {"winaltkeys",
5242#ifdef FEAT_WAK
5243 1
5244#else
5245 0
5246#endif
5247 },
5248 {"writebackup",
5249#ifdef FEAT_WRITEBACKUP
5250 1
5251#else
5252 0
5253#endif
5254 },
5255 {"xim",
5256#ifdef FEAT_XIM
5257 1
5258#else
5259 0
5260#endif
5261 },
5262 {"xfontset",
5263#ifdef FEAT_XFONTSET
5264 1
5265#else
5266 0
5267#endif
5268 },
5269 {"xpm",
5270#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5271 1
5272#else
5273 0
5274#endif
5275 },
5276 {"xpm_w32", // for backward compatibility
5277#ifdef FEAT_XPM_W32
5278 1
5279#else
5280 0
5281#endif
5282 },
5283 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005284#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005285 1
5286#else
5287 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005289 },
5290 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005292 1
5293#else
5294 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005296 },
5297 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005299 1
5300#else
5301 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005303 },
5304 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005306 1
5307#else
5308 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005310 },
5311 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005313 1
5314#else
5315 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005317 },
5318 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319 };
5320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005321 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005322 for (i = 0; has_list[i].name != NULL; ++i)
5323 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005325 x = TRUE;
5326 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 break;
5328 }
5329
Bram Moolenaar79296512020-03-22 16:17:14 +01005330 // features also in has_list[] but sometimes enabled at runtime
5331 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005333 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005334 {
5335 // intentionally empty
5336 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005337#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338 else if (STRICMP(name, "balloon_multiline") == 0)
5339 n = multiline_balloon_available();
5340#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005341#ifdef VIMDLL
5342 else if (STRICMP(name, "filterpipe") == 0)
5343 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344#endif
5345#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5346 else if (STRICMP(name, "iconv") == 0)
5347 n = iconv_enabled(FALSE);
5348#endif
5349#ifdef DYNAMIC_LUA
5350 else if (STRICMP(name, "lua") == 0)
5351 n = lua_enabled(FALSE);
5352#endif
5353#ifdef DYNAMIC_MZSCHEME
5354 else if (STRICMP(name, "mzscheme") == 0)
5355 n = mzscheme_enabled(FALSE);
5356#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005357#ifdef DYNAMIC_PERL
5358 else if (STRICMP(name, "perl") == 0)
5359 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005360#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005361#ifdef DYNAMIC_PYTHON
5362 else if (STRICMP(name, "python") == 0)
5363 n = python_enabled(FALSE);
5364#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365#ifdef DYNAMIC_PYTHON3
5366 else if (STRICMP(name, "python3") == 0)
5367 n = python3_enabled(FALSE);
5368#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005369#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5370 else if (STRICMP(name, "pythonx") == 0)
5371 {
5372# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5373 if (p_pyx == 0)
5374 n = python3_enabled(FALSE) || python_enabled(FALSE);
5375 else if (p_pyx == 3)
5376 n = python3_enabled(FALSE);
5377 else if (p_pyx == 2)
5378 n = python_enabled(FALSE);
5379# elif defined(DYNAMIC_PYTHON)
5380 n = python_enabled(FALSE);
5381# elif defined(DYNAMIC_PYTHON3)
5382 n = python3_enabled(FALSE);
5383# endif
5384 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005386#ifdef DYNAMIC_RUBY
5387 else if (STRICMP(name, "ruby") == 0)
5388 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005389#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005390#ifdef DYNAMIC_TCL
5391 else if (STRICMP(name, "tcl") == 0)
5392 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005393#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005394#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005395 else if (STRICMP(name, "terminal") == 0)
5396 n = terminal_enabled();
5397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005398 }
5399
Bram Moolenaar79296512020-03-22 16:17:14 +01005400 // features not in has_list[]
5401 if (x == FALSE)
5402 {
5403 if (STRNICMP(name, "patch", 5) == 0)
5404 {
5405 x = TRUE;
5406 if (name[5] == '-'
5407 && STRLEN(name) >= 11
5408 && vim_isdigit(name[6])
5409 && vim_isdigit(name[8])
5410 && vim_isdigit(name[10]))
5411 {
5412 int major = atoi((char *)name + 6);
5413 int minor = atoi((char *)name + 8);
5414
5415 // Expect "patch-9.9.01234".
5416 n = (major < VIM_VERSION_MAJOR
5417 || (major == VIM_VERSION_MAJOR
5418 && (minor < VIM_VERSION_MINOR
5419 || (minor == VIM_VERSION_MINOR
5420 && has_patch(atoi((char *)name + 10))))));
5421 }
5422 else
5423 n = has_patch(atoi((char *)name + 5));
5424 }
5425 else if (STRICMP(name, "vim_starting") == 0)
5426 {
5427 x = TRUE;
5428 n = (starting != 0);
5429 }
5430 else if (STRICMP(name, "ttyin") == 0)
5431 {
5432 x = TRUE;
5433 n = mch_input_isatty();
5434 }
5435 else if (STRICMP(name, "ttyout") == 0)
5436 {
5437 x = TRUE;
5438 n = stdout_isatty;
5439 }
5440 else if (STRICMP(name, "multi_byte_encoding") == 0)
5441 {
5442 x = TRUE;
5443 n = has_mbyte;
5444 }
5445 else if (STRICMP(name, "gui_running") == 0)
5446 {
5447 x = TRUE;
5448#ifdef FEAT_GUI
5449 n = (gui.in_use || gui.starting);
5450#endif
5451 }
5452 else if (STRICMP(name, "browse") == 0)
5453 {
5454 x = TRUE;
5455#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5456 n = gui.in_use; // gui_mch_browse() works when GUI is running
5457#endif
5458 }
5459 else if (STRICMP(name, "syntax_items") == 0)
5460 {
5461 x = TRUE;
5462#ifdef FEAT_SYN_HL
5463 n = syntax_present(curwin);
5464#endif
5465 }
5466 else if (STRICMP(name, "vcon") == 0)
5467 {
5468 x = TRUE;
5469#ifdef FEAT_VTP
5470 n = is_term_win32() && has_vtp_working();
5471#endif
5472 }
5473 else if (STRICMP(name, "netbeans_enabled") == 0)
5474 {
5475 x = TRUE;
5476#ifdef FEAT_NETBEANS_INTG
5477 n = netbeans_active();
5478#endif
5479 }
5480 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5481 {
5482 x = TRUE;
5483#ifdef FEAT_MOUSE_GPM
5484 n = gpm_enabled();
5485#endif
5486 }
5487 else if (STRICMP(name, "conpty") == 0)
5488 {
5489 x = TRUE;
5490#if defined(FEAT_TERMINAL) && defined(MSWIN)
5491 n = use_conpty();
5492#endif
5493 }
5494 else if (STRICMP(name, "clipboard_working") == 0)
5495 {
5496 x = TRUE;
5497#ifdef FEAT_CLIPBOARD
5498 n = clip_star.available;
5499#endif
5500 }
5501 }
5502
Bram Moolenaar04637e22020-09-05 18:45:29 +02005503 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005504 // return whether feature could ever be enabled
5505 rettv->vval.v_number = x;
5506 else
5507 // return whether feature is enabled
5508 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509}
5510
5511/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005512 * Return TRUE if "feature" can change later.
5513 * Also when checking for the feature has side effects, such as loading a DLL.
5514 */
5515 int
5516dynamic_feature(char_u *feature)
5517{
5518 return (feature == NULL
5519#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5520 || STRICMP(feature, "balloon_multiline") == 0
5521#endif
5522#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5523 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5524#endif
5525#ifdef VIMDLL
5526 || STRICMP(feature, "filterpipe") == 0
5527#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005528#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005529 // this can only change on Unix where the ":gui" command could be
5530 // used.
5531 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5532#endif
5533#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5534 || STRICMP(feature, "iconv") == 0
5535#endif
5536#ifdef DYNAMIC_LUA
5537 || STRICMP(feature, "lua") == 0
5538#endif
5539#ifdef FEAT_MOUSE_GPM
5540 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5541#endif
5542#ifdef DYNAMIC_MZSCHEME
5543 || STRICMP(feature, "mzscheme") == 0
5544#endif
5545#ifdef FEAT_NETBEANS_INTG
5546 || STRICMP(feature, "netbeans_enabled") == 0
5547#endif
5548#ifdef DYNAMIC_PERL
5549 || STRICMP(feature, "perl") == 0
5550#endif
5551#ifdef DYNAMIC_PYTHON
5552 || STRICMP(feature, "python") == 0
5553#endif
5554#ifdef DYNAMIC_PYTHON3
5555 || STRICMP(feature, "python3") == 0
5556#endif
5557#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5558 || STRICMP(feature, "pythonx") == 0
5559#endif
5560#ifdef DYNAMIC_RUBY
5561 || STRICMP(feature, "ruby") == 0
5562#endif
5563#ifdef FEAT_SYN_HL
5564 || STRICMP(feature, "syntax_items") == 0
5565#endif
5566#ifdef DYNAMIC_TCL
5567 || STRICMP(feature, "tcl") == 0
5568#endif
5569 // once "starting" is zero it will stay that way
5570 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5571 || STRICMP(feature, "multi_byte_encoding") == 0
5572#if defined(FEAT_TERMINAL) && defined(MSWIN)
5573 || STRICMP(feature, "conpty") == 0
5574#endif
5575 );
5576}
5577
5578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005579 * "haslocaldir()" function
5580 */
5581 static void
5582f_haslocaldir(typval_T *argvars, typval_T *rettv)
5583{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005584 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 win_T *wp = NULL;
5586
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005587 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5588
5589 // Check for window-local and tab-local directories
5590 if (wp != NULL && wp->w_localdir != NULL)
5591 rettv->vval.v_number = 1;
5592 else if (tp != NULL && tp->tp_localdir != NULL)
5593 rettv->vval.v_number = 2;
5594 else
5595 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596}
5597
5598/*
5599 * "hasmapto()" function
5600 */
5601 static void
5602f_hasmapto(typval_T *argvars, typval_T *rettv)
5603{
5604 char_u *name;
5605 char_u *mode;
5606 char_u buf[NUMBUFLEN];
5607 int abbr = FALSE;
5608
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005609 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005610 if (argvars[1].v_type == VAR_UNKNOWN)
5611 mode = (char_u *)"nvo";
5612 else
5613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005614 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005616 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617 }
5618
5619 if (map_to_exists(name, mode, abbr))
5620 rettv->vval.v_number = TRUE;
5621 else
5622 rettv->vval.v_number = FALSE;
5623}
5624
5625/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005626 * "highlightID(name)" function
5627 */
5628 static void
5629f_hlID(typval_T *argvars, typval_T *rettv)
5630{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005631 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632}
5633
5634/*
5635 * "highlight_exists()" function
5636 */
5637 static void
5638f_hlexists(typval_T *argvars, typval_T *rettv)
5639{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005640 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641}
5642
5643/*
5644 * "hostname()" function
5645 */
5646 static void
5647f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5648{
5649 char_u hostname[256];
5650
5651 mch_get_host_name(hostname, 256);
5652 rettv->v_type = VAR_STRING;
5653 rettv->vval.v_string = vim_strsave(hostname);
5654}
5655
5656/*
5657 * iconv() function
5658 */
5659 static void
5660f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5661{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662 char_u buf1[NUMBUFLEN];
5663 char_u buf2[NUMBUFLEN];
5664 char_u *from, *to, *str;
5665 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666
5667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = NULL;
5669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005670 str = tv_get_string(&argvars[0]);
5671 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5672 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673 vimconv.vc_type = CONV_NONE;
5674 convert_setup(&vimconv, from, to);
5675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005676 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005677 if (vimconv.vc_type == CONV_NONE)
5678 rettv->vval.v_string = vim_strsave(str);
5679 else
5680 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5681
5682 convert_setup(&vimconv, NULL, NULL);
5683 vim_free(from);
5684 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685}
5686
5687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 * "index()" function
5689 */
5690 static void
5691f_index(typval_T *argvars, typval_T *rettv)
5692{
5693 list_T *l;
5694 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005695 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 long idx = 0;
5697 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005698 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699
5700 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005701 if (argvars[0].v_type == VAR_BLOB)
5702 {
5703 typval_T tv;
5704 int start = 0;
5705
5706 if (argvars[2].v_type != VAR_UNKNOWN)
5707 {
5708 start = tv_get_number_chk(&argvars[2], &error);
5709 if (error)
5710 return;
5711 }
5712 b = argvars[0].vval.v_blob;
5713 if (b == NULL)
5714 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005715 if (start < 0)
5716 {
5717 start = blob_len(b) + start;
5718 if (start < 0)
5719 start = 0;
5720 }
5721
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005722 for (idx = start; idx < blob_len(b); ++idx)
5723 {
5724 tv.v_type = VAR_NUMBER;
5725 tv.vval.v_number = blob_get(b, idx);
5726 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5727 {
5728 rettv->vval.v_number = idx;
5729 return;
5730 }
5731 }
5732 return;
5733 }
5734 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005736 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005737 return;
5738 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005740 l = argvars[0].vval.v_list;
5741 if (l != NULL)
5742 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005743 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744 item = l->lv_first;
5745 if (argvars[2].v_type != VAR_UNKNOWN)
5746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // Start at specified item. Use the cached index that list_find()
5748 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005749 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005750 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005752 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005753 if (error)
5754 item = NULL;
5755 }
5756
5757 for ( ; item != NULL; item = item->li_next, ++idx)
5758 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5759 {
5760 rettv->vval.v_number = idx;
5761 break;
5762 }
5763 }
5764}
5765
5766static int inputsecret_flag = 0;
5767
5768/*
5769 * "input()" function
5770 * Also handles inputsecret() when inputsecret is set.
5771 */
5772 static void
5773f_input(typval_T *argvars, typval_T *rettv)
5774{
5775 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5776}
5777
5778/*
5779 * "inputdialog()" function
5780 */
5781 static void
5782f_inputdialog(typval_T *argvars, typval_T *rettv)
5783{
5784#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005785 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5787 {
5788 char_u *message;
5789 char_u buf[NUMBUFLEN];
5790 char_u *defstr = (char_u *)"";
5791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005792 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005794 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5796 else
5797 IObuff[0] = NUL;
5798 if (message != NULL && defstr != NULL
5799 && do_dialog(VIM_QUESTION, NULL, message,
5800 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5801 rettv->vval.v_string = vim_strsave(IObuff);
5802 else
5803 {
5804 if (message != NULL && defstr != NULL
5805 && argvars[1].v_type != VAR_UNKNOWN
5806 && argvars[2].v_type != VAR_UNKNOWN)
5807 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005808 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005809 else
5810 rettv->vval.v_string = NULL;
5811 }
5812 rettv->v_type = VAR_STRING;
5813 }
5814 else
5815#endif
5816 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5817}
5818
5819/*
5820 * "inputlist()" function
5821 */
5822 static void
5823f_inputlist(typval_T *argvars, typval_T *rettv)
5824{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005825 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005826 listitem_T *li;
5827 int selected;
5828 int mouse_used;
5829
5830#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005831 // While starting up, there is no place to enter text. When running tests
5832 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005833 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 return;
5835#endif
5836 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005838 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005839 return;
5840 }
5841
5842 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005843 msg_row = Rows - 1; // for when 'cmdheight' > 1
5844 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 msg_scroll = TRUE;
5846 msg_clr_eos();
5847
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005848 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005849 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005850 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005852 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 msg_putchar('\n');
5854 }
5855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 selected = prompt_for_number(&mouse_used);
5858 if (mouse_used)
5859 selected -= lines_left;
5860
5861 rettv->vval.v_number = selected;
5862}
5863
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5865
5866/*
5867 * "inputrestore()" function
5868 */
5869 static void
5870f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5871{
5872 if (ga_userinput.ga_len > 0)
5873 {
5874 --ga_userinput.ga_len;
5875 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005876 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005877 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005878 }
5879 else if (p_verbose > 1)
5880 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005881 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005882 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005883 }
5884}
5885
5886/*
5887 * "inputsave()" function
5888 */
5889 static void
5890f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5891{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005892 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005893 if (ga_grow(&ga_userinput, 1) == OK)
5894 {
5895 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5896 + ga_userinput.ga_len);
5897 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899 }
5900 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005901 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902}
5903
5904/*
5905 * "inputsecret()" function
5906 */
5907 static void
5908f_inputsecret(typval_T *argvars, typval_T *rettv)
5909{
5910 ++cmdline_star;
5911 ++inputsecret_flag;
5912 f_input(argvars, rettv);
5913 --cmdline_star;
5914 --inputsecret_flag;
5915}
5916
5917/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005918 * "interrupt()" function
5919 */
5920 static void
5921f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5922{
5923 got_int = TRUE;
5924}
5925
5926/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005927 * "invert(expr)" function
5928 */
5929 static void
5930f_invert(typval_T *argvars, typval_T *rettv)
5931{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005932 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933}
5934
5935/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936 * "islocked()" function
5937 */
5938 static void
5939f_islocked(typval_T *argvars, typval_T *rettv)
5940{
5941 lval_T lv;
5942 char_u *end;
5943 dictitem_T *di;
5944
5945 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005946 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005947 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005948 if (end != NULL && lv.ll_name != NULL)
5949 {
5950 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005951 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 else
5953 {
5954 if (lv.ll_tv == NULL)
5955 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005956 di = find_var(lv.ll_name, NULL, TRUE);
5957 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // Consider a variable locked when:
5960 // 1. the variable itself is locked
5961 // 2. the value of the variable is locked.
5962 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005963 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5964 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 }
5966 }
5967 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005968 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005970 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005972 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5974 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5977 }
5978 }
5979
5980 clear_lval(&lv);
5981}
5982
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984 * "last_buffer_nr()" function.
5985 */
5986 static void
5987f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5988{
5989 int n = 0;
5990 buf_T *buf;
5991
Bram Moolenaar29323592016-07-24 22:04:11 +02005992 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993 if (n < buf->b_fnum)
5994 n = buf->b_fnum;
5995
5996 rettv->vval.v_number = n;
5997}
5998
5999/*
6000 * "len()" function
6001 */
6002 static void
6003f_len(typval_T *argvars, typval_T *rettv)
6004{
6005 switch (argvars[0].v_type)
6006 {
6007 case VAR_STRING:
6008 case VAR_NUMBER:
6009 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006010 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006012 case VAR_BLOB:
6013 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6014 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 case VAR_LIST:
6016 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6017 break;
6018 case VAR_DICT:
6019 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6020 break;
6021 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006022 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006024 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 case VAR_SPECIAL:
6026 case VAR_FLOAT:
6027 case VAR_FUNC:
6028 case VAR_PARTIAL:
6029 case VAR_JOB:
6030 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006031 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006032 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 break;
6034 }
6035}
6036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006038libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006039{
6040#ifdef FEAT_LIBCALL
6041 char_u *string_in;
6042 char_u **string_result;
6043 int nr_result;
6044#endif
6045
6046 rettv->v_type = type;
6047 if (type != VAR_NUMBER)
6048 rettv->vval.v_string = NULL;
6049
6050 if (check_restricted() || check_secure())
6051 return;
6052
6053#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006054 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6056 {
6057 string_in = NULL;
6058 if (argvars[2].v_type == VAR_STRING)
6059 string_in = argvars[2].vval.v_string;
6060 if (type == VAR_NUMBER)
6061 string_result = NULL;
6062 else
6063 string_result = &rettv->vval.v_string;
6064 if (mch_libcall(argvars[0].vval.v_string,
6065 argvars[1].vval.v_string,
6066 string_in,
6067 argvars[2].vval.v_number,
6068 string_result,
6069 &nr_result) == OK
6070 && type == VAR_NUMBER)
6071 rettv->vval.v_number = nr_result;
6072 }
6073#endif
6074}
6075
6076/*
6077 * "libcall()" function
6078 */
6079 static void
6080f_libcall(typval_T *argvars, typval_T *rettv)
6081{
6082 libcall_common(argvars, rettv, VAR_STRING);
6083}
6084
6085/*
6086 * "libcallnr()" function
6087 */
6088 static void
6089f_libcallnr(typval_T *argvars, typval_T *rettv)
6090{
6091 libcall_common(argvars, rettv, VAR_NUMBER);
6092}
6093
6094/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006095 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006096 */
6097 static void
6098f_line(typval_T *argvars, typval_T *rettv)
6099{
6100 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006101 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006103 int id;
6104 tabpage_T *tp;
6105 win_T *wp;
6106 win_T *save_curwin;
6107 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006109 if (argvars[1].v_type != VAR_UNKNOWN)
6110 {
6111 // use window specified in the second argument
6112 id = (int)tv_get_number(&argvars[1]);
6113 wp = win_id2wp_tp(id, &tp);
6114 if (wp != NULL && tp != NULL)
6115 {
6116 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6117 == OK)
6118 {
6119 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006120 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006121 }
6122 restore_win_noblock(save_curwin, save_curtab, TRUE);
6123 }
6124 }
6125 else
6126 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006127 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006128
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006129 if (fp != NULL)
6130 lnum = fp->lnum;
6131 rettv->vval.v_number = lnum;
6132}
6133
6134/*
6135 * "line2byte(lnum)" function
6136 */
6137 static void
6138f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6139{
6140#ifndef FEAT_BYTEOFF
6141 rettv->vval.v_number = -1;
6142#else
6143 linenr_T lnum;
6144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006145 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6147 rettv->vval.v_number = -1;
6148 else
6149 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6150 if (rettv->vval.v_number >= 0)
6151 ++rettv->vval.v_number;
6152#endif
6153}
6154
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155#ifdef FEAT_LUA
6156/*
6157 * "luaeval()" function
6158 */
6159 static void
6160f_luaeval(typval_T *argvars, typval_T *rettv)
6161{
6162 char_u *str;
6163 char_u buf[NUMBUFLEN];
6164
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006165 if (check_restricted() || check_secure())
6166 return;
6167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006168 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 do_luaeval(str, argvars + 1, rettv);
6170}
6171#endif
6172
6173/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174 * "maparg()" function
6175 */
6176 static void
6177f_maparg(typval_T *argvars, typval_T *rettv)
6178{
6179 get_maparg(argvars, rettv, TRUE);
6180}
6181
6182/*
6183 * "mapcheck()" function
6184 */
6185 static void
6186f_mapcheck(typval_T *argvars, typval_T *rettv)
6187{
6188 get_maparg(argvars, rettv, FALSE);
6189}
6190
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006191typedef enum
6192{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006193 MATCH_END, // matchend()
6194 MATCH_MATCH, // match()
6195 MATCH_STR, // matchstr()
6196 MATCH_LIST, // matchlist()
6197 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006198} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006199
6200 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006201find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006202{
6203 char_u *str = NULL;
6204 long len = 0;
6205 char_u *expr = NULL;
6206 char_u *pat;
6207 regmatch_T regmatch;
6208 char_u patbuf[NUMBUFLEN];
6209 char_u strbuf[NUMBUFLEN];
6210 char_u *save_cpo;
6211 long start = 0;
6212 long nth = 1;
6213 colnr_T startcol = 0;
6214 int match = 0;
6215 list_T *l = NULL;
6216 listitem_T *li = NULL;
6217 long idx = 0;
6218 char_u *tofree = NULL;
6219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006220 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006221 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006222 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223
6224 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006225 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006227 // type MATCH_LIST: return empty list when there are no matches.
6228 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229 if (rettv_list_alloc(rettv) == FAIL)
6230 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006231 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232 && (list_append_string(rettv->vval.v_list,
6233 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
6239 (varnumber_T)-1) == FAIL))
6240 {
6241 list_free(rettv->vval.v_list);
6242 rettv->vval.v_list = NULL;
6243 goto theend;
6244 }
6245 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006246 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 {
6248 rettv->v_type = VAR_STRING;
6249 rettv->vval.v_string = NULL;
6250 }
6251
6252 if (argvars[0].v_type == VAR_LIST)
6253 {
6254 if ((l = argvars[0].vval.v_list) == NULL)
6255 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006256 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 li = l->lv_first;
6258 }
6259 else
6260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006261 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262 len = (long)STRLEN(str);
6263 }
6264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006265 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 if (pat == NULL)
6267 goto theend;
6268
6269 if (argvars[2].v_type != VAR_UNKNOWN)
6270 {
6271 int error = FALSE;
6272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006273 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006274 if (error)
6275 goto theend;
6276 if (l != NULL)
6277 {
6278 li = list_find(l, start);
6279 if (li == NULL)
6280 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006281 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 }
6283 else
6284 {
6285 if (start < 0)
6286 start = 0;
6287 if (start > len)
6288 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006289 // When "count" argument is there ignore matches before "start",
6290 // otherwise skip part of the string. Differs when pattern is "^"
6291 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 if (argvars[3].v_type != VAR_UNKNOWN)
6293 startcol = start;
6294 else
6295 {
6296 str += start;
6297 len -= start;
6298 }
6299 }
6300
6301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006302 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 if (error)
6304 goto theend;
6305 }
6306
6307 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6308 if (regmatch.regprog != NULL)
6309 {
6310 regmatch.rm_ic = p_ic;
6311
6312 for (;;)
6313 {
6314 if (l != NULL)
6315 {
6316 if (li == NULL)
6317 {
6318 match = FALSE;
6319 break;
6320 }
6321 vim_free(tofree);
6322 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6323 if (str == NULL)
6324 break;
6325 }
6326
6327 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6328
6329 if (match && --nth <= 0)
6330 break;
6331 if (l == NULL && !match)
6332 break;
6333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006334 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006335 if (l != NULL)
6336 {
6337 li = li->li_next;
6338 ++idx;
6339 }
6340 else
6341 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 startcol = (colnr_T)(regmatch.startp[0]
6343 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344 if (startcol > (colnr_T)len
6345 || str + startcol <= regmatch.startp[0])
6346 {
6347 match = FALSE;
6348 break;
6349 }
6350 }
6351 }
6352
6353 if (match)
6354 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006355 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356 {
6357 listitem_T *li1 = rettv->vval.v_list->lv_first;
6358 listitem_T *li2 = li1->li_next;
6359 listitem_T *li3 = li2->li_next;
6360 listitem_T *li4 = li3->li_next;
6361
6362 vim_free(li1->li_tv.vval.v_string);
6363 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006364 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006365 li3->li_tv.vval.v_number =
6366 (varnumber_T)(regmatch.startp[0] - expr);
6367 li4->li_tv.vval.v_number =
6368 (varnumber_T)(regmatch.endp[0] - expr);
6369 if (l != NULL)
6370 li2->li_tv.vval.v_number = (varnumber_T)idx;
6371 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006372 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 {
6374 int i;
6375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006376 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377 for (i = 0; i < NSUBEXP; ++i)
6378 {
6379 if (regmatch.endp[i] == NULL)
6380 {
6381 if (list_append_string(rettv->vval.v_list,
6382 (char_u *)"", 0) == FAIL)
6383 break;
6384 }
6385 else if (list_append_string(rettv->vval.v_list,
6386 regmatch.startp[i],
6387 (int)(regmatch.endp[i] - regmatch.startp[i]))
6388 == FAIL)
6389 break;
6390 }
6391 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006392 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006394 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 if (l != NULL)
6396 copy_tv(&li->li_tv, rettv);
6397 else
6398 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006399 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 }
6401 else if (l != NULL)
6402 rettv->vval.v_number = idx;
6403 else
6404 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006405 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 rettv->vval.v_number =
6407 (varnumber_T)(regmatch.startp[0] - str);
6408 else
6409 rettv->vval.v_number =
6410 (varnumber_T)(regmatch.endp[0] - str);
6411 rettv->vval.v_number += (varnumber_T)(str - expr);
6412 }
6413 }
6414 vim_regfree(regmatch.regprog);
6415 }
6416
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006417theend:
6418 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420 listitem_remove(rettv->vval.v_list,
6421 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 vim_free(tofree);
6423 p_cpo = save_cpo;
6424}
6425
6426/*
6427 * "match()" function
6428 */
6429 static void
6430f_match(typval_T *argvars, typval_T *rettv)
6431{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006432 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433}
6434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435/*
6436 * "matchend()" function
6437 */
6438 static void
6439f_matchend(typval_T *argvars, typval_T *rettv)
6440{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006441 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442}
6443
6444/*
6445 * "matchlist()" function
6446 */
6447 static void
6448f_matchlist(typval_T *argvars, typval_T *rettv)
6449{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006450 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451}
6452
6453/*
6454 * "matchstr()" function
6455 */
6456 static void
6457f_matchstr(typval_T *argvars, typval_T *rettv)
6458{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006459 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460}
6461
6462/*
6463 * "matchstrpos()" function
6464 */
6465 static void
6466f_matchstrpos(typval_T *argvars, typval_T *rettv)
6467{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006468 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469}
6470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471 static void
6472max_min(typval_T *argvars, typval_T *rettv, int domax)
6473{
6474 varnumber_T n = 0;
6475 varnumber_T i;
6476 int error = FALSE;
6477
6478 if (argvars[0].v_type == VAR_LIST)
6479 {
6480 list_T *l;
6481 listitem_T *li;
6482
6483 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006484 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006486 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006488 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6489 n = l->lv_u.nonmat.lv_start;
6490 else
6491 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6492 * l->lv_u.nonmat.lv_stride;
6493 }
6494 else
6495 {
6496 li = l->lv_first;
6497 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006498 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006499 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006500 if (error)
6501 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006502 for (;;)
6503 {
6504 li = li->li_next;
6505 if (li == NULL)
6506 break;
6507 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006508 if (error)
6509 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006510 if (domax ? i > n : i < n)
6511 n = i;
6512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 }
6514 }
6515 }
6516 }
6517 else if (argvars[0].v_type == VAR_DICT)
6518 {
6519 dict_T *d;
6520 int first = TRUE;
6521 hashitem_T *hi;
6522 int todo;
6523
6524 d = argvars[0].vval.v_dict;
6525 if (d != NULL)
6526 {
6527 todo = (int)d->dv_hashtab.ht_used;
6528 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6529 {
6530 if (!HASHITEM_EMPTY(hi))
6531 {
6532 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006533 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006534 if (error)
6535 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006536 if (first)
6537 {
6538 n = i;
6539 first = FALSE;
6540 }
6541 else if (domax ? i > n : i < n)
6542 n = i;
6543 }
6544 }
6545 }
6546 }
6547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006548 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006549
6550 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551}
6552
6553/*
6554 * "max()" function
6555 */
6556 static void
6557f_max(typval_T *argvars, typval_T *rettv)
6558{
6559 max_min(argvars, rettv, TRUE);
6560}
6561
6562/*
6563 * "min()" function
6564 */
6565 static void
6566f_min(typval_T *argvars, typval_T *rettv)
6567{
6568 max_min(argvars, rettv, FALSE);
6569}
6570
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571#if defined(FEAT_MZSCHEME) || defined(PROTO)
6572/*
6573 * "mzeval()" function
6574 */
6575 static void
6576f_mzeval(typval_T *argvars, typval_T *rettv)
6577{
6578 char_u *str;
6579 char_u buf[NUMBUFLEN];
6580
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006581 if (check_restricted() || check_secure())
6582 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006583 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 do_mzeval(str, rettv);
6585}
6586
6587 void
6588mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6589{
6590 typval_T argvars[3];
6591
6592 argvars[0].v_type = VAR_STRING;
6593 argvars[0].vval.v_string = name;
6594 copy_tv(args, &argvars[1]);
6595 argvars[2].v_type = VAR_UNKNOWN;
6596 f_call(argvars, rettv);
6597 clear_tv(&argvars[1]);
6598}
6599#endif
6600
6601/*
6602 * "nextnonblank()" function
6603 */
6604 static void
6605f_nextnonblank(typval_T *argvars, typval_T *rettv)
6606{
6607 linenr_T lnum;
6608
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006609 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610 {
6611 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6612 {
6613 lnum = 0;
6614 break;
6615 }
6616 if (*skipwhite(ml_get(lnum)) != NUL)
6617 break;
6618 }
6619 rettv->vval.v_number = lnum;
6620}
6621
6622/*
6623 * "nr2char()" function
6624 */
6625 static void
6626f_nr2char(typval_T *argvars, typval_T *rettv)
6627{
6628 char_u buf[NUMBUFLEN];
6629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 if (has_mbyte)
6631 {
6632 int utf8 = 0;
6633
6634 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006635 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006637 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006638 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006639 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 }
6641 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006643 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006644 buf[1] = NUL;
6645 }
6646 rettv->v_type = VAR_STRING;
6647 rettv->vval.v_string = vim_strsave(buf);
6648}
6649
6650/*
6651 * "or(expr, expr)" function
6652 */
6653 static void
6654f_or(typval_T *argvars, typval_T *rettv)
6655{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006656 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6657 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658}
6659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006660#ifdef FEAT_PERL
6661/*
6662 * "perleval()" function
6663 */
6664 static void
6665f_perleval(typval_T *argvars, typval_T *rettv)
6666{
6667 char_u *str;
6668 char_u buf[NUMBUFLEN];
6669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006670 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 do_perleval(str, rettv);
6672}
6673#endif
6674
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675/*
6676 * "prevnonblank()" function
6677 */
6678 static void
6679f_prevnonblank(typval_T *argvars, typval_T *rettv)
6680{
6681 linenr_T lnum;
6682
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006683 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6685 lnum = 0;
6686 else
6687 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6688 --lnum;
6689 rettv->vval.v_number = lnum;
6690}
6691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006692// This dummy va_list is here because:
6693// - passing a NULL pointer doesn't work when va_list isn't a pointer
6694// - locally in the function results in a "used before set" warning
6695// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006696static va_list ap;
6697
6698/*
6699 * "printf()" function
6700 */
6701 static void
6702f_printf(typval_T *argvars, typval_T *rettv)
6703{
6704 char_u buf[NUMBUFLEN];
6705 int len;
6706 char_u *s;
6707 int saved_did_emsg = did_emsg;
6708 char *fmt;
6709
6710 rettv->v_type = VAR_STRING;
6711 rettv->vval.v_string = NULL;
6712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006713 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006715 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006716 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006717 if (!did_emsg)
6718 {
6719 s = alloc(len + 1);
6720 if (s != NULL)
6721 {
6722 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006723 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6724 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725 }
6726 }
6727 did_emsg |= saved_did_emsg;
6728}
6729
6730/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006731 * "pum_getpos()" function
6732 */
6733 static void
6734f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6735{
6736 if (rettv_dict_alloc(rettv) != OK)
6737 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006738 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006739}
6740
6741/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 * "pumvisible()" function
6743 */
6744 static void
6745f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6746{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 if (pum_visible())
6748 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749}
6750
6751#ifdef FEAT_PYTHON3
6752/*
6753 * "py3eval()" function
6754 */
6755 static void
6756f_py3eval(typval_T *argvars, typval_T *rettv)
6757{
6758 char_u *str;
6759 char_u buf[NUMBUFLEN];
6760
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006761 if (check_restricted() || check_secure())
6762 return;
6763
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006764 if (p_pyx == 0)
6765 p_pyx = 3;
6766
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006767 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 do_py3eval(str, rettv);
6769}
6770#endif
6771
6772#ifdef FEAT_PYTHON
6773/*
6774 * "pyeval()" function
6775 */
6776 static void
6777f_pyeval(typval_T *argvars, typval_T *rettv)
6778{
6779 char_u *str;
6780 char_u buf[NUMBUFLEN];
6781
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006782 if (check_restricted() || check_secure())
6783 return;
6784
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006785 if (p_pyx == 0)
6786 p_pyx = 2;
6787
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006788 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006789 do_pyeval(str, rettv);
6790}
6791#endif
6792
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006793#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6794/*
6795 * "pyxeval()" function
6796 */
6797 static void
6798f_pyxeval(typval_T *argvars, typval_T *rettv)
6799{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006800 if (check_restricted() || check_secure())
6801 return;
6802
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006803# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6804 init_pyxversion();
6805 if (p_pyx == 2)
6806 f_pyeval(argvars, rettv);
6807 else
6808 f_py3eval(argvars, rettv);
6809# elif defined(FEAT_PYTHON)
6810 f_pyeval(argvars, rettv);
6811# elif defined(FEAT_PYTHON3)
6812 f_py3eval(argvars, rettv);
6813# endif
6814}
6815#endif
6816
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006817static UINT32_T srand_seed_for_testing = 0;
6818static int srand_seed_for_testing_is_used = FALSE;
6819
6820 static void
6821f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6822{
6823 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006824 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006825 else
6826 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006827 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6828 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006829 }
6830}
6831
6832 static void
6833init_srand(UINT32_T *x)
6834{
6835#ifndef MSWIN
6836 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6837#endif
6838
6839 if (srand_seed_for_testing_is_used)
6840 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006841 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006842 return;
6843 }
6844#ifndef MSWIN
6845 if (dev_urandom_state != FAIL)
6846 {
6847 int fd = open("/dev/urandom", O_RDONLY);
6848 struct {
6849 union {
6850 UINT32_T number;
6851 char bytes[sizeof(UINT32_T)];
6852 } contents;
6853 } buf;
6854
6855 // Attempt reading /dev/urandom.
6856 if (fd == -1)
6857 dev_urandom_state = FAIL;
6858 else
6859 {
6860 buf.contents.number = 0;
6861 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6862 != sizeof(UINT32_T))
6863 dev_urandom_state = FAIL;
6864 else
6865 {
6866 dev_urandom_state = OK;
6867 *x = buf.contents.number;
6868 }
6869 close(fd);
6870 }
6871 }
6872 if (dev_urandom_state != OK)
6873 // Reading /dev/urandom doesn't work, fall back to time().
6874#endif
6875 *x = vim_time();
6876}
6877
6878#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6879#define SPLITMIX32(x, z) ( \
6880 z = (x += 0x9e3779b9), \
6881 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6882 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6883 z ^ (z >> 16) \
6884 )
6885#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6886 result = ROTL(y * 5, 7) * 9; \
6887 t = y << 9; \
6888 z ^= x; \
6889 w ^= y; \
6890 y ^= z, x ^= w; \
6891 z ^= t; \
6892 w = ROTL(w, 11);
6893
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006895 * "rand()" function
6896 */
6897 static void
6898f_rand(typval_T *argvars, typval_T *rettv)
6899{
6900 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006901 static UINT32_T gx, gy, gz, gw;
6902 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006903 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006904 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006905
6906 if (argvars[0].v_type == VAR_UNKNOWN)
6907 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006908 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006909 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006910 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006911 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006912 init_srand(&x);
6913
6914 gx = SPLITMIX32(x, z);
6915 gy = SPLITMIX32(x, z);
6916 gz = SPLITMIX32(x, z);
6917 gw = SPLITMIX32(x, z);
6918 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006919 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006920
6921 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006922 }
6923 else if (argvars[0].v_type == VAR_LIST)
6924 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006925 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006926 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006927 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006928
6929 lx = list_find(l, 0L);
6930 ly = list_find(l, 1L);
6931 lz = list_find(l, 2L);
6932 lw = list_find(l, 3L);
6933 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6934 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6935 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6936 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6937 x = (UINT32_T)lx->li_tv.vval.v_number;
6938 y = (UINT32_T)ly->li_tv.vval.v_number;
6939 z = (UINT32_T)lz->li_tv.vval.v_number;
6940 w = (UINT32_T)lw->li_tv.vval.v_number;
6941
6942 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6943
6944 lx->li_tv.vval.v_number = (varnumber_T)x;
6945 ly->li_tv.vval.v_number = (varnumber_T)y;
6946 lz->li_tv.vval.v_number = (varnumber_T)z;
6947 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006948 }
6949 else
6950 goto theend;
6951
6952 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006953 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006954 return;
6955
6956theend:
6957 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006958 rettv->v_type = VAR_NUMBER;
6959 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006960}
6961
6962/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006963 * "srand()" function
6964 */
6965 static void
6966f_srand(typval_T *argvars, typval_T *rettv)
6967{
6968 UINT32_T x = 0, z;
6969
6970 if (rettv_list_alloc(rettv) == FAIL)
6971 return;
6972 if (argvars[0].v_type == VAR_UNKNOWN)
6973 {
6974 init_srand(&x);
6975 }
6976 else
6977 {
6978 int error = FALSE;
6979
6980 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6981 if (error)
6982 return;
6983 }
6984
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 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6988 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6989}
6990
6991#undef ROTL
6992#undef SPLITMIX32
6993#undef SHUFFLE_XOSHIRO128STARSTAR
6994
6995/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006996 * "range()" function
6997 */
6998 static void
6999f_range(typval_T *argvars, typval_T *rettv)
7000{
7001 varnumber_T start;
7002 varnumber_T end;
7003 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 int error = FALSE;
7005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007 if (argvars[1].v_type == VAR_UNKNOWN)
7008 {
7009 end = start - 1;
7010 start = 0;
7011 }
7012 else
7013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007014 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007016 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 }
7018
7019 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007020 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007022 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007024 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007025 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007027 list_T *list = rettv->vval.v_list;
7028
7029 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007030 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007031 // be called.
7032 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007033 list->lv_u.nonmat.lv_start = start;
7034 list->lv_u.nonmat.lv_end = end;
7035 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007036 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007037 }
7038}
7039
7040/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007041 * Materialize "list".
7042 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007043 */
7044 void
7045range_list_materialize(list_T *list)
7046{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007047 varnumber_T start = list->lv_u.nonmat.lv_start;
7048 varnumber_T end = list->lv_u.nonmat.lv_end;
7049 int stride = list->lv_u.nonmat.lv_stride;
7050 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007051
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007052 list->lv_first = NULL;
7053 list->lv_u.mat.lv_last = NULL;
7054 list->lv_len = 0;
7055 list->lv_u.mat.lv_idx_item = NULL;
7056 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7057 if (list_append_number(list, (varnumber_T)i) == FAIL)
7058 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059}
7060
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007061/*
7062 * "getreginfo()" function
7063 */
7064 static void
7065f_getreginfo(typval_T *argvars, typval_T *rettv)
7066{
7067 char_u *strregname;
7068 int regname;
7069 char_u buf[NUMBUFLEN + 2];
7070 long reglen = 0;
7071 dict_T *dict;
7072 list_T *list;
7073
7074 if (argvars[0].v_type != VAR_UNKNOWN)
7075 {
7076 strregname = tv_get_string_chk(&argvars[0]);
7077 if (strregname == NULL)
7078 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007079 if (in_vim9script() && STRLEN(strregname) > 1)
7080 {
7081 semsg(_(e_register_name_must_be_one_char_str), strregname);
7082 return;
7083 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007084 }
7085 else
7086 strregname = get_vim_var_str(VV_REG);
7087
7088 regname = (strregname == NULL ? '"' : *strregname);
7089 if (regname == 0 || regname == '@')
7090 regname = '"';
7091
7092 if (rettv_dict_alloc(rettv) == FAIL)
7093 return;
7094 dict = rettv->vval.v_dict;
7095
7096 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7097 if (list == NULL)
7098 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007099 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007100
7101 buf[0] = NUL;
7102 buf[1] = NUL;
7103 switch (get_reg_type(regname, &reglen))
7104 {
7105 case MLINE: buf[0] = 'V'; break;
7106 case MCHAR: buf[0] = 'v'; break;
7107 case MBLOCK:
7108 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7109 reglen + 1);
7110 break;
7111 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007112 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007113
7114 buf[0] = get_register_name(get_unname_register());
7115 buf[1] = NUL;
7116 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007117 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007118 else
7119 {
7120 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7121
7122 if (item != NULL)
7123 {
7124 item->di_tv.v_type = VAR_SPECIAL;
7125 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007126 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007127 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007128 }
7129 }
7130}
7131
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007132 static void
7133return_register(int regname, typval_T *rettv)
7134{
7135 char_u buf[2] = {0, 0};
7136
7137 buf[0] = (char_u)regname;
7138 rettv->v_type = VAR_STRING;
7139 rettv->vval.v_string = vim_strsave(buf);
7140}
7141
7142/*
7143 * "reg_executing()" function
7144 */
7145 static void
7146f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7147{
7148 return_register(reg_executing, rettv);
7149}
7150
7151/*
7152 * "reg_recording()" function
7153 */
7154 static void
7155f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7156{
7157 return_register(reg_recording, rettv);
7158}
7159
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007160/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 * "rename({from}, {to})" function
7162 */
7163 static void
7164f_rename(typval_T *argvars, typval_T *rettv)
7165{
7166 char_u buf[NUMBUFLEN];
7167
7168 if (check_restricted() || check_secure())
7169 rettv->vval.v_number = -1;
7170 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007171 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7172 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007173}
7174
7175/*
7176 * "repeat()" function
7177 */
7178 static void
7179f_repeat(typval_T *argvars, typval_T *rettv)
7180{
7181 char_u *p;
7182 int n;
7183 int slen;
7184 int len;
7185 char_u *r;
7186 int i;
7187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007188 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007189 if (argvars[0].v_type == VAR_LIST)
7190 {
7191 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7192 while (n-- > 0)
7193 if (list_extend(rettv->vval.v_list,
7194 argvars[0].vval.v_list, NULL) == FAIL)
7195 break;
7196 }
7197 else
7198 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007199 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007200 rettv->v_type = VAR_STRING;
7201 rettv->vval.v_string = NULL;
7202
7203 slen = (int)STRLEN(p);
7204 len = slen * n;
7205 if (len <= 0)
7206 return;
7207
7208 r = alloc(len + 1);
7209 if (r != NULL)
7210 {
7211 for (i = 0; i < n; i++)
7212 mch_memmove(r + i * slen, p, (size_t)slen);
7213 r[len] = NUL;
7214 }
7215
7216 rettv->vval.v_string = r;
7217 }
7218}
7219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007220#define SP_NOMOVE 0x01 // don't move cursor
7221#define SP_REPEAT 0x02 // repeat to find outer pair
7222#define SP_RETCOUNT 0x04 // return matchcount
7223#define SP_SETPCMARK 0x08 // set previous context mark
7224#define SP_START 0x10 // accept match at start position
7225#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7226#define SP_END 0x40 // leave cursor at end of match
7227#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229/*
7230 * Get flags for a search function.
7231 * Possibly sets "p_ws".
7232 * Returns BACKWARD, FORWARD or zero (for an error).
7233 */
7234 static int
7235get_search_arg(typval_T *varp, int *flagsp)
7236{
7237 int dir = FORWARD;
7238 char_u *flags;
7239 char_u nbuf[NUMBUFLEN];
7240 int mask;
7241
7242 if (varp->v_type != VAR_UNKNOWN)
7243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007246 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 while (*flags != NUL)
7248 {
7249 switch (*flags)
7250 {
7251 case 'b': dir = BACKWARD; break;
7252 case 'w': p_ws = TRUE; break;
7253 case 'W': p_ws = FALSE; break;
7254 default: mask = 0;
7255 if (flagsp != NULL)
7256 switch (*flags)
7257 {
7258 case 'c': mask = SP_START; break;
7259 case 'e': mask = SP_END; break;
7260 case 'm': mask = SP_RETCOUNT; break;
7261 case 'n': mask = SP_NOMOVE; break;
7262 case 'p': mask = SP_SUBPAT; break;
7263 case 'r': mask = SP_REPEAT; break;
7264 case 's': mask = SP_SETPCMARK; break;
7265 case 'z': mask = SP_COLUMN; break;
7266 }
7267 if (mask == 0)
7268 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007269 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007270 dir = 0;
7271 }
7272 else
7273 *flagsp |= mask;
7274 }
7275 if (dir == 0)
7276 break;
7277 ++flags;
7278 }
7279 }
7280 return dir;
7281}
7282
7283/*
7284 * Shared by search() and searchpos() functions.
7285 */
7286 static int
7287search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7288{
7289 int flags;
7290 char_u *pat;
7291 pos_T pos;
7292 pos_T save_cursor;
7293 int save_p_ws = p_ws;
7294 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007295 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007298 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 long time_limit = 0;
7300#endif
7301 int options = SEARCH_KEEP;
7302 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007303 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007304 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007305 pos_T firstpos;
7306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007307 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007308 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 if (dir == 0)
7310 goto theend;
7311 flags = *flagsp;
7312 if (flags & SP_START)
7313 options |= SEARCH_START;
7314 if (flags & SP_END)
7315 options |= SEARCH_END;
7316 if (flags & SP_COLUMN)
7317 options |= SEARCH_COL;
7318
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007319 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007322 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 if (lnum_stop < 0)
7324 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007325 if (argvars[3].v_type != VAR_UNKNOWN)
7326 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007327#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007328 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 if (time_limit < 0)
7330 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007332 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007333 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 }
7335
7336#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007337 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 profile_setlimit(time_limit, &tm);
7339#endif
7340
7341 /*
7342 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7343 * Check to make sure only those flags are set.
7344 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7345 * flags cannot be set. Check for that condition also.
7346 */
7347 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7348 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007350 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 goto theend;
7352 }
7353
7354 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007355 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007356 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007357 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7358#ifdef FEAT_RELTIME
7359 sia.sa_tm = &tm;
7360#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007361
7362 // Repeat until {skip} returns FALSE.
7363 for (;;)
7364 {
7365 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007366 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007367 // finding the first match again means there is no match where {skip}
7368 // evaluates to zero.
7369 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7370 subpatnum = FAIL;
7371
Bram Moolenaara9c01042020-06-07 14:50:50 +02007372 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007373 // didn't find it or no skip argument
7374 break;
7375 firstpos = pos;
7376
Bram Moolenaara9c01042020-06-07 14:50:50 +02007377 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007378 {
7379 int do_skip;
7380 int err;
7381 pos_T save_pos = curwin->w_cursor;
7382
7383 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007384 err = FALSE;
7385 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007386 curwin->w_cursor = save_pos;
7387 if (err)
7388 {
7389 // Evaluating {skip} caused an error, break here.
7390 subpatnum = FAIL;
7391 break;
7392 }
7393 if (!do_skip)
7394 break;
7395 }
7396 }
7397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007398 if (subpatnum != FAIL)
7399 {
7400 if (flags & SP_SUBPAT)
7401 retval = subpatnum;
7402 else
7403 retval = pos.lnum;
7404 if (flags & SP_SETPCMARK)
7405 setpcmark();
7406 curwin->w_cursor = pos;
7407 if (match_pos != NULL)
7408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007409 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410 match_pos->lnum = pos.lnum;
7411 match_pos->col = pos.col + 1;
7412 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007413 // "/$" will put the cursor after the end of the line, may need to
7414 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 check_cursor();
7416 }
7417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007418 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007419 if (flags & SP_NOMOVE)
7420 curwin->w_cursor = save_cursor;
7421 else
7422 curwin->w_set_curswant = TRUE;
7423theend:
7424 p_ws = save_p_ws;
7425
7426 return retval;
7427}
7428
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007429#ifdef FEAT_RUBY
7430/*
7431 * "rubyeval()" function
7432 */
7433 static void
7434f_rubyeval(typval_T *argvars, typval_T *rettv)
7435{
7436 char_u *str;
7437 char_u buf[NUMBUFLEN];
7438
7439 str = tv_get_string_buf(&argvars[0], buf);
7440 do_rubyeval(str, rettv);
7441}
7442#endif
7443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444/*
7445 * "screenattr()" function
7446 */
7447 static void
7448f_screenattr(typval_T *argvars, typval_T *rettv)
7449{
7450 int row;
7451 int col;
7452 int c;
7453
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007454 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7455 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 if (row < 0 || row >= screen_Rows
7457 || col < 0 || col >= screen_Columns)
7458 c = -1;
7459 else
7460 c = ScreenAttrs[LineOffset[row] + col];
7461 rettv->vval.v_number = c;
7462}
7463
7464/*
7465 * "screenchar()" function
7466 */
7467 static void
7468f_screenchar(typval_T *argvars, typval_T *rettv)
7469{
7470 int row;
7471 int col;
7472 int off;
7473 int c;
7474
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007475 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7476 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007477 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 c = -1;
7479 else
7480 {
7481 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 if (enc_utf8 && ScreenLinesUC[off] != 0)
7483 c = ScreenLinesUC[off];
7484 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 c = ScreenLines[off];
7486 }
7487 rettv->vval.v_number = c;
7488}
7489
7490/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007491 * "screenchars()" function
7492 */
7493 static void
7494f_screenchars(typval_T *argvars, typval_T *rettv)
7495{
7496 int row;
7497 int col;
7498 int off;
7499 int c;
7500 int i;
7501
7502 if (rettv_list_alloc(rettv) == FAIL)
7503 return;
7504 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7505 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7506 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7507 return;
7508
7509 off = LineOffset[row] + col;
7510 if (enc_utf8 && ScreenLinesUC[off] != 0)
7511 c = ScreenLinesUC[off];
7512 else
7513 c = ScreenLines[off];
7514 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7515
7516 if (enc_utf8)
7517
7518 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7519 list_append_number(rettv->vval.v_list,
7520 (varnumber_T)ScreenLinesC[i][off]);
7521}
7522
7523/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 * "screencol()" function
7525 *
7526 * First column is 1 to be consistent with virtcol().
7527 */
7528 static void
7529f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7530{
7531 rettv->vval.v_number = screen_screencol() + 1;
7532}
7533
7534/*
7535 * "screenrow()" function
7536 */
7537 static void
7538f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7539{
7540 rettv->vval.v_number = screen_screenrow() + 1;
7541}
7542
7543/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007544 * "screenstring()" function
7545 */
7546 static void
7547f_screenstring(typval_T *argvars, typval_T *rettv)
7548{
7549 int row;
7550 int col;
7551 int off;
7552 int c;
7553 int i;
7554 char_u buf[MB_MAXBYTES + 1];
7555 int buflen = 0;
7556
7557 rettv->vval.v_string = NULL;
7558 rettv->v_type = VAR_STRING;
7559
7560 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7561 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7562 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7563 return;
7564
7565 off = LineOffset[row] + col;
7566 if (enc_utf8 && ScreenLinesUC[off] != 0)
7567 c = ScreenLinesUC[off];
7568 else
7569 c = ScreenLines[off];
7570 buflen += mb_char2bytes(c, buf);
7571
Bram Moolenaarf1387282021-03-22 17:11:15 +01007572 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007573 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7574 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7575
7576 buf[buflen] = NUL;
7577 rettv->vval.v_string = vim_strsave(buf);
7578}
7579
7580/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007581 * "search()" function
7582 */
7583 static void
7584f_search(typval_T *argvars, typval_T *rettv)
7585{
7586 int flags = 0;
7587
7588 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7589}
7590
7591/*
7592 * "searchdecl()" function
7593 */
7594 static void
7595f_searchdecl(typval_T *argvars, typval_T *rettv)
7596{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007597 int locally = TRUE;
7598 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 int error = FALSE;
7600 char_u *name;
7601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007602 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007604 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 if (argvars[1].v_type != VAR_UNKNOWN)
7606 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007607 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007609 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007610 }
7611 if (!error && name != NULL)
7612 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7613 locally, thisblock, SEARCH_KEEP) == FAIL;
7614}
7615
7616/*
7617 * Used by searchpair() and searchpairpos()
7618 */
7619 static int
7620searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7621{
7622 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007623 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 int save_p_ws = p_ws;
7625 int dir;
7626 int flags = 0;
7627 char_u nbuf1[NUMBUFLEN];
7628 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007629 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 long lnum_stop = 0;
7631 long time_limit = 0;
7632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007633 // Get the three pattern arguments: start, middle, end. Will result in an
7634 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007635 spat = tv_get_string_chk(&argvars[0]);
7636 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7637 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007639 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007641 // Handle the optional fourth argument: flags
7642 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 if (dir == 0)
7644 goto theend;
7645
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007646 // Don't accept SP_END or SP_SUBPAT.
7647 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 if ((flags & (SP_END | SP_SUBPAT)) != 0
7649 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 goto theend;
7653 }
7654
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007655 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 if (flags & SP_REPEAT)
7657 p_ws = FALSE;
7658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007659 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 if (argvars[3].v_type == VAR_UNKNOWN
7661 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007662 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007663 else
7664 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007665 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007666 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007667
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 if (argvars[5].v_type != VAR_UNKNOWN)
7669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007670 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007675 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676#ifdef FEAT_RELTIME
7677 if (argvars[6].v_type != VAR_UNKNOWN)
7678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007679 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007682 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007684 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 }
7686#endif
7687 }
7688 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689
7690 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7691 match_pos, lnum_stop, time_limit);
7692
7693theend:
7694 p_ws = save_p_ws;
7695
7696 return retval;
7697}
7698
7699/*
7700 * "searchpair()" function
7701 */
7702 static void
7703f_searchpair(typval_T *argvars, typval_T *rettv)
7704{
7705 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7706}
7707
7708/*
7709 * "searchpairpos()" function
7710 */
7711 static void
7712f_searchpairpos(typval_T *argvars, typval_T *rettv)
7713{
7714 pos_T match_pos;
7715 int lnum = 0;
7716 int col = 0;
7717
7718 if (rettv_list_alloc(rettv) == FAIL)
7719 return;
7720
7721 if (searchpair_cmn(argvars, &match_pos) > 0)
7722 {
7723 lnum = match_pos.lnum;
7724 col = match_pos.col;
7725 }
7726
7727 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7728 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7729}
7730
7731/*
7732 * Search for a start/middle/end thing.
7733 * Used by searchpair(), see its documentation for the details.
7734 * Returns 0 or -1 for no match,
7735 */
7736 long
7737do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007738 char_u *spat, // start pattern
7739 char_u *mpat, // middle pattern
7740 char_u *epat, // end pattern
7741 int dir, // BACKWARD or FORWARD
7742 typval_T *skip, // skip expression
7743 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007745 linenr_T lnum_stop, // stop at this line if not zero
7746 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747{
7748 char_u *save_cpo;
7749 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7750 long retval = 0;
7751 pos_T pos;
7752 pos_T firstpos;
7753 pos_T foundpos;
7754 pos_T save_cursor;
7755 pos_T save_pos;
7756 int n;
7757 int r;
7758 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007759 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 int err;
7761 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007762#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007766 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 save_cpo = p_cpo;
7768 p_cpo = empty_option;
7769
7770#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007771 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 profile_setlimit(time_limit, &tm);
7773#endif
7774
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007775 // Make two search patterns: start/end (pat2, for in nested pairs) and
7776 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007777 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7778 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 if (pat2 == NULL || pat3 == NULL)
7780 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007781 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782 if (*mpat == NUL)
7783 STRCPY(pat3, pat2);
7784 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007785 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 spat, epat, mpat);
7787 if (flags & SP_START)
7788 options |= SEARCH_START;
7789
Bram Moolenaar48570482017-10-30 21:48:41 +01007790 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007791 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793 save_cursor = curwin->w_cursor;
7794 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007795 CLEAR_POS(&firstpos);
7796 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 pat = pat3;
7798 for (;;)
7799 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007800 searchit_arg_T sia;
7801
Bram Moolenaara80faa82020-04-12 19:37:17 +02007802 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007803 sia.sa_stop_lnum = lnum_stop;
7804#ifdef FEAT_RELTIME
7805 sia.sa_tm = &tm;
7806#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007807 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007808 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007809 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007810 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 break;
7812
7813 if (firstpos.lnum == 0)
7814 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007815 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007817 // Found the same position again. Can happen with a pattern that
7818 // has "\zs" at the end and searching backwards. Advance one
7819 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820 if (dir == BACKWARD)
7821 decl(&pos);
7822 else
7823 incl(&pos);
7824 }
7825 foundpos = pos;
7826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007827 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 options &= ~SEARCH_START;
7829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007830 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007831 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 {
7833 save_pos = curwin->w_cursor;
7834 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007835 err = FALSE;
7836 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007837 curwin->w_cursor = save_pos;
7838 if (err)
7839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007840 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 curwin->w_cursor = save_cursor;
7842 retval = -1;
7843 break;
7844 }
7845 if (r)
7846 continue;
7847 }
7848
7849 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007851 // Found end when searching backwards or start when searching
7852 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007854 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 }
7856 else
7857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007858 // Found end when searching forward or start when searching
7859 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007861 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 }
7863
7864 if (nest == 0)
7865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007866 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 if (flags & SP_RETCOUNT)
7868 ++retval;
7869 else
7870 retval = pos.lnum;
7871 if (flags & SP_SETPCMARK)
7872 setpcmark();
7873 curwin->w_cursor = pos;
7874 if (!(flags & SP_REPEAT))
7875 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007876 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 }
7878 }
7879
7880 if (match_pos != NULL)
7881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007882 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883 match_pos->lnum = curwin->w_cursor.lnum;
7884 match_pos->col = curwin->w_cursor.col + 1;
7885 }
7886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007887 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 if ((flags & SP_NOMOVE) || retval == 0)
7889 curwin->w_cursor = save_cursor;
7890
7891theend:
7892 vim_free(pat2);
7893 vim_free(pat3);
7894 if (p_cpo == empty_option)
7895 p_cpo = save_cpo;
7896 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007898 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007899 // If it's still empty it was changed and restored, need to restore in
7900 // the complicated way.
7901 if (*p_cpo == NUL)
7902 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007904 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905
7906 return retval;
7907}
7908
7909/*
7910 * "searchpos()" function
7911 */
7912 static void
7913f_searchpos(typval_T *argvars, typval_T *rettv)
7914{
7915 pos_T match_pos;
7916 int lnum = 0;
7917 int col = 0;
7918 int n;
7919 int flags = 0;
7920
7921 if (rettv_list_alloc(rettv) == FAIL)
7922 return;
7923
7924 n = search_cmn(argvars, &match_pos, &flags);
7925 if (n > 0)
7926 {
7927 lnum = match_pos.lnum;
7928 col = match_pos.col;
7929 }
7930
7931 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7932 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7933 if (flags & SP_SUBPAT)
7934 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7935}
7936
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007937/*
7938 * Set the cursor or mark position.
7939 * If 'charpos' is TRUE, then use the column number as a character offet.
7940 * Otherwise use the column number as a byte offset.
7941 */
7942 static void
7943set_position(typval_T *argvars, typval_T *rettv, int charpos)
7944{
7945 pos_T pos;
7946 int fnum;
7947 char_u *name;
7948 colnr_T curswant = -1;
7949
7950 rettv->vval.v_number = -1;
7951
7952 name = tv_get_string_chk(argvars);
7953 if (name != NULL)
7954 {
7955 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7956 {
7957 if (pos.col != MAXCOL && --pos.col < 0)
7958 pos.col = 0;
7959 if ((name[0] == '.' && name[1] == NUL))
7960 {
7961 // set cursor; "fnum" is ignored
7962 curwin->w_cursor = pos;
7963 if (curswant >= 0)
7964 {
7965 curwin->w_curswant = curswant - 1;
7966 curwin->w_set_curswant = FALSE;
7967 }
7968 check_cursor();
7969 rettv->vval.v_number = 0;
7970 }
7971 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7972 {
7973 // set mark
7974 if (setmark_pos(name[1], &pos, fnum) == OK)
7975 rettv->vval.v_number = 0;
7976 }
7977 else
7978 emsg(_(e_invarg));
7979 }
7980 }
7981}
7982/*
7983 * "setcharpos()" function
7984 */
7985 static void
7986f_setcharpos(typval_T *argvars, typval_T *rettv)
7987{
7988 set_position(argvars, rettv, TRUE);
7989}
7990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7993{
7994 dict_T *d;
7995 dictitem_T *di;
7996 char_u *csearch;
7997
7998 if (argvars[0].v_type != VAR_DICT)
7999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008000 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 return;
8002 }
8003
8004 if ((d = argvars[0].vval.v_dict) != NULL)
8005 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008006 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 if (csearch != NULL)
8008 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 if (enc_utf8)
8010 {
8011 int pcc[MAX_MCO];
8012 int c = utfc_ptr2char(csearch, pcc);
8013
8014 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8015 }
8016 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008018 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 }
8020
8021 di = dict_find(d, (char_u *)"forward", -1);
8022 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008023 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 ? FORWARD : BACKWARD);
8025
8026 di = dict_find(d, (char_u *)"until", -1);
8027 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008028 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 }
8030}
8031
8032/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008033 * "setcursorcharpos" function
8034 */
8035 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008036f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008037{
8038 set_cursorpos(argvars, rettv, TRUE);
8039}
8040
8041/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008042 * "setenv()" function
8043 */
8044 static void
8045f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8046{
8047 char_u namebuf[NUMBUFLEN];
8048 char_u valbuf[NUMBUFLEN];
8049 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8050
8051 if (argvars[1].v_type == VAR_SPECIAL
8052 && argvars[1].vval.v_number == VVAL_NULL)
8053 vim_unsetenv(name);
8054 else
8055 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8056}
8057
8058/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 * "setfperm({fname}, {mode})" function
8060 */
8061 static void
8062f_setfperm(typval_T *argvars, typval_T *rettv)
8063{
8064 char_u *fname;
8065 char_u modebuf[NUMBUFLEN];
8066 char_u *mode_str;
8067 int i;
8068 int mask;
8069 int mode = 0;
8070
8071 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008072 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 if (fname == NULL)
8074 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008075 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076 if (mode_str == NULL)
8077 return;
8078 if (STRLEN(mode_str) != 9)
8079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008080 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 return;
8082 }
8083
8084 mask = 1;
8085 for (i = 8; i >= 0; --i)
8086 {
8087 if (mode_str[i] != '-')
8088 mode |= mask;
8089 mask = mask << 1;
8090 }
8091 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8092}
8093
8094/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 * "setpos()" function
8096 */
8097 static void
8098f_setpos(typval_T *argvars, typval_T *rettv)
8099{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008100 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008101}
8102
8103/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008104 * Translate a register type string to the yank type and block length
8105 */
8106 static int
8107get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8108{
8109 char_u *stropt = *pp;
8110 switch (*stropt)
8111 {
8112 case 'v': case 'c': // character-wise selection
8113 *yank_type = MCHAR;
8114 break;
8115 case 'V': case 'l': // line-wise selection
8116 *yank_type = MLINE;
8117 break;
8118 case 'b': case Ctrl_V: // block-wise selection
8119 *yank_type = MBLOCK;
8120 if (VIM_ISDIGIT(stropt[1]))
8121 {
8122 ++stropt;
8123 *block_len = getdigits(&stropt) - 1;
8124 --stropt;
8125 }
8126 break;
8127 default:
8128 return FAIL;
8129 }
8130 *pp = stropt;
8131 return OK;
8132}
8133
8134/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 * "setreg()" function
8136 */
8137 static void
8138f_setreg(typval_T *argvars, typval_T *rettv)
8139{
8140 int regname;
8141 char_u *strregname;
8142 char_u *stropt;
8143 char_u *strval;
8144 int append;
8145 char_u yank_type;
8146 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008147 typval_T *regcontents;
8148 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008150 pointreg = 0;
8151 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 block_len = -1;
8153 yank_type = MAUTO;
8154 append = FALSE;
8155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008156 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008157 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158
8159 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008160 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008161 if (in_vim9script() && STRLEN(strregname) > 1)
8162 {
8163 semsg(_(e_register_name_must_be_one_char_str), strregname);
8164 return;
8165 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 regname = *strregname;
8167 if (regname == 0 || regname == '@')
8168 regname = '"';
8169
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008170 if (argvars[1].v_type == VAR_DICT)
8171 {
8172 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008173 dictitem_T *di;
8174
8175 if (d == NULL || d->dv_hashtab.ht_used == 0)
8176 {
8177 // Empty dict, clear the register (like setreg(0, []))
8178 char_u *lstval[2] = {NULL, NULL};
8179 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8180 return;
8181 }
8182
8183 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008184 if (di != NULL)
8185 regcontents = &di->di_tv;
8186
8187 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8188 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008189 {
8190 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8191
8192 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008193 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008194 semsg(_(e_invargval), "value");
8195 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008196 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008197 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008198
8199 if (regname == '"')
8200 {
8201 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8202 if (stropt != NULL)
8203 {
8204 pointreg = *stropt;
8205 regname = pointreg;
8206 }
8207 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008208 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008209 pointreg = regname;
8210 }
8211 else
8212 regcontents = &argvars[1];
8213
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 if (argvars[2].v_type != VAR_UNKNOWN)
8215 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008216 if (yank_type != MAUTO)
8217 {
8218 semsg(_(e_toomanyarg), "setreg");
8219 return;
8220 }
8221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008222 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008224 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008225 for (; *stropt != NUL; ++stropt)
8226 switch (*stropt)
8227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008228 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008229 append = TRUE;
8230 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008231 default:
8232 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 }
8234 }
8235
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008236 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 {
8238 char_u **lstval;
8239 char_u **allocval;
8240 char_u buf[NUMBUFLEN];
8241 char_u **curval;
8242 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008243 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 listitem_T *li;
8245 int len;
8246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008247 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 len = ll == NULL ? 0 : ll->lv_len;
8249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008250 // First half: use for pointers to result lines; second half: use for
8251 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008252 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 if (lstval == NULL)
8254 return;
8255 curval = lstval;
8256 allocval = lstval + len + 2;
8257 curallocval = allocval;
8258
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008259 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008261 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008262 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008264 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008265 if (strval == NULL)
8266 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008267 if (strval == buf)
8268 {
8269 // Need to make a copy, next tv_get_string_buf_chk() will
8270 // overwrite the string.
8271 strval = vim_strsave(buf);
8272 if (strval == NULL)
8273 goto free_lstval;
8274 *curallocval++ = strval;
8275 }
8276 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008278 }
8279 *curval++ = NULL;
8280
8281 write_reg_contents_lst(regname, lstval, -1,
8282 append, yank_type, block_len);
8283free_lstval:
8284 while (curallocval > allocval)
8285 vim_free(*--curallocval);
8286 vim_free(lstval);
8287 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008288 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008290 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 if (strval == NULL)
8292 return;
8293 write_reg_contents_ex(regname, strval, -1,
8294 append, yank_type, block_len);
8295 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008296 if (pointreg != 0)
8297 get_yank_register(pointreg, TRUE);
8298
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299 rettv->vval.v_number = 0;
8300}
8301
8302/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008303 * "settagstack()" function
8304 */
8305 static void
8306f_settagstack(typval_T *argvars, typval_T *rettv)
8307{
8308 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8309 win_T *wp;
8310 dict_T *d;
8311 int action = 'r';
8312
8313 rettv->vval.v_number = -1;
8314
8315 // first argument: window number or id
8316 wp = find_win_by_nr_or_id(&argvars[0]);
8317 if (wp == NULL)
8318 return;
8319
8320 // second argument: dict with items to set in the tag stack
8321 if (argvars[1].v_type != VAR_DICT)
8322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008323 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008324 return;
8325 }
8326 d = argvars[1].vval.v_dict;
8327 if (d == NULL)
8328 return;
8329
8330 // third argument: action - 'a' for append and 'r' for replace.
8331 // default is to replace the stack.
8332 if (argvars[2].v_type == VAR_UNKNOWN)
8333 action = 'r';
8334 else if (argvars[2].v_type == VAR_STRING)
8335 {
8336 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008338 if (actstr == NULL)
8339 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008340 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8341 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008342 action = *actstr;
8343 else
8344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008345 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008346 return;
8347 }
8348 }
8349 else
8350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008351 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008352 return;
8353 }
8354
8355 if (set_tagstack(wp, d, action) == OK)
8356 rettv->vval.v_number = 0;
8357}
8358
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359#ifdef FEAT_CRYPT
8360/*
8361 * "sha256({string})" function
8362 */
8363 static void
8364f_sha256(typval_T *argvars, typval_T *rettv)
8365{
8366 char_u *p;
8367
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008368 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 rettv->vval.v_string = vim_strsave(
8370 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8371 rettv->v_type = VAR_STRING;
8372}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008373#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374
8375/*
8376 * "shellescape({string})" function
8377 */
8378 static void
8379f_shellescape(typval_T *argvars, typval_T *rettv)
8380{
Bram Moolenaar20615522017-06-05 18:46:26 +02008381 int do_special = non_zero_arg(&argvars[1]);
8382
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008383 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008384 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 rettv->v_type = VAR_STRING;
8386}
8387
8388/*
8389 * shiftwidth() function
8390 */
8391 static void
8392f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8393{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008394 rettv->vval.v_number = 0;
8395
8396 if (argvars[0].v_type != VAR_UNKNOWN)
8397 {
8398 long col;
8399
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008400 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008401 if (col < 0)
8402 return; // type error; errmsg already given
8403#ifdef FEAT_VARTABS
8404 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8405 return;
8406#endif
8407 }
8408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 rettv->vval.v_number = get_sw_value(curbuf);
8410}
8411
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008412/*
8413 * "soundfold({word})" function
8414 */
8415 static void
8416f_soundfold(typval_T *argvars, typval_T *rettv)
8417{
8418 char_u *s;
8419
8420 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008421 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422#ifdef FEAT_SPELL
8423 rettv->vval.v_string = eval_soundfold(s);
8424#else
8425 rettv->vval.v_string = vim_strsave(s);
8426#endif
8427}
8428
8429/*
8430 * "spellbadword()" function
8431 */
8432 static void
8433f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8434{
8435 char_u *word = (char_u *)"";
8436 hlf_T attr = HLF_COUNT;
8437 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008438#ifdef FEAT_SPELL
8439 int wo_spell_save = curwin->w_p_spell;
8440
8441 if (!curwin->w_p_spell)
8442 {
8443 did_set_spelllang(curwin);
8444 curwin->w_p_spell = TRUE;
8445 }
8446
8447 if (*curwin->w_s->b_p_spl == NUL)
8448 {
8449 emsg(_(e_no_spell));
8450 curwin->w_p_spell = wo_spell_save;
8451 return;
8452 }
8453#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008454
8455 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008456 {
8457#ifdef FEAT_SPELL
8458 curwin->w_p_spell = wo_spell_save;
8459#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008460 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008461 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008462
8463#ifdef FEAT_SPELL
8464 if (argvars[0].v_type == VAR_UNKNOWN)
8465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008466 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8468 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008469 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008471 curwin->w_set_curswant = TRUE;
8472 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008473 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008474 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008476 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008477 int capcol = -1;
8478
8479 if (str != NULL)
8480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008481 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 while (*str != NUL)
8483 {
8484 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8485 if (attr != HLF_COUNT)
8486 {
8487 word = str;
8488 break;
8489 }
8490 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008491 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008492 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 }
8494 }
8495 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008496 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497#endif
8498
8499 list_append_string(rettv->vval.v_list, word, len);
8500 list_append_string(rettv->vval.v_list, (char_u *)(
8501 attr == HLF_SPB ? "bad" :
8502 attr == HLF_SPR ? "rare" :
8503 attr == HLF_SPL ? "local" :
8504 attr == HLF_SPC ? "caps" :
8505 ""), -1);
8506}
8507
8508/*
8509 * "spellsuggest()" function
8510 */
8511 static void
8512f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8513{
8514#ifdef FEAT_SPELL
8515 char_u *str;
8516 int typeerr = FALSE;
8517 int maxcount;
8518 garray_T ga;
8519 int i;
8520 listitem_T *li;
8521 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008522 int wo_spell_save = curwin->w_p_spell;
8523
8524 if (!curwin->w_p_spell)
8525 {
8526 did_set_spelllang(curwin);
8527 curwin->w_p_spell = TRUE;
8528 }
8529
8530 if (*curwin->w_s->b_p_spl == NUL)
8531 {
8532 emsg(_(e_no_spell));
8533 curwin->w_p_spell = wo_spell_save;
8534 return;
8535 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536#endif
8537
8538 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008539 {
8540#ifdef FEAT_SPELL
8541 curwin->w_p_spell = wo_spell_save;
8542#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545
8546#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008547 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 if (argvars[1].v_type != VAR_UNKNOWN)
8551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008552 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 if (maxcount <= 0)
8554 return;
8555 if (argvars[2].v_type != VAR_UNKNOWN)
8556 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008557 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 if (typeerr)
8559 return;
8560 }
8561 }
8562 else
8563 maxcount = 25;
8564
8565 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8566
8567 for (i = 0; i < ga.ga_len; ++i)
8568 {
8569 str = ((char_u **)ga.ga_data)[i];
8570
8571 li = listitem_alloc();
8572 if (li == NULL)
8573 vim_free(str);
8574 else
8575 {
8576 li->li_tv.v_type = VAR_STRING;
8577 li->li_tv.v_lock = 0;
8578 li->li_tv.vval.v_string = str;
8579 list_append(rettv->vval.v_list, li);
8580 }
8581 }
8582 ga_clear(&ga);
8583 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008584 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585#endif
8586}
8587
8588 static void
8589f_split(typval_T *argvars, typval_T *rettv)
8590{
8591 char_u *str;
8592 char_u *end;
8593 char_u *pat = NULL;
8594 regmatch_T regmatch;
8595 char_u patbuf[NUMBUFLEN];
8596 char_u *save_cpo;
8597 int match;
8598 colnr_T col = 0;
8599 int keepempty = FALSE;
8600 int typeerr = FALSE;
8601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008602 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008604 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008606 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 if (argvars[1].v_type != VAR_UNKNOWN)
8608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008609 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 if (pat == NULL)
8611 typeerr = TRUE;
8612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008613 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 }
8615 if (pat == NULL || *pat == NUL)
8616 pat = (char_u *)"[\\x01- ]\\+";
8617
8618 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008619 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008621 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622
8623 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8624 if (regmatch.regprog != NULL)
8625 {
8626 regmatch.rm_ic = FALSE;
8627 while (*str != NUL || keepempty)
8628 {
8629 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008630 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 else
8632 match = vim_regexec_nl(&regmatch, str, col);
8633 if (match)
8634 end = regmatch.startp[0];
8635 else
8636 end = str + STRLEN(str);
8637 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8638 && *str != NUL && match && end < regmatch.endp[0]))
8639 {
8640 if (list_append_string(rettv->vval.v_list, str,
8641 (int)(end - str)) == FAIL)
8642 break;
8643 }
8644 if (!match)
8645 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008646 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 if (regmatch.endp[0] > str)
8648 col = 0;
8649 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008650 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 str = regmatch.endp[0];
8653 }
8654
8655 vim_regfree(regmatch.regprog);
8656 }
8657
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008658theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 p_cpo = save_cpo;
8660}
8661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008662/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008663 * "str2list()" function
8664 */
8665 static void
8666f_str2list(typval_T *argvars, typval_T *rettv)
8667{
8668 char_u *p;
8669 int utf8 = FALSE;
8670
8671 if (rettv_list_alloc(rettv) == FAIL)
8672 return;
8673
8674 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008675 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008676
8677 p = tv_get_string(&argvars[0]);
8678
8679 if (has_mbyte || utf8)
8680 {
8681 int (*ptr2len)(char_u *);
8682 int (*ptr2char)(char_u *);
8683
8684 if (utf8 || enc_utf8)
8685 {
8686 ptr2len = utf_ptr2len;
8687 ptr2char = utf_ptr2char;
8688 }
8689 else
8690 {
8691 ptr2len = mb_ptr2len;
8692 ptr2char = mb_ptr2char;
8693 }
8694
8695 for ( ; *p != NUL; p += (*ptr2len)(p))
8696 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8697 }
8698 else
8699 for ( ; *p != NUL; ++p)
8700 list_append_number(rettv->vval.v_list, *p);
8701}
8702
8703/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704 * "str2nr()" function
8705 */
8706 static void
8707f_str2nr(typval_T *argvars, typval_T *rettv)
8708{
8709 int base = 10;
8710 char_u *p;
8711 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008712 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008713 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714
8715 if (argvars[1].v_type != VAR_UNKNOWN)
8716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008717 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 if (base != 2 && base != 8 && base != 10 && base != 16)
8719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008720 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 return;
8722 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008723 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008724 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 }
8726
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008727 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008728 isneg = (*p == '-');
8729 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730 p = skipwhite(p + 1);
8731 switch (base)
8732 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008733 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008734 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008735 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008737 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8738 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008739 if (isneg)
8740 rettv->vval.v_number = -n;
8741 else
8742 rettv->vval.v_number = n;
8743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744}
8745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746/*
8747 * "strgetchar()" function
8748 */
8749 static void
8750f_strgetchar(typval_T *argvars, typval_T *rettv)
8751{
8752 char_u *str;
8753 int len;
8754 int error = FALSE;
8755 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008756 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757
8758 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008759 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 if (str == NULL)
8761 return;
8762 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008763 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 if (error)
8765 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766
Bram Moolenaar13505972019-01-24 15:04:48 +01008767 while (charidx >= 0 && byteidx < len)
8768 {
8769 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008771 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8772 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008774 --charidx;
8775 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777}
8778
8779/*
8780 * "stridx()" function
8781 */
8782 static void
8783f_stridx(typval_T *argvars, typval_T *rettv)
8784{
8785 char_u buf[NUMBUFLEN];
8786 char_u *needle;
8787 char_u *haystack;
8788 char_u *save_haystack;
8789 char_u *pos;
8790 int start_idx;
8791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008792 needle = tv_get_string_chk(&argvars[1]);
8793 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 rettv->vval.v_number = -1;
8795 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008796 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797
8798 if (argvars[2].v_type != VAR_UNKNOWN)
8799 {
8800 int error = FALSE;
8801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008802 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803 if (error || start_idx >= (int)STRLEN(haystack))
8804 return;
8805 if (start_idx >= 0)
8806 haystack += start_idx;
8807 }
8808
8809 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8810 if (pos != NULL)
8811 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8812}
8813
8814/*
8815 * "string()" function
8816 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008817 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818f_string(typval_T *argvars, typval_T *rettv)
8819{
8820 char_u *tofree;
8821 char_u numbuf[NUMBUFLEN];
8822
8823 rettv->v_type = VAR_STRING;
8824 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8825 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008826 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827 if (rettv->vval.v_string != NULL && tofree == NULL)
8828 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8829}
8830
8831/*
8832 * "strlen()" function
8833 */
8834 static void
8835f_strlen(typval_T *argvars, typval_T *rettv)
8836{
8837 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008838 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839}
8840
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008841 static void
8842strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8843{
8844 char_u *s = tv_get_string(&argvars[0]);
8845 varnumber_T len = 0;
8846 int (*func_mb_ptr2char_adv)(char_u **pp);
8847
8848 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8849 while (*s != NUL)
8850 {
8851 func_mb_ptr2char_adv(&s);
8852 ++len;
8853 }
8854 rettv->vval.v_number = len;
8855}
8856
8857/*
8858 * "strcharlen()" function
8859 */
8860 static void
8861f_strcharlen(typval_T *argvars, typval_T *rettv)
8862{
8863 strchar_common(argvars, rettv, TRUE);
8864}
8865
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866/*
8867 * "strchars()" function
8868 */
8869 static void
8870f_strchars(typval_T *argvars, typval_T *rettv)
8871{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008872 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873
8874 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008875 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008877 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008879 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880}
8881
8882/*
8883 * "strdisplaywidth()" function
8884 */
8885 static void
8886f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8887{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008888 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 int col = 0;
8890
8891 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008892 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008893
8894 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8895}
8896
8897/*
8898 * "strwidth()" function
8899 */
8900 static void
8901f_strwidth(typval_T *argvars, typval_T *rettv)
8902{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008903 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904
Bram Moolenaar13505972019-01-24 15:04:48 +01008905 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906}
8907
8908/*
8909 * "strcharpart()" function
8910 */
8911 static void
8912f_strcharpart(typval_T *argvars, typval_T *rettv)
8913{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 char_u *p;
8915 int nchar;
8916 int nbyte = 0;
8917 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008918 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 int len = 0;
8920 int slen;
8921 int error = FALSE;
8922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 slen = (int)STRLEN(p);
8925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008926 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 if (!error)
8928 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008929 if (argvars[2].v_type != VAR_UNKNOWN
8930 && argvars[3].v_type != VAR_UNKNOWN)
8931 {
8932 skipcc = tv_get_bool(&argvars[3]);
8933 if (skipcc < 0 || skipcc > 1)
8934 {
8935 semsg(_(e_using_number_as_bool_nr), skipcc);
8936 return;
8937 }
8938 }
8939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 if (nchar > 0)
8941 while (nchar > 0 && nbyte < slen)
8942 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008943 if (skipcc)
8944 nbyte += mb_ptr2len(p + nbyte);
8945 else
8946 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 --nchar;
8948 }
8949 else
8950 nbyte = nchar;
8951 if (argvars[2].v_type != VAR_UNKNOWN)
8952 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008953 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 while (charlen > 0 && nbyte + len < slen)
8955 {
8956 int off = nbyte + len;
8957
8958 if (off < 0)
8959 len += 1;
8960 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008961 {
8962 if (skipcc)
8963 len += mb_ptr2len(p + off);
8964 else
8965 len += MB_CPTR2LEN(p + off);
8966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 --charlen;
8968 }
8969 }
8970 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008971 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 }
8973
8974 /*
8975 * Only return the overlap between the specified part and the actual
8976 * string.
8977 */
8978 if (nbyte < 0)
8979 {
8980 len += nbyte;
8981 nbyte = 0;
8982 }
8983 else if (nbyte > slen)
8984 nbyte = slen;
8985 if (len < 0)
8986 len = 0;
8987 else if (nbyte + len > slen)
8988 len = slen - nbyte;
8989
8990 rettv->v_type = VAR_STRING;
8991 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992}
8993
8994/*
8995 * "strpart()" function
8996 */
8997 static void
8998f_strpart(typval_T *argvars, typval_T *rettv)
8999{
9000 char_u *p;
9001 int n;
9002 int len;
9003 int slen;
9004 int error = FALSE;
9005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009006 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 slen = (int)STRLEN(p);
9008
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009009 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 if (error)
9011 len = 0;
9012 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009013 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009015 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009017 // Only return the overlap between the specified part and the actual
9018 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (n < 0)
9020 {
9021 len += n;
9022 n = 0;
9023 }
9024 else if (n > slen)
9025 n = slen;
9026 if (len < 0)
9027 len = 0;
9028 else if (n + len > slen)
9029 len = slen - n;
9030
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009031 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9032 {
9033 int off;
9034
9035 // length in characters
9036 for (off = n; off < slen && len > 0; --len)
9037 off += mb_ptr2len(p + off);
9038 len = off - n;
9039 }
9040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 rettv->v_type = VAR_STRING;
9042 rettv->vval.v_string = vim_strnsave(p + n, len);
9043}
9044
9045/*
9046 * "strridx()" function
9047 */
9048 static void
9049f_strridx(typval_T *argvars, typval_T *rettv)
9050{
9051 char_u buf[NUMBUFLEN];
9052 char_u *needle;
9053 char_u *haystack;
9054 char_u *rest;
9055 char_u *lastmatch = NULL;
9056 int haystack_len, end_idx;
9057
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009058 needle = tv_get_string_chk(&argvars[1]);
9059 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060
9061 rettv->vval.v_number = -1;
9062 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009063 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064
9065 haystack_len = (int)STRLEN(haystack);
9066 if (argvars[2].v_type != VAR_UNKNOWN)
9067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009068 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009071 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 }
9073 else
9074 end_idx = haystack_len;
9075
9076 if (*needle == NUL)
9077 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009078 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 lastmatch = haystack + end_idx;
9080 }
9081 else
9082 {
9083 for (rest = haystack; *rest != '\0'; ++rest)
9084 {
9085 rest = (char_u *)strstr((char *)rest, (char *)needle);
9086 if (rest == NULL || rest > haystack + end_idx)
9087 break;
9088 lastmatch = rest;
9089 }
9090 }
9091
9092 if (lastmatch == NULL)
9093 rettv->vval.v_number = -1;
9094 else
9095 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9096}
9097
9098/*
9099 * "strtrans()" function
9100 */
9101 static void
9102f_strtrans(typval_T *argvars, typval_T *rettv)
9103{
9104 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009105 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106}
9107
9108/*
9109 * "submatch()" function
9110 */
9111 static void
9112f_submatch(typval_T *argvars, typval_T *rettv)
9113{
9114 int error = FALSE;
9115 int no;
9116 int retList = 0;
9117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009118 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 if (error)
9120 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009121 if (no < 0 || no >= NSUBEXP)
9122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009123 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009124 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009125 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009127 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009128 if (error)
9129 return;
9130
9131 if (retList == 0)
9132 {
9133 rettv->v_type = VAR_STRING;
9134 rettv->vval.v_string = reg_submatch(no);
9135 }
9136 else
9137 {
9138 rettv->v_type = VAR_LIST;
9139 rettv->vval.v_list = reg_submatch_list(no);
9140 }
9141}
9142
9143/*
9144 * "substitute()" function
9145 */
9146 static void
9147f_substitute(typval_T *argvars, typval_T *rettv)
9148{
9149 char_u patbuf[NUMBUFLEN];
9150 char_u subbuf[NUMBUFLEN];
9151 char_u flagsbuf[NUMBUFLEN];
9152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009153 char_u *str = tv_get_string_chk(&argvars[0]);
9154 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009155 char_u *sub = NULL;
9156 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009157 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009159 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9160 expr = &argvars[2];
9161 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009162 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009164 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009165 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9166 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167 rettv->vval.v_string = NULL;
9168 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009169 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009170}
9171
9172/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009173 * "swapinfo(swap_filename)" function
9174 */
9175 static void
9176f_swapinfo(typval_T *argvars, typval_T *rettv)
9177{
9178 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009179 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009180}
9181
9182/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009183 * "swapname(expr)" function
9184 */
9185 static void
9186f_swapname(typval_T *argvars, typval_T *rettv)
9187{
9188 buf_T *buf;
9189
9190 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009191 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009192 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9193 || buf->b_ml.ml_mfp->mf_fname == NULL)
9194 rettv->vval.v_string = NULL;
9195 else
9196 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9197}
9198
9199/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 * "synID(lnum, col, trans)" function
9201 */
9202 static void
9203f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9204{
9205 int id = 0;
9206#ifdef FEAT_SYN_HL
9207 linenr_T lnum;
9208 colnr_T col;
9209 int trans;
9210 int transerr = FALSE;
9211
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009212 lnum = tv_get_lnum(argvars); // -1 on type error
9213 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009214 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215
9216 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9217 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9218 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9219#endif
9220
9221 rettv->vval.v_number = id;
9222}
9223
9224/*
9225 * "synIDattr(id, what [, mode])" function
9226 */
9227 static void
9228f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9229{
9230 char_u *p = NULL;
9231#ifdef FEAT_SYN_HL
9232 int id;
9233 char_u *what;
9234 char_u *mode;
9235 char_u modebuf[NUMBUFLEN];
9236 int modec;
9237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009238 id = (int)tv_get_number(&argvars[0]);
9239 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 if (argvars[2].v_type != VAR_UNKNOWN)
9241 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009242 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 modec = TOLOWER_ASC(mode[0]);
9244 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009245 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009246 }
9247 else
9248 {
9249#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9250 if (USE_24BIT)
9251 modec = 'g';
9252 else
9253#endif
9254 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009255 modec = 'c';
9256 else
9257 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 }
9259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 switch (TOLOWER_ASC(what[0]))
9261 {
9262 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009263 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009265 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 p = highlight_has_attr(id, HL_BOLD, modec);
9267 break;
9268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009269 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009270 p = highlight_color(id, what, modec);
9271 break;
9272
9273 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009274 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009276 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 p = highlight_has_attr(id, HL_ITALIC, modec);
9278 break;
9279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009280 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009281 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 break;
9283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009284 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285 p = highlight_has_attr(id, HL_INVERSE, modec);
9286 break;
9287
9288 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009289 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009290 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009291 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009292 else if (TOLOWER_ASC(what[1]) == 't' &&
9293 TOLOWER_ASC(what[2]) == 'r')
9294 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009295 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009296 p = highlight_has_attr(id, HL_STANDOUT, modec);
9297 break;
9298
9299 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009300 if (TOLOWER_ASC(what[1]) == 'l') // ul
9301 p = highlight_color(id, what, modec);
9302 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009303 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9305 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009306 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009307 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9308 break;
9309 }
9310
9311 if (p != NULL)
9312 p = vim_strsave(p);
9313#endif
9314 rettv->v_type = VAR_STRING;
9315 rettv->vval.v_string = p;
9316}
9317
9318/*
9319 * "synIDtrans(id)" function
9320 */
9321 static void
9322f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9323{
9324 int id;
9325
9326#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009327 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009328
9329 if (id > 0)
9330 id = syn_get_final_id(id);
9331 else
9332#endif
9333 id = 0;
9334
9335 rettv->vval.v_number = id;
9336}
9337
9338/*
9339 * "synconcealed(lnum, col)" function
9340 */
9341 static void
9342f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9343{
9344#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9345 linenr_T lnum;
9346 colnr_T col;
9347 int syntax_flags = 0;
9348 int cchar;
9349 int matchid = 0;
9350 char_u str[NUMBUFLEN];
9351#endif
9352
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009353 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354
9355#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009356 lnum = tv_get_lnum(argvars); // -1 on type error
9357 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358
Bram Moolenaara80faa82020-04-12 19:37:17 +02009359 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360
9361 if (rettv_list_alloc(rettv) != FAIL)
9362 {
9363 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9364 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9365 && curwin->w_p_cole > 0)
9366 {
9367 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9368 syntax_flags = get_syntax_info(&matchid);
9369
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009370 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9372 {
9373 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009374 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009375 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9376 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009377 if (cchar != NUL)
9378 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 if (has_mbyte)
9380 (*mb_char2bytes)(cchar, str);
9381 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382 str[0] = cchar;
9383 }
9384 }
9385 }
9386
9387 list_append_number(rettv->vval.v_list,
9388 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009389 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 list_append_string(rettv->vval.v_list, str, -1);
9391 list_append_number(rettv->vval.v_list, matchid);
9392 }
9393#endif
9394}
9395
9396/*
9397 * "synstack(lnum, col)" function
9398 */
9399 static void
9400f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9401{
9402#ifdef FEAT_SYN_HL
9403 linenr_T lnum;
9404 colnr_T col;
9405 int i;
9406 int id;
9407#endif
9408
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009409 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410
9411#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009412 lnum = tv_get_lnum(argvars); // -1 on type error
9413 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414
9415 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9416 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9417 && rettv_list_alloc(rettv) != FAIL)
9418 {
9419 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9420 for (i = 0; ; ++i)
9421 {
9422 id = syn_get_stack_item(i);
9423 if (id < 0)
9424 break;
9425 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9426 break;
9427 }
9428 }
9429#endif
9430}
9431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432/*
9433 * "tabpagebuflist()" function
9434 */
9435 static void
9436f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9437{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009438 tabpage_T *tp;
9439 win_T *wp = NULL;
9440
9441 if (argvars[0].v_type == VAR_UNKNOWN)
9442 wp = firstwin;
9443 else
9444 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009445 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 if (tp != NULL)
9447 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9448 }
9449 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9450 {
9451 for (; wp != NULL; wp = wp->w_next)
9452 if (list_append_number(rettv->vval.v_list,
9453 wp->w_buffer->b_fnum) == FAIL)
9454 break;
9455 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456}
9457
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 * "tagfiles()" function
9460 */
9461 static void
9462f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9463{
9464 char_u *fname;
9465 tagname_T tn;
9466 int first;
9467
9468 if (rettv_list_alloc(rettv) == FAIL)
9469 return;
9470 fname = alloc(MAXPATHL);
9471 if (fname == NULL)
9472 return;
9473
9474 for (first = TRUE; ; first = FALSE)
9475 if (get_tagfname(&tn, first, fname) == FAIL
9476 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9477 break;
9478 tagname_free(&tn);
9479 vim_free(fname);
9480}
9481
9482/*
9483 * "taglist()" function
9484 */
9485 static void
9486f_taglist(typval_T *argvars, typval_T *rettv)
9487{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009488 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489 char_u *tag_pattern;
9490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009491 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492
9493 rettv->vval.v_number = FALSE;
9494 if (*tag_pattern == NUL)
9495 return;
9496
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009498 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009500 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501}
9502
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504 * "tolower(string)" function
9505 */
9506 static void
9507f_tolower(typval_T *argvars, typval_T *rettv)
9508{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009509 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009510 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511}
9512
9513/*
9514 * "toupper(string)" function
9515 */
9516 static void
9517f_toupper(typval_T *argvars, typval_T *rettv)
9518{
9519 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009520 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521}
9522
9523/*
9524 * "tr(string, fromstr, tostr)" function
9525 */
9526 static void
9527f_tr(typval_T *argvars, typval_T *rettv)
9528{
9529 char_u *in_str;
9530 char_u *fromstr;
9531 char_u *tostr;
9532 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009533 int inlen;
9534 int fromlen;
9535 int tolen;
9536 int idx;
9537 char_u *cpstr;
9538 int cplen;
9539 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540 char_u buf[NUMBUFLEN];
9541 char_u buf2[NUMBUFLEN];
9542 garray_T ga;
9543
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 in_str = tv_get_string(&argvars[0]);
9545 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9546 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009548 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009549 rettv->v_type = VAR_STRING;
9550 rettv->vval.v_string = NULL;
9551 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009552 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 ga_init2(&ga, (int)sizeof(char), 80);
9554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009556 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 if (STRLEN(fromstr) != STRLEN(tostr))
9558 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009560 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009561 ga_clear(&ga);
9562 return;
9563 }
9564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009565 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 while (*in_str != NUL)
9567 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 if (has_mbyte)
9569 {
9570 inlen = (*mb_ptr2len)(in_str);
9571 cpstr = in_str;
9572 cplen = inlen;
9573 idx = 0;
9574 for (p = fromstr; *p != NUL; p += fromlen)
9575 {
9576 fromlen = (*mb_ptr2len)(p);
9577 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9578 {
9579 for (p = tostr; *p != NUL; p += tolen)
9580 {
9581 tolen = (*mb_ptr2len)(p);
9582 if (idx-- == 0)
9583 {
9584 cplen = tolen;
9585 cpstr = p;
9586 break;
9587 }
9588 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009589 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 goto error;
9591 break;
9592 }
9593 ++idx;
9594 }
9595
9596 if (first && cpstr == in_str)
9597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009598 // Check that fromstr and tostr have the same number of
9599 // (multi-byte) characters. Done only once when a character
9600 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 first = FALSE;
9602 for (p = tostr; *p != NUL; p += tolen)
9603 {
9604 tolen = (*mb_ptr2len)(p);
9605 --idx;
9606 }
9607 if (idx != 0)
9608 goto error;
9609 }
9610
9611 (void)ga_grow(&ga, cplen);
9612 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9613 ga.ga_len += cplen;
9614
9615 in_str += inlen;
9616 }
9617 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009619 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620 p = vim_strchr(fromstr, *in_str);
9621 if (p != NULL)
9622 ga_append(&ga, tostr[p - fromstr]);
9623 else
9624 ga_append(&ga, *in_str);
9625 ++in_str;
9626 }
9627 }
9628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009629 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 (void)ga_grow(&ga, 1);
9631 ga_append(&ga, NUL);
9632
9633 rettv->vval.v_string = ga.ga_data;
9634}
9635
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009636/*
9637 * "trim({expr})" function
9638 */
9639 static void
9640f_trim(typval_T *argvars, typval_T *rettv)
9641{
9642 char_u buf1[NUMBUFLEN];
9643 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009644 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009645 char_u *mask = NULL;
9646 char_u *tail;
9647 char_u *prev;
9648 char_u *p;
9649 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009650 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009651
9652 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009653 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009654 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009655 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009656
9657 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009658 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009659 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009660
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009662 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009663 int error = 0;
9664
9665 // leading or trailing characters to trim
9666 dir = (int)tv_get_number_chk(&argvars[2], &error);
9667 if (error)
9668 return;
9669 if (dir < 0 || dir > 2)
9670 {
9671 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9672 return;
9673 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009674 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009675 }
9676
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009677 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009678 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009679 // Trim leading characters
9680 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009681 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009682 c1 = PTR2CHAR(head);
9683 if (mask == NULL)
9684 {
9685 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009686 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009687 }
9688 else
9689 {
9690 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9691 if (c1 == PTR2CHAR(p))
9692 break;
9693 if (*p == NUL)
9694 break;
9695 }
9696 MB_PTR_ADV(head);
9697 }
9698 }
9699
9700 tail = head + STRLEN(head);
9701 if (dir == 0 || dir == 2)
9702 {
9703 // Trim trailing characters
9704 for (; tail > head; tail = prev)
9705 {
9706 prev = tail;
9707 MB_PTR_BACK(head, prev);
9708 c1 = PTR2CHAR(prev);
9709 if (mask == NULL)
9710 {
9711 if (c1 > ' ' && c1 != 0xa0)
9712 break;
9713 }
9714 else
9715 {
9716 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9717 if (c1 == PTR2CHAR(p))
9718 break;
9719 if (*p == NUL)
9720 break;
9721 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009722 }
9723 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009724 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009725}
9726
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009727/*
9728 * "type(expr)" function
9729 */
9730 static void
9731f_type(typval_T *argvars, typval_T *rettv)
9732{
9733 int n = -1;
9734
9735 switch (argvars[0].v_type)
9736 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009737 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9738 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009740 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9741 case VAR_LIST: n = VAR_TYPE_LIST; break;
9742 case VAR_DICT: n = VAR_TYPE_DICT; break;
9743 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9744 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9745 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009746 case VAR_JOB: n = VAR_TYPE_JOB; break;
9747 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009748 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009749 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009750 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009751 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009752 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009753 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754 n = -1;
9755 break;
9756 }
9757 rettv->vval.v_number = n;
9758}
9759
9760/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761 * "virtcol(string)" function
9762 */
9763 static void
9764f_virtcol(typval_T *argvars, typval_T *rettv)
9765{
9766 colnr_T vcol = 0;
9767 pos_T *fp;
9768 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009769 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009771 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9773 && fnum == curbuf->b_fnum)
9774 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009775 // Limit the column to a valid value, getvvcol() doesn't check.
9776 if (fp->col < 0)
9777 fp->col = 0;
9778 else
9779 {
9780 len = (int)STRLEN(ml_get(fp->lnum));
9781 if (fp->col > len)
9782 fp->col = len;
9783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 getvvcol(curwin, fp, NULL, NULL, &vcol);
9785 ++vcol;
9786 }
9787
9788 rettv->vval.v_number = vcol;
9789}
9790
9791/*
9792 * "visualmode()" function
9793 */
9794 static void
9795f_visualmode(typval_T *argvars, typval_T *rettv)
9796{
9797 char_u str[2];
9798
9799 rettv->v_type = VAR_STRING;
9800 str[0] = curbuf->b_visual_mode_eval;
9801 str[1] = NUL;
9802 rettv->vval.v_string = vim_strsave(str);
9803
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009804 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805 if (non_zero_arg(&argvars[0]))
9806 curbuf->b_visual_mode_eval = NUL;
9807}
9808
9809/*
9810 * "wildmenumode()" function
9811 */
9812 static void
9813f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9814{
9815#ifdef FEAT_WILDMENU
9816 if (wild_menu_showing)
9817 rettv->vval.v_number = 1;
9818#endif
9819}
9820
9821/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009822 * "windowsversion()" function
9823 */
9824 static void
9825f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9826{
9827 rettv->v_type = VAR_STRING;
9828 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9829}
9830
9831/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832 * "wordcount()" function
9833 */
9834 static void
9835f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9836{
9837 if (rettv_dict_alloc(rettv) == FAIL)
9838 return;
9839 cursor_pos_info(rettv->vval.v_dict);
9840}
9841
9842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843 * "xor(expr, expr)" function
9844 */
9845 static void
9846f_xor(typval_T *argvars, typval_T *rettv)
9847{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009848 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9849 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850}
9851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009852#endif // FEAT_EVAL