blob: 2539515bc8aa9a00ec29a59f8d705cdadae84d30 [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 Lakshmanan18d46582021-06-23 20:46:52 +02001703 {"test_gui_drop_files", 4, 4, 0, NULL,
1704 ret_void, f_test_gui_drop_files},
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001705 {"test_gui_mouse_event", 5, 5, 0, NULL,
1706 ret_void, f_test_gui_mouse_event},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001707 {"test_ignore_error", 1, 1, FEARG_1, NULL,
1708 ret_void, f_test_ignore_error},
1709 {"test_null_blob", 0, 0, 0, NULL,
1710 ret_blob, f_test_null_blob},
1711 {"test_null_channel", 0, 0, 0, NULL,
1712 ret_channel, JOB_FUNC(f_test_null_channel)},
1713 {"test_null_dict", 0, 0, 0, NULL,
1714 ret_dict_any, f_test_null_dict},
1715 {"test_null_function", 0, 0, 0, NULL,
1716 ret_func_any, f_test_null_function},
1717 {"test_null_job", 0, 0, 0, NULL,
1718 ret_job, JOB_FUNC(f_test_null_job)},
1719 {"test_null_list", 0, 0, 0, NULL,
1720 ret_list_any, f_test_null_list},
1721 {"test_null_partial", 0, 0, 0, NULL,
1722 ret_func_any, f_test_null_partial},
1723 {"test_null_string", 0, 0, 0, NULL,
1724 ret_string, f_test_null_string},
1725 {"test_option_not_set", 1, 1, FEARG_1, NULL,
1726 ret_void, f_test_option_not_set},
1727 {"test_override", 2, 2, FEARG_2, NULL,
1728 ret_void, f_test_override},
1729 {"test_refcount", 1, 1, FEARG_1, NULL,
1730 ret_number, f_test_refcount},
1731 {"test_scrollbar", 3, 3, FEARG_2, NULL,
1732 ret_void,
Bram Moolenaarab186732018-09-14 21:27:06 +02001733#ifdef FEAT_GUI
Bram Moolenaar15c47602020-03-26 22:16:48 +01001734 f_test_scrollbar
1735#else
1736 NULL
Bram Moolenaarab186732018-09-14 21:27:06 +02001737#endif
Bram Moolenaar15c47602020-03-26 22:16:48 +01001738 },
Bram Moolenaar94738d82020-10-21 14:25:07 +02001739 {"test_setmouse", 2, 2, 0, NULL,
1740 ret_void, f_test_setmouse},
1741 {"test_settime", 1, 1, FEARG_1, NULL,
1742 ret_void, f_test_settime},
1743 {"test_srand_seed", 0, 1, FEARG_1, NULL,
1744 ret_void, f_test_srand_seed},
1745 {"test_unknown", 0, 0, 0, NULL,
1746 ret_any, f_test_unknown},
1747 {"test_void", 0, 0, 0, NULL,
1748 ret_void, f_test_void},
1749 {"timer_info", 0, 1, FEARG_1, NULL,
1750 ret_list_dict_any, TIMER_FUNC(f_timer_info)},
1751 {"timer_pause", 2, 2, FEARG_1, NULL,
1752 ret_void, TIMER_FUNC(f_timer_pause)},
1753 {"timer_start", 2, 3, FEARG_1, NULL,
1754 ret_number, TIMER_FUNC(f_timer_start)},
1755 {"timer_stop", 1, 1, FEARG_1, NULL,
1756 ret_void, TIMER_FUNC(f_timer_stop)},
1757 {"timer_stopall", 0, 0, 0, NULL,
1758 ret_void, TIMER_FUNC(f_timer_stopall)},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001759 {"tolower", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001760 ret_string, f_tolower},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001761 {"toupper", 1, 1, FEARG_1, arg1_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001762 ret_string, f_toupper},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001763 {"tr", 3, 3, FEARG_1, arg3_string,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001764 ret_string, f_tr},
1765 {"trim", 1, 3, FEARG_1, NULL,
1766 ret_string, f_trim},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001767 {"trunc", 1, 1, FEARG_1, arg1_float_or_nr,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001768 ret_float, FLOAT_FUNC(f_trunc)},
1769 {"type", 1, 1, FEARG_1, NULL,
1770 ret_number, f_type},
Bram Moolenaara47e05f2021-01-12 21:49:00 +01001771 {"typename", 1, 1, FEARG_1, NULL,
1772 ret_string, f_typename},
Bram Moolenaar94738d82020-10-21 14:25:07 +02001773 {"undofile", 1, 1, FEARG_1, NULL,
1774 ret_string, f_undofile},
1775 {"undotree", 0, 0, 0, NULL,
1776 ret_dict_any, f_undotree},
1777 {"uniq", 1, 3, FEARG_1, NULL,
1778 ret_list_any, f_uniq},
1779 {"values", 1, 1, FEARG_1, NULL,
1780 ret_list_any, f_values},
1781 {"virtcol", 1, 1, FEARG_1, NULL,
1782 ret_number, f_virtcol},
1783 {"visualmode", 0, 1, 0, NULL,
1784 ret_string, f_visualmode},
1785 {"wildmenumode", 0, 0, 0, NULL,
1786 ret_number, f_wildmenumode},
1787 {"win_execute", 2, 3, FEARG_2, NULL,
1788 ret_string, f_win_execute},
1789 {"win_findbuf", 1, 1, FEARG_1, NULL,
1790 ret_list_number, f_win_findbuf},
1791 {"win_getid", 0, 2, FEARG_1, NULL,
1792 ret_number, f_win_getid},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001793 {"win_gettype", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001794 ret_string, f_win_gettype},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001795 {"win_gotoid", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001796 ret_number_bool, f_win_gotoid},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001797 {"win_id2tabwin", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001798 ret_list_number, f_win_id2tabwin},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001799 {"win_id2win", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001800 ret_number, f_win_id2win},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001801 {"win_screenpos", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001802 ret_list_number, f_win_screenpos},
1803 {"win_splitmove", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001804 ret_number_bool, f_win_splitmove},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001805 {"winbufnr", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001806 ret_number, f_winbufnr},
1807 {"wincol", 0, 0, 0, NULL,
1808 ret_number, f_wincol},
1809 {"windowsversion", 0, 0, 0, NULL,
1810 ret_string, f_windowsversion},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001811 {"winheight", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001812 ret_number, f_winheight},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001813 {"winlayout", 0, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001814 ret_list_any, f_winlayout},
1815 {"winline", 0, 0, 0, NULL,
1816 ret_number, f_winline},
1817 {"winnr", 0, 1, FEARG_1, NULL,
1818 ret_number, f_winnr},
1819 {"winrestcmd", 0, 0, 0, NULL,
1820 ret_string, f_winrestcmd},
1821 {"winrestview", 1, 1, FEARG_1, NULL,
1822 ret_void, f_winrestview},
1823 {"winsaveview", 0, 0, 0, NULL,
Bram Moolenaar43b69b32021-01-07 20:23:33 +01001824 ret_dict_number, f_winsaveview},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001825 {"winwidth", 1, 1, FEARG_1, arg1_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001826 ret_number, f_winwidth},
1827 {"wordcount", 0, 0, 0, NULL,
1828 ret_dict_number, f_wordcount},
1829 {"writefile", 2, 3, FEARG_1, NULL,
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01001830 ret_number_bool, f_writefile},
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001831 {"xor", 2, 2, FEARG_1, arg2_number,
Bram Moolenaar94738d82020-10-21 14:25:07 +02001832 ret_number, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +02001833};
1834
K.Takataeecf2b32021-06-02 14:56:39 +02001835#if defined(EBCDIC) || defined(PROTO)
1836/*
1837 * Compare funcentry_T by function name.
1838 */
1839 static int
1840compare_func_name(const void *s1, const void *s2)
1841{
1842 funcentry_T *p1 = (funcentry_T *)s1;
1843 funcentry_T *p2 = (funcentry_T *)s2;
1844
1845 return STRCMP(p1->f_name, p2->f_name);
1846}
1847
1848/*
1849 * Sort the function table by function name.
1850 * The sorting of the table above is ASCII dependent.
1851 * On machines using EBCDIC we have to sort it.
1852 */
1853 void
1854sortFunctions(void)
1855{
1856 size_t funcCnt = ARRAY_LENGTH(global_functions);
1857
1858 qsort(global_functions, funcCnt, sizeof(funcentry_T), compare_func_name);
1859}
1860#endif
1861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001862/*
1863 * Function given to ExpandGeneric() to obtain the list of internal
1864 * or user defined function names.
1865 */
1866 char_u *
1867get_function_name(expand_T *xp, int idx)
1868{
1869 static int intidx = -1;
1870 char_u *name;
1871
1872 if (idx == 0)
1873 intidx = -1;
1874 if (intidx < 0)
1875 {
1876 name = get_user_func_name(xp, idx);
1877 if (name != NULL)
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001878 {
1879 if (*name != '<' && STRNCMP("g:", xp->xp_pattern, 2) == 0)
1880 return cat_prefix_varname('g', name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 return name;
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001882 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 }
K.Takataeeec2542021-06-02 13:28:16 +02001884 if (++intidx < (int)ARRAY_LENGTH(global_functions))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001886 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001888 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 STRCAT(IObuff, ")");
1890 return IObuff;
1891 }
1892
1893 return NULL;
1894}
1895
1896/*
1897 * Function given to ExpandGeneric() to obtain the list of internal or
1898 * user defined variable or function names.
1899 */
1900 char_u *
1901get_expr_name(expand_T *xp, int idx)
1902{
1903 static int intidx = -1;
1904 char_u *name;
1905
1906 if (idx == 0)
1907 intidx = -1;
1908 if (intidx < 0)
1909 {
1910 name = get_function_name(xp, idx);
1911 if (name != NULL)
1912 return name;
1913 }
1914 return get_user_var_name(xp, ++intidx);
1915}
1916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001917/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001918 * Find internal function "name" in table "global_functions".
Bram Moolenaar15c47602020-03-26 22:16:48 +01001919 * Return index, or -1 if not found or "implemented" is TRUE and the function
1920 * is not implemented.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 */
Bram Moolenaar15c47602020-03-26 22:16:48 +01001922 static int
1923find_internal_func_opt(char_u *name, int implemented)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924{
1925 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001926 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001927 int cmp;
1928 int x;
1929
K.Takataeeec2542021-06-02 13:28:16 +02001930 last = (int)ARRAY_LENGTH(global_functions) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001931
1932 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 while (first <= last)
1934 {
1935 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001936 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001937 if (cmp < 0)
1938 last = x - 1;
1939 else if (cmp > 0)
1940 first = x + 1;
Bram Moolenaar15c47602020-03-26 22:16:48 +01001941 else if (implemented && global_functions[x].f_func == NULL)
1942 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943 else
1944 return x;
1945 }
1946 return -1;
1947}
1948
Bram Moolenaar15c47602020-03-26 22:16:48 +01001949/*
1950 * Find internal function "name" in table "global_functions".
1951 * Return index, or -1 if not found or the function is not implemented.
1952 */
1953 int
1954find_internal_func(char_u *name)
1955{
1956 return find_internal_func_opt(name, TRUE);
1957}
1958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001959 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001960has_internal_func(char_u *name)
1961{
Bram Moolenaar15c47602020-03-26 22:16:48 +01001962 return find_internal_func_opt(name, TRUE) >= 0;
1963}
1964
1965 static int
1966has_internal_func_name(char_u *name)
1967{
1968 return find_internal_func_opt(name, FALSE) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001969}
1970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 char *
1972internal_func_name(int idx)
1973{
1974 return global_functions[idx].f_name;
1975}
1976
Bram Moolenaar94738d82020-10-21 14:25:07 +02001977/*
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001978 * Check the argument types for builtin function "idx".
Bram Moolenaar94738d82020-10-21 14:25:07 +02001979 * Uses the list of types on the type stack: "types".
1980 * Return FAIL and gives an error message when a type is wrong.
1981 */
1982 int
Bram Moolenaar351ead02021-01-16 16:07:01 +01001983internal_func_check_arg_types(
1984 type_T **types,
1985 int idx,
1986 int argcount,
1987 cctx_T *cctx)
Bram Moolenaar94738d82020-10-21 14:25:07 +02001988{
1989 argcheck_T *argchecks = global_functions[idx].f_argcheck;
1990 int i;
1991
1992 if (argchecks != NULL)
1993 {
1994 argcontext_T context;
1995
1996 context.arg_count = argcount;
Bram Moolenaarca174532020-10-21 16:42:22 +02001997 context.arg_types = types;
Bram Moolenaar351ead02021-01-16 16:07:01 +01001998 context.arg_cctx = cctx;
Bram Moolenaar94738d82020-10-21 14:25:07 +02001999 for (i = 0; i < argcount; ++i)
2000 if (argchecks[i] != NULL)
2001 {
2002 context.arg_idx = i;
Bram Moolenaarca174532020-10-21 16:42:22 +02002003 if (argchecks[i](types[i], &context) == FAIL)
Bram Moolenaar94738d82020-10-21 14:25:07 +02002004 return FAIL;
2005 }
2006 }
2007 return OK;
2008}
2009
Bram Moolenaara1224cb2020-10-22 12:31:49 +02002010/*
2011 * Call the "f_retfunc" function to obtain the return type of function "idx".
2012 * "argtypes" is the list of argument types or NULL when there are no
2013 * arguments.
2014 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 type_T *
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002016internal_func_ret_type(int idx, int argcount, type_T **argtypes)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017{
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01002018 return global_functions[idx].f_retfunc(argcount, argtypes);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019}
2020
2021/*
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002022 * Return TRUE if "idx" is for the map() function.
2023 */
2024 int
2025internal_func_is_map(int idx)
2026{
2027 return global_functions[idx].f_func == f_map;
2028}
2029
2030/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 * Check the argument count to use for internal function "idx".
Bram Moolenaar389df252020-07-09 21:20:47 +02002032 * Returns -1 for failure, 0 if no method base accepted, 1 if method base is
2033 * first argument, 2 if method base is second argument, etc. 9 if method base
2034 * is last argument.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 */
2036 int
2037check_internal_func(int idx, int argcount)
2038{
2039 int res;
2040 char *name;
2041
2042 if (argcount < global_functions[idx].f_min_argc)
2043 res = FCERR_TOOFEW;
2044 else if (argcount > global_functions[idx].f_max_argc)
2045 res = FCERR_TOOMANY;
2046 else
Bram Moolenaar389df252020-07-09 21:20:47 +02002047 return global_functions[idx].f_argtype;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048
2049 name = internal_func_name(idx);
2050 if (res == FCERR_TOOMANY)
2051 semsg(_(e_toomanyarg), name);
2052 else
2053 semsg(_(e_toofewarg), name);
Bram Moolenaar389df252020-07-09 21:20:47 +02002054 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055}
2056
Bram Moolenaarac92e252019-08-03 21:58:38 +02002057 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058call_internal_func(
2059 char_u *name,
2060 int argcount,
2061 typval_T *argvars,
2062 typval_T *rettv)
2063{
2064 int i;
2065
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002066 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002067 if (i < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002068 return FCERR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002069 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002070 return FCERR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002071 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002072 return FCERR_TOOMANY;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002074 global_functions[i].f_func(argvars, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002075 return FCERR_NONE;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002076}
2077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 void
2079call_internal_func_by_idx(
2080 int idx,
2081 typval_T *argvars,
2082 typval_T *rettv)
2083{
2084 global_functions[idx].f_func(argvars, rettv);
2085}
2086
Bram Moolenaarac92e252019-08-03 21:58:38 +02002087/*
2088 * Invoke a method for base->method().
2089 */
2090 int
2091call_internal_method(
2092 char_u *name,
2093 int argcount,
2094 typval_T *argvars,
2095 typval_T *rettv,
2096 typval_T *basetv)
2097{
2098 int i;
2099 int fi;
2100 typval_T argv[MAX_FUNC_ARGS + 1];
2101
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002102 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02002103 if (fi < 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002104 return FCERR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02002105 if (global_functions[fi].f_argtype == 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01002106 return FCERR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002107 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002108 return FCERR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002109 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaaref140542019-12-31 21:27:13 +01002110 return FCERR_TOOMANY;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002111
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002112 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002113 {
2114 // base value goes last
2115 for (i = 0; i < argcount; ++i)
2116 argv[i] = argvars[i];
2117 argv[argcount] = *basetv;
2118 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002119 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02002120 {
2121 // base value goes second
2122 argv[0] = argvars[0];
2123 argv[1] = *basetv;
2124 for (i = 1; i < argcount; ++i)
2125 argv[i + 1] = argvars[i];
2126 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02002127 else if (global_functions[fi].f_argtype == FEARG_3)
2128 {
2129 // base value goes third
2130 argv[0] = argvars[0];
2131 argv[1] = argvars[1];
2132 argv[2] = *basetv;
2133 for (i = 2; i < argcount; ++i)
2134 argv[i + 1] = argvars[i];
2135 }
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002136 else if (global_functions[fi].f_argtype == FEARG_4)
2137 {
2138 // base value goes fourth
2139 argv[0] = argvars[0];
2140 argv[1] = argvars[1];
2141 argv[2] = argvars[2];
2142 argv[3] = *basetv;
2143 for (i = 3; i < argcount; ++i)
2144 argv[i + 1] = argvars[i];
2145 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02002146 else
2147 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002148 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02002149 argv[0] = *basetv;
2150 for (i = 0; i < argcount; ++i)
2151 argv[i + 1] = argvars[i];
2152 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002153 argv[argcount + 1].v_type = VAR_UNKNOWN;
2154
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02002155 global_functions[fi].f_func(argv, rettv);
Bram Moolenaaref140542019-12-31 21:27:13 +01002156 return FCERR_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157}
2158
2159/*
2160 * Return TRUE for a non-zero Number and a non-empty String.
2161 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002162 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163non_zero_arg(typval_T *argvars)
2164{
2165 return ((argvars[0].v_type == VAR_NUMBER
2166 && argvars[0].vval.v_number != 0)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002167 || (argvars[0].v_type == VAR_BOOL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 && argvars[0].vval.v_number == VVAL_TRUE)
2169 || (argvars[0].v_type == VAR_STRING
2170 && argvars[0].vval.v_string != NULL
2171 && *argvars[0].vval.v_string != NUL));
2172}
2173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 * "and(expr, expr)" function
2176 */
2177 static void
2178f_and(typval_T *argvars, typval_T *rettv)
2179{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002180 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
2181 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02002182}
2183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01002185 * "balloon_show()" function
2186 */
2187#ifdef FEAT_BEVAL
2188 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002189f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
2190{
2191 rettv->v_type = VAR_STRING;
2192 if (balloonEval != NULL)
2193 {
2194 if (balloonEval->msg == NULL)
2195 rettv->vval.v_string = NULL;
2196 else
2197 rettv->vval.v_string = vim_strsave(balloonEval->msg);
2198 }
2199}
2200
2201 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01002202f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
2203{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002204 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002205 {
2206 if (argvars[0].v_type == VAR_LIST
2207# ifdef FEAT_GUI
2208 && !gui.in_use
2209# endif
2210 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002211 {
2212 list_T *l = argvars[0].vval.v_list;
2213
2214 // empty list removes the balloon
2215 post_balloon(balloonEval, NULL,
2216 l == NULL || l->lv_len == 0 ? NULL : l);
2217 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002218 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002219 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002220 char_u *mesg;
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002221
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002222 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2223 return;
2224
2225 mesg = tv_get_string_chk(&argvars[0]);
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02002226 if (mesg != NULL)
2227 // empty string removes the balloon
2228 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
2229 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01002230 }
2231}
2232
Bram Moolenaar669a8282017-11-19 20:13:05 +01002233# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01002234 static void
2235f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
2236{
2237 if (rettv_list_alloc(rettv) == OK)
2238 {
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002239 char_u *msg;
Bram Moolenaar246fe032017-11-19 19:56:27 +01002240
Bram Moolenaar32105ae2021-03-27 18:59:25 +01002241 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2242 return;
2243 msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002244 if (msg != NULL)
2245 {
2246 pumitem_T *array;
2247 int size = split_message(msg, &array);
2248 int i;
2249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002250 // Skip the first and last item, they are always empty.
Bram Moolenaar246fe032017-11-19 19:56:27 +01002251 for (i = 1; i < size - 1; ++i)
2252 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01002253 while (size > 0)
2254 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01002255 vim_free(array);
2256 }
2257 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01002258}
Bram Moolenaar669a8282017-11-19 20:13:05 +01002259# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01002260#endif
2261
2262/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002263 * Get the buffer from "arg" and give an error and return NULL if it is not
2264 * valid.
2265 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002266 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002267get_buf_arg(typval_T *arg)
2268{
2269 buf_T *buf;
2270
2271 ++emsg_off;
2272 buf = tv_get_buf(arg, FALSE);
2273 --emsg_off;
2274 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002275 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002276 return buf;
2277}
2278
2279/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002280 * "byte2line(byte)" function
2281 */
2282 static void
2283f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2284{
2285#ifndef FEAT_BYTEOFF
2286 rettv->vval.v_number = -1;
2287#else
2288 long boff = 0;
2289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002290 boff = tv_get_number(&argvars[0]) - 1; // boff gets -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002291 if (boff < 0)
2292 rettv->vval.v_number = -1;
2293 else
2294 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2295 (linenr_T)0, &boff);
2296#endif
2297}
2298
2299 static void
2300byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2301{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002303 char_u *str;
2304 varnumber_T idx;
2305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002306 str = tv_get_string_chk(&argvars[0]);
2307 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 rettv->vval.v_number = -1;
2309 if (str == NULL || idx < 0)
2310 return;
2311
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002312 t = str;
2313 for ( ; idx > 0; idx--)
2314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002315 if (*t == NUL) // EOL reached
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002316 return;
2317 if (enc_utf8 && comp)
2318 t += utf_ptr2len(t);
2319 else
2320 t += (*mb_ptr2len)(t);
2321 }
2322 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002323}
2324
2325/*
2326 * "byteidx()" function
2327 */
2328 static void
2329f_byteidx(typval_T *argvars, typval_T *rettv)
2330{
2331 byteidx(argvars, rettv, FALSE);
2332}
2333
2334/*
2335 * "byteidxcomp()" function
2336 */
2337 static void
2338f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2339{
2340 byteidx(argvars, rettv, TRUE);
2341}
2342
2343/*
2344 * "call(func, arglist [, dict])" function
2345 */
2346 static void
2347f_call(typval_T *argvars, typval_T *rettv)
2348{
2349 char_u *func;
2350 partial_T *partial = NULL;
2351 dict_T *selfdict = NULL;
2352
2353 if (argvars[1].v_type != VAR_LIST)
2354 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002355 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002356 return;
2357 }
2358 if (argvars[1].vval.v_list == NULL)
2359 return;
2360
2361 if (argvars[0].v_type == VAR_FUNC)
2362 func = argvars[0].vval.v_string;
2363 else if (argvars[0].v_type == VAR_PARTIAL)
2364 {
2365 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002366 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367 }
2368 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002369 func = tv_get_string(&argvars[0]);
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002370 if (func == NULL || *func == NUL)
2371 return; // type error, empty name or null function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372
2373 if (argvars[2].v_type != VAR_UNKNOWN)
2374 {
2375 if (argvars[2].v_type != VAR_DICT)
2376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002377 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378 return;
2379 }
2380 selfdict = argvars[2].vval.v_dict;
2381 }
2382
2383 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2384}
2385
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386/*
2387 * "changenr()" function
2388 */
2389 static void
2390f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2391{
2392 rettv->vval.v_number = curbuf->b_u_seq_cur;
2393}
2394
2395/*
2396 * "char2nr(string)" function
2397 */
2398 static void
2399f_char2nr(typval_T *argvars, typval_T *rettv)
2400{
Bram Moolenaarc5809432021-03-27 21:23:30 +01002401 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2402 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002403 if (has_mbyte)
2404 {
2405 int utf8 = 0;
2406
2407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar24f77502020-09-04 19:50:57 +02002408 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409
2410 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002411 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002413 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414 }
2415 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002416 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417}
2418
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002419/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002420 * Get the current cursor column and store it in 'rettv'. If 'charcol' is TRUE,
2421 * returns the character index of the column. Otherwise, returns the byte index
2422 * of the column.
2423 */
2424 static void
2425get_col(typval_T *argvars, typval_T *rettv, int charcol)
2426{
2427 colnr_T col = 0;
2428 pos_T *fp;
2429 int fnum = curbuf->b_fnum;
2430
2431 fp = var2fpos(&argvars[0], FALSE, &fnum, charcol);
2432 if (fp != NULL && fnum == curbuf->b_fnum)
2433 {
2434 if (fp->col == MAXCOL)
2435 {
2436 // '> can be MAXCOL, get the length of the line then
2437 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2438 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2439 else
2440 col = MAXCOL;
2441 }
2442 else
2443 {
2444 col = fp->col + 1;
2445 // col(".") when the cursor is on the NUL at the end of the line
2446 // because of "coladd" can be seen as an extra column.
2447 if (virtual_active() && fp == &curwin->w_cursor)
2448 {
2449 char_u *p = ml_get_cursor();
2450
2451 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2452 curwin->w_virtcol - curwin->w_cursor.coladd))
2453 {
2454 int l;
2455
2456 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2457 col += l;
2458 }
2459 }
2460 }
2461 }
2462 rettv->vval.v_number = col;
2463}
2464
2465/*
2466 * "charcol()" function
2467 */
2468 static void
2469f_charcol(typval_T *argvars, typval_T *rettv)
2470{
2471 get_col(argvars, rettv, TRUE);
2472}
2473
2474/*
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002475 * "charidx()" function
2476 */
2477 static void
2478f_charidx(typval_T *argvars, typval_T *rettv)
2479{
2480 char_u *str;
2481 varnumber_T idx;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002482 varnumber_T countcc = FALSE;
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002483 char_u *p;
2484 int len;
2485 int (*ptr2len)(char_u *);
2486
2487 rettv->vval.v_number = -1;
2488
2489 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER
2490 || (argvars[2].v_type != VAR_UNKNOWN
2491 && argvars[2].v_type != VAR_NUMBER))
2492 {
2493 emsg(_(e_invarg));
2494 return;
2495 }
2496
2497 str = tv_get_string_chk(&argvars[0]);
2498 idx = tv_get_number_chk(&argvars[1], NULL);
2499 if (str == NULL || idx < 0)
2500 return;
2501
2502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002503 countcc = tv_get_bool(&argvars[2]);
Bram Moolenaar17793ef2020-12-28 12:56:58 +01002504 if (countcc < 0 || countcc > 1)
2505 {
2506 semsg(_(e_using_number_as_bool_nr), countcc);
2507 return;
2508 }
2509
2510 if (enc_utf8 && countcc)
2511 ptr2len = utf_ptr2len;
2512 else
2513 ptr2len = mb_ptr2len;
2514
2515 for (p = str, len = 0; p <= str + idx; len++)
2516 {
2517 if (*p == NUL)
2518 return;
2519 p += ptr2len(p);
2520 }
2521
2522 rettv->vval.v_number = len > 0 ? len - 1 : 0;
2523}
2524
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002525 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002526get_optional_window(typval_T *argvars, int idx)
2527{
2528 win_T *win = curwin;
2529
2530 if (argvars[idx].v_type != VAR_UNKNOWN)
2531 {
2532 win = find_win_by_nr_or_id(&argvars[idx]);
2533 if (win == NULL)
2534 {
2535 emsg(_(e_invalwindow));
2536 return NULL;
2537 }
2538 }
2539 return win;
2540}
2541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 * "col(string)" function
2544 */
2545 static void
2546f_col(typval_T *argvars, typval_T *rettv)
2547{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002548 get_col(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549}
2550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551/*
2552 * "confirm(message, buttons[, default [, type]])" function
2553 */
2554 static void
2555f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2556{
2557#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2558 char_u *message;
2559 char_u *buttons = NULL;
2560 char_u buf[NUMBUFLEN];
2561 char_u buf2[NUMBUFLEN];
2562 int def = 1;
2563 int type = VIM_GENERIC;
2564 char_u *typestr;
2565 int error = FALSE;
2566
Bram Moolenaarc5809432021-03-27 21:23:30 +01002567 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
2568 return;
2569
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002570 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 if (message == NULL)
2572 error = TRUE;
2573 if (argvars[1].v_type != VAR_UNKNOWN)
2574 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002575 if (in_vim9script() && check_for_string_arg(argvars, 1) == FAIL)
2576 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002577 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 if (buttons == NULL)
2579 error = TRUE;
2580 if (argvars[2].v_type != VAR_UNKNOWN)
2581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002582 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 if (argvars[3].v_type != VAR_UNKNOWN)
2584 {
Bram Moolenaarc5809432021-03-27 21:23:30 +01002585 if (in_vim9script() && check_for_string_arg(argvars, 3) == FAIL)
2586 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002587 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002588 if (typestr == NULL)
2589 error = TRUE;
2590 else
2591 {
2592 switch (TOUPPER_ASC(*typestr))
2593 {
2594 case 'E': type = VIM_ERROR; break;
2595 case 'Q': type = VIM_QUESTION; break;
2596 case 'I': type = VIM_INFO; break;
2597 case 'W': type = VIM_WARNING; break;
2598 case 'G': type = VIM_GENERIC; break;
2599 }
2600 }
2601 }
2602 }
2603 }
2604
2605 if (buttons == NULL || *buttons == NUL)
2606 buttons = (char_u *)_("&Ok");
2607
2608 if (!error)
2609 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2610 def, NULL, FALSE);
2611#endif
2612}
2613
2614/*
2615 * "copy()" function
2616 */
2617 static void
2618f_copy(typval_T *argvars, typval_T *rettv)
2619{
2620 item_copy(&argvars[0], rettv, FALSE, 0);
2621}
2622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002623/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002624 * Set the cursor position.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002625 * If 'charcol' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002626 * Otherwise use the column number as a byte offset.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 */
2628 static void
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002629set_cursorpos(typval_T *argvars, typval_T *rettv, int charcol)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630{
2631 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002633 int set_curswant = TRUE;
2634
2635 rettv->vval.v_number = -1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002636 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637 {
2638 pos_T pos;
2639 colnr_T curswant = -1;
2640
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002641 if (list2fpos(argvars, &pos, NULL, &curswant, charcol) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002643 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002644 return;
2645 }
2646 line = pos.lnum;
2647 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002649 if (curswant >= 0)
2650 {
2651 curwin->w_curswant = curswant - 1;
2652 set_curswant = FALSE;
2653 }
2654 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002655 else if ((argvars[0].v_type == VAR_NUMBER ||
2656 argvars[0].v_type == VAR_STRING)
Bram Moolenaar9ebcf232021-01-16 16:52:49 +01002657 && (argvars[1].v_type == VAR_NUMBER ||
2658 argvars[1].v_type == VAR_STRING))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002660 line = tv_get_lnum(argvars);
Bram Moolenaar9a963372020-12-21 21:58:46 +01002661 if (line < 0)
2662 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002663 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002664 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01002665 col = buf_charidx_to_byteidx(curbuf, line, col) + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002667 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 }
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002669 else
2670 {
2671 emsg(_(e_invarg));
2672 return;
2673 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002674 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002675 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676 if (line > 0)
2677 curwin->w_cursor.lnum = line;
2678 if (col > 0)
2679 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 // Make sure the cursor is in a valid position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 check_cursor();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002684 // Correct cursor for multi-byte character.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 if (has_mbyte)
2686 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687
2688 curwin->w_set_curswant = set_curswant;
2689 rettv->vval.v_number = 0;
2690}
2691
Bram Moolenaar6f02b002021-01-10 20:22:54 +01002692/*
2693 * "cursor(lnum, col)" function, or
2694 * "cursor(list)"
2695 *
2696 * Moves the cursor to the specified line and column.
2697 * Returns 0 when the position could be set, -1 otherwise.
2698 */
2699 static void
2700f_cursor(typval_T *argvars, typval_T *rettv)
2701{
2702 set_cursorpos(argvars, rettv, FALSE);
2703}
2704
Bram Moolenaar4f974752019-02-17 17:44:42 +01002705#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002706/*
2707 * "debugbreak()" function
2708 */
2709 static void
2710f_debugbreak(typval_T *argvars, typval_T *rettv)
2711{
2712 int pid;
2713
2714 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002716 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002717 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002718 else
2719 {
2720 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2721
2722 if (hProcess != NULL)
2723 {
2724 DebugBreakProcess(hProcess);
2725 CloseHandle(hProcess);
2726 rettv->vval.v_number = OK;
2727 }
2728 }
2729}
2730#endif
2731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002732/*
2733 * "deepcopy()" function
2734 */
2735 static void
2736f_deepcopy(typval_T *argvars, typval_T *rettv)
2737{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002738 varnumber_T noref = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 int copyID;
2740
2741 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002742 noref = tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 if (noref < 0 || noref > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02002744 semsg(_(e_using_number_as_bool_nr), noref);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002745 else
2746 {
2747 copyID = get_copyID();
2748 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2749 }
2750}
2751
2752/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 * "did_filetype()" function
2754 */
2755 static void
2756f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2757{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759}
2760
2761/*
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002762 * "echoraw({expr})" function
2763 */
2764 static void
2765f_echoraw(typval_T *argvars, typval_T *rettv UNUSED)
2766{
2767 char_u *str = tv_get_string_chk(&argvars[0]);
2768
2769 if (str != NULL && *str != NUL)
2770 {
2771 out_str(str);
2772 out_flush();
2773 }
2774}
2775
2776/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 * "empty({expr})" function
2778 */
2779 static void
2780f_empty(typval_T *argvars, typval_T *rettv)
2781{
2782 int n = FALSE;
2783
2784 switch (argvars[0].v_type)
2785 {
2786 case VAR_STRING:
2787 case VAR_FUNC:
2788 n = argvars[0].vval.v_string == NULL
2789 || *argvars[0].vval.v_string == NUL;
2790 break;
2791 case VAR_PARTIAL:
2792 n = FALSE;
2793 break;
2794 case VAR_NUMBER:
2795 n = argvars[0].vval.v_number == 0;
2796 break;
2797 case VAR_FLOAT:
2798#ifdef FEAT_FLOAT
2799 n = argvars[0].vval.v_float == 0.0;
2800 break;
2801#endif
2802 case VAR_LIST:
2803 n = argvars[0].vval.v_list == NULL
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002804 || argvars[0].vval.v_list->lv_len == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 break;
2806 case VAR_DICT:
2807 n = argvars[0].vval.v_dict == NULL
2808 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2809 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002810 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811 case VAR_SPECIAL:
2812 n = argvars[0].vval.v_number != VVAL_TRUE;
2813 break;
2814
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002815 case VAR_BLOB:
2816 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002817 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2818 break;
2819
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820 case VAR_JOB:
2821#ifdef FEAT_JOB_CHANNEL
2822 n = argvars[0].vval.v_job == NULL
2823 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2824 break;
2825#endif
2826 case VAR_CHANNEL:
2827#ifdef FEAT_JOB_CHANNEL
2828 n = argvars[0].vval.v_channel == NULL
2829 || !channel_is_open(argvars[0].vval.v_channel);
2830 break;
2831#endif
2832 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002833 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002835 case VAR_INSTR:
Bram Moolenaardd589232020-02-29 17:38:12 +01002836 internal_error_no_abort("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002837 n = TRUE;
2838 break;
2839 }
2840
2841 rettv->vval.v_number = n;
2842}
2843
2844/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002845 * "environ()" function
2846 */
2847 static void
2848f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2849{
2850#if !defined(AMIGA)
2851 int i = 0;
2852 char_u *entry, *value;
2853# ifdef MSWIN
2854 extern wchar_t **_wenviron;
2855# else
2856 extern char **environ;
2857# endif
2858
2859 if (rettv_dict_alloc(rettv) != OK)
2860 return;
2861
2862# ifdef MSWIN
2863 if (*_wenviron == NULL)
2864 return;
2865# else
2866 if (*environ == NULL)
2867 return;
2868# endif
2869
2870 for (i = 0; ; ++i)
2871 {
2872# ifdef MSWIN
2873 short_u *p;
2874
2875 if ((p = (short_u *)_wenviron[i]) == NULL)
2876 return;
2877 entry = utf16_to_enc(p, NULL);
2878# else
2879 if ((entry = (char_u *)environ[i]) == NULL)
2880 return;
2881 entry = vim_strsave(entry);
2882# endif
2883 if (entry == NULL) // out of memory
2884 return;
2885 if ((value = vim_strchr(entry, '=')) == NULL)
2886 {
2887 vim_free(entry);
2888 continue;
2889 }
2890 *value++ = NUL;
2891 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2892 vim_free(entry);
2893 }
2894#endif
2895}
2896
2897/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898 * "escape({string}, {chars})" function
2899 */
2900 static void
2901f_escape(typval_T *argvars, typval_T *rettv)
2902{
2903 char_u buf[NUMBUFLEN];
2904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002905 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2906 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002907 rettv->v_type = VAR_STRING;
2908}
2909
2910/*
2911 * "eval()" function
2912 */
2913 static void
2914f_eval(typval_T *argvars, typval_T *rettv)
2915{
2916 char_u *s, *p;
2917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002918 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 if (s != NULL)
2920 s = skipwhite(s);
2921
2922 p = s;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923 if (s == NULL || eval1(&s, rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 {
2925 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 need_clr_eos = FALSE;
2928 rettv->v_type = VAR_NUMBER;
2929 rettv->vval.v_number = 0;
2930 }
2931 else if (*s != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002932 semsg(_(e_trailing_arg), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933}
2934
2935/*
2936 * "eventhandler()" function
2937 */
2938 static void
2939f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2940{
Bram Moolenaardfc33a62020-04-29 22:30:13 +02002941 rettv->vval.v_number = vgetc_busy || input_busy;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942}
2943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944static garray_T redir_execute_ga;
2945
2946/*
2947 * Append "value[value_len]" to the execute() output.
2948 */
2949 void
2950execute_redir_str(char_u *value, int value_len)
2951{
2952 int len;
2953
2954 if (value_len == -1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 len = (int)STRLEN(value); // Append the entire string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002957 len = value_len; // Append only "value_len" characters
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958 if (ga_grow(&redir_execute_ga, len) == OK)
2959 {
2960 mch_memmove((char *)redir_execute_ga.ga_data
2961 + redir_execute_ga.ga_len, value, len);
2962 redir_execute_ga.ga_len += len;
2963 }
2964}
2965
2966/*
2967 * Get next line from a list.
2968 * Called by do_cmdline() to get the next line.
2969 * Returns allocated string, or NULL for end of function.
2970 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 static char_u *
2972get_list_line(
2973 int c UNUSED,
2974 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002975 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02002976 getline_opt_T options UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002977{
2978 listitem_T **p = (listitem_T **)cookie;
2979 listitem_T *item = *p;
2980 char_u buf[NUMBUFLEN];
2981 char_u *s;
2982
2983 if (item == NULL)
2984 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002985 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 *p = item->li_next;
2987 return s == NULL ? NULL : vim_strsave(s);
2988}
2989
2990/*
2991 * "execute()" function
2992 */
Bram Moolenaar261f3462019-09-07 15:45:32 +02002993 void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002994execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995{
2996 char_u *cmd = NULL;
2997 list_T *list = NULL;
2998 int save_msg_silent = msg_silent;
2999 int save_emsg_silent = emsg_silent;
3000 int save_emsg_noredir = emsg_noredir;
3001 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003002 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003004 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003005 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006
3007 rettv->vval.v_string = NULL;
3008 rettv->v_type = VAR_STRING;
3009
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003010 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003012 list = argvars[arg_off].vval.v_list;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003013 if (list == NULL || list->lv_len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 // empty list, no commands, empty output
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 return;
3016 ++list->lv_refcount;
3017 }
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003018 else if (argvars[arg_off].v_type == VAR_JOB
3019 || argvars[arg_off].v_type == VAR_CHANNEL)
3020 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02003021 semsg(_(e_using_invalid_value_as_string_str),
3022 vartype_name(argvars[arg_off].v_type));
Bram Moolenaare2a8f072020-01-08 19:32:18 +01003023 return;
3024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 else
3026 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003027 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 if (cmd == NULL)
3029 return;
3030 }
3031
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003032 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 {
3034 char_u buf[NUMBUFLEN];
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02003035 char_u *s = tv_get_string_buf_chk_strict(&argvars[arg_off + 1], buf,
3036 in_vim9script());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037
3038 if (s == NULL)
3039 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003040 if (*s == NUL)
3041 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 if (STRNCMP(s, "silent", 6) == 0)
3043 ++msg_silent;
3044 if (STRCMP(s, "silent!") == 0)
3045 {
3046 emsg_silent = TRUE;
3047 emsg_noredir = TRUE;
3048 }
3049 }
3050 else
3051 ++msg_silent;
3052
3053 if (redir_execute)
3054 save_ga = redir_execute_ga;
3055 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3056 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003057 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003058 if (!echo_output)
3059 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060
3061 if (cmd != NULL)
3062 do_cmdline_cmd(cmd);
3063 else
3064 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003065 listitem_T *item;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003067 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003068 item = list->lv_first;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003069 do_cmdline(NULL, get_list_line, (void *)&item,
3070 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3071 --list->lv_refcount;
3072 }
3073
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003074 // Need to append a NUL to the result.
Bram Moolenaard297f352017-01-29 20:31:21 +01003075 if (ga_grow(&redir_execute_ga, 1) == OK)
3076 {
3077 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3078 rettv->vval.v_string = redir_execute_ga.ga_data;
3079 }
3080 else
3081 {
3082 ga_clear(&redir_execute_ga);
3083 rettv->vval.v_string = NULL;
3084 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 msg_silent = save_msg_silent;
3086 emsg_silent = save_emsg_silent;
3087 emsg_noredir = save_emsg_noredir;
3088
3089 redir_execute = save_redir_execute;
3090 if (redir_execute)
3091 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003092 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003093
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003094 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003095 if (echo_output)
3096 // When not working silently: put it in column zero. A following
3097 // "echon" will overwrite the message, unavoidably.
3098 msg_col = 0;
3099 else
3100 // When working silently: Put it back where it was, since nothing
3101 // should have been written.
3102 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103}
3104
3105/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003106 * "execute()" function
3107 */
3108 static void
3109f_execute(typval_T *argvars, typval_T *rettv)
3110{
3111 execute_common(argvars, rettv, 0);
3112}
3113
3114/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 * "exists()" function
3116 */
3117 static void
3118f_exists(typval_T *argvars, typval_T *rettv)
3119{
3120 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 p = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003124 if (*p == '$') // environment variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003126 // first try "normal" environment variables (fast)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 if (mch_getenv(p + 1) != NULL)
3128 n = TRUE;
3129 else
3130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003131 // try expanding things like $VIM and ${HOME}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132 p = expand_env_save(p);
3133 if (p != NULL && *p != '$')
3134 n = TRUE;
3135 vim_free(p);
3136 }
3137 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003138 else if (*p == '&' || *p == '+') // option
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003139 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003140 n = (eval_option(&p, NULL, TRUE) == OK);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 if (*skipwhite(p) != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003142 n = FALSE; // trailing garbage
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003144 else if (*p == '*') // internal or user defined function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003145 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003146 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003147 }
Bram Moolenaar15c47602020-03-26 22:16:48 +01003148 else if (*p == '?') // internal function only
3149 {
3150 n = has_internal_func_name(p + 1);
3151 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003152 else if (*p == ':')
3153 {
3154 n = cmd_exists(p + 1);
3155 }
3156 else if (*p == '#')
3157 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 if (p[1] == '#')
3159 n = autocmd_supported(p + 2);
3160 else
3161 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003162 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003163 else // internal variable
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003164 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003165 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 }
3167
3168 rettv->vval.v_number = n;
3169}
3170
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003171/*
3172 * "expand()" function
3173 */
3174 static void
3175f_expand(typval_T *argvars, typval_T *rettv)
3176{
3177 char_u *s;
3178 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003179 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003180 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3181 expand_T xpc;
3182 int error = FALSE;
3183 char_u *result;
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003184#ifdef BACKSLASH_IN_FILENAME
3185 char_u *p_csl_save = p_csl;
3186
3187 // avoid using 'completeslash' here
3188 p_csl = empty_option;
3189#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190
3191 rettv->v_type = VAR_STRING;
3192 if (argvars[1].v_type != VAR_UNKNOWN
3193 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003194 && tv_get_bool_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003195 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003196 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003198 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199 if (*s == '%' || *s == '#' || *s == '<')
3200 {
3201 ++emsg_off;
3202 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3203 --emsg_off;
3204 if (rettv->v_type == VAR_LIST)
3205 {
3206 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3207 list_append_string(rettv->vval.v_list, result, -1);
Bram Moolenaar86173482019-10-01 17:02:16 +02003208 vim_free(result);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209 }
3210 else
3211 rettv->vval.v_string = result;
3212 }
3213 else
3214 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003215 // When the optional second argument is non-zero, don't remove matches
3216 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar551d25e2020-09-02 21:37:56 +02003218 && tv_get_bool_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003219 options |= WILD_KEEP_ALL;
3220 if (!error)
3221 {
3222 ExpandInit(&xpc);
3223 xpc.xp_context = EXPAND_FILES;
3224 if (p_wic)
3225 options += WILD_ICASE;
3226 if (rettv->v_type == VAR_STRING)
3227 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3228 options, WILD_ALL);
3229 else if (rettv_list_alloc(rettv) != FAIL)
3230 {
3231 int i;
3232
3233 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3234 for (i = 0; i < xpc.xp_numfiles; i++)
3235 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3236 ExpandCleanup(&xpc);
3237 }
3238 }
3239 else
3240 rettv->vval.v_string = NULL;
3241 }
Bram Moolenaar8f187fc2020-09-26 18:47:11 +02003242#ifdef BACKSLASH_IN_FILENAME
3243 p_csl = p_csl_save;
3244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245}
3246
3247/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003248 * "expandcmd()" function
3249 * Expand all the special characters in a command string.
3250 */
3251 static void
3252f_expandcmd(typval_T *argvars, typval_T *rettv)
3253{
3254 exarg_T eap;
3255 char_u *cmdstr;
3256 char *errormsg = NULL;
3257
3258 rettv->v_type = VAR_STRING;
3259 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3260
3261 memset(&eap, 0, sizeof(eap));
3262 eap.cmd = cmdstr;
3263 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003264 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003265 eap.usefilter = FALSE;
3266 eap.nextcmd = NULL;
3267 eap.cmdidx = CMD_USER;
3268
3269 expand_filename(&eap, &cmdstr, &errormsg);
3270 if (errormsg != NULL && *errormsg != NUL)
3271 emsg(errormsg);
3272
3273 rettv->vval.v_string = cmdstr;
3274}
3275
3276/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 * "feedkeys()" function
3278 */
3279 static void
3280f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3281{
3282 int remap = TRUE;
3283 int insert = FALSE;
3284 char_u *keys, *flags;
3285 char_u nbuf[NUMBUFLEN];
3286 int typed = FALSE;
3287 int execute = FALSE;
3288 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003289 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 char_u *keys_esc;
3291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003292 // This is not allowed in the sandbox. If the commands would still be
3293 // executed in the sandbox it would be OK, but it probably happens later,
3294 // when "sandbox" is no longer set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003295 if (check_secure())
3296 return;
3297
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003298 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299
3300 if (argvars[1].v_type != VAR_UNKNOWN)
3301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003302 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 for ( ; *flags != NUL; ++flags)
3304 {
3305 switch (*flags)
3306 {
3307 case 'n': remap = FALSE; break;
3308 case 'm': remap = TRUE; break;
3309 case 't': typed = TRUE; break;
3310 case 'i': insert = TRUE; break;
3311 case 'x': execute = TRUE; break;
3312 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003313 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 }
3315 }
3316 }
3317
3318 if (*keys != NUL || execute)
3319 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003320 // Need to escape K_SPECIAL and CSI before putting the string in the
3321 // typeahead buffer.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322 keys_esc = vim_strsave_escape_csi(keys);
3323 if (keys_esc != NULL)
3324 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003325 if (lowlevel)
3326 {
3327#ifdef USE_INPUT_BUF
Bram Moolenaar9645e2d2020-03-20 20:48:49 +01003328 int idx;
3329 int len = (int)STRLEN(keys);
3330
3331 for (idx = 0; idx < len; ++idx)
3332 {
3333 // if a CTRL-C was typed, set got_int, similar to what
3334 // happens in fill_input_buf()
3335 if (keys[idx] == 3 && ctrl_c_interrupts && typed)
3336 got_int = TRUE;
3337 add_to_input_buf(keys + idx, 1);
3338 }
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003339#else
3340 emsg(_("E980: lowlevel input not supported"));
3341#endif
3342 }
3343 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003344 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003345 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003347 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003348#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003349 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003350#endif
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003351 || input_busy)
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003352 typebuf_was_filled = TRUE;
3353 }
3354 vim_free(keys_esc);
3355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 if (execute)
3357 {
3358 int save_msg_scroll = msg_scroll;
3359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // Avoid a 1 second delay when the keys start Insert mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003361 msg_scroll = FALSE;
3362
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003363 if (!dangerous)
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003364 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003365 ++ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003366 ++in_feedkeys;
3367 }
Bram Moolenaar905dd902019-04-07 14:21:47 +02003368 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003369 if (!dangerous)
Bram Moolenaar189832b2020-09-23 12:29:11 +02003370 {
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003371 --ex_normal_busy;
Bram Moolenaar4934ad02020-09-28 22:29:58 +02003372 --in_feedkeys;
Bram Moolenaar189832b2020-09-23 12:29:11 +02003373 }
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003375 msg_scroll |= save_msg_scroll;
3376 }
3377 }
3378 }
3379}
3380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003381/*
3382 * "fnameescape({string})" function
3383 */
3384 static void
3385f_fnameescape(typval_T *argvars, typval_T *rettv)
3386{
3387 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003388 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003389 rettv->v_type = VAR_STRING;
3390}
3391
3392/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 * "foreground()" function
3394 */
3395 static void
3396f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3397{
3398#ifdef FEAT_GUI
3399 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003400 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003402 return;
3403 }
3404#endif
3405#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003406 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003407#endif
3408}
3409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003411common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003412{
3413 char_u *s;
3414 char_u *name;
3415 int use_string = FALSE;
3416 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003417 char_u *trans_name = NULL;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003418 int is_global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003419
3420 if (argvars[0].v_type == VAR_FUNC)
3421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003422 // function(MyFunc, [arg], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423 s = argvars[0].vval.v_string;
3424 }
3425 else if (argvars[0].v_type == VAR_PARTIAL
3426 && argvars[0].vval.v_partial != NULL)
3427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003428 // function(dict.MyFunc, [arg])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003430 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 }
3432 else
3433 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003434 // function('MyFunc', [arg], dict)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 use_string = TRUE;
3437 }
3438
Bram Moolenaar843b8842016-08-21 14:36:15 +02003439 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003440 {
3441 name = s;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003442 trans_name = trans_function_name(&name, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003443 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF,
3444 NULL, NULL, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003445 if (*name != NUL)
3446 s = NULL;
3447 }
3448
Bram Moolenaar843b8842016-08-21 14:36:15 +02003449 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3450 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003451 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003452 // Don't check an autoload name for existence here.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003453 else if (trans_name != NULL && (is_funcref
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003454 ? find_func(trans_name, is_global, NULL) == NULL
3455 : !translated_function_exists(trans_name, is_global)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003456 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 else
3458 {
3459 int dict_idx = 0;
3460 int arg_idx = 0;
3461 list_T *list = NULL;
3462
3463 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3464 {
3465 char sid_buf[25];
3466 int off = *s == 's' ? 2 : 5;
3467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003468 // Expand s: and <SID> into <SNR>nr_, so that the function can
3469 // also be called from another script. Using trans_function_name()
3470 // would also work, but some plugins depend on the name being
3471 // printable text.
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003472 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003473 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003474 if (name != NULL)
3475 {
3476 STRCPY(name, sid_buf);
3477 STRCAT(name, s + off);
3478 }
3479 }
3480 else
3481 name = vim_strsave(s);
3482
3483 if (argvars[1].v_type != VAR_UNKNOWN)
3484 {
3485 if (argvars[2].v_type != VAR_UNKNOWN)
3486 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 // function(name, [args], dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003488 arg_idx = 1;
3489 dict_idx = 2;
3490 }
3491 else if (argvars[1].v_type == VAR_DICT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // function(name, dict)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003493 dict_idx = 1;
3494 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003495 // function(name, [args])
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003496 arg_idx = 1;
3497 if (dict_idx > 0)
3498 {
3499 if (argvars[dict_idx].v_type != VAR_DICT)
3500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003501 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003503 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003504 }
3505 if (argvars[dict_idx].vval.v_dict == NULL)
3506 dict_idx = 0;
3507 }
3508 if (arg_idx > 0)
3509 {
3510 if (argvars[arg_idx].v_type != VAR_LIST)
3511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003512 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003514 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515 }
3516 list = argvars[arg_idx].vval.v_list;
3517 if (list == NULL || list->lv_len == 0)
3518 arg_idx = 0;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003519 else if (list->lv_len > MAX_FUNC_ARGS)
3520 {
Bram Moolenaar2118a302019-11-22 19:29:45 +01003521 emsg_funcname((char *)e_toomanyarg, s);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003522 vim_free(name);
3523 goto theend;
3524 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525 }
3526 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003527 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003529 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003531 // result is a VAR_PARTIAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (pt == NULL)
3533 vim_free(name);
3534 else
3535 {
3536 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3537 {
3538 listitem_T *li;
3539 int i = 0;
3540 int arg_len = 0;
3541 int lv_len = 0;
3542
3543 if (arg_pt != NULL)
3544 arg_len = arg_pt->pt_argc;
3545 if (list != NULL)
3546 lv_len = list->lv_len;
3547 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003548 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 if (pt->pt_argv == NULL)
3550 {
3551 vim_free(pt);
3552 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003553 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003555 for (i = 0; i < arg_len; i++)
3556 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3557 if (lv_len > 0)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003558 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003559 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003560 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003561 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003563 }
3564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003565 // For "function(dict.func, [], dict)" and "func" is a partial
3566 // use "dict". That is backwards compatible.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 if (dict_idx > 0)
3568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003569 // The dict is bound explicitly, pt_auto is FALSE.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3571 ++pt->pt_dict->dv_refcount;
3572 }
3573 else if (arg_pt != NULL)
3574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003575 // If the dict was bound automatically the result is also
3576 // bound automatically.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577 pt->pt_dict = arg_pt->pt_dict;
3578 pt->pt_auto = arg_pt->pt_auto;
3579 if (pt->pt_dict != NULL)
3580 ++pt->pt_dict->dv_refcount;
3581 }
3582
3583 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003584 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3585 {
3586 pt->pt_func = arg_pt->pt_func;
3587 func_ptr_ref(pt->pt_func);
3588 vim_free(name);
3589 }
3590 else if (is_funcref)
3591 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003592 pt->pt_func = find_func(trans_name, is_global, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003593 func_ptr_ref(pt->pt_func);
3594 vim_free(name);
3595 }
3596 else
3597 {
3598 pt->pt_name = name;
3599 func_ref(name);
3600 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003601 }
3602 rettv->v_type = VAR_PARTIAL;
3603 rettv->vval.v_partial = pt;
3604 }
3605 else
3606 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 // result is a VAR_FUNC
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 rettv->v_type = VAR_FUNC;
3609 rettv->vval.v_string = name;
3610 func_ref(name);
3611 }
3612 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613theend:
3614 vim_free(trans_name);
3615}
3616
3617/*
3618 * "funcref()" function
3619 */
3620 static void
3621f_funcref(typval_T *argvars, typval_T *rettv)
3622{
3623 common_function(argvars, rettv, TRUE);
3624}
3625
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003626 static type_T *
Bram Moolenaardfc33a62020-04-29 22:30:13 +02003627ret_f_function(int argcount, type_T **argtypes)
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003628{
3629 if (argcount == 1 && argtypes[0]->tt_type == VAR_STRING)
3630 return &t_func_any;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003631 return &t_func_unknown;
Bram Moolenaarfbdd08e2020-03-01 14:04:46 +01003632}
3633
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003634/*
3635 * "function()" function
3636 */
3637 static void
3638f_function(typval_T *argvars, typval_T *rettv)
3639{
3640 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641}
3642
3643/*
3644 * "garbagecollect()" function
3645 */
3646 static void
3647f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3648{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // This is postponed until we are back at the toplevel, because we may be
3650 // using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003651 want_garbage_collect = TRUE;
3652
Bram Moolenaar2df47312020-09-05 17:30:44 +02003653 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 garbage_collect_at_exit = TRUE;
3655}
3656
3657/*
3658 * "get()" function
3659 */
3660 static void
3661f_get(typval_T *argvars, typval_T *rettv)
3662{
3663 listitem_T *li;
3664 list_T *l;
3665 dictitem_T *di;
3666 dict_T *d;
3667 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003668 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003670 if (argvars[0].v_type == VAR_BLOB)
3671 {
3672 int error = FALSE;
3673 int idx = tv_get_number_chk(&argvars[1], &error);
3674
3675 if (!error)
3676 {
3677 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003678 if (idx < 0)
3679 idx = blob_len(argvars[0].vval.v_blob) + idx;
3680 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
3681 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003682 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003683 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003684 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01003685 tv = rettv;
3686 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003687 }
3688 }
3689 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 {
3691 if ((l = argvars[0].vval.v_list) != NULL)
3692 {
3693 int error = FALSE;
3694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003695 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003696 if (!error && li != NULL)
3697 tv = &li->li_tv;
3698 }
3699 }
3700 else if (argvars[0].v_type == VAR_DICT)
3701 {
3702 if ((d = argvars[0].vval.v_dict) != NULL)
3703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003704 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 if (di != NULL)
3706 tv = &di->di_tv;
3707 }
3708 }
3709 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3710 {
3711 partial_T *pt;
3712 partial_T fref_pt;
3713
3714 if (argvars[0].v_type == VAR_PARTIAL)
3715 pt = argvars[0].vval.v_partial;
3716 else
3717 {
Bram Moolenaara80faa82020-04-12 19:37:17 +02003718 CLEAR_FIELD(fref_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003719 fref_pt.pt_name = argvars[0].vval.v_string;
3720 pt = &fref_pt;
3721 }
3722
3723 if (pt != NULL)
3724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003725 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003726 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727
3728 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3729 {
3730 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003731 n = partial_name(pt);
3732 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 rettv->vval.v_string = NULL;
3734 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735 {
3736 rettv->vval.v_string = vim_strsave(n);
3737 if (rettv->v_type == VAR_FUNC)
3738 func_ref(rettv->vval.v_string);
3739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 }
3741 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003742 {
3743 what_is_dict = TRUE;
3744 if (pt->pt_dict != NULL)
3745 rettv_dict_set(rettv, pt->pt_dict);
3746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 else if (STRCMP(what, "args") == 0)
3748 {
3749 rettv->v_type = VAR_LIST;
3750 if (rettv_list_alloc(rettv) == OK)
3751 {
3752 int i;
3753
3754 for (i = 0; i < pt->pt_argc; ++i)
3755 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3756 }
3757 }
3758 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003759 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02003760
3761 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
3762 // third argument
3763 if (!what_is_dict)
3764 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003765 }
3766 }
3767 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01003768 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769
3770 if (tv == NULL)
3771 {
3772 if (argvars[2].v_type != VAR_UNKNOWN)
3773 copy_tv(&argvars[2], rettv);
3774 }
3775 else
3776 copy_tv(tv, rettv);
3777}
3778
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003779/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003780 * "getchangelist()" function
3781 */
3782 static void
3783f_getchangelist(typval_T *argvars, typval_T *rettv)
3784{
3785#ifdef FEAT_JUMPLIST
3786 buf_T *buf;
3787 int i;
3788 list_T *l;
3789 dict_T *d;
3790#endif
3791
3792 if (rettv_list_alloc(rettv) != OK)
3793 return;
3794
3795#ifdef FEAT_JUMPLIST
Bram Moolenaar4c313b12019-08-24 22:58:31 +02003796 if (argvars[0].v_type == VAR_UNKNOWN)
3797 buf = curbuf;
3798 else
Bram Moolenaara5d38412020-09-02 21:02:35 +02003799 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003800 if (buf == NULL)
3801 return;
3802
3803 l = list_alloc();
3804 if (l == NULL)
3805 return;
3806
3807 if (list_append_list(rettv->vval.v_list, l) == FAIL)
3808 return;
3809 /*
3810 * The current window change list index tracks only the position in the
3811 * current buffer change list. For other buffers, use the change list
3812 * length as the current index.
3813 */
3814 list_append_number(rettv->vval.v_list,
3815 (varnumber_T)((buf == curwin->w_buffer)
3816 ? curwin->w_changelistidx : buf->b_changelistlen));
3817
3818 for (i = 0; i < buf->b_changelistlen; ++i)
3819 {
3820 if (buf->b_changelist[i].lnum == 0)
3821 continue;
3822 if ((d = dict_alloc()) == NULL)
3823 return;
3824 if (list_append_dict(l, d) == FAIL)
3825 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02003826 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
3827 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02003828 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01003829 }
3830#endif
3831}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003833 static void
3834getpos_both(
3835 typval_T *argvars,
3836 typval_T *rettv,
3837 int getcurpos,
3838 int charcol)
3839{
3840 pos_T *fp = NULL;
3841 pos_T pos;
3842 win_T *wp = curwin;
3843 list_T *l;
3844 int fnum = -1;
3845
3846 if (rettv_list_alloc(rettv) == OK)
3847 {
3848 l = rettv->vval.v_list;
3849 if (getcurpos)
3850 {
3851 if (argvars[0].v_type != VAR_UNKNOWN)
3852 {
3853 wp = find_win_by_nr_or_id(&argvars[0]);
3854 if (wp != NULL)
3855 fp = &wp->w_cursor;
3856 }
3857 else
3858 fp = &curwin->w_cursor;
3859 if (fp != NULL && charcol)
3860 {
3861 pos = *fp;
Bram Moolenaar91458462021-01-13 20:08:38 +01003862 pos.col =
3863 buf_byteidx_to_charidx(wp->w_buffer, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01003864 fp = &pos;
3865 }
3866 }
3867 else
3868 fp = var2fpos(&argvars[0], TRUE, &fnum, charcol);
3869 if (fnum != -1)
3870 list_append_number(l, (varnumber_T)fnum);
3871 else
3872 list_append_number(l, (varnumber_T)0);
3873 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
3874 : (varnumber_T)0);
3875 list_append_number(l, (fp != NULL)
3876 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
3877 : (varnumber_T)0);
3878 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
3879 (varnumber_T)0);
3880 if (getcurpos)
3881 {
3882 int save_set_curswant = curwin->w_set_curswant;
3883 colnr_T save_curswant = curwin->w_curswant;
3884 colnr_T save_virtcol = curwin->w_virtcol;
3885
3886 if (wp == curwin)
3887 update_curswant();
3888 list_append_number(l, wp == NULL ? 0 : wp->w_curswant == MAXCOL
3889 ? (varnumber_T)MAXCOL : (varnumber_T)wp->w_curswant + 1);
3890
3891 // Do not change "curswant", as it is unexpected that a get
3892 // function has a side effect.
3893 if (wp == curwin && save_set_curswant)
3894 {
3895 curwin->w_set_curswant = save_set_curswant;
3896 curwin->w_curswant = save_curswant;
3897 curwin->w_virtcol = save_virtcol;
3898 curwin->w_valid &= ~VALID_VIRTCOL;
3899 }
3900 }
3901 }
3902 else
3903 rettv->vval.v_number = FALSE;
3904}
3905
3906/*
3907 * "getcharpos()" function
3908 */
3909 static void
3910f_getcharpos(typval_T *argvars UNUSED, typval_T *rettv)
3911{
3912 getpos_both(argvars, rettv, FALSE, TRUE);
3913}
3914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003915/*
3916 * "getcharsearch()" function
3917 */
3918 static void
3919f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
3920{
3921 if (rettv_dict_alloc(rettv) != FAIL)
3922 {
3923 dict_T *dict = rettv->vval.v_dict;
3924
Bram Moolenaare0be1672018-07-08 16:50:37 +02003925 dict_add_string(dict, "char", last_csearch());
3926 dict_add_number(dict, "forward", last_csearch_forward());
3927 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003928 }
3929}
3930
3931/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003932 * "getenv()" function
3933 */
3934 static void
3935f_getenv(typval_T *argvars, typval_T *rettv)
3936{
3937 int mustfree = FALSE;
3938 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
3939
3940 if (p == NULL)
3941 {
3942 rettv->v_type = VAR_SPECIAL;
3943 rettv->vval.v_number = VVAL_NULL;
3944 return;
3945 }
3946 if (!mustfree)
3947 p = vim_strsave(p);
3948 rettv->vval.v_string = p;
3949 rettv->v_type = VAR_STRING;
3950}
3951
3952/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 * "getfontname()" function
3954 */
3955 static void
3956f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
3957{
3958 rettv->v_type = VAR_STRING;
3959 rettv->vval.v_string = NULL;
3960#ifdef FEAT_GUI
3961 if (gui.in_use)
3962 {
3963 GuiFont font;
3964 char_u *name = NULL;
3965
3966 if (argvars[0].v_type == VAR_UNKNOWN)
3967 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003968 // Get the "Normal" font. Either the name saved by
3969 // hl_set_font_name() or from the font ID.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003970 font = gui.norm_font;
3971 name = hl_get_font_name();
3972 }
3973 else
3974 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003975 name = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003976 if (STRCMP(name, "*") == 0) // don't use font dialog
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 return;
3978 font = gui_mch_get_font(name, FALSE);
3979 if (font == NOFONT)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003980 return; // Invalid font name, return empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981 }
3982 rettv->vval.v_string = gui_mch_get_fontname(font, name);
3983 if (argvars[0].v_type != VAR_UNKNOWN)
3984 gui_mch_free_font(font);
3985 }
3986#endif
3987}
3988
3989/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01003990 * "getjumplist()" function
3991 */
3992 static void
3993f_getjumplist(typval_T *argvars, typval_T *rettv)
3994{
3995#ifdef FEAT_JUMPLIST
3996 win_T *wp;
3997 int i;
3998 list_T *l;
3999 dict_T *d;
4000#endif
4001
4002 if (rettv_list_alloc(rettv) != OK)
4003 return;
4004
4005#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004006 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004007 if (wp == NULL)
4008 return;
4009
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004010 cleanup_jumplist(wp, TRUE);
4011
Bram Moolenaar4f505882018-02-10 21:06:32 +01004012 l = list_alloc();
4013 if (l == NULL)
4014 return;
4015
4016 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4017 return;
4018 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4019
4020 for (i = 0; i < wp->w_jumplistlen; ++i)
4021 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004022 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4023 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004024 if ((d = dict_alloc()) == NULL)
4025 return;
4026 if (list_append_dict(l, d) == FAIL)
4027 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004028 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4029 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004030 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004031 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004032 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004033 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004034 }
4035#endif
4036}
4037
4038/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004039 * "getpid()" function
4040 */
4041 static void
4042f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4043{
4044 rettv->vval.v_number = mch_get_pid();
4045}
4046
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004047/*
4048 * "getcurpos()" function
4049 */
4050 static void
4051f_getcurpos(typval_T *argvars, typval_T *rettv)
4052{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004053 getpos_both(argvars, rettv, TRUE, FALSE);
4054}
4055
4056 static void
4057f_getcursorcharpos(typval_T *argvars, typval_T *rettv)
4058{
4059 getpos_both(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004060}
4061
4062/*
4063 * "getpos(string)" function
4064 */
4065 static void
4066f_getpos(typval_T *argvars, typval_T *rettv)
4067{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01004068 getpos_both(argvars, rettv, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069}
4070
4071/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072 * "getreg()" function
4073 */
4074 static void
4075f_getreg(typval_T *argvars, typval_T *rettv)
4076{
4077 char_u *strregname;
4078 int regname;
4079 int arg2 = FALSE;
4080 int return_list = FALSE;
4081 int error = FALSE;
4082
4083 if (argvars[0].v_type != VAR_UNKNOWN)
4084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004086 if (strregname == NULL)
4087 error = TRUE;
4088 else if (in_vim9script() && STRLEN(strregname) > 1)
4089 {
4090 semsg(_(e_register_name_must_be_one_char_str), strregname);
4091 error = TRUE;
4092 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 if (argvars[1].v_type != VAR_UNKNOWN)
4094 {
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004095 arg2 = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar67ff97d2020-09-02 21:45:54 +02004097 return_list = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004098 }
4099 }
4100 else
4101 strregname = get_vim_var_str(VV_REG);
4102
4103 if (error)
4104 return;
4105
4106 regname = (strregname == NULL ? '"' : *strregname);
4107 if (regname == 0)
4108 regname = '"';
4109
4110 if (return_list)
4111 {
4112 rettv->v_type = VAR_LIST;
4113 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4114 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4115 if (rettv->vval.v_list == NULL)
4116 (void)rettv_list_alloc(rettv);
4117 else
4118 ++rettv->vval.v_list->lv_refcount;
4119 }
4120 else
4121 {
4122 rettv->v_type = VAR_STRING;
4123 rettv->vval.v_string = get_reg_contents(regname,
4124 arg2 ? GREG_EXPR_SRC : 0);
4125 }
4126}
4127
4128/*
4129 * "getregtype()" function
4130 */
4131 static void
4132f_getregtype(typval_T *argvars, typval_T *rettv)
4133{
4134 char_u *strregname;
4135 int regname;
4136 char_u buf[NUMBUFLEN + 2];
4137 long reglen = 0;
4138
4139 if (argvars[0].v_type != VAR_UNKNOWN)
4140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004141 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar418a29f2021-02-10 22:23:41 +01004142 if (strregname != NULL && in_vim9script() && STRLEN(strregname) > 1)
4143 {
4144 semsg(_(e_register_name_must_be_one_char_str), strregname);
4145 strregname = NULL;
4146 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 if (strregname == NULL) // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 {
4149 rettv->v_type = VAR_STRING;
4150 rettv->vval.v_string = NULL;
4151 return;
4152 }
4153 }
4154 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004155 // Default to v:register
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156 strregname = get_vim_var_str(VV_REG);
4157
4158 regname = (strregname == NULL ? '"' : *strregname);
4159 if (regname == 0)
4160 regname = '"';
4161
4162 buf[0] = NUL;
4163 buf[1] = NUL;
4164 switch (get_reg_type(regname, &reglen))
4165 {
4166 case MLINE: buf[0] = 'V'; break;
4167 case MCHAR: buf[0] = 'v'; break;
4168 case MBLOCK:
4169 buf[0] = Ctrl_V;
4170 sprintf((char *)buf + 1, "%ld", reglen + 1);
4171 break;
4172 }
4173 rettv->v_type = VAR_STRING;
4174 rettv->vval.v_string = vim_strsave(buf);
4175}
4176
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004177/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01004178 * "gettagstack()" function
4179 */
4180 static void
4181f_gettagstack(typval_T *argvars, typval_T *rettv)
4182{
4183 win_T *wp = curwin; // default is current window
4184
4185 if (rettv_dict_alloc(rettv) != OK)
4186 return;
4187
4188 if (argvars[0].v_type != VAR_UNKNOWN)
4189 {
4190 wp = find_win_by_nr_or_id(&argvars[0]);
4191 if (wp == NULL)
4192 return;
4193 }
4194
4195 get_tagstack(wp, rettv->vval.v_dict);
4196}
4197
Bram Moolenaar0b39c3f2020-08-30 15:52:10 +02004198/*
4199 * "gettext()" function
4200 */
4201 static void
4202f_gettext(typval_T *argvars, typval_T *rettv)
4203{
4204 if (argvars[0].v_type != VAR_STRING
4205 || argvars[0].vval.v_string == NULL
4206 || *argvars[0].vval.v_string == NUL)
4207 {
4208 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
4209 }
4210 else
4211 {
4212 rettv->v_type = VAR_STRING;
4213 rettv->vval.v_string = vim_strsave(
4214 (char_u *)_(argvars[0].vval.v_string));
4215 }
4216}
4217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218// for VIM_VERSION_ defines
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004219#include "version.h"
4220
4221/*
4222 * "has()" function
4223 */
Bram Moolenaara259d8d2020-01-31 20:10:50 +01004224 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004225f_has(typval_T *argvars, typval_T *rettv)
4226{
4227 int i;
4228 char_u *name;
Bram Moolenaar79296512020-03-22 16:17:14 +01004229 int x = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230 int n = FALSE;
Bram Moolenaar79296512020-03-22 16:17:14 +01004231 typedef struct {
4232 char *name;
4233 short present;
4234 } has_item_T;
4235 static has_item_T has_list[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 {
Bram Moolenaar79296512020-03-22 16:17:14 +01004237 {"amiga",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238#ifdef AMIGA
Bram Moolenaar79296512020-03-22 16:17:14 +01004239 1
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004240#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004241 0
Bram Moolenaar39536dd2019-01-29 22:58:21 +01004242#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004243 },
4244 {"arp",
4245#if defined(AMIGA) && defined(FEAT_ARP)
4246 1
4247#else
4248 0
4249#endif
4250 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004251 {"haiku",
4252#ifdef __HAIKU__
4253 1
4254#else
4255 0
4256#endif
4257 },
4258 {"bsd",
4259#if defined(BSD) && !defined(MACOS_X)
4260 1
4261#else
4262 0
4263#endif
4264 },
4265 {"hpux",
4266#ifdef hpux
4267 1
4268#else
4269 0
4270#endif
4271 },
4272 {"linux",
4273#ifdef __linux__
4274 1
4275#else
4276 0
4277#endif
4278 },
4279 {"mac", // Mac OS X (and, once, Mac OS Classic)
4280#ifdef MACOS_X
4281 1
4282#else
4283 0
4284#endif
4285 },
4286 {"osx", // Mac OS X
4287#ifdef MACOS_X
4288 1
4289#else
4290 0
4291#endif
4292 },
4293 {"macunix", // Mac OS X, with the darwin feature
4294#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4295 1
4296#else
4297 0
4298#endif
4299 },
4300 {"osxdarwin", // synonym for macunix
4301#if defined(MACOS_X) && defined(MACOS_X_DARWIN)
4302 1
4303#else
4304 0
4305#endif
4306 },
4307 {"qnx",
4308#ifdef __QNX__
4309 1
4310#else
4311 0
4312#endif
4313 },
4314 {"sun",
4315#ifdef SUN_SYSTEM
4316 1
4317#else
4318 0
4319#endif
4320 },
4321 {"unix",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322#ifdef UNIX
Bram Moolenaar79296512020-03-22 16:17:14 +01004323 1
4324#else
4325 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004327 },
4328 {"vms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329#ifdef VMS
Bram Moolenaar79296512020-03-22 16:17:14 +01004330 1
4331#else
4332 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004333#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004334 },
4335 {"win32",
Bram Moolenaar4f974752019-02-17 17:44:42 +01004336#ifdef MSWIN
Bram Moolenaar79296512020-03-22 16:17:14 +01004337 1
4338#else
4339 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004340#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004341 },
4342 {"win32unix",
Bram Moolenaar1eed5322019-02-26 17:03:54 +01004343#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar79296512020-03-22 16:17:14 +01004344 1
4345#else
4346 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004348 },
4349 {"win64",
Bram Moolenaar44b443c2019-02-18 22:14:18 +01004350#ifdef _WIN64
Bram Moolenaar79296512020-03-22 16:17:14 +01004351 1
4352#else
4353 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004354#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004355 },
4356 {"ebcdic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357#ifdef EBCDIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004358 1
4359#else
4360 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004362 },
4363 {"fname_case",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364#ifndef CASE_INSENSITIVE_FILENAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004365 1
4366#else
4367 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004369 },
4370 {"acl",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371#ifdef HAVE_ACL
Bram Moolenaar79296512020-03-22 16:17:14 +01004372 1
4373#else
4374 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004376 },
4377 {"arabic",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004378#ifdef FEAT_ARABIC
Bram Moolenaar79296512020-03-22 16:17:14 +01004379 1
4380#else
4381 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004383 },
4384 {"autocmd", 1},
4385 {"autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004386#ifdef FEAT_AUTOCHDIR
Bram Moolenaar79296512020-03-22 16:17:14 +01004387 1
4388#else
4389 0
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02004390#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004391 },
4392 {"autoservername",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004393#ifdef FEAT_AUTOSERVERNAME
Bram Moolenaar79296512020-03-22 16:17:14 +01004394 1
4395#else
4396 0
Bram Moolenaare42a6d22017-11-12 19:21:51 +01004397#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004398 },
4399 {"balloon_eval",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004400#ifdef FEAT_BEVAL_GUI
Bram Moolenaar79296512020-03-22 16:17:14 +01004401 1
4402#else
4403 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004405 },
4406 {"balloon_multiline",
4407#if defined(FEAT_BEVAL_GUI) && !defined(FEAT_GUI_MSWIN)
4408 // MS-Windows requires runtime check, see below
4409 1
4410#else
4411 0
4412#endif
4413 },
4414 {"balloon_eval_term",
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004415#ifdef FEAT_BEVAL_TERM
Bram Moolenaar79296512020-03-22 16:17:14 +01004416 1
4417#else
4418 0
Bram Moolenaar51b0f372017-11-18 18:52:04 +01004419#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004420 },
4421 {"builtin_terms",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004422#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
Bram Moolenaar79296512020-03-22 16:17:14 +01004423 1
4424#else
4425 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004427 },
4428 {"all_builtin_terms",
4429#if defined(ALL_BUILTIN_TCAPS)
4430 1
4431#else
4432 0
4433#endif
4434 },
4435 {"browsefilter",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01004437 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 || defined(FEAT_GUI_MOTIF))
Bram Moolenaar79296512020-03-22 16:17:14 +01004439 1
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440#else
Bram Moolenaar79296512020-03-22 16:17:14 +01004441 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01004443 },
4444 {"byte_offset",
4445#ifdef FEAT_BYTEOFF
4446 1
4447#else
4448 0
4449#endif
4450 },
4451 {"channel",
4452#ifdef FEAT_JOB_CHANNEL
4453 1
4454#else
4455 0
4456#endif
4457 },
4458 {"cindent",
4459#ifdef FEAT_CINDENT
4460 1
4461#else
4462 0
4463#endif
4464 },
4465 {"clientserver",
4466#ifdef FEAT_CLIENTSERVER
4467 1
4468#else
4469 0
4470#endif
4471 },
4472 {"clipboard",
4473#ifdef FEAT_CLIPBOARD
4474 1
4475#else
4476 0
4477#endif
4478 },
4479 {"cmdline_compl", 1},
4480 {"cmdline_hist", 1},
Bram Moolenaar21829c52021-01-26 22:42:21 +01004481 {"cmdwin",
4482#ifdef FEAT_CMDWIN
4483 1
4484#else
4485 0
4486#endif
4487 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004488 {"comments", 1},
4489 {"conceal",
4490#ifdef FEAT_CONCEAL
4491 1
4492#else
4493 0
4494#endif
4495 },
4496 {"cryptv",
4497#ifdef FEAT_CRYPT
4498 1
4499#else
4500 0
4501#endif
4502 },
4503 {"crypt-blowfish",
4504#ifdef FEAT_CRYPT
4505 1
4506#else
4507 0
4508#endif
4509 },
4510 {"crypt-blowfish2",
4511#ifdef FEAT_CRYPT
4512 1
4513#else
4514 0
4515#endif
4516 },
4517 {"cscope",
4518#ifdef FEAT_CSCOPE
4519 1
4520#else
4521 0
4522#endif
4523 },
4524 {"cursorbind", 1},
4525 {"cursorshape",
4526#ifdef CURSOR_SHAPE
4527 1
4528#else
4529 0
4530#endif
4531 },
4532 {"debug",
4533#ifdef DEBUG
4534 1
4535#else
4536 0
4537#endif
4538 },
4539 {"dialog_con",
4540#ifdef FEAT_CON_DIALOG
4541 1
4542#else
4543 0
4544#endif
4545 },
4546 {"dialog_gui",
4547#ifdef FEAT_GUI_DIALOG
4548 1
4549#else
4550 0
4551#endif
4552 },
4553 {"diff",
4554#ifdef FEAT_DIFF
4555 1
4556#else
4557 0
4558#endif
4559 },
4560 {"digraphs",
4561#ifdef FEAT_DIGRAPHS
4562 1
4563#else
4564 0
4565#endif
4566 },
4567 {"directx",
4568#ifdef FEAT_DIRECTX
4569 1
4570#else
4571 0
4572#endif
4573 },
4574 {"dnd",
4575#ifdef FEAT_DND
4576 1
4577#else
4578 0
4579#endif
4580 },
4581 {"emacs_tags",
4582#ifdef FEAT_EMACS_TAGS
4583 1
4584#else
4585 0
4586#endif
4587 },
4588 {"eval", 1}, // always present, of course!
4589 {"ex_extra", 1}, // graduated feature
4590 {"extra_search",
4591#ifdef FEAT_SEARCH_EXTRA
4592 1
4593#else
4594 0
4595#endif
4596 },
4597 {"file_in_path",
4598#ifdef FEAT_SEARCHPATH
4599 1
4600#else
4601 0
4602#endif
4603 },
4604 {"filterpipe",
4605#if defined(FEAT_FILTERPIPE) && !defined(VIMDLL)
4606 1
4607#else
4608 0
4609#endif
4610 },
4611 {"find_in_path",
4612#ifdef FEAT_FIND_ID
4613 1
4614#else
4615 0
4616#endif
4617 },
4618 {"float",
4619#ifdef FEAT_FLOAT
4620 1
4621#else
4622 0
4623#endif
4624 },
4625 {"folding",
4626#ifdef FEAT_FOLDING
4627 1
4628#else
4629 0
4630#endif
4631 },
4632 {"footer",
4633#ifdef FEAT_FOOTER
4634 1
4635#else
4636 0
4637#endif
4638 },
4639 {"fork",
4640#if !defined(USE_SYSTEM) && defined(UNIX)
4641 1
4642#else
4643 0
4644#endif
4645 },
4646 {"gettext",
4647#ifdef FEAT_GETTEXT
4648 1
4649#else
4650 0
4651#endif
4652 },
4653 {"gui",
4654#ifdef FEAT_GUI
4655 1
4656#else
4657 0
4658#endif
4659 },
4660 {"gui_neXtaw",
4661#if defined(FEAT_GUI_ATHENA) && defined(FEAT_GUI_NEXTAW)
4662 1
4663#else
4664 0
4665#endif
4666 },
4667 {"gui_athena",
4668#if defined(FEAT_GUI_ATHENA) && !defined(FEAT_GUI_NEXTAW)
4669 1
4670#else
4671 0
4672#endif
4673 },
4674 {"gui_gtk",
4675#ifdef FEAT_GUI_GTK
4676 1
4677#else
4678 0
4679#endif
4680 },
4681 {"gui_gtk2",
4682#if defined(FEAT_GUI_GTK) && !defined(USE_GTK3)
4683 1
4684#else
4685 0
4686#endif
4687 },
4688 {"gui_gtk3",
4689#if defined(FEAT_GUI_GTK) && defined(USE_GTK3)
4690 1
4691#else
4692 0
4693#endif
4694 },
4695 {"gui_gnome",
4696#ifdef FEAT_GUI_GNOME
4697 1
4698#else
4699 0
4700#endif
4701 },
4702 {"gui_haiku",
4703#ifdef FEAT_GUI_HAIKU
4704 1
4705#else
4706 0
4707#endif
4708 },
Bram Moolenaar097148e2020-08-11 21:58:20 +02004709 {"gui_mac", 0},
Bram Moolenaar79296512020-03-22 16:17:14 +01004710 {"gui_motif",
4711#ifdef FEAT_GUI_MOTIF
4712 1
4713#else
4714 0
4715#endif
4716 },
4717 {"gui_photon",
4718#ifdef FEAT_GUI_PHOTON
4719 1
4720#else
4721 0
4722#endif
4723 },
4724 {"gui_win32",
4725#ifdef FEAT_GUI_MSWIN
4726 1
4727#else
4728 0
4729#endif
4730 },
4731 {"iconv",
4732#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
4733 1
4734#else
4735 0
4736#endif
4737 },
4738 {"insert_expand", 1},
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +02004739 {"ipv6",
4740#ifdef FEAT_IPV6
4741 1
4742#else
4743 0
4744#endif
4745 },
Bram Moolenaar79296512020-03-22 16:17:14 +01004746 {"job",
4747#ifdef FEAT_JOB_CHANNEL
4748 1
4749#else
4750 0
4751#endif
4752 },
4753 {"jumplist",
4754#ifdef FEAT_JUMPLIST
4755 1
4756#else
4757 0
4758#endif
4759 },
4760 {"keymap",
4761#ifdef FEAT_KEYMAP
4762 1
4763#else
4764 0
4765#endif
4766 },
4767 {"lambda", 1}, // always with FEAT_EVAL, since 7.4.2120 with closure
4768 {"langmap",
4769#ifdef FEAT_LANGMAP
4770 1
4771#else
4772 0
4773#endif
4774 },
4775 {"libcall",
4776#ifdef FEAT_LIBCALL
4777 1
4778#else
4779 0
4780#endif
4781 },
4782 {"linebreak",
4783#ifdef FEAT_LINEBREAK
4784 1
4785#else
4786 0
4787#endif
4788 },
4789 {"lispindent",
4790#ifdef FEAT_LISP
4791 1
4792#else
4793 0
4794#endif
4795 },
4796 {"listcmds", 1},
4797 {"localmap", 1},
4798 {"lua",
4799#if defined(FEAT_LUA) && !defined(DYNAMIC_LUA)
4800 1
4801#else
4802 0
4803#endif
4804 },
4805 {"menu",
4806#ifdef FEAT_MENU
4807 1
4808#else
4809 0
4810#endif
4811 },
4812 {"mksession",
4813#ifdef FEAT_SESSION
4814 1
4815#else
4816 0
4817#endif
4818 },
4819 {"modify_fname", 1},
4820 {"mouse", 1},
4821 {"mouseshape",
4822#ifdef FEAT_MOUSESHAPE
4823 1
4824#else
4825 0
4826#endif
4827 },
4828 {"mouse_dec",
4829#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_DEC)
4830 1
4831#else
4832 0
4833#endif
4834 },
4835 {"mouse_gpm",
4836#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_GPM)
4837 1
4838#else
4839 0
4840#endif
4841 },
4842 {"mouse_jsbterm",
4843#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_JSB)
4844 1
4845#else
4846 0
4847#endif
4848 },
4849 {"mouse_netterm",
4850#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_NET)
4851 1
4852#else
4853 0
4854#endif
4855 },
4856 {"mouse_pterm",
4857#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_PTERM)
4858 1
4859#else
4860 0
4861#endif
4862 },
4863 {"mouse_sgr",
4864#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4865 1
4866#else
4867 0
4868#endif
4869 },
4870 {"mouse_sysmouse",
4871#if (defined(UNIX) || defined(VMS)) && defined(FEAT_SYSMOUSE)
4872 1
4873#else
4874 0
4875#endif
4876 },
4877 {"mouse_urxvt",
4878#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_URXVT)
4879 1
4880#else
4881 0
4882#endif
4883 },
4884 {"mouse_xterm",
4885#if (defined(UNIX) || defined(VMS)) && defined(FEAT_MOUSE_XTERM)
4886 1
4887#else
4888 0
4889#endif
4890 },
4891 {"multi_byte", 1},
4892 {"multi_byte_ime",
4893#ifdef FEAT_MBYTE_IME
4894 1
4895#else
4896 0
4897#endif
4898 },
4899 {"multi_lang",
4900#ifdef FEAT_MULTI_LANG
4901 1
4902#else
4903 0
4904#endif
4905 },
4906 {"mzscheme",
4907#if defined(FEAT_MZSCHEME) && !defined(DYNAMIC_MZSCHEME)
4908 1
4909#else
4910 0
4911#endif
4912 },
4913 {"num64", 1},
4914 {"ole",
4915#ifdef FEAT_OLE
4916 1
4917#else
4918 0
4919#endif
4920 },
4921 {"packages",
4922#ifdef FEAT_EVAL
4923 1
4924#else
4925 0
4926#endif
4927 },
4928 {"path_extra",
4929#ifdef FEAT_PATH_EXTRA
4930 1
4931#else
4932 0
4933#endif
4934 },
4935 {"perl",
4936#if defined(FEAT_PERL) && !defined(DYNAMIC_PERL)
4937 1
4938#else
4939 0
4940#endif
4941 },
4942 {"persistent_undo",
4943#ifdef FEAT_PERSISTENT_UNDO
4944 1
4945#else
4946 0
4947#endif
4948 },
4949 {"python_compiled",
4950#if defined(FEAT_PYTHON)
4951 1
4952#else
4953 0
4954#endif
4955 },
4956 {"python_dynamic",
4957#if defined(FEAT_PYTHON) && defined(DYNAMIC_PYTHON)
4958 1
4959#else
4960 0
4961#endif
4962 },
4963 {"python",
4964#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
4965 1
4966#else
4967 0
4968#endif
4969 },
4970 {"pythonx",
4971#if (defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)) \
4972 || (defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3))
4973 1
4974#else
4975 0
4976#endif
4977 },
4978 {"python3_compiled",
4979#if defined(FEAT_PYTHON3)
4980 1
4981#else
4982 0
4983#endif
4984 },
4985 {"python3_dynamic",
4986#if defined(FEAT_PYTHON3) && defined(DYNAMIC_PYTHON3)
4987 1
4988#else
4989 0
4990#endif
4991 },
4992 {"python3",
4993#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
4994 1
4995#else
4996 0
4997#endif
4998 },
4999 {"popupwin",
5000#ifdef FEAT_PROP_POPUP
5001 1
5002#else
5003 0
5004#endif
5005 },
5006 {"postscript",
5007#ifdef FEAT_POSTSCRIPT
5008 1
5009#else
5010 0
5011#endif
5012 },
5013 {"printer",
5014#ifdef FEAT_PRINTER
5015 1
5016#else
5017 0
5018#endif
5019 },
5020 {"profile",
5021#ifdef FEAT_PROFILE
5022 1
5023#else
5024 0
5025#endif
5026 },
5027 {"reltime",
5028#ifdef FEAT_RELTIME
5029 1
5030#else
5031 0
5032#endif
5033 },
5034 {"quickfix",
5035#ifdef FEAT_QUICKFIX
5036 1
5037#else
5038 0
5039#endif
5040 },
5041 {"rightleft",
5042#ifdef FEAT_RIGHTLEFT
5043 1
5044#else
5045 0
5046#endif
5047 },
5048 {"ruby",
5049#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5050 1
5051#else
5052 0
5053#endif
5054 },
5055 {"scrollbind", 1},
5056 {"showcmd",
5057#ifdef FEAT_CMDL_INFO
5058 1
5059#else
5060 0
5061#endif
5062 },
5063 {"cmdline_info",
5064#ifdef FEAT_CMDL_INFO
5065 1
5066#else
5067 0
5068#endif
5069 },
5070 {"signs",
5071#ifdef FEAT_SIGNS
5072 1
5073#else
5074 0
5075#endif
5076 },
5077 {"smartindent",
5078#ifdef FEAT_SMARTINDENT
5079 1
5080#else
5081 0
5082#endif
5083 },
5084 {"startuptime",
5085#ifdef STARTUPTIME
5086 1
5087#else
5088 0
5089#endif
5090 },
5091 {"statusline",
5092#ifdef FEAT_STL_OPT
5093 1
5094#else
5095 0
5096#endif
5097 },
5098 {"netbeans_intg",
5099#ifdef FEAT_NETBEANS_INTG
5100 1
5101#else
5102 0
5103#endif
5104 },
Christian Brabandtf573c6e2021-06-20 14:02:16 +02005105 {"sodium",
5106#ifdef FEAT_SODIUM
5107 1
5108#else
5109 0
5110#endif
5111 },
Bram Moolenaar79296512020-03-22 16:17:14 +01005112 {"sound",
5113#ifdef FEAT_SOUND
5114 1
5115#else
5116 0
5117#endif
5118 },
5119 {"spell",
5120#ifdef FEAT_SPELL
5121 1
5122#else
5123 0
5124#endif
5125 },
5126 {"syntax",
5127#ifdef FEAT_SYN_HL
5128 1
5129#else
5130 0
5131#endif
5132 },
5133 {"system",
5134#if defined(USE_SYSTEM) || !defined(UNIX)
5135 1
5136#else
5137 0
5138#endif
5139 },
5140 {"tag_binary",
5141#ifdef FEAT_TAG_BINS
5142 1
5143#else
5144 0
5145#endif
5146 },
5147 {"tcl",
5148#if defined(FEAT_TCL) && !defined(DYNAMIC_TCL)
5149 1
5150#else
5151 0
5152#endif
5153 },
5154 {"termguicolors",
5155#ifdef FEAT_TERMGUICOLORS
5156 1
5157#else
5158 0
5159#endif
5160 },
5161 {"terminal",
5162#if defined(FEAT_TERMINAL) && !defined(MSWIN)
5163 1
5164#else
5165 0
5166#endif
5167 },
5168 {"terminfo",
5169#ifdef TERMINFO
5170 1
5171#else
5172 0
5173#endif
5174 },
5175 {"termresponse",
5176#ifdef FEAT_TERMRESPONSE
5177 1
5178#else
5179 0
5180#endif
5181 },
5182 {"textobjects",
5183#ifdef FEAT_TEXTOBJ
5184 1
5185#else
5186 0
5187#endif
5188 },
5189 {"textprop",
5190#ifdef FEAT_PROP_POPUP
5191 1
5192#else
5193 0
5194#endif
5195 },
5196 {"tgetent",
5197#ifdef HAVE_TGETENT
5198 1
5199#else
5200 0
5201#endif
5202 },
5203 {"timers",
5204#ifdef FEAT_TIMERS
5205 1
5206#else
5207 0
5208#endif
5209 },
5210 {"title",
5211#ifdef FEAT_TITLE
5212 1
5213#else
5214 0
5215#endif
5216 },
5217 {"toolbar",
5218#ifdef FEAT_TOOLBAR
5219 1
5220#else
5221 0
5222#endif
5223 },
5224 {"unnamedplus",
5225#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5226 1
5227#else
5228 0
5229#endif
5230 },
5231 {"user-commands", 1}, // was accidentally included in 5.4
5232 {"user_commands", 1},
5233 {"vartabs",
5234#ifdef FEAT_VARTABS
5235 1
5236#else
5237 0
5238#endif
5239 },
5240 {"vertsplit", 1},
5241 {"viminfo",
5242#ifdef FEAT_VIMINFO
5243 1
5244#else
5245 0
5246#endif
5247 },
5248 {"vimscript-1", 1},
5249 {"vimscript-2", 1},
5250 {"vimscript-3", 1},
5251 {"vimscript-4", 1},
5252 {"virtualedit", 1},
5253 {"visual", 1},
5254 {"visualextra", 1},
5255 {"vreplace", 1},
5256 {"vtp",
5257#ifdef FEAT_VTP
5258 1
5259#else
5260 0
5261#endif
5262 },
5263 {"wildignore",
5264#ifdef FEAT_WILDIGN
5265 1
5266#else
5267 0
5268#endif
5269 },
5270 {"wildmenu",
5271#ifdef FEAT_WILDMENU
5272 1
5273#else
5274 0
5275#endif
5276 },
5277 {"windows", 1},
5278 {"winaltkeys",
5279#ifdef FEAT_WAK
5280 1
5281#else
5282 0
5283#endif
5284 },
5285 {"writebackup",
5286#ifdef FEAT_WRITEBACKUP
5287 1
5288#else
5289 0
5290#endif
5291 },
5292 {"xim",
5293#ifdef FEAT_XIM
5294 1
5295#else
5296 0
5297#endif
5298 },
5299 {"xfontset",
5300#ifdef FEAT_XFONTSET
5301 1
5302#else
5303 0
5304#endif
5305 },
5306 {"xpm",
5307#if defined(FEAT_XPM_W32) || defined(HAVE_XPM)
5308 1
5309#else
5310 0
5311#endif
5312 },
5313 {"xpm_w32", // for backward compatibility
5314#ifdef FEAT_XPM_W32
5315 1
5316#else
5317 0
5318#endif
5319 },
5320 {"xsmp",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005321#ifdef USE_XSMP
Bram Moolenaar79296512020-03-22 16:17:14 +01005322 1
5323#else
5324 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005326 },
5327 {"xsmp_interact",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328#ifdef USE_XSMP_INTERACT
Bram Moolenaar79296512020-03-22 16:17:14 +01005329 1
5330#else
5331 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005332#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005333 },
5334 {"xterm_clipboard",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335#ifdef FEAT_XCLIPBOARD
Bram Moolenaar79296512020-03-22 16:17:14 +01005336 1
5337#else
5338 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005340 },
5341 {"xterm_save",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342#ifdef FEAT_XTERM_SAVE
Bram Moolenaar79296512020-03-22 16:17:14 +01005343 1
5344#else
5345 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005346#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005347 },
5348 {"X11",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005349#if defined(UNIX) && defined(FEAT_X11)
Bram Moolenaar79296512020-03-22 16:17:14 +01005350 1
5351#else
5352 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005354 },
5355 {NULL, 0}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356 };
5357
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005358 name = tv_get_string(&argvars[0]);
Bram Moolenaar79296512020-03-22 16:17:14 +01005359 for (i = 0; has_list[i].name != NULL; ++i)
5360 if (STRICMP(name, has_list[i].name) == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005361 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005362 x = TRUE;
5363 n = has_list[i].present;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 break;
5365 }
5366
Bram Moolenaar79296512020-03-22 16:17:14 +01005367 // features also in has_list[] but sometimes enabled at runtime
5368 if (x == TRUE && n == FALSE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 {
Bram Moolenaar79296512020-03-22 16:17:14 +01005370 if (0)
Bram Moolenaar86b9a3e2020-04-07 19:57:29 +02005371 {
5372 // intentionally empty
5373 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005374#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 else if (STRICMP(name, "balloon_multiline") == 0)
5376 n = multiline_balloon_available();
5377#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005378#ifdef VIMDLL
5379 else if (STRICMP(name, "filterpipe") == 0)
5380 n = gui.in_use || gui.starting;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005381#endif
5382#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5383 else if (STRICMP(name, "iconv") == 0)
5384 n = iconv_enabled(FALSE);
5385#endif
5386#ifdef DYNAMIC_LUA
5387 else if (STRICMP(name, "lua") == 0)
5388 n = lua_enabled(FALSE);
5389#endif
5390#ifdef DYNAMIC_MZSCHEME
5391 else if (STRICMP(name, "mzscheme") == 0)
5392 n = mzscheme_enabled(FALSE);
5393#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005394#ifdef DYNAMIC_PERL
5395 else if (STRICMP(name, "perl") == 0)
5396 n = perl_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005398#ifdef DYNAMIC_PYTHON
5399 else if (STRICMP(name, "python") == 0)
5400 n = python_enabled(FALSE);
5401#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005402#ifdef DYNAMIC_PYTHON3
5403 else if (STRICMP(name, "python3") == 0)
5404 n = python3_enabled(FALSE);
5405#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005406#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5407 else if (STRICMP(name, "pythonx") == 0)
5408 {
5409# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
5410 if (p_pyx == 0)
5411 n = python3_enabled(FALSE) || python_enabled(FALSE);
5412 else if (p_pyx == 3)
5413 n = python3_enabled(FALSE);
5414 else if (p_pyx == 2)
5415 n = python_enabled(FALSE);
5416# elif defined(DYNAMIC_PYTHON)
5417 n = python_enabled(FALSE);
5418# elif defined(DYNAMIC_PYTHON3)
5419 n = python3_enabled(FALSE);
5420# endif
5421 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005422#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005423#ifdef DYNAMIC_RUBY
5424 else if (STRICMP(name, "ruby") == 0)
5425 n = ruby_enabled(FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005426#endif
Bram Moolenaar79296512020-03-22 16:17:14 +01005427#ifdef DYNAMIC_TCL
5428 else if (STRICMP(name, "tcl") == 0)
5429 n = tcl_enabled(FALSE);
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02005430#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005431#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02005432 else if (STRICMP(name, "terminal") == 0)
5433 n = terminal_enabled();
5434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435 }
5436
Bram Moolenaar79296512020-03-22 16:17:14 +01005437 // features not in has_list[]
5438 if (x == FALSE)
5439 {
5440 if (STRNICMP(name, "patch", 5) == 0)
5441 {
5442 x = TRUE;
5443 if (name[5] == '-'
5444 && STRLEN(name) >= 11
5445 && vim_isdigit(name[6])
5446 && vim_isdigit(name[8])
5447 && vim_isdigit(name[10]))
5448 {
5449 int major = atoi((char *)name + 6);
5450 int minor = atoi((char *)name + 8);
5451
5452 // Expect "patch-9.9.01234".
5453 n = (major < VIM_VERSION_MAJOR
5454 || (major == VIM_VERSION_MAJOR
5455 && (minor < VIM_VERSION_MINOR
5456 || (minor == VIM_VERSION_MINOR
5457 && has_patch(atoi((char *)name + 10))))));
5458 }
5459 else
5460 n = has_patch(atoi((char *)name + 5));
5461 }
5462 else if (STRICMP(name, "vim_starting") == 0)
5463 {
5464 x = TRUE;
5465 n = (starting != 0);
5466 }
5467 else if (STRICMP(name, "ttyin") == 0)
5468 {
5469 x = TRUE;
5470 n = mch_input_isatty();
5471 }
5472 else if (STRICMP(name, "ttyout") == 0)
5473 {
5474 x = TRUE;
5475 n = stdout_isatty;
5476 }
5477 else if (STRICMP(name, "multi_byte_encoding") == 0)
5478 {
5479 x = TRUE;
5480 n = has_mbyte;
5481 }
5482 else if (STRICMP(name, "gui_running") == 0)
5483 {
5484 x = TRUE;
5485#ifdef FEAT_GUI
5486 n = (gui.in_use || gui.starting);
5487#endif
5488 }
5489 else if (STRICMP(name, "browse") == 0)
5490 {
5491 x = TRUE;
5492#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5493 n = gui.in_use; // gui_mch_browse() works when GUI is running
5494#endif
5495 }
5496 else if (STRICMP(name, "syntax_items") == 0)
5497 {
5498 x = TRUE;
5499#ifdef FEAT_SYN_HL
5500 n = syntax_present(curwin);
5501#endif
5502 }
5503 else if (STRICMP(name, "vcon") == 0)
5504 {
5505 x = TRUE;
5506#ifdef FEAT_VTP
5507 n = is_term_win32() && has_vtp_working();
5508#endif
5509 }
5510 else if (STRICMP(name, "netbeans_enabled") == 0)
5511 {
5512 x = TRUE;
5513#ifdef FEAT_NETBEANS_INTG
5514 n = netbeans_active();
5515#endif
5516 }
5517 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
5518 {
5519 x = TRUE;
5520#ifdef FEAT_MOUSE_GPM
5521 n = gpm_enabled();
5522#endif
5523 }
5524 else if (STRICMP(name, "conpty") == 0)
5525 {
5526 x = TRUE;
5527#if defined(FEAT_TERMINAL) && defined(MSWIN)
5528 n = use_conpty();
5529#endif
5530 }
5531 else if (STRICMP(name, "clipboard_working") == 0)
5532 {
5533 x = TRUE;
5534#ifdef FEAT_CLIPBOARD
5535 n = clip_star.available;
5536#endif
5537 }
5538 }
5539
Bram Moolenaar04637e22020-09-05 18:45:29 +02005540 if (argvars[1].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[1]))
Bram Moolenaar79296512020-03-22 16:17:14 +01005541 // return whether feature could ever be enabled
5542 rettv->vval.v_number = x;
5543 else
5544 // return whether feature is enabled
5545 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546}
5547
5548/*
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005549 * Return TRUE if "feature" can change later.
5550 * Also when checking for the feature has side effects, such as loading a DLL.
5551 */
5552 int
5553dynamic_feature(char_u *feature)
5554{
5555 return (feature == NULL
5556#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
5557 || STRICMP(feature, "balloon_multiline") == 0
5558#endif
5559#if defined(FEAT_GUI) && defined(FEAT_BROWSE)
5560 || (STRICMP(feature, "browse") == 0 && !gui.in_use)
5561#endif
5562#ifdef VIMDLL
5563 || STRICMP(feature, "filterpipe") == 0
5564#endif
Bram Moolenaar29b281b2020-11-10 20:58:00 +01005565#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar8cebd432020-11-08 12:49:47 +01005566 // this can only change on Unix where the ":gui" command could be
5567 // used.
5568 || (STRICMP(feature, "gui_running") == 0 && !gui.in_use)
5569#endif
5570#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5571 || STRICMP(feature, "iconv") == 0
5572#endif
5573#ifdef DYNAMIC_LUA
5574 || STRICMP(feature, "lua") == 0
5575#endif
5576#ifdef FEAT_MOUSE_GPM
5577 || (STRICMP(feature, "mouse_gpm_enabled") == 0 && !gpm_enabled())
5578#endif
5579#ifdef DYNAMIC_MZSCHEME
5580 || STRICMP(feature, "mzscheme") == 0
5581#endif
5582#ifdef FEAT_NETBEANS_INTG
5583 || STRICMP(feature, "netbeans_enabled") == 0
5584#endif
5585#ifdef DYNAMIC_PERL
5586 || STRICMP(feature, "perl") == 0
5587#endif
5588#ifdef DYNAMIC_PYTHON
5589 || STRICMP(feature, "python") == 0
5590#endif
5591#ifdef DYNAMIC_PYTHON3
5592 || STRICMP(feature, "python3") == 0
5593#endif
5594#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5595 || STRICMP(feature, "pythonx") == 0
5596#endif
5597#ifdef DYNAMIC_RUBY
5598 || STRICMP(feature, "ruby") == 0
5599#endif
5600#ifdef FEAT_SYN_HL
5601 || STRICMP(feature, "syntax_items") == 0
5602#endif
5603#ifdef DYNAMIC_TCL
5604 || STRICMP(feature, "tcl") == 0
5605#endif
5606 // once "starting" is zero it will stay that way
5607 || (STRICMP(feature, "vim_starting") == 0 && starting != 0)
5608 || STRICMP(feature, "multi_byte_encoding") == 0
5609#if defined(FEAT_TERMINAL) && defined(MSWIN)
5610 || STRICMP(feature, "conpty") == 0
5611#endif
5612 );
5613}
5614
5615/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005616 * "haslocaldir()" function
5617 */
5618 static void
5619f_haslocaldir(typval_T *argvars, typval_T *rettv)
5620{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005621 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 win_T *wp = NULL;
5623
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005624 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
5625
5626 // Check for window-local and tab-local directories
5627 if (wp != NULL && wp->w_localdir != NULL)
5628 rettv->vval.v_number = 1;
5629 else if (tp != NULL && tp->tp_localdir != NULL)
5630 rettv->vval.v_number = 2;
5631 else
5632 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633}
5634
5635/*
5636 * "hasmapto()" function
5637 */
5638 static void
5639f_hasmapto(typval_T *argvars, typval_T *rettv)
5640{
5641 char_u *name;
5642 char_u *mode;
5643 char_u buf[NUMBUFLEN];
5644 int abbr = FALSE;
5645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005646 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005647 if (argvars[1].v_type == VAR_UNKNOWN)
5648 mode = (char_u *)"nvo";
5649 else
5650 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005651 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar04d594b2020-09-02 22:25:35 +02005653 abbr = (int)tv_get_bool(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 }
5655
5656 if (map_to_exists(name, mode, abbr))
5657 rettv->vval.v_number = TRUE;
5658 else
5659 rettv->vval.v_number = FALSE;
5660}
5661
5662/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005663 * "highlightID(name)" function
5664 */
5665 static void
5666f_hlID(typval_T *argvars, typval_T *rettv)
5667{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005668 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669}
5670
5671/*
5672 * "highlight_exists()" function
5673 */
5674 static void
5675f_hlexists(typval_T *argvars, typval_T *rettv)
5676{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005677 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678}
5679
5680/*
5681 * "hostname()" function
5682 */
5683 static void
5684f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
5685{
5686 char_u hostname[256];
5687
5688 mch_get_host_name(hostname, 256);
5689 rettv->v_type = VAR_STRING;
5690 rettv->vval.v_string = vim_strsave(hostname);
5691}
5692
5693/*
5694 * iconv() function
5695 */
5696 static void
5697f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
5698{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699 char_u buf1[NUMBUFLEN];
5700 char_u buf2[NUMBUFLEN];
5701 char_u *from, *to, *str;
5702 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703
5704 rettv->v_type = VAR_STRING;
5705 rettv->vval.v_string = NULL;
5706
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005707 str = tv_get_string(&argvars[0]);
5708 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
5709 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710 vimconv.vc_type = CONV_NONE;
5711 convert_setup(&vimconv, from, to);
5712
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005713 // If the encodings are equal, no conversion needed.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005714 if (vimconv.vc_type == CONV_NONE)
5715 rettv->vval.v_string = vim_strsave(str);
5716 else
5717 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
5718
5719 convert_setup(&vimconv, NULL, NULL);
5720 vim_free(from);
5721 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722}
5723
5724/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 * "index()" function
5726 */
5727 static void
5728f_index(typval_T *argvars, typval_T *rettv)
5729{
5730 list_T *l;
5731 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005732 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005733 long idx = 0;
5734 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005735 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736
5737 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005738 if (argvars[0].v_type == VAR_BLOB)
5739 {
5740 typval_T tv;
5741 int start = 0;
5742
5743 if (argvars[2].v_type != VAR_UNKNOWN)
5744 {
5745 start = tv_get_number_chk(&argvars[2], &error);
5746 if (error)
5747 return;
5748 }
5749 b = argvars[0].vval.v_blob;
5750 if (b == NULL)
5751 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01005752 if (start < 0)
5753 {
5754 start = blob_len(b) + start;
5755 if (start < 0)
5756 start = 0;
5757 }
5758
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005759 for (idx = start; idx < blob_len(b); ++idx)
5760 {
5761 tv.v_type = VAR_NUMBER;
5762 tv.vval.v_number = blob_get(b, idx);
5763 if (tv_equal(&tv, &argvars[1], ic, FALSE))
5764 {
5765 rettv->vval.v_number = idx;
5766 return;
5767 }
5768 }
5769 return;
5770 }
5771 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005772 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01005773 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005774 return;
5775 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777 l = argvars[0].vval.v_list;
5778 if (l != NULL)
5779 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005780 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005781 item = l->lv_first;
5782 if (argvars[2].v_type != VAR_UNKNOWN)
5783 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005784 // Start at specified item. Use the cached index that list_find()
5785 // sets, so that a negative number also works.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005786 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01005787 idx = l->lv_u.mat.lv_idx;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6c553f92020-09-02 22:10:34 +02005789 ic = (int)tv_get_bool_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790 if (error)
5791 item = NULL;
5792 }
5793
5794 for ( ; item != NULL; item = item->li_next, ++idx)
5795 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
5796 {
5797 rettv->vval.v_number = idx;
5798 break;
5799 }
5800 }
5801}
5802
5803static int inputsecret_flag = 0;
5804
5805/*
5806 * "input()" function
5807 * Also handles inputsecret() when inputsecret is set.
5808 */
5809 static void
5810f_input(typval_T *argvars, typval_T *rettv)
5811{
5812 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
5813}
5814
5815/*
5816 * "inputdialog()" function
5817 */
5818 static void
5819f_inputdialog(typval_T *argvars, typval_T *rettv)
5820{
5821#if defined(FEAT_GUI_TEXTDIALOG)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 // Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions'
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005823 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
5824 {
5825 char_u *message;
5826 char_u buf[NUMBUFLEN];
5827 char_u *defstr = (char_u *)"";
5828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005829 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005831 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 vim_strncpy(IObuff, defstr, IOSIZE - 1);
5833 else
5834 IObuff[0] = NUL;
5835 if (message != NULL && defstr != NULL
5836 && do_dialog(VIM_QUESTION, NULL, message,
5837 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
5838 rettv->vval.v_string = vim_strsave(IObuff);
5839 else
5840 {
5841 if (message != NULL && defstr != NULL
5842 && argvars[1].v_type != VAR_UNKNOWN
5843 && argvars[2].v_type != VAR_UNKNOWN)
5844 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005845 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005846 else
5847 rettv->vval.v_string = NULL;
5848 }
5849 rettv->v_type = VAR_STRING;
5850 }
5851 else
5852#endif
5853 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
5854}
5855
5856/*
5857 * "inputlist()" function
5858 */
5859 static void
5860f_inputlist(typval_T *argvars, typval_T *rettv)
5861{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005862 list_T *l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 listitem_T *li;
5864 int selected;
5865 int mouse_used;
5866
5867#ifdef NO_CONSOLE_INPUT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005868 // While starting up, there is no place to enter text. When running tests
5869 // with --not-a-term we assume feedkeys() will be used.
Bram Moolenaar91d348a2017-07-29 20:16:03 +02005870 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005871 return;
5872#endif
5873 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
5874 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005875 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005876 return;
5877 }
5878
5879 msg_start();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 msg_row = Rows - 1; // for when 'cmdheight' > 1
5881 lines_left = Rows; // avoid more prompt
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005882 msg_scroll = TRUE;
5883 msg_clr_eos();
5884
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005885 l = argvars[0].vval.v_list;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02005886 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02005887 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005889 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 msg_putchar('\n');
5891 }
5892
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005893 // Ask for choice.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005894 selected = prompt_for_number(&mouse_used);
5895 if (mouse_used)
5896 selected -= lines_left;
5897
5898 rettv->vval.v_number = selected;
5899}
5900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005901static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
5902
5903/*
5904 * "inputrestore()" function
5905 */
5906 static void
5907f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
5908{
5909 if (ga_userinput.ga_len > 0)
5910 {
5911 --ga_userinput.ga_len;
5912 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
Bram Moolenaarc41badb2021-06-07 22:04:52 +02005913 + ga_userinput.ga_len, TRUE);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 }
5916 else if (p_verbose > 1)
5917 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005918 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005920 }
5921}
5922
5923/*
5924 * "inputsave()" function
5925 */
5926 static void
5927f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
5928{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005929 // Add an entry to the stack of typeahead storage.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005930 if (ga_grow(&ga_userinput, 1) == OK)
5931 {
5932 save_typeahead((tasave_T *)(ga_userinput.ga_data)
5933 + ga_userinput.ga_len);
5934 ++ga_userinput.ga_len;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005935 // default return is zero == OK
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005936 }
5937 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 rettv->vval.v_number = 1; // Failed
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005939}
5940
5941/*
5942 * "inputsecret()" function
5943 */
5944 static void
5945f_inputsecret(typval_T *argvars, typval_T *rettv)
5946{
5947 ++cmdline_star;
5948 ++inputsecret_flag;
5949 f_input(argvars, rettv);
5950 --cmdline_star;
5951 --inputsecret_flag;
5952}
5953
5954/*
Bram Moolenaar67a2deb2019-11-25 00:05:32 +01005955 * "interrupt()" function
5956 */
5957 static void
5958f_interrupt(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5959{
5960 got_int = TRUE;
5961}
5962
5963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005964 * "invert(expr)" function
5965 */
5966 static void
5967f_invert(typval_T *argvars, typval_T *rettv)
5968{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005969 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970}
5971
5972/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 * "islocked()" function
5974 */
5975 static void
5976f_islocked(typval_T *argvars, typval_T *rettv)
5977{
5978 lval_T lv;
5979 char_u *end;
5980 dictitem_T *di;
5981
5982 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005983 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01005984 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 if (end != NULL && lv.ll_name != NULL)
5986 {
5987 if (*end != NUL)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005988 semsg(_(e_trailing_arg), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 else
5990 {
5991 if (lv.ll_tv == NULL)
5992 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01005993 di = find_var(lv.ll_name, NULL, TRUE);
5994 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005996 // Consider a variable locked when:
5997 // 1. the variable itself is locked
5998 // 2. the value of the variable is locked.
5999 // 3. the List or Dict value is locked.
Bram Moolenaar79518e22017-02-17 16:31:35 +01006000 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6001 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002 }
6003 }
6004 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006005 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006007 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006008 else if (lv.ll_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006009 // List item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6011 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // Dictionary item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6014 }
6015 }
6016
6017 clear_lval(&lv);
6018}
6019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006020/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021 * "last_buffer_nr()" function.
6022 */
6023 static void
6024f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6025{
6026 int n = 0;
6027 buf_T *buf;
6028
Bram Moolenaar29323592016-07-24 22:04:11 +02006029 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 if (n < buf->b_fnum)
6031 n = buf->b_fnum;
6032
6033 rettv->vval.v_number = n;
6034}
6035
6036/*
6037 * "len()" function
6038 */
6039 static void
6040f_len(typval_T *argvars, typval_T *rettv)
6041{
6042 switch (argvars[0].v_type)
6043 {
6044 case VAR_STRING:
6045 case VAR_NUMBER:
6046 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006047 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006049 case VAR_BLOB:
6050 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6051 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052 case VAR_LIST:
6053 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6054 break;
6055 case VAR_DICT:
6056 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6057 break;
6058 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006059 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006061 case VAR_BOOL:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 case VAR_SPECIAL:
6063 case VAR_FLOAT:
6064 case VAR_FUNC:
6065 case VAR_PARTIAL:
6066 case VAR_JOB:
6067 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006068 case VAR_INSTR:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006069 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 break;
6071 }
6072}
6073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006074 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006075libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076{
6077#ifdef FEAT_LIBCALL
6078 char_u *string_in;
6079 char_u **string_result;
6080 int nr_result;
6081#endif
6082
6083 rettv->v_type = type;
6084 if (type != VAR_NUMBER)
6085 rettv->vval.v_string = NULL;
6086
6087 if (check_restricted() || check_secure())
6088 return;
6089
6090#ifdef FEAT_LIBCALL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006091 // The first two args must be strings, otherwise it's meaningless
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6093 {
6094 string_in = NULL;
6095 if (argvars[2].v_type == VAR_STRING)
6096 string_in = argvars[2].vval.v_string;
6097 if (type == VAR_NUMBER)
6098 string_result = NULL;
6099 else
6100 string_result = &rettv->vval.v_string;
6101 if (mch_libcall(argvars[0].vval.v_string,
6102 argvars[1].vval.v_string,
6103 string_in,
6104 argvars[2].vval.v_number,
6105 string_result,
6106 &nr_result) == OK
6107 && type == VAR_NUMBER)
6108 rettv->vval.v_number = nr_result;
6109 }
6110#endif
6111}
6112
6113/*
6114 * "libcall()" function
6115 */
6116 static void
6117f_libcall(typval_T *argvars, typval_T *rettv)
6118{
6119 libcall_common(argvars, rettv, VAR_STRING);
6120}
6121
6122/*
6123 * "libcallnr()" function
6124 */
6125 static void
6126f_libcallnr(typval_T *argvars, typval_T *rettv)
6127{
6128 libcall_common(argvars, rettv, VAR_NUMBER);
6129}
6130
6131/*
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006132 * "line(string, [winid])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133 */
6134 static void
6135f_line(typval_T *argvars, typval_T *rettv)
6136{
6137 linenr_T lnum = 0;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006138 pos_T *fp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006139 int fnum;
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006140 int id;
6141 tabpage_T *tp;
6142 win_T *wp;
6143 win_T *save_curwin;
6144 tabpage_T *save_curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006145
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006146 if (argvars[1].v_type != VAR_UNKNOWN)
6147 {
6148 // use window specified in the second argument
6149 id = (int)tv_get_number(&argvars[1]);
6150 wp = win_id2wp_tp(id, &tp);
6151 if (wp != NULL && tp != NULL)
6152 {
6153 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE)
6154 == OK)
6155 {
6156 check_cursor();
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006157 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006158 }
6159 restore_win_noblock(save_curwin, save_curtab, TRUE);
6160 }
6161 }
6162 else
6163 // use current window
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006164 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +02006165
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006166 if (fp != NULL)
6167 lnum = fp->lnum;
6168 rettv->vval.v_number = lnum;
6169}
6170
6171/*
6172 * "line2byte(lnum)" function
6173 */
6174 static void
6175f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6176{
6177#ifndef FEAT_BYTEOFF
6178 rettv->vval.v_number = -1;
6179#else
6180 linenr_T lnum;
6181
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006182 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6184 rettv->vval.v_number = -1;
6185 else
6186 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6187 if (rettv->vval.v_number >= 0)
6188 ++rettv->vval.v_number;
6189#endif
6190}
6191
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006192#ifdef FEAT_LUA
6193/*
6194 * "luaeval()" function
6195 */
6196 static void
6197f_luaeval(typval_T *argvars, typval_T *rettv)
6198{
6199 char_u *str;
6200 char_u buf[NUMBUFLEN];
6201
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006202 if (check_restricted() || check_secure())
6203 return;
6204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006205 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 do_luaeval(str, argvars + 1, rettv);
6207}
6208#endif
6209
6210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 * "maparg()" function
6212 */
6213 static void
6214f_maparg(typval_T *argvars, typval_T *rettv)
6215{
6216 get_maparg(argvars, rettv, TRUE);
6217}
6218
6219/*
6220 * "mapcheck()" function
6221 */
6222 static void
6223f_mapcheck(typval_T *argvars, typval_T *rettv)
6224{
6225 get_maparg(argvars, rettv, FALSE);
6226}
6227
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006228typedef enum
6229{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006230 MATCH_END, // matchend()
6231 MATCH_MATCH, // match()
6232 MATCH_STR, // matchstr()
6233 MATCH_LIST, // matchlist()
6234 MATCH_POS // matchstrpos()
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006235} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236
6237 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006238find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006239{
6240 char_u *str = NULL;
6241 long len = 0;
6242 char_u *expr = NULL;
6243 char_u *pat;
6244 regmatch_T regmatch;
6245 char_u patbuf[NUMBUFLEN];
6246 char_u strbuf[NUMBUFLEN];
6247 char_u *save_cpo;
6248 long start = 0;
6249 long nth = 1;
6250 colnr_T startcol = 0;
6251 int match = 0;
6252 list_T *l = NULL;
6253 listitem_T *li = NULL;
6254 long idx = 0;
6255 char_u *tofree = NULL;
6256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006257 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006259 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260
6261 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006262 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006264 // type MATCH_LIST: return empty list when there are no matches.
6265 // type MATCH_POS: return ["", -1, -1, -1]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006266 if (rettv_list_alloc(rettv) == FAIL)
6267 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006268 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 && (list_append_string(rettv->vval.v_list,
6270 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
6276 (varnumber_T)-1) == FAIL))
6277 {
6278 list_free(rettv->vval.v_list);
6279 rettv->vval.v_list = NULL;
6280 goto theend;
6281 }
6282 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006283 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 {
6285 rettv->v_type = VAR_STRING;
6286 rettv->vval.v_string = NULL;
6287 }
6288
6289 if (argvars[0].v_type == VAR_LIST)
6290 {
6291 if ((l = argvars[0].vval.v_list) == NULL)
6292 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02006293 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 li = l->lv_first;
6295 }
6296 else
6297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006298 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006299 len = (long)STRLEN(str);
6300 }
6301
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006302 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 if (pat == NULL)
6304 goto theend;
6305
6306 if (argvars[2].v_type != VAR_UNKNOWN)
6307 {
6308 int error = FALSE;
6309
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006310 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311 if (error)
6312 goto theend;
6313 if (l != NULL)
6314 {
6315 li = list_find(l, start);
6316 if (li == NULL)
6317 goto theend;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01006318 idx = l->lv_u.mat.lv_idx; // use the cached index
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006319 }
6320 else
6321 {
6322 if (start < 0)
6323 start = 0;
6324 if (start > len)
6325 goto theend;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 // When "count" argument is there ignore matches before "start",
6327 // otherwise skip part of the string. Differs when pattern is "^"
6328 // or "\<".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006329 if (argvars[3].v_type != VAR_UNKNOWN)
6330 startcol = start;
6331 else
6332 {
6333 str += start;
6334 len -= start;
6335 }
6336 }
6337
6338 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006339 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 if (error)
6341 goto theend;
6342 }
6343
6344 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6345 if (regmatch.regprog != NULL)
6346 {
6347 regmatch.rm_ic = p_ic;
6348
6349 for (;;)
6350 {
6351 if (l != NULL)
6352 {
6353 if (li == NULL)
6354 {
6355 match = FALSE;
6356 break;
6357 }
6358 vim_free(tofree);
6359 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
6360 if (str == NULL)
6361 break;
6362 }
6363
6364 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
6365
6366 if (match && --nth <= 0)
6367 break;
6368 if (l == NULL && !match)
6369 break;
6370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006371 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 if (l != NULL)
6373 {
6374 li = li->li_next;
6375 ++idx;
6376 }
6377 else
6378 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379 startcol = (colnr_T)(regmatch.startp[0]
6380 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006381 if (startcol > (colnr_T)len
6382 || str + startcol <= regmatch.startp[0])
6383 {
6384 match = FALSE;
6385 break;
6386 }
6387 }
6388 }
6389
6390 if (match)
6391 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006392 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 {
6394 listitem_T *li1 = rettv->vval.v_list->lv_first;
6395 listitem_T *li2 = li1->li_next;
6396 listitem_T *li3 = li2->li_next;
6397 listitem_T *li4 = li3->li_next;
6398
6399 vim_free(li1->li_tv.vval.v_string);
6400 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006401 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 li3->li_tv.vval.v_number =
6403 (varnumber_T)(regmatch.startp[0] - expr);
6404 li4->li_tv.vval.v_number =
6405 (varnumber_T)(regmatch.endp[0] - expr);
6406 if (l != NULL)
6407 li2->li_tv.vval.v_number = (varnumber_T)idx;
6408 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006409 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006410 {
6411 int i;
6412
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006413 // return list with matched string and submatches
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414 for (i = 0; i < NSUBEXP; ++i)
6415 {
6416 if (regmatch.endp[i] == NULL)
6417 {
6418 if (list_append_string(rettv->vval.v_list,
6419 (char_u *)"", 0) == FAIL)
6420 break;
6421 }
6422 else if (list_append_string(rettv->vval.v_list,
6423 regmatch.startp[i],
6424 (int)(regmatch.endp[i] - regmatch.startp[i]))
6425 == FAIL)
6426 break;
6427 }
6428 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006429 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 // return matched string
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432 if (l != NULL)
6433 copy_tv(&li->li_tv, rettv);
6434 else
6435 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaardf44a272020-06-07 20:49:05 +02006436 regmatch.endp[0] - regmatch.startp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 }
6438 else if (l != NULL)
6439 rettv->vval.v_number = idx;
6440 else
6441 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006442 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443 rettv->vval.v_number =
6444 (varnumber_T)(regmatch.startp[0] - str);
6445 else
6446 rettv->vval.v_number =
6447 (varnumber_T)(regmatch.endp[0] - str);
6448 rettv->vval.v_number += (varnumber_T)(str - expr);
6449 }
6450 }
6451 vim_regfree(regmatch.regprog);
6452 }
6453
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006454theend:
6455 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // matchstrpos() without a list: drop the second item.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457 listitem_remove(rettv->vval.v_list,
6458 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459 vim_free(tofree);
6460 p_cpo = save_cpo;
6461}
6462
6463/*
6464 * "match()" function
6465 */
6466 static void
6467f_match(typval_T *argvars, typval_T *rettv)
6468{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006469 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470}
6471
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472/*
6473 * "matchend()" function
6474 */
6475 static void
6476f_matchend(typval_T *argvars, typval_T *rettv)
6477{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006478 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006479}
6480
6481/*
6482 * "matchlist()" function
6483 */
6484 static void
6485f_matchlist(typval_T *argvars, typval_T *rettv)
6486{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006487 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488}
6489
6490/*
6491 * "matchstr()" function
6492 */
6493 static void
6494f_matchstr(typval_T *argvars, typval_T *rettv)
6495{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006496 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497}
6498
6499/*
6500 * "matchstrpos()" function
6501 */
6502 static void
6503f_matchstrpos(typval_T *argvars, typval_T *rettv)
6504{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02006505 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506}
6507
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508 static void
6509max_min(typval_T *argvars, typval_T *rettv, int domax)
6510{
6511 varnumber_T n = 0;
6512 varnumber_T i;
6513 int error = FALSE;
6514
6515 if (argvars[0].v_type == VAR_LIST)
6516 {
6517 list_T *l;
6518 listitem_T *li;
6519
6520 l = argvars[0].vval.v_list;
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006521 if (l != NULL && l->lv_len > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006523 if (l->lv_first == &range_list_item)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006525 if ((l->lv_u.nonmat.lv_stride > 0) ^ domax)
6526 n = l->lv_u.nonmat.lv_start;
6527 else
6528 n = l->lv_u.nonmat.lv_start + (l->lv_len - 1)
6529 * l->lv_u.nonmat.lv_stride;
6530 }
6531 else
6532 {
6533 li = l->lv_first;
6534 if (li != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006535 {
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006536 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006537 if (error)
6538 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006539 for (;;)
6540 {
6541 li = li->li_next;
6542 if (li == NULL)
6543 break;
6544 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006545 if (error)
6546 return; // type error; errmsg already given
Bram Moolenaar9f2d0202020-01-30 16:40:10 +01006547 if (domax ? i > n : i < n)
6548 n = i;
6549 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006550 }
6551 }
6552 }
6553 }
6554 else if (argvars[0].v_type == VAR_DICT)
6555 {
6556 dict_T *d;
6557 int first = TRUE;
6558 hashitem_T *hi;
6559 int todo;
6560
6561 d = argvars[0].vval.v_dict;
6562 if (d != NULL)
6563 {
6564 todo = (int)d->dv_hashtab.ht_used;
6565 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
6566 {
6567 if (!HASHITEM_EMPTY(hi))
6568 {
6569 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006570 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006571 if (error)
6572 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 if (first)
6574 {
6575 n = i;
6576 first = FALSE;
6577 }
6578 else if (domax ? i > n : i < n)
6579 n = i;
6580 }
6581 }
6582 }
6583 }
6584 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006585 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaarab65fc72021-02-04 22:07:16 +01006586
6587 rettv->vval.v_number = n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006588}
6589
6590/*
6591 * "max()" function
6592 */
6593 static void
6594f_max(typval_T *argvars, typval_T *rettv)
6595{
6596 max_min(argvars, rettv, TRUE);
6597}
6598
6599/*
6600 * "min()" function
6601 */
6602 static void
6603f_min(typval_T *argvars, typval_T *rettv)
6604{
6605 max_min(argvars, rettv, FALSE);
6606}
6607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608#if defined(FEAT_MZSCHEME) || defined(PROTO)
6609/*
6610 * "mzeval()" function
6611 */
6612 static void
6613f_mzeval(typval_T *argvars, typval_T *rettv)
6614{
6615 char_u *str;
6616 char_u buf[NUMBUFLEN];
6617
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006618 if (check_restricted() || check_secure())
6619 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006620 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 do_mzeval(str, rettv);
6622}
6623
6624 void
6625mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
6626{
6627 typval_T argvars[3];
6628
6629 argvars[0].v_type = VAR_STRING;
6630 argvars[0].vval.v_string = name;
6631 copy_tv(args, &argvars[1]);
6632 argvars[2].v_type = VAR_UNKNOWN;
6633 f_call(argvars, rettv);
6634 clear_tv(&argvars[1]);
6635}
6636#endif
6637
6638/*
6639 * "nextnonblank()" function
6640 */
6641 static void
6642f_nextnonblank(typval_T *argvars, typval_T *rettv)
6643{
6644 linenr_T lnum;
6645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006646 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006647 {
6648 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
6649 {
6650 lnum = 0;
6651 break;
6652 }
6653 if (*skipwhite(ml_get(lnum)) != NUL)
6654 break;
6655 }
6656 rettv->vval.v_number = lnum;
6657}
6658
6659/*
6660 * "nr2char()" function
6661 */
6662 static void
6663f_nr2char(typval_T *argvars, typval_T *rettv)
6664{
6665 char_u buf[NUMBUFLEN];
6666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667 if (has_mbyte)
6668 {
6669 int utf8 = 0;
6670
6671 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaared6a4302020-09-05 20:29:41 +02006672 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01006674 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006676 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677 }
6678 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006680 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 buf[1] = NUL;
6682 }
6683 rettv->v_type = VAR_STRING;
6684 rettv->vval.v_string = vim_strsave(buf);
6685}
6686
6687/*
6688 * "or(expr, expr)" function
6689 */
6690 static void
6691f_or(typval_T *argvars, typval_T *rettv)
6692{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006693 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
6694 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695}
6696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697#ifdef FEAT_PERL
6698/*
6699 * "perleval()" function
6700 */
6701 static void
6702f_perleval(typval_T *argvars, typval_T *rettv)
6703{
6704 char_u *str;
6705 char_u buf[NUMBUFLEN];
6706
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006707 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006708 do_perleval(str, rettv);
6709}
6710#endif
6711
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712/*
6713 * "prevnonblank()" function
6714 */
6715 static void
6716f_prevnonblank(typval_T *argvars, typval_T *rettv)
6717{
6718 linenr_T lnum;
6719
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006720 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
6722 lnum = 0;
6723 else
6724 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
6725 --lnum;
6726 rettv->vval.v_number = lnum;
6727}
6728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006729// This dummy va_list is here because:
6730// - passing a NULL pointer doesn't work when va_list isn't a pointer
6731// - locally in the function results in a "used before set" warning
6732// - using va_start() to initialize it gives "function with fixed args" error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006733static va_list ap;
6734
6735/*
6736 * "printf()" function
6737 */
6738 static void
6739f_printf(typval_T *argvars, typval_T *rettv)
6740{
6741 char_u buf[NUMBUFLEN];
6742 int len;
6743 char_u *s;
6744 int saved_did_emsg = did_emsg;
6745 char *fmt;
6746
6747 rettv->v_type = VAR_STRING;
6748 rettv->vval.v_string = NULL;
6749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006750 // Get the required length, allocate the buffer and do it for real.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006752 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006753 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754 if (!did_emsg)
6755 {
6756 s = alloc(len + 1);
6757 if (s != NULL)
6758 {
6759 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02006760 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
6761 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006762 }
6763 }
6764 did_emsg |= saved_did_emsg;
6765}
6766
6767/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006768 * "pum_getpos()" function
6769 */
6770 static void
6771f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6772{
6773 if (rettv_dict_alloc(rettv) != OK)
6774 return;
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006775 pum_set_event_info(rettv->vval.v_dict);
Bram Moolenaare9bd5722019-08-17 19:36:06 +02006776}
6777
6778/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779 * "pumvisible()" function
6780 */
6781 static void
6782f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6783{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784 if (pum_visible())
6785 rettv->vval.v_number = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006786}
6787
6788#ifdef FEAT_PYTHON3
6789/*
6790 * "py3eval()" function
6791 */
6792 static void
6793f_py3eval(typval_T *argvars, typval_T *rettv)
6794{
6795 char_u *str;
6796 char_u buf[NUMBUFLEN];
6797
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006798 if (check_restricted() || check_secure())
6799 return;
6800
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006801 if (p_pyx == 0)
6802 p_pyx = 3;
6803
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006804 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 do_py3eval(str, rettv);
6806}
6807#endif
6808
6809#ifdef FEAT_PYTHON
6810/*
6811 * "pyeval()" function
6812 */
6813 static void
6814f_pyeval(typval_T *argvars, typval_T *rettv)
6815{
6816 char_u *str;
6817 char_u buf[NUMBUFLEN];
6818
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006819 if (check_restricted() || check_secure())
6820 return;
6821
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006822 if (p_pyx == 0)
6823 p_pyx = 2;
6824
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006825 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826 do_pyeval(str, rettv);
6827}
6828#endif
6829
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006830#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
6831/*
6832 * "pyxeval()" function
6833 */
6834 static void
6835f_pyxeval(typval_T *argvars, typval_T *rettv)
6836{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006837 if (check_restricted() || check_secure())
6838 return;
6839
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006840# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
6841 init_pyxversion();
6842 if (p_pyx == 2)
6843 f_pyeval(argvars, rettv);
6844 else
6845 f_py3eval(argvars, rettv);
6846# elif defined(FEAT_PYTHON)
6847 f_pyeval(argvars, rettv);
6848# elif defined(FEAT_PYTHON3)
6849 f_py3eval(argvars, rettv);
6850# endif
6851}
6852#endif
6853
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006854static UINT32_T srand_seed_for_testing = 0;
6855static int srand_seed_for_testing_is_used = FALSE;
6856
6857 static void
6858f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED)
6859{
6860 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006861 srand_seed_for_testing_is_used = FALSE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006862 else
6863 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006864 srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]);
6865 srand_seed_for_testing_is_used = TRUE;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006866 }
6867}
6868
6869 static void
6870init_srand(UINT32_T *x)
6871{
6872#ifndef MSWIN
6873 static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
6874#endif
6875
6876 if (srand_seed_for_testing_is_used)
6877 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02006878 *x = srand_seed_for_testing;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006879 return;
6880 }
6881#ifndef MSWIN
6882 if (dev_urandom_state != FAIL)
6883 {
6884 int fd = open("/dev/urandom", O_RDONLY);
6885 struct {
6886 union {
6887 UINT32_T number;
6888 char bytes[sizeof(UINT32_T)];
6889 } contents;
6890 } buf;
6891
6892 // Attempt reading /dev/urandom.
6893 if (fd == -1)
6894 dev_urandom_state = FAIL;
6895 else
6896 {
6897 buf.contents.number = 0;
6898 if (read(fd, buf.contents.bytes, sizeof(UINT32_T))
6899 != sizeof(UINT32_T))
6900 dev_urandom_state = FAIL;
6901 else
6902 {
6903 dev_urandom_state = OK;
6904 *x = buf.contents.number;
6905 }
6906 close(fd);
6907 }
6908 }
6909 if (dev_urandom_state != OK)
6910 // Reading /dev/urandom doesn't work, fall back to time().
6911#endif
6912 *x = vim_time();
6913}
6914
6915#define ROTL(x, k) ((x << k) | (x >> (32 - k)))
6916#define SPLITMIX32(x, z) ( \
6917 z = (x += 0x9e3779b9), \
6918 z = (z ^ (z >> 16)) * 0x85ebca6b, \
6919 z = (z ^ (z >> 13)) * 0xc2b2ae35, \
6920 z ^ (z >> 16) \
6921 )
6922#define SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w) \
6923 result = ROTL(y * 5, 7) * 9; \
6924 t = y << 9; \
6925 z ^= x; \
6926 w ^= y; \
6927 y ^= z, x ^= w; \
6928 z ^= t; \
6929 w = ROTL(w, 11);
6930
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931/*
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006932 * "rand()" function
6933 */
6934 static void
6935f_rand(typval_T *argvars, typval_T *rettv)
6936{
6937 list_T *l = NULL;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006938 static UINT32_T gx, gy, gz, gw;
6939 static int initialized = FALSE;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006940 listitem_T *lx, *ly, *lz, *lw;
Bram Moolenaar0fd797e2020-11-05 20:46:32 +01006941 UINT32_T x = 0, y, z, w, t, result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006942
6943 if (argvars[0].v_type == VAR_UNKNOWN)
6944 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006945 // When no argument is given use the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006946 if (initialized == FALSE)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006947 {
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006948 // Initialize the global seed list.
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006949 init_srand(&x);
6950
6951 gx = SPLITMIX32(x, z);
6952 gy = SPLITMIX32(x, z);
6953 gz = SPLITMIX32(x, z);
6954 gw = SPLITMIX32(x, z);
6955 initialized = TRUE;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006956 }
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006957
6958 SHUFFLE_XOSHIRO128STARSTAR(gx, gy, gz, gw);
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006959 }
6960 else if (argvars[0].v_type == VAR_LIST)
6961 {
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006962 l = argvars[0].vval.v_list;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006963 if (l == NULL || list_len(l) != 4)
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006964 goto theend;
Bram Moolenaar4f645c52020-02-08 16:40:39 +01006965
6966 lx = list_find(l, 0L);
6967 ly = list_find(l, 1L);
6968 lz = list_find(l, 2L);
6969 lw = list_find(l, 3L);
6970 if (lx->li_tv.v_type != VAR_NUMBER) goto theend;
6971 if (ly->li_tv.v_type != VAR_NUMBER) goto theend;
6972 if (lz->li_tv.v_type != VAR_NUMBER) goto theend;
6973 if (lw->li_tv.v_type != VAR_NUMBER) goto theend;
6974 x = (UINT32_T)lx->li_tv.vval.v_number;
6975 y = (UINT32_T)ly->li_tv.vval.v_number;
6976 z = (UINT32_T)lz->li_tv.vval.v_number;
6977 w = (UINT32_T)lw->li_tv.vval.v_number;
6978
6979 SHUFFLE_XOSHIRO128STARSTAR(x, y, z, w);
6980
6981 lx->li_tv.vval.v_number = (varnumber_T)x;
6982 ly->li_tv.vval.v_number = (varnumber_T)y;
6983 lz->li_tv.vval.v_number = (varnumber_T)z;
6984 lw->li_tv.vval.v_number = (varnumber_T)w;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006985 }
6986 else
6987 goto theend;
6988
6989 rettv->v_type = VAR_NUMBER;
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006990 rettv->vval.v_number = (varnumber_T)result;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006991 return;
6992
6993theend:
6994 semsg(_(e_invarg2), tv_get_string(&argvars[0]));
Bram Moolenaarf8c1f922019-11-28 22:13:14 +01006995 rettv->v_type = VAR_NUMBER;
6996 rettv->vval.v_number = -1;
Bram Moolenaar06b0b4b2019-11-25 15:40:55 +01006997}
6998
6999/*
Bram Moolenaar4f645c52020-02-08 16:40:39 +01007000 * "srand()" function
7001 */
7002 static void
7003f_srand(typval_T *argvars, typval_T *rettv)
7004{
7005 UINT32_T x = 0, z;
7006
7007 if (rettv_list_alloc(rettv) == FAIL)
7008 return;
7009 if (argvars[0].v_type == VAR_UNKNOWN)
7010 {
7011 init_srand(&x);
7012 }
7013 else
7014 {
7015 int error = FALSE;
7016
7017 x = (UINT32_T)tv_get_number_chk(&argvars[0], &error);
7018 if (error)
7019 return;
7020 }
7021
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 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7025 list_append_number(rettv->vval.v_list, (varnumber_T)SPLITMIX32(x, z));
7026}
7027
7028#undef ROTL
7029#undef SPLITMIX32
7030#undef SHUFFLE_XOSHIRO128STARSTAR
7031
7032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033 * "range()" function
7034 */
7035 static void
7036f_range(typval_T *argvars, typval_T *rettv)
7037{
7038 varnumber_T start;
7039 varnumber_T end;
7040 varnumber_T stride = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 int error = FALSE;
7042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044 if (argvars[1].v_type == VAR_UNKNOWN)
7045 {
7046 end = start - 1;
7047 start = 0;
7048 }
7049 else
7050 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007051 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007053 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 }
7055
7056 if (error)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007057 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007059 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007061 emsg(_("E727: Start past end"));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007062 else if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007064 list_T *list = rettv->vval.v_list;
7065
7066 // Create a non-materialized list. This is much more efficient and
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007067 // works with ":for". If used otherwise CHECK_LIST_MATERIALIZE() must
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007068 // be called.
7069 list->lv_first = &range_list_item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01007070 list->lv_u.nonmat.lv_start = start;
7071 list->lv_u.nonmat.lv_end = end;
7072 list->lv_u.nonmat.lv_stride = stride;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01007073 list->lv_len = (end - start) / stride + 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 }
7075}
7076
7077/*
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007078 * Materialize "list".
7079 * Do not call directly, use CHECK_LIST_MATERIALIZE()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080 */
7081 void
7082range_list_materialize(list_T *list)
7083{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007084 varnumber_T start = list->lv_u.nonmat.lv_start;
7085 varnumber_T end = list->lv_u.nonmat.lv_end;
7086 int stride = list->lv_u.nonmat.lv_stride;
7087 varnumber_T i;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007088
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02007089 list->lv_first = NULL;
7090 list->lv_u.mat.lv_last = NULL;
7091 list->lv_len = 0;
7092 list->lv_u.mat.lv_idx_item = NULL;
7093 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
7094 if (list_append_number(list, (varnumber_T)i) == FAIL)
7095 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007096}
7097
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007098/*
7099 * "getreginfo()" function
7100 */
7101 static void
7102f_getreginfo(typval_T *argvars, typval_T *rettv)
7103{
7104 char_u *strregname;
7105 int regname;
7106 char_u buf[NUMBUFLEN + 2];
7107 long reglen = 0;
7108 dict_T *dict;
7109 list_T *list;
7110
7111 if (argvars[0].v_type != VAR_UNKNOWN)
7112 {
7113 strregname = tv_get_string_chk(&argvars[0]);
7114 if (strregname == NULL)
7115 return;
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007116 if (in_vim9script() && STRLEN(strregname) > 1)
7117 {
7118 semsg(_(e_register_name_must_be_one_char_str), strregname);
7119 return;
7120 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007121 }
7122 else
7123 strregname = get_vim_var_str(VV_REG);
7124
7125 regname = (strregname == NULL ? '"' : *strregname);
7126 if (regname == 0 || regname == '@')
7127 regname = '"';
7128
7129 if (rettv_dict_alloc(rettv) == FAIL)
7130 return;
7131 dict = rettv->vval.v_dict;
7132
7133 list = (list_T *)get_reg_contents(regname, GREG_EXPR_SRC | GREG_LIST);
7134 if (list == NULL)
7135 return;
Bram Moolenaar91639192020-06-29 19:55:58 +02007136 (void)dict_add_list(dict, "regcontents", list);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007137
7138 buf[0] = NUL;
7139 buf[1] = NUL;
7140 switch (get_reg_type(regname, &reglen))
7141 {
7142 case MLINE: buf[0] = 'V'; break;
7143 case MCHAR: buf[0] = 'v'; break;
7144 case MBLOCK:
7145 vim_snprintf((char *)buf, sizeof(buf), "%c%ld", Ctrl_V,
7146 reglen + 1);
7147 break;
7148 }
Bram Moolenaar91639192020-06-29 19:55:58 +02007149 (void)dict_add_string(dict, (char *)"regtype", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007150
7151 buf[0] = get_register_name(get_unname_register());
7152 buf[1] = NUL;
7153 if (regname == '"')
Bram Moolenaar91639192020-06-29 19:55:58 +02007154 (void)dict_add_string(dict, (char *)"points_to", buf);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007155 else
7156 {
7157 dictitem_T *item = dictitem_alloc((char_u *)"isunnamed");
7158
7159 if (item != NULL)
7160 {
7161 item->di_tv.v_type = VAR_SPECIAL;
7162 item->di_tv.vval.v_number = regname == buf[0]
Bram Moolenaar418a29f2021-02-10 22:23:41 +01007163 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar91639192020-06-29 19:55:58 +02007164 (void)dict_add(dict, item);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02007165 }
7166 }
7167}
7168
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007169 static void
7170return_register(int regname, typval_T *rettv)
7171{
7172 char_u buf[2] = {0, 0};
7173
7174 buf[0] = (char_u)regname;
7175 rettv->v_type = VAR_STRING;
7176 rettv->vval.v_string = vim_strsave(buf);
7177}
7178
7179/*
7180 * "reg_executing()" function
7181 */
7182 static void
7183f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
7184{
7185 return_register(reg_executing, rettv);
7186}
7187
7188/*
7189 * "reg_recording()" function
7190 */
7191 static void
7192f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
7193{
7194 return_register(reg_recording, rettv);
7195}
7196
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01007197/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 * "rename({from}, {to})" function
7199 */
7200 static void
7201f_rename(typval_T *argvars, typval_T *rettv)
7202{
7203 char_u buf[NUMBUFLEN];
7204
7205 if (check_restricted() || check_secure())
7206 rettv->vval.v_number = -1;
7207 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007208 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
7209 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210}
7211
7212/*
7213 * "repeat()" function
7214 */
7215 static void
7216f_repeat(typval_T *argvars, typval_T *rettv)
7217{
7218 char_u *p;
7219 int n;
7220 int slen;
7221 int len;
7222 char_u *r;
7223 int i;
7224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007225 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226 if (argvars[0].v_type == VAR_LIST)
7227 {
7228 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
7229 while (n-- > 0)
7230 if (list_extend(rettv->vval.v_list,
7231 argvars[0].vval.v_list, NULL) == FAIL)
7232 break;
7233 }
7234 else
7235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007236 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237 rettv->v_type = VAR_STRING;
7238 rettv->vval.v_string = NULL;
7239
7240 slen = (int)STRLEN(p);
7241 len = slen * n;
7242 if (len <= 0)
7243 return;
7244
7245 r = alloc(len + 1);
7246 if (r != NULL)
7247 {
7248 for (i = 0; i < n; i++)
7249 mch_memmove(r + i * slen, p, (size_t)slen);
7250 r[len] = NUL;
7251 }
7252
7253 rettv->vval.v_string = r;
7254 }
7255}
7256
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007257#define SP_NOMOVE 0x01 // don't move cursor
7258#define SP_REPEAT 0x02 // repeat to find outer pair
7259#define SP_RETCOUNT 0x04 // return matchcount
7260#define SP_SETPCMARK 0x08 // set previous context mark
7261#define SP_START 0x10 // accept match at start position
7262#define SP_SUBPAT 0x20 // return nr of matching sub-pattern
7263#define SP_END 0x40 // leave cursor at end of match
7264#define SP_COLUMN 0x80 // start at cursor column
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007265
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007266/*
7267 * Get flags for a search function.
7268 * Possibly sets "p_ws".
7269 * Returns BACKWARD, FORWARD or zero (for an error).
7270 */
7271 static int
7272get_search_arg(typval_T *varp, int *flagsp)
7273{
7274 int dir = FORWARD;
7275 char_u *flags;
7276 char_u nbuf[NUMBUFLEN];
7277 int mask;
7278
7279 if (varp->v_type != VAR_UNKNOWN)
7280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007281 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 if (flags == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007283 return 0; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007284 while (*flags != NUL)
7285 {
7286 switch (*flags)
7287 {
7288 case 'b': dir = BACKWARD; break;
7289 case 'w': p_ws = TRUE; break;
7290 case 'W': p_ws = FALSE; break;
7291 default: mask = 0;
7292 if (flagsp != NULL)
7293 switch (*flags)
7294 {
7295 case 'c': mask = SP_START; break;
7296 case 'e': mask = SP_END; break;
7297 case 'm': mask = SP_RETCOUNT; break;
7298 case 'n': mask = SP_NOMOVE; break;
7299 case 'p': mask = SP_SUBPAT; break;
7300 case 'r': mask = SP_REPEAT; break;
7301 case 's': mask = SP_SETPCMARK; break;
7302 case 'z': mask = SP_COLUMN; break;
7303 }
7304 if (mask == 0)
7305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007306 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 dir = 0;
7308 }
7309 else
7310 *flagsp |= mask;
7311 }
7312 if (dir == 0)
7313 break;
7314 ++flags;
7315 }
7316 }
7317 return dir;
7318}
7319
7320/*
7321 * Shared by search() and searchpos() functions.
7322 */
7323 static int
7324search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
7325{
7326 int flags;
7327 char_u *pat;
7328 pos_T pos;
7329 pos_T save_cursor;
7330 int save_p_ws = p_ws;
7331 int dir;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007332 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333 long lnum_stop = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334#ifdef FEAT_RELTIME
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007335 proftime_T tm;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 long time_limit = 0;
7337#endif
7338 int options = SEARCH_KEEP;
7339 int subpatnum;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007340 searchit_arg_T sia;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007341 int use_skip = FALSE;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007342 pos_T firstpos;
7343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007344 pat = tv_get_string(&argvars[0]);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007345 dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 if (dir == 0)
7347 goto theend;
7348 flags = *flagsp;
7349 if (flags & SP_START)
7350 options |= SEARCH_START;
7351 if (flags & SP_END)
7352 options |= SEARCH_END;
7353 if (flags & SP_COLUMN)
7354 options |= SEARCH_COL;
7355
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007356 // Optional arguments: line number to stop searching, timeout and skip.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
7358 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007359 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 if (lnum_stop < 0)
7361 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007362 if (argvars[3].v_type != VAR_UNKNOWN)
7363 {
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007364#ifdef FEAT_RELTIME
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 if (time_limit < 0)
7367 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007368#endif
Bram Moolenaara9c01042020-06-07 14:50:50 +02007369 use_skip = eval_expr_valid_arg(&argvars[4]);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007370 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371 }
7372
7373#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007374 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 profile_setlimit(time_limit, &tm);
7376#endif
7377
7378 /*
7379 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
7380 * Check to make sure only those flags are set.
7381 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
7382 * flags cannot be set. Check for that condition also.
7383 */
7384 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
7385 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 goto theend;
7389 }
7390
7391 pos = save_cursor = curwin->w_cursor;
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007392 CLEAR_FIELD(firstpos);
Bram Moolenaara80faa82020-04-12 19:37:17 +02007393 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007394 sia.sa_stop_lnum = (linenr_T)lnum_stop;
7395#ifdef FEAT_RELTIME
7396 sia.sa_tm = &tm;
7397#endif
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007398
7399 // Repeat until {skip} returns FALSE.
7400 for (;;)
7401 {
7402 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007403 options, RE_SEARCH, &sia);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007404 // finding the first match again means there is no match where {skip}
7405 // evaluates to zero.
7406 if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
7407 subpatnum = FAIL;
7408
Bram Moolenaara9c01042020-06-07 14:50:50 +02007409 if (subpatnum == FAIL || !use_skip)
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007410 // didn't find it or no skip argument
7411 break;
7412 firstpos = pos;
7413
Bram Moolenaara9c01042020-06-07 14:50:50 +02007414 // If the skip expression matches, ignore this match.
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007415 {
7416 int do_skip;
7417 int err;
7418 pos_T save_pos = curwin->w_cursor;
7419
7420 curwin->w_cursor = pos;
Bram Moolenaara9c01042020-06-07 14:50:50 +02007421 err = FALSE;
7422 do_skip = eval_expr_to_bool(&argvars[4], &err);
Bram Moolenaaradc17a52020-06-06 18:37:51 +02007423 curwin->w_cursor = save_pos;
7424 if (err)
7425 {
7426 // Evaluating {skip} caused an error, break here.
7427 subpatnum = FAIL;
7428 break;
7429 }
7430 if (!do_skip)
7431 break;
7432 }
7433 }
7434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 if (subpatnum != FAIL)
7436 {
7437 if (flags & SP_SUBPAT)
7438 retval = subpatnum;
7439 else
7440 retval = pos.lnum;
7441 if (flags & SP_SETPCMARK)
7442 setpcmark();
7443 curwin->w_cursor = pos;
7444 if (match_pos != NULL)
7445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007446 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 match_pos->lnum = pos.lnum;
7448 match_pos->col = pos.col + 1;
7449 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007450 // "/$" will put the cursor after the end of the line, may need to
7451 // correct that here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007452 check_cursor();
7453 }
7454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007455 // If 'n' flag is used: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456 if (flags & SP_NOMOVE)
7457 curwin->w_cursor = save_cursor;
7458 else
7459 curwin->w_set_curswant = TRUE;
7460theend:
7461 p_ws = save_p_ws;
7462
7463 return retval;
7464}
7465
Bram Moolenaare99be0e2019-03-26 22:51:09 +01007466#ifdef FEAT_RUBY
7467/*
7468 * "rubyeval()" function
7469 */
7470 static void
7471f_rubyeval(typval_T *argvars, typval_T *rettv)
7472{
7473 char_u *str;
7474 char_u buf[NUMBUFLEN];
7475
7476 str = tv_get_string_buf(&argvars[0], buf);
7477 do_rubyeval(str, rettv);
7478}
7479#endif
7480
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007481/*
7482 * "screenattr()" function
7483 */
7484 static void
7485f_screenattr(typval_T *argvars, typval_T *rettv)
7486{
7487 int row;
7488 int col;
7489 int c;
7490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007491 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7492 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493 if (row < 0 || row >= screen_Rows
7494 || col < 0 || col >= screen_Columns)
7495 c = -1;
7496 else
7497 c = ScreenAttrs[LineOffset[row] + col];
7498 rettv->vval.v_number = c;
7499}
7500
7501/*
7502 * "screenchar()" function
7503 */
7504 static void
7505f_screenchar(typval_T *argvars, typval_T *rettv)
7506{
7507 int row;
7508 int col;
7509 int off;
7510 int c;
7511
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007512 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7513 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007514 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 c = -1;
7516 else
7517 {
7518 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 if (enc_utf8 && ScreenLinesUC[off] != 0)
7520 c = ScreenLinesUC[off];
7521 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 c = ScreenLines[off];
7523 }
7524 rettv->vval.v_number = c;
7525}
7526
7527/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007528 * "screenchars()" function
7529 */
7530 static void
7531f_screenchars(typval_T *argvars, typval_T *rettv)
7532{
7533 int row;
7534 int col;
7535 int off;
7536 int c;
7537 int i;
7538
7539 if (rettv_list_alloc(rettv) == FAIL)
7540 return;
7541 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7542 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7543 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7544 return;
7545
7546 off = LineOffset[row] + col;
7547 if (enc_utf8 && ScreenLinesUC[off] != 0)
7548 c = ScreenLinesUC[off];
7549 else
7550 c = ScreenLines[off];
7551 list_append_number(rettv->vval.v_list, (varnumber_T)c);
7552
7553 if (enc_utf8)
7554
7555 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7556 list_append_number(rettv->vval.v_list,
7557 (varnumber_T)ScreenLinesC[i][off]);
7558}
7559
7560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 * "screencol()" function
7562 *
7563 * First column is 1 to be consistent with virtcol().
7564 */
7565 static void
7566f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
7567{
7568 rettv->vval.v_number = screen_screencol() + 1;
7569}
7570
7571/*
7572 * "screenrow()" function
7573 */
7574 static void
7575f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
7576{
7577 rettv->vval.v_number = screen_screenrow() + 1;
7578}
7579
7580/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007581 * "screenstring()" function
7582 */
7583 static void
7584f_screenstring(typval_T *argvars, typval_T *rettv)
7585{
7586 int row;
7587 int col;
7588 int off;
7589 int c;
7590 int i;
7591 char_u buf[MB_MAXBYTES + 1];
7592 int buflen = 0;
7593
7594 rettv->vval.v_string = NULL;
7595 rettv->v_type = VAR_STRING;
7596
7597 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
7598 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
7599 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
7600 return;
7601
7602 off = LineOffset[row] + col;
7603 if (enc_utf8 && ScreenLinesUC[off] != 0)
7604 c = ScreenLinesUC[off];
7605 else
7606 c = ScreenLines[off];
7607 buflen += mb_char2bytes(c, buf);
7608
Bram Moolenaarf1387282021-03-22 17:11:15 +01007609 if (enc_utf8 && ScreenLinesUC[off] != 0)
Bram Moolenaar2912abb2019-03-29 14:16:42 +01007610 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
7611 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
7612
7613 buf[buflen] = NUL;
7614 rettv->vval.v_string = vim_strsave(buf);
7615}
7616
7617/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 * "search()" function
7619 */
7620 static void
7621f_search(typval_T *argvars, typval_T *rettv)
7622{
7623 int flags = 0;
7624
7625 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
7626}
7627
7628/*
7629 * "searchdecl()" function
7630 */
7631 static void
7632f_searchdecl(typval_T *argvars, typval_T *rettv)
7633{
Bram Moolenaar30788d32020-09-05 21:35:16 +02007634 int locally = TRUE;
7635 int thisblock = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007636 int error = FALSE;
7637 char_u *name;
7638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007639 rettv->vval.v_number = 1; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007641 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 if (argvars[1].v_type != VAR_UNKNOWN)
7643 {
Bram Moolenaar30788d32020-09-05 21:35:16 +02007644 locally = !(int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar30788d32020-09-05 21:35:16 +02007646 thisblock = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 }
7648 if (!error && name != NULL)
7649 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
7650 locally, thisblock, SEARCH_KEEP) == FAIL;
7651}
7652
7653/*
7654 * Used by searchpair() and searchpairpos()
7655 */
7656 static int
7657searchpair_cmn(typval_T *argvars, pos_T *match_pos)
7658{
7659 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01007660 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 int save_p_ws = p_ws;
7662 int dir;
7663 int flags = 0;
7664 char_u nbuf1[NUMBUFLEN];
7665 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007666 int retval = 0; // default: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 long lnum_stop = 0;
7668 long time_limit = 0;
7669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007670 // Get the three pattern arguments: start, middle, end. Will result in an
7671 // error if not a valid argument.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007672 spat = tv_get_string_chk(&argvars[0]);
7673 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
7674 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 if (spat == NULL || mpat == NULL || epat == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007676 goto theend; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007678 // Handle the optional fourth argument: flags
7679 dir = get_search_arg(&argvars[3], &flags); // may set p_ws
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 if (dir == 0)
7681 goto theend;
7682
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007683 // Don't accept SP_END or SP_SUBPAT.
7684 // Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 if ((flags & (SP_END | SP_SUBPAT)) != 0
7686 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
7687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689 goto theend;
7690 }
7691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007692 // Using 'r' implies 'W', otherwise it doesn't work.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693 if (flags & SP_REPEAT)
7694 p_ws = FALSE;
7695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007696 // Optional fifth argument: skip expression
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 if (argvars[3].v_type == VAR_UNKNOWN
7698 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01007699 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 else
7701 {
Bram Moolenaara9c01042020-06-07 14:50:50 +02007702 // Type is checked later.
Bram Moolenaar48570482017-10-30 21:48:41 +01007703 skip = &argvars[4];
Bram Moolenaara9c01042020-06-07 14:50:50 +02007704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 if (argvars[5].v_type != VAR_UNKNOWN)
7706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007707 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007712 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713#ifdef FEAT_RELTIME
7714 if (argvars[6].v_type != VAR_UNKNOWN)
7715 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007716 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007717 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007719 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02007721 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007722 }
7723#endif
7724 }
7725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726
7727 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
7728 match_pos, lnum_stop, time_limit);
7729
7730theend:
7731 p_ws = save_p_ws;
7732
7733 return retval;
7734}
7735
7736/*
7737 * "searchpair()" function
7738 */
7739 static void
7740f_searchpair(typval_T *argvars, typval_T *rettv)
7741{
7742 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
7743}
7744
7745/*
7746 * "searchpairpos()" function
7747 */
7748 static void
7749f_searchpairpos(typval_T *argvars, typval_T *rettv)
7750{
7751 pos_T match_pos;
7752 int lnum = 0;
7753 int col = 0;
7754
7755 if (rettv_list_alloc(rettv) == FAIL)
7756 return;
7757
7758 if (searchpair_cmn(argvars, &match_pos) > 0)
7759 {
7760 lnum = match_pos.lnum;
7761 col = match_pos.col;
7762 }
7763
7764 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7765 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7766}
7767
7768/*
7769 * Search for a start/middle/end thing.
7770 * Used by searchpair(), see its documentation for the details.
7771 * Returns 0 or -1 for no match,
7772 */
7773 long
7774do_searchpair(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007775 char_u *spat, // start pattern
7776 char_u *mpat, // middle pattern
7777 char_u *epat, // end pattern
7778 int dir, // BACKWARD or FORWARD
7779 typval_T *skip, // skip expression
7780 int flags, // SP_SETPCMARK and other SP_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007781 pos_T *match_pos,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007782 linenr_T lnum_stop, // stop at this line if not zero
7783 long time_limit UNUSED) // stop after this many msec
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007784{
7785 char_u *save_cpo;
7786 char_u *pat, *pat2 = NULL, *pat3 = NULL;
7787 long retval = 0;
7788 pos_T pos;
7789 pos_T firstpos;
7790 pos_T foundpos;
7791 pos_T save_cursor;
7792 pos_T save_pos;
7793 int n;
7794 int r;
7795 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01007796 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 int err;
7798 int options = SEARCH_KEEP;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007799#ifdef FEAT_RELTIME
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 proftime_T tm;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007801#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007803 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 save_cpo = p_cpo;
7805 p_cpo = empty_option;
7806
7807#ifdef FEAT_RELTIME
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007808 // Set the time limit, if there is one.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 profile_setlimit(time_limit, &tm);
7810#endif
7811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007812 // Make two search patterns: start/end (pat2, for in nested pairs) and
7813 // start/middle/end (pat3, for the top pair).
Bram Moolenaar964b3742019-05-24 18:54:09 +02007814 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
7815 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 if (pat2 == NULL || pat3 == NULL)
7817 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007818 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 if (*mpat == NUL)
7820 STRCPY(pat3, pat2);
7821 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01007822 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823 spat, epat, mpat);
7824 if (flags & SP_START)
7825 options |= SEARCH_START;
7826
Bram Moolenaar48570482017-10-30 21:48:41 +01007827 if (skip != NULL)
Bram Moolenaara9c01042020-06-07 14:50:50 +02007828 use_skip = eval_expr_valid_arg(skip);
Bram Moolenaar48570482017-10-30 21:48:41 +01007829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 save_cursor = curwin->w_cursor;
7831 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007832 CLEAR_POS(&firstpos);
7833 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 pat = pat3;
7835 for (;;)
7836 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007837 searchit_arg_T sia;
7838
Bram Moolenaara80faa82020-04-12 19:37:17 +02007839 CLEAR_FIELD(sia);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007840 sia.sa_stop_lnum = lnum_stop;
7841#ifdef FEAT_RELTIME
7842 sia.sa_tm = &tm;
7843#endif
Bram Moolenaar5d24a222018-12-23 19:10:09 +01007844 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02007845 options, RE_SEARCH, &sia);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007846 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007847 // didn't find it or found the first match again: FAIL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 break;
7849
7850 if (firstpos.lnum == 0)
7851 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007852 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007854 // Found the same position again. Can happen with a pattern that
7855 // has "\zs" at the end and searching backwards. Advance one
7856 // character and try again.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857 if (dir == BACKWARD)
7858 decl(&pos);
7859 else
7860 incl(&pos);
7861 }
7862 foundpos = pos;
7863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007864 // clear the start flag to avoid getting stuck here
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007865 options &= ~SEARCH_START;
7866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007867 // If the skip pattern matches, ignore this match.
Bram Moolenaar48570482017-10-30 21:48:41 +01007868 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 {
7870 save_pos = curwin->w_cursor;
7871 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01007872 err = FALSE;
7873 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 curwin->w_cursor = save_pos;
7875 if (err)
7876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007877 // Evaluating {skip} caused an error, break here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 curwin->w_cursor = save_cursor;
7879 retval = -1;
7880 break;
7881 }
7882 if (r)
7883 continue;
7884 }
7885
7886 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
7887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007888 // Found end when searching backwards or start when searching
7889 // forward: nested pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890 ++nest;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007891 pat = pat2; // nested, don't search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892 }
7893 else
7894 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007895 // Found end when searching forward or start when searching
7896 // backward: end of (nested) pair; or found middle in outer pair.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 if (--nest == 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007898 pat = pat3; // outer level, search for middle
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007899 }
7900
7901 if (nest == 0)
7902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007903 // Found the match: return matchcount or line number.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 if (flags & SP_RETCOUNT)
7905 ++retval;
7906 else
7907 retval = pos.lnum;
7908 if (flags & SP_SETPCMARK)
7909 setpcmark();
7910 curwin->w_cursor = pos;
7911 if (!(flags & SP_REPEAT))
7912 break;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007913 nest = 1; // search for next unmatched
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007914 }
7915 }
7916
7917 if (match_pos != NULL)
7918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007919 // Store the match cursor position
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 match_pos->lnum = curwin->w_cursor.lnum;
7921 match_pos->col = curwin->w_cursor.col + 1;
7922 }
7923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007924 // If 'n' flag is used or search failed: restore cursor position.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 if ((flags & SP_NOMOVE) || retval == 0)
7926 curwin->w_cursor = save_cursor;
7927
7928theend:
7929 vim_free(pat2);
7930 vim_free(pat3);
7931 if (p_cpo == empty_option)
7932 p_cpo = save_cpo;
7933 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007935 // Darn, evaluating the {skip} expression changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007936 // If it's still empty it was changed and restored, need to restore in
7937 // the complicated way.
7938 if (*p_cpo == NUL)
7939 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007940 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007941 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942
7943 return retval;
7944}
7945
7946/*
7947 * "searchpos()" function
7948 */
7949 static void
7950f_searchpos(typval_T *argvars, typval_T *rettv)
7951{
7952 pos_T match_pos;
7953 int lnum = 0;
7954 int col = 0;
7955 int n;
7956 int flags = 0;
7957
7958 if (rettv_list_alloc(rettv) == FAIL)
7959 return;
7960
7961 n = search_cmn(argvars, &match_pos, &flags);
7962 if (n > 0)
7963 {
7964 lnum = match_pos.lnum;
7965 col = match_pos.col;
7966 }
7967
7968 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
7969 list_append_number(rettv->vval.v_list, (varnumber_T)col);
7970 if (flags & SP_SUBPAT)
7971 list_append_number(rettv->vval.v_list, (varnumber_T)n);
7972}
7973
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007974/*
7975 * Set the cursor or mark position.
Christian Brabandt98703d72021-06-24 19:55:27 +02007976 * If 'charpos' is TRUE, then use the column number as a character offset.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01007977 * Otherwise use the column number as a byte offset.
7978 */
7979 static void
7980set_position(typval_T *argvars, typval_T *rettv, int charpos)
7981{
7982 pos_T pos;
7983 int fnum;
7984 char_u *name;
7985 colnr_T curswant = -1;
7986
7987 rettv->vval.v_number = -1;
7988
7989 name = tv_get_string_chk(argvars);
7990 if (name != NULL)
7991 {
7992 if (list2fpos(&argvars[1], &pos, &fnum, &curswant, charpos) == OK)
7993 {
7994 if (pos.col != MAXCOL && --pos.col < 0)
7995 pos.col = 0;
7996 if ((name[0] == '.' && name[1] == NUL))
7997 {
7998 // set cursor; "fnum" is ignored
7999 curwin->w_cursor = pos;
8000 if (curswant >= 0)
8001 {
8002 curwin->w_curswant = curswant - 1;
8003 curwin->w_set_curswant = FALSE;
8004 }
8005 check_cursor();
8006 rettv->vval.v_number = 0;
8007 }
8008 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
8009 {
8010 // set mark
8011 if (setmark_pos(name[1], &pos, fnum) == OK)
8012 rettv->vval.v_number = 0;
8013 }
8014 else
8015 emsg(_(e_invarg));
8016 }
8017 }
8018}
8019/*
8020 * "setcharpos()" function
8021 */
8022 static void
8023f_setcharpos(typval_T *argvars, typval_T *rettv)
8024{
8025 set_position(argvars, rettv, TRUE);
8026}
8027
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008028 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
8030{
8031 dict_T *d;
8032 dictitem_T *di;
8033 char_u *csearch;
8034
8035 if (argvars[0].v_type != VAR_DICT)
8036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008037 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 return;
8039 }
8040
8041 if ((d = argvars[0].vval.v_dict) != NULL)
8042 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01008043 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008044 if (csearch != NULL)
8045 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 if (enc_utf8)
8047 {
8048 int pcc[MAX_MCO];
8049 int c = utfc_ptr2char(csearch, pcc);
8050
8051 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
8052 }
8053 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 set_last_csearch(PTR2CHAR(csearch),
Bram Moolenaar1614a142019-10-06 22:00:13 +02008055 csearch, mb_ptr2len(csearch));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008056 }
8057
8058 di = dict_find(d, (char_u *)"forward", -1);
8059 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008060 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 ? FORWARD : BACKWARD);
8062
8063 di = dict_find(d, (char_u *)"until", -1);
8064 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008065 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008066 }
8067}
8068
8069/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008070 * "setcursorcharpos" function
8071 */
8072 static void
Bram Moolenaar3af15ab2021-01-17 16:16:23 +01008073f_setcursorcharpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008074{
8075 set_cursorpos(argvars, rettv, TRUE);
8076}
8077
8078/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02008079 * "setenv()" function
8080 */
8081 static void
8082f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
8083{
8084 char_u namebuf[NUMBUFLEN];
8085 char_u valbuf[NUMBUFLEN];
8086 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
8087
8088 if (argvars[1].v_type == VAR_SPECIAL
8089 && argvars[1].vval.v_number == VVAL_NULL)
8090 vim_unsetenv(name);
8091 else
8092 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
8093}
8094
8095/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 * "setfperm({fname}, {mode})" function
8097 */
8098 static void
8099f_setfperm(typval_T *argvars, typval_T *rettv)
8100{
8101 char_u *fname;
8102 char_u modebuf[NUMBUFLEN];
8103 char_u *mode_str;
8104 int i;
8105 int mask;
8106 int mode = 0;
8107
8108 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008109 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 if (fname == NULL)
8111 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008112 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 if (mode_str == NULL)
8114 return;
8115 if (STRLEN(mode_str) != 9)
8116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008117 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 return;
8119 }
8120
8121 mask = 1;
8122 for (i = 8; i >= 0; --i)
8123 {
8124 if (mode_str[i] != '-')
8125 mode |= mask;
8126 mask = mask << 1;
8127 }
8128 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
8129}
8130
8131/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 * "setpos()" function
8133 */
8134 static void
8135f_setpos(typval_T *argvars, typval_T *rettv)
8136{
Bram Moolenaar6f02b002021-01-10 20:22:54 +01008137 set_position(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138}
8139
8140/*
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008141 * Translate a register type string to the yank type and block length
8142 */
8143 static int
8144get_yank_type(char_u **pp, char_u *yank_type, long *block_len)
8145{
8146 char_u *stropt = *pp;
8147 switch (*stropt)
8148 {
8149 case 'v': case 'c': // character-wise selection
8150 *yank_type = MCHAR;
8151 break;
8152 case 'V': case 'l': // line-wise selection
8153 *yank_type = MLINE;
8154 break;
8155 case 'b': case Ctrl_V: // block-wise selection
8156 *yank_type = MBLOCK;
8157 if (VIM_ISDIGIT(stropt[1]))
8158 {
8159 ++stropt;
8160 *block_len = getdigits(&stropt) - 1;
8161 --stropt;
8162 }
8163 break;
8164 default:
8165 return FAIL;
8166 }
8167 *pp = stropt;
8168 return OK;
8169}
8170
8171/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 * "setreg()" function
8173 */
8174 static void
8175f_setreg(typval_T *argvars, typval_T *rettv)
8176{
8177 int regname;
8178 char_u *strregname;
8179 char_u *stropt;
8180 char_u *strval;
8181 int append;
8182 char_u yank_type;
8183 long block_len;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008184 typval_T *regcontents;
8185 int pointreg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008187 pointreg = 0;
8188 regcontents = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 block_len = -1;
8190 yank_type = MAUTO;
8191 append = FALSE;
8192
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008193 strregname = tv_get_string_chk(argvars);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008194 rettv->vval.v_number = 1; // FAIL is default
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195
8196 if (strregname == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008197 return; // type error; errmsg already given
Bram Moolenaar418a29f2021-02-10 22:23:41 +01008198 if (in_vim9script() && STRLEN(strregname) > 1)
8199 {
8200 semsg(_(e_register_name_must_be_one_char_str), strregname);
8201 return;
8202 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 regname = *strregname;
8204 if (regname == 0 || regname == '@')
8205 regname = '"';
8206
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008207 if (argvars[1].v_type == VAR_DICT)
8208 {
8209 dict_T *d = argvars[1].vval.v_dict;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008210 dictitem_T *di;
8211
8212 if (d == NULL || d->dv_hashtab.ht_used == 0)
8213 {
8214 // Empty dict, clear the register (like setreg(0, []))
8215 char_u *lstval[2] = {NULL, NULL};
8216 write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1);
8217 return;
8218 }
8219
8220 di = dict_find(d, (char_u *)"regcontents", -1);
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008221 if (di != NULL)
8222 regcontents = &di->di_tv;
8223
8224 stropt = dict_get_string(d, (char_u *)"regtype", FALSE);
8225 if (stropt != NULL)
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008226 {
8227 int ret = get_yank_type(&stropt, &yank_type, &block_len);
8228
8229 if (ret == FAIL || *++stropt != NUL)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008230 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008231 semsg(_(e_invargval), "value");
8232 return;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008233 }
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008234 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008235
8236 if (regname == '"')
8237 {
8238 stropt = dict_get_string(d, (char_u *)"points_to", FALSE);
8239 if (stropt != NULL)
8240 {
8241 pointreg = *stropt;
8242 regname = pointreg;
8243 }
8244 }
Bram Moolenaar6a950582020-08-28 16:39:33 +02008245 else if (dict_get_bool(d, (char_u *)"isunnamed", -1) > 0)
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008246 pointreg = regname;
8247 }
8248 else
8249 regcontents = &argvars[1];
8250
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008251 if (argvars[2].v_type != VAR_UNKNOWN)
8252 {
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008253 if (yank_type != MAUTO)
8254 {
8255 semsg(_(e_toomanyarg), "setreg");
8256 return;
8257 }
8258
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008259 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 if (stropt == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008261 return; // type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 for (; *stropt != NUL; ++stropt)
8263 switch (*stropt)
8264 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008265 case 'a': case 'A': // append
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 append = TRUE;
8267 break;
Bram Moolenaar7633fe52020-06-22 19:10:56 +02008268 default:
8269 get_yank_type(&stropt, &yank_type, &block_len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 }
8271 }
8272
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008273 if (regcontents && regcontents->v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008274 {
8275 char_u **lstval;
8276 char_u **allocval;
8277 char_u buf[NUMBUFLEN];
8278 char_u **curval;
8279 char_u **curallocval;
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008280 list_T *ll = regcontents->vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281 listitem_T *li;
8282 int len;
8283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008284 // If the list is NULL handle like an empty list.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 len = ll == NULL ? 0 : ll->lv_len;
8286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008287 // First half: use for pointers to result lines; second half: use for
8288 // pointers to allocated copies.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008289 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 if (lstval == NULL)
8291 return;
8292 curval = lstval;
8293 allocval = lstval + len + 2;
8294 curallocval = allocval;
8295
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008296 if (ll != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02008298 CHECK_LIST_MATERIALIZE(ll);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008299 FOR_ALL_LIST_ITEMS(ll, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008301 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 if (strval == NULL)
8303 goto free_lstval;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01008304 if (strval == buf)
8305 {
8306 // Need to make a copy, next tv_get_string_buf_chk() will
8307 // overwrite the string.
8308 strval = vim_strsave(buf);
8309 if (strval == NULL)
8310 goto free_lstval;
8311 *curallocval++ = strval;
8312 }
8313 *curval++ = strval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 }
8316 *curval++ = NULL;
8317
8318 write_reg_contents_lst(regname, lstval, -1,
8319 append, yank_type, block_len);
8320free_lstval:
8321 while (curallocval > allocval)
8322 vim_free(*--curallocval);
8323 vim_free(lstval);
8324 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008325 else if (regcontents)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 {
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008327 strval = tv_get_string_chk(regcontents);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 if (strval == NULL)
8329 return;
8330 write_reg_contents_ex(regname, strval, -1,
8331 append, yank_type, block_len);
8332 }
Bram Moolenaarbb861e22020-06-07 18:16:36 +02008333 if (pointreg != 0)
8334 get_yank_register(pointreg, TRUE);
8335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336 rettv->vval.v_number = 0;
8337}
8338
8339/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008340 * "settagstack()" function
8341 */
8342 static void
8343f_settagstack(typval_T *argvars, typval_T *rettv)
8344{
8345 static char *e_invact2 = N_("E962: Invalid action: '%s'");
8346 win_T *wp;
8347 dict_T *d;
8348 int action = 'r';
8349
8350 rettv->vval.v_number = -1;
8351
8352 // first argument: window number or id
8353 wp = find_win_by_nr_or_id(&argvars[0]);
8354 if (wp == NULL)
8355 return;
8356
8357 // second argument: dict with items to set in the tag stack
8358 if (argvars[1].v_type != VAR_DICT)
8359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008360 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008361 return;
8362 }
8363 d = argvars[1].vval.v_dict;
8364 if (d == NULL)
8365 return;
8366
8367 // third argument: action - 'a' for append and 'r' for replace.
8368 // default is to replace the stack.
8369 if (argvars[2].v_type == VAR_UNKNOWN)
8370 action = 'r';
8371 else if (argvars[2].v_type == VAR_STRING)
8372 {
8373 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008374 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008375 if (actstr == NULL)
8376 return;
Bram Moolenaar271fa082020-01-02 14:02:16 +01008377 if ((*actstr == 'r' || *actstr == 'a' || *actstr == 't')
8378 && actstr[1] == NUL)
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008379 action = *actstr;
8380 else
8381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008383 return;
8384 }
8385 }
8386 else
8387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008388 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +01008389 return;
8390 }
8391
8392 if (set_tagstack(wp, d, action) == OK)
8393 rettv->vval.v_number = 0;
8394}
8395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396#ifdef FEAT_CRYPT
8397/*
8398 * "sha256({string})" function
8399 */
8400 static void
8401f_sha256(typval_T *argvars, typval_T *rettv)
8402{
8403 char_u *p;
8404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008405 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 rettv->vval.v_string = vim_strsave(
8407 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
8408 rettv->v_type = VAR_STRING;
8409}
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008410#endif // FEAT_CRYPT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411
8412/*
8413 * "shellescape({string})" function
8414 */
8415 static void
8416f_shellescape(typval_T *argvars, typval_T *rettv)
8417{
Bram Moolenaar20615522017-06-05 18:46:26 +02008418 int do_special = non_zero_arg(&argvars[1]);
8419
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008421 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422 rettv->v_type = VAR_STRING;
8423}
8424
8425/*
8426 * shiftwidth() function
8427 */
8428 static void
8429f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
8430{
Bram Moolenaarf9514162018-11-22 03:08:29 +01008431 rettv->vval.v_number = 0;
8432
8433 if (argvars[0].v_type != VAR_UNKNOWN)
8434 {
8435 long col;
8436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008437 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008438 if (col < 0)
8439 return; // type error; errmsg already given
8440#ifdef FEAT_VARTABS
8441 rettv->vval.v_number = get_sw_value_col(curbuf, col);
8442 return;
8443#endif
8444 }
8445
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 rettv->vval.v_number = get_sw_value(curbuf);
8447}
8448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449/*
8450 * "soundfold({word})" function
8451 */
8452 static void
8453f_soundfold(typval_T *argvars, typval_T *rettv)
8454{
8455 char_u *s;
8456
8457 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008458 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459#ifdef FEAT_SPELL
8460 rettv->vval.v_string = eval_soundfold(s);
8461#else
8462 rettv->vval.v_string = vim_strsave(s);
8463#endif
8464}
8465
8466/*
8467 * "spellbadword()" function
8468 */
8469 static void
8470f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
8471{
8472 char_u *word = (char_u *)"";
8473 hlf_T attr = HLF_COUNT;
8474 int len = 0;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008475#ifdef FEAT_SPELL
8476 int wo_spell_save = curwin->w_p_spell;
8477
8478 if (!curwin->w_p_spell)
8479 {
8480 did_set_spelllang(curwin);
8481 curwin->w_p_spell = TRUE;
8482 }
8483
8484 if (*curwin->w_s->b_p_spl == NUL)
8485 {
8486 emsg(_(e_no_spell));
8487 curwin->w_p_spell = wo_spell_save;
8488 return;
8489 }
8490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491
8492 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008493 {
8494#ifdef FEAT_SPELL
8495 curwin->w_p_spell = wo_spell_save;
8496#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008498 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499
8500#ifdef FEAT_SPELL
8501 if (argvars[0].v_type == VAR_UNKNOWN)
8502 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008503 // Find the start and length of the badly spelled word.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008504 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
8505 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008506 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +01008508 curwin->w_set_curswant = TRUE;
8509 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008511 else if (*curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008513 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 int capcol = -1;
8515
8516 if (str != NULL)
8517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008518 // Check the argument for spelling.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 while (*str != NUL)
8520 {
8521 len = spell_check(curwin, str, &attr, &capcol, FALSE);
8522 if (attr != HLF_COUNT)
8523 {
8524 word = str;
8525 break;
8526 }
8527 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +02008528 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +02008529 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 }
8531 }
8532 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008533 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534#endif
8535
8536 list_append_string(rettv->vval.v_list, word, len);
8537 list_append_string(rettv->vval.v_list, (char_u *)(
8538 attr == HLF_SPB ? "bad" :
8539 attr == HLF_SPR ? "rare" :
8540 attr == HLF_SPL ? "local" :
8541 attr == HLF_SPC ? "caps" :
8542 ""), -1);
8543}
8544
8545/*
8546 * "spellsuggest()" function
8547 */
8548 static void
8549f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
8550{
8551#ifdef FEAT_SPELL
8552 char_u *str;
8553 int typeerr = FALSE;
8554 int maxcount;
8555 garray_T ga;
8556 int i;
8557 listitem_T *li;
8558 int need_capital = FALSE;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008559 int wo_spell_save = curwin->w_p_spell;
8560
8561 if (!curwin->w_p_spell)
8562 {
8563 did_set_spelllang(curwin);
8564 curwin->w_p_spell = TRUE;
8565 }
8566
8567 if (*curwin->w_s->b_p_spl == NUL)
8568 {
8569 emsg(_(e_no_spell));
8570 curwin->w_p_spell = wo_spell_save;
8571 return;
8572 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573#endif
8574
8575 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008576 {
8577#ifdef FEAT_SPELL
8578 curwin->w_p_spell = wo_spell_save;
8579#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580 return;
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008581 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582
8583#ifdef FEAT_SPELL
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008584 if (*curwin->w_s->b_p_spl != NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008586 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 if (argvars[1].v_type != VAR_UNKNOWN)
8588 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008589 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 if (maxcount <= 0)
8591 return;
8592 if (argvars[2].v_type != VAR_UNKNOWN)
8593 {
Bram Moolenaar7c27f332020-09-05 22:45:55 +02008594 need_capital = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 if (typeerr)
8596 return;
8597 }
8598 }
8599 else
8600 maxcount = 25;
8601
8602 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
8603
8604 for (i = 0; i < ga.ga_len; ++i)
8605 {
8606 str = ((char_u **)ga.ga_data)[i];
8607
8608 li = listitem_alloc();
8609 if (li == NULL)
8610 vim_free(str);
8611 else
8612 {
8613 li->li_tv.v_type = VAR_STRING;
8614 li->li_tv.v_lock = 0;
8615 li->li_tv.vval.v_string = str;
8616 list_append(rettv->vval.v_list, li);
8617 }
8618 }
8619 ga_clear(&ga);
8620 }
Bram Moolenaar152e79e2020-06-10 15:32:08 +02008621 curwin->w_p_spell = wo_spell_save;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622#endif
8623}
8624
8625 static void
8626f_split(typval_T *argvars, typval_T *rettv)
8627{
8628 char_u *str;
8629 char_u *end;
8630 char_u *pat = NULL;
8631 regmatch_T regmatch;
8632 char_u patbuf[NUMBUFLEN];
8633 char_u *save_cpo;
8634 int match;
8635 colnr_T col = 0;
8636 int keepempty = FALSE;
8637 int typeerr = FALSE;
8638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008639 // Make 'cpoptions' empty, the 'l' flag should not be used here.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01008641 p_cpo = empty_option;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008643 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 if (argvars[1].v_type != VAR_UNKNOWN)
8645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008646 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 if (pat == NULL)
8648 typeerr = TRUE;
8649 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar3986b942020-09-06 16:09:04 +02008650 keepempty = (int)tv_get_bool_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 }
8652 if (pat == NULL || *pat == NUL)
8653 pat = (char_u *)"[\\x01- ]\\+";
8654
8655 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008656 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 if (typeerr)
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008658 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659
8660 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8661 if (regmatch.regprog != NULL)
8662 {
8663 regmatch.rm_ic = FALSE;
8664 while (*str != NUL || keepempty)
8665 {
8666 if (*str == NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008667 match = FALSE; // empty item at the end
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668 else
8669 match = vim_regexec_nl(&regmatch, str, col);
8670 if (match)
8671 end = regmatch.startp[0];
8672 else
8673 end = str + STRLEN(str);
8674 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
8675 && *str != NUL && match && end < regmatch.endp[0]))
8676 {
8677 if (list_append_string(rettv->vval.v_list, str,
8678 (int)(end - str)) == FAIL)
8679 break;
8680 }
8681 if (!match)
8682 break;
Bram Moolenaar13505972019-01-24 15:04:48 +01008683 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008684 if (regmatch.endp[0] > str)
8685 col = 0;
8686 else
Bram Moolenaar13505972019-01-24 15:04:48 +01008687 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689 str = regmatch.endp[0];
8690 }
8691
8692 vim_regfree(regmatch.regprog);
8693 }
8694
Bram Moolenaar7d5e7442020-07-21 22:25:51 +02008695theend:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 p_cpo = save_cpo;
8697}
8698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008700 * "str2list()" function
8701 */
8702 static void
8703f_str2list(typval_T *argvars, typval_T *rettv)
8704{
8705 char_u *p;
8706 int utf8 = FALSE;
8707
8708 if (rettv_list_alloc(rettv) == FAIL)
8709 return;
8710
8711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02008712 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9d401282019-04-06 13:18:12 +02008713
8714 p = tv_get_string(&argvars[0]);
8715
8716 if (has_mbyte || utf8)
8717 {
8718 int (*ptr2len)(char_u *);
8719 int (*ptr2char)(char_u *);
8720
8721 if (utf8 || enc_utf8)
8722 {
8723 ptr2len = utf_ptr2len;
8724 ptr2char = utf_ptr2char;
8725 }
8726 else
8727 {
8728 ptr2len = mb_ptr2len;
8729 ptr2char = mb_ptr2char;
8730 }
8731
8732 for ( ; *p != NUL; p += (*ptr2len)(p))
8733 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
8734 }
8735 else
8736 for ( ; *p != NUL; ++p)
8737 list_append_number(rettv->vval.v_list, *p);
8738}
8739
8740/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 * "str2nr()" function
8742 */
8743 static void
8744f_str2nr(typval_T *argvars, typval_T *rettv)
8745{
8746 int base = 10;
8747 char_u *p;
8748 varnumber_T n;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008749 int what = 0;
Bram Moolenaar08243d22017-01-10 16:12:29 +01008750 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751
8752 if (argvars[1].v_type != VAR_UNKNOWN)
8753 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755 if (base != 2 && base != 8 && base != 10 && base != 16)
8756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008757 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 return;
8759 }
Bram Moolenaar3986b942020-09-06 16:09:04 +02008760 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2]))
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008761 what |= STR2NR_QUOTE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008762 }
8763
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01008764 p = skipwhite(tv_get_string_strict(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +01008765 isneg = (*p == '-');
8766 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767 p = skipwhite(p + 1);
8768 switch (base)
8769 {
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008770 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break;
Bram Moolenaarc17e66c2020-06-02 21:38:22 +02008771 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break;
Bram Moolenaar60a8de22019-09-15 14:33:22 +02008772 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +02008774 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
8775 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +01008776 if (isneg)
8777 rettv->vval.v_number = -n;
8778 else
8779 rettv->vval.v_number = n;
8780
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781}
8782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783/*
8784 * "strgetchar()" function
8785 */
8786 static void
8787f_strgetchar(typval_T *argvars, typval_T *rettv)
8788{
8789 char_u *str;
8790 int len;
8791 int error = FALSE;
8792 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +01008793 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794
8795 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008796 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 if (str == NULL)
8798 return;
8799 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008800 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008801 if (error)
8802 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008803
Bram Moolenaar13505972019-01-24 15:04:48 +01008804 while (charidx >= 0 && byteidx < len)
8805 {
8806 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008808 rettv->vval.v_number = mb_ptr2char(str + byteidx);
8809 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008811 --charidx;
8812 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008814}
8815
8816/*
8817 * "stridx()" function
8818 */
8819 static void
8820f_stridx(typval_T *argvars, typval_T *rettv)
8821{
8822 char_u buf[NUMBUFLEN];
8823 char_u *needle;
8824 char_u *haystack;
8825 char_u *save_haystack;
8826 char_u *pos;
8827 int start_idx;
8828
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008829 needle = tv_get_string_chk(&argvars[1]);
8830 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 rettv->vval.v_number = -1;
8832 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008833 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834
8835 if (argvars[2].v_type != VAR_UNKNOWN)
8836 {
8837 int error = FALSE;
8838
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008839 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 if (error || start_idx >= (int)STRLEN(haystack))
8841 return;
8842 if (start_idx >= 0)
8843 haystack += start_idx;
8844 }
8845
8846 pos = (char_u *)strstr((char *)haystack, (char *)needle);
8847 if (pos != NULL)
8848 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
8849}
8850
8851/*
8852 * "string()" function
8853 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008854 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855f_string(typval_T *argvars, typval_T *rettv)
8856{
8857 char_u *tofree;
8858 char_u numbuf[NUMBUFLEN];
8859
8860 rettv->v_type = VAR_STRING;
8861 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
8862 get_copyID());
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01008863 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 if (rettv->vval.v_string != NULL && tofree == NULL)
8865 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
8866}
8867
8868/*
8869 * "strlen()" function
8870 */
8871 static void
8872f_strlen(typval_T *argvars, typval_T *rettv)
8873{
8874 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008875 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876}
8877
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008878 static void
8879strchar_common(typval_T *argvars, typval_T *rettv, int skipcc)
8880{
8881 char_u *s = tv_get_string(&argvars[0]);
8882 varnumber_T len = 0;
8883 int (*func_mb_ptr2char_adv)(char_u **pp);
8884
8885 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
8886 while (*s != NUL)
8887 {
8888 func_mb_ptr2char_adv(&s);
8889 ++len;
8890 }
8891 rettv->vval.v_number = len;
8892}
8893
8894/*
8895 * "strcharlen()" function
8896 */
8897 static void
8898f_strcharlen(typval_T *argvars, typval_T *rettv)
8899{
8900 strchar_common(argvars, rettv, TRUE);
8901}
8902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903/*
8904 * "strchars()" function
8905 */
8906 static void
8907f_strchars(typval_T *argvars, typval_T *rettv)
8908{
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008909 varnumber_T skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910
8911 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01008912 skipcc = tv_get_bool(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarbade44e2020-09-26 22:39:24 +02008914 semsg(_(e_using_number_as_bool_nr), skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 else
Bram Moolenaar70ce8a12021-03-14 19:02:09 +01008916 strchar_common(argvars, rettv, skipcc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917}
8918
8919/*
8920 * "strdisplaywidth()" function
8921 */
8922 static void
8923f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
8924{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008925 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 int col = 0;
8927
8928 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930
8931 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
8932}
8933
8934/*
8935 * "strwidth()" function
8936 */
8937 static void
8938f_strwidth(typval_T *argvars, typval_T *rettv)
8939{
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02008940 char_u *s = tv_get_string_strict(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941
Bram Moolenaar13505972019-01-24 15:04:48 +01008942 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943}
8944
8945/*
8946 * "strcharpart()" function
8947 */
8948 static void
8949f_strcharpart(typval_T *argvars, typval_T *rettv)
8950{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951 char_u *p;
8952 int nchar;
8953 int nbyte = 0;
8954 int charlen;
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008955 int skipcc = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 int len = 0;
8957 int slen;
8958 int error = FALSE;
8959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008960 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 slen = (int)STRLEN(p);
8962
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008963 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 if (!error)
8965 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008966 if (argvars[2].v_type != VAR_UNKNOWN
8967 && argvars[3].v_type != VAR_UNKNOWN)
8968 {
8969 skipcc = tv_get_bool(&argvars[3]);
8970 if (skipcc < 0 || skipcc > 1)
8971 {
8972 semsg(_(e_using_number_as_bool_nr), skipcc);
8973 return;
8974 }
8975 }
8976
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 if (nchar > 0)
8978 while (nchar > 0 && nbyte < slen)
8979 {
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008980 if (skipcc)
8981 nbyte += mb_ptr2len(p + nbyte);
8982 else
8983 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 --nchar;
8985 }
8986 else
8987 nbyte = nchar;
8988 if (argvars[2].v_type != VAR_UNKNOWN)
8989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008990 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 while (charlen > 0 && nbyte + len < slen)
8992 {
8993 int off = nbyte + len;
8994
8995 if (off < 0)
8996 len += 1;
8997 else
Bram Moolenaar02b4d9b2021-03-14 19:46:45 +01008998 {
8999 if (skipcc)
9000 len += mb_ptr2len(p + off);
9001 else
9002 len += MB_CPTR2LEN(p + off);
9003 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 --charlen;
9005 }
9006 }
9007 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009008 len = slen - nbyte; // default: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 }
9010
9011 /*
9012 * Only return the overlap between the specified part and the actual
9013 * string.
9014 */
9015 if (nbyte < 0)
9016 {
9017 len += nbyte;
9018 nbyte = 0;
9019 }
9020 else if (nbyte > slen)
9021 nbyte = slen;
9022 if (len < 0)
9023 len = 0;
9024 else if (nbyte + len > slen)
9025 len = slen - nbyte;
9026
9027 rettv->v_type = VAR_STRING;
9028 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029}
9030
9031/*
9032 * "strpart()" function
9033 */
9034 static void
9035f_strpart(typval_T *argvars, typval_T *rettv)
9036{
9037 char_u *p;
9038 int n;
9039 int len;
9040 int slen;
9041 int error = FALSE;
9042
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009043 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009044 slen = (int)STRLEN(p);
9045
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047 if (error)
9048 len = 0;
9049 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009050 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009052 len = slen - n; // default len: all bytes that are available.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009054 // Only return the overlap between the specified part and the actual
9055 // string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 if (n < 0)
9057 {
9058 len += n;
9059 n = 0;
9060 }
9061 else if (n > slen)
9062 n = slen;
9063 if (len < 0)
9064 len = 0;
9065 else if (n + len > slen)
9066 len = slen - n;
9067
Bram Moolenaar6c53fca2020-08-23 17:34:46 +02009068 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN)
9069 {
9070 int off;
9071
9072 // length in characters
9073 for (off = n; off < slen && len > 0; --len)
9074 off += mb_ptr2len(p + off);
9075 len = off - n;
9076 }
9077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 rettv->v_type = VAR_STRING;
9079 rettv->vval.v_string = vim_strnsave(p + n, len);
9080}
9081
9082/*
9083 * "strridx()" function
9084 */
9085 static void
9086f_strridx(typval_T *argvars, typval_T *rettv)
9087{
9088 char_u buf[NUMBUFLEN];
9089 char_u *needle;
9090 char_u *haystack;
9091 char_u *rest;
9092 char_u *lastmatch = NULL;
9093 int haystack_len, end_idx;
9094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009095 needle = tv_get_string_chk(&argvars[1]);
9096 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097
9098 rettv->vval.v_number = -1;
9099 if (needle == NULL || haystack == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009100 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009101
9102 haystack_len = (int)STRLEN(haystack);
9103 if (argvars[2].v_type != VAR_UNKNOWN)
9104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009105 // Third argument: upper limit for index
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009106 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 if (end_idx < 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009108 return; // can never find a match
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 }
9110 else
9111 end_idx = haystack_len;
9112
9113 if (*needle == NUL)
9114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009115 // Empty string matches past the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 lastmatch = haystack + end_idx;
9117 }
9118 else
9119 {
9120 for (rest = haystack; *rest != '\0'; ++rest)
9121 {
9122 rest = (char_u *)strstr((char *)rest, (char *)needle);
9123 if (rest == NULL || rest > haystack + end_idx)
9124 break;
9125 lastmatch = rest;
9126 }
9127 }
9128
9129 if (lastmatch == NULL)
9130 rettv->vval.v_number = -1;
9131 else
9132 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9133}
9134
9135/*
9136 * "strtrans()" function
9137 */
9138 static void
9139f_strtrans(typval_T *argvars, typval_T *rettv)
9140{
9141 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009142 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143}
9144
9145/*
9146 * "submatch()" function
9147 */
9148 static void
9149f_submatch(typval_T *argvars, typval_T *rettv)
9150{
9151 int error = FALSE;
9152 int no;
9153 int retList = 0;
9154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009155 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009156 if (error)
9157 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009158 if (no < 0 || no >= NSUBEXP)
9159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009160 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +01009161 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +02009162 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009163 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarad304702020-09-06 18:22:53 +02009164 retList = (int)tv_get_bool_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 if (error)
9166 return;
9167
9168 if (retList == 0)
9169 {
9170 rettv->v_type = VAR_STRING;
9171 rettv->vval.v_string = reg_submatch(no);
9172 }
9173 else
9174 {
9175 rettv->v_type = VAR_LIST;
9176 rettv->vval.v_list = reg_submatch_list(no);
9177 }
9178}
9179
9180/*
9181 * "substitute()" function
9182 */
9183 static void
9184f_substitute(typval_T *argvars, typval_T *rettv)
9185{
9186 char_u patbuf[NUMBUFLEN];
9187 char_u subbuf[NUMBUFLEN];
9188 char_u flagsbuf[NUMBUFLEN];
9189
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009190 char_u *str = tv_get_string_chk(&argvars[0]);
9191 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009192 char_u *sub = NULL;
9193 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009194 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009196 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
9197 expr = &argvars[2];
9198 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009199 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009200
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009201 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009202 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
9203 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009204 rettv->vval.v_string = NULL;
9205 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02009206 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009207}
9208
9209/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009210 * "swapinfo(swap_filename)" function
9211 */
9212 static void
9213f_swapinfo(typval_T *argvars, typval_T *rettv)
9214{
9215 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009216 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02009217}
9218
9219/*
Bram Moolenaar110bd602018-09-16 18:46:59 +02009220 * "swapname(expr)" function
9221 */
9222 static void
9223f_swapname(typval_T *argvars, typval_T *rettv)
9224{
9225 buf_T *buf;
9226
9227 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009228 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +02009229 if (buf == NULL || buf->b_ml.ml_mfp == NULL
9230 || buf->b_ml.ml_mfp->mf_fname == NULL)
9231 rettv->vval.v_string = NULL;
9232 else
9233 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
9234}
9235
9236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237 * "synID(lnum, col, trans)" function
9238 */
9239 static void
9240f_synID(typval_T *argvars UNUSED, typval_T *rettv)
9241{
9242 int id = 0;
9243#ifdef FEAT_SYN_HL
9244 linenr_T lnum;
9245 colnr_T col;
9246 int trans;
9247 int transerr = FALSE;
9248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009249 lnum = tv_get_lnum(argvars); // -1 on type error
9250 col = (linenr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaarfcb6d702020-09-05 21:41:56 +02009251 trans = (int)tv_get_bool_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009252
9253 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9254 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
9255 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
9256#endif
9257
9258 rettv->vval.v_number = id;
9259}
9260
9261/*
9262 * "synIDattr(id, what [, mode])" function
9263 */
9264 static void
9265f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
9266{
9267 char_u *p = NULL;
9268#ifdef FEAT_SYN_HL
9269 int id;
9270 char_u *what;
9271 char_u *mode;
9272 char_u modebuf[NUMBUFLEN];
9273 int modec;
9274
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009275 id = (int)tv_get_number(&argvars[0]);
9276 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277 if (argvars[2].v_type != VAR_UNKNOWN)
9278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009279 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009280 modec = TOLOWER_ASC(mode[0]);
9281 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009282 modec = 0; // replace invalid with current
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009283 }
9284 else
9285 {
9286#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
9287 if (USE_24BIT)
9288 modec = 'g';
9289 else
9290#endif
9291 if (t_colors > 1)
Dominique Pelle4781d6f2021-05-18 21:46:31 +02009292 modec = 'c';
9293 else
9294 modec = 't';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009295 }
9296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009297 switch (TOLOWER_ASC(what[0]))
9298 {
9299 case 'b':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009300 if (TOLOWER_ASC(what[1]) == 'g') // bg[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009302 else // bold
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 p = highlight_has_attr(id, HL_BOLD, modec);
9304 break;
9305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009306 case 'f': // fg[#] or font
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009307 p = highlight_color(id, what, modec);
9308 break;
9309
9310 case 'i':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009311 if (TOLOWER_ASC(what[1]) == 'n') // inverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009312 p = highlight_has_attr(id, HL_INVERSE, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009313 else // italic
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314 p = highlight_has_attr(id, HL_ITALIC, modec);
9315 break;
9316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009317 case 'n': // name
Bram Moolenaarc96272e2017-03-26 13:50:09 +02009318 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009319 break;
9320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009321 case 'r': // reverse
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009322 p = highlight_has_attr(id, HL_INVERSE, modec);
9323 break;
9324
9325 case 's':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009326 if (TOLOWER_ASC(what[1]) == 'p') // sp[#]
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009327 p = highlight_color(id, what, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009328 // strikeout
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02009329 else if (TOLOWER_ASC(what[1]) == 't' &&
9330 TOLOWER_ASC(what[2]) == 'r')
9331 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009332 else // standout
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 p = highlight_has_attr(id, HL_STANDOUT, modec);
9334 break;
9335
9336 case 'u':
Bram Moolenaar391c3622020-09-29 20:59:17 +02009337 if (TOLOWER_ASC(what[1]) == 'l') // ul
9338 p = highlight_color(id, what, modec);
9339 else if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009340 // underline
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9342 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009343 // undercurl
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009344 p = highlight_has_attr(id, HL_UNDERCURL, modec);
9345 break;
9346 }
9347
9348 if (p != NULL)
9349 p = vim_strsave(p);
9350#endif
9351 rettv->v_type = VAR_STRING;
9352 rettv->vval.v_string = p;
9353}
9354
9355/*
9356 * "synIDtrans(id)" function
9357 */
9358 static void
9359f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
9360{
9361 int id;
9362
9363#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365
9366 if (id > 0)
9367 id = syn_get_final_id(id);
9368 else
9369#endif
9370 id = 0;
9371
9372 rettv->vval.v_number = id;
9373}
9374
9375/*
9376 * "synconcealed(lnum, col)" function
9377 */
9378 static void
9379f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
9380{
9381#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
9382 linenr_T lnum;
9383 colnr_T col;
9384 int syntax_flags = 0;
9385 int cchar;
9386 int matchid = 0;
9387 char_u str[NUMBUFLEN];
9388#endif
9389
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009390 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009391
9392#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009393 lnum = tv_get_lnum(argvars); // -1 on type error
9394 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395
Bram Moolenaara80faa82020-04-12 19:37:17 +02009396 CLEAR_FIELD(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397
9398 if (rettv_list_alloc(rettv) != FAIL)
9399 {
9400 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9401 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9402 && curwin->w_p_cole > 0)
9403 {
9404 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
9405 syntax_flags = get_syntax_info(&matchid);
9406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009407 // get the conceal character
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009408 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
9409 {
9410 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +02009411 if (cchar == NUL && curwin->w_p_cole == 1)
Bram Moolenaareed9d462021-02-15 20:38:25 +01009412 cchar = (curwin->w_lcs_chars.conceal == NUL) ? ' '
9413 : curwin->w_lcs_chars.conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009414 if (cchar != NUL)
9415 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009416 if (has_mbyte)
9417 (*mb_char2bytes)(cchar, str);
9418 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 str[0] = cchar;
9420 }
9421 }
9422 }
9423
9424 list_append_number(rettv->vval.v_list,
9425 (syntax_flags & HL_CONCEAL) != 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009426 // -1 to auto-determine strlen
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427 list_append_string(rettv->vval.v_list, str, -1);
9428 list_append_number(rettv->vval.v_list, matchid);
9429 }
9430#endif
9431}
9432
9433/*
9434 * "synstack(lnum, col)" function
9435 */
9436 static void
9437f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
9438{
9439#ifdef FEAT_SYN_HL
9440 linenr_T lnum;
9441 colnr_T col;
9442 int i;
9443 int id;
9444#endif
9445
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009446 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447
9448#ifdef FEAT_SYN_HL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009449 lnum = tv_get_lnum(argvars); // -1 on type error
9450 col = (colnr_T)tv_get_number(&argvars[1]) - 1; // -1 on type error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451
9452 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
9453 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
9454 && rettv_list_alloc(rettv) != FAIL)
9455 {
9456 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
9457 for (i = 0; ; ++i)
9458 {
9459 id = syn_get_stack_item(i);
9460 if (id < 0)
9461 break;
9462 if (list_append_number(rettv->vval.v_list, id) == FAIL)
9463 break;
9464 }
9465 }
9466#endif
9467}
9468
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469/*
9470 * "tabpagebuflist()" function
9471 */
9472 static void
9473f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9474{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 tabpage_T *tp;
9476 win_T *wp = NULL;
9477
9478 if (argvars[0].v_type == VAR_UNKNOWN)
9479 wp = firstwin;
9480 else
9481 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 if (tp != NULL)
9484 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
9485 }
9486 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
9487 {
9488 for (; wp != NULL; wp = wp->w_next)
9489 if (list_append_number(rettv->vval.v_list,
9490 wp->w_buffer->b_fnum) == FAIL)
9491 break;
9492 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493}
9494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 * "tagfiles()" function
9497 */
9498 static void
9499f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
9500{
9501 char_u *fname;
9502 tagname_T tn;
9503 int first;
9504
9505 if (rettv_list_alloc(rettv) == FAIL)
9506 return;
9507 fname = alloc(MAXPATHL);
9508 if (fname == NULL)
9509 return;
9510
9511 for (first = TRUE; ; first = FALSE)
9512 if (get_tagfname(&tn, first, fname) == FAIL
9513 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
9514 break;
9515 tagname_free(&tn);
9516 vim_free(fname);
9517}
9518
9519/*
9520 * "taglist()" function
9521 */
9522 static void
9523f_taglist(typval_T *argvars, typval_T *rettv)
9524{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009525 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009526 char_u *tag_pattern;
9527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009528 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529
9530 rettv->vval.v_number = FALSE;
9531 if (*tag_pattern == NUL)
9532 return;
9533
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009534 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009535 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009536 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01009537 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538}
9539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 * "tolower(string)" function
9542 */
9543 static void
9544f_tolower(typval_T *argvars, typval_T *rettv)
9545{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009546 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009547 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548}
9549
9550/*
9551 * "toupper(string)" function
9552 */
9553 static void
9554f_toupper(typval_T *argvars, typval_T *rettv)
9555{
9556 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009557 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558}
9559
9560/*
9561 * "tr(string, fromstr, tostr)" function
9562 */
9563 static void
9564f_tr(typval_T *argvars, typval_T *rettv)
9565{
9566 char_u *in_str;
9567 char_u *fromstr;
9568 char_u *tostr;
9569 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009570 int inlen;
9571 int fromlen;
9572 int tolen;
9573 int idx;
9574 char_u *cpstr;
9575 int cplen;
9576 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577 char_u buf[NUMBUFLEN];
9578 char_u buf2[NUMBUFLEN];
9579 garray_T ga;
9580
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009581 in_str = tv_get_string(&argvars[0]);
9582 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
9583 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009585 // Default return value: empty string.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009586 rettv->v_type = VAR_STRING;
9587 rettv->vval.v_string = NULL;
9588 if (fromstr == NULL || tostr == NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009589 return; // type error; errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 ga_init2(&ga, (int)sizeof(char), 80);
9591
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 if (!has_mbyte)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009593 // not multi-byte: fromstr and tostr must be the same length
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 if (STRLEN(fromstr) != STRLEN(tostr))
9595 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009597 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 ga_clear(&ga);
9599 return;
9600 }
9601
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009602 // fromstr and tostr have to contain the same number of chars
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 while (*in_str != NUL)
9604 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009605 if (has_mbyte)
9606 {
9607 inlen = (*mb_ptr2len)(in_str);
9608 cpstr = in_str;
9609 cplen = inlen;
9610 idx = 0;
9611 for (p = fromstr; *p != NUL; p += fromlen)
9612 {
9613 fromlen = (*mb_ptr2len)(p);
9614 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
9615 {
9616 for (p = tostr; *p != NUL; p += tolen)
9617 {
9618 tolen = (*mb_ptr2len)(p);
9619 if (idx-- == 0)
9620 {
9621 cplen = tolen;
9622 cpstr = p;
9623 break;
9624 }
9625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009626 if (*p == NUL) // tostr is shorter than fromstr
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 goto error;
9628 break;
9629 }
9630 ++idx;
9631 }
9632
9633 if (first && cpstr == in_str)
9634 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009635 // Check that fromstr and tostr have the same number of
9636 // (multi-byte) characters. Done only once when a character
9637 // of in_str doesn't appear in fromstr.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009638 first = FALSE;
9639 for (p = tostr; *p != NUL; p += tolen)
9640 {
9641 tolen = (*mb_ptr2len)(p);
9642 --idx;
9643 }
9644 if (idx != 0)
9645 goto error;
9646 }
9647
9648 (void)ga_grow(&ga, cplen);
9649 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
9650 ga.ga_len += cplen;
9651
9652 in_str += inlen;
9653 }
9654 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009656 // When not using multi-byte chars we can do it faster.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009657 p = vim_strchr(fromstr, *in_str);
9658 if (p != NULL)
9659 ga_append(&ga, tostr[p - fromstr]);
9660 else
9661 ga_append(&ga, *in_str);
9662 ++in_str;
9663 }
9664 }
9665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009666 // add a terminating NUL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 (void)ga_grow(&ga, 1);
9668 ga_append(&ga, NUL);
9669
9670 rettv->vval.v_string = ga.ga_data;
9671}
9672
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009673/*
9674 * "trim({expr})" function
9675 */
9676 static void
9677f_trim(typval_T *argvars, typval_T *rettv)
9678{
9679 char_u buf1[NUMBUFLEN];
9680 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009681 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009682 char_u *mask = NULL;
9683 char_u *tail;
9684 char_u *prev;
9685 char_u *p;
9686 int c1;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009687 int dir = 0;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009688
9689 rettv->v_type = VAR_STRING;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009690 rettv->vval.v_string = NULL;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009691 if (head == NULL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009692 return;
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009693
9694 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009695 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009696 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009697
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009699 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009700 int error = 0;
9701
9702 // leading or trailing characters to trim
9703 dir = (int)tv_get_number_chk(&argvars[2], &error);
9704 if (error)
9705 return;
9706 if (dir < 0 || dir > 2)
9707 {
9708 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
9709 return;
9710 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009711 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009712 }
9713
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009714 if (dir == 0 || dir == 1)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009715 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009716 // Trim leading characters
9717 while (*head != NUL)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009718 {
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009719 c1 = PTR2CHAR(head);
9720 if (mask == NULL)
9721 {
9722 if (c1 > ' ' && c1 != 0xa0)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009723 break;
Bram Moolenaar2245ae12020-05-31 22:20:36 +02009724 }
9725 else
9726 {
9727 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9728 if (c1 == PTR2CHAR(p))
9729 break;
9730 if (*p == NUL)
9731 break;
9732 }
9733 MB_PTR_ADV(head);
9734 }
9735 }
9736
9737 tail = head + STRLEN(head);
9738 if (dir == 0 || dir == 2)
9739 {
9740 // Trim trailing characters
9741 for (; tail > head; tail = prev)
9742 {
9743 prev = tail;
9744 MB_PTR_BACK(head, prev);
9745 c1 = PTR2CHAR(prev);
9746 if (mask == NULL)
9747 {
9748 if (c1 > ' ' && c1 != 0xa0)
9749 break;
9750 }
9751 else
9752 {
9753 for (p = mask; *p != NUL; MB_PTR_ADV(p))
9754 if (c1 == PTR2CHAR(p))
9755 break;
9756 if (*p == NUL)
9757 break;
9758 }
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009759 }
9760 }
Bram Moolenaardf44a272020-06-07 20:49:05 +02009761 rettv->vval.v_string = vim_strnsave(head, tail - head);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01009762}
9763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764/*
9765 * "type(expr)" function
9766 */
9767 static void
9768f_type(typval_T *argvars, typval_T *rettv)
9769{
9770 int n = -1;
9771
9772 switch (argvars[0].v_type)
9773 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009774 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
9775 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01009777 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
9778 case VAR_LIST: n = VAR_TYPE_LIST; break;
9779 case VAR_DICT: n = VAR_TYPE_DICT; break;
9780 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
9781 case VAR_BOOL: n = VAR_TYPE_BOOL; break;
9782 case VAR_SPECIAL: n = VAR_TYPE_NONE; break;
Bram Moolenaarf562e722016-07-19 17:25:25 +02009783 case VAR_JOB: n = VAR_TYPE_JOB; break;
9784 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009785 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02009786 case VAR_INSTR: n = VAR_TYPE_INSTR; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02009788 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009789 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01009790 internal_error_no_abort("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 n = -1;
9792 break;
9793 }
9794 rettv->vval.v_number = n;
9795}
9796
9797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 * "virtcol(string)" function
9799 */
9800 static void
9801f_virtcol(typval_T *argvars, typval_T *rettv)
9802{
9803 colnr_T vcol = 0;
9804 pos_T *fp;
9805 int fnum = curbuf->b_fnum;
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009806 int len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807
Bram Moolenaar6f02b002021-01-10 20:22:54 +01009808 fp = var2fpos(&argvars[0], FALSE, &fnum, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
9810 && fnum == curbuf->b_fnum)
9811 {
Bram Moolenaarb3d33d82020-01-15 20:36:55 +01009812 // Limit the column to a valid value, getvvcol() doesn't check.
9813 if (fp->col < 0)
9814 fp->col = 0;
9815 else
9816 {
9817 len = (int)STRLEN(ml_get(fp->lnum));
9818 if (fp->col > len)
9819 fp->col = len;
9820 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009821 getvvcol(curwin, fp, NULL, NULL, &vcol);
9822 ++vcol;
9823 }
9824
9825 rettv->vval.v_number = vcol;
9826}
9827
9828/*
9829 * "visualmode()" function
9830 */
9831 static void
9832f_visualmode(typval_T *argvars, typval_T *rettv)
9833{
9834 char_u str[2];
9835
9836 rettv->v_type = VAR_STRING;
9837 str[0] = curbuf->b_visual_mode_eval;
9838 str[1] = NUL;
9839 rettv->vval.v_string = vim_strsave(str);
9840
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009841 // A non-zero number or non-empty string argument: reset mode.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842 if (non_zero_arg(&argvars[0]))
9843 curbuf->b_visual_mode_eval = NUL;
9844}
9845
9846/*
9847 * "wildmenumode()" function
9848 */
9849 static void
9850f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9851{
9852#ifdef FEAT_WILDMENU
9853 if (wild_menu_showing)
9854 rettv->vval.v_number = 1;
9855#endif
9856}
9857
9858/*
Bram Moolenaar0c1e3742019-12-27 13:49:24 +01009859 * "windowsversion()" function
9860 */
9861 static void
9862f_windowsversion(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9863{
9864 rettv->v_type = VAR_STRING;
9865 rettv->vval.v_string = vim_strsave((char_u *)windowsVersion);
9866}
9867
9868/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 * "wordcount()" function
9870 */
9871 static void
9872f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
9873{
9874 if (rettv_dict_alloc(rettv) == FAIL)
9875 return;
9876 cursor_pos_info(rettv->vval.v_dict);
9877}
9878
9879/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 * "xor(expr, expr)" function
9881 */
9882 static void
9883f_xor(typval_T *argvars, typval_T *rettv)
9884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009885 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9886 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887}
9888
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01009889#endif // FEAT_EVAL