blob: 1962ccd15aef54dbe3aa134e314f0d46ff2adb6e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010024#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020025static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010026static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010027# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010028static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010029# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031static void f_byte2line(typval_T *argvars, typval_T *rettv);
32static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
33static void f_byteidx(typval_T *argvars, typval_T *rettv);
34static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
35static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020036static void f_changenr(typval_T *argvars, typval_T *rettv);
37static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010038static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010039static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020041static void f_confirm(typval_T *argvars, typval_T *rettv);
42static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010044#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020045static void f_debugbreak(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010049static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020051static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_escape(typval_T *argvars, typval_T *rettv);
53static void f_eval(typval_T *argvars, typval_T *rettv);
54static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020058static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020062static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_function(typval_T *argvars, typval_T *rettv);
64static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
65static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010066static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010067static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020069static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010071static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_getpid(typval_T *argvars, typval_T *rettv);
73static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010074static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020077static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010079static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +020080static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020081static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
82static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_hlID(typval_T *argvars, typval_T *rettv);
84static void f_hlexists(typval_T *argvars, typval_T *rettv);
85static void f_hostname(typval_T *argvars, typval_T *rettv);
86static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_index(typval_T *argvars, typval_T *rettv);
88static void f_input(typval_T *argvars, typval_T *rettv);
89static void f_inputdialog(typval_T *argvars, typval_T *rettv);
90static void f_inputlist(typval_T *argvars, typval_T *rettv);
91static void f_inputrestore(typval_T *argvars, typval_T *rettv);
92static void f_inputsave(typval_T *argvars, typval_T *rettv);
93static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +010094static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020095static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020097static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
98static void f_len(typval_T *argvars, typval_T *rettv);
99static void f_libcall(typval_T *argvars, typval_T *rettv);
100static void f_libcallnr(typval_T *argvars, typval_T *rettv);
101static void f_line(typval_T *argvars, typval_T *rettv);
102static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103#ifdef FEAT_LUA
104static void f_luaeval(typval_T *argvars, typval_T *rettv);
105#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_maparg(typval_T *argvars, typval_T *rettv);
107static void f_mapcheck(typval_T *argvars, typval_T *rettv);
108static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200109static void f_matchend(typval_T *argvars, typval_T *rettv);
110static void f_matchlist(typval_T *argvars, typval_T *rettv);
111static void f_matchstr(typval_T *argvars, typval_T *rettv);
112static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
113static void f_max(typval_T *argvars, typval_T *rettv);
114static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115#ifdef FEAT_MZSCHEME
116static void f_mzeval(typval_T *argvars, typval_T *rettv);
117#endif
118static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
119static void f_nr2char(typval_T *argvars, typval_T *rettv);
120static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121#ifdef FEAT_PERL
122static void f_perleval(typval_T *argvars, typval_T *rettv);
123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200124static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
125static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200126static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_pumvisible(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_PYTHON3
129static void f_py3eval(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_PYTHON
132static void f_pyeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100134#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
135static void f_pyxeval(typval_T *argvars, typval_T *rettv);
136#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100137static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100138static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200139static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200140static void f_reg_executing(typval_T *argvars, typval_T *rettv);
141static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142static void f_rename(typval_T *argvars, typval_T *rettv);
143static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100144#ifdef FEAT_RUBY
145static void f_rubyeval(typval_T *argvars, typval_T *rettv);
146#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147static void f_screenattr(typval_T *argvars, typval_T *rettv);
148static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100149static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200150static void f_screencol(typval_T *argvars, typval_T *rettv);
151static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100152static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_search(typval_T *argvars, typval_T *rettv);
154static void f_searchdecl(typval_T *argvars, typval_T *rettv);
155static void f_searchpair(typval_T *argvars, typval_T *rettv);
156static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
157static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100158static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100160static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200161static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100165static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166#ifdef FEAT_CRYPT
167static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_shellescape(typval_T *argvars, typval_T *rettv);
170static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_soundfold(typval_T *argvars, typval_T *rettv);
172static void f_spellbadword(typval_T *argvars, typval_T *rettv);
173static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
174static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100175static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200176static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_str2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100178static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_strgetchar(typval_T *argvars, typval_T *rettv);
181static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_strlen(typval_T *argvars, typval_T *rettv);
183static void f_strcharpart(typval_T *argvars, typval_T *rettv);
184static void f_strpart(typval_T *argvars, typval_T *rettv);
185static void f_strridx(typval_T *argvars, typval_T *rettv);
186static void f_strtrans(typval_T *argvars, typval_T *rettv);
187static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
188static void f_strwidth(typval_T *argvars, typval_T *rettv);
189static void f_submatch(typval_T *argvars, typval_T *rettv);
190static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200191static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200192static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_synID(typval_T *argvars, typval_T *rettv);
194static void f_synIDattr(typval_T *argvars, typval_T *rettv);
195static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
196static void f_synstack(typval_T *argvars, typval_T *rettv);
197static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_taglist(typval_T *argvars, typval_T *rettv);
200static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_tolower(typval_T *argvars, typval_T *rettv);
202static void f_toupper(typval_T *argvars, typval_T *rettv);
203static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100204static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_virtcol(typval_T *argvars, typval_T *rettv);
207static void f_visualmode(typval_T *argvars, typval_T *rettv);
208static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100209static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200210static void f_wordcount(typval_T *argvars, typval_T *rettv);
211static void f_xor(typval_T *argvars, typval_T *rettv);
212
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100213
Bram Moolenaar94738d82020-10-21 14:25:07 +0200214/*
215 * Functions that check the argument type of a builtin function.
216 * Each function returns FAIL and gives an error message if the type is wrong.
217 */
218
219// Context passed to an arg_ function.
220typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200221 int arg_count; // actual argument count
222 type_T **arg_types; // list of argument types
223 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100224 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200225} argcontext_T;
226
227// A function to check one argument type. The first argument is the type to
228// check. If needed, other argument types can be obtained with the context.
229// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
230typedef int (*argcheck_T)(type_T *, argcontext_T *);
231
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100232/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100233 * Call need_type() to check an argument type.
234 */
235 static int
236check_arg_type(
237 type_T *expected,
238 type_T *actual,
239 argcontext_T *context)
240{
241 // TODO: would be useful to know if "actual" is a constant and pass it to
242 // need_type() to get a compile time error if possible.
243 return need_type(actual, expected,
244 context->arg_idx - context->arg_count, context->arg_idx + 1,
245 context->arg_cctx, FALSE, FALSE);
246}
247
248/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100249 * Check "type" is a float or a number.
250 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200251 static int
252arg_float_or_nr(type_T *type, argcontext_T *context)
253{
Bram Moolenaarca174532020-10-21 16:42:22 +0200254 if (type->tt_type == VAR_ANY
255 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200256 return OK;
257 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
258 return FAIL;
259}
260
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100261/*
262 * Check "type" is a number.
263 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200264 static int
265arg_number(type_T *type, argcontext_T *context)
266{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100267 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200268}
269
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100270/*
271 * Check "type" is a string.
272 */
273 static int
274arg_string(type_T *type, argcontext_T *context)
275{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100276 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100277}
278
279/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100280 * Check "type" is a bool or number 0 or 1.
281 */
282 static int
283arg_bool(type_T *type, argcontext_T *context)
284{
285 if (type->tt_type == VAR_ANY
286 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
287 return OK;
288 return check_arg_type(&t_bool, type, context);
289}
290
291/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100292 * Check "type" is a list or a blob.
293 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200294 static int
295arg_list_or_blob(type_T *type, argcontext_T *context)
296{
297 if (type->tt_type == VAR_ANY
298 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
299 return OK;
300 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
301 return FAIL;
302}
303
304/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100305 * Check "type" is a list or a dict.
306 */
307 static int
308arg_list_or_dict(type_T *type, argcontext_T *context)
309{
310 if (type->tt_type == VAR_ANY
311 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
312 return OK;
313 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
314 return FAIL;
315}
316
317/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100318 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100319 * Must not be used for the first argcheck_T entry.
320 */
321 static int
322arg_same_as_prev(type_T *type, argcontext_T *context)
323{
324 type_T *prev_type = context->arg_types[context->arg_idx - 1];
325
Bram Moolenaar351ead02021-01-16 16:07:01 +0100326 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100327}
328
329/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100330 * Check "type" is the same basic type as the previous argument, checks list or
331 * dict vs other type, but not member type.
332 * Must not be used for the first argcheck_T entry.
333 */
334 static int
335arg_same_struct_as_prev(type_T *type, argcontext_T *context)
336{
337 type_T *prev_type = context->arg_types[context->arg_idx - 1];
338
339 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100340 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100341 return OK;
342}
343
344/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100345 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200346 * Must not be used for the first argcheck_T entry.
347 */
348 static int
349arg_item_of_prev(type_T *type, argcontext_T *context)
350{
351 type_T *prev_type = context->arg_types[context->arg_idx - 1];
352 type_T *expected;
353
354 if (prev_type->tt_type == VAR_LIST)
355 expected = prev_type->tt_member;
356 else if (prev_type->tt_type == VAR_BLOB)
357 expected = &t_number;
358 else
359 // probably VAR_ANY, can't check
360 return OK;
361
Bram Moolenaar351ead02021-01-16 16:07:01 +0100362 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200363}
364
Bram Moolenaar94738d82020-10-21 14:25:07 +0200365/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100366 * Check "type" which is the third argument of extend().
367 */
368 static int
369arg_extend3(type_T *type, argcontext_T *context)
370{
371 type_T *first_type = context->arg_types[context->arg_idx - 2];
372
373 if (first_type->tt_type == VAR_LIST)
374 return arg_number(type, context);
375 if (first_type->tt_type == VAR_DICT)
376 return arg_string(type, context);
377 return OK;
378}
379
380
381/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200382 * Lists of functions that check the argument types of a builtin function.
383 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100384argcheck_T arg1_string[] = {arg_string};
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100385argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200386argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
388argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100389argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Bram Moolenaarca174532020-10-21 16:42:22 +0200390argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200391
392/*
393 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200394 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200395 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100396 static type_T *
397ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
398{
399 return &t_void;
400}
401 static type_T *
402ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
403{
404 return &t_any;
405}
406 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200407ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
408{
409 return &t_bool;
410}
411 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100412ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
413{
414 return &t_number_bool;
415}
416 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100417ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
418{
419 return &t_number;
420}
421 static type_T *
422ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
423{
424 return &t_float;
425}
426 static type_T *
427ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
428{
429 return &t_string;
430}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200431 static type_T *
432ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100433{
434 return &t_list_any;
435}
436 static type_T *
437ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
438{
439 return &t_list_number;
440}
441 static type_T *
442ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
443{
444 return &t_list_string;
445}
446 static type_T *
447ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
448{
449 return &t_list_dict_any;
450}
451 static type_T *
452ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
453{
454 return &t_dict_any;
455}
456 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100457ret_job_info(int argcount, type_T **argtypes UNUSED)
458{
459 if (argcount == 0)
460 return &t_list_job;
461 return &t_dict_any;
462}
463 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100464ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
465{
466 return &t_dict_number;
467}
468 static type_T *
469ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
470{
471 return &t_dict_string;
472}
473 static type_T *
474ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
475{
476 return &t_blob;
477}
478 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200479ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100480{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200481 return &t_func_any;
482}
483 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100484ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
485{
486 return &t_channel;
487}
488 static type_T *
489ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
490{
491 return &t_job;
492}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200493 static type_T *
494ret_first_arg(int argcount, type_T **argtypes)
495{
496 if (argcount > 0)
497 return argtypes[0];
498 return &t_void;
499}
Bram Moolenaarea696852020-11-09 18:31:39 +0100500// for map(): returns first argument but item type may differ
501 static type_T *
502ret_first_cont(int argcount UNUSED, type_T **argtypes)
503{
504 if (argtypes[0]->tt_type == VAR_LIST)
505 return &t_list_any;
506 if (argtypes[0]->tt_type == VAR_DICT)
507 return &t_dict_any;
508 if (argtypes[0]->tt_type == VAR_BLOB)
509 return argtypes[0];
510 return &t_any;
511}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200512
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200513/*
514 * Used for getqflist(): returns list if there is no argument, dict if there is
515 * one.
516 */
517 static type_T *
518ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
519{
520 if (argcount > 0)
521 return &t_dict_any;
522 return &t_list_dict_any;
523}
524
525/*
526 * Used for getloclist(): returns list if there is one argument, dict if there
527 * are two.
528 */
529 static type_T *
530ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
531{
532 if (argcount > 1)
533 return &t_dict_any;
534 return &t_list_dict_any;
535}
536
Bram Moolenaar846178a2020-07-05 17:04:13 +0200537 static type_T *
538ret_argv(int argcount, type_T **argtypes UNUSED)
539{
540 // argv() returns list of strings
541 if (argcount == 0)
542 return &t_list_string;
543
544 // argv(0) returns a string, but argv(-1] returns a list
545 return &t_any;
546}
547
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200548 static type_T *
549ret_remove(int argcount UNUSED, type_T **argtypes)
550{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200551 if (argtypes != NULL)
552 {
553 if (argtypes[0]->tt_type == VAR_LIST
554 || argtypes[0]->tt_type == VAR_DICT)
555 return argtypes[0]->tt_member;
556 if (argtypes[0]->tt_type == VAR_BLOB)
557 return &t_number;
558 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200559 return &t_any;
560}
561
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200562 static type_T *
563ret_getreg(int argcount, type_T **argtypes UNUSED)
564{
565 // Assume that if the third argument is passed it's non-zero
566 if (argcount == 3)
567 return &t_list_string;
568 return &t_string;
569}
570
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200571 static type_T *
572ret_maparg(int argcount, type_T **argtypes UNUSED)
573{
574 // Assume that if the fourth argument is passed it's non-zero
575 if (argcount == 4)
576 return &t_dict_any;
577 return &t_string;
578}
579
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100580static type_T *ret_f_function(int argcount, type_T **argtypes);
581
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582/*
583 * Array with names and number of arguments of all internal functions
584 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
585 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200586typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200588 char *f_name; // function name
589 char f_min_argc; // minimal number of arguments
590 char f_max_argc; // maximal number of arguments
591 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200592 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100593 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
594 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200596 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200597} funcentry_T;
598
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200599// values for f_argtype; zero means it cannot be used as a method
600#define FEARG_1 1 // base is the first argument
601#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200602#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200603#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200604#define FEARG_LAST 9 // base is the last argument
605
Bram Moolenaar15c47602020-03-26 22:16:48 +0100606#ifdef FEAT_FLOAT
607# define FLOAT_FUNC(name) name
608#else
609# define FLOAT_FUNC(name) NULL
610#endif
611#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
612# define MATH_FUNC(name) name
613#else
614# define MATH_FUNC(name) NULL
615#endif
616#ifdef FEAT_TIMERS
617# define TIMER_FUNC(name) name
618#else
619# define TIMER_FUNC(name) NULL
620#endif
621#ifdef FEAT_JOB_CHANNEL
622# define JOB_FUNC(name) name
623#else
624# define JOB_FUNC(name) NULL
625#endif
626#ifdef FEAT_PROP_POPUP
627# define PROP_FUNC(name) name
628#else
629# define PROP_FUNC(name) NULL
630#endif
631#ifdef FEAT_SIGNS
632# define SIGN_FUNC(name) name
633#else
634# define SIGN_FUNC(name) NULL
635#endif
636#ifdef FEAT_SOUND
637# define SOUND_FUNC(name) name
638#else
639# define SOUND_FUNC(name) NULL
640#endif
641#ifdef FEAT_TERMINAL
642# define TERM_FUNC(name) name
643#else
644# define TERM_FUNC(name) NULL
645#endif
646
Bram Moolenaarac92e252019-08-03 21:58:38 +0200647static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200649 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
650 ret_any, FLOAT_FUNC(f_abs)},
651 {"acos", 1, 1, FEARG_1, NULL,
652 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100653 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200654 ret_first_arg, f_add},
655 {"and", 2, 2, FEARG_1, NULL,
656 ret_number, f_and},
657 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100658 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200659 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100660 ret_number_bool, f_appendbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200661 {"argc", 0, 1, 0, NULL,
662 ret_number, f_argc},
663 {"argidx", 0, 0, 0, NULL,
664 ret_number, f_argidx},
665 {"arglistid", 0, 2, 0, NULL,
666 ret_number, f_arglistid},
667 {"argv", 0, 2, 0, NULL,
668 ret_argv, f_argv},
669 {"asin", 1, 1, FEARG_1, NULL,
670 ret_float, FLOAT_FUNC(f_asin)},
671 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100672 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200673 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100674 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200675 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100676 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200677 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100678 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200679 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100680 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200681 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100682 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200683 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100684 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200685 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100686 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200687 {"assert_nobeep", 1, 2, FEARG_1, NULL,
688 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200689 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100690 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200691 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100692 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200693 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100694 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200695 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100696 ret_number_bool, f_assert_true},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200697 {"atan", 1, 1, FEARG_1, NULL,
698 ret_float, FLOAT_FUNC(f_atan)},
699 {"atan2", 2, 2, FEARG_1, NULL,
700 ret_float, FLOAT_FUNC(f_atan2)},
701 {"balloon_gettext", 0, 0, 0, NULL,
702 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100703#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100704 f_balloon_gettext
705#else
706 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100707#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100708 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 {"balloon_show", 1, 1, FEARG_1, NULL,
710 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100711#ifdef FEAT_BEVAL
712 f_balloon_show
713#else
714 NULL
715#endif
716 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"balloon_split", 1, 1, FEARG_1, NULL,
718 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100719#if defined(FEAT_BEVAL_TERM)
720 f_balloon_split
721#else
722 NULL
723#endif
724 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200725 {"browse", 4, 4, 0, NULL,
726 ret_string, f_browse},
727 {"browsedir", 2, 2, 0, NULL,
728 ret_string, f_browsedir},
729 {"bufadd", 1, 1, FEARG_1, NULL,
730 ret_number, f_bufadd},
731 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100732 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200733 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100734 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200735 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
736 ret_string, f_bufname},
737 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
738 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200739 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100740 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200741 {"bufload", 1, 1, FEARG_1, NULL,
742 ret_void, f_bufload},
743 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100744 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200745 {"bufname", 0, 1, FEARG_1, NULL,
746 ret_string, f_bufname},
747 {"bufnr", 0, 2, FEARG_1, NULL,
748 ret_number, f_bufnr},
749 {"bufwinid", 1, 1, FEARG_1, NULL,
750 ret_number, f_bufwinid},
751 {"bufwinnr", 1, 1, FEARG_1, NULL,
752 ret_number, f_bufwinnr},
753 {"byte2line", 1, 1, FEARG_1, NULL,
754 ret_number, f_byte2line},
755 {"byteidx", 2, 2, FEARG_1, NULL,
756 ret_number, f_byteidx},
757 {"byteidxcomp", 2, 2, FEARG_1, NULL,
758 ret_number, f_byteidxcomp},
759 {"call", 2, 3, FEARG_1, NULL,
760 ret_any, f_call},
761 {"ceil", 1, 1, FEARG_1, NULL,
762 ret_float, FLOAT_FUNC(f_ceil)},
763 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100764 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200765 {"ch_close", 1, 1, FEARG_1, NULL,
766 ret_void, JOB_FUNC(f_ch_close)},
767 {"ch_close_in", 1, 1, FEARG_1, NULL,
768 ret_void, JOB_FUNC(f_ch_close_in)},
769 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
770 ret_any, JOB_FUNC(f_ch_evalexpr)},
771 {"ch_evalraw", 2, 3, FEARG_1, NULL,
772 ret_any, JOB_FUNC(f_ch_evalraw)},
773 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
774 ret_number, JOB_FUNC(f_ch_getbufnr)},
775 {"ch_getjob", 1, 1, FEARG_1, NULL,
776 ret_job, JOB_FUNC(f_ch_getjob)},
777 {"ch_info", 1, 1, FEARG_1, NULL,
778 ret_dict_any, JOB_FUNC(f_ch_info)},
779 {"ch_log", 1, 2, FEARG_1, NULL,
780 ret_void, JOB_FUNC(f_ch_log)},
781 {"ch_logfile", 1, 2, FEARG_1, NULL,
782 ret_void, JOB_FUNC(f_ch_logfile)},
783 {"ch_open", 1, 2, FEARG_1, NULL,
784 ret_channel, JOB_FUNC(f_ch_open)},
785 {"ch_read", 1, 2, FEARG_1, NULL,
786 ret_string, JOB_FUNC(f_ch_read)},
787 {"ch_readblob", 1, 2, FEARG_1, NULL,
788 ret_blob, JOB_FUNC(f_ch_readblob)},
789 {"ch_readraw", 1, 2, FEARG_1, NULL,
790 ret_string, JOB_FUNC(f_ch_readraw)},
791 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
792 ret_void, JOB_FUNC(f_ch_sendexpr)},
793 {"ch_sendraw", 2, 3, FEARG_1, NULL,
794 ret_void, JOB_FUNC(f_ch_sendraw)},
795 {"ch_setoptions", 2, 2, FEARG_1, NULL,
796 ret_void, JOB_FUNC(f_ch_setoptions)},
797 {"ch_status", 1, 2, FEARG_1, NULL,
798 ret_string, JOB_FUNC(f_ch_status)},
799 {"changenr", 0, 0, 0, NULL,
800 ret_number, f_changenr},
801 {"char2nr", 1, 2, FEARG_1, NULL,
802 ret_number, f_char2nr},
803 {"charclass", 1, 1, FEARG_1, NULL,
804 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100805 {"charcol", 1, 1, FEARG_1, NULL,
806 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100807 {"charidx", 2, 3, FEARG_1, NULL,
808 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200809 {"chdir", 1, 1, FEARG_1, NULL,
810 ret_string, f_chdir},
811 {"cindent", 1, 1, FEARG_1, NULL,
812 ret_number, f_cindent},
813 {"clearmatches", 0, 1, FEARG_1, NULL,
814 ret_void, f_clearmatches},
815 {"col", 1, 1, FEARG_1, NULL,
816 ret_number, f_col},
817 {"complete", 2, 2, FEARG_2, NULL,
818 ret_void, f_complete},
819 {"complete_add", 1, 1, FEARG_1, NULL,
820 ret_number, f_complete_add},
821 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100822 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200823 {"complete_info", 0, 1, FEARG_1, NULL,
824 ret_dict_any, f_complete_info},
825 {"confirm", 1, 4, FEARG_1, NULL,
826 ret_number, f_confirm},
827 {"copy", 1, 1, FEARG_1, NULL,
828 ret_first_arg, f_copy},
829 {"cos", 1, 1, FEARG_1, NULL,
830 ret_float, FLOAT_FUNC(f_cos)},
831 {"cosh", 1, 1, FEARG_1, NULL,
832 ret_float, FLOAT_FUNC(f_cosh)},
833 {"count", 2, 4, FEARG_1, NULL,
834 ret_number, f_count},
835 {"cscope_connection",0,3, 0, NULL,
836 ret_number, f_cscope_connection},
837 {"cursor", 1, 3, FEARG_1, NULL,
838 ret_number, f_cursor},
839 {"debugbreak", 1, 1, FEARG_1, NULL,
840 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100841#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100842 f_debugbreak
843#else
844 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200845#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100846 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200847 {"deepcopy", 1, 2, FEARG_1, NULL,
848 ret_first_arg, f_deepcopy},
849 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100850 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200851 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100852 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200853 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100854 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200855 {"diff_filler", 1, 1, FEARG_1, NULL,
856 ret_number, f_diff_filler},
857 {"diff_hlID", 2, 2, FEARG_1, NULL,
858 ret_number, f_diff_hlID},
859 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100860 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200861 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100862 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200863 {"environ", 0, 0, 0, NULL,
864 ret_dict_string, f_environ},
865 {"escape", 2, 2, FEARG_1, NULL,
866 ret_string, f_escape},
867 {"eval", 1, 1, FEARG_1, NULL,
868 ret_any, f_eval},
869 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100870 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200871 {"executable", 1, 1, FEARG_1, NULL,
872 ret_number, f_executable},
873 {"execute", 1, 2, FEARG_1, NULL,
874 ret_string, f_execute},
875 {"exepath", 1, 1, FEARG_1, NULL,
876 ret_string, f_exepath},
877 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100878 ret_number_bool, f_exists},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200879 {"exp", 1, 1, FEARG_1, NULL,
880 ret_float, FLOAT_FUNC(f_exp)},
881 {"expand", 1, 3, FEARG_1, NULL,
882 ret_any, f_expand},
883 {"expandcmd", 1, 1, FEARG_1, NULL,
884 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100885 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200886 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100887 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
888 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200889 {"feedkeys", 1, 2, FEARG_1, NULL,
890 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200891 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100892 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200893 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100894 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200895 {"filewritable", 1, 1, FEARG_1, NULL,
896 ret_number, f_filewritable},
897 {"filter", 2, 2, FEARG_1, NULL,
898 ret_first_arg, f_filter},
899 {"finddir", 1, 3, FEARG_1, NULL,
900 ret_string, f_finddir},
901 {"findfile", 1, 3, FEARG_1, NULL,
902 ret_string, f_findfile},
903 {"flatten", 1, 2, FEARG_1, NULL,
904 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100905 {"flattennew", 1, 2, FEARG_1, NULL,
906 ret_list_any, f_flattennew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200907 {"float2nr", 1, 1, FEARG_1, NULL,
908 ret_number, FLOAT_FUNC(f_float2nr)},
909 {"floor", 1, 1, FEARG_1, NULL,
910 ret_float, FLOAT_FUNC(f_floor)},
911 {"fmod", 2, 2, FEARG_1, NULL,
912 ret_float, FLOAT_FUNC(f_fmod)},
913 {"fnameescape", 1, 1, FEARG_1, NULL,
914 ret_string, f_fnameescape},
915 {"fnamemodify", 2, 2, FEARG_1, NULL,
916 ret_string, f_fnamemodify},
917 {"foldclosed", 1, 1, FEARG_1, NULL,
918 ret_number, f_foldclosed},
919 {"foldclosedend", 1, 1, FEARG_1, NULL,
920 ret_number, f_foldclosedend},
921 {"foldlevel", 1, 1, FEARG_1, NULL,
922 ret_number, f_foldlevel},
923 {"foldtext", 0, 0, 0, NULL,
924 ret_string, f_foldtext},
925 {"foldtextresult", 1, 1, FEARG_1, NULL,
926 ret_string, f_foldtextresult},
927 {"foreground", 0, 0, 0, NULL,
928 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100929 {"fullcommand", 1, 1, FEARG_1, arg1_string,
930 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200931 {"funcref", 1, 3, FEARG_1, NULL,
932 ret_func_any, f_funcref},
933 {"function", 1, 3, FEARG_1, NULL,
934 ret_f_function, f_function},
935 {"garbagecollect", 0, 1, 0, NULL,
936 ret_void, f_garbagecollect},
937 {"get", 2, 3, FEARG_1, NULL,
938 ret_any, f_get},
939 {"getbufinfo", 0, 1, FEARG_1, NULL,
940 ret_list_dict_any, f_getbufinfo},
941 {"getbufline", 2, 3, FEARG_1, NULL,
942 ret_list_string, f_getbufline},
943 {"getbufvar", 2, 3, FEARG_1, NULL,
944 ret_any, f_getbufvar},
945 {"getchangelist", 0, 1, FEARG_1, NULL,
946 ret_list_any, f_getchangelist},
947 {"getchar", 0, 1, 0, NULL,
948 ret_number, f_getchar},
949 {"getcharmod", 0, 0, 0, NULL,
950 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100951 {"getcharpos", 1, 1, FEARG_1, NULL,
952 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200953 {"getcharsearch", 0, 0, 0, NULL,
954 ret_dict_any, f_getcharsearch},
955 {"getcmdline", 0, 0, 0, NULL,
956 ret_string, f_getcmdline},
957 {"getcmdpos", 0, 0, 0, NULL,
958 ret_number, f_getcmdpos},
959 {"getcmdtype", 0, 0, 0, NULL,
960 ret_string, f_getcmdtype},
961 {"getcmdwintype", 0, 0, 0, NULL,
962 ret_string, f_getcmdwintype},
963 {"getcompletion", 2, 3, FEARG_1, NULL,
964 ret_list_string, f_getcompletion},
965 {"getcurpos", 0, 1, FEARG_1, NULL,
966 ret_list_number, f_getcurpos},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100967 {"getcursorcharpos", 0, 1, FEARG_1, NULL,
968 ret_list_number, f_getcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200969 {"getcwd", 0, 2, FEARG_1, NULL,
970 ret_string, f_getcwd},
971 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +0100972 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200973 {"getfontname", 0, 1, 0, NULL,
974 ret_string, f_getfontname},
975 {"getfperm", 1, 1, FEARG_1, NULL,
976 ret_string, f_getfperm},
977 {"getfsize", 1, 1, FEARG_1, NULL,
978 ret_number, f_getfsize},
979 {"getftime", 1, 1, FEARG_1, NULL,
980 ret_number, f_getftime},
981 {"getftype", 1, 1, FEARG_1, NULL,
982 ret_string, f_getftype},
983 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100984 ret_number_bool, f_getimstatus},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200985 {"getjumplist", 0, 2, FEARG_1, NULL,
986 ret_list_any, f_getjumplist},
987 {"getline", 1, 2, FEARG_1, NULL,
988 ret_f_getline, f_getline},
989 {"getloclist", 1, 2, 0, NULL,
990 ret_list_or_dict_1, f_getloclist},
991 {"getmarklist", 0, 1, FEARG_1, NULL,
992 ret_list_dict_any, f_getmarklist},
993 {"getmatches", 0, 1, 0, NULL,
994 ret_list_dict_any, f_getmatches},
995 {"getmousepos", 0, 0, 0, NULL,
996 ret_dict_number, f_getmousepos},
997 {"getpid", 0, 0, 0, NULL,
998 ret_number, f_getpid},
999 {"getpos", 1, 1, FEARG_1, NULL,
1000 ret_list_number, f_getpos},
1001 {"getqflist", 0, 1, 0, NULL,
1002 ret_list_or_dict_0, f_getqflist},
1003 {"getreg", 0, 3, FEARG_1, NULL,
1004 ret_getreg, f_getreg},
1005 {"getreginfo", 0, 1, FEARG_1, NULL,
1006 ret_dict_any, f_getreginfo},
1007 {"getregtype", 0, 1, FEARG_1, NULL,
1008 ret_string, f_getregtype},
1009 {"gettabinfo", 0, 1, FEARG_1, NULL,
1010 ret_list_dict_any, f_gettabinfo},
1011 {"gettabvar", 2, 3, FEARG_1, NULL,
1012 ret_any, f_gettabvar},
1013 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1014 ret_any, f_gettabwinvar},
1015 {"gettagstack", 0, 1, FEARG_1, NULL,
1016 ret_dict_any, f_gettagstack},
1017 {"gettext", 1, 1, FEARG_1, NULL,
1018 ret_string, f_gettext},
1019 {"getwininfo", 0, 1, FEARG_1, NULL,
1020 ret_list_dict_any, f_getwininfo},
1021 {"getwinpos", 0, 1, FEARG_1, NULL,
1022 ret_list_number, f_getwinpos},
1023 {"getwinposx", 0, 0, 0, NULL,
1024 ret_number, f_getwinposx},
1025 {"getwinposy", 0, 0, 0, NULL,
1026 ret_number, f_getwinposy},
1027 {"getwinvar", 2, 3, FEARG_1, NULL,
1028 ret_any, f_getwinvar},
1029 {"glob", 1, 4, FEARG_1, NULL,
1030 ret_any, f_glob},
1031 {"glob2regpat", 1, 1, FEARG_1, NULL,
1032 ret_string, f_glob2regpat},
1033 {"globpath", 2, 5, FEARG_2, NULL,
1034 ret_any, f_globpath},
1035 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001036 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001037 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001038 ret_number_bool, f_has_key},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001039 {"haslocaldir", 0, 2, FEARG_1, NULL,
1040 ret_number, f_haslocaldir},
1041 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001042 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001043 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1044 ret_number, f_hlID},
1045 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001046 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001047 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001048 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001049 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001050 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001051 {"histget", 1, 2, FEARG_1, NULL,
1052 ret_string, f_histget},
1053 {"histnr", 1, 1, FEARG_1, NULL,
1054 ret_number, f_histnr},
1055 {"hlID", 1, 1, FEARG_1, NULL,
1056 ret_number, f_hlID},
1057 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001058 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001059 {"hostname", 0, 0, 0, NULL,
1060 ret_string, f_hostname},
1061 {"iconv", 3, 3, FEARG_1, NULL,
1062 ret_string, f_iconv},
1063 {"indent", 1, 1, FEARG_1, NULL,
1064 ret_number, f_indent},
1065 {"index", 2, 4, FEARG_1, NULL,
1066 ret_number, f_index},
1067 {"input", 1, 3, FEARG_1, NULL,
1068 ret_string, f_input},
1069 {"inputdialog", 1, 3, FEARG_1, NULL,
1070 ret_string, f_inputdialog},
1071 {"inputlist", 1, 1, FEARG_1, NULL,
1072 ret_number, f_inputlist},
1073 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001074 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001075 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 {"inputsecret", 1, 2, FEARG_1, NULL,
1078 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001079 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001080 ret_first_arg, f_insert},
1081 {"interrupt", 0, 0, 0, NULL,
1082 ret_void, f_interrupt},
1083 {"invert", 1, 1, FEARG_1, NULL,
1084 ret_number, f_invert},
1085 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_isdirectory},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"isinf", 1, 1, FEARG_1, NULL,
1088 ret_number, MATH_FUNC(f_isinf)},
1089 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001090 ret_number_bool, f_islocked},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001091 {"isnan", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001092 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001093 {"items", 1, 1, FEARG_1, NULL,
1094 ret_list_any, f_items},
1095 {"job_getchannel", 1, 1, FEARG_1, NULL,
1096 ret_channel, JOB_FUNC(f_job_getchannel)},
1097 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001098 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001099 {"job_setoptions", 2, 2, FEARG_1, NULL,
1100 ret_void, JOB_FUNC(f_job_setoptions)},
1101 {"job_start", 1, 2, FEARG_1, NULL,
1102 ret_job, JOB_FUNC(f_job_start)},
1103 {"job_status", 1, 1, FEARG_1, NULL,
1104 ret_string, JOB_FUNC(f_job_status)},
1105 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001106 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001107 {"join", 1, 2, FEARG_1, NULL,
1108 ret_string, f_join},
1109 {"js_decode", 1, 1, FEARG_1, NULL,
1110 ret_any, f_js_decode},
1111 {"js_encode", 1, 1, FEARG_1, NULL,
1112 ret_string, f_js_encode},
1113 {"json_decode", 1, 1, FEARG_1, NULL,
1114 ret_any, f_json_decode},
1115 {"json_encode", 1, 1, FEARG_1, NULL,
1116 ret_string, f_json_encode},
1117 {"keys", 1, 1, FEARG_1, NULL,
1118 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001119 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1120 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001121 {"len", 1, 1, FEARG_1, NULL,
1122 ret_number, f_len},
1123 {"libcall", 3, 3, FEARG_3, NULL,
1124 ret_string, f_libcall},
1125 {"libcallnr", 3, 3, FEARG_3, NULL,
1126 ret_number, f_libcallnr},
1127 {"line", 1, 2, FEARG_1, NULL,
1128 ret_number, f_line},
1129 {"line2byte", 1, 1, FEARG_1, NULL,
1130 ret_number, f_line2byte},
1131 {"lispindent", 1, 1, FEARG_1, NULL,
1132 ret_number, f_lispindent},
1133 {"list2str", 1, 2, FEARG_1, NULL,
1134 ret_string, f_list2str},
1135 {"listener_add", 1, 2, FEARG_2, NULL,
1136 ret_number, f_listener_add},
1137 {"listener_flush", 0, 1, FEARG_1, NULL,
1138 ret_void, f_listener_flush},
1139 {"listener_remove", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001140 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001141 {"localtime", 0, 0, 0, NULL,
1142 ret_number, f_localtime},
1143 {"log", 1, 1, FEARG_1, NULL,
1144 ret_float, FLOAT_FUNC(f_log)},
1145 {"log10", 1, 1, FEARG_1, NULL,
1146 ret_float, FLOAT_FUNC(f_log10)},
1147 {"luaeval", 1, 2, FEARG_1, NULL,
1148 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001149#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001150 f_luaeval
1151#else
1152 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001153#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001154 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001155 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001156 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001157 {"maparg", 1, 4, FEARG_1, NULL,
1158 ret_maparg, f_maparg},
1159 {"mapcheck", 1, 3, FEARG_1, NULL,
1160 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001161 {"mapnew", 2, 2, FEARG_1, NULL,
1162 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001163 {"mapset", 3, 3, FEARG_1, NULL,
1164 ret_void, f_mapset},
1165 {"match", 2, 4, FEARG_1, NULL,
1166 ret_any, f_match},
1167 {"matchadd", 2, 5, FEARG_1, NULL,
1168 ret_number, f_matchadd},
1169 {"matchaddpos", 2, 5, FEARG_1, NULL,
1170 ret_number, f_matchaddpos},
1171 {"matcharg", 1, 1, FEARG_1, NULL,
1172 ret_list_string, f_matcharg},
1173 {"matchdelete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001174 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001175 {"matchend", 2, 4, FEARG_1, NULL,
1176 ret_number, f_matchend},
1177 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1178 ret_list_string, f_matchfuzzy},
1179 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1180 ret_list_any, f_matchfuzzypos},
1181 {"matchlist", 2, 4, FEARG_1, NULL,
1182 ret_list_string, f_matchlist},
1183 {"matchstr", 2, 4, FEARG_1, NULL,
1184 ret_string, f_matchstr},
1185 {"matchstrpos", 2, 4, FEARG_1, NULL,
1186 ret_list_any, f_matchstrpos},
1187 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001188 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001189 {"menu_info", 1, 2, FEARG_1, NULL,
1190 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001191#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001192 f_menu_info
1193#else
1194 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001195#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001196 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001197 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001198 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001199 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001200 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001201 {"mode", 0, 1, FEARG_1, NULL,
1202 ret_string, f_mode},
1203 {"mzeval", 1, 1, FEARG_1, NULL,
1204 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001205#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001206 f_mzeval
1207#else
1208 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001210 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001211 {"nextnonblank", 1, 1, FEARG_1, NULL,
1212 ret_number, f_nextnonblank},
1213 {"nr2char", 1, 2, FEARG_1, NULL,
1214 ret_string, f_nr2char},
1215 {"or", 2, 2, FEARG_1, NULL,
1216 ret_number, f_or},
1217 {"pathshorten", 1, 2, FEARG_1, NULL,
1218 ret_string, f_pathshorten},
1219 {"perleval", 1, 1, FEARG_1, NULL,
1220 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001222 f_perleval
1223#else
1224 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001225#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001226 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001227 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1228 ret_number, PROP_FUNC(f_popup_atcursor)},
1229 {"popup_beval", 2, 2, FEARG_1, NULL,
1230 ret_number, PROP_FUNC(f_popup_beval)},
1231 {"popup_clear", 0, 1, 0, NULL,
1232 ret_void, PROP_FUNC(f_popup_clear)},
1233 {"popup_close", 1, 2, FEARG_1, NULL,
1234 ret_void, PROP_FUNC(f_popup_close)},
1235 {"popup_create", 2, 2, FEARG_1, NULL,
1236 ret_number, PROP_FUNC(f_popup_create)},
1237 {"popup_dialog", 2, 2, FEARG_1, NULL,
1238 ret_number, PROP_FUNC(f_popup_dialog)},
1239 {"popup_filter_menu", 2, 2, 0, NULL,
1240 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1241 {"popup_filter_yesno", 2, 2, 0, NULL,
1242 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1243 {"popup_findinfo", 0, 0, 0, NULL,
1244 ret_number, PROP_FUNC(f_popup_findinfo)},
1245 {"popup_findpreview", 0, 0, 0, NULL,
1246 ret_number, PROP_FUNC(f_popup_findpreview)},
1247 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1248 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1249 {"popup_getpos", 1, 1, FEARG_1, NULL,
1250 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1251 {"popup_hide", 1, 1, FEARG_1, NULL,
1252 ret_void, PROP_FUNC(f_popup_hide)},
1253 {"popup_list", 0, 0, 0, NULL,
1254 ret_list_number, PROP_FUNC(f_popup_list)},
1255 {"popup_locate", 2, 2, 0, NULL,
1256 ret_number, PROP_FUNC(f_popup_locate)},
1257 {"popup_menu", 2, 2, FEARG_1, NULL,
1258 ret_number, PROP_FUNC(f_popup_menu)},
1259 {"popup_move", 2, 2, FEARG_1, NULL,
1260 ret_void, PROP_FUNC(f_popup_move)},
1261 {"popup_notification", 2, 2, FEARG_1, NULL,
1262 ret_number, PROP_FUNC(f_popup_notification)},
1263 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1264 ret_void, PROP_FUNC(f_popup_setoptions)},
1265 {"popup_settext", 2, 2, FEARG_1, NULL,
1266 ret_void, PROP_FUNC(f_popup_settext)},
1267 {"popup_show", 1, 1, FEARG_1, NULL,
1268 ret_void, PROP_FUNC(f_popup_show)},
1269 {"pow", 2, 2, FEARG_1, NULL,
1270 ret_float, FLOAT_FUNC(f_pow)},
1271 {"prevnonblank", 1, 1, FEARG_1, NULL,
1272 ret_number, f_prevnonblank},
1273 {"printf", 1, 19, FEARG_2, NULL,
1274 ret_string, f_printf},
1275 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1276 ret_string, JOB_FUNC(f_prompt_getprompt)},
1277 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1278 ret_void, JOB_FUNC(f_prompt_setcallback)},
1279 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1280 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1281 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1282 ret_void, JOB_FUNC(f_prompt_setprompt)},
1283 {"prop_add", 3, 3, FEARG_1, NULL,
1284 ret_void, PROP_FUNC(f_prop_add)},
1285 {"prop_clear", 1, 3, FEARG_1, NULL,
1286 ret_void, PROP_FUNC(f_prop_clear)},
1287 {"prop_find", 1, 2, FEARG_1, NULL,
1288 ret_dict_any, PROP_FUNC(f_prop_find)},
1289 {"prop_list", 1, 2, FEARG_1, NULL,
1290 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1291 {"prop_remove", 1, 3, FEARG_1, NULL,
1292 ret_number, PROP_FUNC(f_prop_remove)},
1293 {"prop_type_add", 2, 2, FEARG_1, NULL,
1294 ret_void, PROP_FUNC(f_prop_type_add)},
1295 {"prop_type_change", 2, 2, FEARG_1, NULL,
1296 ret_void, PROP_FUNC(f_prop_type_change)},
1297 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1298 ret_void, PROP_FUNC(f_prop_type_delete)},
1299 {"prop_type_get", 1, 2, FEARG_1, NULL,
1300 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1301 {"prop_type_list", 0, 1, FEARG_1, NULL,
1302 ret_list_string, PROP_FUNC(f_prop_type_list)},
1303 {"pum_getpos", 0, 0, 0, NULL,
1304 ret_dict_number, f_pum_getpos},
1305 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001306 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001307 {"py3eval", 1, 1, FEARG_1, NULL,
1308 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001310 f_py3eval
1311#else
1312 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001314 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001315 {"pyeval", 1, 1, FEARG_1, NULL,
1316 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001318 f_pyeval
1319#else
1320 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001321#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001322 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001323 {"pyxeval", 1, 1, FEARG_1, NULL,
1324 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001325#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001326 f_pyxeval
1327#else
1328 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001329#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001330 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001331 {"rand", 0, 1, FEARG_1, NULL,
1332 ret_number, f_rand},
1333 {"range", 1, 3, FEARG_1, NULL,
1334 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001335 {"readblob", 1, 1, FEARG_1, NULL,
1336 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001337 {"readdir", 1, 3, FEARG_1, NULL,
1338 ret_list_string, f_readdir},
1339 {"readdirex", 1, 3, FEARG_1, NULL,
1340 ret_list_dict_any, f_readdirex},
1341 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001342 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001343 {"reduce", 2, 3, FEARG_1, NULL,
1344 ret_any, f_reduce},
1345 {"reg_executing", 0, 0, 0, NULL,
1346 ret_string, f_reg_executing},
1347 {"reg_recording", 0, 0, 0, NULL,
1348 ret_string, f_reg_recording},
1349 {"reltime", 0, 2, FEARG_1, NULL,
1350 ret_list_any, f_reltime},
1351 {"reltimefloat", 1, 1, FEARG_1, NULL,
1352 ret_float, FLOAT_FUNC(f_reltimefloat)},
1353 {"reltimestr", 1, 1, FEARG_1, NULL,
1354 ret_string, f_reltimestr},
1355 {"remote_expr", 2, 4, FEARG_1, NULL,
1356 ret_string, f_remote_expr},
1357 {"remote_foreground", 1, 1, FEARG_1, NULL,
1358 ret_string, f_remote_foreground},
1359 {"remote_peek", 1, 2, FEARG_1, NULL,
1360 ret_number, f_remote_peek},
1361 {"remote_read", 1, 2, FEARG_1, NULL,
1362 ret_string, f_remote_read},
1363 {"remote_send", 2, 3, FEARG_1, NULL,
1364 ret_string, f_remote_send},
1365 {"remote_startserver", 1, 1, FEARG_1, NULL,
1366 ret_void, f_remote_startserver},
1367 {"remove", 2, 3, FEARG_1, NULL,
1368 ret_remove, f_remove},
1369 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001370 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 {"repeat", 2, 2, FEARG_1, NULL,
1372 ret_first_arg, f_repeat},
1373 {"resolve", 1, 1, FEARG_1, NULL,
1374 ret_string, f_resolve},
1375 {"reverse", 1, 1, FEARG_1, NULL,
1376 ret_first_arg, f_reverse},
1377 {"round", 1, 1, FEARG_1, NULL,
1378 ret_float, FLOAT_FUNC(f_round)},
1379 {"rubyeval", 1, 1, FEARG_1, NULL,
1380 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001381#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001382 f_rubyeval
1383#else
1384 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001385#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001386 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001387 {"screenattr", 2, 2, FEARG_1, NULL,
1388 ret_number, f_screenattr},
1389 {"screenchar", 2, 2, FEARG_1, NULL,
1390 ret_number, f_screenchar},
1391 {"screenchars", 2, 2, FEARG_1, NULL,
1392 ret_list_number, f_screenchars},
1393 {"screencol", 0, 0, 0, NULL,
1394 ret_number, f_screencol},
1395 {"screenpos", 3, 3, FEARG_1, NULL,
1396 ret_dict_number, f_screenpos},
1397 {"screenrow", 0, 0, 0, NULL,
1398 ret_number, f_screenrow},
1399 {"screenstring", 2, 2, FEARG_1, NULL,
1400 ret_string, f_screenstring},
1401 {"search", 1, 5, FEARG_1, NULL,
1402 ret_number, f_search},
1403 {"searchcount", 0, 1, FEARG_1, NULL,
1404 ret_dict_any, f_searchcount},
1405 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001406 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001407 {"searchpair", 3, 7, 0, NULL,
1408 ret_number, f_searchpair},
1409 {"searchpairpos", 3, 7, 0, NULL,
1410 ret_list_number, f_searchpairpos},
1411 {"searchpos", 1, 5, FEARG_1, NULL,
1412 ret_list_number, f_searchpos},
1413 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001414 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001415 {"serverlist", 0, 0, 0, NULL,
1416 ret_string, f_serverlist},
1417 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001418 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001419 {"setbufvar", 3, 3, FEARG_3, NULL,
1420 ret_void, f_setbufvar},
1421 {"setcellwidths", 1, 1, FEARG_1, NULL,
1422 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001423 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001424 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001425 {"setcharsearch", 1, 1, FEARG_1, NULL,
1426 ret_void, f_setcharsearch},
1427 {"setcmdpos", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001428 ret_number_bool, f_setcmdpos},
1429 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1430 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001431 {"setenv", 2, 2, FEARG_2, NULL,
1432 ret_void, f_setenv},
1433 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001434 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001435 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001436 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001437 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001438 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001439 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001440 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001441 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001444 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001445 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001446 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001447 {"settabvar", 3, 3, FEARG_3, NULL,
1448 ret_void, f_settabvar},
1449 {"settabwinvar", 4, 4, FEARG_4, NULL,
1450 ret_void, f_settabwinvar},
1451 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001452 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001453 {"setwinvar", 3, 3, FEARG_3, NULL,
1454 ret_void, f_setwinvar},
1455 {"sha256", 1, 1, FEARG_1, NULL,
1456 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001457#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001458 f_sha256
1459#else
1460 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001462 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 {"shellescape", 1, 2, FEARG_1, NULL,
1464 ret_string, f_shellescape},
1465 {"shiftwidth", 0, 1, FEARG_1, NULL,
1466 ret_number, f_shiftwidth},
1467 {"sign_define", 1, 2, FEARG_1, NULL,
1468 ret_any, SIGN_FUNC(f_sign_define)},
1469 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1470 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1471 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1472 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1473 {"sign_jump", 3, 3, FEARG_1, NULL,
1474 ret_number, SIGN_FUNC(f_sign_jump)},
1475 {"sign_place", 4, 5, FEARG_1, NULL,
1476 ret_number, SIGN_FUNC(f_sign_place)},
1477 {"sign_placelist", 1, 1, FEARG_1, NULL,
1478 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1479 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001482 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001483 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1484 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1485 {"simplify", 1, 1, FEARG_1, NULL,
1486 ret_string, f_simplify},
1487 {"sin", 1, 1, FEARG_1, NULL,
1488 ret_float, FLOAT_FUNC(f_sin)},
1489 {"sinh", 1, 1, FEARG_1, NULL,
1490 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001491 {"slice", 2, 3, FEARG_1, NULL,
1492 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001493 {"sort", 1, 3, FEARG_1, NULL,
1494 ret_first_arg, f_sort},
1495 {"sound_clear", 0, 0, 0, NULL,
1496 ret_void, SOUND_FUNC(f_sound_clear)},
1497 {"sound_playevent", 1, 2, FEARG_1, NULL,
1498 ret_number, SOUND_FUNC(f_sound_playevent)},
1499 {"sound_playfile", 1, 2, FEARG_1, NULL,
1500 ret_number, SOUND_FUNC(f_sound_playfile)},
1501 {"sound_stop", 1, 1, FEARG_1, NULL,
1502 ret_void, SOUND_FUNC(f_sound_stop)},
1503 {"soundfold", 1, 1, FEARG_1, NULL,
1504 ret_string, f_soundfold},
1505 {"spellbadword", 0, 1, FEARG_1, NULL,
1506 ret_list_string, f_spellbadword},
1507 {"spellsuggest", 1, 3, FEARG_1, NULL,
1508 ret_list_string, f_spellsuggest},
1509 {"split", 1, 3, FEARG_1, NULL,
1510 ret_list_string, f_split},
1511 {"sqrt", 1, 1, FEARG_1, NULL,
1512 ret_float, FLOAT_FUNC(f_sqrt)},
1513 {"srand", 0, 1, FEARG_1, NULL,
1514 ret_list_number, f_srand},
1515 {"state", 0, 1, FEARG_1, NULL,
1516 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001517 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 ret_float, FLOAT_FUNC(f_str2float)},
1519 {"str2list", 1, 2, FEARG_1, NULL,
1520 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001521 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001522 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001523 {"strcharlen", 1, 1, FEARG_1, NULL,
1524 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001525 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001526 ret_string, f_strcharpart},
1527 {"strchars", 1, 2, FEARG_1, NULL,
1528 ret_number, f_strchars},
1529 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1530 ret_number, f_strdisplaywidth},
1531 {"strftime", 1, 2, FEARG_1, NULL,
1532 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001534 f_strftime
1535#else
1536 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001538 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001539 {"strgetchar", 2, 2, FEARG_1, NULL,
1540 ret_number, f_strgetchar},
1541 {"stridx", 2, 3, FEARG_1, NULL,
1542 ret_number, f_stridx},
1543 {"string", 1, 1, FEARG_1, NULL,
1544 ret_string, f_string},
1545 {"strlen", 1, 1, FEARG_1, NULL,
1546 ret_number, f_strlen},
1547 {"strpart", 2, 4, FEARG_1, NULL,
1548 ret_string, f_strpart},
1549 {"strptime", 2, 2, FEARG_1, NULL,
1550 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001551#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001552 f_strptime
1553#else
1554 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001555#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001556 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001557 {"strridx", 2, 3, FEARG_1, NULL,
1558 ret_number, f_strridx},
1559 {"strtrans", 1, 1, FEARG_1, NULL,
1560 ret_string, f_strtrans},
1561 {"strwidth", 1, 1, FEARG_1, NULL,
1562 ret_number, f_strwidth},
1563 {"submatch", 1, 2, FEARG_1, NULL,
1564 ret_string, f_submatch},
1565 {"substitute", 4, 4, FEARG_1, NULL,
1566 ret_string, f_substitute},
1567 {"swapinfo", 1, 1, FEARG_1, NULL,
1568 ret_dict_any, f_swapinfo},
1569 {"swapname", 1, 1, FEARG_1, NULL,
1570 ret_string, f_swapname},
1571 {"synID", 3, 3, 0, NULL,
1572 ret_number, f_synID},
1573 {"synIDattr", 2, 3, FEARG_1, NULL,
1574 ret_string, f_synIDattr},
1575 {"synIDtrans", 1, 1, FEARG_1, NULL,
1576 ret_number, f_synIDtrans},
1577 {"synconcealed", 2, 2, 0, NULL,
1578 ret_list_any, f_synconcealed},
1579 {"synstack", 2, 2, 0, NULL,
1580 ret_list_number, f_synstack},
1581 {"system", 1, 2, FEARG_1, NULL,
1582 ret_string, f_system},
1583 {"systemlist", 1, 2, FEARG_1, NULL,
1584 ret_list_string, f_systemlist},
1585 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1586 ret_list_number, f_tabpagebuflist},
1587 {"tabpagenr", 0, 1, 0, NULL,
1588 ret_number, f_tabpagenr},
1589 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1590 ret_number, f_tabpagewinnr},
1591 {"tagfiles", 0, 0, 0, NULL,
1592 ret_list_string, f_tagfiles},
1593 {"taglist", 1, 2, FEARG_1, NULL,
1594 ret_list_dict_any, f_taglist},
1595 {"tan", 1, 1, FEARG_1, NULL,
1596 ret_float, FLOAT_FUNC(f_tan)},
1597 {"tanh", 1, 1, FEARG_1, NULL,
1598 ret_float, FLOAT_FUNC(f_tanh)},
1599 {"tempname", 0, 0, 0, NULL,
1600 ret_string, f_tempname},
1601 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1602 ret_number, TERM_FUNC(f_term_dumpdiff)},
1603 {"term_dumpload", 1, 2, FEARG_1, NULL,
1604 ret_number, TERM_FUNC(f_term_dumpload)},
1605 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1606 ret_void, TERM_FUNC(f_term_dumpwrite)},
1607 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1608 ret_number, TERM_FUNC(f_term_getaltscreen)},
1609 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1610 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001611#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001612 f_term_getansicolors
1613#else
1614 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001615#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001616 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001617 {"term_getattr", 2, 2, FEARG_1, NULL,
1618 ret_number, TERM_FUNC(f_term_getattr)},
1619 {"term_getcursor", 1, 1, FEARG_1, NULL,
1620 ret_list_any, TERM_FUNC(f_term_getcursor)},
1621 {"term_getjob", 1, 1, FEARG_1, NULL,
1622 ret_job, TERM_FUNC(f_term_getjob)},
1623 {"term_getline", 2, 2, FEARG_1, NULL,
1624 ret_string, TERM_FUNC(f_term_getline)},
1625 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1626 ret_number, TERM_FUNC(f_term_getscrolled)},
1627 {"term_getsize", 1, 1, FEARG_1, NULL,
1628 ret_list_number, TERM_FUNC(f_term_getsize)},
1629 {"term_getstatus", 1, 1, FEARG_1, NULL,
1630 ret_string, TERM_FUNC(f_term_getstatus)},
1631 {"term_gettitle", 1, 1, FEARG_1, NULL,
1632 ret_string, TERM_FUNC(f_term_gettitle)},
1633 {"term_gettty", 1, 2, FEARG_1, NULL,
1634 ret_string, TERM_FUNC(f_term_gettty)},
1635 {"term_list", 0, 0, 0, NULL,
1636 ret_list_number, TERM_FUNC(f_term_list)},
1637 {"term_scrape", 2, 2, FEARG_1, NULL,
1638 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1639 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1640 ret_void, TERM_FUNC(f_term_sendkeys)},
1641 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1642 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001643#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001644 f_term_setansicolors
1645#else
1646 NULL
1647#endif
1648 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001649 {"term_setapi", 2, 2, FEARG_1, NULL,
1650 ret_void, TERM_FUNC(f_term_setapi)},
1651 {"term_setkill", 2, 2, FEARG_1, NULL,
1652 ret_void, TERM_FUNC(f_term_setkill)},
1653 {"term_setrestore", 2, 2, FEARG_1, NULL,
1654 ret_void, TERM_FUNC(f_term_setrestore)},
1655 {"term_setsize", 3, 3, FEARG_1, NULL,
1656 ret_void, TERM_FUNC(f_term_setsize)},
1657 {"term_start", 1, 2, FEARG_1, NULL,
1658 ret_number, TERM_FUNC(f_term_start)},
1659 {"term_wait", 1, 2, FEARG_1, NULL,
1660 ret_void, TERM_FUNC(f_term_wait)},
1661 {"terminalprops", 0, 0, 0, NULL,
1662 ret_dict_string, f_terminalprops},
1663 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1664 ret_void, f_test_alloc_fail},
1665 {"test_autochdir", 0, 0, 0, NULL,
1666 ret_void, f_test_autochdir},
1667 {"test_feedinput", 1, 1, FEARG_1, NULL,
1668 ret_void, f_test_feedinput},
1669 {"test_garbagecollect_now", 0, 0, 0, NULL,
1670 ret_void, f_test_garbagecollect_now},
1671 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1672 ret_void, f_test_garbagecollect_soon},
1673 {"test_getvalue", 1, 1, FEARG_1, NULL,
1674 ret_number, f_test_getvalue},
1675 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1676 ret_void, f_test_ignore_error},
1677 {"test_null_blob", 0, 0, 0, NULL,
1678 ret_blob, f_test_null_blob},
1679 {"test_null_channel", 0, 0, 0, NULL,
1680 ret_channel, JOB_FUNC(f_test_null_channel)},
1681 {"test_null_dict", 0, 0, 0, NULL,
1682 ret_dict_any, f_test_null_dict},
1683 {"test_null_function", 0, 0, 0, NULL,
1684 ret_func_any, f_test_null_function},
1685 {"test_null_job", 0, 0, 0, NULL,
1686 ret_job, JOB_FUNC(f_test_null_job)},
1687 {"test_null_list", 0, 0, 0, NULL,
1688 ret_list_any, f_test_null_list},
1689 {"test_null_partial", 0, 0, 0, NULL,
1690 ret_func_any, f_test_null_partial},
1691 {"test_null_string", 0, 0, 0, NULL,
1692 ret_string, f_test_null_string},
1693 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1694 ret_void, f_test_option_not_set},
1695 {"test_override", 2, 2, FEARG_2, NULL,
1696 ret_void, f_test_override},
1697 {"test_refcount", 1, 1, FEARG_1, NULL,
1698 ret_number, f_test_refcount},
1699 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1700 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001701#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001702 f_test_scrollbar
1703#else
1704 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001705#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001706 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001707 {"test_setmouse", 2, 2, 0, NULL,
1708 ret_void, f_test_setmouse},
1709 {"test_settime", 1, 1, FEARG_1, NULL,
1710 ret_void, f_test_settime},
1711 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1712 ret_void, f_test_srand_seed},
1713 {"test_unknown", 0, 0, 0, NULL,
1714 ret_any, f_test_unknown},
1715 {"test_void", 0, 0, 0, NULL,
1716 ret_void, f_test_void},
1717 {"timer_info", 0, 1, FEARG_1, NULL,
1718 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1719 {"timer_pause", 2, 2, FEARG_1, NULL,
1720 ret_void, TIMER_FUNC(f_timer_pause)},
1721 {"timer_start", 2, 3, FEARG_1, NULL,
1722 ret_number, TIMER_FUNC(f_timer_start)},
1723 {"timer_stop", 1, 1, FEARG_1, NULL,
1724 ret_void, TIMER_FUNC(f_timer_stop)},
1725 {"timer_stopall", 0, 0, 0, NULL,
1726 ret_void, TIMER_FUNC(f_timer_stopall)},
1727 {"tolower", 1, 1, FEARG_1, NULL,
1728 ret_string, f_tolower},
1729 {"toupper", 1, 1, FEARG_1, NULL,
1730 ret_string, f_toupper},
1731 {"tr", 3, 3, FEARG_1, NULL,
1732 ret_string, f_tr},
1733 {"trim", 1, 3, FEARG_1, NULL,
1734 ret_string, f_trim},
1735 {"trunc", 1, 1, FEARG_1, NULL,
1736 ret_float, FLOAT_FUNC(f_trunc)},
1737 {"type", 1, 1, FEARG_1, NULL,
1738 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001739 {"typename", 1, 1, FEARG_1, NULL,
1740 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001741 {"undofile", 1, 1, FEARG_1, NULL,
1742 ret_string, f_undofile},
1743 {"undotree", 0, 0, 0, NULL,
1744 ret_dict_any, f_undotree},
1745 {"uniq", 1, 3, FEARG_1, NULL,
1746 ret_list_any, f_uniq},
1747 {"values", 1, 1, FEARG_1, NULL,
1748 ret_list_any, f_values},
1749 {"virtcol", 1, 1, FEARG_1, NULL,
1750 ret_number, f_virtcol},
1751 {"visualmode", 0, 1, 0, NULL,
1752 ret_string, f_visualmode},
1753 {"wildmenumode", 0, 0, 0, NULL,
1754 ret_number, f_wildmenumode},
1755 {"win_execute", 2, 3, FEARG_2, NULL,
1756 ret_string, f_win_execute},
1757 {"win_findbuf", 1, 1, FEARG_1, NULL,
1758 ret_list_number, f_win_findbuf},
1759 {"win_getid", 0, 2, FEARG_1, NULL,
1760 ret_number, f_win_getid},
1761 {"win_gettype", 0, 1, FEARG_1, NULL,
1762 ret_string, f_win_gettype},
1763 {"win_gotoid", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001764 ret_number_bool, f_win_gotoid},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001765 {"win_id2tabwin", 1, 1, FEARG_1, NULL,
1766 ret_list_number, f_win_id2tabwin},
1767 {"win_id2win", 1, 1, FEARG_1, NULL,
1768 ret_number, f_win_id2win},
1769 {"win_screenpos", 1, 1, FEARG_1, NULL,
1770 ret_list_number, f_win_screenpos},
1771 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001772 ret_number_bool, f_win_splitmove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773 {"winbufnr", 1, 1, FEARG_1, NULL,
1774 ret_number, f_winbufnr},
1775 {"wincol", 0, 0, 0, NULL,
1776 ret_number, f_wincol},
1777 {"windowsversion", 0, 0, 0, NULL,
1778 ret_string, f_windowsversion},
1779 {"winheight", 1, 1, FEARG_1, NULL,
1780 ret_number, f_winheight},
1781 {"winlayout", 0, 1, FEARG_1, NULL,
1782 ret_list_any, f_winlayout},
1783 {"winline", 0, 0, 0, NULL,
1784 ret_number, f_winline},
1785 {"winnr", 0, 1, FEARG_1, NULL,
1786 ret_number, f_winnr},
1787 {"winrestcmd", 0, 0, 0, NULL,
1788 ret_string, f_winrestcmd},
1789 {"winrestview", 1, 1, FEARG_1, NULL,
1790 ret_void, f_winrestview},
1791 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001792 ret_dict_number, f_winsaveview},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001793 {"winwidth", 1, 1, FEARG_1, NULL,
1794 ret_number, f_winwidth},
1795 {"wordcount", 0, 0, 0, NULL,
1796 ret_dict_number, f_wordcount},
1797 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001798 ret_number_bool, f_writefile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001799 {"xor", 2, 2, FEARG_1, NULL,
1800 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001801};
1802
K.Takataeecf2b32021-06-02 14:56:39 +02001803#if defined(EBCDIC) || defined(PROTO)
1804/*
1805 * Compare funcentry_T by function name.
1806 */
1807 static int
1808compare_func_name(const void *s1, const void *s2)
1809{
1810 funcentry_T *p1 = (funcentry_T *)s1;
1811 funcentry_T *p2 = (funcentry_T *)s2;
1812
1813 return STRCMP(p1->f_name, p2->f_name);
1814}
1815
1816/*
1817 * Sort the function table by function name.
1818 * The sorting of the table above is ASCII dependent.
1819 * On machines using EBCDIC we have to sort it.
1820 */
1821 void
1822sortFunctions(void)
1823{
1824 size_t funcCnt = ARRAY_LENGTH(global_functions);
1825
1826 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1827}
1828#endif
1829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001830/*
1831 * Function given to ExpandGeneric() to obtain the list of internal
1832 * or user defined function names.
1833 */
1834 char_u *
1835get_function_name(expand_T *xp, int idx)
1836{
1837 static int intidx = -1;
1838 char_u *name;
1839
1840 if (idx == 0)
1841 intidx = -1;
1842 if (intidx < 0)
1843 {
1844 name = get_user_func_name(xp, idx);
1845 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001846 {
1847 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1848 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001849 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001851 }
K.Takataeeec2542021-06-02 13:28:16 +02001852 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001854 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001856 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 STRCAT(IObuff, ")");
1858 return IObuff;
1859 }
1860
1861 return NULL;
1862}
1863
1864/*
1865 * Function given to ExpandGeneric() to obtain the list of internal or
1866 * user defined variable or function names.
1867 */
1868 char_u *
1869get_expr_name(expand_T *xp, int idx)
1870{
1871 static int intidx = -1;
1872 char_u *name;
1873
1874 if (idx == 0)
1875 intidx = -1;
1876 if (intidx < 0)
1877 {
1878 name = get_function_name(xp, idx);
1879 if (name != NULL)
1880 return name;
1881 }
1882 return get_user_var_name(xp, ++intidx);
1883}
1884
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001886 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001887 * Return index, or -1 if not found or "implemented" is TRUE and the function
1888 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001890 static int
1891find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892{
1893 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001894 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 int cmp;
1896 int x;
1897
K.Takataeeec2542021-06-02 13:28:16 +02001898 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001899
1900 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901 while (first <= last)
1902 {
1903 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001904 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001905 if (cmp < 0)
1906 last = x - 1;
1907 else if (cmp > 0)
1908 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001909 else if (implemented && global_functions[x].f_func == NULL)
1910 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911 else
1912 return x;
1913 }
1914 return -1;
1915}
1916
Bram Moolenaar15c47602020-03-26 22:16:48 +01001917/*
1918 * Find internal function "name" in table "global_functions".
1919 * Return index, or -1 if not found or the function is not implemented.
1920 */
1921 int
1922find_internal_func(char_u *name)
1923{
1924 return find_internal_func_opt(name, TRUE);
1925}
1926
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001928has_internal_func(char_u *name)
1929{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001930 return find_internal_func_opt(name, TRUE) >= 0;
1931}
1932
1933 static int
1934has_internal_func_name(char_u *name)
1935{
1936 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001937}
1938
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 char *
1940internal_func_name(int idx)
1941{
1942 return global_functions[idx].f_name;
1943}
1944
Bram Moolenaar94738d82020-10-21 14:25:07 +02001945/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001946 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001947 * Uses the list of types on the type stack: "types".
1948 * Return FAIL and gives an error message when a type is wrong.
1949 */
1950 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001951internal_func_check_arg_types(
1952 type_T **types,
1953 int idx,
1954 int argcount,
1955 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001956{
1957 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1958 int i;
1959
1960 if (argchecks != NULL)
1961 {
1962 argcontext_T context;
1963
1964 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001965 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001966 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001967 for (i = 0; i < argcount; ++i)
1968 if (argchecks[i] != NULL)
1969 {
1970 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02001971 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001972 return FAIL;
1973 }
1974 }
1975 return OK;
1976}
1977
Bram Moolenaara1224cb2020-10-22 12:31:49 +02001978/*
1979 * Call the "f_retfunc" function to obtain the return type of function "idx".
1980 * "argtypes" is the list of argument types or NULL when there are no
1981 * arguments.
1982 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001984internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01001986 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987}
1988
1989/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01001990 * Return TRUE if "idx" is for the map() function.
1991 */
1992 int
1993internal_func_is_map(int idx)
1994{
1995 return global_functions[idx].f_func == f_map;
1996}
1997
1998/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002000 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2001 * first argument, 2 if method base is second argument, etc. 9 if method base
2002 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 */
2004 int
2005check_internal_func(int idx, int argcount)
2006{
2007 int res;
2008 char *name;
2009
2010 if (argcount < global_functions[idx].f_min_argc)
2011 res = FCERR_TOOFEW;
2012 else if (argcount > global_functions[idx].f_max_argc)
2013 res = FCERR_TOOMANY;
2014 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002015 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
2017 name = internal_func_name(idx);
2018 if (res == FCERR_TOOMANY)
2019 semsg(_(e_toomanyarg), name);
2020 else
2021 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002022 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023}
2024
Bram Moolenaarac92e252019-08-03 21:58:38 +02002025 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002026call_internal_func(
2027 char_u *name,
2028 int argcount,
2029 typval_T *argvars,
2030 typval_T *rettv)
2031{
2032 int i;
2033
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002034 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002036 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002037 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002038 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002039 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002040 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002042 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002043 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002044}
2045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 void
2047call_internal_func_by_idx(
2048 int idx,
2049 typval_T *argvars,
2050 typval_T *rettv)
2051{
2052 global_functions[idx].f_func(argvars, rettv);
2053}
2054
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055/*
2056 * Invoke a method for base->method().
2057 */
2058 int
2059call_internal_method(
2060 char_u *name,
2061 int argcount,
2062 typval_T *argvars,
2063 typval_T *rettv,
2064 typval_T *basetv)
2065{
2066 int i;
2067 int fi;
2068 typval_T argv[MAX_FUNC_ARGS + 1];
2069
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002070 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002071 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002072 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002073 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002074 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002075 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002076 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002077 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002078 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002079
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002080 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002081 {
2082 // base value goes last
2083 for (i = 0; i < argcount; ++i)
2084 argv[i] = argvars[i];
2085 argv[argcount] = *basetv;
2086 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002087 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002088 {
2089 // base value goes second
2090 argv[0] = argvars[0];
2091 argv[1] = *basetv;
2092 for (i = 1; i < argcount; ++i)
2093 argv[i + 1] = argvars[i];
2094 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002095 else if (global_functions[fi].f_argtype == FEARG_3)
2096 {
2097 // base value goes third
2098 argv[0] = argvars[0];
2099 argv[1] = argvars[1];
2100 argv[2] = *basetv;
2101 for (i = 2; i < argcount; ++i)
2102 argv[i + 1] = argvars[i];
2103 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002104 else if (global_functions[fi].f_argtype == FEARG_4)
2105 {
2106 // base value goes fourth
2107 argv[0] = argvars[0];
2108 argv[1] = argvars[1];
2109 argv[2] = argvars[2];
2110 argv[3] = *basetv;
2111 for (i = 3; i < argcount; ++i)
2112 argv[i + 1] = argvars[i];
2113 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002114 else
2115 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002116 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002117 argv[0] = *basetv;
2118 for (i = 0; i < argcount; ++i)
2119 argv[i + 1] = argvars[i];
2120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002121 argv[argcount + 1].v_type = VAR_UNKNOWN;
2122
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002123 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002124 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125}
2126
2127/*
2128 * Return TRUE for a non-zero Number and a non-empty String.
2129 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002130 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131non_zero_arg(typval_T *argvars)
2132{
2133 return ((argvars[0].v_type == VAR_NUMBER
2134 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002135 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002136 && argvars[0].vval.v_number == VVAL_TRUE)
2137 || (argvars[0].v_type == VAR_STRING
2138 && argvars[0].vval.v_string != NULL
2139 && *argvars[0].vval.v_string != NUL));
2140}
2141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 * "and(expr, expr)" function
2144 */
2145 static void
2146f_and(typval_T *argvars, typval_T *rettv)
2147{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2149 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002150}
2151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002153 * "balloon_show()" function
2154 */
2155#ifdef FEAT_BEVAL
2156 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002157f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2158{
2159 rettv->v_type = VAR_STRING;
2160 if (balloonEval != NULL)
2161 {
2162 if (balloonEval->msg == NULL)
2163 rettv->vval.v_string = NULL;
2164 else
2165 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2166 }
2167}
2168
2169 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002170f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2171{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002172 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002173 {
2174 if (argvars[0].v_type == VAR_LIST
2175# ifdef FEAT_GUI
2176 && !gui.in_use
2177# endif
2178 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002179 {
2180 list_T *l = argvars[0].vval.v_list;
2181
2182 // empty list removes the balloon
2183 post_balloon(balloonEval, NULL,
2184 l == NULL || l->lv_len == 0 ? NULL : l);
2185 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002186 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002187 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002188 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002189
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002190 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2191 return;
2192
2193 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002194 if (mesg != NULL)
2195 // empty string removes the balloon
2196 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2197 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002198 }
2199}
2200
Bram Moolenaar669a8282017-11-19 20:13:05 +01002201# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002202 static void
2203f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2204{
2205 if (rettv_list_alloc(rettv) == OK)
2206 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002207 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002208
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002209 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2210 return;
2211 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002212 if (msg != NULL)
2213 {
2214 pumitem_T *array;
2215 int size = split_message(msg, &array);
2216 int i;
2217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002218 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002219 for (i = 1; i < size - 1; ++i)
2220 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002221 while (size > 0)
2222 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002223 vim_free(array);
2224 }
2225 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002226}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002227# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002228#endif
2229
2230/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002231 * Get the buffer from "arg" and give an error and return NULL if it is not
2232 * valid.
2233 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002234 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002235get_buf_arg(typval_T *arg)
2236{
2237 buf_T *buf;
2238
2239 ++emsg_off;
2240 buf = tv_get_buf(arg, FALSE);
2241 --emsg_off;
2242 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002244 return buf;
2245}
2246
2247/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002248 * "byte2line(byte)" function
2249 */
2250 static void
2251f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2252{
2253#ifndef FEAT_BYTEOFF
2254 rettv->vval.v_number = -1;
2255#else
2256 long boff = 0;
2257
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002258 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002259 if (boff < 0)
2260 rettv->vval.v_number = -1;
2261 else
2262 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2263 (linenr_T)0, &boff);
2264#endif
2265}
2266
2267 static void
2268byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2269{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002270 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 char_u *str;
2272 varnumber_T idx;
2273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002274 str = tv_get_string_chk(&argvars[0]);
2275 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002276 rettv->vval.v_number = -1;
2277 if (str == NULL || idx < 0)
2278 return;
2279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 t = str;
2281 for ( ; idx > 0; idx--)
2282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002283 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284 return;
2285 if (enc_utf8 && comp)
2286 t += utf_ptr2len(t);
2287 else
2288 t += (*mb_ptr2len)(t);
2289 }
2290 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002291}
2292
2293/*
2294 * "byteidx()" function
2295 */
2296 static void
2297f_byteidx(typval_T *argvars, typval_T *rettv)
2298{
2299 byteidx(argvars, rettv, FALSE);
2300}
2301
2302/*
2303 * "byteidxcomp()" function
2304 */
2305 static void
2306f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2307{
2308 byteidx(argvars, rettv, TRUE);
2309}
2310
2311/*
2312 * "call(func, arglist [, dict])" function
2313 */
2314 static void
2315f_call(typval_T *argvars, typval_T *rettv)
2316{
2317 char_u *func;
2318 partial_T *partial = NULL;
2319 dict_T *selfdict = NULL;
2320
2321 if (argvars[1].v_type != VAR_LIST)
2322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002324 return;
2325 }
2326 if (argvars[1].vval.v_list == NULL)
2327 return;
2328
2329 if (argvars[0].v_type == VAR_FUNC)
2330 func = argvars[0].vval.v_string;
2331 else if (argvars[0].v_type == VAR_PARTIAL)
2332 {
2333 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002334 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002335 }
2336 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002337 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002338 if (func == NULL || *func == NUL)
2339 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002340
2341 if (argvars[2].v_type != VAR_UNKNOWN)
2342 {
2343 if (argvars[2].v_type != VAR_DICT)
2344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346 return;
2347 }
2348 selfdict = argvars[2].vval.v_dict;
2349 }
2350
2351 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2352}
2353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354/*
2355 * "changenr()" function
2356 */
2357 static void
2358f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2359{
2360 rettv->vval.v_number = curbuf->b_u_seq_cur;
2361}
2362
2363/*
2364 * "char2nr(string)" function
2365 */
2366 static void
2367f_char2nr(typval_T *argvars, typval_T *rettv)
2368{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002369 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2370 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (has_mbyte)
2372 {
2373 int utf8 = 0;
2374
2375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002376 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377
2378 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002379 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002381 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 }
2383 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002384 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385}
2386
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002387/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002388 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2389 * returns the character index of the column. Otherwise, returns the byte index
2390 * of the column.
2391 */
2392 static void
2393get_col(typval_T *argvars, typval_T *rettv, int charcol)
2394{
2395 colnr_T col = 0;
2396 pos_T *fp;
2397 int fnum = curbuf->b_fnum;
2398
2399 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2400 if (fp != NULL && fnum == curbuf->b_fnum)
2401 {
2402 if (fp->col == MAXCOL)
2403 {
2404 // '> can be MAXCOL, get the length of the line then
2405 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2406 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2407 else
2408 col = MAXCOL;
2409 }
2410 else
2411 {
2412 col = fp->col + 1;
2413 // col(".") when the cursor is on the NUL at the end of the line
2414 // because of "coladd" can be seen as an extra column.
2415 if (virtual_active() && fp == &curwin->w_cursor)
2416 {
2417 char_u *p = ml_get_cursor();
2418
2419 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2420 curwin->w_virtcol - curwin->w_cursor.coladd))
2421 {
2422 int l;
2423
2424 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2425 col += l;
2426 }
2427 }
2428 }
2429 }
2430 rettv->vval.v_number = col;
2431}
2432
2433/*
2434 * "charcol()" function
2435 */
2436 static void
2437f_charcol(typval_T *argvars, typval_T *rettv)
2438{
2439 get_col(argvars, rettv, TRUE);
2440}
2441
2442/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002443 * "charidx()" function
2444 */
2445 static void
2446f_charidx(typval_T *argvars, typval_T *rettv)
2447{
2448 char_u *str;
2449 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002450 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002451 char_u *p;
2452 int len;
2453 int (*ptr2len)(char_u *);
2454
2455 rettv->vval.v_number = -1;
2456
2457 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2458 || (argvars[2].v_type != VAR_UNKNOWN
2459 && argvars[2].v_type != VAR_NUMBER))
2460 {
2461 emsg(_(e_invarg));
2462 return;
2463 }
2464
2465 str = tv_get_string_chk(&argvars[0]);
2466 idx = tv_get_number_chk(&argvars[1], NULL);
2467 if (str == NULL || idx < 0)
2468 return;
2469
2470 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002471 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002472 if (countcc < 0 || countcc > 1)
2473 {
2474 semsg(_(e_using_number_as_bool_nr), countcc);
2475 return;
2476 }
2477
2478 if (enc_utf8 && countcc)
2479 ptr2len = utf_ptr2len;
2480 else
2481 ptr2len = mb_ptr2len;
2482
2483 for (p = str, len = 0; p <= str + idx; len++)
2484 {
2485 if (*p == NUL)
2486 return;
2487 p += ptr2len(p);
2488 }
2489
2490 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2491}
2492
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002493 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002494get_optional_window(typval_T *argvars, int idx)
2495{
2496 win_T *win = curwin;
2497
2498 if (argvars[idx].v_type != VAR_UNKNOWN)
2499 {
2500 win = find_win_by_nr_or_id(&argvars[idx]);
2501 if (win == NULL)
2502 {
2503 emsg(_(e_invalwindow));
2504 return NULL;
2505 }
2506 }
2507 return win;
2508}
2509
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002511 * "col(string)" function
2512 */
2513 static void
2514f_col(typval_T *argvars, typval_T *rettv)
2515{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002516 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517}
2518
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519/*
2520 * "confirm(message, buttons[, default [, type]])" function
2521 */
2522 static void
2523f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2524{
2525#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2526 char_u *message;
2527 char_u *buttons = NULL;
2528 char_u buf[NUMBUFLEN];
2529 char_u buf2[NUMBUFLEN];
2530 int def = 1;
2531 int type = VIM_GENERIC;
2532 char_u *typestr;
2533 int error = FALSE;
2534
Bram Moolenaarc5809432021-03-27 21:23:30 +01002535 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2536 return;
2537
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002538 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539 if (message == NULL)
2540 error = TRUE;
2541 if (argvars[1].v_type != VAR_UNKNOWN)
2542 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002543 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2544 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002545 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546 if (buttons == NULL)
2547 error = TRUE;
2548 if (argvars[2].v_type != VAR_UNKNOWN)
2549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002550 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 if (argvars[3].v_type != VAR_UNKNOWN)
2552 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002553 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2554 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002555 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556 if (typestr == NULL)
2557 error = TRUE;
2558 else
2559 {
2560 switch (TOUPPER_ASC(*typestr))
2561 {
2562 case 'E': type = VIM_ERROR; break;
2563 case 'Q': type = VIM_QUESTION; break;
2564 case 'I': type = VIM_INFO; break;
2565 case 'W': type = VIM_WARNING; break;
2566 case 'G': type = VIM_GENERIC; break;
2567 }
2568 }
2569 }
2570 }
2571 }
2572
2573 if (buttons == NULL || *buttons == NUL)
2574 buttons = (char_u *)_("&Ok");
2575
2576 if (!error)
2577 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2578 def, NULL, FALSE);
2579#endif
2580}
2581
2582/*
2583 * "copy()" function
2584 */
2585 static void
2586f_copy(typval_T *argvars, typval_T *rettv)
2587{
2588 item_copy(&argvars[0], rettv, FALSE, 0);
2589}
2590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002592 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002593 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002594 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002595 */
2596 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002597set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598{
2599 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002600 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002601 int set_curswant = TRUE;
2602
2603 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002604 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002605 {
2606 pos_T pos;
2607 colnr_T curswant = -1;
2608
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002609 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002611 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002612 return;
2613 }
2614 line = pos.lnum;
2615 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617 if (curswant >= 0)
2618 {
2619 curwin->w_curswant = curswant - 1;
2620 set_curswant = FALSE;
2621 }
2622 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002623 else if ((argvars[0].v_type == VAR_NUMBER ||
2624 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002625 && (argvars[1].v_type == VAR_NUMBER ||
2626 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002629 if (line < 0)
2630 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002631 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002632 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002633 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002635 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002636 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002637 else
2638 {
2639 emsg(_(e_invarg));
2640 return;
2641 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002642 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002643 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 if (line > 0)
2645 curwin->w_cursor.lnum = line;
2646 if (col > 0)
2647 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002650 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002653 if (has_mbyte)
2654 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655
2656 curwin->w_set_curswant = set_curswant;
2657 rettv->vval.v_number = 0;
2658}
2659
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002660/*
2661 * "cursor(lnum, col)" function, or
2662 * "cursor(list)"
2663 *
2664 * Moves the cursor to the specified line and column.
2665 * Returns 0 when the position could be set, -1 otherwise.
2666 */
2667 static void
2668f_cursor(typval_T *argvars, typval_T *rettv)
2669{
2670 set_cursorpos(argvars, rettv, FALSE);
2671}
2672
Bram Moolenaar4f974752019-02-17 17:44:42 +01002673#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002674/*
2675 * "debugbreak()" function
2676 */
2677 static void
2678f_debugbreak(typval_T *argvars, typval_T *rettv)
2679{
2680 int pid;
2681
2682 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002683 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002684 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002685 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002686 else
2687 {
2688 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2689
2690 if (hProcess != NULL)
2691 {
2692 DebugBreakProcess(hProcess);
2693 CloseHandle(hProcess);
2694 rettv->vval.v_number = OK;
2695 }
2696 }
2697}
2698#endif
2699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700/*
2701 * "deepcopy()" function
2702 */
2703 static void
2704f_deepcopy(typval_T *argvars, typval_T *rettv)
2705{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002706 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 int copyID;
2708
2709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002710 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002712 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 else
2714 {
2715 copyID = get_copyID();
2716 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2717 }
2718}
2719
2720/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 * "did_filetype()" function
2722 */
2723 static void
2724f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2725{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002726 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727}
2728
2729/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002730 * "echoraw({expr})" function
2731 */
2732 static void
2733f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2734{
2735 char_u *str = tv_get_string_chk(&argvars[0]);
2736
2737 if (str != NULL && *str != NUL)
2738 {
2739 out_str(str);
2740 out_flush();
2741 }
2742}
2743
2744/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 * "empty({expr})" function
2746 */
2747 static void
2748f_empty(typval_T *argvars, typval_T *rettv)
2749{
2750 int n = FALSE;
2751
2752 switch (argvars[0].v_type)
2753 {
2754 case VAR_STRING:
2755 case VAR_FUNC:
2756 n = argvars[0].vval.v_string == NULL
2757 || *argvars[0].vval.v_string == NUL;
2758 break;
2759 case VAR_PARTIAL:
2760 n = FALSE;
2761 break;
2762 case VAR_NUMBER:
2763 n = argvars[0].vval.v_number == 0;
2764 break;
2765 case VAR_FLOAT:
2766#ifdef FEAT_FLOAT
2767 n = argvars[0].vval.v_float == 0.0;
2768 break;
2769#endif
2770 case VAR_LIST:
2771 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002772 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 break;
2774 case VAR_DICT:
2775 n = argvars[0].vval.v_dict == NULL
2776 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2777 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002778 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002779 case VAR_SPECIAL:
2780 n = argvars[0].vval.v_number != VVAL_TRUE;
2781 break;
2782
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002783 case VAR_BLOB:
2784 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002785 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2786 break;
2787
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 case VAR_JOB:
2789#ifdef FEAT_JOB_CHANNEL
2790 n = argvars[0].vval.v_job == NULL
2791 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2792 break;
2793#endif
2794 case VAR_CHANNEL:
2795#ifdef FEAT_JOB_CHANNEL
2796 n = argvars[0].vval.v_channel == NULL
2797 || !channel_is_open(argvars[0].vval.v_channel);
2798 break;
2799#endif
2800 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002801 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002803 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002804 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 n = TRUE;
2806 break;
2807 }
2808
2809 rettv->vval.v_number = n;
2810}
2811
2812/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002813 * "environ()" function
2814 */
2815 static void
2816f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2817{
2818#if !defined(AMIGA)
2819 int i = 0;
2820 char_u *entry, *value;
2821# ifdef MSWIN
2822 extern wchar_t **_wenviron;
2823# else
2824 extern char **environ;
2825# endif
2826
2827 if (rettv_dict_alloc(rettv) != OK)
2828 return;
2829
2830# ifdef MSWIN
2831 if (*_wenviron == NULL)
2832 return;
2833# else
2834 if (*environ == NULL)
2835 return;
2836# endif
2837
2838 for (i = 0; ; ++i)
2839 {
2840# ifdef MSWIN
2841 short_u *p;
2842
2843 if ((p = (short_u *)_wenviron[i]) == NULL)
2844 return;
2845 entry = utf16_to_enc(p, NULL);
2846# else
2847 if ((entry = (char_u *)environ[i]) == NULL)
2848 return;
2849 entry = vim_strsave(entry);
2850# endif
2851 if (entry == NULL) // out of memory
2852 return;
2853 if ((value = vim_strchr(entry, '=')) == NULL)
2854 {
2855 vim_free(entry);
2856 continue;
2857 }
2858 *value++ = NUL;
2859 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2860 vim_free(entry);
2861 }
2862#endif
2863}
2864
2865/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 * "escape({string}, {chars})" function
2867 */
2868 static void
2869f_escape(typval_T *argvars, typval_T *rettv)
2870{
2871 char_u buf[NUMBUFLEN];
2872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002873 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2874 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 rettv->v_type = VAR_STRING;
2876}
2877
2878/*
2879 * "eval()" function
2880 */
2881 static void
2882f_eval(typval_T *argvars, typval_T *rettv)
2883{
2884 char_u *s, *p;
2885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002886 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002887 if (s != NULL)
2888 s = skipwhite(s);
2889
2890 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 {
2893 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002894 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002895 need_clr_eos = FALSE;
2896 rettv->v_type = VAR_NUMBER;
2897 rettv->vval.v_number = 0;
2898 }
2899 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002900 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901}
2902
2903/*
2904 * "eventhandler()" function
2905 */
2906 static void
2907f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2908{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002909 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910}
2911
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912static garray_T redir_execute_ga;
2913
2914/*
2915 * Append "value[value_len]" to the execute() output.
2916 */
2917 void
2918execute_redir_str(char_u *value, int value_len)
2919{
2920 int len;
2921
2922 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002923 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002925 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 if (ga_grow(&redir_execute_ga, len) == OK)
2927 {
2928 mch_memmove((char *)redir_execute_ga.ga_data
2929 + redir_execute_ga.ga_len, value, len);
2930 redir_execute_ga.ga_len += len;
2931 }
2932}
2933
2934/*
2935 * Get next line from a list.
2936 * Called by do_cmdline() to get the next line.
2937 * Returns allocated string, or NULL for end of function.
2938 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 static char_u *
2940get_list_line(
2941 int c UNUSED,
2942 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002943 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002944 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945{
2946 listitem_T **p = (listitem_T **)cookie;
2947 listitem_T *item = *p;
2948 char_u buf[NUMBUFLEN];
2949 char_u *s;
2950
2951 if (item == NULL)
2952 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002953 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 *p = item->li_next;
2955 return s == NULL ? NULL : vim_strsave(s);
2956}
2957
2958/*
2959 * "execute()" function
2960 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002961 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002962execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963{
2964 char_u *cmd = NULL;
2965 list_T *list = NULL;
2966 int save_msg_silent = msg_silent;
2967 int save_emsg_silent = emsg_silent;
2968 int save_emsg_noredir = emsg_noredir;
2969 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002970 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002972 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002973 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974
2975 rettv->vval.v_string = NULL;
2976 rettv->v_type = VAR_STRING;
2977
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002978 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002980 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002981 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002982 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 return;
2984 ++list->lv_refcount;
2985 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002986 else if (argvars[arg_off].v_type == VAR_JOB
2987 || argvars[arg_off].v_type == VAR_CHANNEL)
2988 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02002989 semsg(_(e_using_invalid_value_as_string_str),
2990 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01002991 return;
2992 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 else
2994 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002995 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 if (cmd == NULL)
2997 return;
2998 }
2999
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003000 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 {
3002 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003003 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004
3005 if (s == NULL)
3006 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003007 if (*s == NUL)
3008 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 if (STRNCMP(s, "silent", 6) == 0)
3010 ++msg_silent;
3011 if (STRCMP(s, "silent!") == 0)
3012 {
3013 emsg_silent = TRUE;
3014 emsg_noredir = TRUE;
3015 }
3016 }
3017 else
3018 ++msg_silent;
3019
3020 if (redir_execute)
3021 save_ga = redir_execute_ga;
3022 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3023 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003024 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003025 if (!echo_output)
3026 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027
3028 if (cmd != NULL)
3029 do_cmdline_cmd(cmd);
3030 else
3031 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003032 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003034 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003035 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 do_cmdline(NULL, get_list_line, (void *)&item,
3037 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3038 --list->lv_refcount;
3039 }
3040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003041 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003042 if (ga_grow(&redir_execute_ga, 1) == OK)
3043 {
3044 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3045 rettv->vval.v_string = redir_execute_ga.ga_data;
3046 }
3047 else
3048 {
3049 ga_clear(&redir_execute_ga);
3050 rettv->vval.v_string = NULL;
3051 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 msg_silent = save_msg_silent;
3053 emsg_silent = save_emsg_silent;
3054 emsg_noredir = save_emsg_noredir;
3055
3056 redir_execute = save_redir_execute;
3057 if (redir_execute)
3058 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003059 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003061 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003062 if (echo_output)
3063 // When not working silently: put it in column zero. A following
3064 // "echon" will overwrite the message, unavoidably.
3065 msg_col = 0;
3066 else
3067 // When working silently: Put it back where it was, since nothing
3068 // should have been written.
3069 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003070}
3071
3072/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003073 * "execute()" function
3074 */
3075 static void
3076f_execute(typval_T *argvars, typval_T *rettv)
3077{
3078 execute_common(argvars, rettv, 0);
3079}
3080
3081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003082 * "exists()" function
3083 */
3084 static void
3085f_exists(typval_T *argvars, typval_T *rettv)
3086{
3087 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003090 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003091 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003093 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094 if (mch_getenv(p + 1) != NULL)
3095 n = TRUE;
3096 else
3097 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003098 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 p = expand_env_save(p);
3100 if (p != NULL && *p != '$')
3101 n = TRUE;
3102 vim_free(p);
3103 }
3104 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003105 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003107 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003109 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003111 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003113 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003114 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003115 else if (*p == '?') // internal function only
3116 {
3117 n = has_internal_func_name(p + 1);
3118 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 else if (*p == ':')
3120 {
3121 n = cmd_exists(p + 1);
3122 }
3123 else if (*p == '#')
3124 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 if (p[1] == '#')
3126 n = autocmd_supported(p + 2);
3127 else
3128 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003130 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003131 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003132 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 }
3134
3135 rettv->vval.v_number = n;
3136}
3137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003138/*
3139 * "expand()" function
3140 */
3141 static void
3142f_expand(typval_T *argvars, typval_T *rettv)
3143{
3144 char_u *s;
3145 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003146 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003147 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3148 expand_T xpc;
3149 int error = FALSE;
3150 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003151#ifdef BACKSLASH_IN_FILENAME
3152 char_u *p_csl_save = p_csl;
3153
3154 // avoid using 'completeslash' here
3155 p_csl = empty_option;
3156#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157
3158 rettv->v_type = VAR_STRING;
3159 if (argvars[1].v_type != VAR_UNKNOWN
3160 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003161 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003163 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003165 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 if (*s == '%' || *s == '#' || *s == '<')
3167 {
3168 ++emsg_off;
3169 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3170 --emsg_off;
3171 if (rettv->v_type == VAR_LIST)
3172 {
3173 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3174 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003175 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003176 }
3177 else
3178 rettv->vval.v_string = result;
3179 }
3180 else
3181 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003182 // When the optional second argument is non-zero, don't remove matches
3183 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003184 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003185 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003186 options |= WILD_KEEP_ALL;
3187 if (!error)
3188 {
3189 ExpandInit(&xpc);
3190 xpc.xp_context = EXPAND_FILES;
3191 if (p_wic)
3192 options += WILD_ICASE;
3193 if (rettv->v_type == VAR_STRING)
3194 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3195 options, WILD_ALL);
3196 else if (rettv_list_alloc(rettv) != FAIL)
3197 {
3198 int i;
3199
3200 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3201 for (i = 0; i < xpc.xp_numfiles; i++)
3202 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3203 ExpandCleanup(&xpc);
3204 }
3205 }
3206 else
3207 rettv->vval.v_string = NULL;
3208 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003209#ifdef BACKSLASH_IN_FILENAME
3210 p_csl = p_csl_save;
3211#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003212}
3213
3214/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003215 * "expandcmd()" function
3216 * Expand all the special characters in a command string.
3217 */
3218 static void
3219f_expandcmd(typval_T *argvars, typval_T *rettv)
3220{
3221 exarg_T eap;
3222 char_u *cmdstr;
3223 char *errormsg = NULL;
3224
3225 rettv->v_type = VAR_STRING;
3226 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3227
3228 memset(&eap, 0, sizeof(eap));
3229 eap.cmd = cmdstr;
3230 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003231 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003232 eap.usefilter = FALSE;
3233 eap.nextcmd = NULL;
3234 eap.cmdidx = CMD_USER;
3235
3236 expand_filename(&eap, &cmdstr, &errormsg);
3237 if (errormsg != NULL && *errormsg != NUL)
3238 emsg(errormsg);
3239
3240 rettv->vval.v_string = cmdstr;
3241}
3242
3243/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 * "feedkeys()" function
3245 */
3246 static void
3247f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3248{
3249 int remap = TRUE;
3250 int insert = FALSE;
3251 char_u *keys, *flags;
3252 char_u nbuf[NUMBUFLEN];
3253 int typed = FALSE;
3254 int execute = FALSE;
3255 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003256 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 char_u *keys_esc;
3258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 // This is not allowed in the sandbox. If the commands would still be
3260 // executed in the sandbox it would be OK, but it probably happens later,
3261 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 if (check_secure())
3263 return;
3264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003265 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266
3267 if (argvars[1].v_type != VAR_UNKNOWN)
3268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003269 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 for ( ; *flags != NUL; ++flags)
3271 {
3272 switch (*flags)
3273 {
3274 case 'n': remap = FALSE; break;
3275 case 'm': remap = TRUE; break;
3276 case 't': typed = TRUE; break;
3277 case 'i': insert = TRUE; break;
3278 case 'x': execute = TRUE; break;
3279 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003280 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281 }
3282 }
3283 }
3284
3285 if (*keys != NUL || execute)
3286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003287 // Need to escape K_SPECIAL and CSI before putting the string in the
3288 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 keys_esc = vim_strsave_escape_csi(keys);
3290 if (keys_esc != NULL)
3291 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003292 if (lowlevel)
3293 {
3294#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003295 int idx;
3296 int len = (int)STRLEN(keys);
3297
3298 for (idx = 0; idx < len; ++idx)
3299 {
3300 // if a CTRL-C was typed, set got_int, similar to what
3301 // happens in fill_input_buf()
3302 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3303 got_int = TRUE;
3304 add_to_input_buf(keys + idx, 1);
3305 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003306#else
3307 emsg(_("E980: lowlevel input not supported"));
3308#endif
3309 }
3310 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003311 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003312 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003313 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003314 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003315#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003316 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003317#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003318 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003319 typebuf_was_filled = TRUE;
3320 }
3321 vim_free(keys_esc);
3322
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003323 if (execute)
3324 {
3325 int save_msg_scroll = msg_scroll;
3326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003327 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 msg_scroll = FALSE;
3329
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003330 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003331 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003332 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003333 ++in_feedkeys;
3334 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003335 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003336 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003337 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003338 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003339 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003340 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 msg_scroll |= save_msg_scroll;
3343 }
3344 }
3345 }
3346}
3347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348/*
3349 * "fnameescape({string})" function
3350 */
3351 static void
3352f_fnameescape(typval_T *argvars, typval_T *rettv)
3353{
3354 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003355 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 rettv->v_type = VAR_STRING;
3357}
3358
3359/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003360 * "foreground()" function
3361 */
3362 static void
3363f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3364{
3365#ifdef FEAT_GUI
3366 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003367 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003369 return;
3370 }
3371#endif
3372#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003373 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003374#endif
3375}
3376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003378common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003379{
3380 char_u *s;
3381 char_u *name;
3382 int use_string = FALSE;
3383 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003384 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003385 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003386
3387 if (argvars[0].v_type == VAR_FUNC)
3388 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003389 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 s = argvars[0].vval.v_string;
3391 }
3392 else if (argvars[0].v_type == VAR_PARTIAL
3393 && argvars[0].vval.v_partial != NULL)
3394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003395 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003396 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003397 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398 }
3399 else
3400 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003401 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003402 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 use_string = TRUE;
3404 }
3405
Bram Moolenaar843b8842016-08-21 14:36:15 +02003406 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003407 {
3408 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003409 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003410 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3411 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003412 if (*name != NUL)
3413 s = NULL;
3414 }
3415
Bram Moolenaar843b8842016-08-21 14:36:15 +02003416 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3417 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003418 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003419 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003420 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003421 ? find_func(trans_name, is_global, NULL) == NULL
3422 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003423 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 else
3425 {
3426 int dict_idx = 0;
3427 int arg_idx = 0;
3428 list_T *list = NULL;
3429
3430 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3431 {
3432 char sid_buf[25];
3433 int off = *s == 's' ? 2 : 5;
3434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003435 // Expand s: and <SID> into <SNR>nr_, so that the function can
3436 // also be called from another script. Using trans_function_name()
3437 // would also work, but some plugins depend on the name being
3438 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003439 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003440 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 if (name != NULL)
3442 {
3443 STRCPY(name, sid_buf);
3444 STRCAT(name, s + off);
3445 }
3446 }
3447 else
3448 name = vim_strsave(s);
3449
3450 if (argvars[1].v_type != VAR_UNKNOWN)
3451 {
3452 if (argvars[2].v_type != VAR_UNKNOWN)
3453 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003454 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 arg_idx = 1;
3456 dict_idx = 2;
3457 }
3458 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003459 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 dict_idx = 1;
3461 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003463 arg_idx = 1;
3464 if (dict_idx > 0)
3465 {
3466 if (argvars[dict_idx].v_type != VAR_DICT)
3467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003468 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003469 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003470 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 }
3472 if (argvars[dict_idx].vval.v_dict == NULL)
3473 dict_idx = 0;
3474 }
3475 if (arg_idx > 0)
3476 {
3477 if (argvars[arg_idx].v_type != VAR_LIST)
3478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003479 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003480 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003481 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 }
3483 list = argvars[arg_idx].vval.v_list;
3484 if (list == NULL || list->lv_len == 0)
3485 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003486 else if (list->lv_len > MAX_FUNC_ARGS)
3487 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003488 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003489 vim_free(name);
3490 goto theend;
3491 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003492 }
3493 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003494 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003495 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003496 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003498 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003499 if (pt == NULL)
3500 vim_free(name);
3501 else
3502 {
3503 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3504 {
3505 listitem_T *li;
3506 int i = 0;
3507 int arg_len = 0;
3508 int lv_len = 0;
3509
3510 if (arg_pt != NULL)
3511 arg_len = arg_pt->pt_argc;
3512 if (list != NULL)
3513 lv_len = list->lv_len;
3514 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003515 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003516 if (pt->pt_argv == NULL)
3517 {
3518 vim_free(pt);
3519 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003520 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003521 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003522 for (i = 0; i < arg_len; i++)
3523 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3524 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003525 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003526 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003527 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003528 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 }
3531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003532 // For "function(dict.func, [], dict)" and "func" is a partial
3533 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534 if (dict_idx > 0)
3535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3538 ++pt->pt_dict->dv_refcount;
3539 }
3540 else if (arg_pt != NULL)
3541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003542 // If the dict was bound automatically the result is also
3543 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 pt->pt_dict = arg_pt->pt_dict;
3545 pt->pt_auto = arg_pt->pt_auto;
3546 if (pt->pt_dict != NULL)
3547 ++pt->pt_dict->dv_refcount;
3548 }
3549
3550 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003551 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3552 {
3553 pt->pt_func = arg_pt->pt_func;
3554 func_ptr_ref(pt->pt_func);
3555 vim_free(name);
3556 }
3557 else if (is_funcref)
3558 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003559 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003560 func_ptr_ref(pt->pt_func);
3561 vim_free(name);
3562 }
3563 else
3564 {
3565 pt->pt_name = name;
3566 func_ref(name);
3567 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 }
3569 rettv->v_type = VAR_PARTIAL;
3570 rettv->vval.v_partial = pt;
3571 }
3572 else
3573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003574 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 rettv->v_type = VAR_FUNC;
3576 rettv->vval.v_string = name;
3577 func_ref(name);
3578 }
3579 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003580theend:
3581 vim_free(trans_name);
3582}
3583
3584/*
3585 * "funcref()" function
3586 */
3587 static void
3588f_funcref(typval_T *argvars, typval_T *rettv)
3589{
3590 common_function(argvars, rettv, TRUE);
3591}
3592
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003593 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003594ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003595{
3596 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3597 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003598 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003599}
3600
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003601/*
3602 * "function()" function
3603 */
3604 static void
3605f_function(typval_T *argvars, typval_T *rettv)
3606{
3607 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608}
3609
3610/*
3611 * "garbagecollect()" function
3612 */
3613 static void
3614f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3615{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003616 // This is postponed until we are back at the toplevel, because we may be
3617 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 want_garbage_collect = TRUE;
3619
Bram Moolenaar2df47312020-09-05 17:30:44 +02003620 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621 garbage_collect_at_exit = TRUE;
3622}
3623
3624/*
3625 * "get()" function
3626 */
3627 static void
3628f_get(typval_T *argvars, typval_T *rettv)
3629{
3630 listitem_T *li;
3631 list_T *l;
3632 dictitem_T *di;
3633 dict_T *d;
3634 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003635 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003637 if (argvars[0].v_type == VAR_BLOB)
3638 {
3639 int error = FALSE;
3640 int idx = tv_get_number_chk(&argvars[1], &error);
3641
3642 if (!error)
3643 {
3644 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003645 if (idx < 0)
3646 idx = blob_len(argvars[0].vval.v_blob) + idx;
3647 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3648 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003649 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003650 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003651 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003652 tv = rettv;
3653 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003654 }
3655 }
3656 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 {
3658 if ((l = argvars[0].vval.v_list) != NULL)
3659 {
3660 int error = FALSE;
3661
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003662 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663 if (!error && li != NULL)
3664 tv = &li->li_tv;
3665 }
3666 }
3667 else if (argvars[0].v_type == VAR_DICT)
3668 {
3669 if ((d = argvars[0].vval.v_dict) != NULL)
3670 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003671 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 if (di != NULL)
3673 tv = &di->di_tv;
3674 }
3675 }
3676 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3677 {
3678 partial_T *pt;
3679 partial_T fref_pt;
3680
3681 if (argvars[0].v_type == VAR_PARTIAL)
3682 pt = argvars[0].vval.v_partial;
3683 else
3684 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003685 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 fref_pt.pt_name = argvars[0].vval.v_string;
3687 pt = &fref_pt;
3688 }
3689
3690 if (pt != NULL)
3691 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003692 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694
3695 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3696 {
3697 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003698 n = partial_name(pt);
3699 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 rettv->vval.v_string = NULL;
3701 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003702 {
3703 rettv->vval.v_string = vim_strsave(n);
3704 if (rettv->v_type == VAR_FUNC)
3705 func_ref(rettv->vval.v_string);
3706 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 }
3708 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003709 {
3710 what_is_dict = TRUE;
3711 if (pt->pt_dict != NULL)
3712 rettv_dict_set(rettv, pt->pt_dict);
3713 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 else if (STRCMP(what, "args") == 0)
3715 {
3716 rettv->v_type = VAR_LIST;
3717 if (rettv_list_alloc(rettv) == OK)
3718 {
3719 int i;
3720
3721 for (i = 0; i < pt->pt_argc; ++i)
3722 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3723 }
3724 }
3725 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003726 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003727
3728 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3729 // third argument
3730 if (!what_is_dict)
3731 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 }
3733 }
3734 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003735 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736
3737 if (tv == NULL)
3738 {
3739 if (argvars[2].v_type != VAR_UNKNOWN)
3740 copy_tv(&argvars[2], rettv);
3741 }
3742 else
3743 copy_tv(tv, rettv);
3744}
3745
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003746/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003747 * "getchangelist()" function
3748 */
3749 static void
3750f_getchangelist(typval_T *argvars, typval_T *rettv)
3751{
3752#ifdef FEAT_JUMPLIST
3753 buf_T *buf;
3754 int i;
3755 list_T *l;
3756 dict_T *d;
3757#endif
3758
3759 if (rettv_list_alloc(rettv) != OK)
3760 return;
3761
3762#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003763 if (argvars[0].v_type == VAR_UNKNOWN)
3764 buf = curbuf;
3765 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003766 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003767 if (buf == NULL)
3768 return;
3769
3770 l = list_alloc();
3771 if (l == NULL)
3772 return;
3773
3774 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3775 return;
3776 /*
3777 * The current window change list index tracks only the position in the
3778 * current buffer change list. For other buffers, use the change list
3779 * length as the current index.
3780 */
3781 list_append_number(rettv->vval.v_list,
3782 (varnumber_T)((buf == curwin->w_buffer)
3783 ? curwin->w_changelistidx : buf->b_changelistlen));
3784
3785 for (i = 0; i < buf->b_changelistlen; ++i)
3786 {
3787 if (buf->b_changelist[i].lnum == 0)
3788 continue;
3789 if ((d = dict_alloc()) == NULL)
3790 return;
3791 if (list_append_dict(l, d) == FAIL)
3792 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003793 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3794 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003795 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003796 }
3797#endif
3798}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003799
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003800 static void
3801getpos_both(
3802 typval_T *argvars,
3803 typval_T *rettv,
3804 int getcurpos,
3805 int charcol)
3806{
3807 pos_T *fp = NULL;
3808 pos_T pos;
3809 win_T *wp = curwin;
3810 list_T *l;
3811 int fnum = -1;
3812
3813 if (rettv_list_alloc(rettv) == OK)
3814 {
3815 l = rettv->vval.v_list;
3816 if (getcurpos)
3817 {
3818 if (argvars[0].v_type != VAR_UNKNOWN)
3819 {
3820 wp = find_win_by_nr_or_id(&argvars[0]);
3821 if (wp != NULL)
3822 fp = &wp->w_cursor;
3823 }
3824 else
3825 fp = &curwin->w_cursor;
3826 if (fp != NULL && charcol)
3827 {
3828 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003829 pos.col =
3830 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003831 fp = &pos;
3832 }
3833 }
3834 else
3835 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3836 if (fnum != -1)
3837 list_append_number(l, (varnumber_T)fnum);
3838 else
3839 list_append_number(l, (varnumber_T)0);
3840 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3841 : (varnumber_T)0);
3842 list_append_number(l, (fp != NULL)
3843 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3844 : (varnumber_T)0);
3845 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3846 (varnumber_T)0);
3847 if (getcurpos)
3848 {
3849 int save_set_curswant = curwin->w_set_curswant;
3850 colnr_T save_curswant = curwin->w_curswant;
3851 colnr_T save_virtcol = curwin->w_virtcol;
3852
3853 if (wp == curwin)
3854 update_curswant();
3855 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3856 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3857
3858 // Do not change "curswant", as it is unexpected that a get
3859 // function has a side effect.
3860 if (wp == curwin && save_set_curswant)
3861 {
3862 curwin->w_set_curswant = save_set_curswant;
3863 curwin->w_curswant = save_curswant;
3864 curwin->w_virtcol = save_virtcol;
3865 curwin->w_valid &= ~VALID_VIRTCOL;
3866 }
3867 }
3868 }
3869 else
3870 rettv->vval.v_number = FALSE;
3871}
3872
3873/*
3874 * "getcharpos()" function
3875 */
3876 static void
3877f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3878{
3879 getpos_both(argvars, rettv, FALSE, TRUE);
3880}
3881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882/*
3883 * "getcharsearch()" function
3884 */
3885 static void
3886f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3887{
3888 if (rettv_dict_alloc(rettv) != FAIL)
3889 {
3890 dict_T *dict = rettv->vval.v_dict;
3891
Bram Moolenaare0be1672018-07-08 16:50:37 +02003892 dict_add_string(dict, "char", last_csearch());
3893 dict_add_number(dict, "forward", last_csearch_forward());
3894 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 }
3896}
3897
3898/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003899 * "getenv()" function
3900 */
3901 static void
3902f_getenv(typval_T *argvars, typval_T *rettv)
3903{
3904 int mustfree = FALSE;
3905 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3906
3907 if (p == NULL)
3908 {
3909 rettv->v_type = VAR_SPECIAL;
3910 rettv->vval.v_number = VVAL_NULL;
3911 return;
3912 }
3913 if (!mustfree)
3914 p = vim_strsave(p);
3915 rettv->vval.v_string = p;
3916 rettv->v_type = VAR_STRING;
3917}
3918
3919/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 * "getfontname()" function
3921 */
3922 static void
3923f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3924{
3925 rettv->v_type = VAR_STRING;
3926 rettv->vval.v_string = NULL;
3927#ifdef FEAT_GUI
3928 if (gui.in_use)
3929 {
3930 GuiFont font;
3931 char_u *name = NULL;
3932
3933 if (argvars[0].v_type == VAR_UNKNOWN)
3934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003935 // Get the "Normal" font. Either the name saved by
3936 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003937 font = gui.norm_font;
3938 name = hl_get_font_name();
3939 }
3940 else
3941 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003942 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003943 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 return;
3945 font = gui_mch_get_font(name, FALSE);
3946 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003947 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 }
3949 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3950 if (argvars[0].v_type != VAR_UNKNOWN)
3951 gui_mch_free_font(font);
3952 }
3953#endif
3954}
3955
3956/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003957 * "getjumplist()" function
3958 */
3959 static void
3960f_getjumplist(typval_T *argvars, typval_T *rettv)
3961{
3962#ifdef FEAT_JUMPLIST
3963 win_T *wp;
3964 int i;
3965 list_T *l;
3966 dict_T *d;
3967#endif
3968
3969 if (rettv_list_alloc(rettv) != OK)
3970 return;
3971
3972#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003973 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01003974 if (wp == NULL)
3975 return;
3976
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01003977 cleanup_jumplist(wp, TRUE);
3978
Bram Moolenaar4f505882018-02-10 21:06:32 +01003979 l = list_alloc();
3980 if (l == NULL)
3981 return;
3982
3983 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3984 return;
3985 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
3986
3987 for (i = 0; i < wp->w_jumplistlen; ++i)
3988 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003989 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
3990 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01003991 if ((d = dict_alloc()) == NULL)
3992 return;
3993 if (list_append_dict(l, d) == FAIL)
3994 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003995 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
3996 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003997 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003998 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01003999 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004000 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004001 }
4002#endif
4003}
4004
4005/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004006 * "getpid()" function
4007 */
4008 static void
4009f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4010{
4011 rettv->vval.v_number = mch_get_pid();
4012}
4013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014/*
4015 * "getcurpos()" function
4016 */
4017 static void
4018f_getcurpos(typval_T *argvars, typval_T *rettv)
4019{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004020 getpos_both(argvars, rettv, TRUE, FALSE);
4021}
4022
4023 static void
4024f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4025{
4026 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004027}
4028
4029/*
4030 * "getpos(string)" function
4031 */
4032 static void
4033f_getpos(typval_T *argvars, typval_T *rettv)
4034{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004035 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004036}
4037
4038/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039 * "getreg()" function
4040 */
4041 static void
4042f_getreg(typval_T *argvars, typval_T *rettv)
4043{
4044 char_u *strregname;
4045 int regname;
4046 int arg2 = FALSE;
4047 int return_list = FALSE;
4048 int error = FALSE;
4049
4050 if (argvars[0].v_type != VAR_UNKNOWN)
4051 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004052 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004053 if (strregname == NULL)
4054 error = TRUE;
4055 else if (in_vim9script() && STRLEN(strregname) > 1)
4056 {
4057 semsg(_(e_register_name_must_be_one_char_str), strregname);
4058 error = TRUE;
4059 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060 if (argvars[1].v_type != VAR_UNKNOWN)
4061 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004062 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004064 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004065 }
4066 }
4067 else
4068 strregname = get_vim_var_str(VV_REG);
4069
4070 if (error)
4071 return;
4072
4073 regname = (strregname == NULL ? '"' : *strregname);
4074 if (regname == 0)
4075 regname = '"';
4076
4077 if (return_list)
4078 {
4079 rettv->v_type = VAR_LIST;
4080 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4081 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4082 if (rettv->vval.v_list == NULL)
4083 (void)rettv_list_alloc(rettv);
4084 else
4085 ++rettv->vval.v_list->lv_refcount;
4086 }
4087 else
4088 {
4089 rettv->v_type = VAR_STRING;
4090 rettv->vval.v_string = get_reg_contents(regname,
4091 arg2 ? GREG_EXPR_SRC : 0);
4092 }
4093}
4094
4095/*
4096 * "getregtype()" function
4097 */
4098 static void
4099f_getregtype(typval_T *argvars, typval_T *rettv)
4100{
4101 char_u *strregname;
4102 int regname;
4103 char_u buf[NUMBUFLEN + 2];
4104 long reglen = 0;
4105
4106 if (argvars[0].v_type != VAR_UNKNOWN)
4107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004108 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004109 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4110 {
4111 semsg(_(e_register_name_must_be_one_char_str), strregname);
4112 strregname = NULL;
4113 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004114 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004115 {
4116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = NULL;
4118 return;
4119 }
4120 }
4121 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004122 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123 strregname = get_vim_var_str(VV_REG);
4124
4125 regname = (strregname == NULL ? '"' : *strregname);
4126 if (regname == 0)
4127 regname = '"';
4128
4129 buf[0] = NUL;
4130 buf[1] = NUL;
4131 switch (get_reg_type(regname, &reglen))
4132 {
4133 case MLINE: buf[0] = 'V'; break;
4134 case MCHAR: buf[0] = 'v'; break;
4135 case MBLOCK:
4136 buf[0] = Ctrl_V;
4137 sprintf((char *)buf + 1, "%ld", reglen + 1);
4138 break;
4139 }
4140 rettv->v_type = VAR_STRING;
4141 rettv->vval.v_string = vim_strsave(buf);
4142}
4143
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004144/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004145 * "gettagstack()" function
4146 */
4147 static void
4148f_gettagstack(typval_T *argvars, typval_T *rettv)
4149{
4150 win_T *wp = curwin; // default is current window
4151
4152 if (rettv_dict_alloc(rettv) != OK)
4153 return;
4154
4155 if (argvars[0].v_type != VAR_UNKNOWN)
4156 {
4157 wp = find_win_by_nr_or_id(&argvars[0]);
4158 if (wp == NULL)
4159 return;
4160 }
4161
4162 get_tagstack(wp, rettv->vval.v_dict);
4163}
4164
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004165/*
4166 * "gettext()" function
4167 */
4168 static void
4169f_gettext(typval_T *argvars, typval_T *rettv)
4170{
4171 if (argvars[0].v_type != VAR_STRING
4172 || argvars[0].vval.v_string == NULL
4173 || *argvars[0].vval.v_string == NUL)
4174 {
4175 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4176 }
4177 else
4178 {
4179 rettv->v_type = VAR_STRING;
4180 rettv->vval.v_string = vim_strsave(
4181 (char_u *)_(argvars[0].vval.v_string));
4182 }
4183}
4184
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004185// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186#include "version.h"
4187
4188/*
4189 * "has()" function
4190 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004191 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004192f_has(typval_T *argvars, typval_T *rettv)
4193{
4194 int i;
4195 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004196 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004197 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004198 typedef struct {
4199 char *name;
4200 short present;
4201 } has_item_T;
4202 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004203 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004204 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004206 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004207#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004208 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004209#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004210 },
4211 {"arp",
4212#if defined(AMIGA) && defined(FEAT_ARP)
4213 1
4214#else
4215 0
4216#endif
4217 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004218 {"haiku",
4219#ifdef __HAIKU__
4220 1
4221#else
4222 0
4223#endif
4224 },
4225 {"bsd",
4226#if defined(BSD) && !defined(MACOS_X)
4227 1
4228#else
4229 0
4230#endif
4231 },
4232 {"hpux",
4233#ifdef hpux
4234 1
4235#else
4236 0
4237#endif
4238 },
4239 {"linux",
4240#ifdef __linux__
4241 1
4242#else
4243 0
4244#endif
4245 },
4246 {"mac", // Mac OS X (and, once, Mac OS Classic)
4247#ifdef MACOS_X
4248 1
4249#else
4250 0
4251#endif
4252 },
4253 {"osx", // Mac OS X
4254#ifdef MACOS_X
4255 1
4256#else
4257 0
4258#endif
4259 },
4260 {"macunix", // Mac OS X, with the darwin feature
4261#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4262 1
4263#else
4264 0
4265#endif
4266 },
4267 {"osxdarwin", // synonym for macunix
4268#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4269 1
4270#else
4271 0
4272#endif
4273 },
4274 {"qnx",
4275#ifdef __QNX__
4276 1
4277#else
4278 0
4279#endif
4280 },
4281 {"sun",
4282#ifdef SUN_SYSTEM
4283 1
4284#else
4285 0
4286#endif
4287 },
4288 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004290 1
4291#else
4292 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004293#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004294 },
4295 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004296#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004297 1
4298#else
4299 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004301 },
4302 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004303#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004304 1
4305#else
4306 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004307#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004308 },
4309 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004310#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004311 1
4312#else
4313 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004314#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004315 },
4316 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004317#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004318 1
4319#else
4320 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004322 },
4323 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004325 1
4326#else
4327 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004329 },
4330 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004332 1
4333#else
4334 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004335#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004336 },
4337 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004339 1
4340#else
4341 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004343 },
4344 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004346 1
4347#else
4348 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004350 },
4351 {"autocmd", 1},
4352 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004353#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004354 1
4355#else
4356 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004357#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004358 },
4359 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004360#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004361 1
4362#else
4363 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004364#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004365 },
4366 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004367#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004368 1
4369#else
4370 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004372 },
4373 {"balloon_multiline",
4374#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4375 // MS-Windows requires runtime check, see below
4376 1
4377#else
4378 0
4379#endif
4380 },
4381 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004382#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004383 1
4384#else
4385 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004386#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004387 },
4388 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004390 1
4391#else
4392 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004393#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004394 },
4395 {"all_builtin_terms",
4396#if defined(ALL_BUILTIN_TCAPS)
4397 1
4398#else
4399 0
4400#endif
4401 },
4402 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004403#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004404 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004406 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004407#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004408 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004410 },
4411 {"byte_offset",
4412#ifdef FEAT_BYTEOFF
4413 1
4414#else
4415 0
4416#endif
4417 },
4418 {"channel",
4419#ifdef FEAT_JOB_CHANNEL
4420 1
4421#else
4422 0
4423#endif
4424 },
4425 {"cindent",
4426#ifdef FEAT_CINDENT
4427 1
4428#else
4429 0
4430#endif
4431 },
4432 {"clientserver",
4433#ifdef FEAT_CLIENTSERVER
4434 1
4435#else
4436 0
4437#endif
4438 },
4439 {"clipboard",
4440#ifdef FEAT_CLIPBOARD
4441 1
4442#else
4443 0
4444#endif
4445 },
4446 {"cmdline_compl", 1},
4447 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004448 {"cmdwin",
4449#ifdef FEAT_CMDWIN
4450 1
4451#else
4452 0
4453#endif
4454 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004455 {"comments", 1},
4456 {"conceal",
4457#ifdef FEAT_CONCEAL
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"cryptv",
4464#ifdef FEAT_CRYPT
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"crypt-blowfish",
4471#ifdef FEAT_CRYPT
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"crypt-blowfish2",
4478#ifdef FEAT_CRYPT
4479 1
4480#else
4481 0
4482#endif
4483 },
4484 {"cscope",
4485#ifdef FEAT_CSCOPE
4486 1
4487#else
4488 0
4489#endif
4490 },
4491 {"cursorbind", 1},
4492 {"cursorshape",
4493#ifdef CURSOR_SHAPE
4494 1
4495#else
4496 0
4497#endif
4498 },
4499 {"debug",
4500#ifdef DEBUG
4501 1
4502#else
4503 0
4504#endif
4505 },
4506 {"dialog_con",
4507#ifdef FEAT_CON_DIALOG
4508 1
4509#else
4510 0
4511#endif
4512 },
4513 {"dialog_gui",
4514#ifdef FEAT_GUI_DIALOG
4515 1
4516#else
4517 0
4518#endif
4519 },
4520 {"diff",
4521#ifdef FEAT_DIFF
4522 1
4523#else
4524 0
4525#endif
4526 },
4527 {"digraphs",
4528#ifdef FEAT_DIGRAPHS
4529 1
4530#else
4531 0
4532#endif
4533 },
4534 {"directx",
4535#ifdef FEAT_DIRECTX
4536 1
4537#else
4538 0
4539#endif
4540 },
4541 {"dnd",
4542#ifdef FEAT_DND
4543 1
4544#else
4545 0
4546#endif
4547 },
4548 {"emacs_tags",
4549#ifdef FEAT_EMACS_TAGS
4550 1
4551#else
4552 0
4553#endif
4554 },
4555 {"eval", 1}, // always present, of course!
4556 {"ex_extra", 1}, // graduated feature
4557 {"extra_search",
4558#ifdef FEAT_SEARCH_EXTRA
4559 1
4560#else
4561 0
4562#endif
4563 },
4564 {"file_in_path",
4565#ifdef FEAT_SEARCHPATH
4566 1
4567#else
4568 0
4569#endif
4570 },
4571 {"filterpipe",
4572#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4573 1
4574#else
4575 0
4576#endif
4577 },
4578 {"find_in_path",
4579#ifdef FEAT_FIND_ID
4580 1
4581#else
4582 0
4583#endif
4584 },
4585 {"float",
4586#ifdef FEAT_FLOAT
4587 1
4588#else
4589 0
4590#endif
4591 },
4592 {"folding",
4593#ifdef FEAT_FOLDING
4594 1
4595#else
4596 0
4597#endif
4598 },
4599 {"footer",
4600#ifdef FEAT_FOOTER
4601 1
4602#else
4603 0
4604#endif
4605 },
4606 {"fork",
4607#if !defined(USE_SYSTEM) && defined(UNIX)
4608 1
4609#else
4610 0
4611#endif
4612 },
4613 {"gettext",
4614#ifdef FEAT_GETTEXT
4615 1
4616#else
4617 0
4618#endif
4619 },
4620 {"gui",
4621#ifdef FEAT_GUI
4622 1
4623#else
4624 0
4625#endif
4626 },
4627 {"gui_neXtaw",
4628#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4629 1
4630#else
4631 0
4632#endif
4633 },
4634 {"gui_athena",
4635#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4636 1
4637#else
4638 0
4639#endif
4640 },
4641 {"gui_gtk",
4642#ifdef FEAT_GUI_GTK
4643 1
4644#else
4645 0
4646#endif
4647 },
4648 {"gui_gtk2",
4649#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4650 1
4651#else
4652 0
4653#endif
4654 },
4655 {"gui_gtk3",
4656#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4657 1
4658#else
4659 0
4660#endif
4661 },
4662 {"gui_gnome",
4663#ifdef FEAT_GUI_GNOME
4664 1
4665#else
4666 0
4667#endif
4668 },
4669 {"gui_haiku",
4670#ifdef FEAT_GUI_HAIKU
4671 1
4672#else
4673 0
4674#endif
4675 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004676 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004677 {"gui_motif",
4678#ifdef FEAT_GUI_MOTIF
4679 1
4680#else
4681 0
4682#endif
4683 },
4684 {"gui_photon",
4685#ifdef FEAT_GUI_PHOTON
4686 1
4687#else
4688 0
4689#endif
4690 },
4691 {"gui_win32",
4692#ifdef FEAT_GUI_MSWIN
4693 1
4694#else
4695 0
4696#endif
4697 },
4698 {"iconv",
4699#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4700 1
4701#else
4702 0
4703#endif
4704 },
4705 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004706 {"ipv6",
4707#ifdef FEAT_IPV6
4708 1
4709#else
4710 0
4711#endif
4712 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004713 {"job",
4714#ifdef FEAT_JOB_CHANNEL
4715 1
4716#else
4717 0
4718#endif
4719 },
4720 {"jumplist",
4721#ifdef FEAT_JUMPLIST
4722 1
4723#else
4724 0
4725#endif
4726 },
4727 {"keymap",
4728#ifdef FEAT_KEYMAP
4729 1
4730#else
4731 0
4732#endif
4733 },
4734 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4735 {"langmap",
4736#ifdef FEAT_LANGMAP
4737 1
4738#else
4739 0
4740#endif
4741 },
4742 {"libcall",
4743#ifdef FEAT_LIBCALL
4744 1
4745#else
4746 0
4747#endif
4748 },
4749 {"linebreak",
4750#ifdef FEAT_LINEBREAK
4751 1
4752#else
4753 0
4754#endif
4755 },
4756 {"lispindent",
4757#ifdef FEAT_LISP
4758 1
4759#else
4760 0
4761#endif
4762 },
4763 {"listcmds", 1},
4764 {"localmap", 1},
4765 {"lua",
4766#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4767 1
4768#else
4769 0
4770#endif
4771 },
4772 {"menu",
4773#ifdef FEAT_MENU
4774 1
4775#else
4776 0
4777#endif
4778 },
4779 {"mksession",
4780#ifdef FEAT_SESSION
4781 1
4782#else
4783 0
4784#endif
4785 },
4786 {"modify_fname", 1},
4787 {"mouse", 1},
4788 {"mouseshape",
4789#ifdef FEAT_MOUSESHAPE
4790 1
4791#else
4792 0
4793#endif
4794 },
4795 {"mouse_dec",
4796#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4797 1
4798#else
4799 0
4800#endif
4801 },
4802 {"mouse_gpm",
4803#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4804 1
4805#else
4806 0
4807#endif
4808 },
4809 {"mouse_jsbterm",
4810#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4811 1
4812#else
4813 0
4814#endif
4815 },
4816 {"mouse_netterm",
4817#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4818 1
4819#else
4820 0
4821#endif
4822 },
4823 {"mouse_pterm",
4824#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4825 1
4826#else
4827 0
4828#endif
4829 },
4830 {"mouse_sgr",
4831#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4832 1
4833#else
4834 0
4835#endif
4836 },
4837 {"mouse_sysmouse",
4838#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4839 1
4840#else
4841 0
4842#endif
4843 },
4844 {"mouse_urxvt",
4845#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4846 1
4847#else
4848 0
4849#endif
4850 },
4851 {"mouse_xterm",
4852#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4853 1
4854#else
4855 0
4856#endif
4857 },
4858 {"multi_byte", 1},
4859 {"multi_byte_ime",
4860#ifdef FEAT_MBYTE_IME
4861 1
4862#else
4863 0
4864#endif
4865 },
4866 {"multi_lang",
4867#ifdef FEAT_MULTI_LANG
4868 1
4869#else
4870 0
4871#endif
4872 },
4873 {"mzscheme",
4874#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4875 1
4876#else
4877 0
4878#endif
4879 },
4880 {"num64", 1},
4881 {"ole",
4882#ifdef FEAT_OLE
4883 1
4884#else
4885 0
4886#endif
4887 },
4888 {"packages",
4889#ifdef FEAT_EVAL
4890 1
4891#else
4892 0
4893#endif
4894 },
4895 {"path_extra",
4896#ifdef FEAT_PATH_EXTRA
4897 1
4898#else
4899 0
4900#endif
4901 },
4902 {"perl",
4903#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4904 1
4905#else
4906 0
4907#endif
4908 },
4909 {"persistent_undo",
4910#ifdef FEAT_PERSISTENT_UNDO
4911 1
4912#else
4913 0
4914#endif
4915 },
4916 {"python_compiled",
4917#if defined(FEAT_PYTHON)
4918 1
4919#else
4920 0
4921#endif
4922 },
4923 {"python_dynamic",
4924#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4925 1
4926#else
4927 0
4928#endif
4929 },
4930 {"python",
4931#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4932 1
4933#else
4934 0
4935#endif
4936 },
4937 {"pythonx",
4938#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4939 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4940 1
4941#else
4942 0
4943#endif
4944 },
4945 {"python3_compiled",
4946#if defined(FEAT_PYTHON3)
4947 1
4948#else
4949 0
4950#endif
4951 },
4952 {"python3_dynamic",
4953#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4954 1
4955#else
4956 0
4957#endif
4958 },
4959 {"python3",
4960#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4961 1
4962#else
4963 0
4964#endif
4965 },
4966 {"popupwin",
4967#ifdef FEAT_PROP_POPUP
4968 1
4969#else
4970 0
4971#endif
4972 },
4973 {"postscript",
4974#ifdef FEAT_POSTSCRIPT
4975 1
4976#else
4977 0
4978#endif
4979 },
4980 {"printer",
4981#ifdef FEAT_PRINTER
4982 1
4983#else
4984 0
4985#endif
4986 },
4987 {"profile",
4988#ifdef FEAT_PROFILE
4989 1
4990#else
4991 0
4992#endif
4993 },
4994 {"reltime",
4995#ifdef FEAT_RELTIME
4996 1
4997#else
4998 0
4999#endif
5000 },
5001 {"quickfix",
5002#ifdef FEAT_QUICKFIX
5003 1
5004#else
5005 0
5006#endif
5007 },
5008 {"rightleft",
5009#ifdef FEAT_RIGHTLEFT
5010 1
5011#else
5012 0
5013#endif
5014 },
5015 {"ruby",
5016#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5017 1
5018#else
5019 0
5020#endif
5021 },
5022 {"scrollbind", 1},
5023 {"showcmd",
5024#ifdef FEAT_CMDL_INFO
5025 1
5026#else
5027 0
5028#endif
5029 },
5030 {"cmdline_info",
5031#ifdef FEAT_CMDL_INFO
5032 1
5033#else
5034 0
5035#endif
5036 },
5037 {"signs",
5038#ifdef FEAT_SIGNS
5039 1
5040#else
5041 0
5042#endif
5043 },
5044 {"smartindent",
5045#ifdef FEAT_SMARTINDENT
5046 1
5047#else
5048 0
5049#endif
5050 },
5051 {"startuptime",
5052#ifdef STARTUPTIME
5053 1
5054#else
5055 0
5056#endif
5057 },
5058 {"statusline",
5059#ifdef FEAT_STL_OPT
5060 1
5061#else
5062 0
5063#endif
5064 },
5065 {"netbeans_intg",
5066#ifdef FEAT_NETBEANS_INTG
5067 1
5068#else
5069 0
5070#endif
5071 },
5072 {"sound",
5073#ifdef FEAT_SOUND
5074 1
5075#else
5076 0
5077#endif
5078 },
5079 {"spell",
5080#ifdef FEAT_SPELL
5081 1
5082#else
5083 0
5084#endif
5085 },
5086 {"syntax",
5087#ifdef FEAT_SYN_HL
5088 1
5089#else
5090 0
5091#endif
5092 },
5093 {"system",
5094#if defined(USE_SYSTEM) || !defined(UNIX)
5095 1
5096#else
5097 0
5098#endif
5099 },
5100 {"tag_binary",
5101#ifdef FEAT_TAG_BINS
5102 1
5103#else
5104 0
5105#endif
5106 },
5107 {"tcl",
5108#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5109 1
5110#else
5111 0
5112#endif
5113 },
5114 {"termguicolors",
5115#ifdef FEAT_TERMGUICOLORS
5116 1
5117#else
5118 0
5119#endif
5120 },
5121 {"terminal",
5122#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5123 1
5124#else
5125 0
5126#endif
5127 },
5128 {"terminfo",
5129#ifdef TERMINFO
5130 1
5131#else
5132 0
5133#endif
5134 },
5135 {"termresponse",
5136#ifdef FEAT_TERMRESPONSE
5137 1
5138#else
5139 0
5140#endif
5141 },
5142 {"textobjects",
5143#ifdef FEAT_TEXTOBJ
5144 1
5145#else
5146 0
5147#endif
5148 },
5149 {"textprop",
5150#ifdef FEAT_PROP_POPUP
5151 1
5152#else
5153 0
5154#endif
5155 },
5156 {"tgetent",
5157#ifdef HAVE_TGETENT
5158 1
5159#else
5160 0
5161#endif
5162 },
5163 {"timers",
5164#ifdef FEAT_TIMERS
5165 1
5166#else
5167 0
5168#endif
5169 },
5170 {"title",
5171#ifdef FEAT_TITLE
5172 1
5173#else
5174 0
5175#endif
5176 },
5177 {"toolbar",
5178#ifdef FEAT_TOOLBAR
5179 1
5180#else
5181 0
5182#endif
5183 },
5184 {"unnamedplus",
5185#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5186 1
5187#else
5188 0
5189#endif
5190 },
5191 {"user-commands", 1}, // was accidentally included in 5.4
5192 {"user_commands", 1},
5193 {"vartabs",
5194#ifdef FEAT_VARTABS
5195 1
5196#else
5197 0
5198#endif
5199 },
5200 {"vertsplit", 1},
5201 {"viminfo",
5202#ifdef FEAT_VIMINFO
5203 1
5204#else
5205 0
5206#endif
5207 },
5208 {"vimscript-1", 1},
5209 {"vimscript-2", 1},
5210 {"vimscript-3", 1},
5211 {"vimscript-4", 1},
5212 {"virtualedit", 1},
5213 {"visual", 1},
5214 {"visualextra", 1},
5215 {"vreplace", 1},
5216 {"vtp",
5217#ifdef FEAT_VTP
5218 1
5219#else
5220 0
5221#endif
5222 },
5223 {"wildignore",
5224#ifdef FEAT_WILDIGN
5225 1
5226#else
5227 0
5228#endif
5229 },
5230 {"wildmenu",
5231#ifdef FEAT_WILDMENU
5232 1
5233#else
5234 0
5235#endif
5236 },
5237 {"windows", 1},
5238 {"winaltkeys",
5239#ifdef FEAT_WAK
5240 1
5241#else
5242 0
5243#endif
5244 },
5245 {"writebackup",
5246#ifdef FEAT_WRITEBACKUP
5247 1
5248#else
5249 0
5250#endif
5251 },
5252 {"xim",
5253#ifdef FEAT_XIM
5254 1
5255#else
5256 0
5257#endif
5258 },
5259 {"xfontset",
5260#ifdef FEAT_XFONTSET
5261 1
5262#else
5263 0
5264#endif
5265 },
5266 {"xpm",
5267#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5268 1
5269#else
5270 0
5271#endif
5272 },
5273 {"xpm_w32", // for backward compatibility
5274#ifdef FEAT_XPM_W32
5275 1
5276#else
5277 0
5278#endif
5279 },
5280 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005282 1
5283#else
5284 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005286 },
5287 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005289 1
5290#else
5291 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005292#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005293 },
5294 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005296 1
5297#else
5298 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005299#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005300 },
5301 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005303 1
5304#else
5305 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005307 },
5308 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005310 1
5311#else
5312 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005314 },
5315 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005316 };
5317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005318 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005319 for (i = 0; has_list[i].name != NULL; ++i)
5320 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005322 x = TRUE;
5323 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 break;
5325 }
5326
Bram Moolenaar79296512020-03-22 16:17:14 +01005327 // features also in has_list[] but sometimes enabled at runtime
5328 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005330 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005331 {
5332 // intentionally empty
5333 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005334#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335 else if (STRICMP(name, "balloon_multiline") == 0)
5336 n = multiline_balloon_available();
5337#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005338#ifdef VIMDLL
5339 else if (STRICMP(name, "filterpipe") == 0)
5340 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341#endif
5342#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5343 else if (STRICMP(name, "iconv") == 0)
5344 n = iconv_enabled(FALSE);
5345#endif
5346#ifdef DYNAMIC_LUA
5347 else if (STRICMP(name, "lua") == 0)
5348 n = lua_enabled(FALSE);
5349#endif
5350#ifdef DYNAMIC_MZSCHEME
5351 else if (STRICMP(name, "mzscheme") == 0)
5352 n = mzscheme_enabled(FALSE);
5353#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005354#ifdef DYNAMIC_PERL
5355 else if (STRICMP(name, "perl") == 0)
5356 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005357#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358#ifdef DYNAMIC_PYTHON
5359 else if (STRICMP(name, "python") == 0)
5360 n = python_enabled(FALSE);
5361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362#ifdef DYNAMIC_PYTHON3
5363 else if (STRICMP(name, "python3") == 0)
5364 n = python3_enabled(FALSE);
5365#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005366#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5367 else if (STRICMP(name, "pythonx") == 0)
5368 {
5369# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5370 if (p_pyx == 0)
5371 n = python3_enabled(FALSE) || python_enabled(FALSE);
5372 else if (p_pyx == 3)
5373 n = python3_enabled(FALSE);
5374 else if (p_pyx == 2)
5375 n = python_enabled(FALSE);
5376# elif defined(DYNAMIC_PYTHON)
5377 n = python_enabled(FALSE);
5378# elif defined(DYNAMIC_PYTHON3)
5379 n = python3_enabled(FALSE);
5380# endif
5381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005383#ifdef DYNAMIC_RUBY
5384 else if (STRICMP(name, "ruby") == 0)
5385 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005387#ifdef DYNAMIC_TCL
5388 else if (STRICMP(name, "tcl") == 0)
5389 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005390#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005391#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005392 else if (STRICMP(name, "terminal") == 0)
5393 n = terminal_enabled();
5394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005395 }
5396
Bram Moolenaar79296512020-03-22 16:17:14 +01005397 // features not in has_list[]
5398 if (x == FALSE)
5399 {
5400 if (STRNICMP(name, "patch", 5) == 0)
5401 {
5402 x = TRUE;
5403 if (name[5] == '-'
5404 && STRLEN(name) >= 11
5405 && vim_isdigit(name[6])
5406 && vim_isdigit(name[8])
5407 && vim_isdigit(name[10]))
5408 {
5409 int major = atoi((char *)name + 6);
5410 int minor = atoi((char *)name + 8);
5411
5412 // Expect "patch-9.9.01234".
5413 n = (major < VIM_VERSION_MAJOR
5414 || (major == VIM_VERSION_MAJOR
5415 && (minor < VIM_VERSION_MINOR
5416 || (minor == VIM_VERSION_MINOR
5417 && has_patch(atoi((char *)name + 10))))));
5418 }
5419 else
5420 n = has_patch(atoi((char *)name + 5));
5421 }
5422 else if (STRICMP(name, "vim_starting") == 0)
5423 {
5424 x = TRUE;
5425 n = (starting != 0);
5426 }
5427 else if (STRICMP(name, "ttyin") == 0)
5428 {
5429 x = TRUE;
5430 n = mch_input_isatty();
5431 }
5432 else if (STRICMP(name, "ttyout") == 0)
5433 {
5434 x = TRUE;
5435 n = stdout_isatty;
5436 }
5437 else if (STRICMP(name, "multi_byte_encoding") == 0)
5438 {
5439 x = TRUE;
5440 n = has_mbyte;
5441 }
5442 else if (STRICMP(name, "gui_running") == 0)
5443 {
5444 x = TRUE;
5445#ifdef FEAT_GUI
5446 n = (gui.in_use || gui.starting);
5447#endif
5448 }
5449 else if (STRICMP(name, "browse") == 0)
5450 {
5451 x = TRUE;
5452#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5453 n = gui.in_use; // gui_mch_browse() works when GUI is running
5454#endif
5455 }
5456 else if (STRICMP(name, "syntax_items") == 0)
5457 {
5458 x = TRUE;
5459#ifdef FEAT_SYN_HL
5460 n = syntax_present(curwin);
5461#endif
5462 }
5463 else if (STRICMP(name, "vcon") == 0)
5464 {
5465 x = TRUE;
5466#ifdef FEAT_VTP
5467 n = is_term_win32() && has_vtp_working();
5468#endif
5469 }
5470 else if (STRICMP(name, "netbeans_enabled") == 0)
5471 {
5472 x = TRUE;
5473#ifdef FEAT_NETBEANS_INTG
5474 n = netbeans_active();
5475#endif
5476 }
5477 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5478 {
5479 x = TRUE;
5480#ifdef FEAT_MOUSE_GPM
5481 n = gpm_enabled();
5482#endif
5483 }
5484 else if (STRICMP(name, "conpty") == 0)
5485 {
5486 x = TRUE;
5487#if defined(FEAT_TERMINAL) && defined(MSWIN)
5488 n = use_conpty();
5489#endif
5490 }
5491 else if (STRICMP(name, "clipboard_working") == 0)
5492 {
5493 x = TRUE;
5494#ifdef FEAT_CLIPBOARD
5495 n = clip_star.available;
5496#endif
5497 }
5498 }
5499
Bram Moolenaar04637e22020-09-05 18:45:29 +02005500 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005501 // return whether feature could ever be enabled
5502 rettv->vval.v_number = x;
5503 else
5504 // return whether feature is enabled
5505 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506}
5507
5508/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005509 * Return TRUE if "feature" can change later.
5510 * Also when checking for the feature has side effects, such as loading a DLL.
5511 */
5512 int
5513dynamic_feature(char_u *feature)
5514{
5515 return (feature == NULL
5516#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5517 || STRICMP(feature, "balloon_multiline") == 0
5518#endif
5519#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5520 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5521#endif
5522#ifdef VIMDLL
5523 || STRICMP(feature, "filterpipe") == 0
5524#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005525#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005526 // this can only change on Unix where the ":gui" command could be
5527 // used.
5528 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5529#endif
5530#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5531 || STRICMP(feature, "iconv") == 0
5532#endif
5533#ifdef DYNAMIC_LUA
5534 || STRICMP(feature, "lua") == 0
5535#endif
5536#ifdef FEAT_MOUSE_GPM
5537 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5538#endif
5539#ifdef DYNAMIC_MZSCHEME
5540 || STRICMP(feature, "mzscheme") == 0
5541#endif
5542#ifdef FEAT_NETBEANS_INTG
5543 || STRICMP(feature, "netbeans_enabled") == 0
5544#endif
5545#ifdef DYNAMIC_PERL
5546 || STRICMP(feature, "perl") == 0
5547#endif
5548#ifdef DYNAMIC_PYTHON
5549 || STRICMP(feature, "python") == 0
5550#endif
5551#ifdef DYNAMIC_PYTHON3
5552 || STRICMP(feature, "python3") == 0
5553#endif
5554#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5555 || STRICMP(feature, "pythonx") == 0
5556#endif
5557#ifdef DYNAMIC_RUBY
5558 || STRICMP(feature, "ruby") == 0
5559#endif
5560#ifdef FEAT_SYN_HL
5561 || STRICMP(feature, "syntax_items") == 0
5562#endif
5563#ifdef DYNAMIC_TCL
5564 || STRICMP(feature, "tcl") == 0
5565#endif
5566 // once "starting" is zero it will stay that way
5567 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5568 || STRICMP(feature, "multi_byte_encoding") == 0
5569#if defined(FEAT_TERMINAL) && defined(MSWIN)
5570 || STRICMP(feature, "conpty") == 0
5571#endif
5572 );
5573}
5574
5575/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005576 * "haslocaldir()" function
5577 */
5578 static void
5579f_haslocaldir(typval_T *argvars, typval_T *rettv)
5580{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005581 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005582 win_T *wp = NULL;
5583
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005584 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5585
5586 // Check for window-local and tab-local directories
5587 if (wp != NULL && wp->w_localdir != NULL)
5588 rettv->vval.v_number = 1;
5589 else if (tp != NULL && tp->tp_localdir != NULL)
5590 rettv->vval.v_number = 2;
5591 else
5592 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005593}
5594
5595/*
5596 * "hasmapto()" function
5597 */
5598 static void
5599f_hasmapto(typval_T *argvars, typval_T *rettv)
5600{
5601 char_u *name;
5602 char_u *mode;
5603 char_u buf[NUMBUFLEN];
5604 int abbr = FALSE;
5605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005606 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005607 if (argvars[1].v_type == VAR_UNKNOWN)
5608 mode = (char_u *)"nvo";
5609 else
5610 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005611 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005613 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 }
5615
5616 if (map_to_exists(name, mode, abbr))
5617 rettv->vval.v_number = TRUE;
5618 else
5619 rettv->vval.v_number = FALSE;
5620}
5621
5622/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 * "highlightID(name)" function
5624 */
5625 static void
5626f_hlID(typval_T *argvars, typval_T *rettv)
5627{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005628 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629}
5630
5631/*
5632 * "highlight_exists()" function
5633 */
5634 static void
5635f_hlexists(typval_T *argvars, typval_T *rettv)
5636{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005637 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638}
5639
5640/*
5641 * "hostname()" function
5642 */
5643 static void
5644f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5645{
5646 char_u hostname[256];
5647
5648 mch_get_host_name(hostname, 256);
5649 rettv->v_type = VAR_STRING;
5650 rettv->vval.v_string = vim_strsave(hostname);
5651}
5652
5653/*
5654 * iconv() function
5655 */
5656 static void
5657f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5658{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 char_u buf1[NUMBUFLEN];
5660 char_u buf2[NUMBUFLEN];
5661 char_u *from, *to, *str;
5662 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005663
5664 rettv->v_type = VAR_STRING;
5665 rettv->vval.v_string = NULL;
5666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005667 str = tv_get_string(&argvars[0]);
5668 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5669 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005670 vimconv.vc_type = CONV_NONE;
5671 convert_setup(&vimconv, from, to);
5672
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005673 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005674 if (vimconv.vc_type == CONV_NONE)
5675 rettv->vval.v_string = vim_strsave(str);
5676 else
5677 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5678
5679 convert_setup(&vimconv, NULL, NULL);
5680 vim_free(from);
5681 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682}
5683
5684/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685 * "index()" function
5686 */
5687 static void
5688f_index(typval_T *argvars, typval_T *rettv)
5689{
5690 list_T *l;
5691 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005692 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 long idx = 0;
5694 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005695 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696
5697 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005698 if (argvars[0].v_type == VAR_BLOB)
5699 {
5700 typval_T tv;
5701 int start = 0;
5702
5703 if (argvars[2].v_type != VAR_UNKNOWN)
5704 {
5705 start = tv_get_number_chk(&argvars[2], &error);
5706 if (error)
5707 return;
5708 }
5709 b = argvars[0].vval.v_blob;
5710 if (b == NULL)
5711 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005712 if (start < 0)
5713 {
5714 start = blob_len(b) + start;
5715 if (start < 0)
5716 start = 0;
5717 }
5718
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005719 for (idx = start; idx < blob_len(b); ++idx)
5720 {
5721 tv.v_type = VAR_NUMBER;
5722 tv.vval.v_number = blob_get(b, idx);
5723 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5724 {
5725 rettv->vval.v_number = idx;
5726 return;
5727 }
5728 }
5729 return;
5730 }
5731 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005732 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005733 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005734 return;
5735 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005736
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005737 l = argvars[0].vval.v_list;
5738 if (l != NULL)
5739 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005741 item = l->lv_first;
5742 if (argvars[2].v_type != VAR_UNKNOWN)
5743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005744 // Start at specified item. Use the cached index that list_find()
5745 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005746 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005747 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005748 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005749 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750 if (error)
5751 item = NULL;
5752 }
5753
5754 for ( ; item != NULL; item = item->li_next, ++idx)
5755 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5756 {
5757 rettv->vval.v_number = idx;
5758 break;
5759 }
5760 }
5761}
5762
5763static int inputsecret_flag = 0;
5764
5765/*
5766 * "input()" function
5767 * Also handles inputsecret() when inputsecret is set.
5768 */
5769 static void
5770f_input(typval_T *argvars, typval_T *rettv)
5771{
5772 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5773}
5774
5775/*
5776 * "inputdialog()" function
5777 */
5778 static void
5779f_inputdialog(typval_T *argvars, typval_T *rettv)
5780{
5781#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005783 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5784 {
5785 char_u *message;
5786 char_u buf[NUMBUFLEN];
5787 char_u *defstr = (char_u *)"";
5788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005789 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005791 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5793 else
5794 IObuff[0] = NUL;
5795 if (message != NULL && defstr != NULL
5796 && do_dialog(VIM_QUESTION, NULL, message,
5797 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5798 rettv->vval.v_string = vim_strsave(IObuff);
5799 else
5800 {
5801 if (message != NULL && defstr != NULL
5802 && argvars[1].v_type != VAR_UNKNOWN
5803 && argvars[2].v_type != VAR_UNKNOWN)
5804 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 else
5807 rettv->vval.v_string = NULL;
5808 }
5809 rettv->v_type = VAR_STRING;
5810 }
5811 else
5812#endif
5813 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5814}
5815
5816/*
5817 * "inputlist()" function
5818 */
5819 static void
5820f_inputlist(typval_T *argvars, typval_T *rettv)
5821{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005822 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823 listitem_T *li;
5824 int selected;
5825 int mouse_used;
5826
5827#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005828 // While starting up, there is no place to enter text. When running tests
5829 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005830 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005831 return;
5832#endif
5833 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005835 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005836 return;
5837 }
5838
5839 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005840 msg_row = Rows - 1; // for when 'cmdheight' > 1
5841 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842 msg_scroll = TRUE;
5843 msg_clr_eos();
5844
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005845 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005846 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005847 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005848 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005849 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 msg_putchar('\n');
5851 }
5852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005853 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 selected = prompt_for_number(&mouse_used);
5855 if (mouse_used)
5856 selected -= lines_left;
5857
5858 rettv->vval.v_number = selected;
5859}
5860
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5862
5863/*
5864 * "inputrestore()" function
5865 */
5866 static void
5867f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5868{
5869 if (ga_userinput.ga_len > 0)
5870 {
5871 --ga_userinput.ga_len;
5872 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
5873 + ga_userinput.ga_len);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005874 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005875 }
5876 else if (p_verbose > 1)
5877 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005878 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005879 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 }
5881}
5882
5883/*
5884 * "inputsave()" function
5885 */
5886 static void
5887f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5888{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005889 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 if (ga_grow(&ga_userinput, 1) == OK)
5891 {
5892 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5893 + ga_userinput.ga_len);
5894 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005895 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 }
5897 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005898 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899}
5900
5901/*
5902 * "inputsecret()" function
5903 */
5904 static void
5905f_inputsecret(typval_T *argvars, typval_T *rettv)
5906{
5907 ++cmdline_star;
5908 ++inputsecret_flag;
5909 f_input(argvars, rettv);
5910 --cmdline_star;
5911 --inputsecret_flag;
5912}
5913
5914/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005915 * "interrupt()" function
5916 */
5917 static void
5918f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5919{
5920 got_int = TRUE;
5921}
5922
5923/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005924 * "invert(expr)" function
5925 */
5926 static void
5927f_invert(typval_T *argvars, typval_T *rettv)
5928{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005929 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930}
5931
5932/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005933 * "islocked()" function
5934 */
5935 static void
5936f_islocked(typval_T *argvars, typval_T *rettv)
5937{
5938 lval_T lv;
5939 char_u *end;
5940 dictitem_T *di;
5941
5942 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005943 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005944 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005945 if (end != NULL && lv.ll_name != NULL)
5946 {
5947 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005948 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005949 else
5950 {
5951 if (lv.ll_tv == NULL)
5952 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005953 di = find_var(lv.ll_name, NULL, TRUE);
5954 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005956 // Consider a variable locked when:
5957 // 1. the variable itself is locked
5958 // 2. the value of the variable is locked.
5959 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005960 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5961 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005962 }
5963 }
5964 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005965 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005967 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
5971 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005972 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
5974 }
5975 }
5976
5977 clear_lval(&lv);
5978}
5979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005980/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005981 * "last_buffer_nr()" function.
5982 */
5983 static void
5984f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
5985{
5986 int n = 0;
5987 buf_T *buf;
5988
Bram Moolenaar29323592016-07-24 22:04:11 +02005989 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990 if (n < buf->b_fnum)
5991 n = buf->b_fnum;
5992
5993 rettv->vval.v_number = n;
5994}
5995
5996/*
5997 * "len()" function
5998 */
5999 static void
6000f_len(typval_T *argvars, typval_T *rettv)
6001{
6002 switch (argvars[0].v_type)
6003 {
6004 case VAR_STRING:
6005 case VAR_NUMBER:
6006 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006009 case VAR_BLOB:
6010 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6011 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 case VAR_LIST:
6013 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6014 break;
6015 case VAR_DICT:
6016 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6017 break;
6018 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006019 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006020 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006021 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 case VAR_SPECIAL:
6023 case VAR_FLOAT:
6024 case VAR_FUNC:
6025 case VAR_PARTIAL:
6026 case VAR_JOB:
6027 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006028 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006029 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 break;
6031 }
6032}
6033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006035libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006036{
6037#ifdef FEAT_LIBCALL
6038 char_u *string_in;
6039 char_u **string_result;
6040 int nr_result;
6041#endif
6042
6043 rettv->v_type = type;
6044 if (type != VAR_NUMBER)
6045 rettv->vval.v_string = NULL;
6046
6047 if (check_restricted() || check_secure())
6048 return;
6049
6050#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006051 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6053 {
6054 string_in = NULL;
6055 if (argvars[2].v_type == VAR_STRING)
6056 string_in = argvars[2].vval.v_string;
6057 if (type == VAR_NUMBER)
6058 string_result = NULL;
6059 else
6060 string_result = &rettv->vval.v_string;
6061 if (mch_libcall(argvars[0].vval.v_string,
6062 argvars[1].vval.v_string,
6063 string_in,
6064 argvars[2].vval.v_number,
6065 string_result,
6066 &nr_result) == OK
6067 && type == VAR_NUMBER)
6068 rettv->vval.v_number = nr_result;
6069 }
6070#endif
6071}
6072
6073/*
6074 * "libcall()" function
6075 */
6076 static void
6077f_libcall(typval_T *argvars, typval_T *rettv)
6078{
6079 libcall_common(argvars, rettv, VAR_STRING);
6080}
6081
6082/*
6083 * "libcallnr()" function
6084 */
6085 static void
6086f_libcallnr(typval_T *argvars, typval_T *rettv)
6087{
6088 libcall_common(argvars, rettv, VAR_NUMBER);
6089}
6090
6091/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006092 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 */
6094 static void
6095f_line(typval_T *argvars, typval_T *rettv)
6096{
6097 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006098 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006100 int id;
6101 tabpage_T *tp;
6102 win_T *wp;
6103 win_T *save_curwin;
6104 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006106 if (argvars[1].v_type != VAR_UNKNOWN)
6107 {
6108 // use window specified in the second argument
6109 id = (int)tv_get_number(&argvars[1]);
6110 wp = win_id2wp_tp(id, &tp);
6111 if (wp != NULL && tp != NULL)
6112 {
6113 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6114 == OK)
6115 {
6116 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006117 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006118 }
6119 restore_win_noblock(save_curwin, save_curtab, TRUE);
6120 }
6121 }
6122 else
6123 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006124 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006126 if (fp != NULL)
6127 lnum = fp->lnum;
6128 rettv->vval.v_number = lnum;
6129}
6130
6131/*
6132 * "line2byte(lnum)" function
6133 */
6134 static void
6135f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6136{
6137#ifndef FEAT_BYTEOFF
6138 rettv->vval.v_number = -1;
6139#else
6140 linenr_T lnum;
6141
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006142 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6144 rettv->vval.v_number = -1;
6145 else
6146 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6147 if (rettv->vval.v_number >= 0)
6148 ++rettv->vval.v_number;
6149#endif
6150}
6151
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152#ifdef FEAT_LUA
6153/*
6154 * "luaeval()" function
6155 */
6156 static void
6157f_luaeval(typval_T *argvars, typval_T *rettv)
6158{
6159 char_u *str;
6160 char_u buf[NUMBUFLEN];
6161
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006162 if (check_restricted() || check_secure())
6163 return;
6164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006165 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 do_luaeval(str, argvars + 1, rettv);
6167}
6168#endif
6169
6170/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006171 * "maparg()" function
6172 */
6173 static void
6174f_maparg(typval_T *argvars, typval_T *rettv)
6175{
6176 get_maparg(argvars, rettv, TRUE);
6177}
6178
6179/*
6180 * "mapcheck()" function
6181 */
6182 static void
6183f_mapcheck(typval_T *argvars, typval_T *rettv)
6184{
6185 get_maparg(argvars, rettv, FALSE);
6186}
6187
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006188typedef enum
6189{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006190 MATCH_END, // matchend()
6191 MATCH_MATCH, // match()
6192 MATCH_STR, // matchstr()
6193 MATCH_LIST, // matchlist()
6194 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006195} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196
6197 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006198find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006199{
6200 char_u *str = NULL;
6201 long len = 0;
6202 char_u *expr = NULL;
6203 char_u *pat;
6204 regmatch_T regmatch;
6205 char_u patbuf[NUMBUFLEN];
6206 char_u strbuf[NUMBUFLEN];
6207 char_u *save_cpo;
6208 long start = 0;
6209 long nth = 1;
6210 colnr_T startcol = 0;
6211 int match = 0;
6212 list_T *l = NULL;
6213 listitem_T *li = NULL;
6214 long idx = 0;
6215 char_u *tofree = NULL;
6216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006217 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006219 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220
6221 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006222 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // type MATCH_LIST: return empty list when there are no matches.
6225 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226 if (rettv_list_alloc(rettv) == FAIL)
6227 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006228 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229 && (list_append_string(rettv->vval.v_list,
6230 (char_u *)"", 0) == FAIL
6231 || list_append_number(rettv->vval.v_list,
6232 (varnumber_T)-1) == FAIL
6233 || list_append_number(rettv->vval.v_list,
6234 (varnumber_T)-1) == FAIL
6235 || list_append_number(rettv->vval.v_list,
6236 (varnumber_T)-1) == FAIL))
6237 {
6238 list_free(rettv->vval.v_list);
6239 rettv->vval.v_list = NULL;
6240 goto theend;
6241 }
6242 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006243 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006244 {
6245 rettv->v_type = VAR_STRING;
6246 rettv->vval.v_string = NULL;
6247 }
6248
6249 if (argvars[0].v_type == VAR_LIST)
6250 {
6251 if ((l = argvars[0].vval.v_list) == NULL)
6252 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006253 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 li = l->lv_first;
6255 }
6256 else
6257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006258 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 len = (long)STRLEN(str);
6260 }
6261
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006262 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 if (pat == NULL)
6264 goto theend;
6265
6266 if (argvars[2].v_type != VAR_UNKNOWN)
6267 {
6268 int error = FALSE;
6269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 if (error)
6272 goto theend;
6273 if (l != NULL)
6274 {
6275 li = list_find(l, start);
6276 if (li == NULL)
6277 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006278 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 }
6280 else
6281 {
6282 if (start < 0)
6283 start = 0;
6284 if (start > len)
6285 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006286 // When "count" argument is there ignore matches before "start",
6287 // otherwise skip part of the string. Differs when pattern is "^"
6288 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 if (argvars[3].v_type != VAR_UNKNOWN)
6290 startcol = start;
6291 else
6292 {
6293 str += start;
6294 len -= start;
6295 }
6296 }
6297
6298 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006299 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300 if (error)
6301 goto theend;
6302 }
6303
6304 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6305 if (regmatch.regprog != NULL)
6306 {
6307 regmatch.rm_ic = p_ic;
6308
6309 for (;;)
6310 {
6311 if (l != NULL)
6312 {
6313 if (li == NULL)
6314 {
6315 match = FALSE;
6316 break;
6317 }
6318 vim_free(tofree);
6319 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6320 if (str == NULL)
6321 break;
6322 }
6323
6324 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6325
6326 if (match && --nth <= 0)
6327 break;
6328 if (l == NULL && !match)
6329 break;
6330
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006331 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332 if (l != NULL)
6333 {
6334 li = li->li_next;
6335 ++idx;
6336 }
6337 else
6338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 startcol = (colnr_T)(regmatch.startp[0]
6340 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341 if (startcol > (colnr_T)len
6342 || str + startcol <= regmatch.startp[0])
6343 {
6344 match = FALSE;
6345 break;
6346 }
6347 }
6348 }
6349
6350 if (match)
6351 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006352 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006353 {
6354 listitem_T *li1 = rettv->vval.v_list->lv_first;
6355 listitem_T *li2 = li1->li_next;
6356 listitem_T *li3 = li2->li_next;
6357 listitem_T *li4 = li3->li_next;
6358
6359 vim_free(li1->li_tv.vval.v_string);
6360 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006361 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006362 li3->li_tv.vval.v_number =
6363 (varnumber_T)(regmatch.startp[0] - expr);
6364 li4->li_tv.vval.v_number =
6365 (varnumber_T)(regmatch.endp[0] - expr);
6366 if (l != NULL)
6367 li2->li_tv.vval.v_number = (varnumber_T)idx;
6368 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006369 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 {
6371 int i;
6372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006373 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374 for (i = 0; i < NSUBEXP; ++i)
6375 {
6376 if (regmatch.endp[i] == NULL)
6377 {
6378 if (list_append_string(rettv->vval.v_list,
6379 (char_u *)"", 0) == FAIL)
6380 break;
6381 }
6382 else if (list_append_string(rettv->vval.v_list,
6383 regmatch.startp[i],
6384 (int)(regmatch.endp[i] - regmatch.startp[i]))
6385 == FAIL)
6386 break;
6387 }
6388 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006389 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006391 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392 if (l != NULL)
6393 copy_tv(&li->li_tv, rettv);
6394 else
6395 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006396 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 }
6398 else if (l != NULL)
6399 rettv->vval.v_number = idx;
6400 else
6401 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006402 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 rettv->vval.v_number =
6404 (varnumber_T)(regmatch.startp[0] - str);
6405 else
6406 rettv->vval.v_number =
6407 (varnumber_T)(regmatch.endp[0] - str);
6408 rettv->vval.v_number += (varnumber_T)(str - expr);
6409 }
6410 }
6411 vim_regfree(regmatch.regprog);
6412 }
6413
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006414theend:
6415 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006416 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417 listitem_remove(rettv->vval.v_list,
6418 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006419 vim_free(tofree);
6420 p_cpo = save_cpo;
6421}
6422
6423/*
6424 * "match()" function
6425 */
6426 static void
6427f_match(typval_T *argvars, typval_T *rettv)
6428{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006429 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430}
6431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432/*
6433 * "matchend()" function
6434 */
6435 static void
6436f_matchend(typval_T *argvars, typval_T *rettv)
6437{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006438 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439}
6440
6441/*
6442 * "matchlist()" function
6443 */
6444 static void
6445f_matchlist(typval_T *argvars, typval_T *rettv)
6446{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006447 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448}
6449
6450/*
6451 * "matchstr()" function
6452 */
6453 static void
6454f_matchstr(typval_T *argvars, typval_T *rettv)
6455{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006456 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457}
6458
6459/*
6460 * "matchstrpos()" function
6461 */
6462 static void
6463f_matchstrpos(typval_T *argvars, typval_T *rettv)
6464{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006465 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466}
6467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468 static void
6469max_min(typval_T *argvars, typval_T *rettv, int domax)
6470{
6471 varnumber_T n = 0;
6472 varnumber_T i;
6473 int error = FALSE;
6474
6475 if (argvars[0].v_type == VAR_LIST)
6476 {
6477 list_T *l;
6478 listitem_T *li;
6479
6480 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006481 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006483 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006485 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6486 n = l->lv_u.nonmat.lv_start;
6487 else
6488 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6489 * l->lv_u.nonmat.lv_stride;
6490 }
6491 else
6492 {
6493 li = l->lv_first;
6494 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006496 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006497 if (error)
6498 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006499 for (;;)
6500 {
6501 li = li->li_next;
6502 if (li == NULL)
6503 break;
6504 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006505 if (error)
6506 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006507 if (domax ? i > n : i < n)
6508 n = i;
6509 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 }
6511 }
6512 }
6513 }
6514 else if (argvars[0].v_type == VAR_DICT)
6515 {
6516 dict_T *d;
6517 int first = TRUE;
6518 hashitem_T *hi;
6519 int todo;
6520
6521 d = argvars[0].vval.v_dict;
6522 if (d != NULL)
6523 {
6524 todo = (int)d->dv_hashtab.ht_used;
6525 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6526 {
6527 if (!HASHITEM_EMPTY(hi))
6528 {
6529 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006530 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006531 if (error)
6532 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 if (first)
6534 {
6535 n = i;
6536 first = FALSE;
6537 }
6538 else if (domax ? i > n : i < n)
6539 n = i;
6540 }
6541 }
6542 }
6543 }
6544 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006545 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006546
6547 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548}
6549
6550/*
6551 * "max()" function
6552 */
6553 static void
6554f_max(typval_T *argvars, typval_T *rettv)
6555{
6556 max_min(argvars, rettv, TRUE);
6557}
6558
6559/*
6560 * "min()" function
6561 */
6562 static void
6563f_min(typval_T *argvars, typval_T *rettv)
6564{
6565 max_min(argvars, rettv, FALSE);
6566}
6567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568#if defined(FEAT_MZSCHEME) || defined(PROTO)
6569/*
6570 * "mzeval()" function
6571 */
6572 static void
6573f_mzeval(typval_T *argvars, typval_T *rettv)
6574{
6575 char_u *str;
6576 char_u buf[NUMBUFLEN];
6577
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006578 if (check_restricted() || check_secure())
6579 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006580 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581 do_mzeval(str, rettv);
6582}
6583
6584 void
6585mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6586{
6587 typval_T argvars[3];
6588
6589 argvars[0].v_type = VAR_STRING;
6590 argvars[0].vval.v_string = name;
6591 copy_tv(args, &argvars[1]);
6592 argvars[2].v_type = VAR_UNKNOWN;
6593 f_call(argvars, rettv);
6594 clear_tv(&argvars[1]);
6595}
6596#endif
6597
6598/*
6599 * "nextnonblank()" function
6600 */
6601 static void
6602f_nextnonblank(typval_T *argvars, typval_T *rettv)
6603{
6604 linenr_T lnum;
6605
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006606 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 {
6608 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6609 {
6610 lnum = 0;
6611 break;
6612 }
6613 if (*skipwhite(ml_get(lnum)) != NUL)
6614 break;
6615 }
6616 rettv->vval.v_number = lnum;
6617}
6618
6619/*
6620 * "nr2char()" function
6621 */
6622 static void
6623f_nr2char(typval_T *argvars, typval_T *rettv)
6624{
6625 char_u buf[NUMBUFLEN];
6626
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627 if (has_mbyte)
6628 {
6629 int utf8 = 0;
6630
6631 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006632 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006633 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006634 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006635 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006636 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006637 }
6638 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006640 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 buf[1] = NUL;
6642 }
6643 rettv->v_type = VAR_STRING;
6644 rettv->vval.v_string = vim_strsave(buf);
6645}
6646
6647/*
6648 * "or(expr, expr)" function
6649 */
6650 static void
6651f_or(typval_T *argvars, typval_T *rettv)
6652{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006653 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6654 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006655}
6656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657#ifdef FEAT_PERL
6658/*
6659 * "perleval()" function
6660 */
6661 static void
6662f_perleval(typval_T *argvars, typval_T *rettv)
6663{
6664 char_u *str;
6665 char_u buf[NUMBUFLEN];
6666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006667 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006668 do_perleval(str, rettv);
6669}
6670#endif
6671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672/*
6673 * "prevnonblank()" function
6674 */
6675 static void
6676f_prevnonblank(typval_T *argvars, typval_T *rettv)
6677{
6678 linenr_T lnum;
6679
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006680 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6682 lnum = 0;
6683 else
6684 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6685 --lnum;
6686 rettv->vval.v_number = lnum;
6687}
6688
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006689// This dummy va_list is here because:
6690// - passing a NULL pointer doesn't work when va_list isn't a pointer
6691// - locally in the function results in a "used before set" warning
6692// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693static va_list ap;
6694
6695/*
6696 * "printf()" function
6697 */
6698 static void
6699f_printf(typval_T *argvars, typval_T *rettv)
6700{
6701 char_u buf[NUMBUFLEN];
6702 int len;
6703 char_u *s;
6704 int saved_did_emsg = did_emsg;
6705 char *fmt;
6706
6707 rettv->v_type = VAR_STRING;
6708 rettv->vval.v_string = NULL;
6709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006710 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006711 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006712 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006713 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714 if (!did_emsg)
6715 {
6716 s = alloc(len + 1);
6717 if (s != NULL)
6718 {
6719 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006720 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6721 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006722 }
6723 }
6724 did_emsg |= saved_did_emsg;
6725}
6726
6727/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006728 * "pum_getpos()" function
6729 */
6730 static void
6731f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6732{
6733 if (rettv_dict_alloc(rettv) != OK)
6734 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006735 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006736}
6737
6738/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 * "pumvisible()" function
6740 */
6741 static void
6742f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6743{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744 if (pum_visible())
6745 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746}
6747
6748#ifdef FEAT_PYTHON3
6749/*
6750 * "py3eval()" function
6751 */
6752 static void
6753f_py3eval(typval_T *argvars, typval_T *rettv)
6754{
6755 char_u *str;
6756 char_u buf[NUMBUFLEN];
6757
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006758 if (check_restricted() || check_secure())
6759 return;
6760
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006761 if (p_pyx == 0)
6762 p_pyx = 3;
6763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006764 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 do_py3eval(str, rettv);
6766}
6767#endif
6768
6769#ifdef FEAT_PYTHON
6770/*
6771 * "pyeval()" function
6772 */
6773 static void
6774f_pyeval(typval_T *argvars, typval_T *rettv)
6775{
6776 char_u *str;
6777 char_u buf[NUMBUFLEN];
6778
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006779 if (check_restricted() || check_secure())
6780 return;
6781
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006782 if (p_pyx == 0)
6783 p_pyx = 2;
6784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006785 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786 do_pyeval(str, rettv);
6787}
6788#endif
6789
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006790#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6791/*
6792 * "pyxeval()" function
6793 */
6794 static void
6795f_pyxeval(typval_T *argvars, typval_T *rettv)
6796{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006797 if (check_restricted() || check_secure())
6798 return;
6799
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006800# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6801 init_pyxversion();
6802 if (p_pyx == 2)
6803 f_pyeval(argvars, rettv);
6804 else
6805 f_py3eval(argvars, rettv);
6806# elif defined(FEAT_PYTHON)
6807 f_pyeval(argvars, rettv);
6808# elif defined(FEAT_PYTHON3)
6809 f_py3eval(argvars, rettv);
6810# endif
6811}
6812#endif
6813
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006814static UINT32_T srand_seed_for_testing = 0;
6815static int srand_seed_for_testing_is_used = FALSE;
6816
6817 static void
6818f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6819{
6820 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006821 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006822 else
6823 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006824 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6825 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006826 }
6827}
6828
6829 static void
6830init_srand(UINT32_T *x)
6831{
6832#ifndef MSWIN
6833 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6834#endif
6835
6836 if (srand_seed_for_testing_is_used)
6837 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006838 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006839 return;
6840 }
6841#ifndef MSWIN
6842 if (dev_urandom_state != FAIL)
6843 {
6844 int fd = open("/dev/urandom", O_RDONLY);
6845 struct {
6846 union {
6847 UINT32_T number;
6848 char bytes[sizeof(UINT32_T)];
6849 } contents;
6850 } buf;
6851
6852 // Attempt reading /dev/urandom.
6853 if (fd == -1)
6854 dev_urandom_state = FAIL;
6855 else
6856 {
6857 buf.contents.number = 0;
6858 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6859 != sizeof(UINT32_T))
6860 dev_urandom_state = FAIL;
6861 else
6862 {
6863 dev_urandom_state = OK;
6864 *x = buf.contents.number;
6865 }
6866 close(fd);
6867 }
6868 }
6869 if (dev_urandom_state != OK)
6870 // Reading /dev/urandom doesn't work, fall back to time().
6871#endif
6872 *x = vim_time();
6873}
6874
6875#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6876#define SPLITMIX32(x, z) ( \
6877 z = (x += 0x9e3779b9), \
6878 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6879 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6880 z ^ (z >> 16) \
6881 )
6882#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6883 result = ROTL(y * 5, 7) * 9; \
6884 t = y << 9; \
6885 z ^= x; \
6886 w ^= y; \
6887 y ^= z, x ^= w; \
6888 z ^= t; \
6889 w = ROTL(w, 11);
6890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006892 * "rand()" function
6893 */
6894 static void
6895f_rand(typval_T *argvars, typval_T *rettv)
6896{
6897 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006898 static UINT32_T gx, gy, gz, gw;
6899 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006900 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006901 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006902
6903 if (argvars[0].v_type == VAR_UNKNOWN)
6904 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006905 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006906 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006907 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006908 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006909 init_srand(&x);
6910
6911 gx = SPLITMIX32(x, z);
6912 gy = SPLITMIX32(x, z);
6913 gz = SPLITMIX32(x, z);
6914 gw = SPLITMIX32(x, z);
6915 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006916 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006917
6918 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006919 }
6920 else if (argvars[0].v_type == VAR_LIST)
6921 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006922 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006923 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006924 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006925
6926 lx = list_find(l, 0L);
6927 ly = list_find(l, 1L);
6928 lz = list_find(l, 2L);
6929 lw = list_find(l, 3L);
6930 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6931 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6932 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6933 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6934 x = (UINT32_T)lx->li_tv.vval.v_number;
6935 y = (UINT32_T)ly->li_tv.vval.v_number;
6936 z = (UINT32_T)lz->li_tv.vval.v_number;
6937 w = (UINT32_T)lw->li_tv.vval.v_number;
6938
6939 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6940
6941 lx->li_tv.vval.v_number = (varnumber_T)x;
6942 ly->li_tv.vval.v_number = (varnumber_T)y;
6943 lz->li_tv.vval.v_number = (varnumber_T)z;
6944 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006945 }
6946 else
6947 goto theend;
6948
6949 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006950 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006951 return;
6952
6953theend:
6954 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006955 rettv->v_type = VAR_NUMBER;
6956 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006957}
6958
6959/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006960 * "srand()" function
6961 */
6962 static void
6963f_srand(typval_T *argvars, typval_T *rettv)
6964{
6965 UINT32_T x = 0, z;
6966
6967 if (rettv_list_alloc(rettv) == FAIL)
6968 return;
6969 if (argvars[0].v_type == VAR_UNKNOWN)
6970 {
6971 init_srand(&x);
6972 }
6973 else
6974 {
6975 int error = FALSE;
6976
6977 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
6978 if (error)
6979 return;
6980 }
6981
6982 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6983 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6984 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6985 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
6986}
6987
6988#undef ROTL
6989#undef SPLITMIX32
6990#undef SHUFFLE_XOSHIRO128STARSTAR
6991
6992/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993 * "range()" function
6994 */
6995 static void
6996f_range(typval_T *argvars, typval_T *rettv)
6997{
6998 varnumber_T start;
6999 varnumber_T end;
7000 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 int error = FALSE;
7002
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007003 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004 if (argvars[1].v_type == VAR_UNKNOWN)
7005 {
7006 end = start - 1;
7007 start = 0;
7008 }
7009 else
7010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007011 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007013 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014 }
7015
7016 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007017 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007018 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007019 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007020 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007021 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007022 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007024 list_T *list = rettv->vval.v_list;
7025
7026 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007027 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007028 // be called.
7029 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007030 list->lv_u.nonmat.lv_start = start;
7031 list->lv_u.nonmat.lv_end = end;
7032 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007033 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007034 }
7035}
7036
7037/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007038 * Materialize "list".
7039 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007040 */
7041 void
7042range_list_materialize(list_T *list)
7043{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007044 varnumber_T start = list->lv_u.nonmat.lv_start;
7045 varnumber_T end = list->lv_u.nonmat.lv_end;
7046 int stride = list->lv_u.nonmat.lv_stride;
7047 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007049 list->lv_first = NULL;
7050 list->lv_u.mat.lv_last = NULL;
7051 list->lv_len = 0;
7052 list->lv_u.mat.lv_idx_item = NULL;
7053 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7054 if (list_append_number(list, (varnumber_T)i) == FAIL)
7055 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056}
7057
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007058/*
7059 * "getreginfo()" function
7060 */
7061 static void
7062f_getreginfo(typval_T *argvars, typval_T *rettv)
7063{
7064 char_u *strregname;
7065 int regname;
7066 char_u buf[NUMBUFLEN + 2];
7067 long reglen = 0;
7068 dict_T *dict;
7069 list_T *list;
7070
7071 if (argvars[0].v_type != VAR_UNKNOWN)
7072 {
7073 strregname = tv_get_string_chk(&argvars[0]);
7074 if (strregname == NULL)
7075 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007076 if (in_vim9script() && STRLEN(strregname) > 1)
7077 {
7078 semsg(_(e_register_name_must_be_one_char_str), strregname);
7079 return;
7080 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007081 }
7082 else
7083 strregname = get_vim_var_str(VV_REG);
7084
7085 regname = (strregname == NULL ? '"' : *strregname);
7086 if (regname == 0 || regname == '@')
7087 regname = '"';
7088
7089 if (rettv_dict_alloc(rettv) == FAIL)
7090 return;
7091 dict = rettv->vval.v_dict;
7092
7093 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7094 if (list == NULL)
7095 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007096 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007097
7098 buf[0] = NUL;
7099 buf[1] = NUL;
7100 switch (get_reg_type(regname, &reglen))
7101 {
7102 case MLINE: buf[0] = 'V'; break;
7103 case MCHAR: buf[0] = 'v'; break;
7104 case MBLOCK:
7105 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7106 reglen + 1);
7107 break;
7108 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007109 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007110
7111 buf[0] = get_register_name(get_unname_register());
7112 buf[1] = NUL;
7113 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007114 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007115 else
7116 {
7117 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7118
7119 if (item != NULL)
7120 {
7121 item->di_tv.v_type = VAR_SPECIAL;
7122 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007123 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007124 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007125 }
7126 }
7127}
7128
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007129 static void
7130return_register(int regname, typval_T *rettv)
7131{
7132 char_u buf[2] = {0, 0};
7133
7134 buf[0] = (char_u)regname;
7135 rettv->v_type = VAR_STRING;
7136 rettv->vval.v_string = vim_strsave(buf);
7137}
7138
7139/*
7140 * "reg_executing()" function
7141 */
7142 static void
7143f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7144{
7145 return_register(reg_executing, rettv);
7146}
7147
7148/*
7149 * "reg_recording()" function
7150 */
7151 static void
7152f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7153{
7154 return_register(reg_recording, rettv);
7155}
7156
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007157/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158 * "rename({from}, {to})" function
7159 */
7160 static void
7161f_rename(typval_T *argvars, typval_T *rettv)
7162{
7163 char_u buf[NUMBUFLEN];
7164
7165 if (check_restricted() || check_secure())
7166 rettv->vval.v_number = -1;
7167 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007168 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7169 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170}
7171
7172/*
7173 * "repeat()" function
7174 */
7175 static void
7176f_repeat(typval_T *argvars, typval_T *rettv)
7177{
7178 char_u *p;
7179 int n;
7180 int slen;
7181 int len;
7182 char_u *r;
7183 int i;
7184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007185 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007186 if (argvars[0].v_type == VAR_LIST)
7187 {
7188 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7189 while (n-- > 0)
7190 if (list_extend(rettv->vval.v_list,
7191 argvars[0].vval.v_list, NULL) == FAIL)
7192 break;
7193 }
7194 else
7195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007196 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007197 rettv->v_type = VAR_STRING;
7198 rettv->vval.v_string = NULL;
7199
7200 slen = (int)STRLEN(p);
7201 len = slen * n;
7202 if (len <= 0)
7203 return;
7204
7205 r = alloc(len + 1);
7206 if (r != NULL)
7207 {
7208 for (i = 0; i < n; i++)
7209 mch_memmove(r + i * slen, p, (size_t)slen);
7210 r[len] = NUL;
7211 }
7212
7213 rettv->vval.v_string = r;
7214 }
7215}
7216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007217#define SP_NOMOVE 0x01 // don't move cursor
7218#define SP_REPEAT 0x02 // repeat to find outer pair
7219#define SP_RETCOUNT 0x04 // return matchcount
7220#define SP_SETPCMARK 0x08 // set previous context mark
7221#define SP_START 0x10 // accept match at start position
7222#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7223#define SP_END 0x40 // leave cursor at end of match
7224#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226/*
7227 * Get flags for a search function.
7228 * Possibly sets "p_ws".
7229 * Returns BACKWARD, FORWARD or zero (for an error).
7230 */
7231 static int
7232get_search_arg(typval_T *varp, int *flagsp)
7233{
7234 int dir = FORWARD;
7235 char_u *flags;
7236 char_u nbuf[NUMBUFLEN];
7237 int mask;
7238
7239 if (varp->v_type != VAR_UNKNOWN)
7240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007241 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007243 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244 while (*flags != NUL)
7245 {
7246 switch (*flags)
7247 {
7248 case 'b': dir = BACKWARD; break;
7249 case 'w': p_ws = TRUE; break;
7250 case 'W': p_ws = FALSE; break;
7251 default: mask = 0;
7252 if (flagsp != NULL)
7253 switch (*flags)
7254 {
7255 case 'c': mask = SP_START; break;
7256 case 'e': mask = SP_END; break;
7257 case 'm': mask = SP_RETCOUNT; break;
7258 case 'n': mask = SP_NOMOVE; break;
7259 case 'p': mask = SP_SUBPAT; break;
7260 case 'r': mask = SP_REPEAT; break;
7261 case 's': mask = SP_SETPCMARK; break;
7262 case 'z': mask = SP_COLUMN; break;
7263 }
7264 if (mask == 0)
7265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007266 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 dir = 0;
7268 }
7269 else
7270 *flagsp |= mask;
7271 }
7272 if (dir == 0)
7273 break;
7274 ++flags;
7275 }
7276 }
7277 return dir;
7278}
7279
7280/*
7281 * Shared by search() and searchpos() functions.
7282 */
7283 static int
7284search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7285{
7286 int flags;
7287 char_u *pat;
7288 pos_T pos;
7289 pos_T save_cursor;
7290 int save_p_ws = p_ws;
7291 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007292 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007295 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296 long time_limit = 0;
7297#endif
7298 int options = SEARCH_KEEP;
7299 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007300 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007301 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007302 pos_T firstpos;
7303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007304 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007305 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 if (dir == 0)
7307 goto theend;
7308 flags = *flagsp;
7309 if (flags & SP_START)
7310 options |= SEARCH_START;
7311 if (flags & SP_END)
7312 options |= SEARCH_END;
7313 if (flags & SP_COLUMN)
7314 options |= SEARCH_COL;
7315
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007316 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (lnum_stop < 0)
7321 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322 if (argvars[3].v_type != VAR_UNKNOWN)
7323 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007324#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007325 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 if (time_limit < 0)
7327 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007329 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007330 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 }
7332
7333#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007334 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007335 profile_setlimit(time_limit, &tm);
7336#endif
7337
7338 /*
7339 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7340 * Check to make sure only those flags are set.
7341 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7342 * flags cannot be set. Check for that condition also.
7343 */
7344 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7345 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007347 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 goto theend;
7349 }
7350
7351 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007352 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007353 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007354 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7355#ifdef FEAT_RELTIME
7356 sia.sa_tm = &tm;
7357#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007358
7359 // Repeat until {skip} returns FALSE.
7360 for (;;)
7361 {
7362 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007363 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007364 // finding the first match again means there is no match where {skip}
7365 // evaluates to zero.
7366 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7367 subpatnum = FAIL;
7368
Bram Moolenaara9c01042020-06-07 14:50:50 +02007369 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007370 // didn't find it or no skip argument
7371 break;
7372 firstpos = pos;
7373
Bram Moolenaara9c01042020-06-07 14:50:50 +02007374 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007375 {
7376 int do_skip;
7377 int err;
7378 pos_T save_pos = curwin->w_cursor;
7379
7380 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007381 err = FALSE;
7382 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007383 curwin->w_cursor = save_pos;
7384 if (err)
7385 {
7386 // Evaluating {skip} caused an error, break here.
7387 subpatnum = FAIL;
7388 break;
7389 }
7390 if (!do_skip)
7391 break;
7392 }
7393 }
7394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 if (subpatnum != FAIL)
7396 {
7397 if (flags & SP_SUBPAT)
7398 retval = subpatnum;
7399 else
7400 retval = pos.lnum;
7401 if (flags & SP_SETPCMARK)
7402 setpcmark();
7403 curwin->w_cursor = pos;
7404 if (match_pos != NULL)
7405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007406 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 match_pos->lnum = pos.lnum;
7408 match_pos->col = pos.col + 1;
7409 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007410 // "/$" will put the cursor after the end of the line, may need to
7411 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 check_cursor();
7413 }
7414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007415 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007416 if (flags & SP_NOMOVE)
7417 curwin->w_cursor = save_cursor;
7418 else
7419 curwin->w_set_curswant = TRUE;
7420theend:
7421 p_ws = save_p_ws;
7422
7423 return retval;
7424}
7425
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007426#ifdef FEAT_RUBY
7427/*
7428 * "rubyeval()" function
7429 */
7430 static void
7431f_rubyeval(typval_T *argvars, typval_T *rettv)
7432{
7433 char_u *str;
7434 char_u buf[NUMBUFLEN];
7435
7436 str = tv_get_string_buf(&argvars[0], buf);
7437 do_rubyeval(str, rettv);
7438}
7439#endif
7440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441/*
7442 * "screenattr()" function
7443 */
7444 static void
7445f_screenattr(typval_T *argvars, typval_T *rettv)
7446{
7447 int row;
7448 int col;
7449 int c;
7450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007451 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7452 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 if (row < 0 || row >= screen_Rows
7454 || col < 0 || col >= screen_Columns)
7455 c = -1;
7456 else
7457 c = ScreenAttrs[LineOffset[row] + col];
7458 rettv->vval.v_number = c;
7459}
7460
7461/*
7462 * "screenchar()" function
7463 */
7464 static void
7465f_screenchar(typval_T *argvars, typval_T *rettv)
7466{
7467 int row;
7468 int col;
7469 int off;
7470 int c;
7471
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007472 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7473 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007474 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007475 c = -1;
7476 else
7477 {
7478 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 if (enc_utf8 && ScreenLinesUC[off] != 0)
7480 c = ScreenLinesUC[off];
7481 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 c = ScreenLines[off];
7483 }
7484 rettv->vval.v_number = c;
7485}
7486
7487/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007488 * "screenchars()" function
7489 */
7490 static void
7491f_screenchars(typval_T *argvars, typval_T *rettv)
7492{
7493 int row;
7494 int col;
7495 int off;
7496 int c;
7497 int i;
7498
7499 if (rettv_list_alloc(rettv) == FAIL)
7500 return;
7501 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7502 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7503 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7504 return;
7505
7506 off = LineOffset[row] + col;
7507 if (enc_utf8 && ScreenLinesUC[off] != 0)
7508 c = ScreenLinesUC[off];
7509 else
7510 c = ScreenLines[off];
7511 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7512
7513 if (enc_utf8)
7514
7515 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7516 list_append_number(rettv->vval.v_list,
7517 (varnumber_T)ScreenLinesC[i][off]);
7518}
7519
7520/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 * "screencol()" function
7522 *
7523 * First column is 1 to be consistent with virtcol().
7524 */
7525 static void
7526f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7527{
7528 rettv->vval.v_number = screen_screencol() + 1;
7529}
7530
7531/*
7532 * "screenrow()" function
7533 */
7534 static void
7535f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7536{
7537 rettv->vval.v_number = screen_screenrow() + 1;
7538}
7539
7540/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007541 * "screenstring()" function
7542 */
7543 static void
7544f_screenstring(typval_T *argvars, typval_T *rettv)
7545{
7546 int row;
7547 int col;
7548 int off;
7549 int c;
7550 int i;
7551 char_u buf[MB_MAXBYTES + 1];
7552 int buflen = 0;
7553
7554 rettv->vval.v_string = NULL;
7555 rettv->v_type = VAR_STRING;
7556
7557 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7558 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7559 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7560 return;
7561
7562 off = LineOffset[row] + col;
7563 if (enc_utf8 && ScreenLinesUC[off] != 0)
7564 c = ScreenLinesUC[off];
7565 else
7566 c = ScreenLines[off];
7567 buflen += mb_char2bytes(c, buf);
7568
Bram Moolenaarf1387282021-03-22 17:11:15 +01007569 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007570 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7571 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7572
7573 buf[buflen] = NUL;
7574 rettv->vval.v_string = vim_strsave(buf);
7575}
7576
7577/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007578 * "search()" function
7579 */
7580 static void
7581f_search(typval_T *argvars, typval_T *rettv)
7582{
7583 int flags = 0;
7584
7585 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7586}
7587
7588/*
7589 * "searchdecl()" function
7590 */
7591 static void
7592f_searchdecl(typval_T *argvars, typval_T *rettv)
7593{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007594 int locally = TRUE;
7595 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596 int error = FALSE;
7597 char_u *name;
7598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007599 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007601 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 if (argvars[1].v_type != VAR_UNKNOWN)
7603 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007604 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007606 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 }
7608 if (!error && name != NULL)
7609 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7610 locally, thisblock, SEARCH_KEEP) == FAIL;
7611}
7612
7613/*
7614 * Used by searchpair() and searchpairpos()
7615 */
7616 static int
7617searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7618{
7619 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007620 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007621 int save_p_ws = p_ws;
7622 int dir;
7623 int flags = 0;
7624 char_u nbuf1[NUMBUFLEN];
7625 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007626 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007627 long lnum_stop = 0;
7628 long time_limit = 0;
7629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007630 // Get the three pattern arguments: start, middle, end. Will result in an
7631 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007632 spat = tv_get_string_chk(&argvars[0]);
7633 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7634 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007636 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007638 // Handle the optional fourth argument: flags
7639 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 if (dir == 0)
7641 goto theend;
7642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007643 // Don't accept SP_END or SP_SUBPAT.
7644 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 if ((flags & (SP_END | SP_SUBPAT)) != 0
7646 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007648 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007649 goto theend;
7650 }
7651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007652 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 if (flags & SP_REPEAT)
7654 p_ws = FALSE;
7655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007656 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007657 if (argvars[3].v_type == VAR_UNKNOWN
7658 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007659 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 else
7661 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007662 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007663 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 if (argvars[5].v_type != VAR_UNKNOWN)
7666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007667 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007670 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007672 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673#ifdef FEAT_RELTIME
7674 if (argvars[6].v_type != VAR_UNKNOWN)
7675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007679 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007681 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007682 }
7683#endif
7684 }
7685 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007686
7687 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7688 match_pos, lnum_stop, time_limit);
7689
7690theend:
7691 p_ws = save_p_ws;
7692
7693 return retval;
7694}
7695
7696/*
7697 * "searchpair()" function
7698 */
7699 static void
7700f_searchpair(typval_T *argvars, typval_T *rettv)
7701{
7702 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7703}
7704
7705/*
7706 * "searchpairpos()" function
7707 */
7708 static void
7709f_searchpairpos(typval_T *argvars, typval_T *rettv)
7710{
7711 pos_T match_pos;
7712 int lnum = 0;
7713 int col = 0;
7714
7715 if (rettv_list_alloc(rettv) == FAIL)
7716 return;
7717
7718 if (searchpair_cmn(argvars, &match_pos) > 0)
7719 {
7720 lnum = match_pos.lnum;
7721 col = match_pos.col;
7722 }
7723
7724 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7725 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7726}
7727
7728/*
7729 * Search for a start/middle/end thing.
7730 * Used by searchpair(), see its documentation for the details.
7731 * Returns 0 or -1 for no match,
7732 */
7733 long
7734do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007735 char_u *spat, // start pattern
7736 char_u *mpat, // middle pattern
7737 char_u *epat, // end pattern
7738 int dir, // BACKWARD or FORWARD
7739 typval_T *skip, // skip expression
7740 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007742 linenr_T lnum_stop, // stop at this line if not zero
7743 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744{
7745 char_u *save_cpo;
7746 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7747 long retval = 0;
7748 pos_T pos;
7749 pos_T firstpos;
7750 pos_T foundpos;
7751 pos_T save_cursor;
7752 pos_T save_pos;
7753 int n;
7754 int r;
7755 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007756 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 int err;
7758 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007759#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007761#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007763 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 save_cpo = p_cpo;
7765 p_cpo = empty_option;
7766
7767#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007768 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 profile_setlimit(time_limit, &tm);
7770#endif
7771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007772 // Make two search patterns: start/end (pat2, for in nested pairs) and
7773 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007774 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7775 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 if (pat2 == NULL || pat3 == NULL)
7777 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007778 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 if (*mpat == NUL)
7780 STRCPY(pat3, pat2);
7781 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007782 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783 spat, epat, mpat);
7784 if (flags & SP_START)
7785 options |= SEARCH_START;
7786
Bram Moolenaar48570482017-10-30 21:48:41 +01007787 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007788 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 save_cursor = curwin->w_cursor;
7791 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007792 CLEAR_POS(&firstpos);
7793 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794 pat = pat3;
7795 for (;;)
7796 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007797 searchit_arg_T sia;
7798
Bram Moolenaara80faa82020-04-12 19:37:17 +02007799 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007800 sia.sa_stop_lnum = lnum_stop;
7801#ifdef FEAT_RELTIME
7802 sia.sa_tm = &tm;
7803#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007804 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007805 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007806 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007807 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 break;
7809
7810 if (firstpos.lnum == 0)
7811 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007812 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007814 // Found the same position again. Can happen with a pattern that
7815 // has "\zs" at the end and searching backwards. Advance one
7816 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 if (dir == BACKWARD)
7818 decl(&pos);
7819 else
7820 incl(&pos);
7821 }
7822 foundpos = pos;
7823
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007824 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825 options &= ~SEARCH_START;
7826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007827 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007828 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829 {
7830 save_pos = curwin->w_cursor;
7831 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007832 err = FALSE;
7833 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 curwin->w_cursor = save_pos;
7835 if (err)
7836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007837 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 curwin->w_cursor = save_cursor;
7839 retval = -1;
7840 break;
7841 }
7842 if (r)
7843 continue;
7844 }
7845
7846 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007848 // Found end when searching backwards or start when searching
7849 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007850 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007851 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 }
7853 else
7854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007855 // Found end when searching forward or start when searching
7856 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007858 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007859 }
7860
7861 if (nest == 0)
7862 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007863 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 if (flags & SP_RETCOUNT)
7865 ++retval;
7866 else
7867 retval = pos.lnum;
7868 if (flags & SP_SETPCMARK)
7869 setpcmark();
7870 curwin->w_cursor = pos;
7871 if (!(flags & SP_REPEAT))
7872 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007873 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 }
7875 }
7876
7877 if (match_pos != NULL)
7878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007879 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880 match_pos->lnum = curwin->w_cursor.lnum;
7881 match_pos->col = curwin->w_cursor.col + 1;
7882 }
7883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007884 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007885 if ((flags & SP_NOMOVE) || retval == 0)
7886 curwin->w_cursor = save_cursor;
7887
7888theend:
7889 vim_free(pat2);
7890 vim_free(pat3);
7891 if (p_cpo == empty_option)
7892 p_cpo = save_cpo;
7893 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007895 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007896 // If it's still empty it was changed and restored, need to restore in
7897 // the complicated way.
7898 if (*p_cpo == NUL)
7899 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902
7903 return retval;
7904}
7905
7906/*
7907 * "searchpos()" function
7908 */
7909 static void
7910f_searchpos(typval_T *argvars, typval_T *rettv)
7911{
7912 pos_T match_pos;
7913 int lnum = 0;
7914 int col = 0;
7915 int n;
7916 int flags = 0;
7917
7918 if (rettv_list_alloc(rettv) == FAIL)
7919 return;
7920
7921 n = search_cmn(argvars, &match_pos, &flags);
7922 if (n > 0)
7923 {
7924 lnum = match_pos.lnum;
7925 col = match_pos.col;
7926 }
7927
7928 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7929 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7930 if (flags & SP_SUBPAT)
7931 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7932}
7933
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007934/*
7935 * Set the cursor or mark position.
7936 * If 'charpos' is TRUE, then use the column number as a character offet.
7937 * Otherwise use the column number as a byte offset.
7938 */
7939 static void
7940set_position(typval_T *argvars, typval_T *rettv, int charpos)
7941{
7942 pos_T pos;
7943 int fnum;
7944 char_u *name;
7945 colnr_T curswant = -1;
7946
7947 rettv->vval.v_number = -1;
7948
7949 name = tv_get_string_chk(argvars);
7950 if (name != NULL)
7951 {
7952 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7953 {
7954 if (pos.col != MAXCOL && --pos.col < 0)
7955 pos.col = 0;
7956 if ((name[0] == '.' && name[1] == NUL))
7957 {
7958 // set cursor; "fnum" is ignored
7959 curwin->w_cursor = pos;
7960 if (curswant >= 0)
7961 {
7962 curwin->w_curswant = curswant - 1;
7963 curwin->w_set_curswant = FALSE;
7964 }
7965 check_cursor();
7966 rettv->vval.v_number = 0;
7967 }
7968 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
7969 {
7970 // set mark
7971 if (setmark_pos(name[1], &pos, fnum) == OK)
7972 rettv->vval.v_number = 0;
7973 }
7974 else
7975 emsg(_(e_invarg));
7976 }
7977 }
7978}
7979/*
7980 * "setcharpos()" function
7981 */
7982 static void
7983f_setcharpos(typval_T *argvars, typval_T *rettv)
7984{
7985 set_position(argvars, rettv, TRUE);
7986}
7987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
7990{
7991 dict_T *d;
7992 dictitem_T *di;
7993 char_u *csearch;
7994
7995 if (argvars[0].v_type != VAR_DICT)
7996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007997 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 return;
7999 }
8000
8001 if ((d = argvars[0].vval.v_dict) != NULL)
8002 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008003 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 if (csearch != NULL)
8005 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 if (enc_utf8)
8007 {
8008 int pcc[MAX_MCO];
8009 int c = utfc_ptr2char(csearch, pcc);
8010
8011 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8012 }
8013 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008014 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008015 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 }
8017
8018 di = dict_find(d, (char_u *)"forward", -1);
8019 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008020 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 ? FORWARD : BACKWARD);
8022
8023 di = dict_find(d, (char_u *)"until", -1);
8024 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008025 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 }
8027}
8028
8029/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008030 * "setcursorcharpos" function
8031 */
8032 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008033f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008034{
8035 set_cursorpos(argvars, rettv, TRUE);
8036}
8037
8038/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008039 * "setenv()" function
8040 */
8041 static void
8042f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8043{
8044 char_u namebuf[NUMBUFLEN];
8045 char_u valbuf[NUMBUFLEN];
8046 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8047
8048 if (argvars[1].v_type == VAR_SPECIAL
8049 && argvars[1].vval.v_number == VVAL_NULL)
8050 vim_unsetenv(name);
8051 else
8052 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8053}
8054
8055/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 * "setfperm({fname}, {mode})" function
8057 */
8058 static void
8059f_setfperm(typval_T *argvars, typval_T *rettv)
8060{
8061 char_u *fname;
8062 char_u modebuf[NUMBUFLEN];
8063 char_u *mode_str;
8064 int i;
8065 int mask;
8066 int mode = 0;
8067
8068 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008069 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 if (fname == NULL)
8071 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008072 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 if (mode_str == NULL)
8074 return;
8075 if (STRLEN(mode_str) != 9)
8076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008077 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078 return;
8079 }
8080
8081 mask = 1;
8082 for (i = 8; i >= 0; --i)
8083 {
8084 if (mode_str[i] != '-')
8085 mode |= mask;
8086 mask = mask << 1;
8087 }
8088 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8089}
8090
8091/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 * "setpos()" function
8093 */
8094 static void
8095f_setpos(typval_T *argvars, typval_T *rettv)
8096{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008097 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098}
8099
8100/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008101 * Translate a register type string to the yank type and block length
8102 */
8103 static int
8104get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8105{
8106 char_u *stropt = *pp;
8107 switch (*stropt)
8108 {
8109 case 'v': case 'c': // character-wise selection
8110 *yank_type = MCHAR;
8111 break;
8112 case 'V': case 'l': // line-wise selection
8113 *yank_type = MLINE;
8114 break;
8115 case 'b': case Ctrl_V: // block-wise selection
8116 *yank_type = MBLOCK;
8117 if (VIM_ISDIGIT(stropt[1]))
8118 {
8119 ++stropt;
8120 *block_len = getdigits(&stropt) - 1;
8121 --stropt;
8122 }
8123 break;
8124 default:
8125 return FAIL;
8126 }
8127 *pp = stropt;
8128 return OK;
8129}
8130
8131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 * "setreg()" function
8133 */
8134 static void
8135f_setreg(typval_T *argvars, typval_T *rettv)
8136{
8137 int regname;
8138 char_u *strregname;
8139 char_u *stropt;
8140 char_u *strval;
8141 int append;
8142 char_u yank_type;
8143 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008144 typval_T *regcontents;
8145 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008146
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008147 pointreg = 0;
8148 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 block_len = -1;
8150 yank_type = MAUTO;
8151 append = FALSE;
8152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008153 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008154 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155
8156 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008157 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008158 if (in_vim9script() && STRLEN(strregname) > 1)
8159 {
8160 semsg(_(e_register_name_must_be_one_char_str), strregname);
8161 return;
8162 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 regname = *strregname;
8164 if (regname == 0 || regname == '@')
8165 regname = '"';
8166
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008167 if (argvars[1].v_type == VAR_DICT)
8168 {
8169 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008170 dictitem_T *di;
8171
8172 if (d == NULL || d->dv_hashtab.ht_used == 0)
8173 {
8174 // Empty dict, clear the register (like setreg(0, []))
8175 char_u *lstval[2] = {NULL, NULL};
8176 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8177 return;
8178 }
8179
8180 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008181 if (di != NULL)
8182 regcontents = &di->di_tv;
8183
8184 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8185 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008186 {
8187 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8188
8189 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008190 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008191 semsg(_(e_invargval), "value");
8192 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008193 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008194 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008195
8196 if (regname == '"')
8197 {
8198 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8199 if (stropt != NULL)
8200 {
8201 pointreg = *stropt;
8202 regname = pointreg;
8203 }
8204 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008205 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008206 pointreg = regname;
8207 }
8208 else
8209 regcontents = &argvars[1];
8210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 if (argvars[2].v_type != VAR_UNKNOWN)
8212 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008213 if (yank_type != MAUTO)
8214 {
8215 semsg(_(e_toomanyarg), "setreg");
8216 return;
8217 }
8218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008221 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 for (; *stropt != NUL; ++stropt)
8223 switch (*stropt)
8224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008225 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 append = TRUE;
8227 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008228 default:
8229 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 }
8231 }
8232
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008233 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 {
8235 char_u **lstval;
8236 char_u **allocval;
8237 char_u buf[NUMBUFLEN];
8238 char_u **curval;
8239 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008240 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 listitem_T *li;
8242 int len;
8243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008244 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245 len = ll == NULL ? 0 : ll->lv_len;
8246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008247 // First half: use for pointers to result lines; second half: use for
8248 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008249 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 if (lstval == NULL)
8251 return;
8252 curval = lstval;
8253 allocval = lstval + len + 2;
8254 curallocval = allocval;
8255
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008256 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008258 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008259 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008261 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 if (strval == NULL)
8263 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008264 if (strval == buf)
8265 {
8266 // Need to make a copy, next tv_get_string_buf_chk() will
8267 // overwrite the string.
8268 strval = vim_strsave(buf);
8269 if (strval == NULL)
8270 goto free_lstval;
8271 *curallocval++ = strval;
8272 }
8273 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 }
8276 *curval++ = NULL;
8277
8278 write_reg_contents_lst(regname, lstval, -1,
8279 append, yank_type, block_len);
8280free_lstval:
8281 while (curallocval > allocval)
8282 vim_free(*--curallocval);
8283 vim_free(lstval);
8284 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008285 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008287 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 if (strval == NULL)
8289 return;
8290 write_reg_contents_ex(regname, strval, -1,
8291 append, yank_type, block_len);
8292 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008293 if (pointreg != 0)
8294 get_yank_register(pointreg, TRUE);
8295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 rettv->vval.v_number = 0;
8297}
8298
8299/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008300 * "settagstack()" function
8301 */
8302 static void
8303f_settagstack(typval_T *argvars, typval_T *rettv)
8304{
8305 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8306 win_T *wp;
8307 dict_T *d;
8308 int action = 'r';
8309
8310 rettv->vval.v_number = -1;
8311
8312 // first argument: window number or id
8313 wp = find_win_by_nr_or_id(&argvars[0]);
8314 if (wp == NULL)
8315 return;
8316
8317 // second argument: dict with items to set in the tag stack
8318 if (argvars[1].v_type != VAR_DICT)
8319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008320 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008321 return;
8322 }
8323 d = argvars[1].vval.v_dict;
8324 if (d == NULL)
8325 return;
8326
8327 // third argument: action - 'a' for append and 'r' for replace.
8328 // default is to replace the stack.
8329 if (argvars[2].v_type == VAR_UNKNOWN)
8330 action = 'r';
8331 else if (argvars[2].v_type == VAR_STRING)
8332 {
8333 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008334 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008335 if (actstr == NULL)
8336 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008337 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8338 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008339 action = *actstr;
8340 else
8341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008342 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008343 return;
8344 }
8345 }
8346 else
8347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008348 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008349 return;
8350 }
8351
8352 if (set_tagstack(wp, d, action) == OK)
8353 rettv->vval.v_number = 0;
8354}
8355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356#ifdef FEAT_CRYPT
8357/*
8358 * "sha256({string})" function
8359 */
8360 static void
8361f_sha256(typval_T *argvars, typval_T *rettv)
8362{
8363 char_u *p;
8364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008365 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 rettv->vval.v_string = vim_strsave(
8367 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8368 rettv->v_type = VAR_STRING;
8369}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008370#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371
8372/*
8373 * "shellescape({string})" function
8374 */
8375 static void
8376f_shellescape(typval_T *argvars, typval_T *rettv)
8377{
Bram Moolenaar20615522017-06-05 18:46:26 +02008378 int do_special = non_zero_arg(&argvars[1]);
8379
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008381 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 rettv->v_type = VAR_STRING;
8383}
8384
8385/*
8386 * shiftwidth() function
8387 */
8388 static void
8389f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8390{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008391 rettv->vval.v_number = 0;
8392
8393 if (argvars[0].v_type != VAR_UNKNOWN)
8394 {
8395 long col;
8396
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008398 if (col < 0)
8399 return; // type error; errmsg already given
8400#ifdef FEAT_VARTABS
8401 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8402 return;
8403#endif
8404 }
8405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 rettv->vval.v_number = get_sw_value(curbuf);
8407}
8408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409/*
8410 * "soundfold({word})" function
8411 */
8412 static void
8413f_soundfold(typval_T *argvars, typval_T *rettv)
8414{
8415 char_u *s;
8416
8417 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008418 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008419#ifdef FEAT_SPELL
8420 rettv->vval.v_string = eval_soundfold(s);
8421#else
8422 rettv->vval.v_string = vim_strsave(s);
8423#endif
8424}
8425
8426/*
8427 * "spellbadword()" function
8428 */
8429 static void
8430f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8431{
8432 char_u *word = (char_u *)"";
8433 hlf_T attr = HLF_COUNT;
8434 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008435#ifdef FEAT_SPELL
8436 int wo_spell_save = curwin->w_p_spell;
8437
8438 if (!curwin->w_p_spell)
8439 {
8440 did_set_spelllang(curwin);
8441 curwin->w_p_spell = TRUE;
8442 }
8443
8444 if (*curwin->w_s->b_p_spl == NUL)
8445 {
8446 emsg(_(e_no_spell));
8447 curwin->w_p_spell = wo_spell_save;
8448 return;
8449 }
8450#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451
8452 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008453 {
8454#ifdef FEAT_SPELL
8455 curwin->w_p_spell = wo_spell_save;
8456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459
8460#ifdef FEAT_SPELL
8461 if (argvars[0].v_type == VAR_UNKNOWN)
8462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008463 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008464 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8465 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008466 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008467 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008468 curwin->w_set_curswant = TRUE;
8469 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008471 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008473 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 int capcol = -1;
8475
8476 if (str != NULL)
8477 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008478 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 while (*str != NUL)
8480 {
8481 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8482 if (attr != HLF_COUNT)
8483 {
8484 word = str;
8485 break;
8486 }
8487 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008488 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008489 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008490 }
8491 }
8492 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008493 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008494#endif
8495
8496 list_append_string(rettv->vval.v_list, word, len);
8497 list_append_string(rettv->vval.v_list, (char_u *)(
8498 attr == HLF_SPB ? "bad" :
8499 attr == HLF_SPR ? "rare" :
8500 attr == HLF_SPL ? "local" :
8501 attr == HLF_SPC ? "caps" :
8502 ""), -1);
8503}
8504
8505/*
8506 * "spellsuggest()" function
8507 */
8508 static void
8509f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8510{
8511#ifdef FEAT_SPELL
8512 char_u *str;
8513 int typeerr = FALSE;
8514 int maxcount;
8515 garray_T ga;
8516 int i;
8517 listitem_T *li;
8518 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008519 int wo_spell_save = curwin->w_p_spell;
8520
8521 if (!curwin->w_p_spell)
8522 {
8523 did_set_spelllang(curwin);
8524 curwin->w_p_spell = TRUE;
8525 }
8526
8527 if (*curwin->w_s->b_p_spl == NUL)
8528 {
8529 emsg(_(e_no_spell));
8530 curwin->w_p_spell = wo_spell_save;
8531 return;
8532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533#endif
8534
8535 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008536 {
8537#ifdef FEAT_SPELL
8538 curwin->w_p_spell = wo_spell_save;
8539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008541 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542
8543#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008544 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (argvars[1].v_type != VAR_UNKNOWN)
8548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 if (maxcount <= 0)
8551 return;
8552 if (argvars[2].v_type != VAR_UNKNOWN)
8553 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008554 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 if (typeerr)
8556 return;
8557 }
8558 }
8559 else
8560 maxcount = 25;
8561
8562 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8563
8564 for (i = 0; i < ga.ga_len; ++i)
8565 {
8566 str = ((char_u **)ga.ga_data)[i];
8567
8568 li = listitem_alloc();
8569 if (li == NULL)
8570 vim_free(str);
8571 else
8572 {
8573 li->li_tv.v_type = VAR_STRING;
8574 li->li_tv.v_lock = 0;
8575 li->li_tv.vval.v_string = str;
8576 list_append(rettv->vval.v_list, li);
8577 }
8578 }
8579 ga_clear(&ga);
8580 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008581 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582#endif
8583}
8584
8585 static void
8586f_split(typval_T *argvars, typval_T *rettv)
8587{
8588 char_u *str;
8589 char_u *end;
8590 char_u *pat = NULL;
8591 regmatch_T regmatch;
8592 char_u patbuf[NUMBUFLEN];
8593 char_u *save_cpo;
8594 int match;
8595 colnr_T col = 0;
8596 int keepempty = FALSE;
8597 int typeerr = FALSE;
8598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008599 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008601 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008603 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 if (argvars[1].v_type != VAR_UNKNOWN)
8605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008606 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 if (pat == NULL)
8608 typeerr = TRUE;
8609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008610 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 }
8612 if (pat == NULL || *pat == NUL)
8613 pat = (char_u *)"[\\x01- ]\\+";
8614
8615 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008616 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008618 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619
8620 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8621 if (regmatch.regprog != NULL)
8622 {
8623 regmatch.rm_ic = FALSE;
8624 while (*str != NUL || keepempty)
8625 {
8626 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008627 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 else
8629 match = vim_regexec_nl(&regmatch, str, col);
8630 if (match)
8631 end = regmatch.startp[0];
8632 else
8633 end = str + STRLEN(str);
8634 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8635 && *str != NUL && match && end < regmatch.endp[0]))
8636 {
8637 if (list_append_string(rettv->vval.v_list, str,
8638 (int)(end - str)) == FAIL)
8639 break;
8640 }
8641 if (!match)
8642 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008643 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (regmatch.endp[0] > str)
8645 col = 0;
8646 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008647 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 str = regmatch.endp[0];
8650 }
8651
8652 vim_regfree(regmatch.regprog);
8653 }
8654
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008655theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 p_cpo = save_cpo;
8657}
8658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008660 * "str2list()" function
8661 */
8662 static void
8663f_str2list(typval_T *argvars, typval_T *rettv)
8664{
8665 char_u *p;
8666 int utf8 = FALSE;
8667
8668 if (rettv_list_alloc(rettv) == FAIL)
8669 return;
8670
8671 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008672 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008673
8674 p = tv_get_string(&argvars[0]);
8675
8676 if (has_mbyte || utf8)
8677 {
8678 int (*ptr2len)(char_u *);
8679 int (*ptr2char)(char_u *);
8680
8681 if (utf8 || enc_utf8)
8682 {
8683 ptr2len = utf_ptr2len;
8684 ptr2char = utf_ptr2char;
8685 }
8686 else
8687 {
8688 ptr2len = mb_ptr2len;
8689 ptr2char = mb_ptr2char;
8690 }
8691
8692 for ( ; *p != NUL; p += (*ptr2len)(p))
8693 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8694 }
8695 else
8696 for ( ; *p != NUL; ++p)
8697 list_append_number(rettv->vval.v_list, *p);
8698}
8699
8700/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 * "str2nr()" function
8702 */
8703 static void
8704f_str2nr(typval_T *argvars, typval_T *rettv)
8705{
8706 int base = 10;
8707 char_u *p;
8708 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008709 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008710 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711
8712 if (argvars[1].v_type != VAR_UNKNOWN)
8713 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008714 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715 if (base != 2 && base != 8 && base != 10 && base != 16)
8716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008717 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008718 return;
8719 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008720 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008721 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 }
8723
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008724 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008725 isneg = (*p == '-');
8726 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 p = skipwhite(p + 1);
8728 switch (base)
8729 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008730 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008731 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008732 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008734 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8735 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008736 if (isneg)
8737 rettv->vval.v_number = -n;
8738 else
8739 rettv->vval.v_number = n;
8740
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741}
8742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743/*
8744 * "strgetchar()" function
8745 */
8746 static void
8747f_strgetchar(typval_T *argvars, typval_T *rettv)
8748{
8749 char_u *str;
8750 int len;
8751 int error = FALSE;
8752 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008753 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754
8755 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 if (str == NULL)
8758 return;
8759 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008760 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008761 if (error)
8762 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008763
Bram Moolenaar13505972019-01-24 15:04:48 +01008764 while (charidx >= 0 && byteidx < len)
8765 {
8766 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008768 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8769 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008771 --charidx;
8772 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774}
8775
8776/*
8777 * "stridx()" function
8778 */
8779 static void
8780f_stridx(typval_T *argvars, typval_T *rettv)
8781{
8782 char_u buf[NUMBUFLEN];
8783 char_u *needle;
8784 char_u *haystack;
8785 char_u *save_haystack;
8786 char_u *pos;
8787 int start_idx;
8788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008789 needle = tv_get_string_chk(&argvars[1]);
8790 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791 rettv->vval.v_number = -1;
8792 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008793 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794
8795 if (argvars[2].v_type != VAR_UNKNOWN)
8796 {
8797 int error = FALSE;
8798
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008799 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 if (error || start_idx >= (int)STRLEN(haystack))
8801 return;
8802 if (start_idx >= 0)
8803 haystack += start_idx;
8804 }
8805
8806 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8807 if (pos != NULL)
8808 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8809}
8810
8811/*
8812 * "string()" function
8813 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008814 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815f_string(typval_T *argvars, typval_T *rettv)
8816{
8817 char_u *tofree;
8818 char_u numbuf[NUMBUFLEN];
8819
8820 rettv->v_type = VAR_STRING;
8821 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8822 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008823 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 if (rettv->vval.v_string != NULL && tofree == NULL)
8825 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8826}
8827
8828/*
8829 * "strlen()" function
8830 */
8831 static void
8832f_strlen(typval_T *argvars, typval_T *rettv)
8833{
8834 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008835 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836}
8837
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008838 static void
8839strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8840{
8841 char_u *s = tv_get_string(&argvars[0]);
8842 varnumber_T len = 0;
8843 int (*func_mb_ptr2char_adv)(char_u **pp);
8844
8845 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8846 while (*s != NUL)
8847 {
8848 func_mb_ptr2char_adv(&s);
8849 ++len;
8850 }
8851 rettv->vval.v_number = len;
8852}
8853
8854/*
8855 * "strcharlen()" function
8856 */
8857 static void
8858f_strcharlen(typval_T *argvars, typval_T *rettv)
8859{
8860 strchar_common(argvars, rettv, TRUE);
8861}
8862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863/*
8864 * "strchars()" function
8865 */
8866 static void
8867f_strchars(typval_T *argvars, typval_T *rettv)
8868{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008869 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870
8871 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008872 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008874 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008876 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877}
8878
8879/*
8880 * "strdisplaywidth()" function
8881 */
8882 static void
8883f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008885 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 int col = 0;
8887
8888 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008889 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008890
8891 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8892}
8893
8894/*
8895 * "strwidth()" function
8896 */
8897 static void
8898f_strwidth(typval_T *argvars, typval_T *rettv)
8899{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008900 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901
Bram Moolenaar13505972019-01-24 15:04:48 +01008902 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903}
8904
8905/*
8906 * "strcharpart()" function
8907 */
8908 static void
8909f_strcharpart(typval_T *argvars, typval_T *rettv)
8910{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 char_u *p;
8912 int nchar;
8913 int nbyte = 0;
8914 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008915 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008916 int len = 0;
8917 int slen;
8918 int error = FALSE;
8919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008920 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 slen = (int)STRLEN(p);
8922
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 if (!error)
8925 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008926 if (argvars[2].v_type != VAR_UNKNOWN
8927 && argvars[3].v_type != VAR_UNKNOWN)
8928 {
8929 skipcc = tv_get_bool(&argvars[3]);
8930 if (skipcc < 0 || skipcc > 1)
8931 {
8932 semsg(_(e_using_number_as_bool_nr), skipcc);
8933 return;
8934 }
8935 }
8936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 if (nchar > 0)
8938 while (nchar > 0 && nbyte < slen)
8939 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008940 if (skipcc)
8941 nbyte += mb_ptr2len(p + nbyte);
8942 else
8943 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 --nchar;
8945 }
8946 else
8947 nbyte = nchar;
8948 if (argvars[2].v_type != VAR_UNKNOWN)
8949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008950 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 while (charlen > 0 && nbyte + len < slen)
8952 {
8953 int off = nbyte + len;
8954
8955 if (off < 0)
8956 len += 1;
8957 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008958 {
8959 if (skipcc)
8960 len += mb_ptr2len(p + off);
8961 else
8962 len += MB_CPTR2LEN(p + off);
8963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 --charlen;
8965 }
8966 }
8967 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008968 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 }
8970
8971 /*
8972 * Only return the overlap between the specified part and the actual
8973 * string.
8974 */
8975 if (nbyte < 0)
8976 {
8977 len += nbyte;
8978 nbyte = 0;
8979 }
8980 else if (nbyte > slen)
8981 nbyte = slen;
8982 if (len < 0)
8983 len = 0;
8984 else if (nbyte + len > slen)
8985 len = slen - nbyte;
8986
8987 rettv->v_type = VAR_STRING;
8988 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989}
8990
8991/*
8992 * "strpart()" function
8993 */
8994 static void
8995f_strpart(typval_T *argvars, typval_T *rettv)
8996{
8997 char_u *p;
8998 int n;
8999 int len;
9000 int slen;
9001 int error = FALSE;
9002
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009003 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 slen = (int)STRLEN(p);
9005
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009006 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 if (error)
9008 len = 0;
9009 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009010 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009012 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009014 // Only return the overlap between the specified part and the actual
9015 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 if (n < 0)
9017 {
9018 len += n;
9019 n = 0;
9020 }
9021 else if (n > slen)
9022 n = slen;
9023 if (len < 0)
9024 len = 0;
9025 else if (n + len > slen)
9026 len = slen - n;
9027
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009028 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9029 {
9030 int off;
9031
9032 // length in characters
9033 for (off = n; off < slen && len > 0; --len)
9034 off += mb_ptr2len(p + off);
9035 len = off - n;
9036 }
9037
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 rettv->v_type = VAR_STRING;
9039 rettv->vval.v_string = vim_strnsave(p + n, len);
9040}
9041
9042/*
9043 * "strridx()" function
9044 */
9045 static void
9046f_strridx(typval_T *argvars, typval_T *rettv)
9047{
9048 char_u buf[NUMBUFLEN];
9049 char_u *needle;
9050 char_u *haystack;
9051 char_u *rest;
9052 char_u *lastmatch = NULL;
9053 int haystack_len, end_idx;
9054
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009055 needle = tv_get_string_chk(&argvars[1]);
9056 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057
9058 rettv->vval.v_number = -1;
9059 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009060 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061
9062 haystack_len = (int)STRLEN(haystack);
9063 if (argvars[2].v_type != VAR_UNKNOWN)
9064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009065 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009066 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009068 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 }
9070 else
9071 end_idx = haystack_len;
9072
9073 if (*needle == NUL)
9074 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009075 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 lastmatch = haystack + end_idx;
9077 }
9078 else
9079 {
9080 for (rest = haystack; *rest != '\0'; ++rest)
9081 {
9082 rest = (char_u *)strstr((char *)rest, (char *)needle);
9083 if (rest == NULL || rest > haystack + end_idx)
9084 break;
9085 lastmatch = rest;
9086 }
9087 }
9088
9089 if (lastmatch == NULL)
9090 rettv->vval.v_number = -1;
9091 else
9092 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9093}
9094
9095/*
9096 * "strtrans()" function
9097 */
9098 static void
9099f_strtrans(typval_T *argvars, typval_T *rettv)
9100{
9101 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009102 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103}
9104
9105/*
9106 * "submatch()" function
9107 */
9108 static void
9109f_submatch(typval_T *argvars, typval_T *rettv)
9110{
9111 int error = FALSE;
9112 int no;
9113 int retList = 0;
9114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009115 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 if (error)
9117 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009118 if (no < 0 || no >= NSUBEXP)
9119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009120 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009121 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009122 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009124 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125 if (error)
9126 return;
9127
9128 if (retList == 0)
9129 {
9130 rettv->v_type = VAR_STRING;
9131 rettv->vval.v_string = reg_submatch(no);
9132 }
9133 else
9134 {
9135 rettv->v_type = VAR_LIST;
9136 rettv->vval.v_list = reg_submatch_list(no);
9137 }
9138}
9139
9140/*
9141 * "substitute()" function
9142 */
9143 static void
9144f_substitute(typval_T *argvars, typval_T *rettv)
9145{
9146 char_u patbuf[NUMBUFLEN];
9147 char_u subbuf[NUMBUFLEN];
9148 char_u flagsbuf[NUMBUFLEN];
9149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009150 char_u *str = tv_get_string_chk(&argvars[0]);
9151 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009152 char_u *sub = NULL;
9153 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009154 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009155
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009156 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9157 expr = &argvars[2];
9158 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009159 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009162 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9163 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009164 rettv->vval.v_string = NULL;
9165 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009166 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009167}
9168
9169/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009170 * "swapinfo(swap_filename)" function
9171 */
9172 static void
9173f_swapinfo(typval_T *argvars, typval_T *rettv)
9174{
9175 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009176 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009177}
9178
9179/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009180 * "swapname(expr)" function
9181 */
9182 static void
9183f_swapname(typval_T *argvars, typval_T *rettv)
9184{
9185 buf_T *buf;
9186
9187 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009188 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009189 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9190 || buf->b_ml.ml_mfp->mf_fname == NULL)
9191 rettv->vval.v_string = NULL;
9192 else
9193 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9194}
9195
9196/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 * "synID(lnum, col, trans)" function
9198 */
9199 static void
9200f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9201{
9202 int id = 0;
9203#ifdef FEAT_SYN_HL
9204 linenr_T lnum;
9205 colnr_T col;
9206 int trans;
9207 int transerr = FALSE;
9208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009209 lnum = tv_get_lnum(argvars); // -1 on type error
9210 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009211 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009212
9213 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9214 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9215 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9216#endif
9217
9218 rettv->vval.v_number = id;
9219}
9220
9221/*
9222 * "synIDattr(id, what [, mode])" function
9223 */
9224 static void
9225f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9226{
9227 char_u *p = NULL;
9228#ifdef FEAT_SYN_HL
9229 int id;
9230 char_u *what;
9231 char_u *mode;
9232 char_u modebuf[NUMBUFLEN];
9233 int modec;
9234
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009235 id = (int)tv_get_number(&argvars[0]);
9236 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237 if (argvars[2].v_type != VAR_UNKNOWN)
9238 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009239 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240 modec = TOLOWER_ASC(mode[0]);
9241 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009242 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 }
9244 else
9245 {
9246#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9247 if (USE_24BIT)
9248 modec = 'g';
9249 else
9250#endif
9251 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009252 modec = 'c';
9253 else
9254 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009255 }
9256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009257 switch (TOLOWER_ASC(what[0]))
9258 {
9259 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009260 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009262 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263 p = highlight_has_attr(id, HL_BOLD, modec);
9264 break;
9265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009266 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 p = highlight_color(id, what, modec);
9268 break;
9269
9270 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009271 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009273 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274 p = highlight_has_attr(id, HL_ITALIC, modec);
9275 break;
9276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009277 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009278 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009279 break;
9280
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009281 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 p = highlight_has_attr(id, HL_INVERSE, modec);
9283 break;
9284
9285 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009286 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009288 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009289 else if (TOLOWER_ASC(what[1]) == 't' &&
9290 TOLOWER_ASC(what[2]) == 'r')
9291 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009292 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 p = highlight_has_attr(id, HL_STANDOUT, modec);
9294 break;
9295
9296 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009297 if (TOLOWER_ASC(what[1]) == 'l') // ul
9298 p = highlight_color(id, what, modec);
9299 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009300 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9302 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009303 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9305 break;
9306 }
9307
9308 if (p != NULL)
9309 p = vim_strsave(p);
9310#endif
9311 rettv->v_type = VAR_STRING;
9312 rettv->vval.v_string = p;
9313}
9314
9315/*
9316 * "synIDtrans(id)" function
9317 */
9318 static void
9319f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9320{
9321 int id;
9322
9323#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009324 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325
9326 if (id > 0)
9327 id = syn_get_final_id(id);
9328 else
9329#endif
9330 id = 0;
9331
9332 rettv->vval.v_number = id;
9333}
9334
9335/*
9336 * "synconcealed(lnum, col)" function
9337 */
9338 static void
9339f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9340{
9341#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9342 linenr_T lnum;
9343 colnr_T col;
9344 int syntax_flags = 0;
9345 int cchar;
9346 int matchid = 0;
9347 char_u str[NUMBUFLEN];
9348#endif
9349
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009350 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009351
9352#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009353 lnum = tv_get_lnum(argvars); // -1 on type error
9354 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355
Bram Moolenaara80faa82020-04-12 19:37:17 +02009356 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009357
9358 if (rettv_list_alloc(rettv) != FAIL)
9359 {
9360 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9361 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9362 && curwin->w_p_cole > 0)
9363 {
9364 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9365 syntax_flags = get_syntax_info(&matchid);
9366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009367 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9369 {
9370 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009371 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009372 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9373 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009374 if (cchar != NUL)
9375 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009376 if (has_mbyte)
9377 (*mb_char2bytes)(cchar, str);
9378 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 str[0] = cchar;
9380 }
9381 }
9382 }
9383
9384 list_append_number(rettv->vval.v_list,
9385 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009386 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 list_append_string(rettv->vval.v_list, str, -1);
9388 list_append_number(rettv->vval.v_list, matchid);
9389 }
9390#endif
9391}
9392
9393/*
9394 * "synstack(lnum, col)" function
9395 */
9396 static void
9397f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9398{
9399#ifdef FEAT_SYN_HL
9400 linenr_T lnum;
9401 colnr_T col;
9402 int i;
9403 int id;
9404#endif
9405
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009406 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407
9408#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009409 lnum = tv_get_lnum(argvars); // -1 on type error
9410 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411
9412 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9413 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9414 && rettv_list_alloc(rettv) != FAIL)
9415 {
9416 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9417 for (i = 0; ; ++i)
9418 {
9419 id = syn_get_stack_item(i);
9420 if (id < 0)
9421 break;
9422 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9423 break;
9424 }
9425 }
9426#endif
9427}
9428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009429/*
9430 * "tabpagebuflist()" function
9431 */
9432 static void
9433f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9434{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009435 tabpage_T *tp;
9436 win_T *wp = NULL;
9437
9438 if (argvars[0].v_type == VAR_UNKNOWN)
9439 wp = firstwin;
9440 else
9441 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009442 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 if (tp != NULL)
9444 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9445 }
9446 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9447 {
9448 for (; wp != NULL; wp = wp->w_next)
9449 if (list_append_number(rettv->vval.v_list,
9450 wp->w_buffer->b_fnum) == FAIL)
9451 break;
9452 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453}
9454
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456 * "tagfiles()" function
9457 */
9458 static void
9459f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9460{
9461 char_u *fname;
9462 tagname_T tn;
9463 int first;
9464
9465 if (rettv_list_alloc(rettv) == FAIL)
9466 return;
9467 fname = alloc(MAXPATHL);
9468 if (fname == NULL)
9469 return;
9470
9471 for (first = TRUE; ; first = FALSE)
9472 if (get_tagfname(&tn, first, fname) == FAIL
9473 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9474 break;
9475 tagname_free(&tn);
9476 vim_free(fname);
9477}
9478
9479/*
9480 * "taglist()" function
9481 */
9482 static void
9483f_taglist(typval_T *argvars, typval_T *rettv)
9484{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009485 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486 char_u *tag_pattern;
9487
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009488 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489
9490 rettv->vval.v_number = FALSE;
9491 if (*tag_pattern == NUL)
9492 return;
9493
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009494 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009497 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498}
9499
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 * "tolower(string)" function
9502 */
9503 static void
9504f_tolower(typval_T *argvars, typval_T *rettv)
9505{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009507 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508}
9509
9510/*
9511 * "toupper(string)" function
9512 */
9513 static void
9514f_toupper(typval_T *argvars, typval_T *rettv)
9515{
9516 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518}
9519
9520/*
9521 * "tr(string, fromstr, tostr)" function
9522 */
9523 static void
9524f_tr(typval_T *argvars, typval_T *rettv)
9525{
9526 char_u *in_str;
9527 char_u *fromstr;
9528 char_u *tostr;
9529 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 int inlen;
9531 int fromlen;
9532 int tolen;
9533 int idx;
9534 char_u *cpstr;
9535 int cplen;
9536 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009537 char_u buf[NUMBUFLEN];
9538 char_u buf2[NUMBUFLEN];
9539 garray_T ga;
9540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009541 in_str = tv_get_string(&argvars[0]);
9542 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9543 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009545 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546 rettv->v_type = VAR_STRING;
9547 rettv->vval.v_string = NULL;
9548 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009549 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 ga_init2(&ga, (int)sizeof(char), 80);
9551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009553 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 if (STRLEN(fromstr) != STRLEN(tostr))
9555 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009557 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 ga_clear(&ga);
9559 return;
9560 }
9561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009562 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563 while (*in_str != NUL)
9564 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 if (has_mbyte)
9566 {
9567 inlen = (*mb_ptr2len)(in_str);
9568 cpstr = in_str;
9569 cplen = inlen;
9570 idx = 0;
9571 for (p = fromstr; *p != NUL; p += fromlen)
9572 {
9573 fromlen = (*mb_ptr2len)(p);
9574 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9575 {
9576 for (p = tostr; *p != NUL; p += tolen)
9577 {
9578 tolen = (*mb_ptr2len)(p);
9579 if (idx-- == 0)
9580 {
9581 cplen = tolen;
9582 cpstr = p;
9583 break;
9584 }
9585 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009586 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 goto error;
9588 break;
9589 }
9590 ++idx;
9591 }
9592
9593 if (first && cpstr == in_str)
9594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009595 // Check that fromstr and tostr have the same number of
9596 // (multi-byte) characters. Done only once when a character
9597 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 first = FALSE;
9599 for (p = tostr; *p != NUL; p += tolen)
9600 {
9601 tolen = (*mb_ptr2len)(p);
9602 --idx;
9603 }
9604 if (idx != 0)
9605 goto error;
9606 }
9607
9608 (void)ga_grow(&ga, cplen);
9609 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9610 ga.ga_len += cplen;
9611
9612 in_str += inlen;
9613 }
9614 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009615 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009616 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009617 p = vim_strchr(fromstr, *in_str);
9618 if (p != NULL)
9619 ga_append(&ga, tostr[p - fromstr]);
9620 else
9621 ga_append(&ga, *in_str);
9622 ++in_str;
9623 }
9624 }
9625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009626 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 (void)ga_grow(&ga, 1);
9628 ga_append(&ga, NUL);
9629
9630 rettv->vval.v_string = ga.ga_data;
9631}
9632
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009633/*
9634 * "trim({expr})" function
9635 */
9636 static void
9637f_trim(typval_T *argvars, typval_T *rettv)
9638{
9639 char_u buf1[NUMBUFLEN];
9640 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009641 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009642 char_u *mask = NULL;
9643 char_u *tail;
9644 char_u *prev;
9645 char_u *p;
9646 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009647 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009648
9649 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009650 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009651 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009652 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009653
9654 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009655 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009656 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009657
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009659 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009660 int error = 0;
9661
9662 // leading or trailing characters to trim
9663 dir = (int)tv_get_number_chk(&argvars[2], &error);
9664 if (error)
9665 return;
9666 if (dir < 0 || dir > 2)
9667 {
9668 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9669 return;
9670 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009671 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009672 }
9673
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009674 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009675 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009676 // Trim leading characters
9677 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009678 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009679 c1 = PTR2CHAR(head);
9680 if (mask == NULL)
9681 {
9682 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009683 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009684 }
9685 else
9686 {
9687 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9688 if (c1 == PTR2CHAR(p))
9689 break;
9690 if (*p == NUL)
9691 break;
9692 }
9693 MB_PTR_ADV(head);
9694 }
9695 }
9696
9697 tail = head + STRLEN(head);
9698 if (dir == 0 || dir == 2)
9699 {
9700 // Trim trailing characters
9701 for (; tail > head; tail = prev)
9702 {
9703 prev = tail;
9704 MB_PTR_BACK(head, prev);
9705 c1 = PTR2CHAR(prev);
9706 if (mask == NULL)
9707 {
9708 if (c1 > ' ' && c1 != 0xa0)
9709 break;
9710 }
9711 else
9712 {
9713 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9714 if (c1 == PTR2CHAR(p))
9715 break;
9716 if (*p == NUL)
9717 break;
9718 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009719 }
9720 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009721 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009722}
9723
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724/*
9725 * "type(expr)" function
9726 */
9727 static void
9728f_type(typval_T *argvars, typval_T *rettv)
9729{
9730 int n = -1;
9731
9732 switch (argvars[0].v_type)
9733 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009734 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9735 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009736 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009737 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9738 case VAR_LIST: n = VAR_TYPE_LIST; break;
9739 case VAR_DICT: n = VAR_TYPE_DICT; break;
9740 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9741 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9742 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009743 case VAR_JOB: n = VAR_TYPE_JOB; break;
9744 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009745 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009746 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009747 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009748 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009749 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009750 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 n = -1;
9752 break;
9753 }
9754 rettv->vval.v_number = n;
9755}
9756
9757/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758 * "virtcol(string)" function
9759 */
9760 static void
9761f_virtcol(typval_T *argvars, typval_T *rettv)
9762{
9763 colnr_T vcol = 0;
9764 pos_T *fp;
9765 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009766 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009768 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9770 && fnum == curbuf->b_fnum)
9771 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009772 // Limit the column to a valid value, getvvcol() doesn't check.
9773 if (fp->col < 0)
9774 fp->col = 0;
9775 else
9776 {
9777 len = (int)STRLEN(ml_get(fp->lnum));
9778 if (fp->col > len)
9779 fp->col = len;
9780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 getvvcol(curwin, fp, NULL, NULL, &vcol);
9782 ++vcol;
9783 }
9784
9785 rettv->vval.v_number = vcol;
9786}
9787
9788/*
9789 * "visualmode()" function
9790 */
9791 static void
9792f_visualmode(typval_T *argvars, typval_T *rettv)
9793{
9794 char_u str[2];
9795
9796 rettv->v_type = VAR_STRING;
9797 str[0] = curbuf->b_visual_mode_eval;
9798 str[1] = NUL;
9799 rettv->vval.v_string = vim_strsave(str);
9800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009801 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 if (non_zero_arg(&argvars[0]))
9803 curbuf->b_visual_mode_eval = NUL;
9804}
9805
9806/*
9807 * "wildmenumode()" function
9808 */
9809 static void
9810f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9811{
9812#ifdef FEAT_WILDMENU
9813 if (wild_menu_showing)
9814 rettv->vval.v_number = 1;
9815#endif
9816}
9817
9818/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009819 * "windowsversion()" function
9820 */
9821 static void
9822f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9823{
9824 rettv->v_type = VAR_STRING;
9825 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9826}
9827
9828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829 * "wordcount()" function
9830 */
9831 static void
9832f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9833{
9834 if (rettv_dict_alloc(rettv) == FAIL)
9835 return;
9836 cursor_pos_info(rettv->vval.v_dict);
9837}
9838
9839/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 * "xor(expr, expr)" function
9841 */
9842 static void
9843f_xor(typval_T *argvars, typval_T *rettv)
9844{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009845 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9846 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847}
9848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009849#endif // FEAT_EVAL