blob: 21d28afec61e2648cdd2dfa1e085046add977fc3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020023static void f_and(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010024#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020025static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010026static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010027# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010028static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010029# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010030#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020031static void f_byte2line(typval_T *argvars, typval_T *rettv);
32static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
33static void f_byteidx(typval_T *argvars, typval_T *rettv);
34static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
35static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020036static void f_changenr(typval_T *argvars, typval_T *rettv);
37static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010038static void f_charcol(typval_T *argvars, typval_T *rettv);
Bram Moolenaar17793ef2020-12-28 12:56:58 +010039static void f_charidx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020040static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020041static void f_confirm(typval_T *argvars, typval_T *rettv);
42static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010044#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020045static void f_debugbreak(typval_T *argvars, typval_T *rettv);
46#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020047static void f_deepcopy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_did_filetype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4132eb52020-02-14 16:53:00 +010049static void f_echoraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020051static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_escape(typval_T *argvars, typval_T *rettv);
53static void f_eval(typval_T *argvars, typval_T *rettv);
54static void f_eventhandler(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020057static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +020058static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_feedkeys(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020060static void f_fnameescape(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020061static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +020062static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020063static void f_function(typval_T *argvars, typval_T *rettv);
64static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
65static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +010066static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010067static void f_getcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020069static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_getfontname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +010071static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_getpid(typval_T *argvars, typval_T *rettv);
73static void f_getcurpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +010074static void f_getcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020075static void f_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020076static void f_getreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarbb861e22020-06-07 18:16:36 +020077static void f_getreginfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020078static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010079static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +020080static void f_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020081static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
82static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_hlID(typval_T *argvars, typval_T *rettv);
84static void f_hlexists(typval_T *argvars, typval_T *rettv);
85static void f_hostname(typval_T *argvars, typval_T *rettv);
86static void f_iconv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_index(typval_T *argvars, typval_T *rettv);
88static void f_input(typval_T *argvars, typval_T *rettv);
89static void f_inputdialog(typval_T *argvars, typval_T *rettv);
90static void f_inputlist(typval_T *argvars, typval_T *rettv);
91static void f_inputrestore(typval_T *argvars, typval_T *rettv);
92static void f_inputsave(typval_T *argvars, typval_T *rettv);
93static void f_inputsecret(typval_T *argvars, typval_T *rettv);
Bram Moolenaar67a2deb2019-11-25 00:05:32 +010094static void f_interrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020095static void f_invert(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020096static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020097static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
98static void f_len(typval_T *argvars, typval_T *rettv);
99static void f_libcall(typval_T *argvars, typval_T *rettv);
100static void f_libcallnr(typval_T *argvars, typval_T *rettv);
101static void f_line(typval_T *argvars, typval_T *rettv);
102static void f_line2byte(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103#ifdef FEAT_LUA
104static void f_luaeval(typval_T *argvars, typval_T *rettv);
105#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_maparg(typval_T *argvars, typval_T *rettv);
107static void f_mapcheck(typval_T *argvars, typval_T *rettv);
108static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200109static void f_matchend(typval_T *argvars, typval_T *rettv);
110static void f_matchlist(typval_T *argvars, typval_T *rettv);
111static void f_matchstr(typval_T *argvars, typval_T *rettv);
112static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
113static void f_max(typval_T *argvars, typval_T *rettv);
114static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200115#ifdef FEAT_MZSCHEME
116static void f_mzeval(typval_T *argvars, typval_T *rettv);
117#endif
118static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
119static void f_nr2char(typval_T *argvars, typval_T *rettv);
120static void f_or(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121#ifdef FEAT_PERL
122static void f_perleval(typval_T *argvars, typval_T *rettv);
123#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200124static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
125static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200126static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_pumvisible(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_PYTHON3
129static void f_py3eval(typval_T *argvars, typval_T *rettv);
130#endif
131#ifdef FEAT_PYTHON
132static void f_pyeval(typval_T *argvars, typval_T *rettv);
133#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100134#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
135static void f_pyxeval(typval_T *argvars, typval_T *rettv);
136#endif
Bram Moolenaar4f645c52020-02-08 16:40:39 +0100137static void f_test_srand_seed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100138static void f_rand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200139static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200140static void f_reg_executing(typval_T *argvars, typval_T *rettv);
141static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200142static void f_rename(typval_T *argvars, typval_T *rettv);
143static void f_repeat(typval_T *argvars, typval_T *rettv);
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100144#ifdef FEAT_RUBY
145static void f_rubyeval(typval_T *argvars, typval_T *rettv);
146#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200147static void f_screenattr(typval_T *argvars, typval_T *rettv);
148static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100149static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200150static void f_screencol(typval_T *argvars, typval_T *rettv);
151static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100152static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_search(typval_T *argvars, typval_T *rettv);
154static void f_searchdecl(typval_T *argvars, typval_T *rettv);
155static void f_searchpair(typval_T *argvars, typval_T *rettv);
156static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
157static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100158static void f_setcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200159static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100160static void f_setcursorcharpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200161static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_setpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200164static void f_setreg(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100165static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166#ifdef FEAT_CRYPT
167static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb005cd82019-09-04 15:54:55 +0200168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200169static void f_shellescape(typval_T *argvars, typval_T *rettv);
170static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200171static void f_soundfold(typval_T *argvars, typval_T *rettv);
172static void f_spellbadword(typval_T *argvars, typval_T *rettv);
173static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
174static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +0100175static void f_srand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200176static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_str2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar70ce8a12021-03-14 19:02:09 +0100178static void f_strcharlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_strgetchar(typval_T *argvars, typval_T *rettv);
181static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_strlen(typval_T *argvars, typval_T *rettv);
183static void f_strcharpart(typval_T *argvars, typval_T *rettv);
184static void f_strpart(typval_T *argvars, typval_T *rettv);
185static void f_strridx(typval_T *argvars, typval_T *rettv);
186static void f_strtrans(typval_T *argvars, typval_T *rettv);
187static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
188static void f_strwidth(typval_T *argvars, typval_T *rettv);
189static void f_submatch(typval_T *argvars, typval_T *rettv);
190static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200191static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200192static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_synID(typval_T *argvars, typval_T *rettv);
194static void f_synIDattr(typval_T *argvars, typval_T *rettv);
195static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
196static void f_synstack(typval_T *argvars, typval_T *rettv);
197static void f_synconcealed(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_taglist(typval_T *argvars, typval_T *rettv);
200static void f_tagfiles(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_tolower(typval_T *argvars, typval_T *rettv);
202static void f_toupper(typval_T *argvars, typval_T *rettv);
203static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100204static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200205static void f_type(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_virtcol(typval_T *argvars, typval_T *rettv);
207static void f_visualmode(typval_T *argvars, typval_T *rettv);
208static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0c1e3742019-12-27 13:49:24 +0100209static void f_windowsversion(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200210static void f_wordcount(typval_T *argvars, typval_T *rettv);
211static void f_xor(typval_T *argvars, typval_T *rettv);
212
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100213
Bram Moolenaar94738d82020-10-21 14:25:07 +0200214/*
215 * Functions that check the argument type of a builtin function.
216 * Each function returns FAIL and gives an error message if the type is wrong.
217 */
218
219// Context passed to an arg_ function.
220typedef struct {
Bram Moolenaarca174532020-10-21 16:42:22 +0200221 int arg_count; // actual argument count
222 type_T **arg_types; // list of argument types
223 int arg_idx; // current argument index (first arg is zero)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100224 cctx_T *arg_cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +0200225} argcontext_T;
226
227// A function to check one argument type. The first argument is the type to
228// check. If needed, other argument types can be obtained with the context.
229// E.g. if "arg_idx" is 1, then (type - 1) is the first argument type.
230typedef int (*argcheck_T)(type_T *, argcontext_T *);
231
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100232/*
Bram Moolenaar351ead02021-01-16 16:07:01 +0100233 * Call need_type() to check an argument type.
234 */
235 static int
236check_arg_type(
237 type_T *expected,
238 type_T *actual,
239 argcontext_T *context)
240{
241 // TODO: would be useful to know if "actual" is a constant and pass it to
242 // need_type() to get a compile time error if possible.
243 return need_type(actual, expected,
244 context->arg_idx - context->arg_count, context->arg_idx + 1,
245 context->arg_cctx, FALSE, FALSE);
246}
247
248/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100249 * Check "type" is a float or a number.
250 */
Bram Moolenaar94738d82020-10-21 14:25:07 +0200251 static int
252arg_float_or_nr(type_T *type, argcontext_T *context)
253{
Bram Moolenaarca174532020-10-21 16:42:22 +0200254 if (type->tt_type == VAR_ANY
255 || type->tt_type == VAR_FLOAT || type->tt_type == VAR_NUMBER)
Bram Moolenaar94738d82020-10-21 14:25:07 +0200256 return OK;
257 arg_type_mismatch(&t_number, type, context->arg_idx + 1);
258 return FAIL;
259}
260
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100261/*
262 * Check "type" is a number.
263 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200264 static int
265arg_number(type_T *type, argcontext_T *context)
266{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100267 return check_arg_type(&t_number, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200268}
269
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100270/*
271 * Check "type" is a string.
272 */
273 static int
274arg_string(type_T *type, argcontext_T *context)
275{
Bram Moolenaar351ead02021-01-16 16:07:01 +0100276 return check_arg_type(&t_string, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100277}
278
279/*
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100280 * Check "type" is a bool or number 0 or 1.
281 */
282 static int
283arg_bool(type_T *type, argcontext_T *context)
284{
285 if (type->tt_type == VAR_ANY
286 || type->tt_type == VAR_NUMBER || type->tt_type == VAR_BOOL)
287 return OK;
288 return check_arg_type(&t_bool, type, context);
289}
290
291/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100292 * Check "type" is a list or a blob.
293 */
Bram Moolenaarca174532020-10-21 16:42:22 +0200294 static int
295arg_list_or_blob(type_T *type, argcontext_T *context)
296{
297 if (type->tt_type == VAR_ANY
298 || type->tt_type == VAR_LIST || type->tt_type == VAR_BLOB)
299 return OK;
300 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
301 return FAIL;
302}
303
304/*
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200305 * Check "type" is a string or a list of strings.
306 */
307 static int
308arg_string_or_list(type_T *type, argcontext_T *context)
309{
310 if (type->tt_type == VAR_ANY || type->tt_type == VAR_STRING)
311 return OK;
312 if (type->tt_type != VAR_LIST)
313 {
314 arg_type_mismatch(&t_string, type, context->arg_idx + 1);
315 return FAIL;
316 }
317 if (type->tt_member->tt_type == VAR_ANY
318 || type->tt_member->tt_type == VAR_STRING)
319 return OK;
320
321 arg_type_mismatch(&t_list_string, type, context->arg_idx + 1);
322 return FAIL;
323}
324
325/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100326 * Check "type" is a list or a dict.
327 */
328 static int
329arg_list_or_dict(type_T *type, argcontext_T *context)
330{
331 if (type->tt_type == VAR_ANY
332 || type->tt_type == VAR_LIST || type->tt_type == VAR_DICT)
333 return OK;
334 arg_type_mismatch(&t_list_any, type, context->arg_idx + 1);
335 return FAIL;
336}
337
338/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100339 * Check "type" is the same type as the previous argument.
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100340 * Must not be used for the first argcheck_T entry.
341 */
342 static int
343arg_same_as_prev(type_T *type, argcontext_T *context)
344{
345 type_T *prev_type = context->arg_types[context->arg_idx - 1];
346
Bram Moolenaar351ead02021-01-16 16:07:01 +0100347 return check_arg_type(prev_type, type, context);
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100348}
349
350/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100351 * Check "type" is the same basic type as the previous argument, checks list or
352 * dict vs other type, but not member type.
353 * Must not be used for the first argcheck_T entry.
354 */
355 static int
356arg_same_struct_as_prev(type_T *type, argcontext_T *context)
357{
358 type_T *prev_type = context->arg_types[context->arg_idx - 1];
359
360 if (prev_type->tt_type != context->arg_types[context->arg_idx]->tt_type)
Bram Moolenaar351ead02021-01-16 16:07:01 +0100361 return check_arg_type(prev_type, type, context);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100362 return OK;
363}
364
365/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100366 * Check "type" is an item of the list or blob of the previous arg.
Bram Moolenaarca174532020-10-21 16:42:22 +0200367 * Must not be used for the first argcheck_T entry.
368 */
369 static int
370arg_item_of_prev(type_T *type, argcontext_T *context)
371{
372 type_T *prev_type = context->arg_types[context->arg_idx - 1];
373 type_T *expected;
374
375 if (prev_type->tt_type == VAR_LIST)
376 expected = prev_type->tt_member;
377 else if (prev_type->tt_type == VAR_BLOB)
378 expected = &t_number;
379 else
380 // probably VAR_ANY, can't check
381 return OK;
382
Bram Moolenaar351ead02021-01-16 16:07:01 +0100383 return check_arg_type(expected, type, context);
Bram Moolenaarca174532020-10-21 16:42:22 +0200384}
385
Bram Moolenaar94738d82020-10-21 14:25:07 +0200386/*
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100387 * Check "type" which is the third argument of extend().
388 */
389 static int
390arg_extend3(type_T *type, argcontext_T *context)
391{
392 type_T *first_type = context->arg_types[context->arg_idx - 2];
393
394 if (first_type->tt_type == VAR_LIST)
395 return arg_number(type, context);
396 if (first_type->tt_type == VAR_DICT)
397 return arg_string(type, context);
398 return OK;
399}
400
401
402/*
Bram Moolenaar94738d82020-10-21 14:25:07 +0200403 * Lists of functions that check the argument types of a builtin function.
404 */
Bram Moolenaar80ad3e22021-01-31 20:48:58 +0100405argcheck_T arg1_string[] = {arg_string};
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200406argcheck_T arg1_number[] = {arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200407argcheck_T arg1_float_or_nr[] = {arg_float_or_nr};
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200408argcheck_T arg2_float_or_nr[] = {arg_float_or_nr, arg_float_or_nr};
409argcheck_T arg2_number[] = {arg_number, arg_number};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100410argcheck_T arg2_listblob_item[] = {arg_list_or_blob, arg_item_of_prev};
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200411argcheck_T arg2_execute[] = {arg_string_or_list, arg_string};
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100412argcheck_T arg23_extend[] = {arg_list_or_dict, arg_same_as_prev, arg_extend3};
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100413argcheck_T arg23_extendnew[] = {arg_list_or_dict, arg_same_struct_as_prev, arg_extend3};
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200414argcheck_T arg3_string[] = {arg_string, arg_string, arg_string};
415argcheck_T arg3_string_nr_bool[] = {arg_string, arg_number, arg_bool};
Bram Moolenaarca174532020-10-21 16:42:22 +0200416argcheck_T arg3_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
Bram Moolenaar94738d82020-10-21 14:25:07 +0200417
418/*
419 * Functions that return the return type of a builtin function.
Bram Moolenaara1224cb2020-10-22 12:31:49 +0200420 * Note that "argtypes" is NULL if "argcount" is zero.
Bram Moolenaar94738d82020-10-21 14:25:07 +0200421 */
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100422 static type_T *
423ret_void(int argcount UNUSED, type_T **argtypes UNUSED)
424{
425 return &t_void;
426}
427 static type_T *
428ret_any(int argcount UNUSED, type_T **argtypes UNUSED)
429{
430 return &t_any;
431}
432 static type_T *
Bram Moolenaar403dc312020-10-17 19:29:51 +0200433ret_bool(int argcount UNUSED, type_T **argtypes UNUSED)
434{
435 return &t_bool;
436}
437 static type_T *
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100438ret_number_bool(int argcount UNUSED, type_T **argtypes UNUSED)
439{
440 return &t_number_bool;
441}
442 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100443ret_number(int argcount UNUSED, type_T **argtypes UNUSED)
444{
445 return &t_number;
446}
447 static type_T *
448ret_float(int argcount UNUSED, type_T **argtypes UNUSED)
449{
450 return &t_float;
451}
452 static type_T *
453ret_string(int argcount UNUSED, type_T **argtypes UNUSED)
454{
455 return &t_string;
456}
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200457 static type_T *
458ret_list_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100459{
460 return &t_list_any;
461}
462 static type_T *
463ret_list_number(int argcount UNUSED, type_T **argtypes UNUSED)
464{
465 return &t_list_number;
466}
467 static type_T *
468ret_list_string(int argcount UNUSED, type_T **argtypes UNUSED)
469{
470 return &t_list_string;
471}
472 static type_T *
473ret_list_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
474{
475 return &t_list_dict_any;
476}
477 static type_T *
478ret_dict_any(int argcount UNUSED, type_T **argtypes UNUSED)
479{
480 return &t_dict_any;
481}
482 static type_T *
Bram Moolenaar64ed4d42021-01-12 21:22:31 +0100483ret_job_info(int argcount, type_T **argtypes UNUSED)
484{
485 if (argcount == 0)
486 return &t_list_job;
487 return &t_dict_any;
488}
489 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100490ret_dict_number(int argcount UNUSED, type_T **argtypes UNUSED)
491{
492 return &t_dict_number;
493}
494 static type_T *
495ret_dict_string(int argcount UNUSED, type_T **argtypes UNUSED)
496{
497 return &t_dict_string;
498}
499 static type_T *
500ret_blob(int argcount UNUSED, type_T **argtypes UNUSED)
501{
502 return &t_blob;
503}
504 static type_T *
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200505ret_func_any(int argcount UNUSED, type_T **argtypes UNUSED)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100506{
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200507 return &t_func_any;
508}
509 static type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100510ret_channel(int argcount UNUSED, type_T **argtypes UNUSED)
511{
512 return &t_channel;
513}
514 static type_T *
515ret_job(int argcount UNUSED, type_T **argtypes UNUSED)
516{
517 return &t_job;
518}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200519 static type_T *
520ret_first_arg(int argcount, type_T **argtypes)
521{
522 if (argcount > 0)
523 return argtypes[0];
524 return &t_void;
525}
Bram Moolenaarea696852020-11-09 18:31:39 +0100526// for map(): returns first argument but item type may differ
527 static type_T *
528ret_first_cont(int argcount UNUSED, type_T **argtypes)
529{
530 if (argtypes[0]->tt_type == VAR_LIST)
531 return &t_list_any;
532 if (argtypes[0]->tt_type == VAR_DICT)
533 return &t_dict_any;
534 if (argtypes[0]->tt_type == VAR_BLOB)
535 return argtypes[0];
536 return &t_any;
537}
Bram Moolenaar865af6b2020-06-18 18:45:49 +0200538
Bram Moolenaarf151ad12020-06-30 13:38:01 +0200539/*
540 * Used for getqflist(): returns list if there is no argument, dict if there is
541 * one.
542 */
543 static type_T *
544ret_list_or_dict_0(int argcount, type_T **argtypes UNUSED)
545{
546 if (argcount > 0)
547 return &t_dict_any;
548 return &t_list_dict_any;
549}
550
551/*
552 * Used for getloclist(): returns list if there is one argument, dict if there
553 * are two.
554 */
555 static type_T *
556ret_list_or_dict_1(int argcount, type_T **argtypes UNUSED)
557{
558 if (argcount > 1)
559 return &t_dict_any;
560 return &t_list_dict_any;
561}
562
Bram Moolenaar846178a2020-07-05 17:04:13 +0200563 static type_T *
564ret_argv(int argcount, type_T **argtypes UNUSED)
565{
566 // argv() returns list of strings
567 if (argcount == 0)
568 return &t_list_string;
569
570 // argv(0) returns a string, but argv(-1] returns a list
571 return &t_any;
572}
573
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200574 static type_T *
575ret_remove(int argcount UNUSED, type_T **argtypes)
576{
Bram Moolenaar5e654232020-09-16 15:22:00 +0200577 if (argtypes != NULL)
578 {
579 if (argtypes[0]->tt_type == VAR_LIST
580 || argtypes[0]->tt_type == VAR_DICT)
581 return argtypes[0]->tt_member;
582 if (argtypes[0]->tt_type == VAR_BLOB)
583 return &t_number;
584 }
Bram Moolenaarad7c2492020-07-05 20:55:29 +0200585 return &t_any;
586}
587
Bram Moolenaar3d945cc2020-08-06 21:26:59 +0200588 static type_T *
589ret_getreg(int argcount, type_T **argtypes UNUSED)
590{
591 // Assume that if the third argument is passed it's non-zero
592 if (argcount == 3)
593 return &t_list_string;
594 return &t_string;
595}
596
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200597 static type_T *
598ret_maparg(int argcount, type_T **argtypes UNUSED)
599{
600 // Assume that if the fourth argument is passed it's non-zero
601 if (argcount == 4)
602 return &t_dict_any;
603 return &t_string;
604}
605
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100606static type_T *ret_f_function(int argcount, type_T **argtypes);
607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608/*
609 * Array with names and number of arguments of all internal functions
610 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
611 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200612typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200614 char *f_name; // function name
615 char f_min_argc; // minimal number of arguments
616 char f_max_argc; // maximal number of arguments
617 char f_argtype; // for method: FEARG_ values
Bram Moolenaar94738d82020-10-21 14:25:07 +0200618 argcheck_T *f_argcheck; // list of functions to check argument types
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +0100619 type_T *(*f_retfunc)(int argcount, type_T **argtypes);
620 // return type function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200622 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200623} funcentry_T;
624
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200625// values for f_argtype; zero means it cannot be used as a method
626#define FEARG_1 1 // base is the first argument
627#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200628#define FEARG_3 3 // base is the third argument
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200629#define FEARG_4 4 // base is the fourth argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200630#define FEARG_LAST 9 // base is the last argument
631
Bram Moolenaar15c47602020-03-26 22:16:48 +0100632#ifdef FEAT_FLOAT
633# define FLOAT_FUNC(name) name
634#else
635# define FLOAT_FUNC(name) NULL
636#endif
637#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
638# define MATH_FUNC(name) name
639#else
640# define MATH_FUNC(name) NULL
641#endif
642#ifdef FEAT_TIMERS
643# define TIMER_FUNC(name) name
644#else
645# define TIMER_FUNC(name) NULL
646#endif
647#ifdef FEAT_JOB_CHANNEL
648# define JOB_FUNC(name) name
649#else
650# define JOB_FUNC(name) NULL
651#endif
652#ifdef FEAT_PROP_POPUP
653# define PROP_FUNC(name) name
654#else
655# define PROP_FUNC(name) NULL
656#endif
657#ifdef FEAT_SIGNS
658# define SIGN_FUNC(name) name
659#else
660# define SIGN_FUNC(name) NULL
661#endif
662#ifdef FEAT_SOUND
663# define SOUND_FUNC(name) name
664#else
665# define SOUND_FUNC(name) NULL
666#endif
667#ifdef FEAT_TERMINAL
668# define TERM_FUNC(name) name
669#else
670# define TERM_FUNC(name) NULL
671#endif
672
Bram Moolenaarac92e252019-08-03 21:58:38 +0200673static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674{
Bram Moolenaar94738d82020-10-21 14:25:07 +0200675 {"abs", 1, 1, FEARG_1, arg1_float_or_nr,
676 ret_any, FLOAT_FUNC(f_abs)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200677 {"acos", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200678 ret_float, FLOAT_FUNC(f_acos)},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100679 {"add", 2, 2, FEARG_1, NULL /* arg2_listblob_item */,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200680 ret_first_arg, f_add},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200681 {"and", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200682 ret_number, f_and},
683 {"append", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100684 ret_number_bool, f_append},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200685 {"appendbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100686 ret_number_bool, f_appendbufline},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200687 {"argc", 0, 1, 0, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200688 ret_number, f_argc},
689 {"argidx", 0, 0, 0, NULL,
690 ret_number, f_argidx},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200691 {"arglistid", 0, 2, 0, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200692 ret_number, f_arglistid},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200693 {"argv", 0, 2, 0, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200694 ret_argv, f_argv},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200695 {"asin", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200696 ret_float, FLOAT_FUNC(f_asin)},
697 {"assert_beeps", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100698 ret_number_bool, f_assert_beeps},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200699 {"assert_equal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100700 ret_number_bool, f_assert_equal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200701 {"assert_equalfile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100702 ret_number_bool, f_assert_equalfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200703 {"assert_exception", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100704 ret_number_bool, f_assert_exception},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200705 {"assert_fails", 1, 5, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100706 ret_number_bool, f_assert_fails},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200707 {"assert_false", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100708 ret_number_bool, f_assert_false},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200709 {"assert_inrange", 3, 4, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100710 ret_number_bool, f_assert_inrange},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200711 {"assert_match", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100712 ret_number_bool, f_assert_match},
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200713 {"assert_nobeep", 1, 2, FEARG_1, NULL,
714 ret_number_bool, f_assert_nobeep},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200715 {"assert_notequal", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100716 ret_number_bool, f_assert_notequal},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200717 {"assert_notmatch", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100718 ret_number_bool, f_assert_notmatch},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200719 {"assert_report", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100720 ret_number_bool, f_assert_report},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200721 {"assert_true", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100722 ret_number_bool, f_assert_true},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200723 {"atan", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200724 ret_float, FLOAT_FUNC(f_atan)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200725 {"atan2", 2, 2, FEARG_1, arg2_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200726 ret_float, FLOAT_FUNC(f_atan2)},
727 {"balloon_gettext", 0, 0, 0, NULL,
728 ret_string,
Bram Moolenaar59716a22017-03-01 20:32:44 +0100729#ifdef FEAT_BEVAL
Bram Moolenaar15c47602020-03-26 22:16:48 +0100730 f_balloon_gettext
731#else
732 NULL
Bram Moolenaar59716a22017-03-01 20:32:44 +0100733#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100734 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200735 {"balloon_show", 1, 1, FEARG_1, NULL,
736 ret_void,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100737#ifdef FEAT_BEVAL
738 f_balloon_show
739#else
740 NULL
741#endif
742 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200743 {"balloon_split", 1, 1, FEARG_1, NULL,
744 ret_list_string,
Bram Moolenaar15c47602020-03-26 22:16:48 +0100745#if defined(FEAT_BEVAL_TERM)
746 f_balloon_split
747#else
748 NULL
749#endif
750 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200751 {"browse", 4, 4, 0, NULL,
752 ret_string, f_browse},
753 {"browsedir", 2, 2, 0, NULL,
754 ret_string, f_browsedir},
755 {"bufadd", 1, 1, FEARG_1, NULL,
756 ret_number, f_bufadd},
757 {"bufexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100758 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200759 {"buffer_exists", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100760 ret_number_bool, f_bufexists},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200761 {"buffer_name", 0, 1, FEARG_1, NULL, // obsolete
762 ret_string, f_bufname},
763 {"buffer_number", 0, 1, FEARG_1, NULL, // obsolete
764 ret_number, f_bufnr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200765 {"buflisted", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100766 ret_number_bool, f_buflisted},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200767 {"bufload", 1, 1, FEARG_1, NULL,
768 ret_void, f_bufload},
769 {"bufloaded", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100770 ret_number_bool, f_bufloaded},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200771 {"bufname", 0, 1, FEARG_1, NULL,
772 ret_string, f_bufname},
773 {"bufnr", 0, 2, FEARG_1, NULL,
774 ret_number, f_bufnr},
775 {"bufwinid", 1, 1, FEARG_1, NULL,
776 ret_number, f_bufwinid},
777 {"bufwinnr", 1, 1, FEARG_1, NULL,
778 ret_number, f_bufwinnr},
779 {"byte2line", 1, 1, FEARG_1, NULL,
780 ret_number, f_byte2line},
781 {"byteidx", 2, 2, FEARG_1, NULL,
782 ret_number, f_byteidx},
783 {"byteidxcomp", 2, 2, FEARG_1, NULL,
784 ret_number, f_byteidxcomp},
785 {"call", 2, 3, FEARG_1, NULL,
786 ret_any, f_call},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200787 {"ceil", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200788 ret_float, FLOAT_FUNC(f_ceil)},
789 {"ch_canread", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100790 ret_number_bool, JOB_FUNC(f_ch_canread)},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200791 {"ch_close", 1, 1, FEARG_1, NULL,
792 ret_void, JOB_FUNC(f_ch_close)},
793 {"ch_close_in", 1, 1, FEARG_1, NULL,
794 ret_void, JOB_FUNC(f_ch_close_in)},
795 {"ch_evalexpr", 2, 3, FEARG_1, NULL,
796 ret_any, JOB_FUNC(f_ch_evalexpr)},
797 {"ch_evalraw", 2, 3, FEARG_1, NULL,
798 ret_any, JOB_FUNC(f_ch_evalraw)},
799 {"ch_getbufnr", 2, 2, FEARG_1, NULL,
800 ret_number, JOB_FUNC(f_ch_getbufnr)},
801 {"ch_getjob", 1, 1, FEARG_1, NULL,
802 ret_job, JOB_FUNC(f_ch_getjob)},
803 {"ch_info", 1, 1, FEARG_1, NULL,
804 ret_dict_any, JOB_FUNC(f_ch_info)},
805 {"ch_log", 1, 2, FEARG_1, NULL,
806 ret_void, JOB_FUNC(f_ch_log)},
807 {"ch_logfile", 1, 2, FEARG_1, NULL,
808 ret_void, JOB_FUNC(f_ch_logfile)},
809 {"ch_open", 1, 2, FEARG_1, NULL,
810 ret_channel, JOB_FUNC(f_ch_open)},
811 {"ch_read", 1, 2, FEARG_1, NULL,
812 ret_string, JOB_FUNC(f_ch_read)},
813 {"ch_readblob", 1, 2, FEARG_1, NULL,
814 ret_blob, JOB_FUNC(f_ch_readblob)},
815 {"ch_readraw", 1, 2, FEARG_1, NULL,
816 ret_string, JOB_FUNC(f_ch_readraw)},
817 {"ch_sendexpr", 2, 3, FEARG_1, NULL,
818 ret_void, JOB_FUNC(f_ch_sendexpr)},
819 {"ch_sendraw", 2, 3, FEARG_1, NULL,
820 ret_void, JOB_FUNC(f_ch_sendraw)},
821 {"ch_setoptions", 2, 2, FEARG_1, NULL,
822 ret_void, JOB_FUNC(f_ch_setoptions)},
823 {"ch_status", 1, 2, FEARG_1, NULL,
824 ret_string, JOB_FUNC(f_ch_status)},
825 {"changenr", 0, 0, 0, NULL,
826 ret_number, f_changenr},
827 {"char2nr", 1, 2, FEARG_1, NULL,
828 ret_number, f_char2nr},
829 {"charclass", 1, 1, FEARG_1, NULL,
830 ret_number, f_charclass},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100831 {"charcol", 1, 1, FEARG_1, NULL,
832 ret_number, f_charcol},
Bram Moolenaar17793ef2020-12-28 12:56:58 +0100833 {"charidx", 2, 3, FEARG_1, NULL,
834 ret_number, f_charidx},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200835 {"chdir", 1, 1, FEARG_1, NULL,
836 ret_string, f_chdir},
837 {"cindent", 1, 1, FEARG_1, NULL,
838 ret_number, f_cindent},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200839 {"clearmatches", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200840 ret_void, f_clearmatches},
841 {"col", 1, 1, FEARG_1, NULL,
842 ret_number, f_col},
843 {"complete", 2, 2, FEARG_2, NULL,
844 ret_void, f_complete},
845 {"complete_add", 1, 1, FEARG_1, NULL,
846 ret_number, f_complete_add},
847 {"complete_check", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100848 ret_number_bool, f_complete_check},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200849 {"complete_info", 0, 1, FEARG_1, NULL,
850 ret_dict_any, f_complete_info},
851 {"confirm", 1, 4, FEARG_1, NULL,
852 ret_number, f_confirm},
853 {"copy", 1, 1, FEARG_1, NULL,
854 ret_first_arg, f_copy},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200855 {"cos", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200856 ret_float, FLOAT_FUNC(f_cos)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200857 {"cosh", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200858 ret_float, FLOAT_FUNC(f_cosh)},
859 {"count", 2, 4, FEARG_1, NULL,
860 ret_number, f_count},
861 {"cscope_connection",0,3, 0, NULL,
862 ret_number, f_cscope_connection},
863 {"cursor", 1, 3, FEARG_1, NULL,
864 ret_number, f_cursor},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200865 {"debugbreak", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200866 ret_number,
Bram Moolenaar4f974752019-02-17 17:44:42 +0100867#ifdef MSWIN
Bram Moolenaar15c47602020-03-26 22:16:48 +0100868 f_debugbreak
869#else
870 NULL
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200871#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +0100872 },
Bram Moolenaar94738d82020-10-21 14:25:07 +0200873 {"deepcopy", 1, 2, FEARG_1, NULL,
874 ret_first_arg, f_deepcopy},
875 {"delete", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100876 ret_number_bool, f_delete},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200877 {"deletebufline", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100878 ret_number_bool, f_deletebufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200879 {"did_filetype", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100880 ret_number_bool, f_did_filetype},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200881 {"diff_filler", 1, 1, FEARG_1, NULL,
882 ret_number, f_diff_filler},
883 {"diff_hlID", 2, 2, FEARG_1, NULL,
884 ret_number, f_diff_hlID},
885 {"echoraw", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100886 ret_void, f_echoraw},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200887 {"empty", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100888 ret_number_bool, f_empty},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200889 {"environ", 0, 0, 0, NULL,
890 ret_dict_string, f_environ},
891 {"escape", 2, 2, FEARG_1, NULL,
892 ret_string, f_escape},
893 {"eval", 1, 1, FEARG_1, NULL,
894 ret_any, f_eval},
895 {"eventhandler", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100896 ret_number_bool, f_eventhandler},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200897 {"executable", 1, 1, FEARG_1, NULL,
898 ret_number, f_executable},
Bram Moolenaarca81f0e2021-06-20 14:41:01 +0200899 {"execute", 1, 2, FEARG_1, arg2_execute,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200900 ret_string, f_execute},
901 {"exepath", 1, 1, FEARG_1, NULL,
902 ret_string, f_exepath},
903 {"exists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100904 ret_number_bool, f_exists},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200905 {"exp", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200906 ret_float, FLOAT_FUNC(f_exp)},
907 {"expand", 1, 3, FEARG_1, NULL,
908 ret_any, f_expand},
909 {"expandcmd", 1, 1, FEARG_1, NULL,
910 ret_string, f_expandcmd},
Bram Moolenaarfbcbffe2020-10-31 19:33:38 +0100911 {"extend", 2, 3, FEARG_1, arg23_extend,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200912 ret_first_arg, f_extend},
Bram Moolenaarb0e6b512021-01-12 20:23:40 +0100913 {"extendnew", 2, 3, FEARG_1, arg23_extendnew,
914 ret_first_cont, f_extendnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200915 {"feedkeys", 1, 2, FEARG_1, NULL,
916 ret_void, f_feedkeys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +0200917 {"file_readable", 1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100918 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200919 {"filereadable", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +0100920 ret_number_bool, f_filereadable},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200921 {"filewritable", 1, 1, FEARG_1, NULL,
922 ret_number, f_filewritable},
923 {"filter", 2, 2, FEARG_1, NULL,
924 ret_first_arg, f_filter},
925 {"finddir", 1, 3, FEARG_1, NULL,
926 ret_string, f_finddir},
927 {"findfile", 1, 3, FEARG_1, NULL,
928 ret_string, f_findfile},
929 {"flatten", 1, 2, FEARG_1, NULL,
930 ret_list_any, f_flatten},
Bram Moolenaar3b690062021-02-01 20:14:51 +0100931 {"flattennew", 1, 2, FEARG_1, NULL,
932 ret_list_any, f_flattennew},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200933 {"float2nr", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200934 ret_number, FLOAT_FUNC(f_float2nr)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200935 {"floor", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200936 ret_float, FLOAT_FUNC(f_floor)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200937 {"fmod", 2, 2, FEARG_1, arg2_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200938 ret_float, FLOAT_FUNC(f_fmod)},
939 {"fnameescape", 1, 1, FEARG_1, NULL,
940 ret_string, f_fnameescape},
941 {"fnamemodify", 2, 2, FEARG_1, NULL,
942 ret_string, f_fnamemodify},
943 {"foldclosed", 1, 1, FEARG_1, NULL,
944 ret_number, f_foldclosed},
945 {"foldclosedend", 1, 1, FEARG_1, NULL,
946 ret_number, f_foldclosedend},
947 {"foldlevel", 1, 1, FEARG_1, NULL,
948 ret_number, f_foldlevel},
949 {"foldtext", 0, 0, 0, NULL,
950 ret_string, f_foldtext},
951 {"foldtextresult", 1, 1, FEARG_1, NULL,
952 ret_string, f_foldtextresult},
953 {"foreground", 0, 0, 0, NULL,
954 ret_void, f_foreground},
Bram Moolenaar038e09e2021-02-06 12:38:51 +0100955 {"fullcommand", 1, 1, FEARG_1, arg1_string,
956 ret_string, f_fullcommand},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200957 {"funcref", 1, 3, FEARG_1, NULL,
958 ret_func_any, f_funcref},
959 {"function", 1, 3, FEARG_1, NULL,
960 ret_f_function, f_function},
961 {"garbagecollect", 0, 1, 0, NULL,
962 ret_void, f_garbagecollect},
963 {"get", 2, 3, FEARG_1, NULL,
964 ret_any, f_get},
965 {"getbufinfo", 0, 1, FEARG_1, NULL,
966 ret_list_dict_any, f_getbufinfo},
967 {"getbufline", 2, 3, FEARG_1, NULL,
968 ret_list_string, f_getbufline},
969 {"getbufvar", 2, 3, FEARG_1, NULL,
970 ret_any, f_getbufvar},
971 {"getchangelist", 0, 1, FEARG_1, NULL,
972 ret_list_any, f_getchangelist},
973 {"getchar", 0, 1, 0, NULL,
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200974 ret_any, f_getchar},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200975 {"getcharmod", 0, 0, 0, NULL,
976 ret_number, f_getcharmod},
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100977 {"getcharpos", 1, 1, FEARG_1, NULL,
978 ret_list_number, f_getcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200979 {"getcharsearch", 0, 0, 0, NULL,
980 ret_dict_any, f_getcharsearch},
Bram Moolenaar3a7503c2021-06-07 18:29:17 +0200981 {"getcharstr", 0, 1, 0, NULL,
982 ret_string, f_getcharstr},
Bram Moolenaar94738d82020-10-21 14:25:07 +0200983 {"getcmdline", 0, 0, 0, NULL,
984 ret_string, f_getcmdline},
985 {"getcmdpos", 0, 0, 0, NULL,
986 ret_number, f_getcmdpos},
987 {"getcmdtype", 0, 0, 0, NULL,
988 ret_string, f_getcmdtype},
989 {"getcmdwintype", 0, 0, 0, NULL,
990 ret_string, f_getcmdwintype},
991 {"getcompletion", 2, 3, FEARG_1, NULL,
992 ret_list_string, f_getcompletion},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200993 {"getcurpos", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200994 ret_list_number, f_getcurpos},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200995 {"getcursorcharpos", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100996 ret_list_number, f_getcursorcharpos},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200997 {"getcwd", 0, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +0200998 ret_string, f_getcwd},
999 {"getenv", 1, 1, FEARG_1, NULL,
Bram Moolenaar7ad67d12021-03-10 16:08:26 +01001000 ret_any, f_getenv},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001001 {"getfontname", 0, 1, 0, NULL,
1002 ret_string, f_getfontname},
1003 {"getfperm", 1, 1, FEARG_1, NULL,
1004 ret_string, f_getfperm},
1005 {"getfsize", 1, 1, FEARG_1, NULL,
1006 ret_number, f_getfsize},
1007 {"getftime", 1, 1, FEARG_1, NULL,
1008 ret_number, f_getftime},
1009 {"getftype", 1, 1, FEARG_1, NULL,
1010 ret_string, f_getftype},
1011 {"getimstatus", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001012 ret_number_bool, f_getimstatus},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001013 {"getjumplist", 0, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001014 ret_list_any, f_getjumplist},
1015 {"getline", 1, 2, FEARG_1, NULL,
1016 ret_f_getline, f_getline},
1017 {"getloclist", 1, 2, 0, NULL,
1018 ret_list_or_dict_1, f_getloclist},
1019 {"getmarklist", 0, 1, FEARG_1, NULL,
1020 ret_list_dict_any, f_getmarklist},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001021 {"getmatches", 0, 1, 0, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001022 ret_list_dict_any, f_getmatches},
1023 {"getmousepos", 0, 0, 0, NULL,
1024 ret_dict_number, f_getmousepos},
1025 {"getpid", 0, 0, 0, NULL,
1026 ret_number, f_getpid},
1027 {"getpos", 1, 1, FEARG_1, NULL,
1028 ret_list_number, f_getpos},
1029 {"getqflist", 0, 1, 0, NULL,
1030 ret_list_or_dict_0, f_getqflist},
1031 {"getreg", 0, 3, FEARG_1, NULL,
1032 ret_getreg, f_getreg},
1033 {"getreginfo", 0, 1, FEARG_1, NULL,
1034 ret_dict_any, f_getreginfo},
1035 {"getregtype", 0, 1, FEARG_1, NULL,
1036 ret_string, f_getregtype},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001037 {"gettabinfo", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001038 ret_list_dict_any, f_gettabinfo},
1039 {"gettabvar", 2, 3, FEARG_1, NULL,
1040 ret_any, f_gettabvar},
1041 {"gettabwinvar", 3, 4, FEARG_1, NULL,
1042 ret_any, f_gettabwinvar},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001043 {"gettagstack", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001044 ret_dict_any, f_gettagstack},
1045 {"gettext", 1, 1, FEARG_1, NULL,
1046 ret_string, f_gettext},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001047 {"getwininfo", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001048 ret_list_dict_any, f_getwininfo},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001049 {"getwinpos", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001050 ret_list_number, f_getwinpos},
1051 {"getwinposx", 0, 0, 0, NULL,
1052 ret_number, f_getwinposx},
1053 {"getwinposy", 0, 0, 0, NULL,
1054 ret_number, f_getwinposy},
1055 {"getwinvar", 2, 3, FEARG_1, NULL,
1056 ret_any, f_getwinvar},
1057 {"glob", 1, 4, FEARG_1, NULL,
1058 ret_any, f_glob},
1059 {"glob2regpat", 1, 1, FEARG_1, NULL,
1060 ret_string, f_glob2regpat},
1061 {"globpath", 2, 5, FEARG_2, NULL,
1062 ret_any, f_globpath},
1063 {"has", 1, 2, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001064 ret_number_bool, f_has},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001065 {"has_key", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001066 ret_number_bool, f_has_key},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001067 {"haslocaldir", 0, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001068 ret_number, f_haslocaldir},
1069 {"hasmapto", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001070 ret_number_bool, f_hasmapto},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001071 {"highlightID", 1, 1, FEARG_1, NULL, // obsolete
1072 ret_number, f_hlID},
1073 {"highlight_exists",1, 1, FEARG_1, NULL, // obsolete
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001074 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001075 {"histadd", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001076 ret_number_bool, f_histadd},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001077 {"histdel", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001078 ret_number_bool, f_histdel},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001079 {"histget", 1, 2, FEARG_1, NULL,
1080 ret_string, f_histget},
1081 {"histnr", 1, 1, FEARG_1, NULL,
1082 ret_number, f_histnr},
1083 {"hlID", 1, 1, FEARG_1, NULL,
1084 ret_number, f_hlID},
1085 {"hlexists", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001086 ret_number_bool, f_hlexists},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001087 {"hostname", 0, 0, 0, NULL,
1088 ret_string, f_hostname},
1089 {"iconv", 3, 3, FEARG_1, NULL,
1090 ret_string, f_iconv},
1091 {"indent", 1, 1, FEARG_1, NULL,
1092 ret_number, f_indent},
1093 {"index", 2, 4, FEARG_1, NULL,
1094 ret_number, f_index},
1095 {"input", 1, 3, FEARG_1, NULL,
1096 ret_string, f_input},
1097 {"inputdialog", 1, 3, FEARG_1, NULL,
1098 ret_string, f_inputdialog},
1099 {"inputlist", 1, 1, FEARG_1, NULL,
1100 ret_number, f_inputlist},
1101 {"inputrestore", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001102 ret_number_bool, f_inputrestore},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001103 {"inputsave", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001104 ret_number_bool, f_inputsave},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001105 {"inputsecret", 1, 2, FEARG_1, NULL,
1106 ret_string, f_inputsecret},
Bram Moolenaarca174532020-10-21 16:42:22 +02001107 {"insert", 2, 3, FEARG_1, arg3_insert,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001108 ret_first_arg, f_insert},
1109 {"interrupt", 0, 0, 0, NULL,
1110 ret_void, f_interrupt},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001111 {"invert", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001112 ret_number, f_invert},
1113 {"isdirectory", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001114 ret_number_bool, f_isdirectory},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001115 {"isinf", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001116 ret_number, MATH_FUNC(f_isinf)},
1117 {"islocked", 1, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001118 ret_number_bool, f_islocked},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001119 {"isnan", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001120 ret_number_bool, MATH_FUNC(f_isnan)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001121 {"items", 1, 1, FEARG_1, NULL,
1122 ret_list_any, f_items},
1123 {"job_getchannel", 1, 1, FEARG_1, NULL,
1124 ret_channel, JOB_FUNC(f_job_getchannel)},
1125 {"job_info", 0, 1, FEARG_1, NULL,
Bram Moolenaar64ed4d42021-01-12 21:22:31 +01001126 ret_job_info, JOB_FUNC(f_job_info)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001127 {"job_setoptions", 2, 2, FEARG_1, NULL,
1128 ret_void, JOB_FUNC(f_job_setoptions)},
1129 {"job_start", 1, 2, FEARG_1, NULL,
1130 ret_job, JOB_FUNC(f_job_start)},
1131 {"job_status", 1, 1, FEARG_1, NULL,
1132 ret_string, JOB_FUNC(f_job_status)},
1133 {"job_stop", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001134 ret_number_bool, JOB_FUNC(f_job_stop)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001135 {"join", 1, 2, FEARG_1, NULL,
1136 ret_string, f_join},
1137 {"js_decode", 1, 1, FEARG_1, NULL,
1138 ret_any, f_js_decode},
1139 {"js_encode", 1, 1, FEARG_1, NULL,
1140 ret_string, f_js_encode},
1141 {"json_decode", 1, 1, FEARG_1, NULL,
1142 ret_any, f_json_decode},
1143 {"json_encode", 1, 1, FEARG_1, NULL,
1144 ret_string, f_json_encode},
1145 {"keys", 1, 1, FEARG_1, NULL,
1146 ret_list_string, f_keys},
Bram Moolenaarb8f519e2020-10-21 14:49:08 +02001147 {"last_buffer_nr", 0, 0, 0, NULL, // obsolete
1148 ret_number, f_last_buffer_nr},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001149 {"len", 1, 1, FEARG_1, NULL,
1150 ret_number, f_len},
1151 {"libcall", 3, 3, FEARG_3, NULL,
1152 ret_string, f_libcall},
1153 {"libcallnr", 3, 3, FEARG_3, NULL,
1154 ret_number, f_libcallnr},
1155 {"line", 1, 2, FEARG_1, NULL,
1156 ret_number, f_line},
1157 {"line2byte", 1, 1, FEARG_1, NULL,
1158 ret_number, f_line2byte},
1159 {"lispindent", 1, 1, FEARG_1, NULL,
1160 ret_number, f_lispindent},
1161 {"list2str", 1, 2, FEARG_1, NULL,
1162 ret_string, f_list2str},
1163 {"listener_add", 1, 2, FEARG_2, NULL,
1164 ret_number, f_listener_add},
1165 {"listener_flush", 0, 1, FEARG_1, NULL,
1166 ret_void, f_listener_flush},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001167 {"listener_remove", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001168 ret_number_bool, f_listener_remove},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001169 {"localtime", 0, 0, 0, NULL,
1170 ret_number, f_localtime},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001171 {"log", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001172 ret_float, FLOAT_FUNC(f_log)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001173 {"log10", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001174 ret_float, FLOAT_FUNC(f_log10)},
1175 {"luaeval", 1, 2, FEARG_1, NULL,
1176 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177#ifdef FEAT_LUA
Bram Moolenaar15c47602020-03-26 22:16:48 +01001178 f_luaeval
1179#else
1180 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001181#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001182 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001183 {"map", 2, 2, FEARG_1, NULL,
Bram Moolenaarea696852020-11-09 18:31:39 +01001184 ret_first_cont, f_map},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001185 {"maparg", 1, 4, FEARG_1, NULL,
1186 ret_maparg, f_maparg},
1187 {"mapcheck", 1, 3, FEARG_1, NULL,
1188 ret_string, f_mapcheck},
Bram Moolenaarea696852020-11-09 18:31:39 +01001189 {"mapnew", 2, 2, FEARG_1, NULL,
1190 ret_first_cont, f_mapnew},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001191 {"mapset", 3, 3, FEARG_1, NULL,
1192 ret_void, f_mapset},
1193 {"match", 2, 4, FEARG_1, NULL,
1194 ret_any, f_match},
1195 {"matchadd", 2, 5, FEARG_1, NULL,
1196 ret_number, f_matchadd},
1197 {"matchaddpos", 2, 5, FEARG_1, NULL,
1198 ret_number, f_matchaddpos},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001199 {"matcharg", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001200 ret_list_string, f_matcharg},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001201 {"matchdelete", 1, 2, FEARG_1, arg2_number,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001202 ret_number_bool, f_matchdelete},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001203 {"matchend", 2, 4, FEARG_1, NULL,
1204 ret_number, f_matchend},
1205 {"matchfuzzy", 2, 3, FEARG_1, NULL,
1206 ret_list_string, f_matchfuzzy},
1207 {"matchfuzzypos", 2, 3, FEARG_1, NULL,
1208 ret_list_any, f_matchfuzzypos},
1209 {"matchlist", 2, 4, FEARG_1, NULL,
1210 ret_list_string, f_matchlist},
1211 {"matchstr", 2, 4, FEARG_1, NULL,
1212 ret_string, f_matchstr},
1213 {"matchstrpos", 2, 4, FEARG_1, NULL,
1214 ret_list_any, f_matchstrpos},
1215 {"max", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001216 ret_number, f_max},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001217 {"menu_info", 1, 2, FEARG_1, NULL,
1218 ret_dict_any,
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001219#ifdef FEAT_MENU
Bram Moolenaar15c47602020-03-26 22:16:48 +01001220 f_menu_info
1221#else
1222 NULL
Bram Moolenaara2cbdea2020-03-16 21:08:31 +01001223#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001224 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001225 {"min", 1, 1, FEARG_1, NULL,
Bram Moolenaar9ae37052021-01-22 22:31:10 +01001226 ret_number, f_min},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001227 {"mkdir", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001228 ret_number_bool, f_mkdir},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001229 {"mode", 0, 1, FEARG_1, NULL,
1230 ret_string, f_mode},
1231 {"mzeval", 1, 1, FEARG_1, NULL,
1232 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001233#ifdef FEAT_MZSCHEME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001234 f_mzeval
1235#else
1236 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001237#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001238 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001239 {"nextnonblank", 1, 1, FEARG_1, NULL,
1240 ret_number, f_nextnonblank},
1241 {"nr2char", 1, 2, FEARG_1, NULL,
1242 ret_string, f_nr2char},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001243 {"or", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001244 ret_number, f_or},
1245 {"pathshorten", 1, 2, FEARG_1, NULL,
1246 ret_string, f_pathshorten},
1247 {"perleval", 1, 1, FEARG_1, NULL,
1248 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249#ifdef FEAT_PERL
Bram Moolenaar15c47602020-03-26 22:16:48 +01001250 f_perleval
1251#else
1252 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001253#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001254 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001255 {"popup_atcursor", 2, 2, FEARG_1, NULL,
1256 ret_number, PROP_FUNC(f_popup_atcursor)},
1257 {"popup_beval", 2, 2, FEARG_1, NULL,
1258 ret_number, PROP_FUNC(f_popup_beval)},
1259 {"popup_clear", 0, 1, 0, NULL,
1260 ret_void, PROP_FUNC(f_popup_clear)},
1261 {"popup_close", 1, 2, FEARG_1, NULL,
1262 ret_void, PROP_FUNC(f_popup_close)},
1263 {"popup_create", 2, 2, FEARG_1, NULL,
1264 ret_number, PROP_FUNC(f_popup_create)},
1265 {"popup_dialog", 2, 2, FEARG_1, NULL,
1266 ret_number, PROP_FUNC(f_popup_dialog)},
1267 {"popup_filter_menu", 2, 2, 0, NULL,
1268 ret_bool, PROP_FUNC(f_popup_filter_menu)},
1269 {"popup_filter_yesno", 2, 2, 0, NULL,
1270 ret_bool, PROP_FUNC(f_popup_filter_yesno)},
1271 {"popup_findinfo", 0, 0, 0, NULL,
1272 ret_number, PROP_FUNC(f_popup_findinfo)},
1273 {"popup_findpreview", 0, 0, 0, NULL,
1274 ret_number, PROP_FUNC(f_popup_findpreview)},
1275 {"popup_getoptions", 1, 1, FEARG_1, NULL,
1276 ret_dict_any, PROP_FUNC(f_popup_getoptions)},
1277 {"popup_getpos", 1, 1, FEARG_1, NULL,
1278 ret_dict_any, PROP_FUNC(f_popup_getpos)},
1279 {"popup_hide", 1, 1, FEARG_1, NULL,
1280 ret_void, PROP_FUNC(f_popup_hide)},
1281 {"popup_list", 0, 0, 0, NULL,
1282 ret_list_number, PROP_FUNC(f_popup_list)},
1283 {"popup_locate", 2, 2, 0, NULL,
1284 ret_number, PROP_FUNC(f_popup_locate)},
1285 {"popup_menu", 2, 2, FEARG_1, NULL,
1286 ret_number, PROP_FUNC(f_popup_menu)},
1287 {"popup_move", 2, 2, FEARG_1, NULL,
1288 ret_void, PROP_FUNC(f_popup_move)},
1289 {"popup_notification", 2, 2, FEARG_1, NULL,
1290 ret_number, PROP_FUNC(f_popup_notification)},
1291 {"popup_setoptions", 2, 2, FEARG_1, NULL,
1292 ret_void, PROP_FUNC(f_popup_setoptions)},
1293 {"popup_settext", 2, 2, FEARG_1, NULL,
1294 ret_void, PROP_FUNC(f_popup_settext)},
1295 {"popup_show", 1, 1, FEARG_1, NULL,
1296 ret_void, PROP_FUNC(f_popup_show)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001297 {"pow", 2, 2, FEARG_1, arg2_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001298 ret_float, FLOAT_FUNC(f_pow)},
1299 {"prevnonblank", 1, 1, FEARG_1, NULL,
1300 ret_number, f_prevnonblank},
1301 {"printf", 1, 19, FEARG_2, NULL,
1302 ret_string, f_printf},
1303 {"prompt_getprompt", 1, 1, FEARG_1, NULL,
1304 ret_string, JOB_FUNC(f_prompt_getprompt)},
1305 {"prompt_setcallback", 2, 2, FEARG_1, NULL,
1306 ret_void, JOB_FUNC(f_prompt_setcallback)},
1307 {"prompt_setinterrupt", 2, 2, FEARG_1, NULL,
1308 ret_void, JOB_FUNC(f_prompt_setinterrupt)},
1309 {"prompt_setprompt", 2, 2, FEARG_1, NULL,
1310 ret_void, JOB_FUNC(f_prompt_setprompt)},
1311 {"prop_add", 3, 3, FEARG_1, NULL,
1312 ret_void, PROP_FUNC(f_prop_add)},
1313 {"prop_clear", 1, 3, FEARG_1, NULL,
1314 ret_void, PROP_FUNC(f_prop_clear)},
1315 {"prop_find", 1, 2, FEARG_1, NULL,
1316 ret_dict_any, PROP_FUNC(f_prop_find)},
1317 {"prop_list", 1, 2, FEARG_1, NULL,
1318 ret_list_dict_any, PROP_FUNC(f_prop_list)},
1319 {"prop_remove", 1, 3, FEARG_1, NULL,
1320 ret_number, PROP_FUNC(f_prop_remove)},
1321 {"prop_type_add", 2, 2, FEARG_1, NULL,
1322 ret_void, PROP_FUNC(f_prop_type_add)},
1323 {"prop_type_change", 2, 2, FEARG_1, NULL,
1324 ret_void, PROP_FUNC(f_prop_type_change)},
1325 {"prop_type_delete", 1, 2, FEARG_1, NULL,
1326 ret_void, PROP_FUNC(f_prop_type_delete)},
1327 {"prop_type_get", 1, 2, FEARG_1, NULL,
1328 ret_dict_any, PROP_FUNC(f_prop_type_get)},
1329 {"prop_type_list", 0, 1, FEARG_1, NULL,
1330 ret_list_string, PROP_FUNC(f_prop_type_list)},
1331 {"pum_getpos", 0, 0, 0, NULL,
1332 ret_dict_number, f_pum_getpos},
1333 {"pumvisible", 0, 0, 0, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001334 ret_number_bool, f_pumvisible},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001335 {"py3eval", 1, 1, FEARG_1, NULL,
1336 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001337#ifdef FEAT_PYTHON3
Bram Moolenaar15c47602020-03-26 22:16:48 +01001338 f_py3eval
1339#else
1340 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001342 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001343 {"pyeval", 1, 1, FEARG_1, NULL,
1344 ret_any,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345#ifdef FEAT_PYTHON
Bram Moolenaar15c47602020-03-26 22:16:48 +01001346 f_pyeval
1347#else
1348 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001349#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001350 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001351 {"pyxeval", 1, 1, FEARG_1, NULL,
1352 ret_any,
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001353#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar15c47602020-03-26 22:16:48 +01001354 f_pyxeval
1355#else
1356 NULL
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001357#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001358 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001359 {"rand", 0, 1, FEARG_1, NULL,
1360 ret_number, f_rand},
1361 {"range", 1, 3, FEARG_1, NULL,
1362 ret_list_number, f_range},
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001363 {"readblob", 1, 1, FEARG_1, NULL,
1364 ret_blob, f_readblob},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001365 {"readdir", 1, 3, FEARG_1, NULL,
1366 ret_list_string, f_readdir},
1367 {"readdirex", 1, 3, FEARG_1, NULL,
1368 ret_list_dict_any, f_readdirex},
1369 {"readfile", 1, 3, FEARG_1, NULL,
Bram Moolenaarc423ad72021-01-13 20:38:03 +01001370 ret_list_string, f_readfile},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001371 {"reduce", 2, 3, FEARG_1, NULL,
1372 ret_any, f_reduce},
1373 {"reg_executing", 0, 0, 0, NULL,
1374 ret_string, f_reg_executing},
1375 {"reg_recording", 0, 0, 0, NULL,
1376 ret_string, f_reg_recording},
1377 {"reltime", 0, 2, FEARG_1, NULL,
1378 ret_list_any, f_reltime},
1379 {"reltimefloat", 1, 1, FEARG_1, NULL,
1380 ret_float, FLOAT_FUNC(f_reltimefloat)},
1381 {"reltimestr", 1, 1, FEARG_1, NULL,
1382 ret_string, f_reltimestr},
1383 {"remote_expr", 2, 4, FEARG_1, NULL,
1384 ret_string, f_remote_expr},
1385 {"remote_foreground", 1, 1, FEARG_1, NULL,
1386 ret_string, f_remote_foreground},
1387 {"remote_peek", 1, 2, FEARG_1, NULL,
1388 ret_number, f_remote_peek},
1389 {"remote_read", 1, 2, FEARG_1, NULL,
1390 ret_string, f_remote_read},
1391 {"remote_send", 2, 3, FEARG_1, NULL,
1392 ret_string, f_remote_send},
1393 {"remote_startserver", 1, 1, FEARG_1, NULL,
1394 ret_void, f_remote_startserver},
1395 {"remove", 2, 3, FEARG_1, NULL,
1396 ret_remove, f_remove},
1397 {"rename", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001398 ret_number_bool, f_rename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001399 {"repeat", 2, 2, FEARG_1, NULL,
1400 ret_first_arg, f_repeat},
1401 {"resolve", 1, 1, FEARG_1, NULL,
1402 ret_string, f_resolve},
1403 {"reverse", 1, 1, FEARG_1, NULL,
1404 ret_first_arg, f_reverse},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001405 {"round", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001406 ret_float, FLOAT_FUNC(f_round)},
1407 {"rubyeval", 1, 1, FEARG_1, NULL,
1408 ret_any,
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001409#ifdef FEAT_RUBY
Bram Moolenaar15c47602020-03-26 22:16:48 +01001410 f_rubyeval
1411#else
1412 NULL
Bram Moolenaare99be0e2019-03-26 22:51:09 +01001413#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001414 },
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001415 {"screenattr", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001416 ret_number, f_screenattr},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001417 {"screenchar", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001418 ret_number, f_screenchar},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001419 {"screenchars", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001420 ret_list_number, f_screenchars},
1421 {"screencol", 0, 0, 0, NULL,
1422 ret_number, f_screencol},
1423 {"screenpos", 3, 3, FEARG_1, NULL,
1424 ret_dict_number, f_screenpos},
1425 {"screenrow", 0, 0, 0, NULL,
1426 ret_number, f_screenrow},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001427 {"screenstring", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001428 ret_string, f_screenstring},
1429 {"search", 1, 5, FEARG_1, NULL,
1430 ret_number, f_search},
1431 {"searchcount", 0, 1, FEARG_1, NULL,
1432 ret_dict_any, f_searchcount},
1433 {"searchdecl", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001434 ret_number_bool, f_searchdecl},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001435 {"searchpair", 3, 7, 0, NULL,
1436 ret_number, f_searchpair},
1437 {"searchpairpos", 3, 7, 0, NULL,
1438 ret_list_number, f_searchpairpos},
1439 {"searchpos", 1, 5, FEARG_1, NULL,
1440 ret_list_number, f_searchpos},
1441 {"server2client", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001442 ret_number_bool, f_server2client},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001443 {"serverlist", 0, 0, 0, NULL,
1444 ret_string, f_serverlist},
1445 {"setbufline", 3, 3, FEARG_3, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001446 ret_number_bool, f_setbufline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001447 {"setbufvar", 3, 3, FEARG_3, NULL,
1448 ret_void, f_setbufvar},
1449 {"setcellwidths", 1, 1, FEARG_1, NULL,
1450 ret_void, f_setcellwidths},
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001451 {"setcharpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001452 ret_number_bool, f_setcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001453 {"setcharsearch", 1, 1, FEARG_1, NULL,
1454 ret_void, f_setcharsearch},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001455 {"setcmdpos", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001456 ret_number_bool, f_setcmdpos},
1457 {"setcursorcharpos", 1, 3, FEARG_1, NULL,
1458 ret_number_bool, f_setcursorcharpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001459 {"setenv", 2, 2, FEARG_2, NULL,
1460 ret_void, f_setenv},
1461 {"setfperm", 2, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001462 ret_number_bool, f_setfperm},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001463 {"setline", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001464 ret_number_bool, f_setline},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001465 {"setloclist", 2, 4, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001466 ret_number_bool, f_setloclist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001467 {"setmatches", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001468 ret_number_bool, f_setmatches},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001469 {"setpos", 2, 2, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001470 ret_number_bool, f_setpos},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001471 {"setqflist", 1, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001472 ret_number_bool, f_setqflist},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001473 {"setreg", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001474 ret_number_bool, f_setreg},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001475 {"settabvar", 3, 3, FEARG_3, NULL,
1476 ret_void, f_settabvar},
1477 {"settabwinvar", 4, 4, FEARG_4, NULL,
1478 ret_void, f_settabwinvar},
1479 {"settagstack", 2, 3, FEARG_2, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001480 ret_number_bool, f_settagstack},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001481 {"setwinvar", 3, 3, FEARG_3, NULL,
1482 ret_void, f_setwinvar},
1483 {"sha256", 1, 1, FEARG_1, NULL,
1484 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001485#ifdef FEAT_CRYPT
Bram Moolenaar15c47602020-03-26 22:16:48 +01001486 f_sha256
1487#else
1488 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001490 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001491 {"shellescape", 1, 2, FEARG_1, NULL,
1492 ret_string, f_shellescape},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001493 {"shiftwidth", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001494 ret_number, f_shiftwidth},
1495 {"sign_define", 1, 2, FEARG_1, NULL,
1496 ret_any, SIGN_FUNC(f_sign_define)},
1497 {"sign_getdefined", 0, 1, FEARG_1, NULL,
1498 ret_list_dict_any, SIGN_FUNC(f_sign_getdefined)},
1499 {"sign_getplaced", 0, 2, FEARG_1, NULL,
1500 ret_list_dict_any, SIGN_FUNC(f_sign_getplaced)},
1501 {"sign_jump", 3, 3, FEARG_1, NULL,
1502 ret_number, SIGN_FUNC(f_sign_jump)},
1503 {"sign_place", 4, 5, FEARG_1, NULL,
1504 ret_number, SIGN_FUNC(f_sign_place)},
1505 {"sign_placelist", 1, 1, FEARG_1, NULL,
1506 ret_list_number, SIGN_FUNC(f_sign_placelist)},
1507 {"sign_undefine", 0, 1, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001508 ret_number_bool, SIGN_FUNC(f_sign_undefine)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001509 {"sign_unplace", 1, 2, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001510 ret_number_bool, SIGN_FUNC(f_sign_unplace)},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001511 {"sign_unplacelist", 1, 2, FEARG_1, NULL,
1512 ret_list_number, SIGN_FUNC(f_sign_unplacelist)},
1513 {"simplify", 1, 1, FEARG_1, NULL,
1514 ret_string, f_simplify},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001515 {"sin", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001516 ret_float, FLOAT_FUNC(f_sin)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001517 {"sinh", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001518 ret_float, FLOAT_FUNC(f_sinh)},
Bram Moolenaar6601b622021-01-13 21:47:15 +01001519 {"slice", 2, 3, FEARG_1, NULL,
1520 ret_first_arg, f_slice},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001521 {"sort", 1, 3, FEARG_1, NULL,
1522 ret_first_arg, f_sort},
1523 {"sound_clear", 0, 0, 0, NULL,
1524 ret_void, SOUND_FUNC(f_sound_clear)},
1525 {"sound_playevent", 1, 2, FEARG_1, NULL,
1526 ret_number, SOUND_FUNC(f_sound_playevent)},
1527 {"sound_playfile", 1, 2, FEARG_1, NULL,
1528 ret_number, SOUND_FUNC(f_sound_playfile)},
1529 {"sound_stop", 1, 1, FEARG_1, NULL,
1530 ret_void, SOUND_FUNC(f_sound_stop)},
1531 {"soundfold", 1, 1, FEARG_1, NULL,
1532 ret_string, f_soundfold},
1533 {"spellbadword", 0, 1, FEARG_1, NULL,
1534 ret_list_string, f_spellbadword},
1535 {"spellsuggest", 1, 3, FEARG_1, NULL,
1536 ret_list_string, f_spellsuggest},
1537 {"split", 1, 3, FEARG_1, NULL,
1538 ret_list_string, f_split},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001539 {"sqrt", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001540 ret_float, FLOAT_FUNC(f_sqrt)},
1541 {"srand", 0, 1, FEARG_1, NULL,
1542 ret_list_number, f_srand},
1543 {"state", 0, 1, FEARG_1, NULL,
1544 ret_string, f_state},
Bram Moolenaar80ad3e22021-01-31 20:48:58 +01001545 {"str2float", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001546 ret_float, FLOAT_FUNC(f_str2float)},
1547 {"str2list", 1, 2, FEARG_1, NULL,
1548 ret_list_number, f_str2list},
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001549 {"str2nr", 1, 3, FEARG_1, arg3_string_nr_bool,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001550 ret_number, f_str2nr},
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01001551 {"strcharlen", 1, 1, FEARG_1, NULL,
1552 ret_number, f_strcharlen},
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01001553 {"strcharpart", 2, 4, FEARG_1, NULL,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001554 ret_string, f_strcharpart},
1555 {"strchars", 1, 2, FEARG_1, NULL,
1556 ret_number, f_strchars},
1557 {"strdisplaywidth", 1, 2, FEARG_1, NULL,
1558 ret_number, f_strdisplaywidth},
1559 {"strftime", 1, 2, FEARG_1, NULL,
1560 ret_string,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561#ifdef HAVE_STRFTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001562 f_strftime
1563#else
1564 NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001566 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001567 {"strgetchar", 2, 2, FEARG_1, NULL,
1568 ret_number, f_strgetchar},
1569 {"stridx", 2, 3, FEARG_1, NULL,
1570 ret_number, f_stridx},
1571 {"string", 1, 1, FEARG_1, NULL,
1572 ret_string, f_string},
1573 {"strlen", 1, 1, FEARG_1, NULL,
1574 ret_number, f_strlen},
1575 {"strpart", 2, 4, FEARG_1, NULL,
1576 ret_string, f_strpart},
1577 {"strptime", 2, 2, FEARG_1, NULL,
1578 ret_number,
Bram Moolenaar10455d42019-11-21 15:36:18 +01001579#ifdef HAVE_STRPTIME
Bram Moolenaar15c47602020-03-26 22:16:48 +01001580 f_strptime
1581#else
1582 NULL
Bram Moolenaar10455d42019-11-21 15:36:18 +01001583#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001584 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001585 {"strridx", 2, 3, FEARG_1, NULL,
1586 ret_number, f_strridx},
1587 {"strtrans", 1, 1, FEARG_1, NULL,
1588 ret_string, f_strtrans},
1589 {"strwidth", 1, 1, FEARG_1, NULL,
1590 ret_number, f_strwidth},
1591 {"submatch", 1, 2, FEARG_1, NULL,
1592 ret_string, f_submatch},
1593 {"substitute", 4, 4, FEARG_1, NULL,
1594 ret_string, f_substitute},
1595 {"swapinfo", 1, 1, FEARG_1, NULL,
1596 ret_dict_any, f_swapinfo},
1597 {"swapname", 1, 1, FEARG_1, NULL,
1598 ret_string, f_swapname},
1599 {"synID", 3, 3, 0, NULL,
1600 ret_number, f_synID},
1601 {"synIDattr", 2, 3, FEARG_1, NULL,
1602 ret_string, f_synIDattr},
1603 {"synIDtrans", 1, 1, FEARG_1, NULL,
1604 ret_number, f_synIDtrans},
1605 {"synconcealed", 2, 2, 0, NULL,
1606 ret_list_any, f_synconcealed},
1607 {"synstack", 2, 2, 0, NULL,
1608 ret_list_number, f_synstack},
1609 {"system", 1, 2, FEARG_1, NULL,
1610 ret_string, f_system},
1611 {"systemlist", 1, 2, FEARG_1, NULL,
1612 ret_list_string, f_systemlist},
1613 {"tabpagebuflist", 0, 1, FEARG_1, NULL,
1614 ret_list_number, f_tabpagebuflist},
1615 {"tabpagenr", 0, 1, 0, NULL,
1616 ret_number, f_tabpagenr},
1617 {"tabpagewinnr", 1, 2, FEARG_1, NULL,
1618 ret_number, f_tabpagewinnr},
1619 {"tagfiles", 0, 0, 0, NULL,
1620 ret_list_string, f_tagfiles},
1621 {"taglist", 1, 2, FEARG_1, NULL,
1622 ret_list_dict_any, f_taglist},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001623 {"tan", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001624 ret_float, FLOAT_FUNC(f_tan)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001625 {"tanh", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001626 ret_float, FLOAT_FUNC(f_tanh)},
1627 {"tempname", 0, 0, 0, NULL,
1628 ret_string, f_tempname},
1629 {"term_dumpdiff", 2, 3, FEARG_1, NULL,
1630 ret_number, TERM_FUNC(f_term_dumpdiff)},
1631 {"term_dumpload", 1, 2, FEARG_1, NULL,
1632 ret_number, TERM_FUNC(f_term_dumpload)},
1633 {"term_dumpwrite", 2, 3, FEARG_2, NULL,
1634 ret_void, TERM_FUNC(f_term_dumpwrite)},
1635 {"term_getaltscreen", 1, 1, FEARG_1, NULL,
1636 ret_number, TERM_FUNC(f_term_getaltscreen)},
1637 {"term_getansicolors", 1, 1, FEARG_1, NULL,
1638 ret_list_string,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001639#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001640 f_term_getansicolors
1641#else
1642 NULL
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001643#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001644 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001645 {"term_getattr", 2, 2, FEARG_1, NULL,
1646 ret_number, TERM_FUNC(f_term_getattr)},
1647 {"term_getcursor", 1, 1, FEARG_1, NULL,
1648 ret_list_any, TERM_FUNC(f_term_getcursor)},
1649 {"term_getjob", 1, 1, FEARG_1, NULL,
1650 ret_job, TERM_FUNC(f_term_getjob)},
1651 {"term_getline", 2, 2, FEARG_1, NULL,
1652 ret_string, TERM_FUNC(f_term_getline)},
1653 {"term_getscrolled", 1, 1, FEARG_1, NULL,
1654 ret_number, TERM_FUNC(f_term_getscrolled)},
1655 {"term_getsize", 1, 1, FEARG_1, NULL,
1656 ret_list_number, TERM_FUNC(f_term_getsize)},
1657 {"term_getstatus", 1, 1, FEARG_1, NULL,
1658 ret_string, TERM_FUNC(f_term_getstatus)},
1659 {"term_gettitle", 1, 1, FEARG_1, NULL,
1660 ret_string, TERM_FUNC(f_term_gettitle)},
1661 {"term_gettty", 1, 2, FEARG_1, NULL,
1662 ret_string, TERM_FUNC(f_term_gettty)},
1663 {"term_list", 0, 0, 0, NULL,
1664 ret_list_number, TERM_FUNC(f_term_list)},
1665 {"term_scrape", 2, 2, FEARG_1, NULL,
1666 ret_list_dict_any, TERM_FUNC(f_term_scrape)},
1667 {"term_sendkeys", 2, 2, FEARG_1, NULL,
1668 ret_void, TERM_FUNC(f_term_sendkeys)},
1669 {"term_setansicolors", 2, 2, FEARG_1, NULL,
1670 ret_void,
Bram Moolenaarbd5e6222020-03-26 23:13:34 +01001671#if defined(FEAT_TERMINAL) && (defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS))
Bram Moolenaar15c47602020-03-26 22:16:48 +01001672 f_term_setansicolors
1673#else
1674 NULL
1675#endif
1676 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001677 {"term_setapi", 2, 2, FEARG_1, NULL,
1678 ret_void, TERM_FUNC(f_term_setapi)},
1679 {"term_setkill", 2, 2, FEARG_1, NULL,
1680 ret_void, TERM_FUNC(f_term_setkill)},
1681 {"term_setrestore", 2, 2, FEARG_1, NULL,
1682 ret_void, TERM_FUNC(f_term_setrestore)},
1683 {"term_setsize", 3, 3, FEARG_1, NULL,
1684 ret_void, TERM_FUNC(f_term_setsize)},
1685 {"term_start", 1, 2, FEARG_1, NULL,
1686 ret_number, TERM_FUNC(f_term_start)},
1687 {"term_wait", 1, 2, FEARG_1, NULL,
1688 ret_void, TERM_FUNC(f_term_wait)},
1689 {"terminalprops", 0, 0, 0, NULL,
1690 ret_dict_string, f_terminalprops},
1691 {"test_alloc_fail", 3, 3, FEARG_1, NULL,
1692 ret_void, f_test_alloc_fail},
1693 {"test_autochdir", 0, 0, 0, NULL,
1694 ret_void, f_test_autochdir},
1695 {"test_feedinput", 1, 1, FEARG_1, NULL,
1696 ret_void, f_test_feedinput},
1697 {"test_garbagecollect_now", 0, 0, 0, NULL,
1698 ret_void, f_test_garbagecollect_now},
1699 {"test_garbagecollect_soon", 0, 0, 0, NULL,
1700 ret_void, f_test_garbagecollect_soon},
1701 {"test_getvalue", 1, 1, FEARG_1, NULL,
1702 ret_number, f_test_getvalue},
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001703 {"test_gui_mouse_event", 5, 5, 0, NULL,
1704 ret_void, f_test_gui_mouse_event},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001705 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1706 ret_void, f_test_ignore_error},
1707 {"test_null_blob", 0, 0, 0, NULL,
1708 ret_blob, f_test_null_blob},
1709 {"test_null_channel", 0, 0, 0, NULL,
1710 ret_channel, JOB_FUNC(f_test_null_channel)},
1711 {"test_null_dict", 0, 0, 0, NULL,
1712 ret_dict_any, f_test_null_dict},
1713 {"test_null_function", 0, 0, 0, NULL,
1714 ret_func_any, f_test_null_function},
1715 {"test_null_job", 0, 0, 0, NULL,
1716 ret_job, JOB_FUNC(f_test_null_job)},
1717 {"test_null_list", 0, 0, 0, NULL,
1718 ret_list_any, f_test_null_list},
1719 {"test_null_partial", 0, 0, 0, NULL,
1720 ret_func_any, f_test_null_partial},
1721 {"test_null_string", 0, 0, 0, NULL,
1722 ret_string, f_test_null_string},
1723 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1724 ret_void, f_test_option_not_set},
1725 {"test_override", 2, 2, FEARG_2, NULL,
1726 ret_void, f_test_override},
1727 {"test_refcount", 1, 1, FEARG_1, NULL,
1728 ret_number, f_test_refcount},
1729 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1730 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001731#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001732 f_test_scrollbar
1733#else
1734 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001735#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001736 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001737 {"test_setmouse", 2, 2, 0, NULL,
1738 ret_void, f_test_setmouse},
1739 {"test_settime", 1, 1, FEARG_1, NULL,
1740 ret_void, f_test_settime},
1741 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1742 ret_void, f_test_srand_seed},
1743 {"test_unknown", 0, 0, 0, NULL,
1744 ret_any, f_test_unknown},
1745 {"test_void", 0, 0, 0, NULL,
1746 ret_void, f_test_void},
1747 {"timer_info", 0, 1, FEARG_1, NULL,
1748 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1749 {"timer_pause", 2, 2, FEARG_1, NULL,
1750 ret_void, TIMER_FUNC(f_timer_pause)},
1751 {"timer_start", 2, 3, FEARG_1, NULL,
1752 ret_number, TIMER_FUNC(f_timer_start)},
1753 {"timer_stop", 1, 1, FEARG_1, NULL,
1754 ret_void, TIMER_FUNC(f_timer_stop)},
1755 {"timer_stopall", 0, 0, 0, NULL,
1756 ret_void, TIMER_FUNC(f_timer_stopall)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001757 {"tolower", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001758 ret_string, f_tolower},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001759 {"toupper", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001760 ret_string, f_toupper},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001761 {"tr", 3, 3, FEARG_1, arg3_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001762 ret_string, f_tr},
1763 {"trim", 1, 3, FEARG_1, NULL,
1764 ret_string, f_trim},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001765 {"trunc", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001766 ret_float, FLOAT_FUNC(f_trunc)},
1767 {"type", 1, 1, FEARG_1, NULL,
1768 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001769 {"typename", 1, 1, FEARG_1, NULL,
1770 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001771 {"undofile", 1, 1, FEARG_1, NULL,
1772 ret_string, f_undofile},
1773 {"undotree", 0, 0, 0, NULL,
1774 ret_dict_any, f_undotree},
1775 {"uniq", 1, 3, FEARG_1, NULL,
1776 ret_list_any, f_uniq},
1777 {"values", 1, 1, FEARG_1, NULL,
1778 ret_list_any, f_values},
1779 {"virtcol", 1, 1, FEARG_1, NULL,
1780 ret_number, f_virtcol},
1781 {"visualmode", 0, 1, 0, NULL,
1782 ret_string, f_visualmode},
1783 {"wildmenumode", 0, 0, 0, NULL,
1784 ret_number, f_wildmenumode},
1785 {"win_execute", 2, 3, FEARG_2, NULL,
1786 ret_string, f_win_execute},
1787 {"win_findbuf", 1, 1, FEARG_1, NULL,
1788 ret_list_number, f_win_findbuf},
1789 {"win_getid", 0, 2, FEARG_1, NULL,
1790 ret_number, f_win_getid},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001791 {"win_gettype", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001792 ret_string, f_win_gettype},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001793 {"win_gotoid", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001794 ret_number_bool, f_win_gotoid},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001795 {"win_id2tabwin", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001796 ret_list_number, f_win_id2tabwin},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001797 {"win_id2win", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001798 ret_number, f_win_id2win},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001799 {"win_screenpos", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001800 ret_list_number, f_win_screenpos},
1801 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001802 ret_number_bool, f_win_splitmove},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001803 {"winbufnr", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001804 ret_number, f_winbufnr},
1805 {"wincol", 0, 0, 0, NULL,
1806 ret_number, f_wincol},
1807 {"windowsversion", 0, 0, 0, NULL,
1808 ret_string, f_windowsversion},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001809 {"winheight", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001810 ret_number, f_winheight},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001811 {"winlayout", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001812 ret_list_any, f_winlayout},
1813 {"winline", 0, 0, 0, NULL,
1814 ret_number, f_winline},
1815 {"winnr", 0, 1, FEARG_1, NULL,
1816 ret_number, f_winnr},
1817 {"winrestcmd", 0, 0, 0, NULL,
1818 ret_string, f_winrestcmd},
1819 {"winrestview", 1, 1, FEARG_1, NULL,
1820 ret_void, f_winrestview},
1821 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001822 ret_dict_number, f_winsaveview},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001823 {"winwidth", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001824 ret_number, f_winwidth},
1825 {"wordcount", 0, 0, 0, NULL,
1826 ret_dict_number, f_wordcount},
1827 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001828 ret_number_bool, f_writefile},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001829 {"xor", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001830 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001831};
1832
K.Takataeecf2b32021-06-02 14:56:39 +02001833#if defined(EBCDIC) || defined(PROTO)
1834/*
1835 * Compare funcentry_T by function name.
1836 */
1837 static int
1838compare_func_name(const void *s1, const void *s2)
1839{
1840 funcentry_T *p1 = (funcentry_T *)s1;
1841 funcentry_T *p2 = (funcentry_T *)s2;
1842
1843 return STRCMP(p1->f_name, p2->f_name);
1844}
1845
1846/*
1847 * Sort the function table by function name.
1848 * The sorting of the table above is ASCII dependent.
1849 * On machines using EBCDIC we have to sort it.
1850 */
1851 void
1852sortFunctions(void)
1853{
1854 size_t funcCnt = ARRAY_LENGTH(global_functions);
1855
1856 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1857}
1858#endif
1859
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001860/*
1861 * Function given to ExpandGeneric() to obtain the list of internal
1862 * or user defined function names.
1863 */
1864 char_u *
1865get_function_name(expand_T *xp, int idx)
1866{
1867 static int intidx = -1;
1868 char_u *name;
1869
1870 if (idx == 0)
1871 intidx = -1;
1872 if (intidx < 0)
1873 {
1874 name = get_user_func_name(xp, idx);
1875 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001876 {
1877 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1878 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001880 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 }
K.Takataeeec2542021-06-02 13:28:16 +02001882 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001884 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001886 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887 STRCAT(IObuff, ")");
1888 return IObuff;
1889 }
1890
1891 return NULL;
1892}
1893
1894/*
1895 * Function given to ExpandGeneric() to obtain the list of internal or
1896 * user defined variable or function names.
1897 */
1898 char_u *
1899get_expr_name(expand_T *xp, int idx)
1900{
1901 static int intidx = -1;
1902 char_u *name;
1903
1904 if (idx == 0)
1905 intidx = -1;
1906 if (intidx < 0)
1907 {
1908 name = get_function_name(xp, idx);
1909 if (name != NULL)
1910 return name;
1911 }
1912 return get_user_var_name(xp, ++intidx);
1913}
1914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001916 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001917 * Return index, or -1 if not found or "implemented" is TRUE and the function
1918 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001919 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001920 static int
1921find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922{
1923 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001924 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 int cmp;
1926 int x;
1927
K.Takataeeec2542021-06-02 13:28:16 +02001928 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001929
1930 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 while (first <= last)
1932 {
1933 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001934 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 if (cmp < 0)
1936 last = x - 1;
1937 else if (cmp > 0)
1938 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001939 else if (implemented && global_functions[x].f_func == NULL)
1940 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941 else
1942 return x;
1943 }
1944 return -1;
1945}
1946
Bram Moolenaar15c47602020-03-26 22:16:48 +01001947/*
1948 * Find internal function "name" in table "global_functions".
1949 * Return index, or -1 if not found or the function is not implemented.
1950 */
1951 int
1952find_internal_func(char_u *name)
1953{
1954 return find_internal_func_opt(name, TRUE);
1955}
1956
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001957 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001958has_internal_func(char_u *name)
1959{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001960 return find_internal_func_opt(name, TRUE) >= 0;
1961}
1962
1963 static int
1964has_internal_func_name(char_u *name)
1965{
1966 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001967}
1968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 char *
1970internal_func_name(int idx)
1971{
1972 return global_functions[idx].f_name;
1973}
1974
Bram Moolenaar94738d82020-10-21 14:25:07 +02001975/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001976 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977 * Uses the list of types on the type stack: "types".
1978 * Return FAIL and gives an error message when a type is wrong.
1979 */
1980 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001981internal_func_check_arg_types(
1982 type_T **types,
1983 int idx,
1984 int argcount,
1985 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001986{
1987 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1988 int i;
1989
1990 if (argchecks != NULL)
1991 {
1992 argcontext_T context;
1993
1994 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001995 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001996 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001997 for (i = 0; i < argcount; ++i)
1998 if (argchecks[i] != NULL)
1999 {
2000 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02002001 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002002 return FAIL;
2003 }
2004 }
2005 return OK;
2006}
2007
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002008/*
2009 * Call the "f_retfunc" function to obtain the return type of function "idx".
2010 * "argtypes" is the list of argument types or NULL when there are no
2011 * arguments.
2012 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002014internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002016 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017}
2018
2019/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002020 * Return TRUE if "idx" is for the map() function.
2021 */
2022 int
2023internal_func_is_map(int idx)
2024{
2025 return global_functions[idx].f_func == f_map;
2026}
2027
2028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002030 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2031 * first argument, 2 if method base is second argument, etc. 9 if method base
2032 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 */
2034 int
2035check_internal_func(int idx, int argcount)
2036{
2037 int res;
2038 char *name;
2039
2040 if (argcount < global_functions[idx].f_min_argc)
2041 res = FCERR_TOOFEW;
2042 else if (argcount > global_functions[idx].f_max_argc)
2043 res = FCERR_TOOMANY;
2044 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002045 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046
2047 name = internal_func_name(idx);
2048 if (res == FCERR_TOOMANY)
2049 semsg(_(e_toomanyarg), name);
2050 else
2051 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002052 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053}
2054
Bram Moolenaarac92e252019-08-03 21:58:38 +02002055 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056call_internal_func(
2057 char_u *name,
2058 int argcount,
2059 typval_T *argvars,
2060 typval_T *rettv)
2061{
2062 int i;
2063
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002064 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002066 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002067 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002068 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002069 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002070 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002072 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002073 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002074}
2075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 void
2077call_internal_func_by_idx(
2078 int idx,
2079 typval_T *argvars,
2080 typval_T *rettv)
2081{
2082 global_functions[idx].f_func(argvars, rettv);
2083}
2084
Bram Moolenaarac92e252019-08-03 21:58:38 +02002085/*
2086 * Invoke a method for base->method().
2087 */
2088 int
2089call_internal_method(
2090 char_u *name,
2091 int argcount,
2092 typval_T *argvars,
2093 typval_T *rettv,
2094 typval_T *basetv)
2095{
2096 int i;
2097 int fi;
2098 typval_T argv[MAX_FUNC_ARGS + 1];
2099
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002100 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002101 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002102 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002103 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002105 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002106 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002107 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002108 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002109
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002110 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002111 {
2112 // base value goes last
2113 for (i = 0; i < argcount; ++i)
2114 argv[i] = argvars[i];
2115 argv[argcount] = *basetv;
2116 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002117 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002118 {
2119 // base value goes second
2120 argv[0] = argvars[0];
2121 argv[1] = *basetv;
2122 for (i = 1; i < argcount; ++i)
2123 argv[i + 1] = argvars[i];
2124 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002125 else if (global_functions[fi].f_argtype == FEARG_3)
2126 {
2127 // base value goes third
2128 argv[0] = argvars[0];
2129 argv[1] = argvars[1];
2130 argv[2] = *basetv;
2131 for (i = 2; i < argcount; ++i)
2132 argv[i + 1] = argvars[i];
2133 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002134 else if (global_functions[fi].f_argtype == FEARG_4)
2135 {
2136 // base value goes fourth
2137 argv[0] = argvars[0];
2138 argv[1] = argvars[1];
2139 argv[2] = argvars[2];
2140 argv[3] = *basetv;
2141 for (i = 3; i < argcount; ++i)
2142 argv[i + 1] = argvars[i];
2143 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002144 else
2145 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002146 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002147 argv[0] = *basetv;
2148 for (i = 0; i < argcount; ++i)
2149 argv[i + 1] = argvars[i];
2150 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002151 argv[argcount + 1].v_type = VAR_UNKNOWN;
2152
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002153 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002154 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155}
2156
2157/*
2158 * Return TRUE for a non-zero Number and a non-empty String.
2159 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002160 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161non_zero_arg(typval_T *argvars)
2162{
2163 return ((argvars[0].v_type == VAR_NUMBER
2164 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002165 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 && argvars[0].vval.v_number == VVAL_TRUE)
2167 || (argvars[0].v_type == VAR_STRING
2168 && argvars[0].vval.v_string != NULL
2169 && *argvars[0].vval.v_string != NUL));
2170}
2171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 * "and(expr, expr)" function
2174 */
2175 static void
2176f_and(typval_T *argvars, typval_T *rettv)
2177{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002178 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2179 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002180}
2181
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002183 * "balloon_show()" function
2184 */
2185#ifdef FEAT_BEVAL
2186 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002187f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2188{
2189 rettv->v_type = VAR_STRING;
2190 if (balloonEval != NULL)
2191 {
2192 if (balloonEval->msg == NULL)
2193 rettv->vval.v_string = NULL;
2194 else
2195 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2196 }
2197}
2198
2199 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002200f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2201{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002202 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002203 {
2204 if (argvars[0].v_type == VAR_LIST
2205# ifdef FEAT_GUI
2206 && !gui.in_use
2207# endif
2208 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002209 {
2210 list_T *l = argvars[0].vval.v_list;
2211
2212 // empty list removes the balloon
2213 post_balloon(balloonEval, NULL,
2214 l == NULL || l->lv_len == 0 ? NULL : l);
2215 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002216 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002217 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002218 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002219
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002220 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2221 return;
2222
2223 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002224 if (mesg != NULL)
2225 // empty string removes the balloon
2226 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2227 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002228 }
2229}
2230
Bram Moolenaar669a8282017-11-19 20:13:05 +01002231# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002232 static void
2233f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2234{
2235 if (rettv_list_alloc(rettv) == OK)
2236 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002237 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002238
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002239 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2240 return;
2241 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002242 if (msg != NULL)
2243 {
2244 pumitem_T *array;
2245 int size = split_message(msg, &array);
2246 int i;
2247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002248 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002249 for (i = 1; i < size - 1; ++i)
2250 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002251 while (size > 0)
2252 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002253 vim_free(array);
2254 }
2255 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002256}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002257# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002258#endif
2259
2260/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002261 * Get the buffer from "arg" and give an error and return NULL if it is not
2262 * valid.
2263 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002264 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002265get_buf_arg(typval_T *arg)
2266{
2267 buf_T *buf;
2268
2269 ++emsg_off;
2270 buf = tv_get_buf(arg, FALSE);
2271 --emsg_off;
2272 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002273 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002274 return buf;
2275}
2276
2277/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002278 * "byte2line(byte)" function
2279 */
2280 static void
2281f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2282{
2283#ifndef FEAT_BYTEOFF
2284 rettv->vval.v_number = -1;
2285#else
2286 long boff = 0;
2287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002288 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289 if (boff < 0)
2290 rettv->vval.v_number = -1;
2291 else
2292 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2293 (linenr_T)0, &boff);
2294#endif
2295}
2296
2297 static void
2298byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2299{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002301 char_u *str;
2302 varnumber_T idx;
2303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 str = tv_get_string_chk(&argvars[0]);
2305 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 rettv->vval.v_number = -1;
2307 if (str == NULL || idx < 0)
2308 return;
2309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002310 t = str;
2311 for ( ; idx > 0; idx--)
2312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002313 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314 return;
2315 if (enc_utf8 && comp)
2316 t += utf_ptr2len(t);
2317 else
2318 t += (*mb_ptr2len)(t);
2319 }
2320 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002321}
2322
2323/*
2324 * "byteidx()" function
2325 */
2326 static void
2327f_byteidx(typval_T *argvars, typval_T *rettv)
2328{
2329 byteidx(argvars, rettv, FALSE);
2330}
2331
2332/*
2333 * "byteidxcomp()" function
2334 */
2335 static void
2336f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2337{
2338 byteidx(argvars, rettv, TRUE);
2339}
2340
2341/*
2342 * "call(func, arglist [, dict])" function
2343 */
2344 static void
2345f_call(typval_T *argvars, typval_T *rettv)
2346{
2347 char_u *func;
2348 partial_T *partial = NULL;
2349 dict_T *selfdict = NULL;
2350
2351 if (argvars[1].v_type != VAR_LIST)
2352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002354 return;
2355 }
2356 if (argvars[1].vval.v_list == NULL)
2357 return;
2358
2359 if (argvars[0].v_type == VAR_FUNC)
2360 func = argvars[0].vval.v_string;
2361 else if (argvars[0].v_type == VAR_PARTIAL)
2362 {
2363 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002364 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365 }
2366 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002367 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002368 if (func == NULL || *func == NUL)
2369 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370
2371 if (argvars[2].v_type != VAR_UNKNOWN)
2372 {
2373 if (argvars[2].v_type != VAR_DICT)
2374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002375 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 return;
2377 }
2378 selfdict = argvars[2].vval.v_dict;
2379 }
2380
2381 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2382}
2383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384/*
2385 * "changenr()" function
2386 */
2387 static void
2388f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2389{
2390 rettv->vval.v_number = curbuf->b_u_seq_cur;
2391}
2392
2393/*
2394 * "char2nr(string)" function
2395 */
2396 static void
2397f_char2nr(typval_T *argvars, typval_T *rettv)
2398{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002399 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2400 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002401 if (has_mbyte)
2402 {
2403 int utf8 = 0;
2404
2405 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002406 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002407
2408 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002409 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002411 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 }
2413 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002414 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002417/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002418 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2419 * returns the character index of the column. Otherwise, returns the byte index
2420 * of the column.
2421 */
2422 static void
2423get_col(typval_T *argvars, typval_T *rettv, int charcol)
2424{
2425 colnr_T col = 0;
2426 pos_T *fp;
2427 int fnum = curbuf->b_fnum;
2428
2429 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2430 if (fp != NULL && fnum == curbuf->b_fnum)
2431 {
2432 if (fp->col == MAXCOL)
2433 {
2434 // '> can be MAXCOL, get the length of the line then
2435 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2436 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2437 else
2438 col = MAXCOL;
2439 }
2440 else
2441 {
2442 col = fp->col + 1;
2443 // col(".") when the cursor is on the NUL at the end of the line
2444 // because of "coladd" can be seen as an extra column.
2445 if (virtual_active() && fp == &curwin->w_cursor)
2446 {
2447 char_u *p = ml_get_cursor();
2448
2449 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2450 curwin->w_virtcol - curwin->w_cursor.coladd))
2451 {
2452 int l;
2453
2454 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2455 col += l;
2456 }
2457 }
2458 }
2459 }
2460 rettv->vval.v_number = col;
2461}
2462
2463/*
2464 * "charcol()" function
2465 */
2466 static void
2467f_charcol(typval_T *argvars, typval_T *rettv)
2468{
2469 get_col(argvars, rettv, TRUE);
2470}
2471
2472/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002473 * "charidx()" function
2474 */
2475 static void
2476f_charidx(typval_T *argvars, typval_T *rettv)
2477{
2478 char_u *str;
2479 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002480 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002481 char_u *p;
2482 int len;
2483 int (*ptr2len)(char_u *);
2484
2485 rettv->vval.v_number = -1;
2486
2487 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2488 || (argvars[2].v_type != VAR_UNKNOWN
2489 && argvars[2].v_type != VAR_NUMBER))
2490 {
2491 emsg(_(e_invarg));
2492 return;
2493 }
2494
2495 str = tv_get_string_chk(&argvars[0]);
2496 idx = tv_get_number_chk(&argvars[1], NULL);
2497 if (str == NULL || idx < 0)
2498 return;
2499
2500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002501 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002502 if (countcc < 0 || countcc > 1)
2503 {
2504 semsg(_(e_using_number_as_bool_nr), countcc);
2505 return;
2506 }
2507
2508 if (enc_utf8 && countcc)
2509 ptr2len = utf_ptr2len;
2510 else
2511 ptr2len = mb_ptr2len;
2512
2513 for (p = str, len = 0; p <= str + idx; len++)
2514 {
2515 if (*p == NUL)
2516 return;
2517 p += ptr2len(p);
2518 }
2519
2520 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2521}
2522
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002523 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002524get_optional_window(typval_T *argvars, int idx)
2525{
2526 win_T *win = curwin;
2527
2528 if (argvars[idx].v_type != VAR_UNKNOWN)
2529 {
2530 win = find_win_by_nr_or_id(&argvars[idx]);
2531 if (win == NULL)
2532 {
2533 emsg(_(e_invalwindow));
2534 return NULL;
2535 }
2536 }
2537 return win;
2538}
2539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 * "col(string)" function
2542 */
2543 static void
2544f_col(typval_T *argvars, typval_T *rettv)
2545{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002546 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002547}
2548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549/*
2550 * "confirm(message, buttons[, default [, type]])" function
2551 */
2552 static void
2553f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2554{
2555#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2556 char_u *message;
2557 char_u *buttons = NULL;
2558 char_u buf[NUMBUFLEN];
2559 char_u buf2[NUMBUFLEN];
2560 int def = 1;
2561 int type = VIM_GENERIC;
2562 char_u *typestr;
2563 int error = FALSE;
2564
Bram Moolenaarc5809432021-03-27 21:23:30 +01002565 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2566 return;
2567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002568 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 if (message == NULL)
2570 error = TRUE;
2571 if (argvars[1].v_type != VAR_UNKNOWN)
2572 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002573 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2574 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002575 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 if (buttons == NULL)
2577 error = TRUE;
2578 if (argvars[2].v_type != VAR_UNKNOWN)
2579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002580 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581 if (argvars[3].v_type != VAR_UNKNOWN)
2582 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002583 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2584 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002585 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586 if (typestr == NULL)
2587 error = TRUE;
2588 else
2589 {
2590 switch (TOUPPER_ASC(*typestr))
2591 {
2592 case 'E': type = VIM_ERROR; break;
2593 case 'Q': type = VIM_QUESTION; break;
2594 case 'I': type = VIM_INFO; break;
2595 case 'W': type = VIM_WARNING; break;
2596 case 'G': type = VIM_GENERIC; break;
2597 }
2598 }
2599 }
2600 }
2601 }
2602
2603 if (buttons == NULL || *buttons == NUL)
2604 buttons = (char_u *)_("&Ok");
2605
2606 if (!error)
2607 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2608 def, NULL, FALSE);
2609#endif
2610}
2611
2612/*
2613 * "copy()" function
2614 */
2615 static void
2616f_copy(typval_T *argvars, typval_T *rettv)
2617{
2618 item_copy(&argvars[0], rettv, FALSE, 0);
2619}
2620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002622 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002623 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002624 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002625 */
2626 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002627set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628{
2629 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631 int set_curswant = TRUE;
2632
2633 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002634 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002635 {
2636 pos_T pos;
2637 colnr_T curswant = -1;
2638
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002639 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 return;
2643 }
2644 line = pos.lnum;
2645 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002647 if (curswant >= 0)
2648 {
2649 curwin->w_curswant = curswant - 1;
2650 set_curswant = FALSE;
2651 }
2652 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002653 else if ((argvars[0].v_type == VAR_NUMBER ||
2654 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002655 && (argvars[1].v_type == VAR_NUMBER ||
2656 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002658 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002659 if (line < 0)
2660 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002661 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002662 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002663 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002665 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002667 else
2668 {
2669 emsg(_(e_invarg));
2670 return;
2671 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002672 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002673 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674 if (line > 0)
2675 curwin->w_cursor.lnum = line;
2676 if (col > 0)
2677 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002680 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 if (has_mbyte)
2684 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685
2686 curwin->w_set_curswant = set_curswant;
2687 rettv->vval.v_number = 0;
2688}
2689
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002690/*
2691 * "cursor(lnum, col)" function, or
2692 * "cursor(list)"
2693 *
2694 * Moves the cursor to the specified line and column.
2695 * Returns 0 when the position could be set, -1 otherwise.
2696 */
2697 static void
2698f_cursor(typval_T *argvars, typval_T *rettv)
2699{
2700 set_cursorpos(argvars, rettv, FALSE);
2701}
2702
Bram Moolenaar4f974752019-02-17 17:44:42 +01002703#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002704/*
2705 * "debugbreak()" function
2706 */
2707 static void
2708f_debugbreak(typval_T *argvars, typval_T *rettv)
2709{
2710 int pid;
2711
2712 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002713 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002714 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002715 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002716 else
2717 {
2718 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2719
2720 if (hProcess != NULL)
2721 {
2722 DebugBreakProcess(hProcess);
2723 CloseHandle(hProcess);
2724 rettv->vval.v_number = OK;
2725 }
2726 }
2727}
2728#endif
2729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730/*
2731 * "deepcopy()" function
2732 */
2733 static void
2734f_deepcopy(typval_T *argvars, typval_T *rettv)
2735{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002736 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002737 int copyID;
2738
2739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002740 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002741 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002742 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 else
2744 {
2745 copyID = get_copyID();
2746 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2747 }
2748}
2749
2750/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 * "did_filetype()" function
2752 */
2753 static void
2754f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2755{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757}
2758
2759/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002760 * "echoraw({expr})" function
2761 */
2762 static void
2763f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2764{
2765 char_u *str = tv_get_string_chk(&argvars[0]);
2766
2767 if (str != NULL && *str != NUL)
2768 {
2769 out_str(str);
2770 out_flush();
2771 }
2772}
2773
2774/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 * "empty({expr})" function
2776 */
2777 static void
2778f_empty(typval_T *argvars, typval_T *rettv)
2779{
2780 int n = FALSE;
2781
2782 switch (argvars[0].v_type)
2783 {
2784 case VAR_STRING:
2785 case VAR_FUNC:
2786 n = argvars[0].vval.v_string == NULL
2787 || *argvars[0].vval.v_string == NUL;
2788 break;
2789 case VAR_PARTIAL:
2790 n = FALSE;
2791 break;
2792 case VAR_NUMBER:
2793 n = argvars[0].vval.v_number == 0;
2794 break;
2795 case VAR_FLOAT:
2796#ifdef FEAT_FLOAT
2797 n = argvars[0].vval.v_float == 0.0;
2798 break;
2799#endif
2800 case VAR_LIST:
2801 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002802 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 break;
2804 case VAR_DICT:
2805 n = argvars[0].vval.v_dict == NULL
2806 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2807 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002808 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809 case VAR_SPECIAL:
2810 n = argvars[0].vval.v_number != VVAL_TRUE;
2811 break;
2812
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002813 case VAR_BLOB:
2814 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002815 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2816 break;
2817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002818 case VAR_JOB:
2819#ifdef FEAT_JOB_CHANNEL
2820 n = argvars[0].vval.v_job == NULL
2821 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2822 break;
2823#endif
2824 case VAR_CHANNEL:
2825#ifdef FEAT_JOB_CHANNEL
2826 n = argvars[0].vval.v_channel == NULL
2827 || !channel_is_open(argvars[0].vval.v_channel);
2828 break;
2829#endif
2830 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002831 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002833 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002834 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 n = TRUE;
2836 break;
2837 }
2838
2839 rettv->vval.v_number = n;
2840}
2841
2842/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002843 * "environ()" function
2844 */
2845 static void
2846f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2847{
2848#if !defined(AMIGA)
2849 int i = 0;
2850 char_u *entry, *value;
2851# ifdef MSWIN
2852 extern wchar_t **_wenviron;
2853# else
2854 extern char **environ;
2855# endif
2856
2857 if (rettv_dict_alloc(rettv) != OK)
2858 return;
2859
2860# ifdef MSWIN
2861 if (*_wenviron == NULL)
2862 return;
2863# else
2864 if (*environ == NULL)
2865 return;
2866# endif
2867
2868 for (i = 0; ; ++i)
2869 {
2870# ifdef MSWIN
2871 short_u *p;
2872
2873 if ((p = (short_u *)_wenviron[i]) == NULL)
2874 return;
2875 entry = utf16_to_enc(p, NULL);
2876# else
2877 if ((entry = (char_u *)environ[i]) == NULL)
2878 return;
2879 entry = vim_strsave(entry);
2880# endif
2881 if (entry == NULL) // out of memory
2882 return;
2883 if ((value = vim_strchr(entry, '=')) == NULL)
2884 {
2885 vim_free(entry);
2886 continue;
2887 }
2888 *value++ = NUL;
2889 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2890 vim_free(entry);
2891 }
2892#endif
2893}
2894
2895/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002896 * "escape({string}, {chars})" function
2897 */
2898 static void
2899f_escape(typval_T *argvars, typval_T *rettv)
2900{
2901 char_u buf[NUMBUFLEN];
2902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002903 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2904 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 rettv->v_type = VAR_STRING;
2906}
2907
2908/*
2909 * "eval()" function
2910 */
2911 static void
2912f_eval(typval_T *argvars, typval_T *rettv)
2913{
2914 char_u *s, *p;
2915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002916 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 if (s != NULL)
2918 s = skipwhite(s);
2919
2920 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922 {
2923 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002924 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 need_clr_eos = FALSE;
2926 rettv->v_type = VAR_NUMBER;
2927 rettv->vval.v_number = 0;
2928 }
2929 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002930 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931}
2932
2933/*
2934 * "eventhandler()" function
2935 */
2936 static void
2937f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2938{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002939 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940}
2941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942static garray_T redir_execute_ga;
2943
2944/*
2945 * Append "value[value_len]" to the execute() output.
2946 */
2947 void
2948execute_redir_str(char_u *value, int value_len)
2949{
2950 int len;
2951
2952 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002953 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 if (ga_grow(&redir_execute_ga, len) == OK)
2957 {
2958 mch_memmove((char *)redir_execute_ga.ga_data
2959 + redir_execute_ga.ga_len, value, len);
2960 redir_execute_ga.ga_len += len;
2961 }
2962}
2963
2964/*
2965 * Get next line from a list.
2966 * Called by do_cmdline() to get the next line.
2967 * Returns allocated string, or NULL for end of function.
2968 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 static char_u *
2970get_list_line(
2971 int c UNUSED,
2972 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002973 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002974 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002975{
2976 listitem_T **p = (listitem_T **)cookie;
2977 listitem_T *item = *p;
2978 char_u buf[NUMBUFLEN];
2979 char_u *s;
2980
2981 if (item == NULL)
2982 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002983 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002984 *p = item->li_next;
2985 return s == NULL ? NULL : vim_strsave(s);
2986}
2987
2988/*
2989 * "execute()" function
2990 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002991 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002992execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993{
2994 char_u *cmd = NULL;
2995 list_T *list = NULL;
2996 int save_msg_silent = msg_silent;
2997 int save_emsg_silent = emsg_silent;
2998 int save_emsg_noredir = emsg_noredir;
2999 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003000 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003002 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003003 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004
3005 rettv->vval.v_string = NULL;
3006 rettv->v_type = VAR_STRING;
3007
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003008 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003010 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003011 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003012 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 return;
3014 ++list->lv_refcount;
3015 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003016 else if (argvars[arg_off].v_type == VAR_JOB
3017 || argvars[arg_off].v_type == VAR_CHANNEL)
3018 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003019 semsg(_(e_using_invalid_value_as_string_str),
3020 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003021 return;
3022 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 else
3024 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003025 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 if (cmd == NULL)
3027 return;
3028 }
3029
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003030 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 {
3032 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003033 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3034 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035
3036 if (s == NULL)
3037 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003038 if (*s == NUL)
3039 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 if (STRNCMP(s, "silent", 6) == 0)
3041 ++msg_silent;
3042 if (STRCMP(s, "silent!") == 0)
3043 {
3044 emsg_silent = TRUE;
3045 emsg_noredir = TRUE;
3046 }
3047 }
3048 else
3049 ++msg_silent;
3050
3051 if (redir_execute)
3052 save_ga = redir_execute_ga;
3053 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3054 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003055 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003056 if (!echo_output)
3057 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058
3059 if (cmd != NULL)
3060 do_cmdline_cmd(cmd);
3061 else
3062 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003063 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003065 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003066 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003067 do_cmdline(NULL, get_list_line, (void *)&item,
3068 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3069 --list->lv_refcount;
3070 }
3071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003072 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003073 if (ga_grow(&redir_execute_ga, 1) == OK)
3074 {
3075 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3076 rettv->vval.v_string = redir_execute_ga.ga_data;
3077 }
3078 else
3079 {
3080 ga_clear(&redir_execute_ga);
3081 rettv->vval.v_string = NULL;
3082 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 msg_silent = save_msg_silent;
3084 emsg_silent = save_emsg_silent;
3085 emsg_noredir = save_emsg_noredir;
3086
3087 redir_execute = save_redir_execute;
3088 if (redir_execute)
3089 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003090 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003091
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003092 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003093 if (echo_output)
3094 // When not working silently: put it in column zero. A following
3095 // "echon" will overwrite the message, unavoidably.
3096 msg_col = 0;
3097 else
3098 // When working silently: Put it back where it was, since nothing
3099 // should have been written.
3100 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101}
3102
3103/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003104 * "execute()" function
3105 */
3106 static void
3107f_execute(typval_T *argvars, typval_T *rettv)
3108{
3109 execute_common(argvars, rettv, 0);
3110}
3111
3112/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 * "exists()" function
3114 */
3115 static void
3116f_exists(typval_T *argvars, typval_T *rettv)
3117{
3118 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003119 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003121 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003122 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003124 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 if (mch_getenv(p + 1) != NULL)
3126 n = TRUE;
3127 else
3128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003129 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130 p = expand_env_save(p);
3131 if (p != NULL && *p != '$')
3132 n = TRUE;
3133 vim_free(p);
3134 }
3135 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003136 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003138 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003140 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003142 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003144 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003145 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003146 else if (*p == '?') // internal function only
3147 {
3148 n = has_internal_func_name(p + 1);
3149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003150 else if (*p == ':')
3151 {
3152 n = cmd_exists(p + 1);
3153 }
3154 else if (*p == '#')
3155 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 if (p[1] == '#')
3157 n = autocmd_supported(p + 2);
3158 else
3159 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003160 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003161 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003163 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 }
3165
3166 rettv->vval.v_number = n;
3167}
3168
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169/*
3170 * "expand()" function
3171 */
3172 static void
3173f_expand(typval_T *argvars, typval_T *rettv)
3174{
3175 char_u *s;
3176 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003178 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3179 expand_T xpc;
3180 int error = FALSE;
3181 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003182#ifdef BACKSLASH_IN_FILENAME
3183 char_u *p_csl_save = p_csl;
3184
3185 // avoid using 'completeslash' here
3186 p_csl = empty_option;
3187#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003188
3189 rettv->v_type = VAR_STRING;
3190 if (argvars[1].v_type != VAR_UNKNOWN
3191 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003192 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003194 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003196 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197 if (*s == '%' || *s == '#' || *s == '<')
3198 {
3199 ++emsg_off;
3200 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3201 --emsg_off;
3202 if (rettv->v_type == VAR_LIST)
3203 {
3204 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3205 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003206 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003207 }
3208 else
3209 rettv->vval.v_string = result;
3210 }
3211 else
3212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003213 // When the optional second argument is non-zero, don't remove matches
3214 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003216 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217 options |= WILD_KEEP_ALL;
3218 if (!error)
3219 {
3220 ExpandInit(&xpc);
3221 xpc.xp_context = EXPAND_FILES;
3222 if (p_wic)
3223 options += WILD_ICASE;
3224 if (rettv->v_type == VAR_STRING)
3225 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3226 options, WILD_ALL);
3227 else if (rettv_list_alloc(rettv) != FAIL)
3228 {
3229 int i;
3230
3231 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3232 for (i = 0; i < xpc.xp_numfiles; i++)
3233 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3234 ExpandCleanup(&xpc);
3235 }
3236 }
3237 else
3238 rettv->vval.v_string = NULL;
3239 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003240#ifdef BACKSLASH_IN_FILENAME
3241 p_csl = p_csl_save;
3242#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243}
3244
3245/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003246 * "expandcmd()" function
3247 * Expand all the special characters in a command string.
3248 */
3249 static void
3250f_expandcmd(typval_T *argvars, typval_T *rettv)
3251{
3252 exarg_T eap;
3253 char_u *cmdstr;
3254 char *errormsg = NULL;
3255
3256 rettv->v_type = VAR_STRING;
3257 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3258
3259 memset(&eap, 0, sizeof(eap));
3260 eap.cmd = cmdstr;
3261 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003262 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003263 eap.usefilter = FALSE;
3264 eap.nextcmd = NULL;
3265 eap.cmdidx = CMD_USER;
3266
3267 expand_filename(&eap, &cmdstr, &errormsg);
3268 if (errormsg != NULL && *errormsg != NUL)
3269 emsg(errormsg);
3270
3271 rettv->vval.v_string = cmdstr;
3272}
3273
3274/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 * "feedkeys()" function
3276 */
3277 static void
3278f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3279{
3280 int remap = TRUE;
3281 int insert = FALSE;
3282 char_u *keys, *flags;
3283 char_u nbuf[NUMBUFLEN];
3284 int typed = FALSE;
3285 int execute = FALSE;
3286 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003287 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 char_u *keys_esc;
3289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003290 // This is not allowed in the sandbox. If the commands would still be
3291 // executed in the sandbox it would be OK, but it probably happens later,
3292 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 if (check_secure())
3294 return;
3295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003296 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297
3298 if (argvars[1].v_type != VAR_UNKNOWN)
3299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003300 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003301 for ( ; *flags != NUL; ++flags)
3302 {
3303 switch (*flags)
3304 {
3305 case 'n': remap = FALSE; break;
3306 case 'm': remap = TRUE; break;
3307 case 't': typed = TRUE; break;
3308 case 'i': insert = TRUE; break;
3309 case 'x': execute = TRUE; break;
3310 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003311 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312 }
3313 }
3314 }
3315
3316 if (*keys != NUL || execute)
3317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003318 // Need to escape K_SPECIAL and CSI before putting the string in the
3319 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003320 keys_esc = vim_strsave_escape_csi(keys);
3321 if (keys_esc != NULL)
3322 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003323 if (lowlevel)
3324 {
3325#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003326 int idx;
3327 int len = (int)STRLEN(keys);
3328
3329 for (idx = 0; idx < len; ++idx)
3330 {
3331 // if a CTRL-C was typed, set got_int, similar to what
3332 // happens in fill_input_buf()
3333 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3334 got_int = TRUE;
3335 add_to_input_buf(keys + idx, 1);
3336 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003337#else
3338 emsg(_("E980: lowlevel input not supported"));
3339#endif
3340 }
3341 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003342 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003343 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003345 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003346#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003347 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003348#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003349 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003350 typebuf_was_filled = TRUE;
3351 }
3352 vim_free(keys_esc);
3353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 if (execute)
3355 {
3356 int save_msg_scroll = msg_scroll;
3357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 msg_scroll = FALSE;
3360
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003361 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003362 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003363 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003364 ++in_feedkeys;
3365 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003366 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003367 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003368 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003369 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003370 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003371 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003372
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003373 msg_scroll |= save_msg_scroll;
3374 }
3375 }
3376 }
3377}
3378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003379/*
3380 * "fnameescape({string})" function
3381 */
3382 static void
3383f_fnameescape(typval_T *argvars, typval_T *rettv)
3384{
3385 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003386 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003387 rettv->v_type = VAR_STRING;
3388}
3389
3390/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 * "foreground()" function
3392 */
3393 static void
3394f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3395{
3396#ifdef FEAT_GUI
3397 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003398 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003400 return;
3401 }
3402#endif
3403#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003404 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405#endif
3406}
3407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003408 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003409common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410{
3411 char_u *s;
3412 char_u *name;
3413 int use_string = FALSE;
3414 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003415 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003416 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417
3418 if (argvars[0].v_type == VAR_FUNC)
3419 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003420 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 s = argvars[0].vval.v_string;
3422 }
3423 else if (argvars[0].v_type == VAR_PARTIAL
3424 && argvars[0].vval.v_partial != NULL)
3425 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003426 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003428 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429 }
3430 else
3431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003432 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 use_string = TRUE;
3435 }
3436
Bram Moolenaar843b8842016-08-21 14:36:15 +02003437 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003438 {
3439 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003440 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003441 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3442 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003443 if (*name != NUL)
3444 s = NULL;
3445 }
3446
Bram Moolenaar843b8842016-08-21 14:36:15 +02003447 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3448 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003449 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003450 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003451 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003452 ? find_func(trans_name, is_global, NULL) == NULL
3453 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 else
3456 {
3457 int dict_idx = 0;
3458 int arg_idx = 0;
3459 list_T *list = NULL;
3460
3461 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3462 {
3463 char sid_buf[25];
3464 int off = *s == 's' ? 2 : 5;
3465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003466 // Expand s: and <SID> into <SNR>nr_, so that the function can
3467 // also be called from another script. Using trans_function_name()
3468 // would also work, but some plugins depend on the name being
3469 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003470 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003471 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 if (name != NULL)
3473 {
3474 STRCPY(name, sid_buf);
3475 STRCAT(name, s + off);
3476 }
3477 }
3478 else
3479 name = vim_strsave(s);
3480
3481 if (argvars[1].v_type != VAR_UNKNOWN)
3482 {
3483 if (argvars[2].v_type != VAR_UNKNOWN)
3484 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003485 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 arg_idx = 1;
3487 dict_idx = 2;
3488 }
3489 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003490 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003491 dict_idx = 1;
3492 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 arg_idx = 1;
3495 if (dict_idx > 0)
3496 {
3497 if (argvars[dict_idx].v_type != VAR_DICT)
3498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003499 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003500 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003501 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 }
3503 if (argvars[dict_idx].vval.v_dict == NULL)
3504 dict_idx = 0;
3505 }
3506 if (arg_idx > 0)
3507 {
3508 if (argvars[arg_idx].v_type != VAR_LIST)
3509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003510 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003511 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003512 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513 }
3514 list = argvars[arg_idx].vval.v_list;
3515 if (list == NULL || list->lv_len == 0)
3516 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003517 else if (list->lv_len > MAX_FUNC_ARGS)
3518 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003519 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003520 vim_free(name);
3521 goto theend;
3522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 }
3524 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003525 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003527 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003529 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 if (pt == NULL)
3531 vim_free(name);
3532 else
3533 {
3534 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3535 {
3536 listitem_T *li;
3537 int i = 0;
3538 int arg_len = 0;
3539 int lv_len = 0;
3540
3541 if (arg_pt != NULL)
3542 arg_len = arg_pt->pt_argc;
3543 if (list != NULL)
3544 lv_len = list->lv_len;
3545 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003546 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547 if (pt->pt_argv == NULL)
3548 {
3549 vim_free(pt);
3550 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003551 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003553 for (i = 0; i < arg_len; i++)
3554 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3555 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003556 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003557 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003558 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003559 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003560 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 }
3562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003563 // For "function(dict.func, [], dict)" and "func" is a partial
3564 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 if (dict_idx > 0)
3566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3569 ++pt->pt_dict->dv_refcount;
3570 }
3571 else if (arg_pt != NULL)
3572 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003573 // If the dict was bound automatically the result is also
3574 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 pt->pt_dict = arg_pt->pt_dict;
3576 pt->pt_auto = arg_pt->pt_auto;
3577 if (pt->pt_dict != NULL)
3578 ++pt->pt_dict->dv_refcount;
3579 }
3580
3581 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003582 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3583 {
3584 pt->pt_func = arg_pt->pt_func;
3585 func_ptr_ref(pt->pt_func);
3586 vim_free(name);
3587 }
3588 else if (is_funcref)
3589 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003590 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003591 func_ptr_ref(pt->pt_func);
3592 vim_free(name);
3593 }
3594 else
3595 {
3596 pt->pt_name = name;
3597 func_ref(name);
3598 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 }
3600 rettv->v_type = VAR_PARTIAL;
3601 rettv->vval.v_partial = pt;
3602 }
3603 else
3604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003605 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003606 rettv->v_type = VAR_FUNC;
3607 rettv->vval.v_string = name;
3608 func_ref(name);
3609 }
3610 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611theend:
3612 vim_free(trans_name);
3613}
3614
3615/*
3616 * "funcref()" function
3617 */
3618 static void
3619f_funcref(typval_T *argvars, typval_T *rettv)
3620{
3621 common_function(argvars, rettv, TRUE);
3622}
3623
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003624 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003625ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003626{
3627 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3628 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003629 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003630}
3631
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632/*
3633 * "function()" function
3634 */
3635 static void
3636f_function(typval_T *argvars, typval_T *rettv)
3637{
3638 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639}
3640
3641/*
3642 * "garbagecollect()" function
3643 */
3644 static void
3645f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3646{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // This is postponed until we are back at the toplevel, because we may be
3648 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 want_garbage_collect = TRUE;
3650
Bram Moolenaar2df47312020-09-05 17:30:44 +02003651 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 garbage_collect_at_exit = TRUE;
3653}
3654
3655/*
3656 * "get()" function
3657 */
3658 static void
3659f_get(typval_T *argvars, typval_T *rettv)
3660{
3661 listitem_T *li;
3662 list_T *l;
3663 dictitem_T *di;
3664 dict_T *d;
3665 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003666 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003668 if (argvars[0].v_type == VAR_BLOB)
3669 {
3670 int error = FALSE;
3671 int idx = tv_get_number_chk(&argvars[1], &error);
3672
3673 if (!error)
3674 {
3675 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003676 if (idx < 0)
3677 idx = blob_len(argvars[0].vval.v_blob) + idx;
3678 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3679 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003680 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003681 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003682 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003683 tv = rettv;
3684 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003685 }
3686 }
3687 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 {
3689 if ((l = argvars[0].vval.v_list) != NULL)
3690 {
3691 int error = FALSE;
3692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003693 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 if (!error && li != NULL)
3695 tv = &li->li_tv;
3696 }
3697 }
3698 else if (argvars[0].v_type == VAR_DICT)
3699 {
3700 if ((d = argvars[0].vval.v_dict) != NULL)
3701 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003702 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703 if (di != NULL)
3704 tv = &di->di_tv;
3705 }
3706 }
3707 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3708 {
3709 partial_T *pt;
3710 partial_T fref_pt;
3711
3712 if (argvars[0].v_type == VAR_PARTIAL)
3713 pt = argvars[0].vval.v_partial;
3714 else
3715 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003716 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003717 fref_pt.pt_name = argvars[0].vval.v_string;
3718 pt = &fref_pt;
3719 }
3720
3721 if (pt != NULL)
3722 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003723 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003724 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725
3726 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3727 {
3728 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 n = partial_name(pt);
3730 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731 rettv->vval.v_string = NULL;
3732 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 {
3734 rettv->vval.v_string = vim_strsave(n);
3735 if (rettv->v_type == VAR_FUNC)
3736 func_ref(rettv->vval.v_string);
3737 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 }
3739 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003740 {
3741 what_is_dict = TRUE;
3742 if (pt->pt_dict != NULL)
3743 rettv_dict_set(rettv, pt->pt_dict);
3744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745 else if (STRCMP(what, "args") == 0)
3746 {
3747 rettv->v_type = VAR_LIST;
3748 if (rettv_list_alloc(rettv) == OK)
3749 {
3750 int i;
3751
3752 for (i = 0; i < pt->pt_argc; ++i)
3753 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3754 }
3755 }
3756 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003757 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003758
3759 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3760 // third argument
3761 if (!what_is_dict)
3762 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763 }
3764 }
3765 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003766 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003767
3768 if (tv == NULL)
3769 {
3770 if (argvars[2].v_type != VAR_UNKNOWN)
3771 copy_tv(&argvars[2], rettv);
3772 }
3773 else
3774 copy_tv(tv, rettv);
3775}
3776
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003777/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003778 * "getchangelist()" function
3779 */
3780 static void
3781f_getchangelist(typval_T *argvars, typval_T *rettv)
3782{
3783#ifdef FEAT_JUMPLIST
3784 buf_T *buf;
3785 int i;
3786 list_T *l;
3787 dict_T *d;
3788#endif
3789
3790 if (rettv_list_alloc(rettv) != OK)
3791 return;
3792
3793#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003794 if (argvars[0].v_type == VAR_UNKNOWN)
3795 buf = curbuf;
3796 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003797 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003798 if (buf == NULL)
3799 return;
3800
3801 l = list_alloc();
3802 if (l == NULL)
3803 return;
3804
3805 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3806 return;
3807 /*
3808 * The current window change list index tracks only the position in the
3809 * current buffer change list. For other buffers, use the change list
3810 * length as the current index.
3811 */
3812 list_append_number(rettv->vval.v_list,
3813 (varnumber_T)((buf == curwin->w_buffer)
3814 ? curwin->w_changelistidx : buf->b_changelistlen));
3815
3816 for (i = 0; i < buf->b_changelistlen; ++i)
3817 {
3818 if (buf->b_changelist[i].lnum == 0)
3819 continue;
3820 if ((d = dict_alloc()) == NULL)
3821 return;
3822 if (list_append_dict(l, d) == FAIL)
3823 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003824 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3825 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003826 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003827 }
3828#endif
3829}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003831 static void
3832getpos_both(
3833 typval_T *argvars,
3834 typval_T *rettv,
3835 int getcurpos,
3836 int charcol)
3837{
3838 pos_T *fp = NULL;
3839 pos_T pos;
3840 win_T *wp = curwin;
3841 list_T *l;
3842 int fnum = -1;
3843
3844 if (rettv_list_alloc(rettv) == OK)
3845 {
3846 l = rettv->vval.v_list;
3847 if (getcurpos)
3848 {
3849 if (argvars[0].v_type != VAR_UNKNOWN)
3850 {
3851 wp = find_win_by_nr_or_id(&argvars[0]);
3852 if (wp != NULL)
3853 fp = &wp->w_cursor;
3854 }
3855 else
3856 fp = &curwin->w_cursor;
3857 if (fp != NULL && charcol)
3858 {
3859 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003860 pos.col =
3861 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003862 fp = &pos;
3863 }
3864 }
3865 else
3866 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3867 if (fnum != -1)
3868 list_append_number(l, (varnumber_T)fnum);
3869 else
3870 list_append_number(l, (varnumber_T)0);
3871 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3872 : (varnumber_T)0);
3873 list_append_number(l, (fp != NULL)
3874 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3875 : (varnumber_T)0);
3876 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3877 (varnumber_T)0);
3878 if (getcurpos)
3879 {
3880 int save_set_curswant = curwin->w_set_curswant;
3881 colnr_T save_curswant = curwin->w_curswant;
3882 colnr_T save_virtcol = curwin->w_virtcol;
3883
3884 if (wp == curwin)
3885 update_curswant();
3886 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3887 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3888
3889 // Do not change "curswant", as it is unexpected that a get
3890 // function has a side effect.
3891 if (wp == curwin && save_set_curswant)
3892 {
3893 curwin->w_set_curswant = save_set_curswant;
3894 curwin->w_curswant = save_curswant;
3895 curwin->w_virtcol = save_virtcol;
3896 curwin->w_valid &= ~VALID_VIRTCOL;
3897 }
3898 }
3899 }
3900 else
3901 rettv->vval.v_number = FALSE;
3902}
3903
3904/*
3905 * "getcharpos()" function
3906 */
3907 static void
3908f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3909{
3910 getpos_both(argvars, rettv, FALSE, TRUE);
3911}
3912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003913/*
3914 * "getcharsearch()" function
3915 */
3916 static void
3917f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3918{
3919 if (rettv_dict_alloc(rettv) != FAIL)
3920 {
3921 dict_T *dict = rettv->vval.v_dict;
3922
Bram Moolenaare0be1672018-07-08 16:50:37 +02003923 dict_add_string(dict, "char", last_csearch());
3924 dict_add_number(dict, "forward", last_csearch_forward());
3925 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 }
3927}
3928
3929/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003930 * "getenv()" function
3931 */
3932 static void
3933f_getenv(typval_T *argvars, typval_T *rettv)
3934{
3935 int mustfree = FALSE;
3936 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3937
3938 if (p == NULL)
3939 {
3940 rettv->v_type = VAR_SPECIAL;
3941 rettv->vval.v_number = VVAL_NULL;
3942 return;
3943 }
3944 if (!mustfree)
3945 p = vim_strsave(p);
3946 rettv->vval.v_string = p;
3947 rettv->v_type = VAR_STRING;
3948}
3949
3950/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951 * "getfontname()" function
3952 */
3953 static void
3954f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3955{
3956 rettv->v_type = VAR_STRING;
3957 rettv->vval.v_string = NULL;
3958#ifdef FEAT_GUI
3959 if (gui.in_use)
3960 {
3961 GuiFont font;
3962 char_u *name = NULL;
3963
3964 if (argvars[0].v_type == VAR_UNKNOWN)
3965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003966 // Get the "Normal" font. Either the name saved by
3967 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 font = gui.norm_font;
3969 name = hl_get_font_name();
3970 }
3971 else
3972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003973 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003974 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 return;
3976 font = gui_mch_get_font(name, FALSE);
3977 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003978 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003979 }
3980 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3981 if (argvars[0].v_type != VAR_UNKNOWN)
3982 gui_mch_free_font(font);
3983 }
3984#endif
3985}
3986
3987/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003988 * "getjumplist()" function
3989 */
3990 static void
3991f_getjumplist(typval_T *argvars, typval_T *rettv)
3992{
3993#ifdef FEAT_JUMPLIST
3994 win_T *wp;
3995 int i;
3996 list_T *l;
3997 dict_T *d;
3998#endif
3999
4000 if (rettv_list_alloc(rettv) != OK)
4001 return;
4002
4003#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004004 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004005 if (wp == NULL)
4006 return;
4007
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004008 cleanup_jumplist(wp, TRUE);
4009
Bram Moolenaar4f505882018-02-10 21:06:32 +01004010 l = list_alloc();
4011 if (l == NULL)
4012 return;
4013
4014 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4015 return;
4016 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4017
4018 for (i = 0; i < wp->w_jumplistlen; ++i)
4019 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004020 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4021 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004022 if ((d = dict_alloc()) == NULL)
4023 return;
4024 if (list_append_dict(l, d) == FAIL)
4025 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004026 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4027 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004029 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004030 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004031 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004032 }
4033#endif
4034}
4035
4036/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004037 * "getpid()" function
4038 */
4039 static void
4040f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4041{
4042 rettv->vval.v_number = mch_get_pid();
4043}
4044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004045/*
4046 * "getcurpos()" function
4047 */
4048 static void
4049f_getcurpos(typval_T *argvars, typval_T *rettv)
4050{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004051 getpos_both(argvars, rettv, TRUE, FALSE);
4052}
4053
4054 static void
4055f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4056{
4057 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058}
4059
4060/*
4061 * "getpos(string)" function
4062 */
4063 static void
4064f_getpos(typval_T *argvars, typval_T *rettv)
4065{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004066 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067}
4068
4069/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 * "getreg()" function
4071 */
4072 static void
4073f_getreg(typval_T *argvars, typval_T *rettv)
4074{
4075 char_u *strregname;
4076 int regname;
4077 int arg2 = FALSE;
4078 int return_list = FALSE;
4079 int error = FALSE;
4080
4081 if (argvars[0].v_type != VAR_UNKNOWN)
4082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004083 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004084 if (strregname == NULL)
4085 error = TRUE;
4086 else if (in_vim9script() && STRLEN(strregname) > 1)
4087 {
4088 semsg(_(e_register_name_must_be_one_char_str), strregname);
4089 error = TRUE;
4090 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 if (argvars[1].v_type != VAR_UNKNOWN)
4092 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004093 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004095 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 }
4097 }
4098 else
4099 strregname = get_vim_var_str(VV_REG);
4100
4101 if (error)
4102 return;
4103
4104 regname = (strregname == NULL ? '"' : *strregname);
4105 if (regname == 0)
4106 regname = '"';
4107
4108 if (return_list)
4109 {
4110 rettv->v_type = VAR_LIST;
4111 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4112 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4113 if (rettv->vval.v_list == NULL)
4114 (void)rettv_list_alloc(rettv);
4115 else
4116 ++rettv->vval.v_list->lv_refcount;
4117 }
4118 else
4119 {
4120 rettv->v_type = VAR_STRING;
4121 rettv->vval.v_string = get_reg_contents(regname,
4122 arg2 ? GREG_EXPR_SRC : 0);
4123 }
4124}
4125
4126/*
4127 * "getregtype()" function
4128 */
4129 static void
4130f_getregtype(typval_T *argvars, typval_T *rettv)
4131{
4132 char_u *strregname;
4133 int regname;
4134 char_u buf[NUMBUFLEN + 2];
4135 long reglen = 0;
4136
4137 if (argvars[0].v_type != VAR_UNKNOWN)
4138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004139 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004140 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4141 {
4142 semsg(_(e_register_name_must_be_one_char_str), strregname);
4143 strregname = NULL;
4144 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004145 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 {
4147 rettv->v_type = VAR_STRING;
4148 rettv->vval.v_string = NULL;
4149 return;
4150 }
4151 }
4152 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004153 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154 strregname = get_vim_var_str(VV_REG);
4155
4156 regname = (strregname == NULL ? '"' : *strregname);
4157 if (regname == 0)
4158 regname = '"';
4159
4160 buf[0] = NUL;
4161 buf[1] = NUL;
4162 switch (get_reg_type(regname, &reglen))
4163 {
4164 case MLINE: buf[0] = 'V'; break;
4165 case MCHAR: buf[0] = 'v'; break;
4166 case MBLOCK:
4167 buf[0] = Ctrl_V;
4168 sprintf((char *)buf + 1, "%ld", reglen + 1);
4169 break;
4170 }
4171 rettv->v_type = VAR_STRING;
4172 rettv->vval.v_string = vim_strsave(buf);
4173}
4174
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004175/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004176 * "gettagstack()" function
4177 */
4178 static void
4179f_gettagstack(typval_T *argvars, typval_T *rettv)
4180{
4181 win_T *wp = curwin; // default is current window
4182
4183 if (rettv_dict_alloc(rettv) != OK)
4184 return;
4185
4186 if (argvars[0].v_type != VAR_UNKNOWN)
4187 {
4188 wp = find_win_by_nr_or_id(&argvars[0]);
4189 if (wp == NULL)
4190 return;
4191 }
4192
4193 get_tagstack(wp, rettv->vval.v_dict);
4194}
4195
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004196/*
4197 * "gettext()" function
4198 */
4199 static void
4200f_gettext(typval_T *argvars, typval_T *rettv)
4201{
4202 if (argvars[0].v_type != VAR_STRING
4203 || argvars[0].vval.v_string == NULL
4204 || *argvars[0].vval.v_string == NUL)
4205 {
4206 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4207 }
4208 else
4209 {
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = vim_strsave(
4212 (char_u *)_(argvars[0].vval.v_string));
4213 }
4214}
4215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004216// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217#include "version.h"
4218
4219/*
4220 * "has()" function
4221 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004222 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223f_has(typval_T *argvars, typval_T *rettv)
4224{
4225 int i;
4226 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004227 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004229 typedef struct {
4230 char *name;
4231 short present;
4232 } has_item_T;
4233 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004235 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004237 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004238#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004239 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004240#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004241 },
4242 {"arp",
4243#if defined(AMIGA) && defined(FEAT_ARP)
4244 1
4245#else
4246 0
4247#endif
4248 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004249 {"haiku",
4250#ifdef __HAIKU__
4251 1
4252#else
4253 0
4254#endif
4255 },
4256 {"bsd",
4257#if defined(BSD) && !defined(MACOS_X)
4258 1
4259#else
4260 0
4261#endif
4262 },
4263 {"hpux",
4264#ifdef hpux
4265 1
4266#else
4267 0
4268#endif
4269 },
4270 {"linux",
4271#ifdef __linux__
4272 1
4273#else
4274 0
4275#endif
4276 },
4277 {"mac", // Mac OS X (and, once, Mac OS Classic)
4278#ifdef MACOS_X
4279 1
4280#else
4281 0
4282#endif
4283 },
4284 {"osx", // Mac OS X
4285#ifdef MACOS_X
4286 1
4287#else
4288 0
4289#endif
4290 },
4291 {"macunix", // Mac OS X, with the darwin feature
4292#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4293 1
4294#else
4295 0
4296#endif
4297 },
4298 {"osxdarwin", // synonym for macunix
4299#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4300 1
4301#else
4302 0
4303#endif
4304 },
4305 {"qnx",
4306#ifdef __QNX__
4307 1
4308#else
4309 0
4310#endif
4311 },
4312 {"sun",
4313#ifdef SUN_SYSTEM
4314 1
4315#else
4316 0
4317#endif
4318 },
4319 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004320#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004321 1
4322#else
4323 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004325 },
4326 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004327#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004328 1
4329#else
4330 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004332 },
4333 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004334#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004335 1
4336#else
4337 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004339 },
4340 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004341#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004342 1
4343#else
4344 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004346 },
4347 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004348#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004349 1
4350#else
4351 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004353 },
4354 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004355#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004356 1
4357#else
4358 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004359#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004360 },
4361 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004363 1
4364#else
4365 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004366#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004367 },
4368 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004369#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004370 1
4371#else
4372 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004374 },
4375 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004376#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004377 1
4378#else
4379 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004380#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004381 },
4382 {"autocmd", 1},
4383 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004384#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004385 1
4386#else
4387 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004388#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004389 },
4390 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004391#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004392 1
4393#else
4394 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004395#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004396 },
4397 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004398#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004399 1
4400#else
4401 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004402#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004403 },
4404 {"balloon_multiline",
4405#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4406 // MS-Windows requires runtime check, see below
4407 1
4408#else
4409 0
4410#endif
4411 },
4412 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004413#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004414 1
4415#else
4416 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004417#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004418 },
4419 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004421 1
4422#else
4423 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004425 },
4426 {"all_builtin_terms",
4427#if defined(ALL_BUILTIN_TCAPS)
4428 1
4429#else
4430 0
4431#endif
4432 },
4433 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004435 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004437 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004439 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004441 },
4442 {"byte_offset",
4443#ifdef FEAT_BYTEOFF
4444 1
4445#else
4446 0
4447#endif
4448 },
4449 {"channel",
4450#ifdef FEAT_JOB_CHANNEL
4451 1
4452#else
4453 0
4454#endif
4455 },
4456 {"cindent",
4457#ifdef FEAT_CINDENT
4458 1
4459#else
4460 0
4461#endif
4462 },
4463 {"clientserver",
4464#ifdef FEAT_CLIENTSERVER
4465 1
4466#else
4467 0
4468#endif
4469 },
4470 {"clipboard",
4471#ifdef FEAT_CLIPBOARD
4472 1
4473#else
4474 0
4475#endif
4476 },
4477 {"cmdline_compl", 1},
4478 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004479 {"cmdwin",
4480#ifdef FEAT_CMDWIN
4481 1
4482#else
4483 0
4484#endif
4485 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004486 {"comments", 1},
4487 {"conceal",
4488#ifdef FEAT_CONCEAL
4489 1
4490#else
4491 0
4492#endif
4493 },
4494 {"cryptv",
4495#ifdef FEAT_CRYPT
4496 1
4497#else
4498 0
4499#endif
4500 },
4501 {"crypt-blowfish",
4502#ifdef FEAT_CRYPT
4503 1
4504#else
4505 0
4506#endif
4507 },
4508 {"crypt-blowfish2",
4509#ifdef FEAT_CRYPT
4510 1
4511#else
4512 0
4513#endif
4514 },
4515 {"cscope",
4516#ifdef FEAT_CSCOPE
4517 1
4518#else
4519 0
4520#endif
4521 },
4522 {"cursorbind", 1},
4523 {"cursorshape",
4524#ifdef CURSOR_SHAPE
4525 1
4526#else
4527 0
4528#endif
4529 },
4530 {"debug",
4531#ifdef DEBUG
4532 1
4533#else
4534 0
4535#endif
4536 },
4537 {"dialog_con",
4538#ifdef FEAT_CON_DIALOG
4539 1
4540#else
4541 0
4542#endif
4543 },
4544 {"dialog_gui",
4545#ifdef FEAT_GUI_DIALOG
4546 1
4547#else
4548 0
4549#endif
4550 },
4551 {"diff",
4552#ifdef FEAT_DIFF
4553 1
4554#else
4555 0
4556#endif
4557 },
4558 {"digraphs",
4559#ifdef FEAT_DIGRAPHS
4560 1
4561#else
4562 0
4563#endif
4564 },
4565 {"directx",
4566#ifdef FEAT_DIRECTX
4567 1
4568#else
4569 0
4570#endif
4571 },
4572 {"dnd",
4573#ifdef FEAT_DND
4574 1
4575#else
4576 0
4577#endif
4578 },
4579 {"emacs_tags",
4580#ifdef FEAT_EMACS_TAGS
4581 1
4582#else
4583 0
4584#endif
4585 },
4586 {"eval", 1}, // always present, of course!
4587 {"ex_extra", 1}, // graduated feature
4588 {"extra_search",
4589#ifdef FEAT_SEARCH_EXTRA
4590 1
4591#else
4592 0
4593#endif
4594 },
4595 {"file_in_path",
4596#ifdef FEAT_SEARCHPATH
4597 1
4598#else
4599 0
4600#endif
4601 },
4602 {"filterpipe",
4603#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4604 1
4605#else
4606 0
4607#endif
4608 },
4609 {"find_in_path",
4610#ifdef FEAT_FIND_ID
4611 1
4612#else
4613 0
4614#endif
4615 },
4616 {"float",
4617#ifdef FEAT_FLOAT
4618 1
4619#else
4620 0
4621#endif
4622 },
4623 {"folding",
4624#ifdef FEAT_FOLDING
4625 1
4626#else
4627 0
4628#endif
4629 },
4630 {"footer",
4631#ifdef FEAT_FOOTER
4632 1
4633#else
4634 0
4635#endif
4636 },
4637 {"fork",
4638#if !defined(USE_SYSTEM) && defined(UNIX)
4639 1
4640#else
4641 0
4642#endif
4643 },
4644 {"gettext",
4645#ifdef FEAT_GETTEXT
4646 1
4647#else
4648 0
4649#endif
4650 },
4651 {"gui",
4652#ifdef FEAT_GUI
4653 1
4654#else
4655 0
4656#endif
4657 },
4658 {"gui_neXtaw",
4659#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4660 1
4661#else
4662 0
4663#endif
4664 },
4665 {"gui_athena",
4666#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4667 1
4668#else
4669 0
4670#endif
4671 },
4672 {"gui_gtk",
4673#ifdef FEAT_GUI_GTK
4674 1
4675#else
4676 0
4677#endif
4678 },
4679 {"gui_gtk2",
4680#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4681 1
4682#else
4683 0
4684#endif
4685 },
4686 {"gui_gtk3",
4687#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4688 1
4689#else
4690 0
4691#endif
4692 },
4693 {"gui_gnome",
4694#ifdef FEAT_GUI_GNOME
4695 1
4696#else
4697 0
4698#endif
4699 },
4700 {"gui_haiku",
4701#ifdef FEAT_GUI_HAIKU
4702 1
4703#else
4704 0
4705#endif
4706 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004707 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004708 {"gui_motif",
4709#ifdef FEAT_GUI_MOTIF
4710 1
4711#else
4712 0
4713#endif
4714 },
4715 {"gui_photon",
4716#ifdef FEAT_GUI_PHOTON
4717 1
4718#else
4719 0
4720#endif
4721 },
4722 {"gui_win32",
4723#ifdef FEAT_GUI_MSWIN
4724 1
4725#else
4726 0
4727#endif
4728 },
4729 {"iconv",
4730#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4731 1
4732#else
4733 0
4734#endif
4735 },
4736 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004737 {"ipv6",
4738#ifdef FEAT_IPV6
4739 1
4740#else
4741 0
4742#endif
4743 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004744 {"job",
4745#ifdef FEAT_JOB_CHANNEL
4746 1
4747#else
4748 0
4749#endif
4750 },
4751 {"jumplist",
4752#ifdef FEAT_JUMPLIST
4753 1
4754#else
4755 0
4756#endif
4757 },
4758 {"keymap",
4759#ifdef FEAT_KEYMAP
4760 1
4761#else
4762 0
4763#endif
4764 },
4765 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4766 {"langmap",
4767#ifdef FEAT_LANGMAP
4768 1
4769#else
4770 0
4771#endif
4772 },
4773 {"libcall",
4774#ifdef FEAT_LIBCALL
4775 1
4776#else
4777 0
4778#endif
4779 },
4780 {"linebreak",
4781#ifdef FEAT_LINEBREAK
4782 1
4783#else
4784 0
4785#endif
4786 },
4787 {"lispindent",
4788#ifdef FEAT_LISP
4789 1
4790#else
4791 0
4792#endif
4793 },
4794 {"listcmds", 1},
4795 {"localmap", 1},
4796 {"lua",
4797#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4798 1
4799#else
4800 0
4801#endif
4802 },
4803 {"menu",
4804#ifdef FEAT_MENU
4805 1
4806#else
4807 0
4808#endif
4809 },
4810 {"mksession",
4811#ifdef FEAT_SESSION
4812 1
4813#else
4814 0
4815#endif
4816 },
4817 {"modify_fname", 1},
4818 {"mouse", 1},
4819 {"mouseshape",
4820#ifdef FEAT_MOUSESHAPE
4821 1
4822#else
4823 0
4824#endif
4825 },
4826 {"mouse_dec",
4827#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4828 1
4829#else
4830 0
4831#endif
4832 },
4833 {"mouse_gpm",
4834#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4835 1
4836#else
4837 0
4838#endif
4839 },
4840 {"mouse_jsbterm",
4841#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4842 1
4843#else
4844 0
4845#endif
4846 },
4847 {"mouse_netterm",
4848#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4849 1
4850#else
4851 0
4852#endif
4853 },
4854 {"mouse_pterm",
4855#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4856 1
4857#else
4858 0
4859#endif
4860 },
4861 {"mouse_sgr",
4862#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4863 1
4864#else
4865 0
4866#endif
4867 },
4868 {"mouse_sysmouse",
4869#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4870 1
4871#else
4872 0
4873#endif
4874 },
4875 {"mouse_urxvt",
4876#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4877 1
4878#else
4879 0
4880#endif
4881 },
4882 {"mouse_xterm",
4883#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4884 1
4885#else
4886 0
4887#endif
4888 },
4889 {"multi_byte", 1},
4890 {"multi_byte_ime",
4891#ifdef FEAT_MBYTE_IME
4892 1
4893#else
4894 0
4895#endif
4896 },
4897 {"multi_lang",
4898#ifdef FEAT_MULTI_LANG
4899 1
4900#else
4901 0
4902#endif
4903 },
4904 {"mzscheme",
4905#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4906 1
4907#else
4908 0
4909#endif
4910 },
4911 {"num64", 1},
4912 {"ole",
4913#ifdef FEAT_OLE
4914 1
4915#else
4916 0
4917#endif
4918 },
4919 {"packages",
4920#ifdef FEAT_EVAL
4921 1
4922#else
4923 0
4924#endif
4925 },
4926 {"path_extra",
4927#ifdef FEAT_PATH_EXTRA
4928 1
4929#else
4930 0
4931#endif
4932 },
4933 {"perl",
4934#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4935 1
4936#else
4937 0
4938#endif
4939 },
4940 {"persistent_undo",
4941#ifdef FEAT_PERSISTENT_UNDO
4942 1
4943#else
4944 0
4945#endif
4946 },
4947 {"python_compiled",
4948#if defined(FEAT_PYTHON)
4949 1
4950#else
4951 0
4952#endif
4953 },
4954 {"python_dynamic",
4955#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4956 1
4957#else
4958 0
4959#endif
4960 },
4961 {"python",
4962#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4963 1
4964#else
4965 0
4966#endif
4967 },
4968 {"pythonx",
4969#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4970 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4971 1
4972#else
4973 0
4974#endif
4975 },
4976 {"python3_compiled",
4977#if defined(FEAT_PYTHON3)
4978 1
4979#else
4980 0
4981#endif
4982 },
4983 {"python3_dynamic",
4984#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4985 1
4986#else
4987 0
4988#endif
4989 },
4990 {"python3",
4991#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4992 1
4993#else
4994 0
4995#endif
4996 },
4997 {"popupwin",
4998#ifdef FEAT_PROP_POPUP
4999 1
5000#else
5001 0
5002#endif
5003 },
5004 {"postscript",
5005#ifdef FEAT_POSTSCRIPT
5006 1
5007#else
5008 0
5009#endif
5010 },
5011 {"printer",
5012#ifdef FEAT_PRINTER
5013 1
5014#else
5015 0
5016#endif
5017 },
5018 {"profile",
5019#ifdef FEAT_PROFILE
5020 1
5021#else
5022 0
5023#endif
5024 },
5025 {"reltime",
5026#ifdef FEAT_RELTIME
5027 1
5028#else
5029 0
5030#endif
5031 },
5032 {"quickfix",
5033#ifdef FEAT_QUICKFIX
5034 1
5035#else
5036 0
5037#endif
5038 },
5039 {"rightleft",
5040#ifdef FEAT_RIGHTLEFT
5041 1
5042#else
5043 0
5044#endif
5045 },
5046 {"ruby",
5047#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5048 1
5049#else
5050 0
5051#endif
5052 },
5053 {"scrollbind", 1},
5054 {"showcmd",
5055#ifdef FEAT_CMDL_INFO
5056 1
5057#else
5058 0
5059#endif
5060 },
5061 {"cmdline_info",
5062#ifdef FEAT_CMDL_INFO
5063 1
5064#else
5065 0
5066#endif
5067 },
5068 {"signs",
5069#ifdef FEAT_SIGNS
5070 1
5071#else
5072 0
5073#endif
5074 },
5075 {"smartindent",
5076#ifdef FEAT_SMARTINDENT
5077 1
5078#else
5079 0
5080#endif
5081 },
5082 {"startuptime",
5083#ifdef STARTUPTIME
5084 1
5085#else
5086 0
5087#endif
5088 },
5089 {"statusline",
5090#ifdef FEAT_STL_OPT
5091 1
5092#else
5093 0
5094#endif
5095 },
5096 {"netbeans_intg",
5097#ifdef FEAT_NETBEANS_INTG
5098 1
5099#else
5100 0
5101#endif
5102 },
Christian Brabandtf573c6e2021-06-20 14:02:16 +02005103 {"sodium",
5104#ifdef FEAT_SODIUM
5105 1
5106#else
5107 0
5108#endif
5109 },
Bram Moolenaar79296512020-03-22 16:17:14 +01005110 {"sound",
5111#ifdef FEAT_SOUND
5112 1
5113#else
5114 0
5115#endif
5116 },
5117 {"spell",
5118#ifdef FEAT_SPELL
5119 1
5120#else
5121 0
5122#endif
5123 },
5124 {"syntax",
5125#ifdef FEAT_SYN_HL
5126 1
5127#else
5128 0
5129#endif
5130 },
5131 {"system",
5132#if defined(USE_SYSTEM) || !defined(UNIX)
5133 1
5134#else
5135 0
5136#endif
5137 },
5138 {"tag_binary",
5139#ifdef FEAT_TAG_BINS
5140 1
5141#else
5142 0
5143#endif
5144 },
5145 {"tcl",
5146#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5147 1
5148#else
5149 0
5150#endif
5151 },
5152 {"termguicolors",
5153#ifdef FEAT_TERMGUICOLORS
5154 1
5155#else
5156 0
5157#endif
5158 },
5159 {"terminal",
5160#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5161 1
5162#else
5163 0
5164#endif
5165 },
5166 {"terminfo",
5167#ifdef TERMINFO
5168 1
5169#else
5170 0
5171#endif
5172 },
5173 {"termresponse",
5174#ifdef FEAT_TERMRESPONSE
5175 1
5176#else
5177 0
5178#endif
5179 },
5180 {"textobjects",
5181#ifdef FEAT_TEXTOBJ
5182 1
5183#else
5184 0
5185#endif
5186 },
5187 {"textprop",
5188#ifdef FEAT_PROP_POPUP
5189 1
5190#else
5191 0
5192#endif
5193 },
5194 {"tgetent",
5195#ifdef HAVE_TGETENT
5196 1
5197#else
5198 0
5199#endif
5200 },
5201 {"timers",
5202#ifdef FEAT_TIMERS
5203 1
5204#else
5205 0
5206#endif
5207 },
5208 {"title",
5209#ifdef FEAT_TITLE
5210 1
5211#else
5212 0
5213#endif
5214 },
5215 {"toolbar",
5216#ifdef FEAT_TOOLBAR
5217 1
5218#else
5219 0
5220#endif
5221 },
5222 {"unnamedplus",
5223#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5224 1
5225#else
5226 0
5227#endif
5228 },
5229 {"user-commands", 1}, // was accidentally included in 5.4
5230 {"user_commands", 1},
5231 {"vartabs",
5232#ifdef FEAT_VARTABS
5233 1
5234#else
5235 0
5236#endif
5237 },
5238 {"vertsplit", 1},
5239 {"viminfo",
5240#ifdef FEAT_VIMINFO
5241 1
5242#else
5243 0
5244#endif
5245 },
5246 {"vimscript-1", 1},
5247 {"vimscript-2", 1},
5248 {"vimscript-3", 1},
5249 {"vimscript-4", 1},
5250 {"virtualedit", 1},
5251 {"visual", 1},
5252 {"visualextra", 1},
5253 {"vreplace", 1},
5254 {"vtp",
5255#ifdef FEAT_VTP
5256 1
5257#else
5258 0
5259#endif
5260 },
5261 {"wildignore",
5262#ifdef FEAT_WILDIGN
5263 1
5264#else
5265 0
5266#endif
5267 },
5268 {"wildmenu",
5269#ifdef FEAT_WILDMENU
5270 1
5271#else
5272 0
5273#endif
5274 },
5275 {"windows", 1},
5276 {"winaltkeys",
5277#ifdef FEAT_WAK
5278 1
5279#else
5280 0
5281#endif
5282 },
5283 {"writebackup",
5284#ifdef FEAT_WRITEBACKUP
5285 1
5286#else
5287 0
5288#endif
5289 },
5290 {"xim",
5291#ifdef FEAT_XIM
5292 1
5293#else
5294 0
5295#endif
5296 },
5297 {"xfontset",
5298#ifdef FEAT_XFONTSET
5299 1
5300#else
5301 0
5302#endif
5303 },
5304 {"xpm",
5305#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5306 1
5307#else
5308 0
5309#endif
5310 },
5311 {"xpm_w32", // for backward compatibility
5312#ifdef FEAT_XPM_W32
5313 1
5314#else
5315 0
5316#endif
5317 },
5318 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005320 1
5321#else
5322 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005324 },
5325 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005326#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005327 1
5328#else
5329 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005331 },
5332 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005334 1
5335#else
5336 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005337#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005338 },
5339 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005340#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005341 1
5342#else
5343 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005345 },
5346 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005348 1
5349#else
5350 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005352 },
5353 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354 };
5355
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005356 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005357 for (i = 0; has_list[i].name != NULL; ++i)
5358 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005360 x = TRUE;
5361 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362 break;
5363 }
5364
Bram Moolenaar79296512020-03-22 16:17:14 +01005365 // features also in has_list[] but sometimes enabled at runtime
5366 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005368 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005369 {
5370 // intentionally empty
5371 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005372#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373 else if (STRICMP(name, "balloon_multiline") == 0)
5374 n = multiline_balloon_available();
5375#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005376#ifdef VIMDLL
5377 else if (STRICMP(name, "filterpipe") == 0)
5378 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005379#endif
5380#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5381 else if (STRICMP(name, "iconv") == 0)
5382 n = iconv_enabled(FALSE);
5383#endif
5384#ifdef DYNAMIC_LUA
5385 else if (STRICMP(name, "lua") == 0)
5386 n = lua_enabled(FALSE);
5387#endif
5388#ifdef DYNAMIC_MZSCHEME
5389 else if (STRICMP(name, "mzscheme") == 0)
5390 n = mzscheme_enabled(FALSE);
5391#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005392#ifdef DYNAMIC_PERL
5393 else if (STRICMP(name, "perl") == 0)
5394 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396#ifdef DYNAMIC_PYTHON
5397 else if (STRICMP(name, "python") == 0)
5398 n = python_enabled(FALSE);
5399#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005400#ifdef DYNAMIC_PYTHON3
5401 else if (STRICMP(name, "python3") == 0)
5402 n = python3_enabled(FALSE);
5403#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005404#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5405 else if (STRICMP(name, "pythonx") == 0)
5406 {
5407# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5408 if (p_pyx == 0)
5409 n = python3_enabled(FALSE) || python_enabled(FALSE);
5410 else if (p_pyx == 3)
5411 n = python3_enabled(FALSE);
5412 else if (p_pyx == 2)
5413 n = python_enabled(FALSE);
5414# elif defined(DYNAMIC_PYTHON)
5415 n = python_enabled(FALSE);
5416# elif defined(DYNAMIC_PYTHON3)
5417 n = python3_enabled(FALSE);
5418# endif
5419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005421#ifdef DYNAMIC_RUBY
5422 else if (STRICMP(name, "ruby") == 0)
5423 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005425#ifdef DYNAMIC_TCL
5426 else if (STRICMP(name, "tcl") == 0)
5427 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005428#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005429#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005430 else if (STRICMP(name, "terminal") == 0)
5431 n = terminal_enabled();
5432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433 }
5434
Bram Moolenaar79296512020-03-22 16:17:14 +01005435 // features not in has_list[]
5436 if (x == FALSE)
5437 {
5438 if (STRNICMP(name, "patch", 5) == 0)
5439 {
5440 x = TRUE;
5441 if (name[5] == '-'
5442 && STRLEN(name) >= 11
5443 && vim_isdigit(name[6])
5444 && vim_isdigit(name[8])
5445 && vim_isdigit(name[10]))
5446 {
5447 int major = atoi((char *)name + 6);
5448 int minor = atoi((char *)name + 8);
5449
5450 // Expect "patch-9.9.01234".
5451 n = (major < VIM_VERSION_MAJOR
5452 || (major == VIM_VERSION_MAJOR
5453 && (minor < VIM_VERSION_MINOR
5454 || (minor == VIM_VERSION_MINOR
5455 && has_patch(atoi((char *)name + 10))))));
5456 }
5457 else
5458 n = has_patch(atoi((char *)name + 5));
5459 }
5460 else if (STRICMP(name, "vim_starting") == 0)
5461 {
5462 x = TRUE;
5463 n = (starting != 0);
5464 }
5465 else if (STRICMP(name, "ttyin") == 0)
5466 {
5467 x = TRUE;
5468 n = mch_input_isatty();
5469 }
5470 else if (STRICMP(name, "ttyout") == 0)
5471 {
5472 x = TRUE;
5473 n = stdout_isatty;
5474 }
5475 else if (STRICMP(name, "multi_byte_encoding") == 0)
5476 {
5477 x = TRUE;
5478 n = has_mbyte;
5479 }
5480 else if (STRICMP(name, "gui_running") == 0)
5481 {
5482 x = TRUE;
5483#ifdef FEAT_GUI
5484 n = (gui.in_use || gui.starting);
5485#endif
5486 }
5487 else if (STRICMP(name, "browse") == 0)
5488 {
5489 x = TRUE;
5490#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5491 n = gui.in_use; // gui_mch_browse() works when GUI is running
5492#endif
5493 }
5494 else if (STRICMP(name, "syntax_items") == 0)
5495 {
5496 x = TRUE;
5497#ifdef FEAT_SYN_HL
5498 n = syntax_present(curwin);
5499#endif
5500 }
5501 else if (STRICMP(name, "vcon") == 0)
5502 {
5503 x = TRUE;
5504#ifdef FEAT_VTP
5505 n = is_term_win32() && has_vtp_working();
5506#endif
5507 }
5508 else if (STRICMP(name, "netbeans_enabled") == 0)
5509 {
5510 x = TRUE;
5511#ifdef FEAT_NETBEANS_INTG
5512 n = netbeans_active();
5513#endif
5514 }
5515 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5516 {
5517 x = TRUE;
5518#ifdef FEAT_MOUSE_GPM
5519 n = gpm_enabled();
5520#endif
5521 }
5522 else if (STRICMP(name, "conpty") == 0)
5523 {
5524 x = TRUE;
5525#if defined(FEAT_TERMINAL) && defined(MSWIN)
5526 n = use_conpty();
5527#endif
5528 }
5529 else if (STRICMP(name, "clipboard_working") == 0)
5530 {
5531 x = TRUE;
5532#ifdef FEAT_CLIPBOARD
5533 n = clip_star.available;
5534#endif
5535 }
5536 }
5537
Bram Moolenaar04637e22020-09-05 18:45:29 +02005538 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005539 // return whether feature could ever be enabled
5540 rettv->vval.v_number = x;
5541 else
5542 // return whether feature is enabled
5543 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544}
5545
5546/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005547 * Return TRUE if "feature" can change later.
5548 * Also when checking for the feature has side effects, such as loading a DLL.
5549 */
5550 int
5551dynamic_feature(char_u *feature)
5552{
5553 return (feature == NULL
5554#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5555 || STRICMP(feature, "balloon_multiline") == 0
5556#endif
5557#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5558 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5559#endif
5560#ifdef VIMDLL
5561 || STRICMP(feature, "filterpipe") == 0
5562#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005563#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005564 // this can only change on Unix where the ":gui" command could be
5565 // used.
5566 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5567#endif
5568#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5569 || STRICMP(feature, "iconv") == 0
5570#endif
5571#ifdef DYNAMIC_LUA
5572 || STRICMP(feature, "lua") == 0
5573#endif
5574#ifdef FEAT_MOUSE_GPM
5575 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5576#endif
5577#ifdef DYNAMIC_MZSCHEME
5578 || STRICMP(feature, "mzscheme") == 0
5579#endif
5580#ifdef FEAT_NETBEANS_INTG
5581 || STRICMP(feature, "netbeans_enabled") == 0
5582#endif
5583#ifdef DYNAMIC_PERL
5584 || STRICMP(feature, "perl") == 0
5585#endif
5586#ifdef DYNAMIC_PYTHON
5587 || STRICMP(feature, "python") == 0
5588#endif
5589#ifdef DYNAMIC_PYTHON3
5590 || STRICMP(feature, "python3") == 0
5591#endif
5592#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5593 || STRICMP(feature, "pythonx") == 0
5594#endif
5595#ifdef DYNAMIC_RUBY
5596 || STRICMP(feature, "ruby") == 0
5597#endif
5598#ifdef FEAT_SYN_HL
5599 || STRICMP(feature, "syntax_items") == 0
5600#endif
5601#ifdef DYNAMIC_TCL
5602 || STRICMP(feature, "tcl") == 0
5603#endif
5604 // once "starting" is zero it will stay that way
5605 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5606 || STRICMP(feature, "multi_byte_encoding") == 0
5607#if defined(FEAT_TERMINAL) && defined(MSWIN)
5608 || STRICMP(feature, "conpty") == 0
5609#endif
5610 );
5611}
5612
5613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 * "haslocaldir()" function
5615 */
5616 static void
5617f_haslocaldir(typval_T *argvars, typval_T *rettv)
5618{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005619 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620 win_T *wp = NULL;
5621
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005622 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5623
5624 // Check for window-local and tab-local directories
5625 if (wp != NULL && wp->w_localdir != NULL)
5626 rettv->vval.v_number = 1;
5627 else if (tp != NULL && tp->tp_localdir != NULL)
5628 rettv->vval.v_number = 2;
5629 else
5630 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631}
5632
5633/*
5634 * "hasmapto()" function
5635 */
5636 static void
5637f_hasmapto(typval_T *argvars, typval_T *rettv)
5638{
5639 char_u *name;
5640 char_u *mode;
5641 char_u buf[NUMBUFLEN];
5642 int abbr = FALSE;
5643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005644 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 if (argvars[1].v_type == VAR_UNKNOWN)
5646 mode = (char_u *)"nvo";
5647 else
5648 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005649 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005651 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 }
5653
5654 if (map_to_exists(name, mode, abbr))
5655 rettv->vval.v_number = TRUE;
5656 else
5657 rettv->vval.v_number = FALSE;
5658}
5659
5660/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005661 * "highlightID(name)" function
5662 */
5663 static void
5664f_hlID(typval_T *argvars, typval_T *rettv)
5665{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005666 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005667}
5668
5669/*
5670 * "highlight_exists()" function
5671 */
5672 static void
5673f_hlexists(typval_T *argvars, typval_T *rettv)
5674{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005675 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676}
5677
5678/*
5679 * "hostname()" function
5680 */
5681 static void
5682f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5683{
5684 char_u hostname[256];
5685
5686 mch_get_host_name(hostname, 256);
5687 rettv->v_type = VAR_STRING;
5688 rettv->vval.v_string = vim_strsave(hostname);
5689}
5690
5691/*
5692 * iconv() function
5693 */
5694 static void
5695f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5696{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005697 char_u buf1[NUMBUFLEN];
5698 char_u buf2[NUMBUFLEN];
5699 char_u *from, *to, *str;
5700 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701
5702 rettv->v_type = VAR_STRING;
5703 rettv->vval.v_string = NULL;
5704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005705 str = tv_get_string(&argvars[0]);
5706 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5707 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005708 vimconv.vc_type = CONV_NONE;
5709 convert_setup(&vimconv, from, to);
5710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005711 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005712 if (vimconv.vc_type == CONV_NONE)
5713 rettv->vval.v_string = vim_strsave(str);
5714 else
5715 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5716
5717 convert_setup(&vimconv, NULL, NULL);
5718 vim_free(from);
5719 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005720}
5721
5722/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 * "index()" function
5724 */
5725 static void
5726f_index(typval_T *argvars, typval_T *rettv)
5727{
5728 list_T *l;
5729 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005730 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005731 long idx = 0;
5732 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005733 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005734
5735 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005736 if (argvars[0].v_type == VAR_BLOB)
5737 {
5738 typval_T tv;
5739 int start = 0;
5740
5741 if (argvars[2].v_type != VAR_UNKNOWN)
5742 {
5743 start = tv_get_number_chk(&argvars[2], &error);
5744 if (error)
5745 return;
5746 }
5747 b = argvars[0].vval.v_blob;
5748 if (b == NULL)
5749 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005750 if (start < 0)
5751 {
5752 start = blob_len(b) + start;
5753 if (start < 0)
5754 start = 0;
5755 }
5756
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005757 for (idx = start; idx < blob_len(b); ++idx)
5758 {
5759 tv.v_type = VAR_NUMBER;
5760 tv.vval.v_number = blob_get(b, idx);
5761 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5762 {
5763 rettv->vval.v_number = idx;
5764 return;
5765 }
5766 }
5767 return;
5768 }
5769 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005771 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005772 return;
5773 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005774
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 l = argvars[0].vval.v_list;
5776 if (l != NULL)
5777 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005778 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005779 item = l->lv_first;
5780 if (argvars[2].v_type != VAR_UNKNOWN)
5781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005782 // Start at specified item. Use the cached index that list_find()
5783 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005784 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005785 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005787 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 if (error)
5789 item = NULL;
5790 }
5791
5792 for ( ; item != NULL; item = item->li_next, ++idx)
5793 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5794 {
5795 rettv->vval.v_number = idx;
5796 break;
5797 }
5798 }
5799}
5800
5801static int inputsecret_flag = 0;
5802
5803/*
5804 * "input()" function
5805 * Also handles inputsecret() when inputsecret is set.
5806 */
5807 static void
5808f_input(typval_T *argvars, typval_T *rettv)
5809{
5810 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5811}
5812
5813/*
5814 * "inputdialog()" function
5815 */
5816 static void
5817f_inputdialog(typval_T *argvars, typval_T *rettv)
5818{
5819#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005820 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5822 {
5823 char_u *message;
5824 char_u buf[NUMBUFLEN];
5825 char_u *defstr = (char_u *)"";
5826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005827 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005829 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5831 else
5832 IObuff[0] = NUL;
5833 if (message != NULL && defstr != NULL
5834 && do_dialog(VIM_QUESTION, NULL, message,
5835 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5836 rettv->vval.v_string = vim_strsave(IObuff);
5837 else
5838 {
5839 if (message != NULL && defstr != NULL
5840 && argvars[1].v_type != VAR_UNKNOWN
5841 && argvars[2].v_type != VAR_UNKNOWN)
5842 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844 else
5845 rettv->vval.v_string = NULL;
5846 }
5847 rettv->v_type = VAR_STRING;
5848 }
5849 else
5850#endif
5851 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5852}
5853
5854/*
5855 * "inputlist()" function
5856 */
5857 static void
5858f_inputlist(typval_T *argvars, typval_T *rettv)
5859{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005860 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005861 listitem_T *li;
5862 int selected;
5863 int mouse_used;
5864
5865#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005866 // While starting up, there is no place to enter text. When running tests
5867 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005868 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005869 return;
5870#endif
5871 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5872 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005873 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005874 return;
5875 }
5876
5877 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005878 msg_row = Rows - 1; // for when 'cmdheight' > 1
5879 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 msg_scroll = TRUE;
5881 msg_clr_eos();
5882
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005883 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005884 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005885 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005886 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005887 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 msg_putchar('\n');
5889 }
5890
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005891 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 selected = prompt_for_number(&mouse_used);
5893 if (mouse_used)
5894 selected -= lines_left;
5895
5896 rettv->vval.v_number = selected;
5897}
5898
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5900
5901/*
5902 * "inputrestore()" function
5903 */
5904 static void
5905f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5906{
5907 if (ga_userinput.ga_len > 0)
5908 {
5909 --ga_userinput.ga_len;
5910 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005911 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005912 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913 }
5914 else if (p_verbose > 1)
5915 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005916 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005917 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005918 }
5919}
5920
5921/*
5922 * "inputsave()" function
5923 */
5924 static void
5925f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5926{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005928 if (ga_grow(&ga_userinput, 1) == OK)
5929 {
5930 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5931 + ga_userinput.ga_len);
5932 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005933 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005934 }
5935 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005937}
5938
5939/*
5940 * "inputsecret()" function
5941 */
5942 static void
5943f_inputsecret(typval_T *argvars, typval_T *rettv)
5944{
5945 ++cmdline_star;
5946 ++inputsecret_flag;
5947 f_input(argvars, rettv);
5948 --cmdline_star;
5949 --inputsecret_flag;
5950}
5951
5952/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005953 * "interrupt()" function
5954 */
5955 static void
5956f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5957{
5958 got_int = TRUE;
5959}
5960
5961/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005962 * "invert(expr)" function
5963 */
5964 static void
5965f_invert(typval_T *argvars, typval_T *rettv)
5966{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005967 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968}
5969
5970/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971 * "islocked()" function
5972 */
5973 static void
5974f_islocked(typval_T *argvars, typval_T *rettv)
5975{
5976 lval_T lv;
5977 char_u *end;
5978 dictitem_T *di;
5979
5980 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005981 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005982 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 if (end != NULL && lv.ll_name != NULL)
5984 {
5985 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005986 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987 else
5988 {
5989 if (lv.ll_tv == NULL)
5990 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005991 di = find_var(lv.ll_name, NULL, TRUE);
5992 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 // Consider a variable locked when:
5995 // 1. the variable itself is locked
5996 // 2. the value of the variable is locked.
5997 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01005998 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
5999 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000 }
6001 }
6002 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006003 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006004 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006005 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6009 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006010 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6012 }
6013 }
6014
6015 clear_lval(&lv);
6016}
6017
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006018/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 * "last_buffer_nr()" function.
6020 */
6021 static void
6022f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6023{
6024 int n = 0;
6025 buf_T *buf;
6026
Bram Moolenaar29323592016-07-24 22:04:11 +02006027 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 if (n < buf->b_fnum)
6029 n = buf->b_fnum;
6030
6031 rettv->vval.v_number = n;
6032}
6033
6034/*
6035 * "len()" function
6036 */
6037 static void
6038f_len(typval_T *argvars, typval_T *rettv)
6039{
6040 switch (argvars[0].v_type)
6041 {
6042 case VAR_STRING:
6043 case VAR_NUMBER:
6044 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006045 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006047 case VAR_BLOB:
6048 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6049 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006050 case VAR_LIST:
6051 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6052 break;
6053 case VAR_DICT:
6054 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6055 break;
6056 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006057 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006059 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060 case VAR_SPECIAL:
6061 case VAR_FLOAT:
6062 case VAR_FUNC:
6063 case VAR_PARTIAL:
6064 case VAR_JOB:
6065 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006066 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006067 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 break;
6069 }
6070}
6071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006073libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074{
6075#ifdef FEAT_LIBCALL
6076 char_u *string_in;
6077 char_u **string_result;
6078 int nr_result;
6079#endif
6080
6081 rettv->v_type = type;
6082 if (type != VAR_NUMBER)
6083 rettv->vval.v_string = NULL;
6084
6085 if (check_restricted() || check_secure())
6086 return;
6087
6088#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006089 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6091 {
6092 string_in = NULL;
6093 if (argvars[2].v_type == VAR_STRING)
6094 string_in = argvars[2].vval.v_string;
6095 if (type == VAR_NUMBER)
6096 string_result = NULL;
6097 else
6098 string_result = &rettv->vval.v_string;
6099 if (mch_libcall(argvars[0].vval.v_string,
6100 argvars[1].vval.v_string,
6101 string_in,
6102 argvars[2].vval.v_number,
6103 string_result,
6104 &nr_result) == OK
6105 && type == VAR_NUMBER)
6106 rettv->vval.v_number = nr_result;
6107 }
6108#endif
6109}
6110
6111/*
6112 * "libcall()" function
6113 */
6114 static void
6115f_libcall(typval_T *argvars, typval_T *rettv)
6116{
6117 libcall_common(argvars, rettv, VAR_STRING);
6118}
6119
6120/*
6121 * "libcallnr()" function
6122 */
6123 static void
6124f_libcallnr(typval_T *argvars, typval_T *rettv)
6125{
6126 libcall_common(argvars, rettv, VAR_NUMBER);
6127}
6128
6129/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006130 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 */
6132 static void
6133f_line(typval_T *argvars, typval_T *rettv)
6134{
6135 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006136 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006138 int id;
6139 tabpage_T *tp;
6140 win_T *wp;
6141 win_T *save_curwin;
6142 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006143
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006144 if (argvars[1].v_type != VAR_UNKNOWN)
6145 {
6146 // use window specified in the second argument
6147 id = (int)tv_get_number(&argvars[1]);
6148 wp = win_id2wp_tp(id, &tp);
6149 if (wp != NULL && tp != NULL)
6150 {
6151 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6152 == OK)
6153 {
6154 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006155 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006156 }
6157 restore_win_noblock(save_curwin, save_curtab, TRUE);
6158 }
6159 }
6160 else
6161 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006162 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006163
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 if (fp != NULL)
6165 lnum = fp->lnum;
6166 rettv->vval.v_number = lnum;
6167}
6168
6169/*
6170 * "line2byte(lnum)" function
6171 */
6172 static void
6173f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6174{
6175#ifndef FEAT_BYTEOFF
6176 rettv->vval.v_number = -1;
6177#else
6178 linenr_T lnum;
6179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006180 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6182 rettv->vval.v_number = -1;
6183 else
6184 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6185 if (rettv->vval.v_number >= 0)
6186 ++rettv->vval.v_number;
6187#endif
6188}
6189
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190#ifdef FEAT_LUA
6191/*
6192 * "luaeval()" function
6193 */
6194 static void
6195f_luaeval(typval_T *argvars, typval_T *rettv)
6196{
6197 char_u *str;
6198 char_u buf[NUMBUFLEN];
6199
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006200 if (check_restricted() || check_secure())
6201 return;
6202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006203 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204 do_luaeval(str, argvars + 1, rettv);
6205}
6206#endif
6207
6208/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006209 * "maparg()" function
6210 */
6211 static void
6212f_maparg(typval_T *argvars, typval_T *rettv)
6213{
6214 get_maparg(argvars, rettv, TRUE);
6215}
6216
6217/*
6218 * "mapcheck()" function
6219 */
6220 static void
6221f_mapcheck(typval_T *argvars, typval_T *rettv)
6222{
6223 get_maparg(argvars, rettv, FALSE);
6224}
6225
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006226typedef enum
6227{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006228 MATCH_END, // matchend()
6229 MATCH_MATCH, // match()
6230 MATCH_STR, // matchstr()
6231 MATCH_LIST, // matchlist()
6232 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006233} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006234
6235 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006236find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237{
6238 char_u *str = NULL;
6239 long len = 0;
6240 char_u *expr = NULL;
6241 char_u *pat;
6242 regmatch_T regmatch;
6243 char_u patbuf[NUMBUFLEN];
6244 char_u strbuf[NUMBUFLEN];
6245 char_u *save_cpo;
6246 long start = 0;
6247 long nth = 1;
6248 colnr_T startcol = 0;
6249 int match = 0;
6250 list_T *l = NULL;
6251 listitem_T *li = NULL;
6252 long idx = 0;
6253 char_u *tofree = NULL;
6254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006255 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006257 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258
6259 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006260 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006262 // type MATCH_LIST: return empty list when there are no matches.
6263 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006264 if (rettv_list_alloc(rettv) == FAIL)
6265 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006266 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267 && (list_append_string(rettv->vval.v_list,
6268 (char_u *)"", 0) == FAIL
6269 || list_append_number(rettv->vval.v_list,
6270 (varnumber_T)-1) == FAIL
6271 || list_append_number(rettv->vval.v_list,
6272 (varnumber_T)-1) == FAIL
6273 || list_append_number(rettv->vval.v_list,
6274 (varnumber_T)-1) == FAIL))
6275 {
6276 list_free(rettv->vval.v_list);
6277 rettv->vval.v_list = NULL;
6278 goto theend;
6279 }
6280 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006281 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282 {
6283 rettv->v_type = VAR_STRING;
6284 rettv->vval.v_string = NULL;
6285 }
6286
6287 if (argvars[0].v_type == VAR_LIST)
6288 {
6289 if ((l = argvars[0].vval.v_list) == NULL)
6290 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006291 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 li = l->lv_first;
6293 }
6294 else
6295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 len = (long)STRLEN(str);
6298 }
6299
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006300 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 if (pat == NULL)
6302 goto theend;
6303
6304 if (argvars[2].v_type != VAR_UNKNOWN)
6305 {
6306 int error = FALSE;
6307
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006308 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309 if (error)
6310 goto theend;
6311 if (l != NULL)
6312 {
6313 li = list_find(l, start);
6314 if (li == NULL)
6315 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006316 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317 }
6318 else
6319 {
6320 if (start < 0)
6321 start = 0;
6322 if (start > len)
6323 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006324 // When "count" argument is there ignore matches before "start",
6325 // otherwise skip part of the string. Differs when pattern is "^"
6326 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327 if (argvars[3].v_type != VAR_UNKNOWN)
6328 startcol = start;
6329 else
6330 {
6331 str += start;
6332 len -= start;
6333 }
6334 }
6335
6336 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006337 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338 if (error)
6339 goto theend;
6340 }
6341
6342 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6343 if (regmatch.regprog != NULL)
6344 {
6345 regmatch.rm_ic = p_ic;
6346
6347 for (;;)
6348 {
6349 if (l != NULL)
6350 {
6351 if (li == NULL)
6352 {
6353 match = FALSE;
6354 break;
6355 }
6356 vim_free(tofree);
6357 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6358 if (str == NULL)
6359 break;
6360 }
6361
6362 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6363
6364 if (match && --nth <= 0)
6365 break;
6366 if (l == NULL && !match)
6367 break;
6368
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006369 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006370 if (l != NULL)
6371 {
6372 li = li->li_next;
6373 ++idx;
6374 }
6375 else
6376 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377 startcol = (colnr_T)(regmatch.startp[0]
6378 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379 if (startcol > (colnr_T)len
6380 || str + startcol <= regmatch.startp[0])
6381 {
6382 match = FALSE;
6383 break;
6384 }
6385 }
6386 }
6387
6388 if (match)
6389 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006390 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006391 {
6392 listitem_T *li1 = rettv->vval.v_list->lv_first;
6393 listitem_T *li2 = li1->li_next;
6394 listitem_T *li3 = li2->li_next;
6395 listitem_T *li4 = li3->li_next;
6396
6397 vim_free(li1->li_tv.vval.v_string);
6398 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006399 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006400 li3->li_tv.vval.v_number =
6401 (varnumber_T)(regmatch.startp[0] - expr);
6402 li4->li_tv.vval.v_number =
6403 (varnumber_T)(regmatch.endp[0] - expr);
6404 if (l != NULL)
6405 li2->li_tv.vval.v_number = (varnumber_T)idx;
6406 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006407 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 {
6409 int i;
6410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006411 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412 for (i = 0; i < NSUBEXP; ++i)
6413 {
6414 if (regmatch.endp[i] == NULL)
6415 {
6416 if (list_append_string(rettv->vval.v_list,
6417 (char_u *)"", 0) == FAIL)
6418 break;
6419 }
6420 else if (list_append_string(rettv->vval.v_list,
6421 regmatch.startp[i],
6422 (int)(regmatch.endp[i] - regmatch.startp[i]))
6423 == FAIL)
6424 break;
6425 }
6426 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006427 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006429 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430 if (l != NULL)
6431 copy_tv(&li->li_tv, rettv);
6432 else
6433 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006434 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 }
6436 else if (l != NULL)
6437 rettv->vval.v_number = idx;
6438 else
6439 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006440 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 rettv->vval.v_number =
6442 (varnumber_T)(regmatch.startp[0] - str);
6443 else
6444 rettv->vval.v_number =
6445 (varnumber_T)(regmatch.endp[0] - str);
6446 rettv->vval.v_number += (varnumber_T)(str - expr);
6447 }
6448 }
6449 vim_regfree(regmatch.regprog);
6450 }
6451
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006452theend:
6453 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006454 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006455 listitem_remove(rettv->vval.v_list,
6456 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457 vim_free(tofree);
6458 p_cpo = save_cpo;
6459}
6460
6461/*
6462 * "match()" function
6463 */
6464 static void
6465f_match(typval_T *argvars, typval_T *rettv)
6466{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006467 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468}
6469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470/*
6471 * "matchend()" function
6472 */
6473 static void
6474f_matchend(typval_T *argvars, typval_T *rettv)
6475{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006476 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477}
6478
6479/*
6480 * "matchlist()" function
6481 */
6482 static void
6483f_matchlist(typval_T *argvars, typval_T *rettv)
6484{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006485 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486}
6487
6488/*
6489 * "matchstr()" function
6490 */
6491 static void
6492f_matchstr(typval_T *argvars, typval_T *rettv)
6493{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006494 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495}
6496
6497/*
6498 * "matchstrpos()" function
6499 */
6500 static void
6501f_matchstrpos(typval_T *argvars, typval_T *rettv)
6502{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006503 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504}
6505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 static void
6507max_min(typval_T *argvars, typval_T *rettv, int domax)
6508{
6509 varnumber_T n = 0;
6510 varnumber_T i;
6511 int error = FALSE;
6512
6513 if (argvars[0].v_type == VAR_LIST)
6514 {
6515 list_T *l;
6516 listitem_T *li;
6517
6518 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006519 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006521 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006523 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6524 n = l->lv_u.nonmat.lv_start;
6525 else
6526 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6527 * l->lv_u.nonmat.lv_stride;
6528 }
6529 else
6530 {
6531 li = l->lv_first;
6532 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006533 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006534 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006535 if (error)
6536 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006537 for (;;)
6538 {
6539 li = li->li_next;
6540 if (li == NULL)
6541 break;
6542 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006543 if (error)
6544 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006545 if (domax ? i > n : i < n)
6546 n = i;
6547 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548 }
6549 }
6550 }
6551 }
6552 else if (argvars[0].v_type == VAR_DICT)
6553 {
6554 dict_T *d;
6555 int first = TRUE;
6556 hashitem_T *hi;
6557 int todo;
6558
6559 d = argvars[0].vval.v_dict;
6560 if (d != NULL)
6561 {
6562 todo = (int)d->dv_hashtab.ht_used;
6563 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6564 {
6565 if (!HASHITEM_EMPTY(hi))
6566 {
6567 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006568 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006569 if (error)
6570 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 if (first)
6572 {
6573 n = i;
6574 first = FALSE;
6575 }
6576 else if (domax ? i > n : i < n)
6577 n = i;
6578 }
6579 }
6580 }
6581 }
6582 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006583 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006584
6585 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586}
6587
6588/*
6589 * "max()" function
6590 */
6591 static void
6592f_max(typval_T *argvars, typval_T *rettv)
6593{
6594 max_min(argvars, rettv, TRUE);
6595}
6596
6597/*
6598 * "min()" function
6599 */
6600 static void
6601f_min(typval_T *argvars, typval_T *rettv)
6602{
6603 max_min(argvars, rettv, FALSE);
6604}
6605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#if defined(FEAT_MZSCHEME) || defined(PROTO)
6607/*
6608 * "mzeval()" function
6609 */
6610 static void
6611f_mzeval(typval_T *argvars, typval_T *rettv)
6612{
6613 char_u *str;
6614 char_u buf[NUMBUFLEN];
6615
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006616 if (check_restricted() || check_secure())
6617 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006618 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619 do_mzeval(str, rettv);
6620}
6621
6622 void
6623mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6624{
6625 typval_T argvars[3];
6626
6627 argvars[0].v_type = VAR_STRING;
6628 argvars[0].vval.v_string = name;
6629 copy_tv(args, &argvars[1]);
6630 argvars[2].v_type = VAR_UNKNOWN;
6631 f_call(argvars, rettv);
6632 clear_tv(&argvars[1]);
6633}
6634#endif
6635
6636/*
6637 * "nextnonblank()" function
6638 */
6639 static void
6640f_nextnonblank(typval_T *argvars, typval_T *rettv)
6641{
6642 linenr_T lnum;
6643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006644 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 {
6646 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6647 {
6648 lnum = 0;
6649 break;
6650 }
6651 if (*skipwhite(ml_get(lnum)) != NUL)
6652 break;
6653 }
6654 rettv->vval.v_number = lnum;
6655}
6656
6657/*
6658 * "nr2char()" function
6659 */
6660 static void
6661f_nr2char(typval_T *argvars, typval_T *rettv)
6662{
6663 char_u buf[NUMBUFLEN];
6664
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 if (has_mbyte)
6666 {
6667 int utf8 = 0;
6668
6669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006670 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006671 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006672 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006674 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 }
6676 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006678 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 buf[1] = NUL;
6680 }
6681 rettv->v_type = VAR_STRING;
6682 rettv->vval.v_string = vim_strsave(buf);
6683}
6684
6685/*
6686 * "or(expr, expr)" function
6687 */
6688 static void
6689f_or(typval_T *argvars, typval_T *rettv)
6690{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006691 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6692 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693}
6694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695#ifdef FEAT_PERL
6696/*
6697 * "perleval()" function
6698 */
6699 static void
6700f_perleval(typval_T *argvars, typval_T *rettv)
6701{
6702 char_u *str;
6703 char_u buf[NUMBUFLEN];
6704
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006705 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706 do_perleval(str, rettv);
6707}
6708#endif
6709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006710/*
6711 * "prevnonblank()" function
6712 */
6713 static void
6714f_prevnonblank(typval_T *argvars, typval_T *rettv)
6715{
6716 linenr_T lnum;
6717
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006718 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006719 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6720 lnum = 0;
6721 else
6722 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6723 --lnum;
6724 rettv->vval.v_number = lnum;
6725}
6726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006727// This dummy va_list is here because:
6728// - passing a NULL pointer doesn't work when va_list isn't a pointer
6729// - locally in the function results in a "used before set" warning
6730// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731static va_list ap;
6732
6733/*
6734 * "printf()" function
6735 */
6736 static void
6737f_printf(typval_T *argvars, typval_T *rettv)
6738{
6739 char_u buf[NUMBUFLEN];
6740 int len;
6741 char_u *s;
6742 int saved_did_emsg = did_emsg;
6743 char *fmt;
6744
6745 rettv->v_type = VAR_STRING;
6746 rettv->vval.v_string = NULL;
6747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006748 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006750 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006751 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752 if (!did_emsg)
6753 {
6754 s = alloc(len + 1);
6755 if (s != NULL)
6756 {
6757 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006758 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6759 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760 }
6761 }
6762 did_emsg |= saved_did_emsg;
6763}
6764
6765/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006766 * "pum_getpos()" function
6767 */
6768 static void
6769f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6770{
6771 if (rettv_dict_alloc(rettv) != OK)
6772 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006773 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006774}
6775
6776/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777 * "pumvisible()" function
6778 */
6779 static void
6780f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6781{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 if (pum_visible())
6783 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784}
6785
6786#ifdef FEAT_PYTHON3
6787/*
6788 * "py3eval()" function
6789 */
6790 static void
6791f_py3eval(typval_T *argvars, typval_T *rettv)
6792{
6793 char_u *str;
6794 char_u buf[NUMBUFLEN];
6795
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006796 if (check_restricted() || check_secure())
6797 return;
6798
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006799 if (p_pyx == 0)
6800 p_pyx = 3;
6801
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006802 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006803 do_py3eval(str, rettv);
6804}
6805#endif
6806
6807#ifdef FEAT_PYTHON
6808/*
6809 * "pyeval()" function
6810 */
6811 static void
6812f_pyeval(typval_T *argvars, typval_T *rettv)
6813{
6814 char_u *str;
6815 char_u buf[NUMBUFLEN];
6816
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006817 if (check_restricted() || check_secure())
6818 return;
6819
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006820 if (p_pyx == 0)
6821 p_pyx = 2;
6822
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006823 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 do_pyeval(str, rettv);
6825}
6826#endif
6827
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006828#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6829/*
6830 * "pyxeval()" function
6831 */
6832 static void
6833f_pyxeval(typval_T *argvars, typval_T *rettv)
6834{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006835 if (check_restricted() || check_secure())
6836 return;
6837
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006838# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6839 init_pyxversion();
6840 if (p_pyx == 2)
6841 f_pyeval(argvars, rettv);
6842 else
6843 f_py3eval(argvars, rettv);
6844# elif defined(FEAT_PYTHON)
6845 f_pyeval(argvars, rettv);
6846# elif defined(FEAT_PYTHON3)
6847 f_py3eval(argvars, rettv);
6848# endif
6849}
6850#endif
6851
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006852static UINT32_T srand_seed_for_testing = 0;
6853static int srand_seed_for_testing_is_used = FALSE;
6854
6855 static void
6856f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6857{
6858 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006859 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006860 else
6861 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006862 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6863 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006864 }
6865}
6866
6867 static void
6868init_srand(UINT32_T *x)
6869{
6870#ifndef MSWIN
6871 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6872#endif
6873
6874 if (srand_seed_for_testing_is_used)
6875 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006876 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006877 return;
6878 }
6879#ifndef MSWIN
6880 if (dev_urandom_state != FAIL)
6881 {
6882 int fd = open("/dev/urandom", O_RDONLY);
6883 struct {
6884 union {
6885 UINT32_T number;
6886 char bytes[sizeof(UINT32_T)];
6887 } contents;
6888 } buf;
6889
6890 // Attempt reading /dev/urandom.
6891 if (fd == -1)
6892 dev_urandom_state = FAIL;
6893 else
6894 {
6895 buf.contents.number = 0;
6896 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6897 != sizeof(UINT32_T))
6898 dev_urandom_state = FAIL;
6899 else
6900 {
6901 dev_urandom_state = OK;
6902 *x = buf.contents.number;
6903 }
6904 close(fd);
6905 }
6906 }
6907 if (dev_urandom_state != OK)
6908 // Reading /dev/urandom doesn't work, fall back to time().
6909#endif
6910 *x = vim_time();
6911}
6912
6913#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6914#define SPLITMIX32(x, z) ( \
6915 z = (x += 0x9e3779b9), \
6916 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6917 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6918 z ^ (z >> 16) \
6919 )
6920#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6921 result = ROTL(y * 5, 7) * 9; \
6922 t = y << 9; \
6923 z ^= x; \
6924 w ^= y; \
6925 y ^= z, x ^= w; \
6926 z ^= t; \
6927 w = ROTL(w, 11);
6928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006930 * "rand()" function
6931 */
6932 static void
6933f_rand(typval_T *argvars, typval_T *rettv)
6934{
6935 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006936 static UINT32_T gx, gy, gz, gw;
6937 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006938 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006939 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006940
6941 if (argvars[0].v_type == VAR_UNKNOWN)
6942 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006943 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006944 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006945 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006946 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006947 init_srand(&x);
6948
6949 gx = SPLITMIX32(x, z);
6950 gy = SPLITMIX32(x, z);
6951 gz = SPLITMIX32(x, z);
6952 gw = SPLITMIX32(x, z);
6953 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006954 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006955
6956 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006957 }
6958 else if (argvars[0].v_type == VAR_LIST)
6959 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006960 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006961 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006962 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006963
6964 lx = list_find(l, 0L);
6965 ly = list_find(l, 1L);
6966 lz = list_find(l, 2L);
6967 lw = list_find(l, 3L);
6968 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6969 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6970 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6971 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6972 x = (UINT32_T)lx->li_tv.vval.v_number;
6973 y = (UINT32_T)ly->li_tv.vval.v_number;
6974 z = (UINT32_T)lz->li_tv.vval.v_number;
6975 w = (UINT32_T)lw->li_tv.vval.v_number;
6976
6977 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6978
6979 lx->li_tv.vval.v_number = (varnumber_T)x;
6980 ly->li_tv.vval.v_number = (varnumber_T)y;
6981 lz->li_tv.vval.v_number = (varnumber_T)z;
6982 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006983 }
6984 else
6985 goto theend;
6986
6987 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006988 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006989 return;
6990
6991theend:
6992 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006993 rettv->v_type = VAR_NUMBER;
6994 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006995}
6996
6997/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006998 * "srand()" function
6999 */
7000 static void
7001f_srand(typval_T *argvars, typval_T *rettv)
7002{
7003 UINT32_T x = 0, z;
7004
7005 if (rettv_list_alloc(rettv) == FAIL)
7006 return;
7007 if (argvars[0].v_type == VAR_UNKNOWN)
7008 {
7009 init_srand(&x);
7010 }
7011 else
7012 {
7013 int error = FALSE;
7014
7015 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7016 if (error)
7017 return;
7018 }
7019
7020 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7021 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7022 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7023 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7024}
7025
7026#undef ROTL
7027#undef SPLITMIX32
7028#undef SHUFFLE_XOSHIRO128STARSTAR
7029
7030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007031 * "range()" function
7032 */
7033 static void
7034f_range(typval_T *argvars, typval_T *rettv)
7035{
7036 varnumber_T start;
7037 varnumber_T end;
7038 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 int error = FALSE;
7040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (argvars[1].v_type == VAR_UNKNOWN)
7043 {
7044 end = start - 1;
7045 start = 0;
7046 }
7047 else
7048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007049 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007051 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 }
7053
7054 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007055 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007057 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007059 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007061 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 list_T *list = rettv->vval.v_list;
7063
7064 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007065 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007066 // be called.
7067 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007068 list->lv_u.nonmat.lv_start = start;
7069 list->lv_u.nonmat.lv_end = end;
7070 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007071 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 }
7073}
7074
7075/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007076 * Materialize "list".
7077 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078 */
7079 void
7080range_list_materialize(list_T *list)
7081{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007082 varnumber_T start = list->lv_u.nonmat.lv_start;
7083 varnumber_T end = list->lv_u.nonmat.lv_end;
7084 int stride = list->lv_u.nonmat.lv_stride;
7085 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007086
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007087 list->lv_first = NULL;
7088 list->lv_u.mat.lv_last = NULL;
7089 list->lv_len = 0;
7090 list->lv_u.mat.lv_idx_item = NULL;
7091 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7092 if (list_append_number(list, (varnumber_T)i) == FAIL)
7093 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094}
7095
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007096/*
7097 * "getreginfo()" function
7098 */
7099 static void
7100f_getreginfo(typval_T *argvars, typval_T *rettv)
7101{
7102 char_u *strregname;
7103 int regname;
7104 char_u buf[NUMBUFLEN + 2];
7105 long reglen = 0;
7106 dict_T *dict;
7107 list_T *list;
7108
7109 if (argvars[0].v_type != VAR_UNKNOWN)
7110 {
7111 strregname = tv_get_string_chk(&argvars[0]);
7112 if (strregname == NULL)
7113 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007114 if (in_vim9script() && STRLEN(strregname) > 1)
7115 {
7116 semsg(_(e_register_name_must_be_one_char_str), strregname);
7117 return;
7118 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007119 }
7120 else
7121 strregname = get_vim_var_str(VV_REG);
7122
7123 regname = (strregname == NULL ? '"' : *strregname);
7124 if (regname == 0 || regname == '@')
7125 regname = '"';
7126
7127 if (rettv_dict_alloc(rettv) == FAIL)
7128 return;
7129 dict = rettv->vval.v_dict;
7130
7131 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7132 if (list == NULL)
7133 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007134 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007135
7136 buf[0] = NUL;
7137 buf[1] = NUL;
7138 switch (get_reg_type(regname, &reglen))
7139 {
7140 case MLINE: buf[0] = 'V'; break;
7141 case MCHAR: buf[0] = 'v'; break;
7142 case MBLOCK:
7143 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7144 reglen + 1);
7145 break;
7146 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007147 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007148
7149 buf[0] = get_register_name(get_unname_register());
7150 buf[1] = NUL;
7151 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007152 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007153 else
7154 {
7155 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7156
7157 if (item != NULL)
7158 {
7159 item->di_tv.v_type = VAR_SPECIAL;
7160 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007161 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007162 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007163 }
7164 }
7165}
7166
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007167 static void
7168return_register(int regname, typval_T *rettv)
7169{
7170 char_u buf[2] = {0, 0};
7171
7172 buf[0] = (char_u)regname;
7173 rettv->v_type = VAR_STRING;
7174 rettv->vval.v_string = vim_strsave(buf);
7175}
7176
7177/*
7178 * "reg_executing()" function
7179 */
7180 static void
7181f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7182{
7183 return_register(reg_executing, rettv);
7184}
7185
7186/*
7187 * "reg_recording()" function
7188 */
7189 static void
7190f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7191{
7192 return_register(reg_recording, rettv);
7193}
7194
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007195/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007196 * "rename({from}, {to})" function
7197 */
7198 static void
7199f_rename(typval_T *argvars, typval_T *rettv)
7200{
7201 char_u buf[NUMBUFLEN];
7202
7203 if (check_restricted() || check_secure())
7204 rettv->vval.v_number = -1;
7205 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7207 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208}
7209
7210/*
7211 * "repeat()" function
7212 */
7213 static void
7214f_repeat(typval_T *argvars, typval_T *rettv)
7215{
7216 char_u *p;
7217 int n;
7218 int slen;
7219 int len;
7220 char_u *r;
7221 int i;
7222
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007223 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007224 if (argvars[0].v_type == VAR_LIST)
7225 {
7226 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7227 while (n-- > 0)
7228 if (list_extend(rettv->vval.v_list,
7229 argvars[0].vval.v_list, NULL) == FAIL)
7230 break;
7231 }
7232 else
7233 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235 rettv->v_type = VAR_STRING;
7236 rettv->vval.v_string = NULL;
7237
7238 slen = (int)STRLEN(p);
7239 len = slen * n;
7240 if (len <= 0)
7241 return;
7242
7243 r = alloc(len + 1);
7244 if (r != NULL)
7245 {
7246 for (i = 0; i < n; i++)
7247 mch_memmove(r + i * slen, p, (size_t)slen);
7248 r[len] = NUL;
7249 }
7250
7251 rettv->vval.v_string = r;
7252 }
7253}
7254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007255#define SP_NOMOVE 0x01 // don't move cursor
7256#define SP_REPEAT 0x02 // repeat to find outer pair
7257#define SP_RETCOUNT 0x04 // return matchcount
7258#define SP_SETPCMARK 0x08 // set previous context mark
7259#define SP_START 0x10 // accept match at start position
7260#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7261#define SP_END 0x40 // leave cursor at end of match
7262#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264/*
7265 * Get flags for a search function.
7266 * Possibly sets "p_ws".
7267 * Returns BACKWARD, FORWARD or zero (for an error).
7268 */
7269 static int
7270get_search_arg(typval_T *varp, int *flagsp)
7271{
7272 int dir = FORWARD;
7273 char_u *flags;
7274 char_u nbuf[NUMBUFLEN];
7275 int mask;
7276
7277 if (varp->v_type != VAR_UNKNOWN)
7278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007279 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007280 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007281 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 while (*flags != NUL)
7283 {
7284 switch (*flags)
7285 {
7286 case 'b': dir = BACKWARD; break;
7287 case 'w': p_ws = TRUE; break;
7288 case 'W': p_ws = FALSE; break;
7289 default: mask = 0;
7290 if (flagsp != NULL)
7291 switch (*flags)
7292 {
7293 case 'c': mask = SP_START; break;
7294 case 'e': mask = SP_END; break;
7295 case 'm': mask = SP_RETCOUNT; break;
7296 case 'n': mask = SP_NOMOVE; break;
7297 case 'p': mask = SP_SUBPAT; break;
7298 case 'r': mask = SP_REPEAT; break;
7299 case 's': mask = SP_SETPCMARK; break;
7300 case 'z': mask = SP_COLUMN; break;
7301 }
7302 if (mask == 0)
7303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007304 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305 dir = 0;
7306 }
7307 else
7308 *flagsp |= mask;
7309 }
7310 if (dir == 0)
7311 break;
7312 ++flags;
7313 }
7314 }
7315 return dir;
7316}
7317
7318/*
7319 * Shared by search() and searchpos() functions.
7320 */
7321 static int
7322search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7323{
7324 int flags;
7325 char_u *pat;
7326 pos_T pos;
7327 pos_T save_cursor;
7328 int save_p_ws = p_ws;
7329 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007330 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007332#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007333 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 long time_limit = 0;
7335#endif
7336 int options = SEARCH_KEEP;
7337 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007338 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007339 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007340 pos_T firstpos;
7341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007343 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 if (dir == 0)
7345 goto theend;
7346 flags = *flagsp;
7347 if (flags & SP_START)
7348 options |= SEARCH_START;
7349 if (flags & SP_END)
7350 options |= SEARCH_END;
7351 if (flags & SP_COLUMN)
7352 options |= SEARCH_COL;
7353
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007354 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007355 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 if (lnum_stop < 0)
7359 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 if (argvars[3].v_type != VAR_UNKNOWN)
7361 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007362#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007363 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 if (time_limit < 0)
7365 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007367 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007368 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 }
7370
7371#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007372 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373 profile_setlimit(time_limit, &tm);
7374#endif
7375
7376 /*
7377 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7378 * Check to make sure only those flags are set.
7379 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7380 * flags cannot be set. Check for that condition also.
7381 */
7382 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7383 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 goto theend;
7387 }
7388
7389 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007390 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007391 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007392 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7393#ifdef FEAT_RELTIME
7394 sia.sa_tm = &tm;
7395#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007396
7397 // Repeat until {skip} returns FALSE.
7398 for (;;)
7399 {
7400 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007401 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007402 // finding the first match again means there is no match where {skip}
7403 // evaluates to zero.
7404 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7405 subpatnum = FAIL;
7406
Bram Moolenaara9c01042020-06-07 14:50:50 +02007407 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007408 // didn't find it or no skip argument
7409 break;
7410 firstpos = pos;
7411
Bram Moolenaara9c01042020-06-07 14:50:50 +02007412 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007413 {
7414 int do_skip;
7415 int err;
7416 pos_T save_pos = curwin->w_cursor;
7417
7418 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007419 err = FALSE;
7420 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007421 curwin->w_cursor = save_pos;
7422 if (err)
7423 {
7424 // Evaluating {skip} caused an error, break here.
7425 subpatnum = FAIL;
7426 break;
7427 }
7428 if (!do_skip)
7429 break;
7430 }
7431 }
7432
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007433 if (subpatnum != FAIL)
7434 {
7435 if (flags & SP_SUBPAT)
7436 retval = subpatnum;
7437 else
7438 retval = pos.lnum;
7439 if (flags & SP_SETPCMARK)
7440 setpcmark();
7441 curwin->w_cursor = pos;
7442 if (match_pos != NULL)
7443 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007444 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445 match_pos->lnum = pos.lnum;
7446 match_pos->col = pos.col + 1;
7447 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007448 // "/$" will put the cursor after the end of the line, may need to
7449 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 check_cursor();
7451 }
7452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007453 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454 if (flags & SP_NOMOVE)
7455 curwin->w_cursor = save_cursor;
7456 else
7457 curwin->w_set_curswant = TRUE;
7458theend:
7459 p_ws = save_p_ws;
7460
7461 return retval;
7462}
7463
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007464#ifdef FEAT_RUBY
7465/*
7466 * "rubyeval()" function
7467 */
7468 static void
7469f_rubyeval(typval_T *argvars, typval_T *rettv)
7470{
7471 char_u *str;
7472 char_u buf[NUMBUFLEN];
7473
7474 str = tv_get_string_buf(&argvars[0], buf);
7475 do_rubyeval(str, rettv);
7476}
7477#endif
7478
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479/*
7480 * "screenattr()" function
7481 */
7482 static void
7483f_screenattr(typval_T *argvars, typval_T *rettv)
7484{
7485 int row;
7486 int col;
7487 int c;
7488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007489 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7490 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 if (row < 0 || row >= screen_Rows
7492 || col < 0 || col >= screen_Columns)
7493 c = -1;
7494 else
7495 c = ScreenAttrs[LineOffset[row] + col];
7496 rettv->vval.v_number = c;
7497}
7498
7499/*
7500 * "screenchar()" function
7501 */
7502 static void
7503f_screenchar(typval_T *argvars, typval_T *rettv)
7504{
7505 int row;
7506 int col;
7507 int off;
7508 int c;
7509
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007510 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7511 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007512 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 c = -1;
7514 else
7515 {
7516 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 if (enc_utf8 && ScreenLinesUC[off] != 0)
7518 c = ScreenLinesUC[off];
7519 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 c = ScreenLines[off];
7521 }
7522 rettv->vval.v_number = c;
7523}
7524
7525/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007526 * "screenchars()" function
7527 */
7528 static void
7529f_screenchars(typval_T *argvars, typval_T *rettv)
7530{
7531 int row;
7532 int col;
7533 int off;
7534 int c;
7535 int i;
7536
7537 if (rettv_list_alloc(rettv) == FAIL)
7538 return;
7539 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7540 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7541 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7542 return;
7543
7544 off = LineOffset[row] + col;
7545 if (enc_utf8 && ScreenLinesUC[off] != 0)
7546 c = ScreenLinesUC[off];
7547 else
7548 c = ScreenLines[off];
7549 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7550
7551 if (enc_utf8)
7552
7553 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7554 list_append_number(rettv->vval.v_list,
7555 (varnumber_T)ScreenLinesC[i][off]);
7556}
7557
7558/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007559 * "screencol()" function
7560 *
7561 * First column is 1 to be consistent with virtcol().
7562 */
7563 static void
7564f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7565{
7566 rettv->vval.v_number = screen_screencol() + 1;
7567}
7568
7569/*
7570 * "screenrow()" function
7571 */
7572 static void
7573f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7574{
7575 rettv->vval.v_number = screen_screenrow() + 1;
7576}
7577
7578/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007579 * "screenstring()" function
7580 */
7581 static void
7582f_screenstring(typval_T *argvars, typval_T *rettv)
7583{
7584 int row;
7585 int col;
7586 int off;
7587 int c;
7588 int i;
7589 char_u buf[MB_MAXBYTES + 1];
7590 int buflen = 0;
7591
7592 rettv->vval.v_string = NULL;
7593 rettv->v_type = VAR_STRING;
7594
7595 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7596 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7597 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7598 return;
7599
7600 off = LineOffset[row] + col;
7601 if (enc_utf8 && ScreenLinesUC[off] != 0)
7602 c = ScreenLinesUC[off];
7603 else
7604 c = ScreenLines[off];
7605 buflen += mb_char2bytes(c, buf);
7606
Bram Moolenaarf1387282021-03-22 17:11:15 +01007607 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007608 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7609 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7610
7611 buf[buflen] = NUL;
7612 rettv->vval.v_string = vim_strsave(buf);
7613}
7614
7615/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 * "search()" function
7617 */
7618 static void
7619f_search(typval_T *argvars, typval_T *rettv)
7620{
7621 int flags = 0;
7622
7623 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7624}
7625
7626/*
7627 * "searchdecl()" function
7628 */
7629 static void
7630f_searchdecl(typval_T *argvars, typval_T *rettv)
7631{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007632 int locally = TRUE;
7633 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 int error = FALSE;
7635 char_u *name;
7636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007637 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 if (argvars[1].v_type != VAR_UNKNOWN)
7641 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007642 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007644 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 }
7646 if (!error && name != NULL)
7647 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7648 locally, thisblock, SEARCH_KEEP) == FAIL;
7649}
7650
7651/*
7652 * Used by searchpair() and searchpairpos()
7653 */
7654 static int
7655searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7656{
7657 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007658 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659 int save_p_ws = p_ws;
7660 int dir;
7661 int flags = 0;
7662 char_u nbuf1[NUMBUFLEN];
7663 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007664 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007665 long lnum_stop = 0;
7666 long time_limit = 0;
7667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007668 // Get the three pattern arguments: start, middle, end. Will result in an
7669 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007670 spat = tv_get_string_chk(&argvars[0]);
7671 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7672 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007674 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007676 // Handle the optional fourth argument: flags
7677 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 if (dir == 0)
7679 goto theend;
7680
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007681 // Don't accept SP_END or SP_SUBPAT.
7682 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if ((flags & (SP_END | SP_SUBPAT)) != 0
7684 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007686 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 goto theend;
7688 }
7689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007690 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691 if (flags & SP_REPEAT)
7692 p_ws = FALSE;
7693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007694 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 if (argvars[3].v_type == VAR_UNKNOWN
7696 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007697 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007698 else
7699 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007700 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007701 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 if (argvars[5].v_type != VAR_UNKNOWN)
7704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007705 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007708 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007710 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711#ifdef FEAT_RELTIME
7712 if (argvars[6].v_type != VAR_UNKNOWN)
7713 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007714 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007717 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720 }
7721#endif
7722 }
7723 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724
7725 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7726 match_pos, lnum_stop, time_limit);
7727
7728theend:
7729 p_ws = save_p_ws;
7730
7731 return retval;
7732}
7733
7734/*
7735 * "searchpair()" function
7736 */
7737 static void
7738f_searchpair(typval_T *argvars, typval_T *rettv)
7739{
7740 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7741}
7742
7743/*
7744 * "searchpairpos()" function
7745 */
7746 static void
7747f_searchpairpos(typval_T *argvars, typval_T *rettv)
7748{
7749 pos_T match_pos;
7750 int lnum = 0;
7751 int col = 0;
7752
7753 if (rettv_list_alloc(rettv) == FAIL)
7754 return;
7755
7756 if (searchpair_cmn(argvars, &match_pos) > 0)
7757 {
7758 lnum = match_pos.lnum;
7759 col = match_pos.col;
7760 }
7761
7762 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7763 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7764}
7765
7766/*
7767 * Search for a start/middle/end thing.
7768 * Used by searchpair(), see its documentation for the details.
7769 * Returns 0 or -1 for no match,
7770 */
7771 long
7772do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007773 char_u *spat, // start pattern
7774 char_u *mpat, // middle pattern
7775 char_u *epat, // end pattern
7776 int dir, // BACKWARD or FORWARD
7777 typval_T *skip, // skip expression
7778 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007780 linenr_T lnum_stop, // stop at this line if not zero
7781 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782{
7783 char_u *save_cpo;
7784 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7785 long retval = 0;
7786 pos_T pos;
7787 pos_T firstpos;
7788 pos_T foundpos;
7789 pos_T save_cursor;
7790 pos_T save_pos;
7791 int n;
7792 int r;
7793 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007794 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007795 int err;
7796 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007797#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007799#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007801 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 save_cpo = p_cpo;
7803 p_cpo = empty_option;
7804
7805#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007806 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 profile_setlimit(time_limit, &tm);
7808#endif
7809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007810 // Make two search patterns: start/end (pat2, for in nested pairs) and
7811 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007812 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7813 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 if (pat2 == NULL || pat3 == NULL)
7815 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007816 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 if (*mpat == NUL)
7818 STRCPY(pat3, pat2);
7819 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007820 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 spat, epat, mpat);
7822 if (flags & SP_START)
7823 options |= SEARCH_START;
7824
Bram Moolenaar48570482017-10-30 21:48:41 +01007825 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007826 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 save_cursor = curwin->w_cursor;
7829 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007830 CLEAR_POS(&firstpos);
7831 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 pat = pat3;
7833 for (;;)
7834 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007835 searchit_arg_T sia;
7836
Bram Moolenaara80faa82020-04-12 19:37:17 +02007837 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007838 sia.sa_stop_lnum = lnum_stop;
7839#ifdef FEAT_RELTIME
7840 sia.sa_tm = &tm;
7841#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007842 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007843 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007844 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007845 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846 break;
7847
7848 if (firstpos.lnum == 0)
7849 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007850 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007852 // Found the same position again. Can happen with a pattern that
7853 // has "\zs" at the end and searching backwards. Advance one
7854 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855 if (dir == BACKWARD)
7856 decl(&pos);
7857 else
7858 incl(&pos);
7859 }
7860 foundpos = pos;
7861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007862 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 options &= ~SEARCH_START;
7864
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007865 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007866 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007867 {
7868 save_pos = curwin->w_cursor;
7869 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007870 err = FALSE;
7871 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872 curwin->w_cursor = save_pos;
7873 if (err)
7874 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007875 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 curwin->w_cursor = save_cursor;
7877 retval = -1;
7878 break;
7879 }
7880 if (r)
7881 continue;
7882 }
7883
7884 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007886 // Found end when searching backwards or start when searching
7887 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007889 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 }
7891 else
7892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007893 // Found end when searching forward or start when searching
7894 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007896 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 }
7898
7899 if (nest == 0)
7900 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007901 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 if (flags & SP_RETCOUNT)
7903 ++retval;
7904 else
7905 retval = pos.lnum;
7906 if (flags & SP_SETPCMARK)
7907 setpcmark();
7908 curwin->w_cursor = pos;
7909 if (!(flags & SP_REPEAT))
7910 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007911 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 }
7913 }
7914
7915 if (match_pos != NULL)
7916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007917 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918 match_pos->lnum = curwin->w_cursor.lnum;
7919 match_pos->col = curwin->w_cursor.col + 1;
7920 }
7921
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007922 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007923 if ((flags & SP_NOMOVE) || retval == 0)
7924 curwin->w_cursor = save_cursor;
7925
7926theend:
7927 vim_free(pat2);
7928 vim_free(pat3);
7929 if (p_cpo == empty_option)
7930 p_cpo = save_cpo;
7931 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007932 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007933 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007934 // If it's still empty it was changed and restored, need to restore in
7935 // the complicated way.
7936 if (*p_cpo == NUL)
7937 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007938 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940
7941 return retval;
7942}
7943
7944/*
7945 * "searchpos()" function
7946 */
7947 static void
7948f_searchpos(typval_T *argvars, typval_T *rettv)
7949{
7950 pos_T match_pos;
7951 int lnum = 0;
7952 int col = 0;
7953 int n;
7954 int flags = 0;
7955
7956 if (rettv_list_alloc(rettv) == FAIL)
7957 return;
7958
7959 n = search_cmn(argvars, &match_pos, &flags);
7960 if (n > 0)
7961 {
7962 lnum = match_pos.lnum;
7963 col = match_pos.col;
7964 }
7965
7966 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7967 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7968 if (flags & SP_SUBPAT)
7969 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7970}
7971
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007972/*
7973 * Set the cursor or mark position.
7974 * If 'charpos' is TRUE, then use the column number as a character offet.
7975 * Otherwise use the column number as a byte offset.
7976 */
7977 static void
7978set_position(typval_T *argvars, typval_T *rettv, int charpos)
7979{
7980 pos_T pos;
7981 int fnum;
7982 char_u *name;
7983 colnr_T curswant = -1;
7984
7985 rettv->vval.v_number = -1;
7986
7987 name = tv_get_string_chk(argvars);
7988 if (name != NULL)
7989 {
7990 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7991 {
7992 if (pos.col != MAXCOL && --pos.col < 0)
7993 pos.col = 0;
7994 if ((name[0] == '.' && name[1] == NUL))
7995 {
7996 // set cursor; "fnum" is ignored
7997 curwin->w_cursor = pos;
7998 if (curswant >= 0)
7999 {
8000 curwin->w_curswant = curswant - 1;
8001 curwin->w_set_curswant = FALSE;
8002 }
8003 check_cursor();
8004 rettv->vval.v_number = 0;
8005 }
8006 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8007 {
8008 // set mark
8009 if (setmark_pos(name[1], &pos, fnum) == OK)
8010 rettv->vval.v_number = 0;
8011 }
8012 else
8013 emsg(_(e_invarg));
8014 }
8015 }
8016}
8017/*
8018 * "setcharpos()" function
8019 */
8020 static void
8021f_setcharpos(typval_T *argvars, typval_T *rettv)
8022{
8023 set_position(argvars, rettv, TRUE);
8024}
8025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8028{
8029 dict_T *d;
8030 dictitem_T *di;
8031 char_u *csearch;
8032
8033 if (argvars[0].v_type != VAR_DICT)
8034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008035 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 return;
8037 }
8038
8039 if ((d = argvars[0].vval.v_dict) != NULL)
8040 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008041 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008042 if (csearch != NULL)
8043 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 if (enc_utf8)
8045 {
8046 int pcc[MAX_MCO];
8047 int c = utfc_ptr2char(csearch, pcc);
8048
8049 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8050 }
8051 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008052 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008053 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 }
8055
8056 di = dict_find(d, (char_u *)"forward", -1);
8057 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008058 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059 ? FORWARD : BACKWARD);
8060
8061 di = dict_find(d, (char_u *)"until", -1);
8062 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008063 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 }
8065}
8066
8067/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008068 * "setcursorcharpos" function
8069 */
8070 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008071f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008072{
8073 set_cursorpos(argvars, rettv, TRUE);
8074}
8075
8076/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008077 * "setenv()" function
8078 */
8079 static void
8080f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8081{
8082 char_u namebuf[NUMBUFLEN];
8083 char_u valbuf[NUMBUFLEN];
8084 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8085
8086 if (argvars[1].v_type == VAR_SPECIAL
8087 && argvars[1].vval.v_number == VVAL_NULL)
8088 vim_unsetenv(name);
8089 else
8090 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8091}
8092
8093/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 * "setfperm({fname}, {mode})" function
8095 */
8096 static void
8097f_setfperm(typval_T *argvars, typval_T *rettv)
8098{
8099 char_u *fname;
8100 char_u modebuf[NUMBUFLEN];
8101 char_u *mode_str;
8102 int i;
8103 int mask;
8104 int mode = 0;
8105
8106 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008107 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008108 if (fname == NULL)
8109 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008110 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 if (mode_str == NULL)
8112 return;
8113 if (STRLEN(mode_str) != 9)
8114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008115 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 return;
8117 }
8118
8119 mask = 1;
8120 for (i = 8; i >= 0; --i)
8121 {
8122 if (mode_str[i] != '-')
8123 mode |= mask;
8124 mask = mask << 1;
8125 }
8126 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8127}
8128
8129/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 * "setpos()" function
8131 */
8132 static void
8133f_setpos(typval_T *argvars, typval_T *rettv)
8134{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008135 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136}
8137
8138/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008139 * Translate a register type string to the yank type and block length
8140 */
8141 static int
8142get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8143{
8144 char_u *stropt = *pp;
8145 switch (*stropt)
8146 {
8147 case 'v': case 'c': // character-wise selection
8148 *yank_type = MCHAR;
8149 break;
8150 case 'V': case 'l': // line-wise selection
8151 *yank_type = MLINE;
8152 break;
8153 case 'b': case Ctrl_V: // block-wise selection
8154 *yank_type = MBLOCK;
8155 if (VIM_ISDIGIT(stropt[1]))
8156 {
8157 ++stropt;
8158 *block_len = getdigits(&stropt) - 1;
8159 --stropt;
8160 }
8161 break;
8162 default:
8163 return FAIL;
8164 }
8165 *pp = stropt;
8166 return OK;
8167}
8168
8169/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 * "setreg()" function
8171 */
8172 static void
8173f_setreg(typval_T *argvars, typval_T *rettv)
8174{
8175 int regname;
8176 char_u *strregname;
8177 char_u *stropt;
8178 char_u *strval;
8179 int append;
8180 char_u yank_type;
8181 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008182 typval_T *regcontents;
8183 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008185 pointreg = 0;
8186 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008187 block_len = -1;
8188 yank_type = MAUTO;
8189 append = FALSE;
8190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008191 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008192 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193
8194 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008195 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008196 if (in_vim9script() && STRLEN(strregname) > 1)
8197 {
8198 semsg(_(e_register_name_must_be_one_char_str), strregname);
8199 return;
8200 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 regname = *strregname;
8202 if (regname == 0 || regname == '@')
8203 regname = '"';
8204
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008205 if (argvars[1].v_type == VAR_DICT)
8206 {
8207 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008208 dictitem_T *di;
8209
8210 if (d == NULL || d->dv_hashtab.ht_used == 0)
8211 {
8212 // Empty dict, clear the register (like setreg(0, []))
8213 char_u *lstval[2] = {NULL, NULL};
8214 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8215 return;
8216 }
8217
8218 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008219 if (di != NULL)
8220 regcontents = &di->di_tv;
8221
8222 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8223 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008224 {
8225 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8226
8227 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008228 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008229 semsg(_(e_invargval), "value");
8230 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008231 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008232 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008233
8234 if (regname == '"')
8235 {
8236 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8237 if (stropt != NULL)
8238 {
8239 pointreg = *stropt;
8240 regname = pointreg;
8241 }
8242 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008243 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008244 pointreg = regname;
8245 }
8246 else
8247 regcontents = &argvars[1];
8248
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008249 if (argvars[2].v_type != VAR_UNKNOWN)
8250 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008251 if (yank_type != MAUTO)
8252 {
8253 semsg(_(e_toomanyarg), "setreg");
8254 return;
8255 }
8256
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008257 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008259 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 for (; *stropt != NUL; ++stropt)
8261 switch (*stropt)
8262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008263 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008264 append = TRUE;
8265 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008266 default:
8267 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 }
8269 }
8270
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008271 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 {
8273 char_u **lstval;
8274 char_u **allocval;
8275 char_u buf[NUMBUFLEN];
8276 char_u **curval;
8277 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008278 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 listitem_T *li;
8280 int len;
8281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008282 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 len = ll == NULL ? 0 : ll->lv_len;
8284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008285 // First half: use for pointers to result lines; second half: use for
8286 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008287 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008288 if (lstval == NULL)
8289 return;
8290 curval = lstval;
8291 allocval = lstval + len + 2;
8292 curallocval = allocval;
8293
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008294 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008296 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008297 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008299 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 if (strval == NULL)
8301 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008302 if (strval == buf)
8303 {
8304 // Need to make a copy, next tv_get_string_buf_chk() will
8305 // overwrite the string.
8306 strval = vim_strsave(buf);
8307 if (strval == NULL)
8308 goto free_lstval;
8309 *curallocval++ = strval;
8310 }
8311 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008313 }
8314 *curval++ = NULL;
8315
8316 write_reg_contents_lst(regname, lstval, -1,
8317 append, yank_type, block_len);
8318free_lstval:
8319 while (curallocval > allocval)
8320 vim_free(*--curallocval);
8321 vim_free(lstval);
8322 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008323 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008325 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (strval == NULL)
8327 return;
8328 write_reg_contents_ex(regname, strval, -1,
8329 append, yank_type, block_len);
8330 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008331 if (pointreg != 0)
8332 get_yank_register(pointreg, TRUE);
8333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 rettv->vval.v_number = 0;
8335}
8336
8337/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008338 * "settagstack()" function
8339 */
8340 static void
8341f_settagstack(typval_T *argvars, typval_T *rettv)
8342{
8343 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8344 win_T *wp;
8345 dict_T *d;
8346 int action = 'r';
8347
8348 rettv->vval.v_number = -1;
8349
8350 // first argument: window number or id
8351 wp = find_win_by_nr_or_id(&argvars[0]);
8352 if (wp == NULL)
8353 return;
8354
8355 // second argument: dict with items to set in the tag stack
8356 if (argvars[1].v_type != VAR_DICT)
8357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008358 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008359 return;
8360 }
8361 d = argvars[1].vval.v_dict;
8362 if (d == NULL)
8363 return;
8364
8365 // third argument: action - 'a' for append and 'r' for replace.
8366 // default is to replace the stack.
8367 if (argvars[2].v_type == VAR_UNKNOWN)
8368 action = 'r';
8369 else if (argvars[2].v_type == VAR_STRING)
8370 {
8371 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008372 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008373 if (actstr == NULL)
8374 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008375 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8376 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008377 action = *actstr;
8378 else
8379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008380 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008381 return;
8382 }
8383 }
8384 else
8385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008387 return;
8388 }
8389
8390 if (set_tagstack(wp, d, action) == OK)
8391 rettv->vval.v_number = 0;
8392}
8393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394#ifdef FEAT_CRYPT
8395/*
8396 * "sha256({string})" function
8397 */
8398 static void
8399f_sha256(typval_T *argvars, typval_T *rettv)
8400{
8401 char_u *p;
8402
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 rettv->vval.v_string = vim_strsave(
8405 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8406 rettv->v_type = VAR_STRING;
8407}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008408#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409
8410/*
8411 * "shellescape({string})" function
8412 */
8413 static void
8414f_shellescape(typval_T *argvars, typval_T *rettv)
8415{
Bram Moolenaar20615522017-06-05 18:46:26 +02008416 int do_special = non_zero_arg(&argvars[1]);
8417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008419 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 rettv->v_type = VAR_STRING;
8421}
8422
8423/*
8424 * shiftwidth() function
8425 */
8426 static void
8427f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8428{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008429 rettv->vval.v_number = 0;
8430
8431 if (argvars[0].v_type != VAR_UNKNOWN)
8432 {
8433 long col;
8434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008435 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008436 if (col < 0)
8437 return; // type error; errmsg already given
8438#ifdef FEAT_VARTABS
8439 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8440 return;
8441#endif
8442 }
8443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008444 rettv->vval.v_number = get_sw_value(curbuf);
8445}
8446
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447/*
8448 * "soundfold({word})" function
8449 */
8450 static void
8451f_soundfold(typval_T *argvars, typval_T *rettv)
8452{
8453 char_u *s;
8454
8455 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008456 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008457#ifdef FEAT_SPELL
8458 rettv->vval.v_string = eval_soundfold(s);
8459#else
8460 rettv->vval.v_string = vim_strsave(s);
8461#endif
8462}
8463
8464/*
8465 * "spellbadword()" function
8466 */
8467 static void
8468f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8469{
8470 char_u *word = (char_u *)"";
8471 hlf_T attr = HLF_COUNT;
8472 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008473#ifdef FEAT_SPELL
8474 int wo_spell_save = curwin->w_p_spell;
8475
8476 if (!curwin->w_p_spell)
8477 {
8478 did_set_spelllang(curwin);
8479 curwin->w_p_spell = TRUE;
8480 }
8481
8482 if (*curwin->w_s->b_p_spl == NUL)
8483 {
8484 emsg(_(e_no_spell));
8485 curwin->w_p_spell = wo_spell_save;
8486 return;
8487 }
8488#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489
8490 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008491 {
8492#ifdef FEAT_SPELL
8493 curwin->w_p_spell = wo_spell_save;
8494#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008495 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008496 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497
8498#ifdef FEAT_SPELL
8499 if (argvars[0].v_type == VAR_UNKNOWN)
8500 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008501 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8503 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008504 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008506 curwin->w_set_curswant = TRUE;
8507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008508 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008509 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008511 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 int capcol = -1;
8513
8514 if (str != NULL)
8515 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008516 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 while (*str != NUL)
8518 {
8519 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8520 if (attr != HLF_COUNT)
8521 {
8522 word = str;
8523 break;
8524 }
8525 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008526 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008527 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 }
8529 }
8530 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008531 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532#endif
8533
8534 list_append_string(rettv->vval.v_list, word, len);
8535 list_append_string(rettv->vval.v_list, (char_u *)(
8536 attr == HLF_SPB ? "bad" :
8537 attr == HLF_SPR ? "rare" :
8538 attr == HLF_SPL ? "local" :
8539 attr == HLF_SPC ? "caps" :
8540 ""), -1);
8541}
8542
8543/*
8544 * "spellsuggest()" function
8545 */
8546 static void
8547f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8548{
8549#ifdef FEAT_SPELL
8550 char_u *str;
8551 int typeerr = FALSE;
8552 int maxcount;
8553 garray_T ga;
8554 int i;
8555 listitem_T *li;
8556 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008557 int wo_spell_save = curwin->w_p_spell;
8558
8559 if (!curwin->w_p_spell)
8560 {
8561 did_set_spelllang(curwin);
8562 curwin->w_p_spell = TRUE;
8563 }
8564
8565 if (*curwin->w_s->b_p_spl == NUL)
8566 {
8567 emsg(_(e_no_spell));
8568 curwin->w_p_spell = wo_spell_save;
8569 return;
8570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571#endif
8572
8573 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008574 {
8575#ifdef FEAT_SPELL
8576 curwin->w_p_spell = wo_spell_save;
8577#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580
8581#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008582 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008584 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 if (argvars[1].v_type != VAR_UNKNOWN)
8586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008587 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (maxcount <= 0)
8589 return;
8590 if (argvars[2].v_type != VAR_UNKNOWN)
8591 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008592 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 if (typeerr)
8594 return;
8595 }
8596 }
8597 else
8598 maxcount = 25;
8599
8600 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8601
8602 for (i = 0; i < ga.ga_len; ++i)
8603 {
8604 str = ((char_u **)ga.ga_data)[i];
8605
8606 li = listitem_alloc();
8607 if (li == NULL)
8608 vim_free(str);
8609 else
8610 {
8611 li->li_tv.v_type = VAR_STRING;
8612 li->li_tv.v_lock = 0;
8613 li->li_tv.vval.v_string = str;
8614 list_append(rettv->vval.v_list, li);
8615 }
8616 }
8617 ga_clear(&ga);
8618 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008619 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620#endif
8621}
8622
8623 static void
8624f_split(typval_T *argvars, typval_T *rettv)
8625{
8626 char_u *str;
8627 char_u *end;
8628 char_u *pat = NULL;
8629 regmatch_T regmatch;
8630 char_u patbuf[NUMBUFLEN];
8631 char_u *save_cpo;
8632 int match;
8633 colnr_T col = 0;
8634 int keepempty = FALSE;
8635 int typeerr = FALSE;
8636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008637 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008639 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 if (argvars[1].v_type != VAR_UNKNOWN)
8643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008644 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 if (pat == NULL)
8646 typeerr = TRUE;
8647 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008648 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 }
8650 if (pat == NULL || *pat == NUL)
8651 pat = (char_u *)"[\\x01- ]\\+";
8652
8653 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008654 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008656 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657
8658 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8659 if (regmatch.regprog != NULL)
8660 {
8661 regmatch.rm_ic = FALSE;
8662 while (*str != NUL || keepempty)
8663 {
8664 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008665 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 else
8667 match = vim_regexec_nl(&regmatch, str, col);
8668 if (match)
8669 end = regmatch.startp[0];
8670 else
8671 end = str + STRLEN(str);
8672 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8673 && *str != NUL && match && end < regmatch.endp[0]))
8674 {
8675 if (list_append_string(rettv->vval.v_list, str,
8676 (int)(end - str)) == FAIL)
8677 break;
8678 }
8679 if (!match)
8680 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008681 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 if (regmatch.endp[0] > str)
8683 col = 0;
8684 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008685 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 str = regmatch.endp[0];
8688 }
8689
8690 vim_regfree(regmatch.regprog);
8691 }
8692
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008693theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 p_cpo = save_cpo;
8695}
8696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008698 * "str2list()" function
8699 */
8700 static void
8701f_str2list(typval_T *argvars, typval_T *rettv)
8702{
8703 char_u *p;
8704 int utf8 = FALSE;
8705
8706 if (rettv_list_alloc(rettv) == FAIL)
8707 return;
8708
8709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008710 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008711
8712 p = tv_get_string(&argvars[0]);
8713
8714 if (has_mbyte || utf8)
8715 {
8716 int (*ptr2len)(char_u *);
8717 int (*ptr2char)(char_u *);
8718
8719 if (utf8 || enc_utf8)
8720 {
8721 ptr2len = utf_ptr2len;
8722 ptr2char = utf_ptr2char;
8723 }
8724 else
8725 {
8726 ptr2len = mb_ptr2len;
8727 ptr2char = mb_ptr2char;
8728 }
8729
8730 for ( ; *p != NUL; p += (*ptr2len)(p))
8731 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8732 }
8733 else
8734 for ( ; *p != NUL; ++p)
8735 list_append_number(rettv->vval.v_list, *p);
8736}
8737
8738/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008739 * "str2nr()" function
8740 */
8741 static void
8742f_str2nr(typval_T *argvars, typval_T *rettv)
8743{
8744 int base = 10;
8745 char_u *p;
8746 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008747 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008748 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749
8750 if (argvars[1].v_type != VAR_UNKNOWN)
8751 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008752 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008753 if (base != 2 && base != 8 && base != 10 && base != 16)
8754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008755 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 return;
8757 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008758 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008759 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 }
8761
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008762 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008763 isneg = (*p == '-');
8764 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 p = skipwhite(p + 1);
8766 switch (base)
8767 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008768 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008769 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008770 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008772 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8773 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008774 if (isneg)
8775 rettv->vval.v_number = -n;
8776 else
8777 rettv->vval.v_number = n;
8778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779}
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781/*
8782 * "strgetchar()" function
8783 */
8784 static void
8785f_strgetchar(typval_T *argvars, typval_T *rettv)
8786{
8787 char_u *str;
8788 int len;
8789 int error = FALSE;
8790 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008791 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792
8793 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008794 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 if (str == NULL)
8796 return;
8797 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008798 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 if (error)
8800 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801
Bram Moolenaar13505972019-01-24 15:04:48 +01008802 while (charidx >= 0 && byteidx < len)
8803 {
8804 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008806 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8807 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008808 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008809 --charidx;
8810 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812}
8813
8814/*
8815 * "stridx()" function
8816 */
8817 static void
8818f_stridx(typval_T *argvars, typval_T *rettv)
8819{
8820 char_u buf[NUMBUFLEN];
8821 char_u *needle;
8822 char_u *haystack;
8823 char_u *save_haystack;
8824 char_u *pos;
8825 int start_idx;
8826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008827 needle = tv_get_string_chk(&argvars[1]);
8828 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 rettv->vval.v_number = -1;
8830 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008831 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832
8833 if (argvars[2].v_type != VAR_UNKNOWN)
8834 {
8835 int error = FALSE;
8836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 if (error || start_idx >= (int)STRLEN(haystack))
8839 return;
8840 if (start_idx >= 0)
8841 haystack += start_idx;
8842 }
8843
8844 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8845 if (pos != NULL)
8846 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8847}
8848
8849/*
8850 * "string()" function
8851 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008852 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853f_string(typval_T *argvars, typval_T *rettv)
8854{
8855 char_u *tofree;
8856 char_u numbuf[NUMBUFLEN];
8857
8858 rettv->v_type = VAR_STRING;
8859 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8860 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008861 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 if (rettv->vval.v_string != NULL && tofree == NULL)
8863 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8864}
8865
8866/*
8867 * "strlen()" function
8868 */
8869 static void
8870f_strlen(typval_T *argvars, typval_T *rettv)
8871{
8872 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008873 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874}
8875
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008876 static void
8877strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8878{
8879 char_u *s = tv_get_string(&argvars[0]);
8880 varnumber_T len = 0;
8881 int (*func_mb_ptr2char_adv)(char_u **pp);
8882
8883 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8884 while (*s != NUL)
8885 {
8886 func_mb_ptr2char_adv(&s);
8887 ++len;
8888 }
8889 rettv->vval.v_number = len;
8890}
8891
8892/*
8893 * "strcharlen()" function
8894 */
8895 static void
8896f_strcharlen(typval_T *argvars, typval_T *rettv)
8897{
8898 strchar_common(argvars, rettv, TRUE);
8899}
8900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901/*
8902 * "strchars()" function
8903 */
8904 static void
8905f_strchars(typval_T *argvars, typval_T *rettv)
8906{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008907 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908
8909 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008910 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008912 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008914 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915}
8916
8917/*
8918 * "strdisplaywidth()" function
8919 */
8920 static void
8921f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8922{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008923 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 int col = 0;
8925
8926 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008927 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928
8929 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8930}
8931
8932/*
8933 * "strwidth()" function
8934 */
8935 static void
8936f_strwidth(typval_T *argvars, typval_T *rettv)
8937{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008938 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939
Bram Moolenaar13505972019-01-24 15:04:48 +01008940 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941}
8942
8943/*
8944 * "strcharpart()" function
8945 */
8946 static void
8947f_strcharpart(typval_T *argvars, typval_T *rettv)
8948{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 char_u *p;
8950 int nchar;
8951 int nbyte = 0;
8952 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008953 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954 int len = 0;
8955 int slen;
8956 int error = FALSE;
8957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008958 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 slen = (int)STRLEN(p);
8960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008961 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962 if (!error)
8963 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008964 if (argvars[2].v_type != VAR_UNKNOWN
8965 && argvars[3].v_type != VAR_UNKNOWN)
8966 {
8967 skipcc = tv_get_bool(&argvars[3]);
8968 if (skipcc < 0 || skipcc > 1)
8969 {
8970 semsg(_(e_using_number_as_bool_nr), skipcc);
8971 return;
8972 }
8973 }
8974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 if (nchar > 0)
8976 while (nchar > 0 && nbyte < slen)
8977 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008978 if (skipcc)
8979 nbyte += mb_ptr2len(p + nbyte);
8980 else
8981 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008982 --nchar;
8983 }
8984 else
8985 nbyte = nchar;
8986 if (argvars[2].v_type != VAR_UNKNOWN)
8987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008988 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008989 while (charlen > 0 && nbyte + len < slen)
8990 {
8991 int off = nbyte + len;
8992
8993 if (off < 0)
8994 len += 1;
8995 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008996 {
8997 if (skipcc)
8998 len += mb_ptr2len(p + off);
8999 else
9000 len += MB_CPTR2LEN(p + off);
9001 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002 --charlen;
9003 }
9004 }
9005 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009006 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 }
9008
9009 /*
9010 * Only return the overlap between the specified part and the actual
9011 * string.
9012 */
9013 if (nbyte < 0)
9014 {
9015 len += nbyte;
9016 nbyte = 0;
9017 }
9018 else if (nbyte > slen)
9019 nbyte = slen;
9020 if (len < 0)
9021 len = 0;
9022 else if (nbyte + len > slen)
9023 len = slen - nbyte;
9024
9025 rettv->v_type = VAR_STRING;
9026 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027}
9028
9029/*
9030 * "strpart()" function
9031 */
9032 static void
9033f_strpart(typval_T *argvars, typval_T *rettv)
9034{
9035 char_u *p;
9036 int n;
9037 int len;
9038 int slen;
9039 int error = FALSE;
9040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009041 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009042 slen = (int)STRLEN(p);
9043
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009044 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 if (error)
9046 len = 0;
9047 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009048 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009049 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009050 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009052 // Only return the overlap between the specified part and the actual
9053 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 if (n < 0)
9055 {
9056 len += n;
9057 n = 0;
9058 }
9059 else if (n > slen)
9060 n = slen;
9061 if (len < 0)
9062 len = 0;
9063 else if (n + len > slen)
9064 len = slen - n;
9065
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009066 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9067 {
9068 int off;
9069
9070 // length in characters
9071 for (off = n; off < slen && len > 0; --len)
9072 off += mb_ptr2len(p + off);
9073 len = off - n;
9074 }
9075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 rettv->v_type = VAR_STRING;
9077 rettv->vval.v_string = vim_strnsave(p + n, len);
9078}
9079
9080/*
9081 * "strridx()" function
9082 */
9083 static void
9084f_strridx(typval_T *argvars, typval_T *rettv)
9085{
9086 char_u buf[NUMBUFLEN];
9087 char_u *needle;
9088 char_u *haystack;
9089 char_u *rest;
9090 char_u *lastmatch = NULL;
9091 int haystack_len, end_idx;
9092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009093 needle = tv_get_string_chk(&argvars[1]);
9094 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095
9096 rettv->vval.v_number = -1;
9097 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009098 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099
9100 haystack_len = (int)STRLEN(haystack);
9101 if (argvars[2].v_type != VAR_UNKNOWN)
9102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009103 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009104 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009106 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 }
9108 else
9109 end_idx = haystack_len;
9110
9111 if (*needle == NUL)
9112 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009113 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 lastmatch = haystack + end_idx;
9115 }
9116 else
9117 {
9118 for (rest = haystack; *rest != '\0'; ++rest)
9119 {
9120 rest = (char_u *)strstr((char *)rest, (char *)needle);
9121 if (rest == NULL || rest > haystack + end_idx)
9122 break;
9123 lastmatch = rest;
9124 }
9125 }
9126
9127 if (lastmatch == NULL)
9128 rettv->vval.v_number = -1;
9129 else
9130 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9131}
9132
9133/*
9134 * "strtrans()" function
9135 */
9136 static void
9137f_strtrans(typval_T *argvars, typval_T *rettv)
9138{
9139 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009140 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009141}
9142
9143/*
9144 * "submatch()" function
9145 */
9146 static void
9147f_submatch(typval_T *argvars, typval_T *rettv)
9148{
9149 int error = FALSE;
9150 int no;
9151 int retList = 0;
9152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009153 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 if (error)
9155 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009156 if (no < 0 || no >= NSUBEXP)
9157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009158 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009159 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009160 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009161 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009162 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 if (error)
9164 return;
9165
9166 if (retList == 0)
9167 {
9168 rettv->v_type = VAR_STRING;
9169 rettv->vval.v_string = reg_submatch(no);
9170 }
9171 else
9172 {
9173 rettv->v_type = VAR_LIST;
9174 rettv->vval.v_list = reg_submatch_list(no);
9175 }
9176}
9177
9178/*
9179 * "substitute()" function
9180 */
9181 static void
9182f_substitute(typval_T *argvars, typval_T *rettv)
9183{
9184 char_u patbuf[NUMBUFLEN];
9185 char_u subbuf[NUMBUFLEN];
9186 char_u flagsbuf[NUMBUFLEN];
9187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009188 char_u *str = tv_get_string_chk(&argvars[0]);
9189 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009190 char_u *sub = NULL;
9191 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009192 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009193
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009194 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9195 expr = &argvars[2];
9196 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009197 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009200 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9201 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 rettv->vval.v_string = NULL;
9203 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009204 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009205}
9206
9207/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009208 * "swapinfo(swap_filename)" function
9209 */
9210 static void
9211f_swapinfo(typval_T *argvars, typval_T *rettv)
9212{
9213 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009214 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009215}
9216
9217/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009218 * "swapname(expr)" function
9219 */
9220 static void
9221f_swapname(typval_T *argvars, typval_T *rettv)
9222{
9223 buf_T *buf;
9224
9225 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009226 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009227 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9228 || buf->b_ml.ml_mfp->mf_fname == NULL)
9229 rettv->vval.v_string = NULL;
9230 else
9231 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9232}
9233
9234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235 * "synID(lnum, col, trans)" function
9236 */
9237 static void
9238f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9239{
9240 int id = 0;
9241#ifdef FEAT_SYN_HL
9242 linenr_T lnum;
9243 colnr_T col;
9244 int trans;
9245 int transerr = FALSE;
9246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009247 lnum = tv_get_lnum(argvars); // -1 on type error
9248 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009249 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250
9251 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9252 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9253 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9254#endif
9255
9256 rettv->vval.v_number = id;
9257}
9258
9259/*
9260 * "synIDattr(id, what [, mode])" function
9261 */
9262 static void
9263f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9264{
9265 char_u *p = NULL;
9266#ifdef FEAT_SYN_HL
9267 int id;
9268 char_u *what;
9269 char_u *mode;
9270 char_u modebuf[NUMBUFLEN];
9271 int modec;
9272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009273 id = (int)tv_get_number(&argvars[0]);
9274 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275 if (argvars[2].v_type != VAR_UNKNOWN)
9276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009277 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 modec = TOLOWER_ASC(mode[0]);
9279 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009280 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009281 }
9282 else
9283 {
9284#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9285 if (USE_24BIT)
9286 modec = 'g';
9287 else
9288#endif
9289 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009290 modec = 'c';
9291 else
9292 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 }
9294
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 switch (TOLOWER_ASC(what[0]))
9296 {
9297 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009298 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009299 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009300 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 p = highlight_has_attr(id, HL_BOLD, modec);
9302 break;
9303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009304 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009305 p = highlight_color(id, what, modec);
9306 break;
9307
9308 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009309 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009311 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 p = highlight_has_attr(id, HL_ITALIC, modec);
9313 break;
9314
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009315 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009316 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317 break;
9318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009319 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009320 p = highlight_has_attr(id, HL_INVERSE, modec);
9321 break;
9322
9323 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009324 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009325 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009326 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009327 else if (TOLOWER_ASC(what[1]) == 't' &&
9328 TOLOWER_ASC(what[2]) == 'r')
9329 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009330 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 p = highlight_has_attr(id, HL_STANDOUT, modec);
9332 break;
9333
9334 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009335 if (TOLOWER_ASC(what[1]) == 'l') // ul
9336 p = highlight_color(id, what, modec);
9337 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009338 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9340 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009341 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009342 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9343 break;
9344 }
9345
9346 if (p != NULL)
9347 p = vim_strsave(p);
9348#endif
9349 rettv->v_type = VAR_STRING;
9350 rettv->vval.v_string = p;
9351}
9352
9353/*
9354 * "synIDtrans(id)" function
9355 */
9356 static void
9357f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9358{
9359 int id;
9360
9361#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009362 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009363
9364 if (id > 0)
9365 id = syn_get_final_id(id);
9366 else
9367#endif
9368 id = 0;
9369
9370 rettv->vval.v_number = id;
9371}
9372
9373/*
9374 * "synconcealed(lnum, col)" function
9375 */
9376 static void
9377f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9378{
9379#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9380 linenr_T lnum;
9381 colnr_T col;
9382 int syntax_flags = 0;
9383 int cchar;
9384 int matchid = 0;
9385 char_u str[NUMBUFLEN];
9386#endif
9387
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009388 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389
9390#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009391 lnum = tv_get_lnum(argvars); // -1 on type error
9392 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009393
Bram Moolenaara80faa82020-04-12 19:37:17 +02009394 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395
9396 if (rettv_list_alloc(rettv) != FAIL)
9397 {
9398 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9399 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9400 && curwin->w_p_cole > 0)
9401 {
9402 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9403 syntax_flags = get_syntax_info(&matchid);
9404
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009405 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009406 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9407 {
9408 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009409 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009410 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9411 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 if (cchar != NUL)
9413 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 if (has_mbyte)
9415 (*mb_char2bytes)(cchar, str);
9416 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417 str[0] = cchar;
9418 }
9419 }
9420 }
9421
9422 list_append_number(rettv->vval.v_list,
9423 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009424 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425 list_append_string(rettv->vval.v_list, str, -1);
9426 list_append_number(rettv->vval.v_list, matchid);
9427 }
9428#endif
9429}
9430
9431/*
9432 * "synstack(lnum, col)" function
9433 */
9434 static void
9435f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9436{
9437#ifdef FEAT_SYN_HL
9438 linenr_T lnum;
9439 colnr_T col;
9440 int i;
9441 int id;
9442#endif
9443
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009444 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445
9446#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009447 lnum = tv_get_lnum(argvars); // -1 on type error
9448 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449
9450 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9451 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9452 && rettv_list_alloc(rettv) != FAIL)
9453 {
9454 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9455 for (i = 0; ; ++i)
9456 {
9457 id = syn_get_stack_item(i);
9458 if (id < 0)
9459 break;
9460 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9461 break;
9462 }
9463 }
9464#endif
9465}
9466
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009467/*
9468 * "tabpagebuflist()" function
9469 */
9470 static void
9471f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9472{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 tabpage_T *tp;
9474 win_T *wp = NULL;
9475
9476 if (argvars[0].v_type == VAR_UNKNOWN)
9477 wp = firstwin;
9478 else
9479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009480 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009481 if (tp != NULL)
9482 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9483 }
9484 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9485 {
9486 for (; wp != NULL; wp = wp->w_next)
9487 if (list_append_number(rettv->vval.v_list,
9488 wp->w_buffer->b_fnum) == FAIL)
9489 break;
9490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491}
9492
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 * "tagfiles()" function
9495 */
9496 static void
9497f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9498{
9499 char_u *fname;
9500 tagname_T tn;
9501 int first;
9502
9503 if (rettv_list_alloc(rettv) == FAIL)
9504 return;
9505 fname = alloc(MAXPATHL);
9506 if (fname == NULL)
9507 return;
9508
9509 for (first = TRUE; ; first = FALSE)
9510 if (get_tagfname(&tn, first, fname) == FAIL
9511 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9512 break;
9513 tagname_free(&tn);
9514 vim_free(fname);
9515}
9516
9517/*
9518 * "taglist()" function
9519 */
9520 static void
9521f_taglist(typval_T *argvars, typval_T *rettv)
9522{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009523 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009524 char_u *tag_pattern;
9525
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009526 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527
9528 rettv->vval.v_number = FALSE;
9529 if (*tag_pattern == NUL)
9530 return;
9531
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009533 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009535 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536}
9537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009539 * "tolower(string)" function
9540 */
9541 static void
9542f_tolower(typval_T *argvars, typval_T *rettv)
9543{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009545 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546}
9547
9548/*
9549 * "toupper(string)" function
9550 */
9551 static void
9552f_toupper(typval_T *argvars, typval_T *rettv)
9553{
9554 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009555 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556}
9557
9558/*
9559 * "tr(string, fromstr, tostr)" function
9560 */
9561 static void
9562f_tr(typval_T *argvars, typval_T *rettv)
9563{
9564 char_u *in_str;
9565 char_u *fromstr;
9566 char_u *tostr;
9567 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 int inlen;
9569 int fromlen;
9570 int tolen;
9571 int idx;
9572 char_u *cpstr;
9573 int cplen;
9574 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009575 char_u buf[NUMBUFLEN];
9576 char_u buf2[NUMBUFLEN];
9577 garray_T ga;
9578
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009579 in_str = tv_get_string(&argvars[0]);
9580 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9581 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009583 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584 rettv->v_type = VAR_STRING;
9585 rettv->vval.v_string = NULL;
9586 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009587 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 ga_init2(&ga, (int)sizeof(char), 80);
9589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009591 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 if (STRLEN(fromstr) != STRLEN(tostr))
9593 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009595 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596 ga_clear(&ga);
9597 return;
9598 }
9599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009600 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601 while (*in_str != NUL)
9602 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 if (has_mbyte)
9604 {
9605 inlen = (*mb_ptr2len)(in_str);
9606 cpstr = in_str;
9607 cplen = inlen;
9608 idx = 0;
9609 for (p = fromstr; *p != NUL; p += fromlen)
9610 {
9611 fromlen = (*mb_ptr2len)(p);
9612 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9613 {
9614 for (p = tostr; *p != NUL; p += tolen)
9615 {
9616 tolen = (*mb_ptr2len)(p);
9617 if (idx-- == 0)
9618 {
9619 cplen = tolen;
9620 cpstr = p;
9621 break;
9622 }
9623 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009624 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009625 goto error;
9626 break;
9627 }
9628 ++idx;
9629 }
9630
9631 if (first && cpstr == in_str)
9632 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009633 // Check that fromstr and tostr have the same number of
9634 // (multi-byte) characters. Done only once when a character
9635 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 first = FALSE;
9637 for (p = tostr; *p != NUL; p += tolen)
9638 {
9639 tolen = (*mb_ptr2len)(p);
9640 --idx;
9641 }
9642 if (idx != 0)
9643 goto error;
9644 }
9645
9646 (void)ga_grow(&ga, cplen);
9647 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9648 ga.ga_len += cplen;
9649
9650 in_str += inlen;
9651 }
9652 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009654 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 p = vim_strchr(fromstr, *in_str);
9656 if (p != NULL)
9657 ga_append(&ga, tostr[p - fromstr]);
9658 else
9659 ga_append(&ga, *in_str);
9660 ++in_str;
9661 }
9662 }
9663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009664 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 (void)ga_grow(&ga, 1);
9666 ga_append(&ga, NUL);
9667
9668 rettv->vval.v_string = ga.ga_data;
9669}
9670
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009671/*
9672 * "trim({expr})" function
9673 */
9674 static void
9675f_trim(typval_T *argvars, typval_T *rettv)
9676{
9677 char_u buf1[NUMBUFLEN];
9678 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009679 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009680 char_u *mask = NULL;
9681 char_u *tail;
9682 char_u *prev;
9683 char_u *p;
9684 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009685 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009686
9687 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009688 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009689 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009690 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009691
9692 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009694 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009695
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009696 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009697 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009698 int error = 0;
9699
9700 // leading or trailing characters to trim
9701 dir = (int)tv_get_number_chk(&argvars[2], &error);
9702 if (error)
9703 return;
9704 if (dir < 0 || dir > 2)
9705 {
9706 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9707 return;
9708 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009709 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009710 }
9711
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009712 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009713 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009714 // Trim leading characters
9715 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009716 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009717 c1 = PTR2CHAR(head);
9718 if (mask == NULL)
9719 {
9720 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009721 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009722 }
9723 else
9724 {
9725 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9726 if (c1 == PTR2CHAR(p))
9727 break;
9728 if (*p == NUL)
9729 break;
9730 }
9731 MB_PTR_ADV(head);
9732 }
9733 }
9734
9735 tail = head + STRLEN(head);
9736 if (dir == 0 || dir == 2)
9737 {
9738 // Trim trailing characters
9739 for (; tail > head; tail = prev)
9740 {
9741 prev = tail;
9742 MB_PTR_BACK(head, prev);
9743 c1 = PTR2CHAR(prev);
9744 if (mask == NULL)
9745 {
9746 if (c1 > ' ' && c1 != 0xa0)
9747 break;
9748 }
9749 else
9750 {
9751 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9752 if (c1 == PTR2CHAR(p))
9753 break;
9754 if (*p == NUL)
9755 break;
9756 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009757 }
9758 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009759 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009760}
9761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762/*
9763 * "type(expr)" function
9764 */
9765 static void
9766f_type(typval_T *argvars, typval_T *rettv)
9767{
9768 int n = -1;
9769
9770 switch (argvars[0].v_type)
9771 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009772 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9773 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009775 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9776 case VAR_LIST: n = VAR_TYPE_LIST; break;
9777 case VAR_DICT: n = VAR_TYPE_DICT; break;
9778 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9779 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9780 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009781 case VAR_JOB: n = VAR_TYPE_JOB; break;
9782 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009783 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009784 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009786 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009787 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009788 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 n = -1;
9790 break;
9791 }
9792 rettv->vval.v_number = n;
9793}
9794
9795/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 * "virtcol(string)" function
9797 */
9798 static void
9799f_virtcol(typval_T *argvars, typval_T *rettv)
9800{
9801 colnr_T vcol = 0;
9802 pos_T *fp;
9803 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009804 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009806 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9808 && fnum == curbuf->b_fnum)
9809 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009810 // Limit the column to a valid value, getvvcol() doesn't check.
9811 if (fp->col < 0)
9812 fp->col = 0;
9813 else
9814 {
9815 len = (int)STRLEN(ml_get(fp->lnum));
9816 if (fp->col > len)
9817 fp->col = len;
9818 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 getvvcol(curwin, fp, NULL, NULL, &vcol);
9820 ++vcol;
9821 }
9822
9823 rettv->vval.v_number = vcol;
9824}
9825
9826/*
9827 * "visualmode()" function
9828 */
9829 static void
9830f_visualmode(typval_T *argvars, typval_T *rettv)
9831{
9832 char_u str[2];
9833
9834 rettv->v_type = VAR_STRING;
9835 str[0] = curbuf->b_visual_mode_eval;
9836 str[1] = NUL;
9837 rettv->vval.v_string = vim_strsave(str);
9838
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009839 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840 if (non_zero_arg(&argvars[0]))
9841 curbuf->b_visual_mode_eval = NUL;
9842}
9843
9844/*
9845 * "wildmenumode()" function
9846 */
9847 static void
9848f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9849{
9850#ifdef FEAT_WILDMENU
9851 if (wild_menu_showing)
9852 rettv->vval.v_number = 1;
9853#endif
9854}
9855
9856/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009857 * "windowsversion()" function
9858 */
9859 static void
9860f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9861{
9862 rettv->v_type = VAR_STRING;
9863 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9864}
9865
9866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 * "wordcount()" function
9868 */
9869 static void
9870f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9871{
9872 if (rettv_dict_alloc(rettv) == FAIL)
9873 return;
9874 cursor_pos_info(rettv->vval.v_dict);
9875}
9876
9877/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009878 * "xor(expr, expr)" function
9879 */
9880 static void
9881f_xor(typval_T *argvars, typval_T *rettv)
9882{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009883 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9884 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885}
9886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009887#endif // FEAT_EVAL