blob: b24f55b31dc6672e52ca3084d4e7ad3557d6735b [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
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_changenr(typval_T *argvars, typval_T *rettv);
90static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020091static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_cindent(typval_T *argvars, typval_T *rettv);
93static void f_clearmatches(typval_T *argvars, typval_T *rettv);
94static void f_col(typval_T *argvars, typval_T *rettv);
95#if defined(FEAT_INS_EXPAND)
96static void f_complete(typval_T *argvars, typval_T *rettv);
97static void f_complete_add(typval_T *argvars, typval_T *rettv);
98static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +010099static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200100#endif
101static void f_confirm(typval_T *argvars, typval_T *rettv);
102static void f_copy(typval_T *argvars, typval_T *rettv);
103#ifdef FEAT_FLOAT
104static void f_cos(typval_T *argvars, typval_T *rettv);
105static void f_cosh(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_count(typval_T *argvars, typval_T *rettv);
108static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
109static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100110#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200111static void f_debugbreak(typval_T *argvars, typval_T *rettv);
112#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200113static void f_deepcopy(typval_T *argvars, typval_T *rettv);
114static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200115static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200116static void f_did_filetype(typval_T *argvars, typval_T *rettv);
117static void f_diff_filler(typval_T *argvars, typval_T *rettv);
118static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
119static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200120static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200121static void f_escape(typval_T *argvars, typval_T *rettv);
122static void f_eval(typval_T *argvars, typval_T *rettv);
123static void f_eventhandler(typval_T *argvars, typval_T *rettv);
124static void f_executable(typval_T *argvars, typval_T *rettv);
125static void f_execute(typval_T *argvars, typval_T *rettv);
126static void f_exepath(typval_T *argvars, typval_T *rettv);
127static void f_exists(typval_T *argvars, typval_T *rettv);
128#ifdef FEAT_FLOAT
129static void f_exp(typval_T *argvars, typval_T *rettv);
130#endif
131static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200132static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_extend(typval_T *argvars, typval_T *rettv);
134static void f_feedkeys(typval_T *argvars, typval_T *rettv);
135static void f_filereadable(typval_T *argvars, typval_T *rettv);
136static void f_filewritable(typval_T *argvars, typval_T *rettv);
137static void f_filter(typval_T *argvars, typval_T *rettv);
138static void f_finddir(typval_T *argvars, typval_T *rettv);
139static void f_findfile(typval_T *argvars, typval_T *rettv);
140#ifdef FEAT_FLOAT
141static void f_float2nr(typval_T *argvars, typval_T *rettv);
142static void f_floor(typval_T *argvars, typval_T *rettv);
143static void f_fmod(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_fnameescape(typval_T *argvars, typval_T *rettv);
146static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
147static void f_foldclosed(typval_T *argvars, typval_T *rettv);
148static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
149static void f_foldlevel(typval_T *argvars, typval_T *rettv);
150static void f_foldtext(typval_T *argvars, typval_T *rettv);
151static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
152static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200153static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200154static void f_function(typval_T *argvars, typval_T *rettv);
155static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
156static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200157static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_getbufline(typval_T *argvars, typval_T *rettv);
159static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100160static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_getchar(typval_T *argvars, typval_T *rettv);
162static void f_getcharmod(typval_T *argvars, typval_T *rettv);
163static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
164static void f_getcmdline(typval_T *argvars, typval_T *rettv);
165#if defined(FEAT_CMDL_COMPL)
166static void f_getcompletion(typval_T *argvars, typval_T *rettv);
167#endif
168static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
169static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
170static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
171static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200172static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_getfontname(typval_T *argvars, typval_T *rettv);
174static void f_getfperm(typval_T *argvars, typval_T *rettv);
175static void f_getfsize(typval_T *argvars, typval_T *rettv);
176static void f_getftime(typval_T *argvars, typval_T *rettv);
177static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100178static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200179static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200180static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getmatches(typval_T *argvars, typval_T *rettv);
182static void f_getpid(typval_T *argvars, typval_T *rettv);
183static void f_getcurpos(typval_T *argvars, typval_T *rettv);
184static void f_getpos(typval_T *argvars, typval_T *rettv);
185static void f_getqflist(typval_T *argvars, typval_T *rettv);
186static void f_getreg(typval_T *argvars, typval_T *rettv);
187static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200188static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_gettabvar(typval_T *argvars, typval_T *rettv);
190static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100191static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100193static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getwinposx(typval_T *argvars, typval_T *rettv);
195static void f_getwinposy(typval_T *argvars, typval_T *rettv);
196static void f_getwinvar(typval_T *argvars, typval_T *rettv);
197static void f_glob(typval_T *argvars, typval_T *rettv);
198static void f_globpath(typval_T *argvars, typval_T *rettv);
199static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
200static void f_has(typval_T *argvars, typval_T *rettv);
201static void f_has_key(typval_T *argvars, typval_T *rettv);
202static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
203static void f_hasmapto(typval_T *argvars, typval_T *rettv);
204static void f_histadd(typval_T *argvars, typval_T *rettv);
205static void f_histdel(typval_T *argvars, typval_T *rettv);
206static void f_histget(typval_T *argvars, typval_T *rettv);
207static void f_histnr(typval_T *argvars, typval_T *rettv);
208static void f_hlID(typval_T *argvars, typval_T *rettv);
209static void f_hlexists(typval_T *argvars, typval_T *rettv);
210static void f_hostname(typval_T *argvars, typval_T *rettv);
211static void f_iconv(typval_T *argvars, typval_T *rettv);
212static void f_indent(typval_T *argvars, typval_T *rettv);
213static void f_index(typval_T *argvars, typval_T *rettv);
214static void f_input(typval_T *argvars, typval_T *rettv);
215static void f_inputdialog(typval_T *argvars, typval_T *rettv);
216static void f_inputlist(typval_T *argvars, typval_T *rettv);
217static void f_inputrestore(typval_T *argvars, typval_T *rettv);
218static void f_inputsave(typval_T *argvars, typval_T *rettv);
219static void f_inputsecret(typval_T *argvars, typval_T *rettv);
220static void f_insert(typval_T *argvars, typval_T *rettv);
221static void f_invert(typval_T *argvars, typval_T *rettv);
222static void f_isdirectory(typval_T *argvars, typval_T *rettv);
223static void f_islocked(typval_T *argvars, typval_T *rettv);
224#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200225static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200226static void f_isnan(typval_T *argvars, typval_T *rettv);
227#endif
228static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200229static void f_join(typval_T *argvars, typval_T *rettv);
230static void f_js_decode(typval_T *argvars, typval_T *rettv);
231static void f_js_encode(typval_T *argvars, typval_T *rettv);
232static void f_json_decode(typval_T *argvars, typval_T *rettv);
233static void f_json_encode(typval_T *argvars, typval_T *rettv);
234static void f_keys(typval_T *argvars, typval_T *rettv);
235static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
236static void f_len(typval_T *argvars, typval_T *rettv);
237static void f_libcall(typval_T *argvars, typval_T *rettv);
238static void f_libcallnr(typval_T *argvars, typval_T *rettv);
239static void f_line(typval_T *argvars, typval_T *rettv);
240static void f_line2byte(typval_T *argvars, typval_T *rettv);
241static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200242static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200243static void f_localtime(typval_T *argvars, typval_T *rettv);
244#ifdef FEAT_FLOAT
245static void f_log(typval_T *argvars, typval_T *rettv);
246static void f_log10(typval_T *argvars, typval_T *rettv);
247#endif
248#ifdef FEAT_LUA
249static void f_luaeval(typval_T *argvars, typval_T *rettv);
250#endif
251static void f_map(typval_T *argvars, typval_T *rettv);
252static void f_maparg(typval_T *argvars, typval_T *rettv);
253static void f_mapcheck(typval_T *argvars, typval_T *rettv);
254static void f_match(typval_T *argvars, typval_T *rettv);
255static void f_matchadd(typval_T *argvars, typval_T *rettv);
256static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
257static void f_matcharg(typval_T *argvars, typval_T *rettv);
258static void f_matchdelete(typval_T *argvars, typval_T *rettv);
259static void f_matchend(typval_T *argvars, typval_T *rettv);
260static void f_matchlist(typval_T *argvars, typval_T *rettv);
261static void f_matchstr(typval_T *argvars, typval_T *rettv);
262static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
263static void f_max(typval_T *argvars, typval_T *rettv);
264static void f_min(typval_T *argvars, typval_T *rettv);
265#ifdef vim_mkdir
266static void f_mkdir(typval_T *argvars, typval_T *rettv);
267#endif
268static void f_mode(typval_T *argvars, typval_T *rettv);
269#ifdef FEAT_MZSCHEME
270static void f_mzeval(typval_T *argvars, typval_T *rettv);
271#endif
272static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
273static void f_nr2char(typval_T *argvars, typval_T *rettv);
274static void f_or(typval_T *argvars, typval_T *rettv);
275static void f_pathshorten(typval_T *argvars, typval_T *rettv);
276#ifdef FEAT_PERL
277static void f_perleval(typval_T *argvars, typval_T *rettv);
278#endif
279#ifdef FEAT_FLOAT
280static void f_pow(typval_T *argvars, typval_T *rettv);
281#endif
282static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
283static void f_printf(typval_T *argvars, typval_T *rettv);
284static void f_pumvisible(typval_T *argvars, typval_T *rettv);
285#ifdef FEAT_PYTHON3
286static void f_py3eval(typval_T *argvars, typval_T *rettv);
287#endif
288#ifdef FEAT_PYTHON
289static void f_pyeval(typval_T *argvars, typval_T *rettv);
290#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100291#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
292static void f_pyxeval(typval_T *argvars, typval_T *rettv);
293#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200294static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200295static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200296static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200297static void f_reg_executing(typval_T *argvars, typval_T *rettv);
298static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_reltime(typval_T *argvars, typval_T *rettv);
300#ifdef FEAT_FLOAT
301static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
302#endif
303static void f_reltimestr(typval_T *argvars, typval_T *rettv);
304static void f_remote_expr(typval_T *argvars, typval_T *rettv);
305static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
306static void f_remote_peek(typval_T *argvars, typval_T *rettv);
307static void f_remote_read(typval_T *argvars, typval_T *rettv);
308static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100309static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200310static void f_remove(typval_T *argvars, typval_T *rettv);
311static void f_rename(typval_T *argvars, typval_T *rettv);
312static void f_repeat(typval_T *argvars, typval_T *rettv);
313static void f_resolve(typval_T *argvars, typval_T *rettv);
314static void f_reverse(typval_T *argvars, typval_T *rettv);
315#ifdef FEAT_FLOAT
316static void f_round(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100318#ifdef FEAT_RUBY
319static void f_rubyeval(typval_T *argvars, typval_T *rettv);
320#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_screenattr(typval_T *argvars, typval_T *rettv);
322static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100323static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200324static void f_screencol(typval_T *argvars, typval_T *rettv);
325static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100326static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_search(typval_T *argvars, typval_T *rettv);
328static void f_searchdecl(typval_T *argvars, typval_T *rettv);
329static void f_searchpair(typval_T *argvars, typval_T *rettv);
330static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
331static void f_searchpos(typval_T *argvars, typval_T *rettv);
332static void f_server2client(typval_T *argvars, typval_T *rettv);
333static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200334static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_setbufvar(typval_T *argvars, typval_T *rettv);
336static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
337static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200338static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200339static void f_setfperm(typval_T *argvars, typval_T *rettv);
340static void f_setline(typval_T *argvars, typval_T *rettv);
341static void f_setloclist(typval_T *argvars, typval_T *rettv);
342static void f_setmatches(typval_T *argvars, typval_T *rettv);
343static void f_setpos(typval_T *argvars, typval_T *rettv);
344static void f_setqflist(typval_T *argvars, typval_T *rettv);
345static void f_setreg(typval_T *argvars, typval_T *rettv);
346static void f_settabvar(typval_T *argvars, typval_T *rettv);
347static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100348static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200349static void f_setwinvar(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_CRYPT
351static void f_sha256(typval_T *argvars, typval_T *rettv);
352#endif /* FEAT_CRYPT */
353static void f_shellescape(typval_T *argvars, typval_T *rettv);
354static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
355static void f_simplify(typval_T *argvars, typval_T *rettv);
356#ifdef FEAT_FLOAT
357static void f_sin(typval_T *argvars, typval_T *rettv);
358static void f_sinh(typval_T *argvars, typval_T *rettv);
359#endif
360static void f_sort(typval_T *argvars, typval_T *rettv);
361static void f_soundfold(typval_T *argvars, typval_T *rettv);
362static void f_spellbadword(typval_T *argvars, typval_T *rettv);
363static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
364static void f_split(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_FLOAT
366static void f_sqrt(typval_T *argvars, typval_T *rettv);
367static void f_str2float(typval_T *argvars, typval_T *rettv);
368#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200369static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200370static void f_str2nr(typval_T *argvars, typval_T *rettv);
371static void f_strchars(typval_T *argvars, typval_T *rettv);
372#ifdef HAVE_STRFTIME
373static void f_strftime(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_strgetchar(typval_T *argvars, typval_T *rettv);
376static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200377static void f_strlen(typval_T *argvars, typval_T *rettv);
378static void f_strcharpart(typval_T *argvars, typval_T *rettv);
379static void f_strpart(typval_T *argvars, typval_T *rettv);
380static void f_strridx(typval_T *argvars, typval_T *rettv);
381static void f_strtrans(typval_T *argvars, typval_T *rettv);
382static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
383static void f_strwidth(typval_T *argvars, typval_T *rettv);
384static void f_submatch(typval_T *argvars, typval_T *rettv);
385static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200386static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200387static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200388static void f_synID(typval_T *argvars, typval_T *rettv);
389static void f_synIDattr(typval_T *argvars, typval_T *rettv);
390static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
391static void f_synstack(typval_T *argvars, typval_T *rettv);
392static void f_synconcealed(typval_T *argvars, typval_T *rettv);
393static void f_system(typval_T *argvars, typval_T *rettv);
394static void f_systemlist(typval_T *argvars, typval_T *rettv);
395static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
397static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
398static void f_taglist(typval_T *argvars, typval_T *rettv);
399static void f_tagfiles(typval_T *argvars, typval_T *rettv);
400static void f_tempname(typval_T *argvars, typval_T *rettv);
401static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
402static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200403static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200404static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200405static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100406static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100407static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaaradc67142019-06-22 01:40:42 +0200409static void f_test_garbagecollect_soon(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100410static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100411static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
416#ifdef FEAT_JOB_CHANNEL
417static void f_test_null_job(typval_T *argvars, typval_T *rettv);
418#endif
419static void f_test_null_list(typval_T *argvars, typval_T *rettv);
420static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
421static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200422#ifdef FEAT_GUI
423static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
424#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200425#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200426static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200427#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428static void f_test_settime(typval_T *argvars, typval_T *rettv);
429#ifdef FEAT_FLOAT
430static void f_tan(typval_T *argvars, typval_T *rettv);
431static void f_tanh(typval_T *argvars, typval_T *rettv);
432#endif
433#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200434static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200435static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436static void f_timer_start(typval_T *argvars, typval_T *rettv);
437static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200438static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200439#endif
440static void f_tolower(typval_T *argvars, typval_T *rettv);
441static void f_toupper(typval_T *argvars, typval_T *rettv);
442static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100443static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444#ifdef FEAT_FLOAT
445static void f_trunc(typval_T *argvars, typval_T *rettv);
446#endif
447static void f_type(typval_T *argvars, typval_T *rettv);
448static void f_undofile(typval_T *argvars, typval_T *rettv);
449static void f_undotree(typval_T *argvars, typval_T *rettv);
450static void f_uniq(typval_T *argvars, typval_T *rettv);
451static void f_values(typval_T *argvars, typval_T *rettv);
452static void f_virtcol(typval_T *argvars, typval_T *rettv);
453static void f_visualmode(typval_T *argvars, typval_T *rettv);
454static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200455static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200456static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
457static void f_win_getid(typval_T *argvars, typval_T *rettv);
458static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
459static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
460static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100461static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200462static void f_winbufnr(typval_T *argvars, typval_T *rettv);
463static void f_wincol(typval_T *argvars, typval_T *rettv);
464static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200465static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200466static void f_winline(typval_T *argvars, typval_T *rettv);
467static void f_winnr(typval_T *argvars, typval_T *rettv);
468static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
469static void f_winrestview(typval_T *argvars, typval_T *rettv);
470static void f_winsaveview(typval_T *argvars, typval_T *rettv);
471static void f_winwidth(typval_T *argvars, typval_T *rettv);
472static void f_writefile(typval_T *argvars, typval_T *rettv);
473static void f_wordcount(typval_T *argvars, typval_T *rettv);
474static void f_xor(typval_T *argvars, typval_T *rettv);
475
476/*
477 * Array with names and number of arguments of all internal functions
478 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
479 */
480static struct fst
481{
482 char *f_name; /* function name */
483 char f_min_argc; /* minimal number of arguments */
484 char f_max_argc; /* maximal number of arguments */
485 void (*f_func)(typval_T *args, typval_T *rvar);
486 /* implementation of function */
487} functions[] =
488{
489#ifdef FEAT_FLOAT
490 {"abs", 1, 1, f_abs},
491 {"acos", 1, 1, f_acos}, /* WJMc */
492#endif
493 {"add", 2, 2, f_add},
494 {"and", 2, 2, f_and},
495 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200496 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200497 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"argidx", 0, 0, f_argidx},
499 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200500 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501#ifdef FEAT_FLOAT
502 {"asin", 1, 1, f_asin}, /* WJMc */
503#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100504 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100506 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200508 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100510 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_match", 2, 3, f_assert_match},
512 {"assert_notequal", 2, 3, f_assert_notequal},
513 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100514 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515 {"assert_true", 1, 2, f_assert_true},
516#ifdef FEAT_FLOAT
517 {"atan", 1, 1, f_atan},
518 {"atan2", 2, 2, f_atan2},
519#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100520#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200521 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100522 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100523# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100524 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100525# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100526#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"browse", 4, 4, f_browse},
528 {"browsedir", 2, 2, f_browsedir},
529 {"bufexists", 1, 1, f_bufexists},
530 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
531 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
532 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
533 {"buflisted", 1, 1, f_buflisted},
534 {"bufloaded", 1, 1, f_bufloaded},
535 {"bufname", 1, 1, f_bufname},
536 {"bufnr", 1, 2, f_bufnr},
537 {"bufwinid", 1, 1, f_bufwinid},
538 {"bufwinnr", 1, 1, f_bufwinnr},
539 {"byte2line", 1, 1, f_byte2line},
540 {"byteidx", 2, 2, f_byteidx},
541 {"byteidxcomp", 2, 2, f_byteidxcomp},
542 {"call", 2, 3, f_call},
543#ifdef FEAT_FLOAT
544 {"ceil", 1, 1, f_ceil},
545#endif
546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100547 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200549 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
551 {"ch_evalraw", 2, 3, f_ch_evalraw},
552 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
553 {"ch_getjob", 1, 1, f_ch_getjob},
554 {"ch_info", 1, 1, f_ch_info},
555 {"ch_log", 1, 2, f_ch_log},
556 {"ch_logfile", 1, 2, f_ch_logfile},
557 {"ch_open", 1, 2, f_ch_open},
558 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100559 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200560 {"ch_readraw", 1, 2, f_ch_readraw},
561 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
562 {"ch_sendraw", 2, 3, f_ch_sendraw},
563 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200564 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565#endif
566 {"changenr", 0, 0, f_changenr},
567 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200568 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100570 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200571 {"col", 1, 1, f_col},
572#if defined(FEAT_INS_EXPAND)
573 {"complete", 2, 2, f_complete},
574 {"complete_add", 1, 1, f_complete_add},
575 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100576 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577#endif
578 {"confirm", 1, 4, f_confirm},
579 {"copy", 1, 1, f_copy},
580#ifdef FEAT_FLOAT
581 {"cos", 1, 1, f_cos},
582 {"cosh", 1, 1, f_cosh},
583#endif
584 {"count", 2, 4, f_count},
585 {"cscope_connection",0,3, f_cscope_connection},
586 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100587#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200588 {"debugbreak", 1, 1, f_debugbreak},
589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"deepcopy", 1, 2, f_deepcopy},
591 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200592 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"did_filetype", 0, 0, f_did_filetype},
594 {"diff_filler", 1, 1, f_diff_filler},
595 {"diff_hlID", 2, 2, f_diff_hlID},
596 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200597 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200598 {"escape", 2, 2, f_escape},
599 {"eval", 1, 1, f_eval},
600 {"eventhandler", 0, 0, f_eventhandler},
601 {"executable", 1, 1, f_executable},
602 {"execute", 1, 2, f_execute},
603 {"exepath", 1, 1, f_exepath},
604 {"exists", 1, 1, f_exists},
605#ifdef FEAT_FLOAT
606 {"exp", 1, 1, f_exp},
607#endif
608 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200609 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"extend", 2, 3, f_extend},
611 {"feedkeys", 1, 2, f_feedkeys},
612 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
613 {"filereadable", 1, 1, f_filereadable},
614 {"filewritable", 1, 1, f_filewritable},
615 {"filter", 2, 2, f_filter},
616 {"finddir", 1, 3, f_finddir},
617 {"findfile", 1, 3, f_findfile},
618#ifdef FEAT_FLOAT
619 {"float2nr", 1, 1, f_float2nr},
620 {"floor", 1, 1, f_floor},
621 {"fmod", 2, 2, f_fmod},
622#endif
623 {"fnameescape", 1, 1, f_fnameescape},
624 {"fnamemodify", 2, 2, f_fnamemodify},
625 {"foldclosed", 1, 1, f_foldclosed},
626 {"foldclosedend", 1, 1, f_foldclosedend},
627 {"foldlevel", 1, 1, f_foldlevel},
628 {"foldtext", 0, 0, f_foldtext},
629 {"foldtextresult", 1, 1, f_foldtextresult},
630 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"function", 1, 3, f_function},
633 {"garbagecollect", 0, 1, f_garbagecollect},
634 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getbufline", 2, 3, f_getbufline},
637 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100638 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getchar", 0, 1, f_getchar},
640 {"getcharmod", 0, 0, f_getcharmod},
641 {"getcharsearch", 0, 0, f_getcharsearch},
642 {"getcmdline", 0, 0, f_getcmdline},
643 {"getcmdpos", 0, 0, f_getcmdpos},
644 {"getcmdtype", 0, 0, f_getcmdtype},
645 {"getcmdwintype", 0, 0, f_getcmdwintype},
646#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200647 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
649 {"getcurpos", 0, 0, f_getcurpos},
650 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200651 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100660 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100669 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200670 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100671 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getwinposx", 0, 0, f_getwinposx},
673 {"getwinposy", 0, 0, f_getwinposy},
674 {"getwinvar", 2, 3, f_getwinvar},
675 {"glob", 1, 4, f_glob},
676 {"glob2regpat", 1, 1, f_glob2regpat},
677 {"globpath", 2, 5, f_globpath},
678 {"has", 1, 1, f_has},
679 {"has_key", 2, 2, f_has_key},
680 {"haslocaldir", 0, 2, f_haslocaldir},
681 {"hasmapto", 1, 3, f_hasmapto},
682 {"highlightID", 1, 1, f_hlID}, /* obsolete */
683 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
684 {"histadd", 2, 2, f_histadd},
685 {"histdel", 1, 2, f_histdel},
686 {"histget", 1, 2, f_histget},
687 {"histnr", 1, 1, f_histnr},
688 {"hlID", 1, 1, f_hlID},
689 {"hlexists", 1, 1, f_hlexists},
690 {"hostname", 0, 0, f_hostname},
691 {"iconv", 3, 3, f_iconv},
692 {"indent", 1, 1, f_indent},
693 {"index", 2, 4, f_index},
694 {"input", 1, 3, f_input},
695 {"inputdialog", 1, 3, f_inputdialog},
696 {"inputlist", 1, 1, f_inputlist},
697 {"inputrestore", 0, 0, f_inputrestore},
698 {"inputsave", 0, 0, f_inputsave},
699 {"inputsecret", 1, 2, f_inputsecret},
700 {"insert", 2, 3, f_insert},
701 {"invert", 1, 1, f_invert},
702 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200703#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
704 {"isinf", 1, 1, f_isinf},
705#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"islocked", 1, 1, f_islocked},
707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
708 {"isnan", 1, 1, f_isnan},
709#endif
710 {"items", 1, 1, f_items},
711#ifdef FEAT_JOB_CHANNEL
712 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200713 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200714 {"job_setoptions", 2, 2, f_job_setoptions},
715 {"job_start", 1, 2, f_job_start},
716 {"job_status", 1, 1, f_job_status},
717 {"job_stop", 1, 2, f_job_stop},
718#endif
719 {"join", 1, 2, f_join},
720 {"js_decode", 1, 1, f_js_decode},
721 {"js_encode", 1, 1, f_js_encode},
722 {"json_decode", 1, 1, f_json_decode},
723 {"json_encode", 1, 1, f_json_encode},
724 {"keys", 1, 1, f_keys},
725 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
726 {"len", 1, 1, f_len},
727 {"libcall", 3, 3, f_libcall},
728 {"libcallnr", 3, 3, f_libcallnr},
729 {"line", 1, 1, f_line},
730 {"line2byte", 1, 1, f_line2byte},
731 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200732 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200733 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200734 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200735 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200736 {"localtime", 0, 0, f_localtime},
737#ifdef FEAT_FLOAT
738 {"log", 1, 1, f_log},
739 {"log10", 1, 1, f_log10},
740#endif
741#ifdef FEAT_LUA
742 {"luaeval", 1, 2, f_luaeval},
743#endif
744 {"map", 2, 2, f_map},
745 {"maparg", 1, 4, f_maparg},
746 {"mapcheck", 1, 3, f_mapcheck},
747 {"match", 2, 4, f_match},
748 {"matchadd", 2, 5, f_matchadd},
749 {"matchaddpos", 2, 5, f_matchaddpos},
750 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100751 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"matchend", 2, 4, f_matchend},
753 {"matchlist", 2, 4, f_matchlist},
754 {"matchstr", 2, 4, f_matchstr},
755 {"matchstrpos", 2, 4, f_matchstrpos},
756 {"max", 1, 1, f_max},
757 {"min", 1, 1, f_min},
758#ifdef vim_mkdir
759 {"mkdir", 1, 3, f_mkdir},
760#endif
761 {"mode", 0, 1, f_mode},
762#ifdef FEAT_MZSCHEME
763 {"mzeval", 1, 1, f_mzeval},
764#endif
765 {"nextnonblank", 1, 1, f_nextnonblank},
766 {"nr2char", 1, 2, f_nr2char},
767 {"or", 2, 2, f_or},
768 {"pathshorten", 1, 1, f_pathshorten},
769#ifdef FEAT_PERL
770 {"perleval", 1, 1, f_perleval},
771#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200772#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200773 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200774 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200775 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200776 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200777 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200778 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200779 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200780 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200781 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200782 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200783 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200784 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200785 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaarae943152019-06-16 22:54:14 +0200786 {"popup_setoptions", 2, 2, f_popup_setoptions},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200787 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200788 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200789#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790#ifdef FEAT_FLOAT
791 {"pow", 2, 2, f_pow},
792#endif
793 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100794 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200795#ifdef FEAT_JOB_CHANNEL
796 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200797 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200798 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
799#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100800#ifdef FEAT_TEXT_PROP
801 {"prop_add", 3, 3, f_prop_add},
802 {"prop_clear", 1, 3, f_prop_clear},
803 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100804 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100805 {"prop_type_add", 2, 2, f_prop_type_add},
806 {"prop_type_change", 2, 2, f_prop_type_change},
807 {"prop_type_delete", 1, 2, f_prop_type_delete},
808 {"prop_type_get", 1, 2, f_prop_type_get},
809 {"prop_type_list", 0, 1, f_prop_type_list},
810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811 {"pumvisible", 0, 0, f_pumvisible},
812#ifdef FEAT_PYTHON3
813 {"py3eval", 1, 1, f_py3eval},
814#endif
815#ifdef FEAT_PYTHON
816 {"pyeval", 1, 1, f_pyeval},
817#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100818#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
819 {"pyxeval", 1, 1, f_pyxeval},
820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200822 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200824 {"reg_executing", 0, 0, f_reg_executing},
825 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200826 {"reltime", 0, 2, f_reltime},
827#ifdef FEAT_FLOAT
828 {"reltimefloat", 1, 1, f_reltimefloat},
829#endif
830 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100831 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200832 {"remote_foreground", 1, 1, f_remote_foreground},
833 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100834 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100836 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837 {"remove", 2, 3, f_remove},
838 {"rename", 2, 2, f_rename},
839 {"repeat", 2, 2, f_repeat},
840 {"resolve", 1, 1, f_resolve},
841 {"reverse", 1, 1, f_reverse},
842#ifdef FEAT_FLOAT
843 {"round", 1, 1, f_round},
844#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100845#ifdef FEAT_RUBY
846 {"rubyeval", 1, 1, f_rubyeval},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"screenattr", 2, 2, f_screenattr},
849 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100850 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"screencol", 0, 0, f_screencol},
852 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100853 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"search", 1, 4, f_search},
855 {"searchdecl", 1, 3, f_searchdecl},
856 {"searchpair", 3, 7, f_searchpair},
857 {"searchpairpos", 3, 7, f_searchpairpos},
858 {"searchpos", 1, 4, f_searchpos},
859 {"server2client", 2, 2, f_server2client},
860 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200861 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"setbufvar", 3, 3, f_setbufvar},
863 {"setcharsearch", 1, 1, f_setcharsearch},
864 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200865 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"setfperm", 2, 2, f_setfperm},
867 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200868 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100869 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200871 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"setreg", 2, 3, f_setreg},
873 {"settabvar", 3, 3, f_settabvar},
874 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100875 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"setwinvar", 3, 3, f_setwinvar},
877#ifdef FEAT_CRYPT
878 {"sha256", 1, 1, f_sha256},
879#endif
880 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100881 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100882#ifdef FEAT_SIGNS
883 {"sign_define", 1, 2, f_sign_define},
884 {"sign_getdefined", 0, 1, f_sign_getdefined},
885 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100886 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100887 {"sign_place", 4, 5, f_sign_place},
888 {"sign_undefine", 0, 1, f_sign_undefine},
889 {"sign_unplace", 1, 2, f_sign_unplace},
890#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"simplify", 1, 1, f_simplify},
892#ifdef FEAT_FLOAT
893 {"sin", 1, 1, f_sin},
894 {"sinh", 1, 1, f_sinh},
895#endif
896 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200897#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200898 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200899 {"sound_playevent", 1, 2, f_sound_playevent},
900 {"sound_playfile", 1, 2, f_sound_playfile},
901 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200902#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"soundfold", 1, 1, f_soundfold},
904 {"spellbadword", 0, 1, f_spellbadword},
905 {"spellsuggest", 1, 3, f_spellsuggest},
906 {"split", 1, 3, f_split},
907#ifdef FEAT_FLOAT
908 {"sqrt", 1, 1, f_sqrt},
909 {"str2float", 1, 1, f_str2float},
910#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200911 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912 {"str2nr", 1, 2, f_str2nr},
913 {"strcharpart", 2, 3, f_strcharpart},
914 {"strchars", 1, 2, f_strchars},
915 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
916#ifdef HAVE_STRFTIME
917 {"strftime", 1, 2, f_strftime},
918#endif
919 {"strgetchar", 2, 2, f_strgetchar},
920 {"stridx", 2, 3, f_stridx},
921 {"string", 1, 1, f_string},
922 {"strlen", 1, 1, f_strlen},
923 {"strpart", 2, 3, f_strpart},
924 {"strridx", 2, 3, f_strridx},
925 {"strtrans", 1, 1, f_strtrans},
926 {"strwidth", 1, 1, f_strwidth},
927 {"submatch", 1, 2, f_submatch},
928 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200929 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200930 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"synID", 3, 3, f_synID},
932 {"synIDattr", 2, 3, f_synIDattr},
933 {"synIDtrans", 1, 1, f_synIDtrans},
934 {"synconcealed", 2, 2, f_synconcealed},
935 {"synstack", 2, 2, f_synstack},
936 {"system", 1, 2, f_system},
937 {"systemlist", 1, 2, f_systemlist},
938 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
939 {"tabpagenr", 0, 1, f_tabpagenr},
940 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
941 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100942 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#ifdef FEAT_FLOAT
944 {"tan", 1, 1, f_tan},
945 {"tanh", 1, 1, f_tanh},
946#endif
947 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200948#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100949 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
950 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100951 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200952 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200953# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
954 {"term_getansicolors", 1, 1, f_term_getansicolors},
955# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200956 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200957 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200958 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200959 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200960 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200961 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200962 {"term_getstatus", 1, 1, f_term_getstatus},
963 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200964 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200965 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200966 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200968# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
969 {"term_setansicolors", 2, 2, f_term_setansicolors},
970# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100971 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100972 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200973 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200975 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
978 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200979 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200980 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaaradc67142019-06-22 01:40:42 +0200981 {"test_garbagecollect_soon", 0, 0, f_test_garbagecollect_soon},
Bram Moolenaareda65222019-05-16 20:29:44 +0200982 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100983 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100984 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#ifdef FEAT_JOB_CHANNEL
986 {"test_null_channel", 0, 0, f_test_null_channel},
987#endif
988 {"test_null_dict", 0, 0, f_test_null_dict},
989#ifdef FEAT_JOB_CHANNEL
990 {"test_null_job", 0, 0, f_test_null_job},
991#endif
992 {"test_null_list", 0, 0, f_test_null_list},
993 {"test_null_partial", 0, 0, f_test_null_partial},
994 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200995 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100996 {"test_override", 2, 2, f_test_override},
997 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200998#ifdef FEAT_GUI
999 {"test_scrollbar", 3, 3, f_test_scrollbar},
1000#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001001#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001002 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001003#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"test_settime", 1, 1, f_test_settime},
1005#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001006 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001007 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 {"timer_start", 2, 3, f_timer_start},
1009 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001010 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011#endif
1012 {"tolower", 1, 1, f_tolower},
1013 {"toupper", 1, 1, f_toupper},
1014 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001015 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001016#ifdef FEAT_FLOAT
1017 {"trunc", 1, 1, f_trunc},
1018#endif
1019 {"type", 1, 1, f_type},
1020 {"undofile", 1, 1, f_undofile},
1021 {"undotree", 0, 0, f_undotree},
1022 {"uniq", 1, 3, f_uniq},
1023 {"values", 1, 1, f_values},
1024 {"virtcol", 1, 1, f_virtcol},
1025 {"visualmode", 0, 1, f_visualmode},
1026 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001027 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"win_findbuf", 1, 1, f_win_findbuf},
1029 {"win_getid", 0, 2, f_win_getid},
1030 {"win_gotoid", 1, 1, f_win_gotoid},
1031 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1032 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001033 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001034 {"winbufnr", 1, 1, f_winbufnr},
1035 {"wincol", 0, 0, f_wincol},
1036 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001037 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038 {"winline", 0, 0, f_winline},
1039 {"winnr", 0, 1, f_winnr},
1040 {"winrestcmd", 0, 0, f_winrestcmd},
1041 {"winrestview", 1, 1, f_winrestview},
1042 {"winsaveview", 0, 0, f_winsaveview},
1043 {"winwidth", 1, 1, f_winwidth},
1044 {"wordcount", 0, 0, f_wordcount},
1045 {"writefile", 2, 3, f_writefile},
1046 {"xor", 2, 2, f_xor},
1047};
1048
1049#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1050
1051/*
1052 * Function given to ExpandGeneric() to obtain the list of internal
1053 * or user defined function names.
1054 */
1055 char_u *
1056get_function_name(expand_T *xp, int idx)
1057{
1058 static int intidx = -1;
1059 char_u *name;
1060
1061 if (idx == 0)
1062 intidx = -1;
1063 if (intidx < 0)
1064 {
1065 name = get_user_func_name(xp, idx);
1066 if (name != NULL)
1067 return name;
1068 }
1069 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1070 {
1071 STRCPY(IObuff, functions[intidx].f_name);
1072 STRCAT(IObuff, "(");
1073 if (functions[intidx].f_max_argc == 0)
1074 STRCAT(IObuff, ")");
1075 return IObuff;
1076 }
1077
1078 return NULL;
1079}
1080
1081/*
1082 * Function given to ExpandGeneric() to obtain the list of internal or
1083 * user defined variable or function names.
1084 */
1085 char_u *
1086get_expr_name(expand_T *xp, int idx)
1087{
1088 static int intidx = -1;
1089 char_u *name;
1090
1091 if (idx == 0)
1092 intidx = -1;
1093 if (intidx < 0)
1094 {
1095 name = get_function_name(xp, idx);
1096 if (name != NULL)
1097 return name;
1098 }
1099 return get_user_var_name(xp, ++intidx);
1100}
1101
1102#endif /* FEAT_CMDL_COMPL */
1103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001104/*
1105 * Find internal function in table above.
1106 * Return index, or -1 if not found
1107 */
1108 int
1109find_internal_func(
1110 char_u *name) /* name of the function */
1111{
1112 int first = 0;
1113 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1114 int cmp;
1115 int x;
1116
1117 /*
1118 * Find the function name in the table. Binary search.
1119 */
1120 while (first <= last)
1121 {
1122 x = first + ((unsigned)(last - first) >> 1);
1123 cmp = STRCMP(name, functions[x].f_name);
1124 if (cmp < 0)
1125 last = x - 1;
1126 else if (cmp > 0)
1127 first = x + 1;
1128 else
1129 return x;
1130 }
1131 return -1;
1132}
1133
1134 int
1135call_internal_func(
1136 char_u *name,
1137 int argcount,
1138 typval_T *argvars,
1139 typval_T *rettv)
1140{
1141 int i;
1142
1143 i = find_internal_func(name);
1144 if (i < 0)
1145 return ERROR_UNKNOWN;
1146 if (argcount < functions[i].f_min_argc)
1147 return ERROR_TOOFEW;
1148 if (argcount > functions[i].f_max_argc)
1149 return ERROR_TOOMANY;
1150 argvars[argcount].v_type = VAR_UNKNOWN;
1151 functions[i].f_func(argvars, rettv);
1152 return ERROR_NONE;
1153}
1154
1155/*
1156 * Return TRUE for a non-zero Number and a non-empty String.
1157 */
1158 static int
1159non_zero_arg(typval_T *argvars)
1160{
1161 return ((argvars[0].v_type == VAR_NUMBER
1162 && argvars[0].vval.v_number != 0)
1163 || (argvars[0].v_type == VAR_SPECIAL
1164 && argvars[0].vval.v_number == VVAL_TRUE)
1165 || (argvars[0].v_type == VAR_STRING
1166 && argvars[0].vval.v_string != NULL
1167 && *argvars[0].vval.v_string != NUL));
1168}
1169
1170/*
1171 * Get the lnum from the first argument.
1172 * Also accepts ".", "$", etc., but that only works for the current buffer.
1173 * Returns -1 on error.
1174 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001175 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001176tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001177{
1178 typval_T rettv;
1179 linenr_T lnum;
1180
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001182 if (lnum == 0) /* no valid number, try using line() */
1183 {
1184 rettv.v_type = VAR_NUMBER;
1185 f_line(argvars, &rettv);
1186 lnum = (linenr_T)rettv.vval.v_number;
1187 clear_tv(&rettv);
1188 }
1189 return lnum;
1190}
1191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001192/*
1193 * Get the lnum from the first argument.
1194 * Also accepts "$", then "buf" is used.
1195 * Returns 0 on error.
1196 */
1197 static linenr_T
1198tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1199{
1200 if (argvars[0].v_type == VAR_STRING
1201 && argvars[0].vval.v_string != NULL
1202 && argvars[0].vval.v_string[0] == '$'
1203 && buf != NULL)
1204 return buf->b_ml.ml_line_count;
1205 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1206}
1207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001208#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209/*
1210 * Get the float value of "argvars[0]" into "f".
1211 * Returns FAIL when the argument is not a Number or Float.
1212 */
1213 static int
1214get_float_arg(typval_T *argvars, float_T *f)
1215{
1216 if (argvars[0].v_type == VAR_FLOAT)
1217 {
1218 *f = argvars[0].vval.v_float;
1219 return OK;
1220 }
1221 if (argvars[0].v_type == VAR_NUMBER)
1222 {
1223 *f = (float_T)argvars[0].vval.v_number;
1224 return OK;
1225 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227 return FAIL;
1228}
1229
1230/*
1231 * "abs(expr)" function
1232 */
1233 static void
1234f_abs(typval_T *argvars, typval_T *rettv)
1235{
1236 if (argvars[0].v_type == VAR_FLOAT)
1237 {
1238 rettv->v_type = VAR_FLOAT;
1239 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1240 }
1241 else
1242 {
1243 varnumber_T n;
1244 int error = FALSE;
1245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001246 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 if (error)
1248 rettv->vval.v_number = -1;
1249 else if (n > 0)
1250 rettv->vval.v_number = n;
1251 else
1252 rettv->vval.v_number = -n;
1253 }
1254}
1255
1256/*
1257 * "acos()" function
1258 */
1259 static void
1260f_acos(typval_T *argvars, typval_T *rettv)
1261{
1262 float_T f = 0.0;
1263
1264 rettv->v_type = VAR_FLOAT;
1265 if (get_float_arg(argvars, &f) == OK)
1266 rettv->vval.v_float = acos(f);
1267 else
1268 rettv->vval.v_float = 0.0;
1269}
1270#endif
1271
1272/*
1273 * "add(list, item)" function
1274 */
1275 static void
1276f_add(typval_T *argvars, typval_T *rettv)
1277{
1278 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280
1281 rettv->vval.v_number = 1; /* Default: Failed */
1282 if (argvars[0].v_type == VAR_LIST)
1283 {
1284 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001285 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001286 (char_u *)N_("add() argument"), TRUE)
1287 && list_append_tv(l, &argvars[1]) == OK)
1288 copy_tv(&argvars[0], rettv);
1289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 else if (argvars[0].v_type == VAR_BLOB)
1291 {
1292 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001293 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 (char_u *)N_("add() argument"), TRUE))
1295 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001296 int error = FALSE;
1297 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1298
1299 if (!error)
1300 {
1301 ga_append(&b->bv_ga, (int)n);
1302 copy_tv(&argvars[0], rettv);
1303 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 }
1305 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001307 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308}
1309
1310/*
1311 * "and(expr, expr)" function
1312 */
1313 static void
1314f_and(typval_T *argvars, typval_T *rettv)
1315{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001316 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1317 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001318}
1319
1320/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001321 * If there is a window for "curbuf", make it the current window.
1322 */
1323 static void
1324find_win_for_curbuf(void)
1325{
1326 wininfo_T *wip;
1327
1328 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1329 {
1330 if (wip->wi_win != NULL)
1331 {
1332 curwin = wip->wi_win;
1333 break;
1334 }
1335 }
1336}
1337
1338/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001339 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 */
1341 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001342set_buffer_lines(
1343 buf_T *buf,
1344 linenr_T lnum_arg,
1345 int append,
1346 typval_T *lines,
1347 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001348{
Bram Moolenaarca851592018-06-06 21:04:07 +02001349 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1350 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351 list_T *l = NULL;
1352 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001354 linenr_T append_lnum;
1355 buf_T *curbuf_save = NULL;
1356 win_T *curwin_save = NULL;
1357 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001358
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 /* When using the current buffer ml_mfp will be set if needed. Useful when
1360 * setline() is used on startup. For other buffers the buffer must be
1361 * loaded. */
1362 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001364 rettv->vval.v_number = 1; /* FAIL */
1365 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 }
1367
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 curbuf_save = curbuf;
1371 curwin_save = curwin;
1372 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001373 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 }
1375
1376 if (append)
1377 // appendbufline() uses the line number below which we insert
1378 append_lnum = lnum - 1;
1379 else
1380 // setbufline() uses the line number above which we insert, we only
1381 // append if it's below the last line
1382 append_lnum = curbuf->b_ml.ml_line_count;
1383
1384 if (lines->v_type == VAR_LIST)
1385 {
1386 l = lines->vval.v_list;
1387 li = l->lv_first;
1388 }
1389 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001391
1392 /* default result is zero == OK */
1393 for (;;)
1394 {
1395 if (l != NULL)
1396 {
1397 /* list argument, get next string */
1398 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001400 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 li = li->li_next;
1402 }
1403
Bram Moolenaarca851592018-06-06 21:04:07 +02001404 rettv->vval.v_number = 1; /* FAIL */
1405 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1406 break;
1407
1408 /* When coming here from Insert mode, sync undo, so that this can be
1409 * undone separately from what was previously inserted. */
1410 if (u_sync_once == 2)
1411 {
1412 u_sync_once = 1; /* notify that u_sync() was called */
1413 u_sync(TRUE);
1414 }
1415
1416 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1417 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001418 // Existing line, replace it.
1419 // Removes any existing text properties.
1420 if (u_savesub(lnum) == OK && ml_replace_len(
1421 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001422 {
1423 changed_bytes(lnum, 0);
1424 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1425 check_cursor_col();
1426 rettv->vval.v_number = 0; /* OK */
1427 }
1428 }
1429 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1430 {
1431 /* append the line */
1432 ++added;
1433 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1434 rettv->vval.v_number = 0; /* OK */
1435 }
1436
1437 if (l == NULL) /* only one string argument */
1438 break;
1439 ++lnum;
1440 }
1441
1442 if (added > 0)
1443 {
1444 win_T *wp;
1445 tabpage_T *tp;
1446
1447 appended_lines_mark(append_lnum, added);
1448 FOR_ALL_TAB_WINDOWS(tp, wp)
1449 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1450 wp->w_cursor.lnum += added;
1451 check_cursor_col();
1452
Bram Moolenaarf2732452018-06-03 14:47:35 +02001453#ifdef FEAT_JOB_CHANNEL
1454 if (bt_prompt(curbuf) && (State & INSERT))
1455 // show the line with the prompt
1456 update_topline();
1457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001459
1460 if (!is_curbuf)
1461 {
1462 curbuf = curbuf_save;
1463 curwin = curwin_save;
1464 }
1465}
1466
1467/*
1468 * "append(lnum, string/list)" function
1469 */
1470 static void
1471f_append(typval_T *argvars, typval_T *rettv)
1472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001474
1475 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1476}
1477
1478/*
1479 * "appendbufline(buf, lnum, string/list)" function
1480 */
1481 static void
1482f_appendbufline(typval_T *argvars, typval_T *rettv)
1483{
1484 linenr_T lnum;
1485 buf_T *buf;
1486
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001487 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 if (buf == NULL)
1489 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1494 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495}
1496
1497/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001498 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001499 */
1500 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001501f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001503 win_T *wp;
1504
1505 if (argvars[0].v_type == VAR_UNKNOWN)
1506 // use the current window
1507 rettv->vval.v_number = ARGCOUNT;
1508 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001509 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 // use the global argument list
1511 rettv->vval.v_number = GARGCOUNT;
1512 else
1513 {
1514 // use the argument list of the specified window
1515 wp = find_win_by_nr_or_id(&argvars[0]);
1516 if (wp != NULL)
1517 rettv->vval.v_number = WARGCOUNT(wp);
1518 else
1519 rettv->vval.v_number = -1;
1520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521}
1522
1523/*
1524 * "argidx()" function
1525 */
1526 static void
1527f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1528{
1529 rettv->vval.v_number = curwin->w_arg_idx;
1530}
1531
1532/*
1533 * "arglistid()" function
1534 */
1535 static void
1536f_arglistid(typval_T *argvars, typval_T *rettv)
1537{
1538 win_T *wp;
1539
1540 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001541 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001542 if (wp != NULL)
1543 rettv->vval.v_number = wp->w_alist->id;
1544}
1545
1546/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 * Get the argument list for a given window
1548 */
1549 static void
1550get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1551{
1552 int idx;
1553
1554 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1555 for (idx = 0; idx < argcount; ++idx)
1556 list_append_string(rettv->vval.v_list,
1557 alist_name(&arglist[idx]), -1);
1558}
1559
1560/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 * "argv(nr)" function
1562 */
1563 static void
1564f_argv(typval_T *argvars, typval_T *rettv)
1565{
1566 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001567 aentry_T *arglist = NULL;
1568 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569
1570 if (argvars[0].v_type != VAR_UNKNOWN)
1571 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001572 if (argvars[1].v_type == VAR_UNKNOWN)
1573 {
1574 arglist = ARGLIST;
1575 argcount = ARGCOUNT;
1576 }
1577 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001578 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 {
1580 arglist = GARGLIST;
1581 argcount = GARGCOUNT;
1582 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 {
1585 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1586
1587 if (wp != NULL)
1588 {
1589 /* Use the argument list of the specified window */
1590 arglist = WARGLIST(wp);
1591 argcount = WARGCOUNT(wp);
1592 }
1593 }
1594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 if (arglist != NULL && idx >= 0 && idx < argcount)
1599 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1600 else if (idx == -1)
1601 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 else
1604 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605}
1606
1607/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001608 * "assert_beeps(cmd [, error])" function
1609 */
1610 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001611f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001612{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001614}
1615
1616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 * "assert_equal(expected, actual[, msg])" function
1618 */
1619 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001620f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623}
1624
1625/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001626 * "assert_equalfile(fname-one, fname-two)" function
1627 */
1628 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001629f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001630{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001632}
1633
1634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 * "assert_notequal(expected, actual[, msg])" function
1636 */
1637 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001638f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641}
1642
1643/*
1644 * "assert_exception(string[, msg])" function
1645 */
1646 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650}
1651
1652/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001653 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 */
1655 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659}
1660
1661/*
1662 * "assert_false(actual[, msg])" function
1663 */
1664 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668}
1669
1670/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001671 * "assert_inrange(lower, upper[, msg])" function
1672 */
1673 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001675{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001677}
1678
1679/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680 * "assert_match(pattern, actual[, msg])" function
1681 */
1682 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686}
1687
1688/*
1689 * "assert_notmatch(pattern, actual[, msg])" function
1690 */
1691 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695}
1696
1697/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001698 * "assert_report(msg)" function
1699 */
1700 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001702{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001704}
1705
1706/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707 * "assert_true(actual[, msg])" function
1708 */
1709 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001711{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713}
1714
1715#ifdef FEAT_FLOAT
1716/*
1717 * "asin()" function
1718 */
1719 static void
1720f_asin(typval_T *argvars, typval_T *rettv)
1721{
1722 float_T f = 0.0;
1723
1724 rettv->v_type = VAR_FLOAT;
1725 if (get_float_arg(argvars, &f) == OK)
1726 rettv->vval.v_float = asin(f);
1727 else
1728 rettv->vval.v_float = 0.0;
1729}
1730
1731/*
1732 * "atan()" function
1733 */
1734 static void
1735f_atan(typval_T *argvars, typval_T *rettv)
1736{
1737 float_T f = 0.0;
1738
1739 rettv->v_type = VAR_FLOAT;
1740 if (get_float_arg(argvars, &f) == OK)
1741 rettv->vval.v_float = atan(f);
1742 else
1743 rettv->vval.v_float = 0.0;
1744}
1745
1746/*
1747 * "atan2()" function
1748 */
1749 static void
1750f_atan2(typval_T *argvars, typval_T *rettv)
1751{
1752 float_T fx = 0.0, fy = 0.0;
1753
1754 rettv->v_type = VAR_FLOAT;
1755 if (get_float_arg(argvars, &fx) == OK
1756 && get_float_arg(&argvars[1], &fy) == OK)
1757 rettv->vval.v_float = atan2(fx, fy);
1758 else
1759 rettv->vval.v_float = 0.0;
1760}
1761#endif
1762
1763/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001764 * "balloon_show()" function
1765 */
1766#ifdef FEAT_BEVAL
1767 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001768f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1769{
1770 rettv->v_type = VAR_STRING;
1771 if (balloonEval != NULL)
1772 {
1773 if (balloonEval->msg == NULL)
1774 rettv->vval.v_string = NULL;
1775 else
1776 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1777 }
1778}
1779
1780 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001781f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1782{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001783 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001784 {
1785 if (argvars[0].v_type == VAR_LIST
1786# ifdef FEAT_GUI
1787 && !gui.in_use
1788# endif
1789 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001790 {
1791 list_T *l = argvars[0].vval.v_list;
1792
1793 // empty list removes the balloon
1794 post_balloon(balloonEval, NULL,
1795 l == NULL || l->lv_len == 0 ? NULL : l);
1796 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001797 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001798 {
1799 char_u *mesg = tv_get_string_chk(&argvars[0]);
1800
1801 if (mesg != NULL)
1802 // empty string removes the balloon
1803 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1804 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001805 }
1806}
1807
Bram Moolenaar669a8282017-11-19 20:13:05 +01001808# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 static void
1810f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1811{
1812 if (rettv_list_alloc(rettv) == OK)
1813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001814 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001815
1816 if (msg != NULL)
1817 {
1818 pumitem_T *array;
1819 int size = split_message(msg, &array);
1820 int i;
1821
1822 /* Skip the first and last item, they are always empty. */
1823 for (i = 1; i < size - 1; ++i)
1824 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001825 while (size > 0)
1826 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827 vim_free(array);
1828 }
1829 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001830}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001831# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001832#endif
1833
1834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001835 * "browse(save, title, initdir, default)" function
1836 */
1837 static void
1838f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1839{
1840#ifdef FEAT_BROWSE
1841 int save;
1842 char_u *title;
1843 char_u *initdir;
1844 char_u *defname;
1845 char_u buf[NUMBUFLEN];
1846 char_u buf2[NUMBUFLEN];
1847 int error = FALSE;
1848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001849 save = (int)tv_get_number_chk(&argvars[0], &error);
1850 title = tv_get_string_chk(&argvars[1]);
1851 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1852 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853
1854 if (error || title == NULL || initdir == NULL || defname == NULL)
1855 rettv->vval.v_string = NULL;
1856 else
1857 rettv->vval.v_string =
1858 do_browse(save ? BROWSE_SAVE : 0,
1859 title, defname, NULL, initdir, NULL, curbuf);
1860#else
1861 rettv->vval.v_string = NULL;
1862#endif
1863 rettv->v_type = VAR_STRING;
1864}
1865
1866/*
1867 * "browsedir(title, initdir)" function
1868 */
1869 static void
1870f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1871{
1872#ifdef FEAT_BROWSE
1873 char_u *title;
1874 char_u *initdir;
1875 char_u buf[NUMBUFLEN];
1876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001877 title = tv_get_string_chk(&argvars[0]);
1878 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001879
1880 if (title == NULL || initdir == NULL)
1881 rettv->vval.v_string = NULL;
1882 else
1883 rettv->vval.v_string = do_browse(BROWSE_DIR,
1884 title, NULL, NULL, initdir, NULL, curbuf);
1885#else
1886 rettv->vval.v_string = NULL;
1887#endif
1888 rettv->v_type = VAR_STRING;
1889}
1890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891/*
1892 * Find a buffer by number or exact name.
1893 */
1894 static buf_T *
1895find_buffer(typval_T *avar)
1896{
1897 buf_T *buf = NULL;
1898
1899 if (avar->v_type == VAR_NUMBER)
1900 buf = buflist_findnr((int)avar->vval.v_number);
1901 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1902 {
1903 buf = buflist_findname_exp(avar->vval.v_string);
1904 if (buf == NULL)
1905 {
1906 /* No full path name match, try a match with a URL or a "nofile"
1907 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001908 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001909 if (buf->b_fname != NULL
1910 && (path_with_url(buf->b_fname)
1911#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001912 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913#endif
1914 )
1915 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1916 break;
1917 }
1918 }
1919 return buf;
1920}
1921
1922/*
1923 * "bufexists(expr)" function
1924 */
1925 static void
1926f_bufexists(typval_T *argvars, typval_T *rettv)
1927{
1928 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1929}
1930
1931/*
1932 * "buflisted(expr)" function
1933 */
1934 static void
1935f_buflisted(typval_T *argvars, typval_T *rettv)
1936{
1937 buf_T *buf;
1938
1939 buf = find_buffer(&argvars[0]);
1940 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1941}
1942
1943/*
1944 * "bufloaded(expr)" function
1945 */
1946 static void
1947f_bufloaded(typval_T *argvars, typval_T *rettv)
1948{
1949 buf_T *buf;
1950
1951 buf = find_buffer(&argvars[0]);
1952 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1953}
1954
1955 buf_T *
1956buflist_find_by_name(char_u *name, int curtab_only)
1957{
1958 int save_magic;
1959 char_u *save_cpo;
1960 buf_T *buf;
1961
1962 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1963 save_magic = p_magic;
1964 p_magic = TRUE;
1965 save_cpo = p_cpo;
1966 p_cpo = (char_u *)"";
1967
1968 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1969 TRUE, FALSE, curtab_only));
1970
1971 p_magic = save_magic;
1972 p_cpo = save_cpo;
1973 return buf;
1974}
1975
1976/*
1977 * Get buffer by number or pattern.
1978 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001979 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001980tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001981{
1982 char_u *name = tv->vval.v_string;
1983 buf_T *buf;
1984
1985 if (tv->v_type == VAR_NUMBER)
1986 return buflist_findnr((int)tv->vval.v_number);
1987 if (tv->v_type != VAR_STRING)
1988 return NULL;
1989 if (name == NULL || *name == NUL)
1990 return curbuf;
1991 if (name[0] == '$' && name[1] == NUL)
1992 return lastbuf;
1993
1994 buf = buflist_find_by_name(name, curtab_only);
1995
1996 /* If not found, try expanding the name, like done for bufexists(). */
1997 if (buf == NULL)
1998 buf = find_buffer(tv);
1999
2000 return buf;
2001}
2002
2003/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002004 * Get the buffer from "arg" and give an error and return NULL if it is not
2005 * valid.
2006 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002007 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002008get_buf_arg(typval_T *arg)
2009{
2010 buf_T *buf;
2011
2012 ++emsg_off;
2013 buf = tv_get_buf(arg, FALSE);
2014 --emsg_off;
2015 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002017 return buf;
2018}
2019
2020/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021 * "bufname(expr)" function
2022 */
2023 static void
2024f_bufname(typval_T *argvars, typval_T *rettv)
2025{
2026 buf_T *buf;
2027
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002028 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002029 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002030 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002031 rettv->v_type = VAR_STRING;
2032 if (buf != NULL && buf->b_fname != NULL)
2033 rettv->vval.v_string = vim_strsave(buf->b_fname);
2034 else
2035 rettv->vval.v_string = NULL;
2036 --emsg_off;
2037}
2038
2039/*
2040 * "bufnr(expr)" function
2041 */
2042 static void
2043f_bufnr(typval_T *argvars, typval_T *rettv)
2044{
2045 buf_T *buf;
2046 int error = FALSE;
2047 char_u *name;
2048
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002049 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002051 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002052 --emsg_off;
2053
2054 /* If the buffer isn't found and the second argument is not zero create a
2055 * new buffer. */
2056 if (buf == NULL
2057 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002058 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002059 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002060 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 && !error)
2062 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2063
2064 if (buf != NULL)
2065 rettv->vval.v_number = buf->b_fnum;
2066 else
2067 rettv->vval.v_number = -1;
2068}
2069
2070 static void
2071buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2072{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 win_T *wp;
2074 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 buf_T *buf;
2076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002077 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002078 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002079 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002080 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002081 {
2082 ++winnr;
2083 if (wp->w_buffer == buf)
2084 break;
2085 }
2086 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 --emsg_off;
2088}
2089
2090/*
2091 * "bufwinid(nr)" function
2092 */
2093 static void
2094f_bufwinid(typval_T *argvars, typval_T *rettv)
2095{
2096 buf_win_common(argvars, rettv, FALSE);
2097}
2098
2099/*
2100 * "bufwinnr(nr)" function
2101 */
2102 static void
2103f_bufwinnr(typval_T *argvars, typval_T *rettv)
2104{
2105 buf_win_common(argvars, rettv, TRUE);
2106}
2107
2108/*
2109 * "byte2line(byte)" function
2110 */
2111 static void
2112f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2113{
2114#ifndef FEAT_BYTEOFF
2115 rettv->vval.v_number = -1;
2116#else
2117 long boff = 0;
2118
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002119 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120 if (boff < 0)
2121 rettv->vval.v_number = -1;
2122 else
2123 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2124 (linenr_T)0, &boff);
2125#endif
2126}
2127
2128 static void
2129byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2130{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002131 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132 char_u *str;
2133 varnumber_T idx;
2134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002135 str = tv_get_string_chk(&argvars[0]);
2136 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002137 rettv->vval.v_number = -1;
2138 if (str == NULL || idx < 0)
2139 return;
2140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002141 t = str;
2142 for ( ; idx > 0; idx--)
2143 {
2144 if (*t == NUL) /* EOL reached */
2145 return;
2146 if (enc_utf8 && comp)
2147 t += utf_ptr2len(t);
2148 else
2149 t += (*mb_ptr2len)(t);
2150 }
2151 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152}
2153
2154/*
2155 * "byteidx()" function
2156 */
2157 static void
2158f_byteidx(typval_T *argvars, typval_T *rettv)
2159{
2160 byteidx(argvars, rettv, FALSE);
2161}
2162
2163/*
2164 * "byteidxcomp()" function
2165 */
2166 static void
2167f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2168{
2169 byteidx(argvars, rettv, TRUE);
2170}
2171
2172/*
2173 * "call(func, arglist [, dict])" function
2174 */
2175 static void
2176f_call(typval_T *argvars, typval_T *rettv)
2177{
2178 char_u *func;
2179 partial_T *partial = NULL;
2180 dict_T *selfdict = NULL;
2181
2182 if (argvars[1].v_type != VAR_LIST)
2183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002184 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002185 return;
2186 }
2187 if (argvars[1].vval.v_list == NULL)
2188 return;
2189
2190 if (argvars[0].v_type == VAR_FUNC)
2191 func = argvars[0].vval.v_string;
2192 else if (argvars[0].v_type == VAR_PARTIAL)
2193 {
2194 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002195 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002196 }
2197 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002198 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002199 if (*func == NUL)
2200 return; /* type error or empty name */
2201
2202 if (argvars[2].v_type != VAR_UNKNOWN)
2203 {
2204 if (argvars[2].v_type != VAR_DICT)
2205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 return;
2208 }
2209 selfdict = argvars[2].vval.v_dict;
2210 }
2211
2212 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2213}
2214
2215#ifdef FEAT_FLOAT
2216/*
2217 * "ceil({float})" function
2218 */
2219 static void
2220f_ceil(typval_T *argvars, typval_T *rettv)
2221{
2222 float_T f = 0.0;
2223
2224 rettv->v_type = VAR_FLOAT;
2225 if (get_float_arg(argvars, &f) == OK)
2226 rettv->vval.v_float = ceil(f);
2227 else
2228 rettv->vval.v_float = 0.0;
2229}
2230#endif
2231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232/*
2233 * "changenr()" function
2234 */
2235 static void
2236f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2237{
2238 rettv->vval.v_number = curbuf->b_u_seq_cur;
2239}
2240
2241/*
2242 * "char2nr(string)" function
2243 */
2244 static void
2245f_char2nr(typval_T *argvars, typval_T *rettv)
2246{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002247 if (has_mbyte)
2248 {
2249 int utf8 = 0;
2250
2251 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002252 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002253
2254 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002255 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002256 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002257 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002258 }
2259 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002260 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261}
2262
2263/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002264 * "chdir(dir)" function
2265 */
2266 static void
2267f_chdir(typval_T *argvars, typval_T *rettv)
2268{
2269 char_u *cwd;
2270 cdscope_T scope = CDSCOPE_GLOBAL;
2271
2272 rettv->v_type = VAR_STRING;
2273 rettv->vval.v_string = NULL;
2274
2275 if (argvars[0].v_type != VAR_STRING)
2276 return;
2277
2278 // Return the current directory
2279 cwd = alloc(MAXPATHL);
2280 if (cwd != NULL)
2281 {
2282 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2283 {
2284#ifdef BACKSLASH_IN_FILENAME
2285 slash_adjust(cwd);
2286#endif
2287 rettv->vval.v_string = vim_strsave(cwd);
2288 }
2289 vim_free(cwd);
2290 }
2291
2292 if (curwin->w_localdir != NULL)
2293 scope = CDSCOPE_WINDOW;
2294 else if (curtab->tp_localdir != NULL)
2295 scope = CDSCOPE_TABPAGE;
2296
2297 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2298 // Directory change failed
2299 VIM_CLEAR(rettv->vval.v_string);
2300}
2301
2302/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002303 * "cindent(lnum)" function
2304 */
2305 static void
2306f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2307{
2308#ifdef FEAT_CINDENT
2309 pos_T pos;
2310 linenr_T lnum;
2311
2312 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002313 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002314 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2315 {
2316 curwin->w_cursor.lnum = lnum;
2317 rettv->vval.v_number = get_c_indent();
2318 curwin->w_cursor = pos;
2319 }
2320 else
2321#endif
2322 rettv->vval.v_number = -1;
2323}
2324
Bram Moolenaaraff74912019-03-30 18:11:49 +01002325 static win_T *
2326get_optional_window(typval_T *argvars, int idx)
2327{
2328 win_T *win = curwin;
2329
2330 if (argvars[idx].v_type != VAR_UNKNOWN)
2331 {
2332 win = find_win_by_nr_or_id(&argvars[idx]);
2333 if (win == NULL)
2334 {
2335 emsg(_(e_invalwindow));
2336 return NULL;
2337 }
2338 }
2339 return win;
2340}
2341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342/*
2343 * "clearmatches()" function
2344 */
2345 static void
2346f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2347{
2348#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002349 win_T *win = get_optional_window(argvars, 0);
2350
2351 if (win != NULL)
2352 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353#endif
2354}
2355
2356/*
2357 * "col(string)" function
2358 */
2359 static void
2360f_col(typval_T *argvars, typval_T *rettv)
2361{
2362 colnr_T col = 0;
2363 pos_T *fp;
2364 int fnum = curbuf->b_fnum;
2365
2366 fp = var2fpos(&argvars[0], FALSE, &fnum);
2367 if (fp != NULL && fnum == curbuf->b_fnum)
2368 {
2369 if (fp->col == MAXCOL)
2370 {
2371 /* '> can be MAXCOL, get the length of the line then */
2372 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2373 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2374 else
2375 col = MAXCOL;
2376 }
2377 else
2378 {
2379 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 /* col(".") when the cursor is on the NUL at the end of the line
2381 * because of "coladd" can be seen as an extra column. */
2382 if (virtual_active() && fp == &curwin->w_cursor)
2383 {
2384 char_u *p = ml_get_cursor();
2385
2386 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2387 curwin->w_virtcol - curwin->w_cursor.coladd))
2388 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002389 int l;
2390
2391 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2392 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 }
2394 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 }
2396 }
2397 rettv->vval.v_number = col;
2398}
2399
2400#if defined(FEAT_INS_EXPAND)
2401/*
2402 * "complete()" function
2403 */
2404 static void
2405f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2406{
2407 int startcol;
2408
2409 if ((State & INSERT) == 0)
2410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002411 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 return;
2413 }
2414
2415 /* Check for undo allowed here, because if something was already inserted
2416 * the line was already saved for undo and this check isn't done. */
2417 if (!undo_allowed())
2418 return;
2419
2420 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002422 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423 return;
2424 }
2425
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002426 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 if (startcol <= 0)
2428 return;
2429
2430 set_completion(startcol - 1, argvars[1].vval.v_list);
2431}
2432
2433/*
2434 * "complete_add()" function
2435 */
2436 static void
2437f_complete_add(typval_T *argvars, typval_T *rettv)
2438{
2439 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2440}
2441
2442/*
2443 * "complete_check()" function
2444 */
2445 static void
2446f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2447{
2448 int saved = RedrawingDisabled;
2449
2450 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002451 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002452 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002453 RedrawingDisabled = saved;
2454}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002455
2456/*
2457 * "complete_info()" function
2458 */
2459 static void
2460f_complete_info(typval_T *argvars, typval_T *rettv)
2461{
2462 list_T *what_list = NULL;
2463
2464 if (rettv_dict_alloc(rettv) != OK)
2465 return;
2466
2467 if (argvars[0].v_type != VAR_UNKNOWN)
2468 {
2469 if (argvars[0].v_type != VAR_LIST)
2470 {
2471 emsg(_(e_listreq));
2472 return;
2473 }
2474 what_list = argvars[0].vval.v_list;
2475 }
2476 get_complete_info(what_list, rettv->vval.v_dict);
2477}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478#endif
2479
2480/*
2481 * "confirm(message, buttons[, default [, type]])" function
2482 */
2483 static void
2484f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2485{
2486#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2487 char_u *message;
2488 char_u *buttons = NULL;
2489 char_u buf[NUMBUFLEN];
2490 char_u buf2[NUMBUFLEN];
2491 int def = 1;
2492 int type = VIM_GENERIC;
2493 char_u *typestr;
2494 int error = FALSE;
2495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002496 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497 if (message == NULL)
2498 error = TRUE;
2499 if (argvars[1].v_type != VAR_UNKNOWN)
2500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002501 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502 if (buttons == NULL)
2503 error = TRUE;
2504 if (argvars[2].v_type != VAR_UNKNOWN)
2505 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002506 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002507 if (argvars[3].v_type != VAR_UNKNOWN)
2508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002509 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002510 if (typestr == NULL)
2511 error = TRUE;
2512 else
2513 {
2514 switch (TOUPPER_ASC(*typestr))
2515 {
2516 case 'E': type = VIM_ERROR; break;
2517 case 'Q': type = VIM_QUESTION; break;
2518 case 'I': type = VIM_INFO; break;
2519 case 'W': type = VIM_WARNING; break;
2520 case 'G': type = VIM_GENERIC; break;
2521 }
2522 }
2523 }
2524 }
2525 }
2526
2527 if (buttons == NULL || *buttons == NUL)
2528 buttons = (char_u *)_("&Ok");
2529
2530 if (!error)
2531 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2532 def, NULL, FALSE);
2533#endif
2534}
2535
2536/*
2537 * "copy()" function
2538 */
2539 static void
2540f_copy(typval_T *argvars, typval_T *rettv)
2541{
2542 item_copy(&argvars[0], rettv, FALSE, 0);
2543}
2544
2545#ifdef FEAT_FLOAT
2546/*
2547 * "cos()" function
2548 */
2549 static void
2550f_cos(typval_T *argvars, typval_T *rettv)
2551{
2552 float_T f = 0.0;
2553
2554 rettv->v_type = VAR_FLOAT;
2555 if (get_float_arg(argvars, &f) == OK)
2556 rettv->vval.v_float = cos(f);
2557 else
2558 rettv->vval.v_float = 0.0;
2559}
2560
2561/*
2562 * "cosh()" function
2563 */
2564 static void
2565f_cosh(typval_T *argvars, typval_T *rettv)
2566{
2567 float_T f = 0.0;
2568
2569 rettv->v_type = VAR_FLOAT;
2570 if (get_float_arg(argvars, &f) == OK)
2571 rettv->vval.v_float = cosh(f);
2572 else
2573 rettv->vval.v_float = 0.0;
2574}
2575#endif
2576
2577/*
2578 * "count()" function
2579 */
2580 static void
2581f_count(typval_T *argvars, typval_T *rettv)
2582{
2583 long n = 0;
2584 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002585 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586
Bram Moolenaar9966b212017-07-28 16:46:57 +02002587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002589
2590 if (argvars[0].v_type == VAR_STRING)
2591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002592 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002593 char_u *p = argvars[0].vval.v_string;
2594 char_u *next;
2595
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002596 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002597 {
2598 if (ic)
2599 {
2600 size_t len = STRLEN(expr);
2601
2602 while (*p != NUL)
2603 {
2604 if (MB_STRNICMP(p, expr, len) == 0)
2605 {
2606 ++n;
2607 p += len;
2608 }
2609 else
2610 MB_PTR_ADV(p);
2611 }
2612 }
2613 else
2614 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2615 != NULL)
2616 {
2617 ++n;
2618 p = next + STRLEN(expr);
2619 }
2620 }
2621
2622 }
2623 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 {
2625 listitem_T *li;
2626 list_T *l;
2627 long idx;
2628
2629 if ((l = argvars[0].vval.v_list) != NULL)
2630 {
2631 li = l->lv_first;
2632 if (argvars[2].v_type != VAR_UNKNOWN)
2633 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 if (argvars[3].v_type != VAR_UNKNOWN)
2635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002636 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637 if (!error)
2638 {
2639 li = list_find(l, idx);
2640 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002641 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642 }
2643 }
2644 if (error)
2645 li = NULL;
2646 }
2647
2648 for ( ; li != NULL; li = li->li_next)
2649 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2650 ++n;
2651 }
2652 }
2653 else if (argvars[0].v_type == VAR_DICT)
2654 {
2655 int todo;
2656 dict_T *d;
2657 hashitem_T *hi;
2658
2659 if ((d = argvars[0].vval.v_dict) != NULL)
2660 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (argvars[2].v_type != VAR_UNKNOWN)
2662 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002664 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665 }
2666
2667 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2668 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2669 {
2670 if (!HASHITEM_EMPTY(hi))
2671 {
2672 --todo;
2673 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2674 ++n;
2675 }
2676 }
2677 }
2678 }
2679 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 rettv->vval.v_number = n;
2682}
2683
2684/*
2685 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2686 *
2687 * Checks the existence of a cscope connection.
2688 */
2689 static void
2690f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2691{
2692#ifdef FEAT_CSCOPE
2693 int num = 0;
2694 char_u *dbpath = NULL;
2695 char_u *prepend = NULL;
2696 char_u buf[NUMBUFLEN];
2697
2698 if (argvars[0].v_type != VAR_UNKNOWN
2699 && argvars[1].v_type != VAR_UNKNOWN)
2700 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002701 num = (int)tv_get_number(&argvars[0]);
2702 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002704 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 }
2706
2707 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2708#endif
2709}
2710
2711/*
2712 * "cursor(lnum, col)" function, or
2713 * "cursor(list)"
2714 *
2715 * Moves the cursor to the specified line and column.
2716 * Returns 0 when the position could be set, -1 otherwise.
2717 */
2718 static void
2719f_cursor(typval_T *argvars, typval_T *rettv)
2720{
2721 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002722 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002723 int set_curswant = TRUE;
2724
2725 rettv->vval.v_number = -1;
2726 if (argvars[1].v_type == VAR_UNKNOWN)
2727 {
2728 pos_T pos;
2729 colnr_T curswant = -1;
2730
2731 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002733 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002734 return;
2735 }
2736 line = pos.lnum;
2737 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002738 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002739 if (curswant >= 0)
2740 {
2741 curwin->w_curswant = curswant - 1;
2742 set_curswant = FALSE;
2743 }
2744 }
2745 else
2746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002747 line = tv_get_lnum(argvars);
2748 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002750 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002752 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 return; /* type error; errmsg already given */
2754 if (line > 0)
2755 curwin->w_cursor.lnum = line;
2756 if (col > 0)
2757 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759
2760 /* Make sure the cursor is in a valid position. */
2761 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002762 /* Correct cursor for multi-byte character. */
2763 if (has_mbyte)
2764 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765
2766 curwin->w_set_curswant = set_curswant;
2767 rettv->vval.v_number = 0;
2768}
2769
Bram Moolenaar4f974752019-02-17 17:44:42 +01002770#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002771/*
2772 * "debugbreak()" function
2773 */
2774 static void
2775f_debugbreak(typval_T *argvars, typval_T *rettv)
2776{
2777 int pid;
2778
2779 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002780 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002781 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002782 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002783 else
2784 {
2785 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2786
2787 if (hProcess != NULL)
2788 {
2789 DebugBreakProcess(hProcess);
2790 CloseHandle(hProcess);
2791 rettv->vval.v_number = OK;
2792 }
2793 }
2794}
2795#endif
2796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797/*
2798 * "deepcopy()" function
2799 */
2800 static void
2801f_deepcopy(typval_T *argvars, typval_T *rettv)
2802{
2803 int noref = 0;
2804 int copyID;
2805
2806 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002807 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002808 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002809 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002810 else
2811 {
2812 copyID = get_copyID();
2813 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2814 }
2815}
2816
2817/*
2818 * "delete()" function
2819 */
2820 static void
2821f_delete(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u nbuf[NUMBUFLEN];
2824 char_u *name;
2825 char_u *flags;
2826
2827 rettv->vval.v_number = -1;
2828 if (check_restricted() || check_secure())
2829 return;
2830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002831 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002832 if (name == NULL || *name == NUL)
2833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002835 return;
2836 }
2837
2838 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002839 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 else
2841 flags = (char_u *)"";
2842
2843 if (*flags == NUL)
2844 /* delete a file */
2845 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2846 else if (STRCMP(flags, "d") == 0)
2847 /* delete an empty directory */
2848 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2849 else if (STRCMP(flags, "rf") == 0)
2850 /* delete a directory recursively */
2851 rettv->vval.v_number = delete_recursive(name);
2852 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002853 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854}
2855
2856/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002857 * "deletebufline()" function
2858 */
2859 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002860f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002861{
2862 buf_T *buf;
2863 linenr_T first, last;
2864 linenr_T lnum;
2865 long count;
2866 int is_curbuf;
2867 buf_T *curbuf_save = NULL;
2868 win_T *curwin_save = NULL;
2869 tabpage_T *tp;
2870 win_T *wp;
2871
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002872 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002873 if (buf == NULL)
2874 {
2875 rettv->vval.v_number = 1; /* FAIL */
2876 return;
2877 }
2878 is_curbuf = buf == curbuf;
2879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002880 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002881 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002882 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002883 else
2884 last = first;
2885
2886 if (buf->b_ml.ml_mfp == NULL || first < 1
2887 || first > buf->b_ml.ml_line_count || last < first)
2888 {
2889 rettv->vval.v_number = 1; /* FAIL */
2890 return;
2891 }
2892
2893 if (!is_curbuf)
2894 {
2895 curbuf_save = curbuf;
2896 curwin_save = curwin;
2897 curbuf = buf;
2898 find_win_for_curbuf();
2899 }
2900 if (last > curbuf->b_ml.ml_line_count)
2901 last = curbuf->b_ml.ml_line_count;
2902 count = last - first + 1;
2903
2904 // When coming here from Insert mode, sync undo, so that this can be
2905 // undone separately from what was previously inserted.
2906 if (u_sync_once == 2)
2907 {
2908 u_sync_once = 1; // notify that u_sync() was called
2909 u_sync(TRUE);
2910 }
2911
2912 if (u_save(first - 1, last + 1) == FAIL)
2913 {
2914 rettv->vval.v_number = 1; /* FAIL */
2915 return;
2916 }
2917
2918 for (lnum = first; lnum <= last; ++lnum)
2919 ml_delete(first, TRUE);
2920
2921 FOR_ALL_TAB_WINDOWS(tp, wp)
2922 if (wp->w_buffer == buf)
2923 {
2924 if (wp->w_cursor.lnum > last)
2925 wp->w_cursor.lnum -= count;
2926 else if (wp->w_cursor.lnum> first)
2927 wp->w_cursor.lnum = first;
2928 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2929 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2930 }
2931 check_cursor_col();
2932 deleted_lines_mark(first, count);
2933
2934 if (!is_curbuf)
2935 {
2936 curbuf = curbuf_save;
2937 curwin = curwin_save;
2938 }
2939}
2940
2941/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002942 * "did_filetype()" function
2943 */
2944 static void
2945f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2946{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002948}
2949
2950/*
2951 * "diff_filler()" function
2952 */
2953 static void
2954f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2955{
2956#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002957 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958#endif
2959}
2960
2961/*
2962 * "diff_hlID()" function
2963 */
2964 static void
2965f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2966{
2967#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002968 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002970 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 static int fnum = 0;
2972 static int change_start = 0;
2973 static int change_end = 0;
2974 static hlf_T hlID = (hlf_T)0;
2975 int filler_lines;
2976 int col;
2977
2978 if (lnum < 0) /* ignore type error in {lnum} arg */
2979 lnum = 0;
2980 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002981 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 || fnum != curbuf->b_fnum)
2983 {
2984 /* New line, buffer, change: need to get the values. */
2985 filler_lines = diff_check(curwin, lnum);
2986 if (filler_lines < 0)
2987 {
2988 if (filler_lines == -1)
2989 {
2990 change_start = MAXCOL;
2991 change_end = -1;
2992 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2993 hlID = HLF_ADD; /* added line */
2994 else
2995 hlID = HLF_CHD; /* changed line */
2996 }
2997 else
2998 hlID = HLF_ADD; /* added line */
2999 }
3000 else
3001 hlID = (hlf_T)0;
3002 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003003 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003004 fnum = curbuf->b_fnum;
3005 }
3006
3007 if (hlID == HLF_CHD || hlID == HLF_TXD)
3008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003009 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 if (col >= change_start && col <= change_end)
3011 hlID = HLF_TXD; /* changed text */
3012 else
3013 hlID = HLF_CHD; /* changed line */
3014 }
3015 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3016#endif
3017}
3018
3019/*
3020 * "empty({expr})" function
3021 */
3022 static void
3023f_empty(typval_T *argvars, typval_T *rettv)
3024{
3025 int n = FALSE;
3026
3027 switch (argvars[0].v_type)
3028 {
3029 case VAR_STRING:
3030 case VAR_FUNC:
3031 n = argvars[0].vval.v_string == NULL
3032 || *argvars[0].vval.v_string == NUL;
3033 break;
3034 case VAR_PARTIAL:
3035 n = FALSE;
3036 break;
3037 case VAR_NUMBER:
3038 n = argvars[0].vval.v_number == 0;
3039 break;
3040 case VAR_FLOAT:
3041#ifdef FEAT_FLOAT
3042 n = argvars[0].vval.v_float == 0.0;
3043 break;
3044#endif
3045 case VAR_LIST:
3046 n = argvars[0].vval.v_list == NULL
3047 || argvars[0].vval.v_list->lv_first == NULL;
3048 break;
3049 case VAR_DICT:
3050 n = argvars[0].vval.v_dict == NULL
3051 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3052 break;
3053 case VAR_SPECIAL:
3054 n = argvars[0].vval.v_number != VVAL_TRUE;
3055 break;
3056
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003057 case VAR_BLOB:
3058 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003059 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3060 break;
3061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003062 case VAR_JOB:
3063#ifdef FEAT_JOB_CHANNEL
3064 n = argvars[0].vval.v_job == NULL
3065 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3066 break;
3067#endif
3068 case VAR_CHANNEL:
3069#ifdef FEAT_JOB_CHANNEL
3070 n = argvars[0].vval.v_channel == NULL
3071 || !channel_is_open(argvars[0].vval.v_channel);
3072 break;
3073#endif
3074 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003075 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 n = TRUE;
3077 break;
3078 }
3079
3080 rettv->vval.v_number = n;
3081}
3082
3083/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003084 * "environ()" function
3085 */
3086 static void
3087f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3088{
3089#if !defined(AMIGA)
3090 int i = 0;
3091 char_u *entry, *value;
3092# ifdef MSWIN
3093 extern wchar_t **_wenviron;
3094# else
3095 extern char **environ;
3096# endif
3097
3098 if (rettv_dict_alloc(rettv) != OK)
3099 return;
3100
3101# ifdef MSWIN
3102 if (*_wenviron == NULL)
3103 return;
3104# else
3105 if (*environ == NULL)
3106 return;
3107# endif
3108
3109 for (i = 0; ; ++i)
3110 {
3111# ifdef MSWIN
3112 short_u *p;
3113
3114 if ((p = (short_u *)_wenviron[i]) == NULL)
3115 return;
3116 entry = utf16_to_enc(p, NULL);
3117# else
3118 if ((entry = (char_u *)environ[i]) == NULL)
3119 return;
3120 entry = vim_strsave(entry);
3121# endif
3122 if (entry == NULL) // out of memory
3123 return;
3124 if ((value = vim_strchr(entry, '=')) == NULL)
3125 {
3126 vim_free(entry);
3127 continue;
3128 }
3129 *value++ = NUL;
3130 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3131 vim_free(entry);
3132 }
3133#endif
3134}
3135
3136/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003137 * "escape({string}, {chars})" function
3138 */
3139 static void
3140f_escape(typval_T *argvars, typval_T *rettv)
3141{
3142 char_u buf[NUMBUFLEN];
3143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003144 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3145 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003146 rettv->v_type = VAR_STRING;
3147}
3148
3149/*
3150 * "eval()" function
3151 */
3152 static void
3153f_eval(typval_T *argvars, typval_T *rettv)
3154{
3155 char_u *s, *p;
3156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003157 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003158 if (s != NULL)
3159 s = skipwhite(s);
3160
3161 p = s;
3162 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3163 {
3164 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003165 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 need_clr_eos = FALSE;
3167 rettv->v_type = VAR_NUMBER;
3168 rettv->vval.v_number = 0;
3169 }
3170 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003171 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003172}
3173
3174/*
3175 * "eventhandler()" function
3176 */
3177 static void
3178f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3179{
3180 rettv->vval.v_number = vgetc_busy;
3181}
3182
3183/*
3184 * "executable()" function
3185 */
3186 static void
3187f_executable(typval_T *argvars, typval_T *rettv)
3188{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190
3191 /* Check in $PATH and also check directly if there is a directory name. */
3192 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3193 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3194}
3195
3196static garray_T redir_execute_ga;
3197
3198/*
3199 * Append "value[value_len]" to the execute() output.
3200 */
3201 void
3202execute_redir_str(char_u *value, int value_len)
3203{
3204 int len;
3205
3206 if (value_len == -1)
3207 len = (int)STRLEN(value); /* Append the entire string */
3208 else
3209 len = value_len; /* Append only "value_len" characters */
3210 if (ga_grow(&redir_execute_ga, len) == OK)
3211 {
3212 mch_memmove((char *)redir_execute_ga.ga_data
3213 + redir_execute_ga.ga_len, value, len);
3214 redir_execute_ga.ga_len += len;
3215 }
3216}
3217
3218/*
3219 * Get next line from a list.
3220 * Called by do_cmdline() to get the next line.
3221 * Returns allocated string, or NULL for end of function.
3222 */
3223
3224 static char_u *
3225get_list_line(
3226 int c UNUSED,
3227 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003228 int indent UNUSED,
3229 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230{
3231 listitem_T **p = (listitem_T **)cookie;
3232 listitem_T *item = *p;
3233 char_u buf[NUMBUFLEN];
3234 char_u *s;
3235
3236 if (item == NULL)
3237 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003238 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 *p = item->li_next;
3240 return s == NULL ? NULL : vim_strsave(s);
3241}
3242
3243/*
3244 * "execute()" function
3245 */
3246 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003247execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248{
3249 char_u *cmd = NULL;
3250 list_T *list = NULL;
3251 int save_msg_silent = msg_silent;
3252 int save_emsg_silent = emsg_silent;
3253 int save_emsg_noredir = emsg_noredir;
3254 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003255 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003256 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003257 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003258 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259
3260 rettv->vval.v_string = NULL;
3261 rettv->v_type = VAR_STRING;
3262
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003263 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003265 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 if (list == NULL || list->lv_first == NULL)
3267 /* empty list, no commands, empty output */
3268 return;
3269 ++list->lv_refcount;
3270 }
3271 else
3272 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003273 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003274 if (cmd == NULL)
3275 return;
3276 }
3277
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003278 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 {
3280 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003281 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282
3283 if (s == NULL)
3284 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003285 if (*s == NUL)
3286 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 if (STRNCMP(s, "silent", 6) == 0)
3288 ++msg_silent;
3289 if (STRCMP(s, "silent!") == 0)
3290 {
3291 emsg_silent = TRUE;
3292 emsg_noredir = TRUE;
3293 }
3294 }
3295 else
3296 ++msg_silent;
3297
3298 if (redir_execute)
3299 save_ga = redir_execute_ga;
3300 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3301 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003302 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003303 if (!echo_output)
3304 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305
3306 if (cmd != NULL)
3307 do_cmdline_cmd(cmd);
3308 else
3309 {
3310 listitem_T *item = list->lv_first;
3311
3312 do_cmdline(NULL, get_list_line, (void *)&item,
3313 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3314 --list->lv_refcount;
3315 }
3316
Bram Moolenaard297f352017-01-29 20:31:21 +01003317 /* Need to append a NUL to the result. */
3318 if (ga_grow(&redir_execute_ga, 1) == OK)
3319 {
3320 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3321 rettv->vval.v_string = redir_execute_ga.ga_data;
3322 }
3323 else
3324 {
3325 ga_clear(&redir_execute_ga);
3326 rettv->vval.v_string = NULL;
3327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328 msg_silent = save_msg_silent;
3329 emsg_silent = save_emsg_silent;
3330 emsg_noredir = save_emsg_noredir;
3331
3332 redir_execute = save_redir_execute;
3333 if (redir_execute)
3334 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003335 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003337 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003338 if (echo_output)
3339 // When not working silently: put it in column zero. A following
3340 // "echon" will overwrite the message, unavoidably.
3341 msg_col = 0;
3342 else
3343 // When working silently: Put it back where it was, since nothing
3344 // should have been written.
3345 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346}
3347
3348/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003349 * "execute()" function
3350 */
3351 static void
3352f_execute(typval_T *argvars, typval_T *rettv)
3353{
3354 execute_common(argvars, rettv, 0);
3355}
3356
3357/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003358 * "exepath()" function
3359 */
3360 static void
3361f_exepath(typval_T *argvars, typval_T *rettv)
3362{
3363 char_u *p = NULL;
3364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003365 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 rettv->v_type = VAR_STRING;
3367 rettv->vval.v_string = p;
3368}
3369
3370/*
3371 * "exists()" function
3372 */
3373 static void
3374f_exists(typval_T *argvars, typval_T *rettv)
3375{
3376 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003377 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003379 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 if (*p == '$') /* environment variable */
3381 {
3382 /* first try "normal" environment variables (fast) */
3383 if (mch_getenv(p + 1) != NULL)
3384 n = TRUE;
3385 else
3386 {
3387 /* try expanding things like $VIM and ${HOME} */
3388 p = expand_env_save(p);
3389 if (p != NULL && *p != '$')
3390 n = TRUE;
3391 vim_free(p);
3392 }
3393 }
3394 else if (*p == '&' || *p == '+') /* option */
3395 {
3396 n = (get_option_tv(&p, NULL, TRUE) == OK);
3397 if (*skipwhite(p) != NUL)
3398 n = FALSE; /* trailing garbage */
3399 }
3400 else if (*p == '*') /* internal or user defined function */
3401 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003402 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003403 }
3404 else if (*p == ':')
3405 {
3406 n = cmd_exists(p + 1);
3407 }
3408 else if (*p == '#')
3409 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003410 if (p[1] == '#')
3411 n = autocmd_supported(p + 2);
3412 else
3413 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 }
3415 else /* internal variable */
3416 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003417 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003418 }
3419
3420 rettv->vval.v_number = n;
3421}
3422
3423#ifdef FEAT_FLOAT
3424/*
3425 * "exp()" function
3426 */
3427 static void
3428f_exp(typval_T *argvars, typval_T *rettv)
3429{
3430 float_T f = 0.0;
3431
3432 rettv->v_type = VAR_FLOAT;
3433 if (get_float_arg(argvars, &f) == OK)
3434 rettv->vval.v_float = exp(f);
3435 else
3436 rettv->vval.v_float = 0.0;
3437}
3438#endif
3439
3440/*
3441 * "expand()" function
3442 */
3443 static void
3444f_expand(typval_T *argvars, typval_T *rettv)
3445{
3446 char_u *s;
3447 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003448 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003449 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3450 expand_T xpc;
3451 int error = FALSE;
3452 char_u *result;
3453
3454 rettv->v_type = VAR_STRING;
3455 if (argvars[1].v_type != VAR_UNKNOWN
3456 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003457 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003459 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003461 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462 if (*s == '%' || *s == '#' || *s == '<')
3463 {
3464 ++emsg_off;
3465 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3466 --emsg_off;
3467 if (rettv->v_type == VAR_LIST)
3468 {
3469 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3470 list_append_string(rettv->vval.v_list, result, -1);
3471 else
3472 vim_free(result);
3473 }
3474 else
3475 rettv->vval.v_string = result;
3476 }
3477 else
3478 {
3479 /* When the optional second argument is non-zero, don't remove matches
3480 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3481 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003482 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483 options |= WILD_KEEP_ALL;
3484 if (!error)
3485 {
3486 ExpandInit(&xpc);
3487 xpc.xp_context = EXPAND_FILES;
3488 if (p_wic)
3489 options += WILD_ICASE;
3490 if (rettv->v_type == VAR_STRING)
3491 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3492 options, WILD_ALL);
3493 else if (rettv_list_alloc(rettv) != FAIL)
3494 {
3495 int i;
3496
3497 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3498 for (i = 0; i < xpc.xp_numfiles; i++)
3499 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3500 ExpandCleanup(&xpc);
3501 }
3502 }
3503 else
3504 rettv->vval.v_string = NULL;
3505 }
3506}
3507
3508/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003509 * "expandcmd()" function
3510 * Expand all the special characters in a command string.
3511 */
3512 static void
3513f_expandcmd(typval_T *argvars, typval_T *rettv)
3514{
3515 exarg_T eap;
3516 char_u *cmdstr;
3517 char *errormsg = NULL;
3518
3519 rettv->v_type = VAR_STRING;
3520 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3521
3522 memset(&eap, 0, sizeof(eap));
3523 eap.cmd = cmdstr;
3524 eap.arg = cmdstr;
3525 eap.argt |= NOSPC;
3526 eap.usefilter = FALSE;
3527 eap.nextcmd = NULL;
3528 eap.cmdidx = CMD_USER;
3529
3530 expand_filename(&eap, &cmdstr, &errormsg);
3531 if (errormsg != NULL && *errormsg != NUL)
3532 emsg(errormsg);
3533
3534 rettv->vval.v_string = cmdstr;
3535}
3536
3537/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538 * "extend(list, list [, idx])" function
3539 * "extend(dict, dict [, action])" function
3540 */
3541 static void
3542f_extend(typval_T *argvars, typval_T *rettv)
3543{
3544 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3545
3546 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3547 {
3548 list_T *l1, *l2;
3549 listitem_T *item;
3550 long before;
3551 int error = FALSE;
3552
3553 l1 = argvars[0].vval.v_list;
3554 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003555 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556 && l2 != NULL)
3557 {
3558 if (argvars[2].v_type != VAR_UNKNOWN)
3559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003560 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (error)
3562 return; /* type error; errmsg already given */
3563
3564 if (before == l1->lv_len)
3565 item = NULL;
3566 else
3567 {
3568 item = list_find(l1, before);
3569 if (item == NULL)
3570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003571 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572 return;
3573 }
3574 }
3575 }
3576 else
3577 item = NULL;
3578 list_extend(l1, l2, item);
3579
3580 copy_tv(&argvars[0], rettv);
3581 }
3582 }
3583 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3584 {
3585 dict_T *d1, *d2;
3586 char_u *action;
3587 int i;
3588
3589 d1 = argvars[0].vval.v_dict;
3590 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003591 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003592 && d2 != NULL)
3593 {
3594 /* Check the third argument. */
3595 if (argvars[2].v_type != VAR_UNKNOWN)
3596 {
3597 static char *(av[]) = {"keep", "force", "error"};
3598
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003599 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600 if (action == NULL)
3601 return; /* type error; errmsg already given */
3602 for (i = 0; i < 3; ++i)
3603 if (STRCMP(action, av[i]) == 0)
3604 break;
3605 if (i == 3)
3606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003607 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003608 return;
3609 }
3610 }
3611 else
3612 action = (char_u *)"force";
3613
3614 dict_extend(d1, d2, action);
3615
3616 copy_tv(&argvars[0], rettv);
3617 }
3618 }
3619 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003620 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621}
3622
3623/*
3624 * "feedkeys()" function
3625 */
3626 static void
3627f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3628{
3629 int remap = TRUE;
3630 int insert = FALSE;
3631 char_u *keys, *flags;
3632 char_u nbuf[NUMBUFLEN];
3633 int typed = FALSE;
3634 int execute = FALSE;
3635 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003636 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637 char_u *keys_esc;
3638
3639 /* This is not allowed in the sandbox. If the commands would still be
3640 * executed in the sandbox it would be OK, but it probably happens later,
3641 * when "sandbox" is no longer set. */
3642 if (check_secure())
3643 return;
3644
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003645 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646
3647 if (argvars[1].v_type != VAR_UNKNOWN)
3648 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003649 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 for ( ; *flags != NUL; ++flags)
3651 {
3652 switch (*flags)
3653 {
3654 case 'n': remap = FALSE; break;
3655 case 'm': remap = TRUE; break;
3656 case 't': typed = TRUE; break;
3657 case 'i': insert = TRUE; break;
3658 case 'x': execute = TRUE; break;
3659 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003660 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 }
3662 }
3663 }
3664
3665 if (*keys != NUL || execute)
3666 {
3667 /* Need to escape K_SPECIAL and CSI before putting the string in the
3668 * typeahead buffer. */
3669 keys_esc = vim_strsave_escape_csi(keys);
3670 if (keys_esc != NULL)
3671 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003672 if (lowlevel)
3673 {
3674#ifdef USE_INPUT_BUF
3675 add_to_input_buf(keys, (int)STRLEN(keys));
3676#else
3677 emsg(_("E980: lowlevel input not supported"));
3678#endif
3679 }
3680 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003681 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003682 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003684 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003685#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003686 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003687#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003688 )
3689 typebuf_was_filled = TRUE;
3690 }
3691 vim_free(keys_esc);
3692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 if (execute)
3694 {
3695 int save_msg_scroll = msg_scroll;
3696
3697 /* Avoid a 1 second delay when the keys start Insert mode. */
3698 msg_scroll = FALSE;
3699
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003700 if (!dangerous)
3701 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003702 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003703 if (!dangerous)
3704 --ex_normal_busy;
3705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 msg_scroll |= save_msg_scroll;
3707 }
3708 }
3709 }
3710}
3711
3712/*
3713 * "filereadable()" function
3714 */
3715 static void
3716f_filereadable(typval_T *argvars, typval_T *rettv)
3717{
3718 int fd;
3719 char_u *p;
3720 int n;
3721
3722#ifndef O_NONBLOCK
3723# define O_NONBLOCK 0
3724#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003725 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3727 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3728 {
3729 n = TRUE;
3730 close(fd);
3731 }
3732 else
3733 n = FALSE;
3734
3735 rettv->vval.v_number = n;
3736}
3737
3738/*
3739 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3740 * rights to write into.
3741 */
3742 static void
3743f_filewritable(typval_T *argvars, typval_T *rettv)
3744{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003745 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746}
3747
3748 static void
3749findfilendir(
3750 typval_T *argvars UNUSED,
3751 typval_T *rettv,
3752 int find_what UNUSED)
3753{
3754#ifdef FEAT_SEARCHPATH
3755 char_u *fname;
3756 char_u *fresult = NULL;
3757 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3758 char_u *p;
3759 char_u pathbuf[NUMBUFLEN];
3760 int count = 1;
3761 int first = TRUE;
3762 int error = FALSE;
3763#endif
3764
3765 rettv->vval.v_string = NULL;
3766 rettv->v_type = VAR_STRING;
3767
3768#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003769 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770
3771 if (argvars[1].v_type != VAR_UNKNOWN)
3772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003773 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 if (p == NULL)
3775 error = TRUE;
3776 else
3777 {
3778 if (*p != NUL)
3779 path = p;
3780
3781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003782 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003783 }
3784 }
3785
3786 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3787 error = TRUE;
3788
3789 if (*fname != NUL && !error)
3790 {
3791 do
3792 {
3793 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3794 vim_free(fresult);
3795 fresult = find_file_in_path_option(first ? fname : NULL,
3796 first ? (int)STRLEN(fname) : 0,
3797 0, first, path,
3798 find_what,
3799 curbuf->b_ffname,
3800 find_what == FINDFILE_DIR
3801 ? (char_u *)"" : curbuf->b_p_sua);
3802 first = FALSE;
3803
3804 if (fresult != NULL && rettv->v_type == VAR_LIST)
3805 list_append_string(rettv->vval.v_list, fresult, -1);
3806
3807 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3808 }
3809
3810 if (rettv->v_type == VAR_STRING)
3811 rettv->vval.v_string = fresult;
3812#endif
3813}
3814
3815/*
3816 * "filter()" function
3817 */
3818 static void
3819f_filter(typval_T *argvars, typval_T *rettv)
3820{
3821 filter_map(argvars, rettv, FALSE);
3822}
3823
3824/*
3825 * "finddir({fname}[, {path}[, {count}]])" function
3826 */
3827 static void
3828f_finddir(typval_T *argvars, typval_T *rettv)
3829{
3830 findfilendir(argvars, rettv, FINDFILE_DIR);
3831}
3832
3833/*
3834 * "findfile({fname}[, {path}[, {count}]])" function
3835 */
3836 static void
3837f_findfile(typval_T *argvars, typval_T *rettv)
3838{
3839 findfilendir(argvars, rettv, FINDFILE_FILE);
3840}
3841
3842#ifdef FEAT_FLOAT
3843/*
3844 * "float2nr({float})" function
3845 */
3846 static void
3847f_float2nr(typval_T *argvars, typval_T *rettv)
3848{
3849 float_T f = 0.0;
3850
3851 if (get_float_arg(argvars, &f) == OK)
3852 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003853 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003854 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003855 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003856 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003857 else
3858 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859 }
3860}
3861
3862/*
3863 * "floor({float})" function
3864 */
3865 static void
3866f_floor(typval_T *argvars, typval_T *rettv)
3867{
3868 float_T f = 0.0;
3869
3870 rettv->v_type = VAR_FLOAT;
3871 if (get_float_arg(argvars, &f) == OK)
3872 rettv->vval.v_float = floor(f);
3873 else
3874 rettv->vval.v_float = 0.0;
3875}
3876
3877/*
3878 * "fmod()" function
3879 */
3880 static void
3881f_fmod(typval_T *argvars, typval_T *rettv)
3882{
3883 float_T fx = 0.0, fy = 0.0;
3884
3885 rettv->v_type = VAR_FLOAT;
3886 if (get_float_arg(argvars, &fx) == OK
3887 && get_float_arg(&argvars[1], &fy) == OK)
3888 rettv->vval.v_float = fmod(fx, fy);
3889 else
3890 rettv->vval.v_float = 0.0;
3891}
3892#endif
3893
3894/*
3895 * "fnameescape({string})" function
3896 */
3897 static void
3898f_fnameescape(typval_T *argvars, typval_T *rettv)
3899{
3900 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003901 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 rettv->v_type = VAR_STRING;
3903}
3904
3905/*
3906 * "fnamemodify({fname}, {mods})" function
3907 */
3908 static void
3909f_fnamemodify(typval_T *argvars, typval_T *rettv)
3910{
3911 char_u *fname;
3912 char_u *mods;
3913 int usedlen = 0;
3914 int len;
3915 char_u *fbuf = NULL;
3916 char_u buf[NUMBUFLEN];
3917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003918 fname = tv_get_string_chk(&argvars[0]);
3919 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 if (fname == NULL || mods == NULL)
3921 fname = NULL;
3922 else
3923 {
3924 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003925 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 }
3927
3928 rettv->v_type = VAR_STRING;
3929 if (fname == NULL)
3930 rettv->vval.v_string = NULL;
3931 else
3932 rettv->vval.v_string = vim_strnsave(fname, len);
3933 vim_free(fbuf);
3934}
3935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936/*
3937 * "foldclosed()" function
3938 */
3939 static void
3940foldclosed_both(
3941 typval_T *argvars UNUSED,
3942 typval_T *rettv,
3943 int end UNUSED)
3944{
3945#ifdef FEAT_FOLDING
3946 linenr_T lnum;
3947 linenr_T first, last;
3948
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003949 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3951 {
3952 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3953 {
3954 if (end)
3955 rettv->vval.v_number = (varnumber_T)last;
3956 else
3957 rettv->vval.v_number = (varnumber_T)first;
3958 return;
3959 }
3960 }
3961#endif
3962 rettv->vval.v_number = -1;
3963}
3964
3965/*
3966 * "foldclosed()" function
3967 */
3968 static void
3969f_foldclosed(typval_T *argvars, typval_T *rettv)
3970{
3971 foldclosed_both(argvars, rettv, FALSE);
3972}
3973
3974/*
3975 * "foldclosedend()" function
3976 */
3977 static void
3978f_foldclosedend(typval_T *argvars, typval_T *rettv)
3979{
3980 foldclosed_both(argvars, rettv, TRUE);
3981}
3982
3983/*
3984 * "foldlevel()" function
3985 */
3986 static void
3987f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3988{
3989#ifdef FEAT_FOLDING
3990 linenr_T lnum;
3991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003992 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3994 rettv->vval.v_number = foldLevel(lnum);
3995#endif
3996}
3997
3998/*
3999 * "foldtext()" function
4000 */
4001 static void
4002f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4003{
4004#ifdef FEAT_FOLDING
4005 linenr_T foldstart;
4006 linenr_T foldend;
4007 char_u *dashes;
4008 linenr_T lnum;
4009 char_u *s;
4010 char_u *r;
4011 int len;
4012 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004013 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014#endif
4015
4016 rettv->v_type = VAR_STRING;
4017 rettv->vval.v_string = NULL;
4018#ifdef FEAT_FOLDING
4019 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4020 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4021 dashes = get_vim_var_str(VV_FOLDDASHES);
4022 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4023 && dashes != NULL)
4024 {
4025 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004026 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004027 if (!linewhite(lnum))
4028 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004029
4030 /* Find interesting text in this line. */
4031 s = skipwhite(ml_get(lnum));
4032 /* skip C comment-start */
4033 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4034 {
4035 s = skipwhite(s + 2);
4036 if (*skipwhite(s) == NUL
4037 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4038 {
4039 s = skipwhite(ml_get(lnum + 1));
4040 if (*s == '*')
4041 s = skipwhite(s + 1);
4042 }
4043 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004044 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004045 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004046 r = alloc(STRLEN(txt)
4047 + STRLEN(dashes) // for %s
4048 + 20 // for %3ld
4049 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004050 if (r != NULL)
4051 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004052 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004053 len = (int)STRLEN(r);
4054 STRCAT(r, s);
4055 /* remove 'foldmarker' and 'commentstring' */
4056 foldtext_cleanup(r + len);
4057 rettv->vval.v_string = r;
4058 }
4059 }
4060#endif
4061}
4062
4063/*
4064 * "foldtextresult(lnum)" function
4065 */
4066 static void
4067f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4068{
4069#ifdef FEAT_FOLDING
4070 linenr_T lnum;
4071 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004072 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 foldinfo_T foldinfo;
4074 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004075 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004076#endif
4077
4078 rettv->v_type = VAR_STRING;
4079 rettv->vval.v_string = NULL;
4080#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004081 if (entered)
4082 return; /* reject recursive use */
4083 entered = TRUE;
4084
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004085 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 /* treat illegal types and illegal string values for {lnum} the same */
4087 if (lnum < 0)
4088 lnum = 0;
4089 fold_count = foldedCount(curwin, lnum, &foldinfo);
4090 if (fold_count > 0)
4091 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004092 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4093 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094 if (text == buf)
4095 text = vim_strsave(text);
4096 rettv->vval.v_string = text;
4097 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004098
4099 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100#endif
4101}
4102
4103/*
4104 * "foreground()" function
4105 */
4106 static void
4107f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4108{
4109#ifdef FEAT_GUI
4110 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004111 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004112 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004113 return;
4114 }
4115#endif
4116#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118#endif
4119}
4120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004122common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004123{
4124 char_u *s;
4125 char_u *name;
4126 int use_string = FALSE;
4127 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004128 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004129
4130 if (argvars[0].v_type == VAR_FUNC)
4131 {
4132 /* function(MyFunc, [arg], dict) */
4133 s = argvars[0].vval.v_string;
4134 }
4135 else if (argvars[0].v_type == VAR_PARTIAL
4136 && argvars[0].vval.v_partial != NULL)
4137 {
4138 /* function(dict.MyFunc, [arg]) */
4139 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141 }
4142 else
4143 {
4144 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004145 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 use_string = TRUE;
4147 }
4148
Bram Moolenaar843b8842016-08-21 14:36:15 +02004149 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 {
4151 name = s;
4152 trans_name = trans_function_name(&name, FALSE,
4153 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4154 if (*name != NUL)
4155 s = NULL;
4156 }
4157
Bram Moolenaar843b8842016-08-21 14:36:15 +02004158 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4159 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004161 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004162 else if (trans_name != NULL && (is_funcref
4163 ? find_func(trans_name) == NULL
4164 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004165 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 else
4167 {
4168 int dict_idx = 0;
4169 int arg_idx = 0;
4170 list_T *list = NULL;
4171
4172 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4173 {
4174 char sid_buf[25];
4175 int off = *s == 's' ? 2 : 5;
4176
4177 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4178 * also be called from another script. Using trans_function_name()
4179 * would also work, but some plugins depend on the name being
4180 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004181 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004182 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 if (name != NULL)
4184 {
4185 STRCPY(name, sid_buf);
4186 STRCAT(name, s + off);
4187 }
4188 }
4189 else
4190 name = vim_strsave(s);
4191
4192 if (argvars[1].v_type != VAR_UNKNOWN)
4193 {
4194 if (argvars[2].v_type != VAR_UNKNOWN)
4195 {
4196 /* function(name, [args], dict) */
4197 arg_idx = 1;
4198 dict_idx = 2;
4199 }
4200 else if (argvars[1].v_type == VAR_DICT)
4201 /* function(name, dict) */
4202 dict_idx = 1;
4203 else
4204 /* function(name, [args]) */
4205 arg_idx = 1;
4206 if (dict_idx > 0)
4207 {
4208 if (argvars[dict_idx].v_type != VAR_DICT)
4209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004210 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004211 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004212 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213 }
4214 if (argvars[dict_idx].vval.v_dict == NULL)
4215 dict_idx = 0;
4216 }
4217 if (arg_idx > 0)
4218 {
4219 if (argvars[arg_idx].v_type != VAR_LIST)
4220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004223 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004224 }
4225 list = argvars[arg_idx].vval.v_list;
4226 if (list == NULL || list->lv_len == 0)
4227 arg_idx = 0;
4228 }
4229 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004230 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004231 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004232 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004233
4234 /* result is a VAR_PARTIAL */
4235 if (pt == NULL)
4236 vim_free(name);
4237 else
4238 {
4239 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4240 {
4241 listitem_T *li;
4242 int i = 0;
4243 int arg_len = 0;
4244 int lv_len = 0;
4245
4246 if (arg_pt != NULL)
4247 arg_len = arg_pt->pt_argc;
4248 if (list != NULL)
4249 lv_len = list->lv_len;
4250 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004251 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 if (pt->pt_argv == NULL)
4253 {
4254 vim_free(pt);
4255 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004256 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004257 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004258 for (i = 0; i < arg_len; i++)
4259 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4260 if (lv_len > 0)
4261 for (li = list->lv_first; li != NULL;
4262 li = li->li_next)
4263 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 }
4265
4266 /* For "function(dict.func, [], dict)" and "func" is a partial
4267 * use "dict". That is backwards compatible. */
4268 if (dict_idx > 0)
4269 {
4270 /* The dict is bound explicitly, pt_auto is FALSE. */
4271 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4272 ++pt->pt_dict->dv_refcount;
4273 }
4274 else if (arg_pt != NULL)
4275 {
4276 /* If the dict was bound automatically the result is also
4277 * bound automatically. */
4278 pt->pt_dict = arg_pt->pt_dict;
4279 pt->pt_auto = arg_pt->pt_auto;
4280 if (pt->pt_dict != NULL)
4281 ++pt->pt_dict->dv_refcount;
4282 }
4283
4284 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004285 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4286 {
4287 pt->pt_func = arg_pt->pt_func;
4288 func_ptr_ref(pt->pt_func);
4289 vim_free(name);
4290 }
4291 else if (is_funcref)
4292 {
4293 pt->pt_func = find_func(trans_name);
4294 func_ptr_ref(pt->pt_func);
4295 vim_free(name);
4296 }
4297 else
4298 {
4299 pt->pt_name = name;
4300 func_ref(name);
4301 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 }
4303 rettv->v_type = VAR_PARTIAL;
4304 rettv->vval.v_partial = pt;
4305 }
4306 else
4307 {
4308 /* result is a VAR_FUNC */
4309 rettv->v_type = VAR_FUNC;
4310 rettv->vval.v_string = name;
4311 func_ref(name);
4312 }
4313 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314theend:
4315 vim_free(trans_name);
4316}
4317
4318/*
4319 * "funcref()" function
4320 */
4321 static void
4322f_funcref(typval_T *argvars, typval_T *rettv)
4323{
4324 common_function(argvars, rettv, TRUE);
4325}
4326
4327/*
4328 * "function()" function
4329 */
4330 static void
4331f_function(typval_T *argvars, typval_T *rettv)
4332{
4333 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334}
4335
4336/*
4337 * "garbagecollect()" function
4338 */
4339 static void
4340f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4341{
4342 /* This is postponed until we are back at the toplevel, because we may be
4343 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4344 want_garbage_collect = TRUE;
4345
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004346 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347 garbage_collect_at_exit = TRUE;
4348}
4349
4350/*
4351 * "get()" function
4352 */
4353 static void
4354f_get(typval_T *argvars, typval_T *rettv)
4355{
4356 listitem_T *li;
4357 list_T *l;
4358 dictitem_T *di;
4359 dict_T *d;
4360 typval_T *tv = NULL;
4361
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004362 if (argvars[0].v_type == VAR_BLOB)
4363 {
4364 int error = FALSE;
4365 int idx = tv_get_number_chk(&argvars[1], &error);
4366
4367 if (!error)
4368 {
4369 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004370 if (idx < 0)
4371 idx = blob_len(argvars[0].vval.v_blob) + idx;
4372 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4373 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004374 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004375 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004376 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004377 tv = rettv;
4378 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004379 }
4380 }
4381 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 {
4383 if ((l = argvars[0].vval.v_list) != NULL)
4384 {
4385 int error = FALSE;
4386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004387 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388 if (!error && li != NULL)
4389 tv = &li->li_tv;
4390 }
4391 }
4392 else if (argvars[0].v_type == VAR_DICT)
4393 {
4394 if ((d = argvars[0].vval.v_dict) != NULL)
4395 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004396 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004397 if (di != NULL)
4398 tv = &di->di_tv;
4399 }
4400 }
4401 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4402 {
4403 partial_T *pt;
4404 partial_T fref_pt;
4405
4406 if (argvars[0].v_type == VAR_PARTIAL)
4407 pt = argvars[0].vval.v_partial;
4408 else
4409 {
4410 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4411 fref_pt.pt_name = argvars[0].vval.v_string;
4412 pt = &fref_pt;
4413 }
4414
4415 if (pt != NULL)
4416 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004417 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004418 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419
4420 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4421 {
4422 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004423 n = partial_name(pt);
4424 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 rettv->vval.v_string = NULL;
4426 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004427 {
4428 rettv->vval.v_string = vim_strsave(n);
4429 if (rettv->v_type == VAR_FUNC)
4430 func_ref(rettv->vval.v_string);
4431 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432 }
4433 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004434 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 else if (STRCMP(what, "args") == 0)
4436 {
4437 rettv->v_type = VAR_LIST;
4438 if (rettv_list_alloc(rettv) == OK)
4439 {
4440 int i;
4441
4442 for (i = 0; i < pt->pt_argc; ++i)
4443 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4444 }
4445 }
4446 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004447 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 return;
4449 }
4450 }
4451 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004452 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453
4454 if (tv == NULL)
4455 {
4456 if (argvars[2].v_type != VAR_UNKNOWN)
4457 copy_tv(&argvars[2], rettv);
4458 }
4459 else
4460 copy_tv(tv, rettv);
4461}
4462
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004463/*
4464 * Returns buffer options, variables and other attributes in a dictionary.
4465 */
4466 static dict_T *
4467get_buffer_info(buf_T *buf)
4468{
4469 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004470 tabpage_T *tp;
4471 win_T *wp;
4472 list_T *windows;
4473
4474 dict = dict_alloc();
4475 if (dict == NULL)
4476 return NULL;
4477
Bram Moolenaare0be1672018-07-08 16:50:37 +02004478 dict_add_number(dict, "bufnr", buf->b_fnum);
4479 dict_add_string(dict, "name", buf->b_ffname);
4480 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4481 : buflist_findlnum(buf));
4482 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4483 dict_add_number(dict, "listed", buf->b_p_bl);
4484 dict_add_number(dict, "changed", bufIsChanged(buf));
4485 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4486 dict_add_number(dict, "hidden",
4487 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004488
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004489 /* Get a reference to buffer variables */
4490 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004491
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004492 /* List of windows displaying this buffer */
4493 windows = list_alloc();
4494 if (windows != NULL)
4495 {
4496 FOR_ALL_TAB_WINDOWS(tp, wp)
4497 if (wp->w_buffer == buf)
4498 list_append_number(windows, (varnumber_T)wp->w_id);
4499 dict_add_list(dict, "windows", windows);
4500 }
4501
4502#ifdef FEAT_SIGNS
4503 if (buf->b_signlist != NULL)
4504 {
4505 /* List of signs placed in this buffer */
4506 list_T *signs = list_alloc();
4507 if (signs != NULL)
4508 {
4509 get_buffer_signs(buf, signs);
4510 dict_add_list(dict, "signs", signs);
4511 }
4512 }
4513#endif
4514
4515 return dict;
4516}
4517
4518/*
4519 * "getbufinfo()" function
4520 */
4521 static void
4522f_getbufinfo(typval_T *argvars, typval_T *rettv)
4523{
4524 buf_T *buf = NULL;
4525 buf_T *argbuf = NULL;
4526 dict_T *d;
4527 int filtered = FALSE;
4528 int sel_buflisted = FALSE;
4529 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004530 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004531
4532 if (rettv_list_alloc(rettv) != OK)
4533 return;
4534
4535 /* List of all the buffers or selected buffers */
4536 if (argvars[0].v_type == VAR_DICT)
4537 {
4538 dict_T *sel_d = argvars[0].vval.v_dict;
4539
4540 if (sel_d != NULL)
4541 {
4542 dictitem_T *di;
4543
4544 filtered = TRUE;
4545
4546 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004547 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004548 sel_buflisted = TRUE;
4549
4550 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004551 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004552 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004553
4554 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004555 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004556 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004557 }
4558 }
4559 else if (argvars[0].v_type != VAR_UNKNOWN)
4560 {
4561 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004562 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004563 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004564 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004565 --emsg_off;
4566 if (argbuf == NULL)
4567 return;
4568 }
4569
4570 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004571 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004572 {
4573 if (argbuf != NULL && argbuf != buf)
4574 continue;
4575 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004576 || (sel_buflisted && !buf->b_p_bl)
4577 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004578 continue;
4579
4580 d = get_buffer_info(buf);
4581 if (d != NULL)
4582 list_append_dict(rettv->vval.v_list, d);
4583 if (argbuf != NULL)
4584 return;
4585 }
4586}
4587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588/*
4589 * Get line or list of lines from buffer "buf" into "rettv".
4590 * Return a range (from start to end) of lines in rettv from the specified
4591 * buffer.
4592 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4593 */
4594 static void
4595get_buffer_lines(
4596 buf_T *buf,
4597 linenr_T start,
4598 linenr_T end,
4599 int retlist,
4600 typval_T *rettv)
4601{
4602 char_u *p;
4603
4604 rettv->v_type = VAR_STRING;
4605 rettv->vval.v_string = NULL;
4606 if (retlist && rettv_list_alloc(rettv) == FAIL)
4607 return;
4608
4609 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4610 return;
4611
4612 if (!retlist)
4613 {
4614 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4615 p = ml_get_buf(buf, start, FALSE);
4616 else
4617 p = (char_u *)"";
4618 rettv->vval.v_string = vim_strsave(p);
4619 }
4620 else
4621 {
4622 if (end < start)
4623 return;
4624
4625 if (start < 1)
4626 start = 1;
4627 if (end > buf->b_ml.ml_line_count)
4628 end = buf->b_ml.ml_line_count;
4629 while (start <= end)
4630 if (list_append_string(rettv->vval.v_list,
4631 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4632 break;
4633 }
4634}
4635
4636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004637 * "getbufline()" function
4638 */
4639 static void
4640f_getbufline(typval_T *argvars, typval_T *rettv)
4641{
4642 linenr_T lnum;
4643 linenr_T end;
4644 buf_T *buf;
4645
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004646 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004648 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004649 --emsg_off;
4650
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004651 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004652 if (argvars[2].v_type == VAR_UNKNOWN)
4653 end = lnum;
4654 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004655 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656
4657 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4658}
4659
4660/*
4661 * "getbufvar()" function
4662 */
4663 static void
4664f_getbufvar(typval_T *argvars, typval_T *rettv)
4665{
4666 buf_T *buf;
4667 buf_T *save_curbuf;
4668 char_u *varname;
4669 dictitem_T *v;
4670 int done = FALSE;
4671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004672 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4673 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004675 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676
4677 rettv->v_type = VAR_STRING;
4678 rettv->vval.v_string = NULL;
4679
4680 if (buf != NULL && varname != NULL)
4681 {
4682 /* set curbuf to be our buf, temporarily */
4683 save_curbuf = curbuf;
4684 curbuf = buf;
4685
Bram Moolenaar30567352016-08-27 21:25:44 +02004686 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004687 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004688 if (varname[1] == NUL)
4689 {
4690 /* get all buffer-local options in a dict */
4691 dict_T *opts = get_winbuf_options(TRUE);
4692
4693 if (opts != NULL)
4694 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004695 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004696 done = TRUE;
4697 }
4698 }
4699 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4700 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701 done = TRUE;
4702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004703 else
4704 {
4705 /* Look up the variable. */
4706 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4707 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4708 'b', varname, FALSE);
4709 if (v != NULL)
4710 {
4711 copy_tv(&v->di_tv, rettv);
4712 done = TRUE;
4713 }
4714 }
4715
4716 /* restore previous notion of curbuf */
4717 curbuf = save_curbuf;
4718 }
4719
4720 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4721 /* use the default value */
4722 copy_tv(&argvars[2], rettv);
4723
4724 --emsg_off;
4725}
4726
4727/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004728 * "getchangelist()" function
4729 */
4730 static void
4731f_getchangelist(typval_T *argvars, typval_T *rettv)
4732{
4733#ifdef FEAT_JUMPLIST
4734 buf_T *buf;
4735 int i;
4736 list_T *l;
4737 dict_T *d;
4738#endif
4739
4740 if (rettv_list_alloc(rettv) != OK)
4741 return;
4742
4743#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004744 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004745 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004746 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004747 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004748 if (buf == NULL)
4749 return;
4750
4751 l = list_alloc();
4752 if (l == NULL)
4753 return;
4754
4755 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4756 return;
4757 /*
4758 * The current window change list index tracks only the position in the
4759 * current buffer change list. For other buffers, use the change list
4760 * length as the current index.
4761 */
4762 list_append_number(rettv->vval.v_list,
4763 (varnumber_T)((buf == curwin->w_buffer)
4764 ? curwin->w_changelistidx : buf->b_changelistlen));
4765
4766 for (i = 0; i < buf->b_changelistlen; ++i)
4767 {
4768 if (buf->b_changelist[i].lnum == 0)
4769 continue;
4770 if ((d = dict_alloc()) == NULL)
4771 return;
4772 if (list_append_dict(l, d) == FAIL)
4773 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004774 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4775 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004776 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004777 }
4778#endif
4779}
4780/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004781 * "getchar()" function
4782 */
4783 static void
4784f_getchar(typval_T *argvars, typval_T *rettv)
4785{
4786 varnumber_T n;
4787 int error = FALSE;
4788
Bram Moolenaar84d93902018-09-11 20:10:20 +02004789#ifdef MESSAGE_QUEUE
4790 // vpeekc() used to check for messages, but that caused problems, invoking
4791 // a callback where it was not expected. Some plugins use getchar(1) in a
4792 // loop to await a message, therefore make sure we check for messages here.
4793 parse_queued_messages();
4794#endif
4795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004796 /* Position the cursor. Needed after a message that ends in a space. */
4797 windgoto(msg_row, msg_col);
4798
4799 ++no_mapping;
4800 ++allow_keys;
4801 for (;;)
4802 {
4803 if (argvars[0].v_type == VAR_UNKNOWN)
4804 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004805 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004806 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004807 /* getchar(1): only check if char avail */
4808 n = vpeekc_any();
4809 else if (error || vpeekc_any() == NUL)
4810 /* illegal argument or getchar(0) and no char avail: return zero */
4811 n = 0;
4812 else
4813 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004814 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815
4816 if (n == K_IGNORE)
4817 continue;
4818 break;
4819 }
4820 --no_mapping;
4821 --allow_keys;
4822
4823 set_vim_var_nr(VV_MOUSE_WIN, 0);
4824 set_vim_var_nr(VV_MOUSE_WINID, 0);
4825 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4826 set_vim_var_nr(VV_MOUSE_COL, 0);
4827
4828 rettv->vval.v_number = n;
4829 if (IS_SPECIAL(n) || mod_mask != 0)
4830 {
4831 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4832 int i = 0;
4833
4834 /* Turn a special key into three bytes, plus modifier. */
4835 if (mod_mask != 0)
4836 {
4837 temp[i++] = K_SPECIAL;
4838 temp[i++] = KS_MODIFIER;
4839 temp[i++] = mod_mask;
4840 }
4841 if (IS_SPECIAL(n))
4842 {
4843 temp[i++] = K_SPECIAL;
4844 temp[i++] = K_SECOND(n);
4845 temp[i++] = K_THIRD(n);
4846 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004847 else if (has_mbyte)
4848 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004849 else
4850 temp[i++] = n;
4851 temp[i++] = NUL;
4852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = vim_strsave(temp);
4854
4855#ifdef FEAT_MOUSE
4856 if (is_mouse_key(n))
4857 {
4858 int row = mouse_row;
4859 int col = mouse_col;
4860 win_T *win;
4861 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004862 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004863 int winnr = 1;
4864
4865 if (row >= 0 && col >= 0)
4866 {
4867 /* Find the window at the mouse coordinates and compute the
4868 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004869 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004870 if (win == NULL)
4871 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004873# ifdef FEAT_TEXT_PROP
4874 if (bt_popup(win->w_buffer))
4875 winnr = 0;
4876 else
4877# endif
4878 for (wp = firstwin; wp != win && wp != NULL;
4879 wp = wp->w_next)
4880 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004881 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4882 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4883 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4884 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4885 }
4886 }
4887#endif
4888 }
4889}
4890
4891/*
4892 * "getcharmod()" function
4893 */
4894 static void
4895f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4896{
4897 rettv->vval.v_number = mod_mask;
4898}
4899
4900/*
4901 * "getcharsearch()" function
4902 */
4903 static void
4904f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4905{
4906 if (rettv_dict_alloc(rettv) != FAIL)
4907 {
4908 dict_T *dict = rettv->vval.v_dict;
4909
Bram Moolenaare0be1672018-07-08 16:50:37 +02004910 dict_add_string(dict, "char", last_csearch());
4911 dict_add_number(dict, "forward", last_csearch_forward());
4912 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913 }
4914}
4915
4916/*
4917 * "getcmdline()" function
4918 */
4919 static void
4920f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4921{
4922 rettv->v_type = VAR_STRING;
4923 rettv->vval.v_string = get_cmdline_str();
4924}
4925
4926/*
4927 * "getcmdpos()" function
4928 */
4929 static void
4930f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4931{
4932 rettv->vval.v_number = get_cmdline_pos() + 1;
4933}
4934
4935/*
4936 * "getcmdtype()" function
4937 */
4938 static void
4939f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4940{
4941 rettv->v_type = VAR_STRING;
4942 rettv->vval.v_string = alloc(2);
4943 if (rettv->vval.v_string != NULL)
4944 {
4945 rettv->vval.v_string[0] = get_cmdline_type();
4946 rettv->vval.v_string[1] = NUL;
4947 }
4948}
4949
4950/*
4951 * "getcmdwintype()" function
4952 */
4953 static void
4954f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4955{
4956 rettv->v_type = VAR_STRING;
4957 rettv->vval.v_string = NULL;
4958#ifdef FEAT_CMDWIN
4959 rettv->vval.v_string = alloc(2);
4960 if (rettv->vval.v_string != NULL)
4961 {
4962 rettv->vval.v_string[0] = cmdwin_type;
4963 rettv->vval.v_string[1] = NUL;
4964 }
4965#endif
4966}
4967
4968#if defined(FEAT_CMDL_COMPL)
4969/*
4970 * "getcompletion()" function
4971 */
4972 static void
4973f_getcompletion(typval_T *argvars, typval_T *rettv)
4974{
4975 char_u *pat;
4976 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004977 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004978 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4979 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004982 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004984 if (p_wic)
4985 options |= WILD_ICASE;
4986
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004987 /* For filtered results, 'wildignore' is used */
4988 if (!filtered)
4989 options |= WILD_KEEP_ALL;
4990
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004993 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004994 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004995 if (xpc.xp_context == EXPAND_NOTHING)
4996 {
4997 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004998 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005000 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001 return;
5002 }
5003
5004# if defined(FEAT_MENU)
5005 if (xpc.xp_context == EXPAND_MENUS)
5006 {
5007 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5008 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5009 }
5010# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005011#ifdef FEAT_CSCOPE
5012 if (xpc.xp_context == EXPAND_CSCOPE)
5013 {
5014 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5015 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5016 }
5017#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005018#ifdef FEAT_SIGNS
5019 if (xpc.xp_context == EXPAND_SIGN)
5020 {
5021 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5022 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5023 }
5024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025
5026 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5027 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5028 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005029 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005030
5031 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5032
5033 for (i = 0; i < xpc.xp_numfiles; i++)
5034 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5035 }
5036 vim_free(pat);
5037 ExpandCleanup(&xpc);
5038}
5039#endif
5040
5041/*
5042 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005043 *
5044 * Return the current working directory of a window in a tab page.
5045 * First optional argument 'winnr' is the window number or -1 and the second
5046 * optional argument 'tabnr' is the tab page number.
5047 *
5048 * If no arguments are supplied, then return the directory of the current
5049 * window.
5050 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5051 * the specified window.
5052 * If 'winnr' is 0 then return the directory of the current window.
5053 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5054 * directory of the specified tab page. Otherwise return the directory of the
5055 * specified window in the specified tab page.
5056 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 */
5058 static void
5059f_getcwd(typval_T *argvars, typval_T *rettv)
5060{
5061 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005062 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005063 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005064 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 rettv->v_type = VAR_STRING;
5067 rettv->vval.v_string = NULL;
5068
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005069 if (argvars[0].v_type == VAR_NUMBER
5070 && argvars[0].vval.v_number == -1
5071 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005072 global = TRUE;
5073 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005074 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005075
5076 if (wp != NULL && wp->w_localdir != NULL)
5077 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005078 else if (tp != NULL && tp->tp_localdir != NULL)
5079 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5080 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005082 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 rettv->vval.v_string = vim_strsave(globaldir);
5084 else
5085 {
5086 cwd = alloc(MAXPATHL);
5087 if (cwd != NULL)
5088 {
5089 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5090 rettv->vval.v_string = vim_strsave(cwd);
5091 vim_free(cwd);
5092 }
5093 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005094 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005095#ifdef BACKSLASH_IN_FILENAME
5096 if (rettv->vval.v_string != NULL)
5097 slash_adjust(rettv->vval.v_string);
5098#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099}
5100
5101/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005102 * "getenv()" function
5103 */
5104 static void
5105f_getenv(typval_T *argvars, typval_T *rettv)
5106{
5107 int mustfree = FALSE;
5108 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5109
5110 if (p == NULL)
5111 {
5112 rettv->v_type = VAR_SPECIAL;
5113 rettv->vval.v_number = VVAL_NULL;
5114 return;
5115 }
5116 if (!mustfree)
5117 p = vim_strsave(p);
5118 rettv->vval.v_string = p;
5119 rettv->v_type = VAR_STRING;
5120}
5121
5122/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 * "getfontname()" function
5124 */
5125 static void
5126f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5127{
5128 rettv->v_type = VAR_STRING;
5129 rettv->vval.v_string = NULL;
5130#ifdef FEAT_GUI
5131 if (gui.in_use)
5132 {
5133 GuiFont font;
5134 char_u *name = NULL;
5135
5136 if (argvars[0].v_type == VAR_UNKNOWN)
5137 {
5138 /* Get the "Normal" font. Either the name saved by
5139 * hl_set_font_name() or from the font ID. */
5140 font = gui.norm_font;
5141 name = hl_get_font_name();
5142 }
5143 else
5144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005145 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5147 return;
5148 font = gui_mch_get_font(name, FALSE);
5149 if (font == NOFONT)
5150 return; /* Invalid font name, return empty string. */
5151 }
5152 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5153 if (argvars[0].v_type != VAR_UNKNOWN)
5154 gui_mch_free_font(font);
5155 }
5156#endif
5157}
5158
5159/*
5160 * "getfperm({fname})" function
5161 */
5162 static void
5163f_getfperm(typval_T *argvars, typval_T *rettv)
5164{
5165 char_u *fname;
5166 stat_T st;
5167 char_u *perm = NULL;
5168 char_u flags[] = "rwx";
5169 int i;
5170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005171 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172
5173 rettv->v_type = VAR_STRING;
5174 if (mch_stat((char *)fname, &st) >= 0)
5175 {
5176 perm = vim_strsave((char_u *)"---------");
5177 if (perm != NULL)
5178 {
5179 for (i = 0; i < 9; i++)
5180 {
5181 if (st.st_mode & (1 << (8 - i)))
5182 perm[i] = flags[i % 3];
5183 }
5184 }
5185 }
5186 rettv->vval.v_string = perm;
5187}
5188
5189/*
5190 * "getfsize({fname})" function
5191 */
5192 static void
5193f_getfsize(typval_T *argvars, typval_T *rettv)
5194{
5195 char_u *fname;
5196 stat_T st;
5197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199
5200 rettv->v_type = VAR_NUMBER;
5201
5202 if (mch_stat((char *)fname, &st) >= 0)
5203 {
5204 if (mch_isdir(fname))
5205 rettv->vval.v_number = 0;
5206 else
5207 {
5208 rettv->vval.v_number = (varnumber_T)st.st_size;
5209
5210 /* non-perfect check for overflow */
5211 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5212 rettv->vval.v_number = -2;
5213 }
5214 }
5215 else
5216 rettv->vval.v_number = -1;
5217}
5218
5219/*
5220 * "getftime({fname})" function
5221 */
5222 static void
5223f_getftime(typval_T *argvars, typval_T *rettv)
5224{
5225 char_u *fname;
5226 stat_T st;
5227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229
5230 if (mch_stat((char *)fname, &st) >= 0)
5231 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5232 else
5233 rettv->vval.v_number = -1;
5234}
5235
5236/*
5237 * "getftype({fname})" function
5238 */
5239 static void
5240f_getftype(typval_T *argvars, typval_T *rettv)
5241{
5242 char_u *fname;
5243 stat_T st;
5244 char_u *type = NULL;
5245 char *t;
5246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248
5249 rettv->v_type = VAR_STRING;
5250 if (mch_lstat((char *)fname, &st) >= 0)
5251 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252 if (S_ISREG(st.st_mode))
5253 t = "file";
5254 else if (S_ISDIR(st.st_mode))
5255 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256 else if (S_ISLNK(st.st_mode))
5257 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005258 else if (S_ISBLK(st.st_mode))
5259 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 else if (S_ISCHR(st.st_mode))
5261 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262 else if (S_ISFIFO(st.st_mode))
5263 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005264 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005265 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005266 else
5267 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005268 type = vim_strsave((char_u *)t);
5269 }
5270 rettv->vval.v_string = type;
5271}
5272
5273/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005274 * "getjumplist()" function
5275 */
5276 static void
5277f_getjumplist(typval_T *argvars, typval_T *rettv)
5278{
5279#ifdef FEAT_JUMPLIST
5280 win_T *wp;
5281 int i;
5282 list_T *l;
5283 dict_T *d;
5284#endif
5285
5286 if (rettv_list_alloc(rettv) != OK)
5287 return;
5288
5289#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005290 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005291 if (wp == NULL)
5292 return;
5293
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005294 cleanup_jumplist(wp, TRUE);
5295
Bram Moolenaar4f505882018-02-10 21:06:32 +01005296 l = list_alloc();
5297 if (l == NULL)
5298 return;
5299
5300 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5301 return;
5302 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5303
5304 for (i = 0; i < wp->w_jumplistlen; ++i)
5305 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005306 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5307 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005308 if ((d = dict_alloc()) == NULL)
5309 return;
5310 if (list_append_dict(l, d) == FAIL)
5311 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005312 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5313 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005314 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005315 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005316 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005317 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005318 }
5319#endif
5320}
5321
5322/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 * "getline(lnum, [end])" function
5324 */
5325 static void
5326f_getline(typval_T *argvars, typval_T *rettv)
5327{
5328 linenr_T lnum;
5329 linenr_T end;
5330 int retlist;
5331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005332 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333 if (argvars[1].v_type == VAR_UNKNOWN)
5334 {
5335 end = 0;
5336 retlist = FALSE;
5337 }
5338 else
5339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005340 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341 retlist = TRUE;
5342 }
5343
5344 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5345}
5346
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005347#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005348 static void
5349get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5350{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005351 if (what_arg->v_type == VAR_UNKNOWN)
5352 {
5353 if (rettv_list_alloc(rettv) == OK)
5354 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005355 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005356 }
5357 else
5358 {
5359 if (rettv_dict_alloc(rettv) == OK)
5360 if (is_qf || (wp != NULL))
5361 {
5362 if (what_arg->v_type == VAR_DICT)
5363 {
5364 dict_T *d = what_arg->vval.v_dict;
5365
5366 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005367 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005368 }
5369 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005370 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005371 }
5372 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005373}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005374#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005375
5376/*
5377 * "getloclist()" function
5378 */
5379 static void
5380f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5381{
5382#ifdef FEAT_QUICKFIX
5383 win_T *wp;
5384
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005385 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005386 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5387#endif
5388}
5389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390/*
5391 * "getmatches()" function
5392 */
5393 static void
5394f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5395{
5396#ifdef FEAT_SEARCH_EXTRA
5397 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005398 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005399 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005400 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005401
Bram Moolenaaraff74912019-03-30 18:11:49 +01005402 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5403 return;
5404
5405 cur = win->w_match_head;
5406 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005407 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005408 dict = dict_alloc();
5409 if (dict == NULL)
5410 return;
5411 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005412 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005413 /* match added with matchaddpos() */
5414 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005415 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005416 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005417 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005418 list_T *l;
5419
5420 llpos = &cur->pos.pos[i];
5421 if (llpos->lnum == 0)
5422 break;
5423 l = list_alloc();
5424 if (l == NULL)
5425 break;
5426 list_append_number(l, (varnumber_T)llpos->lnum);
5427 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005429 list_append_number(l, (varnumber_T)llpos->col);
5430 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005432 sprintf(buf, "pos%d", i + 1);
5433 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005434 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005436 else
5437 {
5438 dict_add_string(dict, "pattern", cur->pattern);
5439 }
5440 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5441 dict_add_number(dict, "priority", (long)cur->priority);
5442 dict_add_number(dict, "id", (long)cur->id);
5443# if defined(FEAT_CONCEAL)
5444 if (cur->conceal_char)
5445 {
5446 char_u buf[MB_MAXBYTES + 1];
5447
5448 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5449 dict_add_string(dict, "conceal", (char_u *)&buf);
5450 }
5451# endif
5452 list_append_dict(rettv->vval.v_list, dict);
5453 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454 }
5455#endif
5456}
5457
5458/*
5459 * "getpid()" function
5460 */
5461 static void
5462f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5463{
5464 rettv->vval.v_number = mch_get_pid();
5465}
5466
5467 static void
5468getpos_both(
5469 typval_T *argvars,
5470 typval_T *rettv,
5471 int getcurpos)
5472{
5473 pos_T *fp;
5474 list_T *l;
5475 int fnum = -1;
5476
5477 if (rettv_list_alloc(rettv) == OK)
5478 {
5479 l = rettv->vval.v_list;
5480 if (getcurpos)
5481 fp = &curwin->w_cursor;
5482 else
5483 fp = var2fpos(&argvars[0], TRUE, &fnum);
5484 if (fnum != -1)
5485 list_append_number(l, (varnumber_T)fnum);
5486 else
5487 list_append_number(l, (varnumber_T)0);
5488 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5489 : (varnumber_T)0);
5490 list_append_number(l, (fp != NULL)
5491 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5492 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005493 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494 (varnumber_T)0);
5495 if (getcurpos)
5496 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005497 int save_set_curswant = curwin->w_set_curswant;
5498 colnr_T save_curswant = curwin->w_curswant;
5499 colnr_T save_virtcol = curwin->w_virtcol;
5500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 update_curswant();
5502 list_append_number(l, curwin->w_curswant == MAXCOL ?
5503 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005504
5505 // Do not change "curswant", as it is unexpected that a get
5506 // function has a side effect.
5507 if (save_set_curswant)
5508 {
5509 curwin->w_set_curswant = save_set_curswant;
5510 curwin->w_curswant = save_curswant;
5511 curwin->w_virtcol = save_virtcol;
5512 curwin->w_valid &= ~VALID_VIRTCOL;
5513 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005514 }
5515 }
5516 else
5517 rettv->vval.v_number = FALSE;
5518}
5519
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005520/*
5521 * "getcurpos()" function
5522 */
5523 static void
5524f_getcurpos(typval_T *argvars, typval_T *rettv)
5525{
5526 getpos_both(argvars, rettv, TRUE);
5527}
5528
5529/*
5530 * "getpos(string)" function
5531 */
5532 static void
5533f_getpos(typval_T *argvars, typval_T *rettv)
5534{
5535 getpos_both(argvars, rettv, FALSE);
5536}
5537
5538/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005539 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 */
5541 static void
5542f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5543{
5544#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005545 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546#endif
5547}
5548
5549/*
5550 * "getreg()" function
5551 */
5552 static void
5553f_getreg(typval_T *argvars, typval_T *rettv)
5554{
5555 char_u *strregname;
5556 int regname;
5557 int arg2 = FALSE;
5558 int return_list = FALSE;
5559 int error = FALSE;
5560
5561 if (argvars[0].v_type != VAR_UNKNOWN)
5562 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 error = strregname == NULL;
5565 if (argvars[1].v_type != VAR_UNKNOWN)
5566 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005567 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005568 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005569 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005570 }
5571 }
5572 else
5573 strregname = get_vim_var_str(VV_REG);
5574
5575 if (error)
5576 return;
5577
5578 regname = (strregname == NULL ? '"' : *strregname);
5579 if (regname == 0)
5580 regname = '"';
5581
5582 if (return_list)
5583 {
5584 rettv->v_type = VAR_LIST;
5585 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5586 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5587 if (rettv->vval.v_list == NULL)
5588 (void)rettv_list_alloc(rettv);
5589 else
5590 ++rettv->vval.v_list->lv_refcount;
5591 }
5592 else
5593 {
5594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = get_reg_contents(regname,
5596 arg2 ? GREG_EXPR_SRC : 0);
5597 }
5598}
5599
5600/*
5601 * "getregtype()" function
5602 */
5603 static void
5604f_getregtype(typval_T *argvars, typval_T *rettv)
5605{
5606 char_u *strregname;
5607 int regname;
5608 char_u buf[NUMBUFLEN + 2];
5609 long reglen = 0;
5610
5611 if (argvars[0].v_type != VAR_UNKNOWN)
5612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005613 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 if (strregname == NULL) /* type error; errmsg already given */
5615 {
5616 rettv->v_type = VAR_STRING;
5617 rettv->vval.v_string = NULL;
5618 return;
5619 }
5620 }
5621 else
5622 /* Default to v:register */
5623 strregname = get_vim_var_str(VV_REG);
5624
5625 regname = (strregname == NULL ? '"' : *strregname);
5626 if (regname == 0)
5627 regname = '"';
5628
5629 buf[0] = NUL;
5630 buf[1] = NUL;
5631 switch (get_reg_type(regname, &reglen))
5632 {
5633 case MLINE: buf[0] = 'V'; break;
5634 case MCHAR: buf[0] = 'v'; break;
5635 case MBLOCK:
5636 buf[0] = Ctrl_V;
5637 sprintf((char *)buf + 1, "%ld", reglen + 1);
5638 break;
5639 }
5640 rettv->v_type = VAR_STRING;
5641 rettv->vval.v_string = vim_strsave(buf);
5642}
5643
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005644/*
5645 * Returns information (variables, options, etc.) about a tab page
5646 * as a dictionary.
5647 */
5648 static dict_T *
5649get_tabpage_info(tabpage_T *tp, int tp_idx)
5650{
5651 win_T *wp;
5652 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005653 list_T *l;
5654
5655 dict = dict_alloc();
5656 if (dict == NULL)
5657 return NULL;
5658
Bram Moolenaare0be1672018-07-08 16:50:37 +02005659 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660
5661 l = list_alloc();
5662 if (l != NULL)
5663 {
5664 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5665 wp; wp = wp->w_next)
5666 list_append_number(l, (varnumber_T)wp->w_id);
5667 dict_add_list(dict, "windows", l);
5668 }
5669
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005670 /* Make a reference to tabpage variables */
5671 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005672
5673 return dict;
5674}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675
5676/*
5677 * "gettabinfo()" function
5678 */
5679 static void
5680f_gettabinfo(typval_T *argvars, typval_T *rettv)
5681{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682 tabpage_T *tp, *tparg = NULL;
5683 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005684 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685
5686 if (rettv_list_alloc(rettv) != OK)
5687 return;
5688
5689 if (argvars[0].v_type != VAR_UNKNOWN)
5690 {
5691 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005692 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005693 if (tparg == NULL)
5694 return;
5695 }
5696
5697 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005698 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005699 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005700 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005701 if (tparg != NULL && tp != tparg)
5702 continue;
5703 d = get_tabpage_info(tp, tpnr);
5704 if (d != NULL)
5705 list_append_dict(rettv->vval.v_list, d);
5706 if (tparg != NULL)
5707 return;
5708 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005709}
5710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005711/*
5712 * "gettabvar()" function
5713 */
5714 static void
5715f_gettabvar(typval_T *argvars, typval_T *rettv)
5716{
5717 win_T *oldcurwin;
5718 tabpage_T *tp, *oldtabpage;
5719 dictitem_T *v;
5720 char_u *varname;
5721 int done = FALSE;
5722
5723 rettv->v_type = VAR_STRING;
5724 rettv->vval.v_string = NULL;
5725
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005726 varname = tv_get_string_chk(&argvars[1]);
5727 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728 if (tp != NULL && varname != NULL)
5729 {
5730 /* Set tp to be our tabpage, temporarily. Also set the window to the
5731 * first window in the tabpage, otherwise the window is not valid. */
5732 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005733 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5734 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 {
5736 /* look up the variable */
5737 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5738 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5739 if (v != NULL)
5740 {
5741 copy_tv(&v->di_tv, rettv);
5742 done = TRUE;
5743 }
5744 }
5745
5746 /* restore previous notion of curwin */
5747 restore_win(oldcurwin, oldtabpage, TRUE);
5748 }
5749
5750 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5751 copy_tv(&argvars[2], rettv);
5752}
5753
5754/*
5755 * "gettabwinvar()" function
5756 */
5757 static void
5758f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5759{
5760 getwinvar(argvars, rettv, 1);
5761}
5762
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005763/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005764 * "gettagstack()" function
5765 */
5766 static void
5767f_gettagstack(typval_T *argvars, typval_T *rettv)
5768{
5769 win_T *wp = curwin; // default is current window
5770
5771 if (rettv_dict_alloc(rettv) != OK)
5772 return;
5773
5774 if (argvars[0].v_type != VAR_UNKNOWN)
5775 {
5776 wp = find_win_by_nr_or_id(&argvars[0]);
5777 if (wp == NULL)
5778 return;
5779 }
5780
5781 get_tagstack(wp, rettv->vval.v_dict);
5782}
5783
5784/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005785 * Returns information about a window as a dictionary.
5786 */
5787 static dict_T *
5788get_win_info(win_T *wp, short tpnr, short winnr)
5789{
5790 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005791
5792 dict = dict_alloc();
5793 if (dict == NULL)
5794 return NULL;
5795
Bram Moolenaare0be1672018-07-08 16:50:37 +02005796 dict_add_number(dict, "tabnr", tpnr);
5797 dict_add_number(dict, "winnr", winnr);
5798 dict_add_number(dict, "winid", wp->w_id);
5799 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005800 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005801 dict_add_number(dict, "topline", wp->w_topline);
5802 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005803#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005804 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005805#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005806 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005807 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005808 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005809
Bram Moolenaar69905d12017-08-13 18:14:47 +02005810#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005811 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005812#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005813#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005814 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5815 dict_add_number(dict, "loclist",
5816 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005817#endif
5818
Bram Moolenaar30567352016-08-27 21:25:44 +02005819 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005820 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005821
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005822 return dict;
5823}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005824
5825/*
5826 * "getwininfo()" function
5827 */
5828 static void
5829f_getwininfo(typval_T *argvars, typval_T *rettv)
5830{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005831 tabpage_T *tp;
5832 win_T *wp = NULL, *wparg = NULL;
5833 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005834 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005835
5836 if (rettv_list_alloc(rettv) != OK)
5837 return;
5838
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005839 if (argvars[0].v_type != VAR_UNKNOWN)
5840 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005841 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005842 if (wparg == NULL)
5843 return;
5844 }
5845
5846 /* Collect information about either all the windows across all the tab
5847 * pages or one particular window.
5848 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005849 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005850 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005851 tabnr++;
5852 winnr = 0;
5853 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005854 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005855 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005856 if (wparg != NULL && wp != wparg)
5857 continue;
5858 d = get_win_info(wp, tabnr, winnr);
5859 if (d != NULL)
5860 list_append_dict(rettv->vval.v_list, d);
5861 if (wparg != NULL)
5862 /* found information about a specific window */
5863 return;
5864 }
5865 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005866}
5867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005868/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005869 * "win_execute()" function
5870 */
5871 static void
5872f_win_execute(typval_T *argvars, typval_T *rettv)
5873{
5874 int id = (int)tv_get_number(argvars);
5875 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005876 win_T *save_curwin;
5877 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005878
5879 if (wp != NULL)
5880 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005881 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
5882 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005883 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005884 check_cursor();
5885 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005886 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005887 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005888 }
5889}
5890
5891/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005892 * "win_findbuf()" function
5893 */
5894 static void
5895f_win_findbuf(typval_T *argvars, typval_T *rettv)
5896{
5897 if (rettv_list_alloc(rettv) != FAIL)
5898 win_findbuf(argvars, rettv->vval.v_list);
5899}
5900
5901/*
5902 * "win_getid()" function
5903 */
5904 static void
5905f_win_getid(typval_T *argvars, typval_T *rettv)
5906{
5907 rettv->vval.v_number = win_getid(argvars);
5908}
5909
5910/*
5911 * "win_gotoid()" function
5912 */
5913 static void
5914f_win_gotoid(typval_T *argvars, typval_T *rettv)
5915{
5916 rettv->vval.v_number = win_gotoid(argvars);
5917}
5918
5919/*
5920 * "win_id2tabwin()" function
5921 */
5922 static void
5923f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5924{
5925 if (rettv_list_alloc(rettv) != FAIL)
5926 win_id2tabwin(argvars, rettv->vval.v_list);
5927}
5928
5929/*
5930 * "win_id2win()" function
5931 */
5932 static void
5933f_win_id2win(typval_T *argvars, typval_T *rettv)
5934{
5935 rettv->vval.v_number = win_id2win(argvars);
5936}
5937
5938/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005939 * "win_screenpos()" function
5940 */
5941 static void
5942f_win_screenpos(typval_T *argvars, typval_T *rettv)
5943{
5944 win_T *wp;
5945
5946 if (rettv_list_alloc(rettv) == FAIL)
5947 return;
5948
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005949 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005950 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5951 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5952}
5953
5954/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005955 * "getwinpos({timeout})" function
5956 */
5957 static void
5958f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5959{
5960 int x = -1;
5961 int y = -1;
5962
5963 if (rettv_list_alloc(rettv) == FAIL)
5964 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005965#if defined(FEAT_GUI) \
5966 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5967 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005968 {
5969 varnumber_T timeout = 100;
5970
5971 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005972 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005973
5974 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005975 }
5976#endif
5977 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5978 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5979}
5980
5981
5982/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005983 * "getwinposx()" function
5984 */
5985 static void
5986f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5987{
5988 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005989#if defined(FEAT_GUI) \
5990 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5991 || defined(MSWIN)
5992
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005993 {
5994 int x, y;
5995
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005996 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005997 rettv->vval.v_number = x;
5998 }
5999#endif
6000}
6001
6002/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006003 * "getwinposy()" function
6004 */
6005 static void
6006f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6007{
6008 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006009#if defined(FEAT_GUI) \
6010 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6011 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006012 {
6013 int x, y;
6014
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006015 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006016 rettv->vval.v_number = y;
6017 }
6018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019}
6020
6021/*
6022 * "getwinvar()" function
6023 */
6024 static void
6025f_getwinvar(typval_T *argvars, typval_T *rettv)
6026{
6027 getwinvar(argvars, rettv, 0);
6028}
6029
6030/*
6031 * "glob()" function
6032 */
6033 static void
6034f_glob(typval_T *argvars, typval_T *rettv)
6035{
6036 int options = WILD_SILENT|WILD_USE_NL;
6037 expand_T xpc;
6038 int error = FALSE;
6039
6040 /* When the optional second argument is non-zero, don't remove matches
6041 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6042 rettv->v_type = VAR_STRING;
6043 if (argvars[1].v_type != VAR_UNKNOWN)
6044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006045 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046 options |= WILD_KEEP_ALL;
6047 if (argvars[2].v_type != VAR_UNKNOWN)
6048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006049 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006050 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006051 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006052 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053 options |= WILD_ALLLINKS;
6054 }
6055 }
6056 if (!error)
6057 {
6058 ExpandInit(&xpc);
6059 xpc.xp_context = EXPAND_FILES;
6060 if (p_wic)
6061 options += WILD_ICASE;
6062 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006063 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 NULL, options, WILD_ALL);
6065 else if (rettv_list_alloc(rettv) != FAIL)
6066 {
6067 int i;
6068
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006069 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006070 NULL, options, WILD_ALL_KEEP);
6071 for (i = 0; i < xpc.xp_numfiles; i++)
6072 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6073
6074 ExpandCleanup(&xpc);
6075 }
6076 }
6077 else
6078 rettv->vval.v_string = NULL;
6079}
6080
6081/*
6082 * "globpath()" function
6083 */
6084 static void
6085f_globpath(typval_T *argvars, typval_T *rettv)
6086{
6087 int flags = 0;
6088 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006089 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090 int error = FALSE;
6091 garray_T ga;
6092 int i;
6093
6094 /* When the optional second argument is non-zero, don't remove matches
6095 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6096 rettv->v_type = VAR_STRING;
6097 if (argvars[2].v_type != VAR_UNKNOWN)
6098 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006099 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 flags |= WILD_KEEP_ALL;
6101 if (argvars[3].v_type != VAR_UNKNOWN)
6102 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006103 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006104 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006105 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006106 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 flags |= WILD_ALLLINKS;
6108 }
6109 }
6110 if (file != NULL && !error)
6111 {
6112 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006113 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114 if (rettv->v_type == VAR_STRING)
6115 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6116 else if (rettv_list_alloc(rettv) != FAIL)
6117 for (i = 0; i < ga.ga_len; ++i)
6118 list_append_string(rettv->vval.v_list,
6119 ((char_u **)(ga.ga_data))[i], -1);
6120 ga_clear_strings(&ga);
6121 }
6122 else
6123 rettv->vval.v_string = NULL;
6124}
6125
6126/*
6127 * "glob2regpat()" function
6128 */
6129 static void
6130f_glob2regpat(typval_T *argvars, typval_T *rettv)
6131{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006132 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133
6134 rettv->v_type = VAR_STRING;
6135 rettv->vval.v_string = (pat == NULL)
6136 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6137}
6138
6139/* for VIM_VERSION_ defines */
6140#include "version.h"
6141
6142/*
6143 * "has()" function
6144 */
6145 static void
6146f_has(typval_T *argvars, typval_T *rettv)
6147{
6148 int i;
6149 char_u *name;
6150 int n = FALSE;
6151 static char *(has_list[]) =
6152 {
6153#ifdef AMIGA
6154 "amiga",
6155# ifdef FEAT_ARP
6156 "arp",
6157# endif
6158#endif
6159#ifdef __BEOS__
6160 "beos",
6161#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006162#if defined(BSD) && !defined(MACOS_X)
6163 "bsd",
6164#endif
6165#ifdef hpux
6166 "hpux",
6167#endif
6168#ifdef __linux__
6169 "linux",
6170#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006171#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006172 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6173 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006174# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006175 "macunix", /* Mac OS X, with the darwin feature */
6176 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006177# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006178#endif
6179#ifdef __QNX__
6180 "qnx",
6181#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006182#ifdef SUN_SYSTEM
6183 "sun",
6184#else
6185 "moon",
6186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187#ifdef UNIX
6188 "unix",
6189#endif
6190#ifdef VMS
6191 "vms",
6192#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006193#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194 "win32",
6195#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006196#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197 "win32unix",
6198#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006199#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200 "win64",
6201#endif
6202#ifdef EBCDIC
6203 "ebcdic",
6204#endif
6205#ifndef CASE_INSENSITIVE_FILENAME
6206 "fname_case",
6207#endif
6208#ifdef HAVE_ACL
6209 "acl",
6210#endif
6211#ifdef FEAT_ARABIC
6212 "arabic",
6213#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006215#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006216 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006217#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006218#ifdef FEAT_AUTOSERVERNAME
6219 "autoservername",
6220#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006221#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006223# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006224 "balloon_multiline",
6225# endif
6226#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006227#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006228 "balloon_eval_term",
6229#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006230#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6231 "builtin_terms",
6232# ifdef ALL_BUILTIN_TCAPS
6233 "all_builtin_terms",
6234# endif
6235#endif
6236#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006237 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006238 || defined(FEAT_GUI_MOTIF))
6239 "browsefilter",
6240#endif
6241#ifdef FEAT_BYTEOFF
6242 "byte_offset",
6243#endif
6244#ifdef FEAT_JOB_CHANNEL
6245 "channel",
6246#endif
6247#ifdef FEAT_CINDENT
6248 "cindent",
6249#endif
6250#ifdef FEAT_CLIENTSERVER
6251 "clientserver",
6252#endif
6253#ifdef FEAT_CLIPBOARD
6254 "clipboard",
6255#endif
6256#ifdef FEAT_CMDL_COMPL
6257 "cmdline_compl",
6258#endif
6259#ifdef FEAT_CMDHIST
6260 "cmdline_hist",
6261#endif
6262#ifdef FEAT_COMMENTS
6263 "comments",
6264#endif
6265#ifdef FEAT_CONCEAL
6266 "conceal",
6267#endif
6268#ifdef FEAT_CRYPT
6269 "cryptv",
6270 "crypt-blowfish",
6271 "crypt-blowfish2",
6272#endif
6273#ifdef FEAT_CSCOPE
6274 "cscope",
6275#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277#ifdef CURSOR_SHAPE
6278 "cursorshape",
6279#endif
6280#ifdef DEBUG
6281 "debug",
6282#endif
6283#ifdef FEAT_CON_DIALOG
6284 "dialog_con",
6285#endif
6286#ifdef FEAT_GUI_DIALOG
6287 "dialog_gui",
6288#endif
6289#ifdef FEAT_DIFF
6290 "diff",
6291#endif
6292#ifdef FEAT_DIGRAPHS
6293 "digraphs",
6294#endif
6295#ifdef FEAT_DIRECTX
6296 "directx",
6297#endif
6298#ifdef FEAT_DND
6299 "dnd",
6300#endif
6301#ifdef FEAT_EMACS_TAGS
6302 "emacs_tags",
6303#endif
6304 "eval", /* always present, of course! */
6305 "ex_extra", /* graduated feature */
6306#ifdef FEAT_SEARCH_EXTRA
6307 "extra_search",
6308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#ifdef FEAT_SEARCHPATH
6310 "file_in_path",
6311#endif
6312#ifdef FEAT_FILTERPIPE
6313 "filterpipe",
6314#endif
6315#ifdef FEAT_FIND_ID
6316 "find_in_path",
6317#endif
6318#ifdef FEAT_FLOAT
6319 "float",
6320#endif
6321#ifdef FEAT_FOLDING
6322 "folding",
6323#endif
6324#ifdef FEAT_FOOTER
6325 "footer",
6326#endif
6327#if !defined(USE_SYSTEM) && defined(UNIX)
6328 "fork",
6329#endif
6330#ifdef FEAT_GETTEXT
6331 "gettext",
6332#endif
6333#ifdef FEAT_GUI
6334 "gui",
6335#endif
6336#ifdef FEAT_GUI_ATHENA
6337# ifdef FEAT_GUI_NEXTAW
6338 "gui_neXtaw",
6339# else
6340 "gui_athena",
6341# endif
6342#endif
6343#ifdef FEAT_GUI_GTK
6344 "gui_gtk",
6345# ifdef USE_GTK3
6346 "gui_gtk3",
6347# else
6348 "gui_gtk2",
6349# endif
6350#endif
6351#ifdef FEAT_GUI_GNOME
6352 "gui_gnome",
6353#endif
6354#ifdef FEAT_GUI_MAC
6355 "gui_mac",
6356#endif
6357#ifdef FEAT_GUI_MOTIF
6358 "gui_motif",
6359#endif
6360#ifdef FEAT_GUI_PHOTON
6361 "gui_photon",
6362#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006363#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364 "gui_win32",
6365#endif
6366#ifdef FEAT_HANGULIN
6367 "hangul_input",
6368#endif
6369#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6370 "iconv",
6371#endif
6372#ifdef FEAT_INS_EXPAND
6373 "insert_expand",
6374#endif
6375#ifdef FEAT_JOB_CHANNEL
6376 "job",
6377#endif
6378#ifdef FEAT_JUMPLIST
6379 "jumplist",
6380#endif
6381#ifdef FEAT_KEYMAP
6382 "keymap",
6383#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006384 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385#ifdef FEAT_LANGMAP
6386 "langmap",
6387#endif
6388#ifdef FEAT_LIBCALL
6389 "libcall",
6390#endif
6391#ifdef FEAT_LINEBREAK
6392 "linebreak",
6393#endif
6394#ifdef FEAT_LISP
6395 "lispindent",
6396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398#ifdef FEAT_LOCALMAP
6399 "localmap",
6400#endif
6401#ifdef FEAT_LUA
6402# ifndef DYNAMIC_LUA
6403 "lua",
6404# endif
6405#endif
6406#ifdef FEAT_MENU
6407 "menu",
6408#endif
6409#ifdef FEAT_SESSION
6410 "mksession",
6411#endif
6412#ifdef FEAT_MODIFY_FNAME
6413 "modify_fname",
6414#endif
6415#ifdef FEAT_MOUSE
6416 "mouse",
6417#endif
6418#ifdef FEAT_MOUSESHAPE
6419 "mouseshape",
6420#endif
6421#if defined(UNIX) || defined(VMS)
6422# ifdef FEAT_MOUSE_DEC
6423 "mouse_dec",
6424# endif
6425# ifdef FEAT_MOUSE_GPM
6426 "mouse_gpm",
6427# endif
6428# ifdef FEAT_MOUSE_JSB
6429 "mouse_jsbterm",
6430# endif
6431# ifdef FEAT_MOUSE_NET
6432 "mouse_netterm",
6433# endif
6434# ifdef FEAT_MOUSE_PTERM
6435 "mouse_pterm",
6436# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006437# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 "mouse_sgr",
6439# endif
6440# ifdef FEAT_SYSMOUSE
6441 "mouse_sysmouse",
6442# endif
6443# ifdef FEAT_MOUSE_URXVT
6444 "mouse_urxvt",
6445# endif
6446# ifdef FEAT_MOUSE_XTERM
6447 "mouse_xterm",
6448# endif
6449#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006450 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006451#ifdef FEAT_MBYTE_IME
6452 "multi_byte_ime",
6453#endif
6454#ifdef FEAT_MULTI_LANG
6455 "multi_lang",
6456#endif
6457#ifdef FEAT_MZSCHEME
6458#ifndef DYNAMIC_MZSCHEME
6459 "mzscheme",
6460#endif
6461#endif
6462#ifdef FEAT_NUM64
6463 "num64",
6464#endif
6465#ifdef FEAT_OLE
6466 "ole",
6467#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006468#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006470#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471#ifdef FEAT_PATH_EXTRA
6472 "path_extra",
6473#endif
6474#ifdef FEAT_PERL
6475#ifndef DYNAMIC_PERL
6476 "perl",
6477#endif
6478#endif
6479#ifdef FEAT_PERSISTENT_UNDO
6480 "persistent_undo",
6481#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006482#if defined(FEAT_PYTHON)
6483 "python_compiled",
6484# if defined(DYNAMIC_PYTHON)
6485 "python_dynamic",
6486# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006487 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006488 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006489# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006491#if defined(FEAT_PYTHON3)
6492 "python3_compiled",
6493# if defined(DYNAMIC_PYTHON3)
6494 "python3_dynamic",
6495# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006497 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006498# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499#endif
6500#ifdef FEAT_POSTSCRIPT
6501 "postscript",
6502#endif
6503#ifdef FEAT_PRINTER
6504 "printer",
6505#endif
6506#ifdef FEAT_PROFILE
6507 "profile",
6508#endif
6509#ifdef FEAT_RELTIME
6510 "reltime",
6511#endif
6512#ifdef FEAT_QUICKFIX
6513 "quickfix",
6514#endif
6515#ifdef FEAT_RIGHTLEFT
6516 "rightleft",
6517#endif
6518#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6519 "ruby",
6520#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#ifdef FEAT_CMDL_INFO
6523 "showcmd",
6524 "cmdline_info",
6525#endif
6526#ifdef FEAT_SIGNS
6527 "signs",
6528#endif
6529#ifdef FEAT_SMARTINDENT
6530 "smartindent",
6531#endif
6532#ifdef STARTUPTIME
6533 "startuptime",
6534#endif
6535#ifdef FEAT_STL_OPT
6536 "statusline",
6537#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006538#ifdef FEAT_NETBEANS_INTG
6539 "netbeans_intg",
6540#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006541#ifdef FEAT_SOUND
6542 "sound",
6543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544#ifdef FEAT_SPELL
6545 "spell",
6546#endif
6547#ifdef FEAT_SYN_HL
6548 "syntax",
6549#endif
6550#if defined(USE_SYSTEM) || !defined(UNIX)
6551 "system",
6552#endif
6553#ifdef FEAT_TAG_BINS
6554 "tag_binary",
6555#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556#ifdef FEAT_TCL
6557# ifndef DYNAMIC_TCL
6558 "tcl",
6559# endif
6560#endif
6561#ifdef FEAT_TERMGUICOLORS
6562 "termguicolors",
6563#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006564#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006565 "terminal",
6566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006567#ifdef TERMINFO
6568 "terminfo",
6569#endif
6570#ifdef FEAT_TERMRESPONSE
6571 "termresponse",
6572#endif
6573#ifdef FEAT_TEXTOBJ
6574 "textobjects",
6575#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006576#ifdef FEAT_TEXT_PROP
6577 "textprop",
6578#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579#ifdef HAVE_TGETENT
6580 "tgetent",
6581#endif
6582#ifdef FEAT_TIMERS
6583 "timers",
6584#endif
6585#ifdef FEAT_TITLE
6586 "title",
6587#endif
6588#ifdef FEAT_TOOLBAR
6589 "toolbar",
6590#endif
6591#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6592 "unnamedplus",
6593#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594 "user-commands", /* was accidentally included in 5.4 */
6595 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006596#ifdef FEAT_VARTABS
6597 "vartabs",
6598#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006599 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600#ifdef FEAT_VIMINFO
6601 "viminfo",
6602#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006603 "vimscript-1",
6604 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006605 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006609 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006610#ifdef FEAT_VTP
6611 "vtp",
6612#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006613#ifdef FEAT_WILDIGN
6614 "wildignore",
6615#endif
6616#ifdef FEAT_WILDMENU
6617 "wildmenu",
6618#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006619 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006620#ifdef FEAT_WAK
6621 "winaltkeys",
6622#endif
6623#ifdef FEAT_WRITEBACKUP
6624 "writebackup",
6625#endif
6626#ifdef FEAT_XIM
6627 "xim",
6628#endif
6629#ifdef FEAT_XFONTSET
6630 "xfontset",
6631#endif
6632#ifdef FEAT_XPM_W32
6633 "xpm",
6634 "xpm_w32", /* for backward compatibility */
6635#else
6636# if defined(HAVE_XPM)
6637 "xpm",
6638# endif
6639#endif
6640#ifdef USE_XSMP
6641 "xsmp",
6642#endif
6643#ifdef USE_XSMP_INTERACT
6644 "xsmp_interact",
6645#endif
6646#ifdef FEAT_XCLIPBOARD
6647 "xterm_clipboard",
6648#endif
6649#ifdef FEAT_XTERM_SAVE
6650 "xterm_save",
6651#endif
6652#if defined(UNIX) && defined(FEAT_X11)
6653 "X11",
6654#endif
6655 NULL
6656 };
6657
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006658 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659 for (i = 0; has_list[i] != NULL; ++i)
6660 if (STRICMP(name, has_list[i]) == 0)
6661 {
6662 n = TRUE;
6663 break;
6664 }
6665
6666 if (n == FALSE)
6667 {
6668 if (STRNICMP(name, "patch", 5) == 0)
6669 {
6670 if (name[5] == '-'
6671 && STRLEN(name) >= 11
6672 && vim_isdigit(name[6])
6673 && vim_isdigit(name[8])
6674 && vim_isdigit(name[10]))
6675 {
6676 int major = atoi((char *)name + 6);
6677 int minor = atoi((char *)name + 8);
6678
6679 /* Expect "patch-9.9.01234". */
6680 n = (major < VIM_VERSION_MAJOR
6681 || (major == VIM_VERSION_MAJOR
6682 && (minor < VIM_VERSION_MINOR
6683 || (minor == VIM_VERSION_MINOR
6684 && has_patch(atoi((char *)name + 10))))));
6685 }
6686 else
6687 n = has_patch(atoi((char *)name + 5));
6688 }
6689 else if (STRICMP(name, "vim_starting") == 0)
6690 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006691 else if (STRICMP(name, "ttyin") == 0)
6692 n = mch_input_isatty();
6693 else if (STRICMP(name, "ttyout") == 0)
6694 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 else if (STRICMP(name, "multi_byte_encoding") == 0)
6696 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006697#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698 else if (STRICMP(name, "balloon_multiline") == 0)
6699 n = multiline_balloon_available();
6700#endif
6701#ifdef DYNAMIC_TCL
6702 else if (STRICMP(name, "tcl") == 0)
6703 n = tcl_enabled(FALSE);
6704#endif
6705#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6706 else if (STRICMP(name, "iconv") == 0)
6707 n = iconv_enabled(FALSE);
6708#endif
6709#ifdef DYNAMIC_LUA
6710 else if (STRICMP(name, "lua") == 0)
6711 n = lua_enabled(FALSE);
6712#endif
6713#ifdef DYNAMIC_MZSCHEME
6714 else if (STRICMP(name, "mzscheme") == 0)
6715 n = mzscheme_enabled(FALSE);
6716#endif
6717#ifdef DYNAMIC_RUBY
6718 else if (STRICMP(name, "ruby") == 0)
6719 n = ruby_enabled(FALSE);
6720#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006721#ifdef DYNAMIC_PYTHON
6722 else if (STRICMP(name, "python") == 0)
6723 n = python_enabled(FALSE);
6724#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006725#ifdef DYNAMIC_PYTHON3
6726 else if (STRICMP(name, "python3") == 0)
6727 n = python3_enabled(FALSE);
6728#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006729#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6730 else if (STRICMP(name, "pythonx") == 0)
6731 {
6732# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6733 if (p_pyx == 0)
6734 n = python3_enabled(FALSE) || python_enabled(FALSE);
6735 else if (p_pyx == 3)
6736 n = python3_enabled(FALSE);
6737 else if (p_pyx == 2)
6738 n = python_enabled(FALSE);
6739# elif defined(DYNAMIC_PYTHON)
6740 n = python_enabled(FALSE);
6741# elif defined(DYNAMIC_PYTHON3)
6742 n = python3_enabled(FALSE);
6743# endif
6744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745#endif
6746#ifdef DYNAMIC_PERL
6747 else if (STRICMP(name, "perl") == 0)
6748 n = perl_enabled(FALSE);
6749#endif
6750#ifdef FEAT_GUI
6751 else if (STRICMP(name, "gui_running") == 0)
6752 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006753# ifdef FEAT_BROWSE
6754 else if (STRICMP(name, "browse") == 0)
6755 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6756# endif
6757#endif
6758#ifdef FEAT_SYN_HL
6759 else if (STRICMP(name, "syntax_items") == 0)
6760 n = syntax_present(curwin);
6761#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006762#ifdef FEAT_VTP
6763 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006764 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#endif
6766#ifdef FEAT_NETBEANS_INTG
6767 else if (STRICMP(name, "netbeans_enabled") == 0)
6768 n = netbeans_active();
6769#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006770#ifdef FEAT_MOUSE_GPM
6771 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6772 n = gpm_enabled();
6773#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006774#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006775 else if (STRICMP(name, "terminal") == 0)
6776 n = terminal_enabled();
6777#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006778#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006779 else if (STRICMP(name, "conpty") == 0)
6780 n = use_conpty();
6781#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782 }
6783
6784 rettv->vval.v_number = n;
6785}
6786
6787/*
6788 * "has_key()" function
6789 */
6790 static void
6791f_has_key(typval_T *argvars, typval_T *rettv)
6792{
6793 if (argvars[0].v_type != VAR_DICT)
6794 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006795 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 return;
6797 }
6798 if (argvars[0].vval.v_dict == NULL)
6799 return;
6800
6801 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006802 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006803}
6804
6805/*
6806 * "haslocaldir()" function
6807 */
6808 static void
6809f_haslocaldir(typval_T *argvars, typval_T *rettv)
6810{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006811 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 win_T *wp = NULL;
6813
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006814 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6815
6816 // Check for window-local and tab-local directories
6817 if (wp != NULL && wp->w_localdir != NULL)
6818 rettv->vval.v_number = 1;
6819 else if (tp != NULL && tp->tp_localdir != NULL)
6820 rettv->vval.v_number = 2;
6821 else
6822 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823}
6824
6825/*
6826 * "hasmapto()" function
6827 */
6828 static void
6829f_hasmapto(typval_T *argvars, typval_T *rettv)
6830{
6831 char_u *name;
6832 char_u *mode;
6833 char_u buf[NUMBUFLEN];
6834 int abbr = FALSE;
6835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006836 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837 if (argvars[1].v_type == VAR_UNKNOWN)
6838 mode = (char_u *)"nvo";
6839 else
6840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006841 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006842 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006843 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 }
6845
6846 if (map_to_exists(name, mode, abbr))
6847 rettv->vval.v_number = TRUE;
6848 else
6849 rettv->vval.v_number = FALSE;
6850}
6851
6852/*
6853 * "histadd()" function
6854 */
6855 static void
6856f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6857{
6858#ifdef FEAT_CMDHIST
6859 int histype;
6860 char_u *str;
6861 char_u buf[NUMBUFLEN];
6862#endif
6863
6864 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01006865 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 return;
6867#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006868 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006869 histype = str != NULL ? get_histtype(str) : -1;
6870 if (histype >= 0)
6871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 if (*str != NUL)
6874 {
6875 init_history();
6876 add_to_history(histype, str, FALSE, NUL);
6877 rettv->vval.v_number = TRUE;
6878 return;
6879 }
6880 }
6881#endif
6882}
6883
6884/*
6885 * "histdel()" function
6886 */
6887 static void
6888f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6889{
6890#ifdef FEAT_CMDHIST
6891 int n;
6892 char_u buf[NUMBUFLEN];
6893 char_u *str;
6894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006895 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 if (str == NULL)
6897 n = 0;
6898 else if (argvars[1].v_type == VAR_UNKNOWN)
6899 /* only one argument: clear entire history */
6900 n = clr_history(get_histtype(str));
6901 else if (argvars[1].v_type == VAR_NUMBER)
6902 /* index given: remove that entry */
6903 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006904 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905 else
6906 /* string given: remove all matching entries */
6907 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006908 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909 rettv->vval.v_number = n;
6910#endif
6911}
6912
6913/*
6914 * "histget()" function
6915 */
6916 static void
6917f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6918{
6919#ifdef FEAT_CMDHIST
6920 int type;
6921 int idx;
6922 char_u *str;
6923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 if (str == NULL)
6926 rettv->vval.v_string = NULL;
6927 else
6928 {
6929 type = get_histtype(str);
6930 if (argvars[1].v_type == VAR_UNKNOWN)
6931 idx = get_history_idx(type);
6932 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006933 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006934 /* -1 on type error */
6935 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6936 }
6937#else
6938 rettv->vval.v_string = NULL;
6939#endif
6940 rettv->v_type = VAR_STRING;
6941}
6942
6943/*
6944 * "histnr()" function
6945 */
6946 static void
6947f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6948{
6949 int i;
6950
6951#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006952 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953
6954 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6955 if (i >= HIST_CMD && i < HIST_COUNT)
6956 i = get_history_idx(i);
6957 else
6958#endif
6959 i = -1;
6960 rettv->vval.v_number = i;
6961}
6962
6963/*
6964 * "highlightID(name)" function
6965 */
6966 static void
6967f_hlID(typval_T *argvars, typval_T *rettv)
6968{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006969 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970}
6971
6972/*
6973 * "highlight_exists()" function
6974 */
6975 static void
6976f_hlexists(typval_T *argvars, typval_T *rettv)
6977{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006978 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979}
6980
6981/*
6982 * "hostname()" function
6983 */
6984 static void
6985f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6986{
6987 char_u hostname[256];
6988
6989 mch_get_host_name(hostname, 256);
6990 rettv->v_type = VAR_STRING;
6991 rettv->vval.v_string = vim_strsave(hostname);
6992}
6993
6994/*
6995 * iconv() function
6996 */
6997 static void
6998f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6999{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000 char_u buf1[NUMBUFLEN];
7001 char_u buf2[NUMBUFLEN];
7002 char_u *from, *to, *str;
7003 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007004
7005 rettv->v_type = VAR_STRING;
7006 rettv->vval.v_string = NULL;
7007
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007008 str = tv_get_string(&argvars[0]);
7009 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7010 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007011 vimconv.vc_type = CONV_NONE;
7012 convert_setup(&vimconv, from, to);
7013
7014 /* If the encodings are equal, no conversion needed. */
7015 if (vimconv.vc_type == CONV_NONE)
7016 rettv->vval.v_string = vim_strsave(str);
7017 else
7018 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7019
7020 convert_setup(&vimconv, NULL, NULL);
7021 vim_free(from);
7022 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023}
7024
7025/*
7026 * "indent()" function
7027 */
7028 static void
7029f_indent(typval_T *argvars, typval_T *rettv)
7030{
7031 linenr_T lnum;
7032
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007033 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7035 rettv->vval.v_number = get_indent_lnum(lnum);
7036 else
7037 rettv->vval.v_number = -1;
7038}
7039
7040/*
7041 * "index()" function
7042 */
7043 static void
7044f_index(typval_T *argvars, typval_T *rettv)
7045{
7046 list_T *l;
7047 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007048 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 long idx = 0;
7050 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007051 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052
7053 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007054 if (argvars[0].v_type == VAR_BLOB)
7055 {
7056 typval_T tv;
7057 int start = 0;
7058
7059 if (argvars[2].v_type != VAR_UNKNOWN)
7060 {
7061 start = tv_get_number_chk(&argvars[2], &error);
7062 if (error)
7063 return;
7064 }
7065 b = argvars[0].vval.v_blob;
7066 if (b == NULL)
7067 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007068 if (start < 0)
7069 {
7070 start = blob_len(b) + start;
7071 if (start < 0)
7072 start = 0;
7073 }
7074
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007075 for (idx = start; idx < blob_len(b); ++idx)
7076 {
7077 tv.v_type = VAR_NUMBER;
7078 tv.vval.v_number = blob_get(b, idx);
7079 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7080 {
7081 rettv->vval.v_number = idx;
7082 return;
7083 }
7084 }
7085 return;
7086 }
7087 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007089 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 return;
7091 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 l = argvars[0].vval.v_list;
7094 if (l != NULL)
7095 {
7096 item = l->lv_first;
7097 if (argvars[2].v_type != VAR_UNKNOWN)
7098 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007099 /* Start at specified item. Use the cached index that list_find()
7100 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007101 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 idx = l->lv_idx;
7103 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007104 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105 if (error)
7106 item = NULL;
7107 }
7108
7109 for ( ; item != NULL; item = item->li_next, ++idx)
7110 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7111 {
7112 rettv->vval.v_number = idx;
7113 break;
7114 }
7115 }
7116}
7117
7118static int inputsecret_flag = 0;
7119
7120/*
7121 * "input()" function
7122 * Also handles inputsecret() when inputsecret is set.
7123 */
7124 static void
7125f_input(typval_T *argvars, typval_T *rettv)
7126{
7127 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7128}
7129
7130/*
7131 * "inputdialog()" function
7132 */
7133 static void
7134f_inputdialog(typval_T *argvars, typval_T *rettv)
7135{
7136#if defined(FEAT_GUI_TEXTDIALOG)
7137 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7138 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7139 {
7140 char_u *message;
7141 char_u buf[NUMBUFLEN];
7142 char_u *defstr = (char_u *)"";
7143
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007144 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007146 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7148 else
7149 IObuff[0] = NUL;
7150 if (message != NULL && defstr != NULL
7151 && do_dialog(VIM_QUESTION, NULL, message,
7152 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7153 rettv->vval.v_string = vim_strsave(IObuff);
7154 else
7155 {
7156 if (message != NULL && defstr != NULL
7157 && argvars[1].v_type != VAR_UNKNOWN
7158 && argvars[2].v_type != VAR_UNKNOWN)
7159 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007160 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 else
7162 rettv->vval.v_string = NULL;
7163 }
7164 rettv->v_type = VAR_STRING;
7165 }
7166 else
7167#endif
7168 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7169}
7170
7171/*
7172 * "inputlist()" function
7173 */
7174 static void
7175f_inputlist(typval_T *argvars, typval_T *rettv)
7176{
7177 listitem_T *li;
7178 int selected;
7179 int mouse_used;
7180
7181#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007182 /* While starting up, there is no place to enter text. When running tests
7183 * with --not-a-term we assume feedkeys() will be used. */
7184 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 return;
7186#endif
7187 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007189 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 return;
7191 }
7192
7193 msg_start();
7194 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7195 lines_left = Rows; /* avoid more prompt */
7196 msg_scroll = TRUE;
7197 msg_clr_eos();
7198
7199 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7200 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007201 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202 msg_putchar('\n');
7203 }
7204
7205 /* Ask for choice. */
7206 selected = prompt_for_number(&mouse_used);
7207 if (mouse_used)
7208 selected -= lines_left;
7209
7210 rettv->vval.v_number = selected;
7211}
7212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7214
7215/*
7216 * "inputrestore()" function
7217 */
7218 static void
7219f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7220{
7221 if (ga_userinput.ga_len > 0)
7222 {
7223 --ga_userinput.ga_len;
7224 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7225 + ga_userinput.ga_len);
7226 /* default return is zero == OK */
7227 }
7228 else if (p_verbose > 1)
7229 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007230 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231 rettv->vval.v_number = 1; /* Failed */
7232 }
7233}
7234
7235/*
7236 * "inputsave()" function
7237 */
7238 static void
7239f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7240{
7241 /* Add an entry to the stack of typeahead storage. */
7242 if (ga_grow(&ga_userinput, 1) == OK)
7243 {
7244 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7245 + ga_userinput.ga_len);
7246 ++ga_userinput.ga_len;
7247 /* default return is zero == OK */
7248 }
7249 else
7250 rettv->vval.v_number = 1; /* Failed */
7251}
7252
7253/*
7254 * "inputsecret()" function
7255 */
7256 static void
7257f_inputsecret(typval_T *argvars, typval_T *rettv)
7258{
7259 ++cmdline_star;
7260 ++inputsecret_flag;
7261 f_input(argvars, rettv);
7262 --cmdline_star;
7263 --inputsecret_flag;
7264}
7265
7266/*
7267 * "insert()" function
7268 */
7269 static void
7270f_insert(typval_T *argvars, typval_T *rettv)
7271{
7272 long before = 0;
7273 listitem_T *item;
7274 list_T *l;
7275 int error = FALSE;
7276
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007277 if (argvars[0].v_type == VAR_BLOB)
7278 {
7279 int val, len;
7280 char_u *p;
7281
7282 len = blob_len(argvars[0].vval.v_blob);
7283 if (argvars[2].v_type != VAR_UNKNOWN)
7284 {
7285 before = (long)tv_get_number_chk(&argvars[2], &error);
7286 if (error)
7287 return; // type error; errmsg already given
7288 if (before < 0 || before > len)
7289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007290 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007291 return;
7292 }
7293 }
7294 val = tv_get_number_chk(&argvars[1], &error);
7295 if (error)
7296 return;
7297 if (val < 0 || val > 255)
7298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007299 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007300 return;
7301 }
7302
7303 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7304 return;
7305 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7306 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7307 *(p + before) = val;
7308 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7309
7310 copy_tv(&argvars[0], rettv);
7311 }
7312 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007313 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007314 else if ((l = argvars[0].vval.v_list) != NULL
7315 && !var_check_lock(l->lv_lock,
7316 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317 {
7318 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320 if (error)
7321 return; /* type error; errmsg already given */
7322
7323 if (before == l->lv_len)
7324 item = NULL;
7325 else
7326 {
7327 item = list_find(l, before);
7328 if (item == NULL)
7329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007330 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 l = NULL;
7332 }
7333 }
7334 if (l != NULL)
7335 {
7336 list_insert_tv(l, &argvars[1], item);
7337 copy_tv(&argvars[0], rettv);
7338 }
7339 }
7340}
7341
7342/*
7343 * "invert(expr)" function
7344 */
7345 static void
7346f_invert(typval_T *argvars, typval_T *rettv)
7347{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007348 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349}
7350
7351/*
7352 * "isdirectory()" function
7353 */
7354 static void
7355f_isdirectory(typval_T *argvars, typval_T *rettv)
7356{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007357 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358}
7359
7360/*
7361 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7362 * or it refers to a List or Dictionary that is locked.
7363 */
7364 static int
7365tv_islocked(typval_T *tv)
7366{
7367 return (tv->v_lock & VAR_LOCKED)
7368 || (tv->v_type == VAR_LIST
7369 && tv->vval.v_list != NULL
7370 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7371 || (tv->v_type == VAR_DICT
7372 && tv->vval.v_dict != NULL
7373 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7374}
7375
7376/*
7377 * "islocked()" function
7378 */
7379 static void
7380f_islocked(typval_T *argvars, typval_T *rettv)
7381{
7382 lval_T lv;
7383 char_u *end;
7384 dictitem_T *di;
7385
7386 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007387 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007388 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389 if (end != NULL && lv.ll_name != NULL)
7390 {
7391 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007392 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 else
7394 {
7395 if (lv.ll_tv == NULL)
7396 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007397 di = find_var(lv.ll_name, NULL, TRUE);
7398 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007400 /* Consider a variable locked when:
7401 * 1. the variable itself is locked
7402 * 2. the value of the variable is locked.
7403 * 3. the List or Dict value is locked.
7404 */
7405 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7406 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 }
7408 }
7409 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007410 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007411 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007412 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413 else if (lv.ll_list != NULL)
7414 /* List item. */
7415 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7416 else
7417 /* Dictionary item. */
7418 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7419 }
7420 }
7421
7422 clear_lval(&lv);
7423}
7424
7425#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7426/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007427 * "isinf()" function
7428 */
7429 static void
7430f_isinf(typval_T *argvars, typval_T *rettv)
7431{
7432 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7433 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7434}
7435
7436/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 * "isnan()" function
7438 */
7439 static void
7440f_isnan(typval_T *argvars, typval_T *rettv)
7441{
7442 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7443 && isnan(argvars[0].vval.v_float);
7444}
7445#endif
7446
7447/*
7448 * "items(dict)" function
7449 */
7450 static void
7451f_items(typval_T *argvars, typval_T *rettv)
7452{
7453 dict_list(argvars, rettv, 2);
7454}
7455
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456/*
7457 * "join()" function
7458 */
7459 static void
7460f_join(typval_T *argvars, typval_T *rettv)
7461{
7462 garray_T ga;
7463 char_u *sep;
7464
7465 if (argvars[0].v_type != VAR_LIST)
7466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007467 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007468 return;
7469 }
7470 if (argvars[0].vval.v_list == NULL)
7471 return;
7472 if (argvars[1].v_type == VAR_UNKNOWN)
7473 sep = (char_u *)" ";
7474 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007475 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476
7477 rettv->v_type = VAR_STRING;
7478
7479 if (sep != NULL)
7480 {
7481 ga_init2(&ga, (int)sizeof(char), 80);
7482 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7483 ga_append(&ga, NUL);
7484 rettv->vval.v_string = (char_u *)ga.ga_data;
7485 }
7486 else
7487 rettv->vval.v_string = NULL;
7488}
7489
7490/*
7491 * "js_decode()" function
7492 */
7493 static void
7494f_js_decode(typval_T *argvars, typval_T *rettv)
7495{
7496 js_read_T reader;
7497
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007498 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007499 reader.js_fill = NULL;
7500 reader.js_used = 0;
7501 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007502 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503}
7504
7505/*
7506 * "js_encode()" function
7507 */
7508 static void
7509f_js_encode(typval_T *argvars, typval_T *rettv)
7510{
7511 rettv->v_type = VAR_STRING;
7512 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7513}
7514
7515/*
7516 * "json_decode()" function
7517 */
7518 static void
7519f_json_decode(typval_T *argvars, typval_T *rettv)
7520{
7521 js_read_T reader;
7522
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007523 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 reader.js_fill = NULL;
7525 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007526 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527}
7528
7529/*
7530 * "json_encode()" function
7531 */
7532 static void
7533f_json_encode(typval_T *argvars, typval_T *rettv)
7534{
7535 rettv->v_type = VAR_STRING;
7536 rettv->vval.v_string = json_encode(&argvars[0], 0);
7537}
7538
7539/*
7540 * "keys()" function
7541 */
7542 static void
7543f_keys(typval_T *argvars, typval_T *rettv)
7544{
7545 dict_list(argvars, rettv, 0);
7546}
7547
7548/*
7549 * "last_buffer_nr()" function.
7550 */
7551 static void
7552f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7553{
7554 int n = 0;
7555 buf_T *buf;
7556
Bram Moolenaar29323592016-07-24 22:04:11 +02007557 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 if (n < buf->b_fnum)
7559 n = buf->b_fnum;
7560
7561 rettv->vval.v_number = n;
7562}
7563
7564/*
7565 * "len()" function
7566 */
7567 static void
7568f_len(typval_T *argvars, typval_T *rettv)
7569{
7570 switch (argvars[0].v_type)
7571 {
7572 case VAR_STRING:
7573 case VAR_NUMBER:
7574 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007575 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007576 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007577 case VAR_BLOB:
7578 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7579 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007580 case VAR_LIST:
7581 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7582 break;
7583 case VAR_DICT:
7584 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7585 break;
7586 case VAR_UNKNOWN:
7587 case VAR_SPECIAL:
7588 case VAR_FLOAT:
7589 case VAR_FUNC:
7590 case VAR_PARTIAL:
7591 case VAR_JOB:
7592 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 break;
7595 }
7596}
7597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007599libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600{
7601#ifdef FEAT_LIBCALL
7602 char_u *string_in;
7603 char_u **string_result;
7604 int nr_result;
7605#endif
7606
7607 rettv->v_type = type;
7608 if (type != VAR_NUMBER)
7609 rettv->vval.v_string = NULL;
7610
7611 if (check_restricted() || check_secure())
7612 return;
7613
7614#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007615 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7617 {
7618 string_in = NULL;
7619 if (argvars[2].v_type == VAR_STRING)
7620 string_in = argvars[2].vval.v_string;
7621 if (type == VAR_NUMBER)
7622 string_result = NULL;
7623 else
7624 string_result = &rettv->vval.v_string;
7625 if (mch_libcall(argvars[0].vval.v_string,
7626 argvars[1].vval.v_string,
7627 string_in,
7628 argvars[2].vval.v_number,
7629 string_result,
7630 &nr_result) == OK
7631 && type == VAR_NUMBER)
7632 rettv->vval.v_number = nr_result;
7633 }
7634#endif
7635}
7636
7637/*
7638 * "libcall()" function
7639 */
7640 static void
7641f_libcall(typval_T *argvars, typval_T *rettv)
7642{
7643 libcall_common(argvars, rettv, VAR_STRING);
7644}
7645
7646/*
7647 * "libcallnr()" function
7648 */
7649 static void
7650f_libcallnr(typval_T *argvars, typval_T *rettv)
7651{
7652 libcall_common(argvars, rettv, VAR_NUMBER);
7653}
7654
7655/*
7656 * "line(string)" function
7657 */
7658 static void
7659f_line(typval_T *argvars, typval_T *rettv)
7660{
7661 linenr_T lnum = 0;
7662 pos_T *fp;
7663 int fnum;
7664
7665 fp = var2fpos(&argvars[0], TRUE, &fnum);
7666 if (fp != NULL)
7667 lnum = fp->lnum;
7668 rettv->vval.v_number = lnum;
7669}
7670
7671/*
7672 * "line2byte(lnum)" function
7673 */
7674 static void
7675f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7676{
7677#ifndef FEAT_BYTEOFF
7678 rettv->vval.v_number = -1;
7679#else
7680 linenr_T lnum;
7681
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007682 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7684 rettv->vval.v_number = -1;
7685 else
7686 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7687 if (rettv->vval.v_number >= 0)
7688 ++rettv->vval.v_number;
7689#endif
7690}
7691
7692/*
7693 * "lispindent(lnum)" function
7694 */
7695 static void
7696f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7697{
7698#ifdef FEAT_LISP
7699 pos_T pos;
7700 linenr_T lnum;
7701
7702 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007703 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7705 {
7706 curwin->w_cursor.lnum = lnum;
7707 rettv->vval.v_number = get_lisp_indent();
7708 curwin->w_cursor = pos;
7709 }
7710 else
7711#endif
7712 rettv->vval.v_number = -1;
7713}
7714
7715/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02007716 * "list2str()" function
7717 */
7718 static void
7719f_list2str(typval_T *argvars, typval_T *rettv)
7720{
7721 list_T *l;
7722 listitem_T *li;
7723 garray_T ga;
7724 int utf8 = FALSE;
7725
7726 rettv->v_type = VAR_STRING;
7727 rettv->vval.v_string = NULL;
7728 if (argvars[0].v_type != VAR_LIST)
7729 {
7730 emsg(_(e_invarg));
7731 return;
7732 }
7733
7734 l = argvars[0].vval.v_list;
7735 if (l == NULL)
7736 return; // empty list results in empty string
7737
7738 if (argvars[1].v_type != VAR_UNKNOWN)
7739 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
7740
7741 ga_init2(&ga, 1, 80);
7742 if (has_mbyte || utf8)
7743 {
7744 char_u buf[MB_MAXBYTES + 1];
7745 int (*char2bytes)(int, char_u *);
7746
7747 if (utf8 || enc_utf8)
7748 char2bytes = utf_char2bytes;
7749 else
7750 char2bytes = mb_char2bytes;
7751
7752 for (li = l->lv_first; li != NULL; li = li->li_next)
7753 {
7754 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
7755 ga_concat(&ga, buf);
7756 }
7757 ga_append(&ga, NUL);
7758 }
7759 else if (ga_grow(&ga, list_len(l) + 1) == OK)
7760 {
7761 for (li = l->lv_first; li != NULL; li = li->li_next)
7762 ga_append(&ga, tv_get_number(&li->li_tv));
7763 ga_append(&ga, NUL);
7764 }
7765
7766 rettv->v_type = VAR_STRING;
7767 rettv->vval.v_string = ga.ga_data;
7768}
7769
7770/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771 * "localtime()" function
7772 */
7773 static void
7774f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7775{
7776 rettv->vval.v_number = (varnumber_T)time(NULL);
7777}
7778
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 static void
7780get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7781{
7782 char_u *keys;
7783 char_u *which;
7784 char_u buf[NUMBUFLEN];
7785 char_u *keys_buf = NULL;
7786 char_u *rhs;
7787 int mode;
7788 int abbr = FALSE;
7789 int get_dict = FALSE;
7790 mapblock_T *mp;
7791 int buffer_local;
7792
7793 /* return empty string for failure */
7794 rettv->v_type = VAR_STRING;
7795 rettv->vval.v_string = NULL;
7796
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007797 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 if (*keys == NUL)
7799 return;
7800
7801 if (argvars[1].v_type != VAR_UNKNOWN)
7802 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 if (argvars[2].v_type != VAR_UNKNOWN)
7805 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007806 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007808 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809 }
7810 }
7811 else
7812 which = (char_u *)"";
7813 if (which == NULL)
7814 return;
7815
7816 mode = get_map_mode(&which, 0);
7817
7818 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7819 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7820 vim_free(keys_buf);
7821
7822 if (!get_dict)
7823 {
7824 /* Return a string. */
7825 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007826 {
7827 if (*rhs == NUL)
7828 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7829 else
7830 rettv->vval.v_string = str2special_save(rhs, FALSE);
7831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832
7833 }
7834 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7835 {
7836 /* Return a dictionary. */
7837 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7838 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7839 dict_T *dict = rettv->vval.v_dict;
7840
Bram Moolenaare0be1672018-07-08 16:50:37 +02007841 dict_add_string(dict, "lhs", lhs);
7842 dict_add_string(dict, "rhs", mp->m_orig_str);
7843 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7844 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7845 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007846 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7847 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007848 dict_add_number(dict, "buffer", (long)buffer_local);
7849 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7850 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851
7852 vim_free(lhs);
7853 vim_free(mapmode);
7854 }
7855}
7856
7857#ifdef FEAT_FLOAT
7858/*
7859 * "log()" function
7860 */
7861 static void
7862f_log(typval_T *argvars, typval_T *rettv)
7863{
7864 float_T f = 0.0;
7865
7866 rettv->v_type = VAR_FLOAT;
7867 if (get_float_arg(argvars, &f) == OK)
7868 rettv->vval.v_float = log(f);
7869 else
7870 rettv->vval.v_float = 0.0;
7871}
7872
7873/*
7874 * "log10()" function
7875 */
7876 static void
7877f_log10(typval_T *argvars, typval_T *rettv)
7878{
7879 float_T f = 0.0;
7880
7881 rettv->v_type = VAR_FLOAT;
7882 if (get_float_arg(argvars, &f) == OK)
7883 rettv->vval.v_float = log10(f);
7884 else
7885 rettv->vval.v_float = 0.0;
7886}
7887#endif
7888
7889#ifdef FEAT_LUA
7890/*
7891 * "luaeval()" function
7892 */
7893 static void
7894f_luaeval(typval_T *argvars, typval_T *rettv)
7895{
7896 char_u *str;
7897 char_u buf[NUMBUFLEN];
7898
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007899 if (check_restricted() || check_secure())
7900 return;
7901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007902 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 do_luaeval(str, argvars + 1, rettv);
7904}
7905#endif
7906
7907/*
7908 * "map()" function
7909 */
7910 static void
7911f_map(typval_T *argvars, typval_T *rettv)
7912{
7913 filter_map(argvars, rettv, TRUE);
7914}
7915
7916/*
7917 * "maparg()" function
7918 */
7919 static void
7920f_maparg(typval_T *argvars, typval_T *rettv)
7921{
7922 get_maparg(argvars, rettv, TRUE);
7923}
7924
7925/*
7926 * "mapcheck()" function
7927 */
7928 static void
7929f_mapcheck(typval_T *argvars, typval_T *rettv)
7930{
7931 get_maparg(argvars, rettv, FALSE);
7932}
7933
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007934typedef enum
7935{
7936 MATCH_END, /* matchend() */
7937 MATCH_MATCH, /* match() */
7938 MATCH_STR, /* matchstr() */
7939 MATCH_LIST, /* matchlist() */
7940 MATCH_POS /* matchstrpos() */
7941} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007942
7943 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007944find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007945{
7946 char_u *str = NULL;
7947 long len = 0;
7948 char_u *expr = NULL;
7949 char_u *pat;
7950 regmatch_T regmatch;
7951 char_u patbuf[NUMBUFLEN];
7952 char_u strbuf[NUMBUFLEN];
7953 char_u *save_cpo;
7954 long start = 0;
7955 long nth = 1;
7956 colnr_T startcol = 0;
7957 int match = 0;
7958 list_T *l = NULL;
7959 listitem_T *li = NULL;
7960 long idx = 0;
7961 char_u *tofree = NULL;
7962
7963 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7964 save_cpo = p_cpo;
7965 p_cpo = (char_u *)"";
7966
7967 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007968 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007970 /* type MATCH_LIST: return empty list when there are no matches.
7971 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007972 if (rettv_list_alloc(rettv) == FAIL)
7973 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007974 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 && (list_append_string(rettv->vval.v_list,
7976 (char_u *)"", 0) == FAIL
7977 || list_append_number(rettv->vval.v_list,
7978 (varnumber_T)-1) == FAIL
7979 || list_append_number(rettv->vval.v_list,
7980 (varnumber_T)-1) == FAIL
7981 || list_append_number(rettv->vval.v_list,
7982 (varnumber_T)-1) == FAIL))
7983 {
7984 list_free(rettv->vval.v_list);
7985 rettv->vval.v_list = NULL;
7986 goto theend;
7987 }
7988 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007989 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 {
7991 rettv->v_type = VAR_STRING;
7992 rettv->vval.v_string = NULL;
7993 }
7994
7995 if (argvars[0].v_type == VAR_LIST)
7996 {
7997 if ((l = argvars[0].vval.v_list) == NULL)
7998 goto theend;
7999 li = l->lv_first;
8000 }
8001 else
8002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008003 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008004 len = (long)STRLEN(str);
8005 }
8006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008007 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (pat == NULL)
8009 goto theend;
8010
8011 if (argvars[2].v_type != VAR_UNKNOWN)
8012 {
8013 int error = FALSE;
8014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008015 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008016 if (error)
8017 goto theend;
8018 if (l != NULL)
8019 {
8020 li = list_find(l, start);
8021 if (li == NULL)
8022 goto theend;
8023 idx = l->lv_idx; /* use the cached index */
8024 }
8025 else
8026 {
8027 if (start < 0)
8028 start = 0;
8029 if (start > len)
8030 goto theend;
8031 /* When "count" argument is there ignore matches before "start",
8032 * otherwise skip part of the string. Differs when pattern is "^"
8033 * or "\<". */
8034 if (argvars[3].v_type != VAR_UNKNOWN)
8035 startcol = start;
8036 else
8037 {
8038 str += start;
8039 len -= start;
8040 }
8041 }
8042
8043 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008044 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 if (error)
8046 goto theend;
8047 }
8048
8049 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8050 if (regmatch.regprog != NULL)
8051 {
8052 regmatch.rm_ic = p_ic;
8053
8054 for (;;)
8055 {
8056 if (l != NULL)
8057 {
8058 if (li == NULL)
8059 {
8060 match = FALSE;
8061 break;
8062 }
8063 vim_free(tofree);
8064 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8065 if (str == NULL)
8066 break;
8067 }
8068
8069 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8070
8071 if (match && --nth <= 0)
8072 break;
8073 if (l == NULL && !match)
8074 break;
8075
8076 /* Advance to just after the match. */
8077 if (l != NULL)
8078 {
8079 li = li->li_next;
8080 ++idx;
8081 }
8082 else
8083 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 startcol = (colnr_T)(regmatch.startp[0]
8085 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086 if (startcol > (colnr_T)len
8087 || str + startcol <= regmatch.startp[0])
8088 {
8089 match = FALSE;
8090 break;
8091 }
8092 }
8093 }
8094
8095 if (match)
8096 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008097 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 {
8099 listitem_T *li1 = rettv->vval.v_list->lv_first;
8100 listitem_T *li2 = li1->li_next;
8101 listitem_T *li3 = li2->li_next;
8102 listitem_T *li4 = li3->li_next;
8103
8104 vim_free(li1->li_tv.vval.v_string);
8105 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8106 (int)(regmatch.endp[0] - regmatch.startp[0]));
8107 li3->li_tv.vval.v_number =
8108 (varnumber_T)(regmatch.startp[0] - expr);
8109 li4->li_tv.vval.v_number =
8110 (varnumber_T)(regmatch.endp[0] - expr);
8111 if (l != NULL)
8112 li2->li_tv.vval.v_number = (varnumber_T)idx;
8113 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008114 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 {
8116 int i;
8117
8118 /* return list with matched string and submatches */
8119 for (i = 0; i < NSUBEXP; ++i)
8120 {
8121 if (regmatch.endp[i] == NULL)
8122 {
8123 if (list_append_string(rettv->vval.v_list,
8124 (char_u *)"", 0) == FAIL)
8125 break;
8126 }
8127 else if (list_append_string(rettv->vval.v_list,
8128 regmatch.startp[i],
8129 (int)(regmatch.endp[i] - regmatch.startp[i]))
8130 == FAIL)
8131 break;
8132 }
8133 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008134 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 {
8136 /* return matched string */
8137 if (l != NULL)
8138 copy_tv(&li->li_tv, rettv);
8139 else
8140 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8141 (int)(regmatch.endp[0] - regmatch.startp[0]));
8142 }
8143 else if (l != NULL)
8144 rettv->vval.v_number = idx;
8145 else
8146 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008147 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 rettv->vval.v_number =
8149 (varnumber_T)(regmatch.startp[0] - str);
8150 else
8151 rettv->vval.v_number =
8152 (varnumber_T)(regmatch.endp[0] - str);
8153 rettv->vval.v_number += (varnumber_T)(str - expr);
8154 }
8155 }
8156 vim_regfree(regmatch.regprog);
8157 }
8158
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008159theend:
8160 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161 /* matchstrpos() without a list: drop the second item. */
8162 listitem_remove(rettv->vval.v_list,
8163 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008164 vim_free(tofree);
8165 p_cpo = save_cpo;
8166}
8167
8168/*
8169 * "match()" function
8170 */
8171 static void
8172f_match(typval_T *argvars, typval_T *rettv)
8173{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008174 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175}
8176
Bram Moolenaar95e51472018-07-28 16:55:56 +02008177#ifdef FEAT_SEARCH_EXTRA
8178 static int
8179matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8180{
8181 dictitem_T *di;
8182
8183 if (tv->v_type != VAR_DICT)
8184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008185 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008186 return FAIL;
8187 }
8188
8189 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008190 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008191 (char_u *)"conceal", FALSE);
8192
8193 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8194 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008195 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008196 if (*win == NULL)
8197 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008198 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008199 return FAIL;
8200 }
8201 }
8202
8203 return OK;
8204}
8205#endif
8206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207/*
8208 * "matchadd()" function
8209 */
8210 static void
8211f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8212{
8213#ifdef FEAT_SEARCH_EXTRA
8214 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008215 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8216 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008217 int prio = 10; /* default priority */
8218 int id = -1;
8219 int error = FALSE;
8220 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008221 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222
8223 rettv->vval.v_number = -1;
8224
8225 if (grp == NULL || pat == NULL)
8226 return;
8227 if (argvars[2].v_type != VAR_UNKNOWN)
8228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008229 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008230 if (argvars[3].v_type != VAR_UNKNOWN)
8231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008232 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008233 if (argvars[4].v_type != VAR_UNKNOWN
8234 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8235 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008236 }
8237 }
8238 if (error == TRUE)
8239 return;
8240 if (id >= 1 && id <= 3)
8241 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008242 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 return;
8244 }
8245
Bram Moolenaar95e51472018-07-28 16:55:56 +02008246 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008247 conceal_char);
8248#endif
8249}
8250
8251/*
8252 * "matchaddpos()" function
8253 */
8254 static void
8255f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8256{
8257#ifdef FEAT_SEARCH_EXTRA
8258 char_u buf[NUMBUFLEN];
8259 char_u *group;
8260 int prio = 10;
8261 int id = -1;
8262 int error = FALSE;
8263 list_T *l;
8264 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008265 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266
8267 rettv->vval.v_number = -1;
8268
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008269 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 if (group == NULL)
8271 return;
8272
8273 if (argvars[1].v_type != VAR_LIST)
8274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008275 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008276 return;
8277 }
8278 l = argvars[1].vval.v_list;
8279 if (l == NULL)
8280 return;
8281
8282 if (argvars[2].v_type != VAR_UNKNOWN)
8283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008284 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008285 if (argvars[3].v_type != VAR_UNKNOWN)
8286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008287 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008288
8289 if (argvars[4].v_type != VAR_UNKNOWN
8290 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8291 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 }
8293 }
8294 if (error == TRUE)
8295 return;
8296
8297 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8298 if (id == 1 || id == 2)
8299 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008300 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 return;
8302 }
8303
Bram Moolenaar95e51472018-07-28 16:55:56 +02008304 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 conceal_char);
8306#endif
8307}
8308
8309/*
8310 * "matcharg()" function
8311 */
8312 static void
8313f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8314{
8315 if (rettv_list_alloc(rettv) == OK)
8316 {
8317#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008318 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 matchitem_T *m;
8320
8321 if (id >= 1 && id <= 3)
8322 {
8323 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8324 {
8325 list_append_string(rettv->vval.v_list,
8326 syn_id2name(m->hlg_id), -1);
8327 list_append_string(rettv->vval.v_list, m->pattern, -1);
8328 }
8329 else
8330 {
8331 list_append_string(rettv->vval.v_list, NULL, -1);
8332 list_append_string(rettv->vval.v_list, NULL, -1);
8333 }
8334 }
8335#endif
8336 }
8337}
8338
8339/*
8340 * "matchdelete()" function
8341 */
8342 static void
8343f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8344{
8345#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008346 win_T *win = get_optional_window(argvars, 1);
8347
8348 if (win == NULL)
8349 rettv->vval.v_number = -1;
8350 else
8351 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008352 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353#endif
8354}
8355
8356/*
8357 * "matchend()" function
8358 */
8359 static void
8360f_matchend(typval_T *argvars, typval_T *rettv)
8361{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008362 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363}
8364
8365/*
8366 * "matchlist()" function
8367 */
8368 static void
8369f_matchlist(typval_T *argvars, typval_T *rettv)
8370{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008371 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008372}
8373
8374/*
8375 * "matchstr()" function
8376 */
8377 static void
8378f_matchstr(typval_T *argvars, typval_T *rettv)
8379{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008380 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008381}
8382
8383/*
8384 * "matchstrpos()" function
8385 */
8386 static void
8387f_matchstrpos(typval_T *argvars, typval_T *rettv)
8388{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008389 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008390}
8391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 static void
8393max_min(typval_T *argvars, typval_T *rettv, int domax)
8394{
8395 varnumber_T n = 0;
8396 varnumber_T i;
8397 int error = FALSE;
8398
8399 if (argvars[0].v_type == VAR_LIST)
8400 {
8401 list_T *l;
8402 listitem_T *li;
8403
8404 l = argvars[0].vval.v_list;
8405 if (l != NULL)
8406 {
8407 li = l->lv_first;
8408 if (li != NULL)
8409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008410 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 for (;;)
8412 {
8413 li = li->li_next;
8414 if (li == NULL)
8415 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 if (domax ? i > n : i < n)
8418 n = i;
8419 }
8420 }
8421 }
8422 }
8423 else if (argvars[0].v_type == VAR_DICT)
8424 {
8425 dict_T *d;
8426 int first = TRUE;
8427 hashitem_T *hi;
8428 int todo;
8429
8430 d = argvars[0].vval.v_dict;
8431 if (d != NULL)
8432 {
8433 todo = (int)d->dv_hashtab.ht_used;
8434 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8435 {
8436 if (!HASHITEM_EMPTY(hi))
8437 {
8438 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008439 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (first)
8441 {
8442 n = i;
8443 first = FALSE;
8444 }
8445 else if (domax ? i > n : i < n)
8446 n = i;
8447 }
8448 }
8449 }
8450 }
8451 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008452 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453 rettv->vval.v_number = error ? 0 : n;
8454}
8455
8456/*
8457 * "max()" function
8458 */
8459 static void
8460f_max(typval_T *argvars, typval_T *rettv)
8461{
8462 max_min(argvars, rettv, TRUE);
8463}
8464
8465/*
8466 * "min()" function
8467 */
8468 static void
8469f_min(typval_T *argvars, typval_T *rettv)
8470{
8471 max_min(argvars, rettv, FALSE);
8472}
8473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474/*
8475 * Create the directory in which "dir" is located, and higher levels when
8476 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008477 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 */
8479 static int
8480mkdir_recurse(char_u *dir, int prot)
8481{
8482 char_u *p;
8483 char_u *updir;
8484 int r = FAIL;
8485
8486 /* Get end of directory name in "dir".
8487 * We're done when it's "/" or "c:/". */
8488 p = gettail_sep(dir);
8489 if (p <= get_past_head(dir))
8490 return OK;
8491
8492 /* If the directory exists we're done. Otherwise: create it.*/
8493 updir = vim_strnsave(dir, (int)(p - dir));
8494 if (updir == NULL)
8495 return FAIL;
8496 if (mch_isdir(updir))
8497 r = OK;
8498 else if (mkdir_recurse(updir, prot) == OK)
8499 r = vim_mkdir_emsg(updir, prot);
8500 vim_free(updir);
8501 return r;
8502}
8503
8504#ifdef vim_mkdir
8505/*
8506 * "mkdir()" function
8507 */
8508 static void
8509f_mkdir(typval_T *argvars, typval_T *rettv)
8510{
8511 char_u *dir;
8512 char_u buf[NUMBUFLEN];
8513 int prot = 0755;
8514
8515 rettv->vval.v_number = FAIL;
8516 if (check_restricted() || check_secure())
8517 return;
8518
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008519 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008521 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008523 if (*gettail(dir) == NUL)
8524 /* remove trailing slashes */
8525 *gettail_sep(dir) = NUL;
8526
8527 if (argvars[1].v_type != VAR_UNKNOWN)
8528 {
8529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008532 if (prot == -1)
8533 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008535 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008536 {
8537 if (mch_isdir(dir))
8538 {
8539 /* With the "p" flag it's OK if the dir already exists. */
8540 rettv->vval.v_number = OK;
8541 return;
8542 }
8543 mkdir_recurse(dir, prot);
8544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008546 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547}
8548#endif
8549
8550/*
8551 * "mode()" function
8552 */
8553 static void
8554f_mode(typval_T *argvars, typval_T *rettv)
8555{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008556 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557
Bram Moolenaar612cc382018-07-29 15:34:26 +02008558 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559
8560 if (time_for_testing == 93784)
8561 {
8562 /* Testing the two-character code. */
8563 buf[0] = 'x';
8564 buf[1] = '!';
8565 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008566#ifdef FEAT_TERMINAL
8567 else if (term_use_loop())
8568 buf[0] = 't';
8569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 else if (VIsual_active)
8571 {
8572 if (VIsual_select)
8573 buf[0] = VIsual_mode + 's' - 'v';
8574 else
8575 buf[0] = VIsual_mode;
8576 }
8577 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8578 || State == CONFIRM)
8579 {
8580 buf[0] = 'r';
8581 if (State == ASKMORE)
8582 buf[1] = 'm';
8583 else if (State == CONFIRM)
8584 buf[1] = '?';
8585 }
8586 else if (State == EXTERNCMD)
8587 buf[0] = '!';
8588 else if (State & INSERT)
8589 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 if (State & VREPLACE_FLAG)
8591 {
8592 buf[0] = 'R';
8593 buf[1] = 'v';
8594 }
8595 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008596 {
8597 if (State & REPLACE_FLAG)
8598 buf[0] = 'R';
8599 else
8600 buf[0] = 'i';
8601#ifdef FEAT_INS_EXPAND
8602 if (ins_compl_active())
8603 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008604 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008605 buf[1] = 'x';
8606#endif
8607 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008609 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 {
8611 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008612 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008614 else if (exmode_active == EXMODE_NORMAL)
8615 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 }
8617 else
8618 {
8619 buf[0] = 'n';
8620 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008623 // to be able to detect force-linewise/blockwise/characterwise operations
8624 buf[2] = motion_force;
8625 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008626 else if (restart_edit == 'I' || restart_edit == 'R'
8627 || restart_edit == 'V')
8628 {
8629 buf[1] = 'i';
8630 buf[2] = restart_edit;
8631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 }
8633
8634 /* Clear out the minor mode when the argument is not a non-zero number or
8635 * non-empty string. */
8636 if (!non_zero_arg(&argvars[0]))
8637 buf[1] = NUL;
8638
8639 rettv->vval.v_string = vim_strsave(buf);
8640 rettv->v_type = VAR_STRING;
8641}
8642
8643#if defined(FEAT_MZSCHEME) || defined(PROTO)
8644/*
8645 * "mzeval()" function
8646 */
8647 static void
8648f_mzeval(typval_T *argvars, typval_T *rettv)
8649{
8650 char_u *str;
8651 char_u buf[NUMBUFLEN];
8652
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008653 if (check_restricted() || check_secure())
8654 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008655 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 do_mzeval(str, rettv);
8657}
8658
8659 void
8660mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8661{
8662 typval_T argvars[3];
8663
8664 argvars[0].v_type = VAR_STRING;
8665 argvars[0].vval.v_string = name;
8666 copy_tv(args, &argvars[1]);
8667 argvars[2].v_type = VAR_UNKNOWN;
8668 f_call(argvars, rettv);
8669 clear_tv(&argvars[1]);
8670}
8671#endif
8672
8673/*
8674 * "nextnonblank()" function
8675 */
8676 static void
8677f_nextnonblank(typval_T *argvars, typval_T *rettv)
8678{
8679 linenr_T lnum;
8680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008681 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 {
8683 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8684 {
8685 lnum = 0;
8686 break;
8687 }
8688 if (*skipwhite(ml_get(lnum)) != NUL)
8689 break;
8690 }
8691 rettv->vval.v_number = lnum;
8692}
8693
8694/*
8695 * "nr2char()" function
8696 */
8697 static void
8698f_nr2char(typval_T *argvars, typval_T *rettv)
8699{
8700 char_u buf[NUMBUFLEN];
8701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 if (has_mbyte)
8703 {
8704 int utf8 = 0;
8705
8706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008707 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01008709 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008711 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 }
8713 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008715 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716 buf[1] = NUL;
8717 }
8718 rettv->v_type = VAR_STRING;
8719 rettv->vval.v_string = vim_strsave(buf);
8720}
8721
8722/*
8723 * "or(expr, expr)" function
8724 */
8725 static void
8726f_or(typval_T *argvars, typval_T *rettv)
8727{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008728 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8729 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008730}
8731
8732/*
8733 * "pathshorten()" function
8734 */
8735 static void
8736f_pathshorten(typval_T *argvars, typval_T *rettv)
8737{
8738 char_u *p;
8739
8740 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008741 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 if (p == NULL)
8743 rettv->vval.v_string = NULL;
8744 else
8745 {
8746 p = vim_strsave(p);
8747 rettv->vval.v_string = p;
8748 if (p != NULL)
8749 shorten_dir(p);
8750 }
8751}
8752
8753#ifdef FEAT_PERL
8754/*
8755 * "perleval()" function
8756 */
8757 static void
8758f_perleval(typval_T *argvars, typval_T *rettv)
8759{
8760 char_u *str;
8761 char_u buf[NUMBUFLEN];
8762
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008763 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 do_perleval(str, rettv);
8765}
8766#endif
8767
8768#ifdef FEAT_FLOAT
8769/*
8770 * "pow()" function
8771 */
8772 static void
8773f_pow(typval_T *argvars, typval_T *rettv)
8774{
8775 float_T fx = 0.0, fy = 0.0;
8776
8777 rettv->v_type = VAR_FLOAT;
8778 if (get_float_arg(argvars, &fx) == OK
8779 && get_float_arg(&argvars[1], &fy) == OK)
8780 rettv->vval.v_float = pow(fx, fy);
8781 else
8782 rettv->vval.v_float = 0.0;
8783}
8784#endif
8785
8786/*
8787 * "prevnonblank()" function
8788 */
8789 static void
8790f_prevnonblank(typval_T *argvars, typval_T *rettv)
8791{
8792 linenr_T lnum;
8793
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008794 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8796 lnum = 0;
8797 else
8798 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8799 --lnum;
8800 rettv->vval.v_number = lnum;
8801}
8802
8803/* This dummy va_list is here because:
8804 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8805 * - locally in the function results in a "used before set" warning
8806 * - using va_start() to initialize it gives "function with fixed args" error */
8807static va_list ap;
8808
8809/*
8810 * "printf()" function
8811 */
8812 static void
8813f_printf(typval_T *argvars, typval_T *rettv)
8814{
8815 char_u buf[NUMBUFLEN];
8816 int len;
8817 char_u *s;
8818 int saved_did_emsg = did_emsg;
8819 char *fmt;
8820
8821 rettv->v_type = VAR_STRING;
8822 rettv->vval.v_string = NULL;
8823
8824 /* Get the required length, allocate the buffer and do it for real. */
8825 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008826 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008827 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828 if (!did_emsg)
8829 {
8830 s = alloc(len + 1);
8831 if (s != NULL)
8832 {
8833 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008834 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8835 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 }
8837 }
8838 did_emsg |= saved_did_emsg;
8839}
8840
8841/*
8842 * "pumvisible()" function
8843 */
8844 static void
8845f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8846{
8847#ifdef FEAT_INS_EXPAND
8848 if (pum_visible())
8849 rettv->vval.v_number = 1;
8850#endif
8851}
8852
8853#ifdef FEAT_PYTHON3
8854/*
8855 * "py3eval()" function
8856 */
8857 static void
8858f_py3eval(typval_T *argvars, typval_T *rettv)
8859{
8860 char_u *str;
8861 char_u buf[NUMBUFLEN];
8862
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008863 if (check_restricted() || check_secure())
8864 return;
8865
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008866 if (p_pyx == 0)
8867 p_pyx = 3;
8868
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008869 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 do_py3eval(str, rettv);
8871}
8872#endif
8873
8874#ifdef FEAT_PYTHON
8875/*
8876 * "pyeval()" function
8877 */
8878 static void
8879f_pyeval(typval_T *argvars, typval_T *rettv)
8880{
8881 char_u *str;
8882 char_u buf[NUMBUFLEN];
8883
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008884 if (check_restricted() || check_secure())
8885 return;
8886
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008887 if (p_pyx == 0)
8888 p_pyx = 2;
8889
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008890 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008891 do_pyeval(str, rettv);
8892}
8893#endif
8894
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008895#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8896/*
8897 * "pyxeval()" function
8898 */
8899 static void
8900f_pyxeval(typval_T *argvars, typval_T *rettv)
8901{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008902 if (check_restricted() || check_secure())
8903 return;
8904
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008905# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8906 init_pyxversion();
8907 if (p_pyx == 2)
8908 f_pyeval(argvars, rettv);
8909 else
8910 f_py3eval(argvars, rettv);
8911# elif defined(FEAT_PYTHON)
8912 f_pyeval(argvars, rettv);
8913# elif defined(FEAT_PYTHON3)
8914 f_py3eval(argvars, rettv);
8915# endif
8916}
8917#endif
8918
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919/*
8920 * "range()" function
8921 */
8922 static void
8923f_range(typval_T *argvars, typval_T *rettv)
8924{
8925 varnumber_T start;
8926 varnumber_T end;
8927 varnumber_T stride = 1;
8928 varnumber_T i;
8929 int error = FALSE;
8930
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008931 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 if (argvars[1].v_type == VAR_UNKNOWN)
8933 {
8934 end = start - 1;
8935 start = 0;
8936 }
8937 else
8938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008939 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008941 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942 }
8943
8944 if (error)
8945 return; /* type error; errmsg already given */
8946 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008947 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008949 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 else
8951 {
8952 if (rettv_list_alloc(rettv) == OK)
8953 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8954 if (list_append_number(rettv->vval.v_list,
8955 (varnumber_T)i) == FAIL)
8956 break;
8957 }
8958}
8959
8960/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008961 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008962 */
8963 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008964readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008965{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008966 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008967 typval_T save_val;
8968 typval_T rettv;
8969 typval_T argv[2];
8970 int retval = 0;
8971 int error = FALSE;
8972
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008973 if (expr->v_type == VAR_UNKNOWN)
8974 return 1;
8975
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008976 prepare_vimvar(VV_VAL, &save_val);
8977 set_vim_var_string(VV_VAL, name, -1);
8978 argv[0].v_type = VAR_STRING;
8979 argv[0].vval.v_string = name;
8980
8981 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8982 goto theend;
8983
8984 retval = tv_get_number_chk(&rettv, &error);
8985 if (error)
8986 retval = -1;
8987 clear_tv(&rettv);
8988
8989theend:
8990 set_vim_var_string(VV_VAL, NULL, 0);
8991 restore_vimvar(VV_VAL, &save_val);
8992 return retval;
8993}
8994
8995/*
8996 * "readdir()" function
8997 */
8998 static void
8999f_readdir(typval_T *argvars, typval_T *rettv)
9000{
9001 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009002 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009003 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009004 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009005 garray_T ga;
9006 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009007
9008 if (rettv_list_alloc(rettv) == FAIL)
9009 return;
9010 path = tv_get_string(&argvars[0]);
9011 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009012
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009013 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9014 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009015 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009016 for (i = 0; i < ga.ga_len; i++)
9017 {
9018 p = ((char_u **)ga.ga_data)[i];
9019 list_append_string(rettv->vval.v_list, p, -1);
9020 }
9021 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009022 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009023}
9024
9025/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 * "readfile()" function
9027 */
9028 static void
9029f_readfile(typval_T *argvars, typval_T *rettv)
9030{
9031 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009032 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 int failed = FALSE;
9034 char_u *fname;
9035 FILE *fd;
9036 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9037 int io_size = sizeof(buf);
9038 int readlen; /* size of last fread() */
9039 char_u *prev = NULL; /* previously read bytes, if any */
9040 long prevlen = 0; /* length of data in prev */
9041 long prevsize = 0; /* size of prev buffer */
9042 long maxline = MAXLNUM;
9043 long cnt = 0;
9044 char_u *p; /* position in buf */
9045 char_u *start; /* start of current line */
9046
9047 if (argvars[1].v_type != VAR_UNKNOWN)
9048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009049 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009051 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9052 blob = TRUE;
9053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009055 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 }
9057
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009058 if (blob)
9059 {
9060 if (rettv_blob_alloc(rettv) == FAIL)
9061 return;
9062 }
9063 else
9064 {
9065 if (rettv_list_alloc(rettv) == FAIL)
9066 return;
9067 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068
9069 /* Always open the file in binary mode, library functions have a mind of
9070 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009071 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009074 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 return;
9076 }
9077
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009078 if (blob)
9079 {
9080 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009082 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009083 blob_free(rettv->vval.v_blob);
9084 }
9085 fclose(fd);
9086 return;
9087 }
9088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 while (cnt < maxline || maxline < 0)
9090 {
9091 readlen = (int)fread(buf, 1, io_size, fd);
9092
9093 /* This for loop processes what was read, but is also entered at end
9094 * of file so that either:
9095 * - an incomplete line gets written
9096 * - a "binary" file gets an empty line at the end if it ends in a
9097 * newline. */
9098 for (p = buf, start = buf;
9099 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9100 ++p)
9101 {
9102 if (*p == '\n' || readlen <= 0)
9103 {
9104 listitem_T *li;
9105 char_u *s = NULL;
9106 long_u len = p - start;
9107
9108 /* Finished a line. Remove CRs before NL. */
9109 if (readlen > 0 && !binary)
9110 {
9111 while (len > 0 && start[len - 1] == '\r')
9112 --len;
9113 /* removal may cross back to the "prev" string */
9114 if (len == 0)
9115 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9116 --prevlen;
9117 }
9118 if (prevlen == 0)
9119 s = vim_strnsave(start, (int)len);
9120 else
9121 {
9122 /* Change "prev" buffer to be the right size. This way
9123 * the bytes are only copied once, and very long lines are
9124 * allocated only once. */
9125 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9126 {
9127 mch_memmove(s + prevlen, start, len);
9128 s[prevlen + len] = NUL;
9129 prev = NULL; /* the list will own the string */
9130 prevlen = prevsize = 0;
9131 }
9132 }
9133 if (s == NULL)
9134 {
9135 do_outofmem_msg((long_u) prevlen + len + 1);
9136 failed = TRUE;
9137 break;
9138 }
9139
9140 if ((li = listitem_alloc()) == NULL)
9141 {
9142 vim_free(s);
9143 failed = TRUE;
9144 break;
9145 }
9146 li->li_tv.v_type = VAR_STRING;
9147 li->li_tv.v_lock = 0;
9148 li->li_tv.vval.v_string = s;
9149 list_append(rettv->vval.v_list, li);
9150
9151 start = p + 1; /* step over newline */
9152 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9153 break;
9154 }
9155 else if (*p == NUL)
9156 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9158 * when finding the BF and check the previous two bytes. */
9159 else if (*p == 0xbf && enc_utf8 && !binary)
9160 {
9161 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9162 * + 1, these may be in the "prev" string. */
9163 char_u back1 = p >= buf + 1 ? p[-1]
9164 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9165 char_u back2 = p >= buf + 2 ? p[-2]
9166 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9167 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9168
9169 if (back2 == 0xef && back1 == 0xbb)
9170 {
9171 char_u *dest = p - 2;
9172
9173 /* Usually a BOM is at the beginning of a file, and so at
9174 * the beginning of a line; then we can just step over it.
9175 */
9176 if (start == dest)
9177 start = p + 1;
9178 else
9179 {
9180 /* have to shuffle buf to close gap */
9181 int adjust_prevlen = 0;
9182
9183 if (dest < buf)
9184 {
9185 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9186 dest = buf;
9187 }
9188 if (readlen > p - buf + 1)
9189 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9190 readlen -= 3 - adjust_prevlen;
9191 prevlen -= adjust_prevlen;
9192 p = dest - 1;
9193 }
9194 }
9195 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 } /* for */
9197
9198 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9199 break;
9200 if (start < p)
9201 {
9202 /* There's part of a line in buf, store it in "prev". */
9203 if (p - start + prevlen >= prevsize)
9204 {
9205 /* need bigger "prev" buffer */
9206 char_u *newprev;
9207
9208 /* A common use case is ordinary text files and "prev" gets a
9209 * fragment of a line, so the first allocation is made
9210 * small, to avoid repeatedly 'allocing' large and
9211 * 'reallocing' small. */
9212 if (prevsize == 0)
9213 prevsize = (long)(p - start);
9214 else
9215 {
9216 long grow50pc = (prevsize * 3) / 2;
9217 long growmin = (long)((p - start) * 2 + prevlen);
9218 prevsize = grow50pc > growmin ? grow50pc : growmin;
9219 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009220 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 if (newprev == NULL)
9222 {
9223 do_outofmem_msg((long_u)prevsize);
9224 failed = TRUE;
9225 break;
9226 }
9227 prev = newprev;
9228 }
9229 /* Add the line part to end of "prev". */
9230 mch_memmove(prev + prevlen, start, p - start);
9231 prevlen += (long)(p - start);
9232 }
9233 } /* while */
9234
9235 /*
9236 * For a negative line count use only the lines at the end of the file,
9237 * free the rest.
9238 */
9239 if (!failed && maxline < 0)
9240 while (cnt > -maxline)
9241 {
9242 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9243 --cnt;
9244 }
9245
9246 if (failed)
9247 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009248 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009249 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009250 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009251 }
9252
9253 vim_free(prev);
9254 fclose(fd);
9255}
9256
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009257 static void
9258return_register(int regname, typval_T *rettv)
9259{
9260 char_u buf[2] = {0, 0};
9261
9262 buf[0] = (char_u)regname;
9263 rettv->v_type = VAR_STRING;
9264 rettv->vval.v_string = vim_strsave(buf);
9265}
9266
9267/*
9268 * "reg_executing()" function
9269 */
9270 static void
9271f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9272{
9273 return_register(reg_executing, rettv);
9274}
9275
9276/*
9277 * "reg_recording()" function
9278 */
9279 static void
9280f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9281{
9282 return_register(reg_recording, rettv);
9283}
9284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286/*
9287 * Convert a List to proftime_T.
9288 * Return FAIL when there is something wrong.
9289 */
9290 static int
9291list2proftime(typval_T *arg, proftime_T *tm)
9292{
9293 long n1, n2;
9294 int error = FALSE;
9295
9296 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9297 || arg->vval.v_list->lv_len != 2)
9298 return FAIL;
9299 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9300 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009301# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009302 tm->HighPart = n1;
9303 tm->LowPart = n2;
9304# else
9305 tm->tv_sec = n1;
9306 tm->tv_usec = n2;
9307# endif
9308 return error ? FAIL : OK;
9309}
9310#endif /* FEAT_RELTIME */
9311
9312/*
9313 * "reltime()" function
9314 */
9315 static void
9316f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9317{
9318#ifdef FEAT_RELTIME
9319 proftime_T res;
9320 proftime_T start;
9321
9322 if (argvars[0].v_type == VAR_UNKNOWN)
9323 {
9324 /* No arguments: get current time. */
9325 profile_start(&res);
9326 }
9327 else if (argvars[1].v_type == VAR_UNKNOWN)
9328 {
9329 if (list2proftime(&argvars[0], &res) == FAIL)
9330 return;
9331 profile_end(&res);
9332 }
9333 else
9334 {
9335 /* Two arguments: compute the difference. */
9336 if (list2proftime(&argvars[0], &start) == FAIL
9337 || list2proftime(&argvars[1], &res) == FAIL)
9338 return;
9339 profile_sub(&res, &start);
9340 }
9341
9342 if (rettv_list_alloc(rettv) == OK)
9343 {
9344 long n1, n2;
9345
Bram Moolenaar4f974752019-02-17 17:44:42 +01009346# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 n1 = res.HighPart;
9348 n2 = res.LowPart;
9349# else
9350 n1 = res.tv_sec;
9351 n2 = res.tv_usec;
9352# endif
9353 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9354 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9355 }
9356#endif
9357}
9358
9359#ifdef FEAT_FLOAT
9360/*
9361 * "reltimefloat()" function
9362 */
9363 static void
9364f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9365{
9366# ifdef FEAT_RELTIME
9367 proftime_T tm;
9368# endif
9369
9370 rettv->v_type = VAR_FLOAT;
9371 rettv->vval.v_float = 0;
9372# ifdef FEAT_RELTIME
9373 if (list2proftime(&argvars[0], &tm) == OK)
9374 rettv->vval.v_float = profile_float(&tm);
9375# endif
9376}
9377#endif
9378
9379/*
9380 * "reltimestr()" function
9381 */
9382 static void
9383f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9384{
9385#ifdef FEAT_RELTIME
9386 proftime_T tm;
9387#endif
9388
9389 rettv->v_type = VAR_STRING;
9390 rettv->vval.v_string = NULL;
9391#ifdef FEAT_RELTIME
9392 if (list2proftime(&argvars[0], &tm) == OK)
9393 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9394#endif
9395}
9396
9397#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009398 static void
9399make_connection(void)
9400{
9401 if (X_DISPLAY == NULL
9402# ifdef FEAT_GUI
9403 && !gui.in_use
9404# endif
9405 )
9406 {
9407 x_force_connect = TRUE;
9408 setup_term_clip();
9409 x_force_connect = FALSE;
9410 }
9411}
9412
9413 static int
9414check_connection(void)
9415{
9416 make_connection();
9417 if (X_DISPLAY == NULL)
9418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 return FAIL;
9421 }
9422 return OK;
9423}
9424#endif
9425
9426#ifdef FEAT_CLIENTSERVER
9427 static void
9428remote_common(typval_T *argvars, typval_T *rettv, int expr)
9429{
9430 char_u *server_name;
9431 char_u *keys;
9432 char_u *r = NULL;
9433 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009434 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009435# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436 HWND w;
9437# else
9438 Window w;
9439# endif
9440
9441 if (check_restricted() || check_secure())
9442 return;
9443
9444# ifdef FEAT_X11
9445 if (check_connection() == FAIL)
9446 return;
9447# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009448 if (argvars[2].v_type != VAR_UNKNOWN
9449 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009450 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 if (server_name == NULL)
9454 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009455 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009456# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009457 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009459 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9460 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009461# endif
9462 {
9463 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009465 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009466 vim_free(r);
9467 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009468 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009469 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 return;
9471 }
9472
9473 rettv->vval.v_string = r;
9474
9475 if (argvars[2].v_type != VAR_UNKNOWN)
9476 {
9477 dictitem_T v;
9478 char_u str[30];
9479 char_u *idvar;
9480
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009481 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009482 if (idvar != NULL && *idvar != NUL)
9483 {
9484 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9485 v.di_tv.v_type = VAR_STRING;
9486 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009488 vim_free(v.di_tv.vval.v_string);
9489 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 }
9491}
9492#endif
9493
9494/*
9495 * "remote_expr()" function
9496 */
9497 static void
9498f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9499{
9500 rettv->v_type = VAR_STRING;
9501 rettv->vval.v_string = NULL;
9502#ifdef FEAT_CLIENTSERVER
9503 remote_common(argvars, rettv, TRUE);
9504#endif
9505}
9506
9507/*
9508 * "remote_foreground()" function
9509 */
9510 static void
9511f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9512{
9513#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009514# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515 /* On Win32 it's done in this application. */
9516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518
9519 if (server_name != NULL)
9520 serverForeground(server_name);
9521 }
9522# else
9523 /* Send a foreground() expression to the server. */
9524 argvars[1].v_type = VAR_STRING;
9525 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9526 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009527 rettv->v_type = VAR_STRING;
9528 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 remote_common(argvars, rettv, TRUE);
9530 vim_free(argvars[1].vval.v_string);
9531# endif
9532#endif
9533}
9534
9535 static void
9536f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9537{
9538#ifdef FEAT_CLIENTSERVER
9539 dictitem_T v;
9540 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009541# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542 long_u n = 0;
9543# endif
9544 char_u *serverid;
9545
9546 if (check_restricted() || check_secure())
9547 {
9548 rettv->vval.v_number = -1;
9549 return;
9550 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009551 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 if (serverid == NULL)
9553 {
9554 rettv->vval.v_number = -1;
9555 return; /* type error; errmsg already given */
9556 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009557# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9559 if (n == 0)
9560 rettv->vval.v_number = -1;
9561 else
9562 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009563 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 rettv->vval.v_number = (s != NULL);
9565 }
9566# else
9567 if (check_connection() == FAIL)
9568 return;
9569
9570 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9571 serverStrToWin(serverid), &s);
9572# endif
9573
9574 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9575 {
9576 char_u *retvar;
9577
9578 v.di_tv.v_type = VAR_STRING;
9579 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009580 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009581 if (retvar != NULL)
9582 set_var(retvar, &v.di_tv, FALSE);
9583 vim_free(v.di_tv.vval.v_string);
9584 }
9585#else
9586 rettv->vval.v_number = -1;
9587#endif
9588}
9589
9590 static void
9591f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9592{
9593 char_u *r = NULL;
9594
9595#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009596 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597
9598 if (serverid != NULL && !check_restricted() && !check_secure())
9599 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009600 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009601# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009602 /* The server's HWND is encoded in the 'id' parameter */
9603 long_u n = 0;
9604# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009605
9606 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009607 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009608
Bram Moolenaar4f974752019-02-17 17:44:42 +01009609# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9611 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009612 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009613 if (r == NULL)
9614# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009615 if (check_connection() == FAIL
9616 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9617 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009619 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620 }
9621#endif
9622 rettv->v_type = VAR_STRING;
9623 rettv->vval.v_string = r;
9624}
9625
9626/*
9627 * "remote_send()" function
9628 */
9629 static void
9630f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9631{
9632 rettv->v_type = VAR_STRING;
9633 rettv->vval.v_string = NULL;
9634#ifdef FEAT_CLIENTSERVER
9635 remote_common(argvars, rettv, FALSE);
9636#endif
9637}
9638
9639/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009640 * "remote_startserver()" function
9641 */
9642 static void
9643f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9644{
9645#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009646 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009647
9648 if (server == NULL)
9649 return; /* type error; errmsg already given */
9650 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009651 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009652 else
9653 {
9654# ifdef FEAT_X11
9655 if (check_connection() == OK)
9656 serverRegisterName(X_DISPLAY, server);
9657# else
9658 serverSetName(server);
9659# endif
9660 }
9661#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009662 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009663#endif
9664}
9665
9666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 * "remove()" function
9668 */
9669 static void
9670f_remove(typval_T *argvars, typval_T *rettv)
9671{
9672 list_T *l;
9673 listitem_T *item, *item2;
9674 listitem_T *li;
9675 long idx;
9676 long end;
9677 char_u *key;
9678 dict_T *d;
9679 dictitem_T *di;
9680 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009681 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682
9683 if (argvars[0].v_type == VAR_DICT)
9684 {
9685 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009686 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009687 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009688 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009690 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 if (key != NULL)
9692 {
9693 di = dict_find(d, key, -1);
9694 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009695 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9697 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9698 {
9699 *rettv = di->di_tv;
9700 init_tv(&di->di_tv);
9701 dictitem_remove(d, di);
9702 }
9703 }
9704 }
9705 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009706 else if (argvars[0].v_type == VAR_BLOB)
9707 {
9708 idx = (long)tv_get_number_chk(&argvars[1], &error);
9709 if (!error)
9710 {
9711 blob_T *b = argvars[0].vval.v_blob;
9712 int len = blob_len(b);
9713 char_u *p;
9714
9715 if (idx < 0)
9716 // count from the end
9717 idx = len + idx;
9718 if (idx < 0 || idx >= len)
9719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009720 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009721 return;
9722 }
9723 if (argvars[2].v_type == VAR_UNKNOWN)
9724 {
9725 // Remove one item, return its value.
9726 p = (char_u *)b->bv_ga.ga_data;
9727 rettv->vval.v_number = (varnumber_T) *(p + idx);
9728 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9729 --b->bv_ga.ga_len;
9730 }
9731 else
9732 {
9733 blob_T *blob;
9734
9735 // Remove range of items, return list with values.
9736 end = (long)tv_get_number_chk(&argvars[2], &error);
9737 if (error)
9738 return;
9739 if (end < 0)
9740 // count from the end
9741 end = len + end;
9742 if (end >= len || idx > end)
9743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009744 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009745 return;
9746 }
9747 blob = blob_alloc();
9748 if (blob == NULL)
9749 return;
9750 blob->bv_ga.ga_len = end - idx + 1;
9751 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9752 {
9753 vim_free(blob);
9754 return;
9755 }
9756 p = (char_u *)b->bv_ga.ga_data;
9757 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9758 (size_t)(end - idx + 1));
9759 ++blob->bv_refcount;
9760 rettv->v_type = VAR_BLOB;
9761 rettv->vval.v_blob = blob;
9762
9763 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9764 b->bv_ga.ga_len -= end - idx + 1;
9765 }
9766 }
9767 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009769 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009771 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009773 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009775 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009776 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009777 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009778 else
9779 {
9780 if (argvars[2].v_type == VAR_UNKNOWN)
9781 {
9782 /* Remove one item, return its value. */
9783 vimlist_remove(l, item, item);
9784 *rettv = item->li_tv;
9785 vim_free(item);
9786 }
9787 else
9788 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009789 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009790 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009792 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009793 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009794 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 else
9796 {
9797 int cnt = 0;
9798
9799 for (li = item; li != NULL; li = li->li_next)
9800 {
9801 ++cnt;
9802 if (li == item2)
9803 break;
9804 }
9805 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009806 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 else
9808 {
9809 vimlist_remove(l, item, item2);
9810 if (rettv_list_alloc(rettv) == OK)
9811 {
9812 l = rettv->vval.v_list;
9813 l->lv_first = item;
9814 l->lv_last = item2;
9815 item->li_prev = NULL;
9816 item2->li_next = NULL;
9817 l->lv_len = cnt;
9818 }
9819 }
9820 }
9821 }
9822 }
9823 }
9824}
9825
9826/*
9827 * "rename({from}, {to})" function
9828 */
9829 static void
9830f_rename(typval_T *argvars, typval_T *rettv)
9831{
9832 char_u buf[NUMBUFLEN];
9833
9834 if (check_restricted() || check_secure())
9835 rettv->vval.v_number = -1;
9836 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009837 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9838 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839}
9840
9841/*
9842 * "repeat()" function
9843 */
9844 static void
9845f_repeat(typval_T *argvars, typval_T *rettv)
9846{
9847 char_u *p;
9848 int n;
9849 int slen;
9850 int len;
9851 char_u *r;
9852 int i;
9853
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009854 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 if (argvars[0].v_type == VAR_LIST)
9856 {
9857 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9858 while (n-- > 0)
9859 if (list_extend(rettv->vval.v_list,
9860 argvars[0].vval.v_list, NULL) == FAIL)
9861 break;
9862 }
9863 else
9864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009865 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 rettv->v_type = VAR_STRING;
9867 rettv->vval.v_string = NULL;
9868
9869 slen = (int)STRLEN(p);
9870 len = slen * n;
9871 if (len <= 0)
9872 return;
9873
9874 r = alloc(len + 1);
9875 if (r != NULL)
9876 {
9877 for (i = 0; i < n; i++)
9878 mch_memmove(r + i * slen, p, (size_t)slen);
9879 r[len] = NUL;
9880 }
9881
9882 rettv->vval.v_string = r;
9883 }
9884}
9885
9886/*
9887 * "resolve()" function
9888 */
9889 static void
9890f_resolve(typval_T *argvars, typval_T *rettv)
9891{
9892 char_u *p;
9893#ifdef HAVE_READLINK
9894 char_u *buf = NULL;
9895#endif
9896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009897 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898#ifdef FEAT_SHORTCUT
9899 {
9900 char_u *v = NULL;
9901
Bram Moolenaardce1e892019-02-10 23:18:53 +01009902 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 if (v != NULL)
9904 rettv->vval.v_string = v;
9905 else
9906 rettv->vval.v_string = vim_strsave(p);
9907 }
9908#else
9909# ifdef HAVE_READLINK
9910 {
9911 char_u *cpy;
9912 int len;
9913 char_u *remain = NULL;
9914 char_u *q;
9915 int is_relative_to_current = FALSE;
9916 int has_trailing_pathsep = FALSE;
9917 int limit = 100;
9918
9919 p = vim_strsave(p);
9920
9921 if (p[0] == '.' && (vim_ispathsep(p[1])
9922 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9923 is_relative_to_current = TRUE;
9924
9925 len = STRLEN(p);
9926 if (len > 0 && after_pathsep(p, p + len))
9927 {
9928 has_trailing_pathsep = TRUE;
9929 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9930 }
9931
9932 q = getnextcomp(p);
9933 if (*q != NUL)
9934 {
9935 /* Separate the first path component in "p", and keep the
9936 * remainder (beginning with the path separator). */
9937 remain = vim_strsave(q - 1);
9938 q[-1] = NUL;
9939 }
9940
9941 buf = alloc(MAXPATHL + 1);
9942 if (buf == NULL)
9943 goto fail;
9944
9945 for (;;)
9946 {
9947 for (;;)
9948 {
9949 len = readlink((char *)p, (char *)buf, MAXPATHL);
9950 if (len <= 0)
9951 break;
9952 buf[len] = NUL;
9953
9954 if (limit-- == 0)
9955 {
9956 vim_free(p);
9957 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009958 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 rettv->vval.v_string = NULL;
9960 goto fail;
9961 }
9962
9963 /* Ensure that the result will have a trailing path separator
9964 * if the argument has one. */
9965 if (remain == NULL && has_trailing_pathsep)
9966 add_pathsep(buf);
9967
9968 /* Separate the first path component in the link value and
9969 * concatenate the remainders. */
9970 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9971 if (*q != NUL)
9972 {
9973 if (remain == NULL)
9974 remain = vim_strsave(q - 1);
9975 else
9976 {
9977 cpy = concat_str(q - 1, remain);
9978 if (cpy != NULL)
9979 {
9980 vim_free(remain);
9981 remain = cpy;
9982 }
9983 }
9984 q[-1] = NUL;
9985 }
9986
9987 q = gettail(p);
9988 if (q > p && *q == NUL)
9989 {
9990 /* Ignore trailing path separator. */
9991 q[-1] = NUL;
9992 q = gettail(p);
9993 }
9994 if (q > p && !mch_isFullName(buf))
9995 {
9996 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009997 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 if (cpy != NULL)
9999 {
10000 STRCPY(cpy, p);
10001 STRCPY(gettail(cpy), buf);
10002 vim_free(p);
10003 p = cpy;
10004 }
10005 }
10006 else
10007 {
10008 vim_free(p);
10009 p = vim_strsave(buf);
10010 }
10011 }
10012
10013 if (remain == NULL)
10014 break;
10015
10016 /* Append the first path component of "remain" to "p". */
10017 q = getnextcomp(remain + 1);
10018 len = q - remain - (*q != NUL);
10019 cpy = vim_strnsave(p, STRLEN(p) + len);
10020 if (cpy != NULL)
10021 {
10022 STRNCAT(cpy, remain, len);
10023 vim_free(p);
10024 p = cpy;
10025 }
10026 /* Shorten "remain". */
10027 if (*q != NUL)
10028 STRMOVE(remain, q - 1);
10029 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010030 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031 }
10032
10033 /* If the result is a relative path name, make it explicitly relative to
10034 * the current directory if and only if the argument had this form. */
10035 if (!vim_ispathsep(*p))
10036 {
10037 if (is_relative_to_current
10038 && *p != NUL
10039 && !(p[0] == '.'
10040 && (p[1] == NUL
10041 || vim_ispathsep(p[1])
10042 || (p[1] == '.'
10043 && (p[2] == NUL
10044 || vim_ispathsep(p[2]))))))
10045 {
10046 /* Prepend "./". */
10047 cpy = concat_str((char_u *)"./", p);
10048 if (cpy != NULL)
10049 {
10050 vim_free(p);
10051 p = cpy;
10052 }
10053 }
10054 else if (!is_relative_to_current)
10055 {
10056 /* Strip leading "./". */
10057 q = p;
10058 while (q[0] == '.' && vim_ispathsep(q[1]))
10059 q += 2;
10060 if (q > p)
10061 STRMOVE(p, p + 2);
10062 }
10063 }
10064
10065 /* Ensure that the result will have no trailing path separator
10066 * if the argument had none. But keep "/" or "//". */
10067 if (!has_trailing_pathsep)
10068 {
10069 q = p + STRLEN(p);
10070 if (after_pathsep(p, q))
10071 *gettail_sep(p) = NUL;
10072 }
10073
10074 rettv->vval.v_string = p;
10075 }
10076# else
10077 rettv->vval.v_string = vim_strsave(p);
10078# endif
10079#endif
10080
10081 simplify_filename(rettv->vval.v_string);
10082
10083#ifdef HAVE_READLINK
10084fail:
10085 vim_free(buf);
10086#endif
10087 rettv->v_type = VAR_STRING;
10088}
10089
10090/*
10091 * "reverse({list})" function
10092 */
10093 static void
10094f_reverse(typval_T *argvars, typval_T *rettv)
10095{
10096 list_T *l;
10097 listitem_T *li, *ni;
10098
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010099 if (argvars[0].v_type == VAR_BLOB)
10100 {
10101 blob_T *b = argvars[0].vval.v_blob;
10102 int i, len = blob_len(b);
10103
10104 for (i = 0; i < len / 2; i++)
10105 {
10106 int tmp = blob_get(b, i);
10107
10108 blob_set(b, i, blob_get(b, len - i - 1));
10109 blob_set(b, len - i - 1, tmp);
10110 }
10111 rettv_blob_set(rettv, b);
10112 return;
10113 }
10114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010116 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010117 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010118 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010119 (char_u *)N_("reverse() argument"), TRUE))
10120 {
10121 li = l->lv_last;
10122 l->lv_first = l->lv_last = NULL;
10123 l->lv_len = 0;
10124 while (li != NULL)
10125 {
10126 ni = li->li_prev;
10127 list_append(l, li);
10128 li = ni;
10129 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010130 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010131 l->lv_idx = l->lv_len - l->lv_idx - 1;
10132 }
10133}
10134
10135#define SP_NOMOVE 0x01 /* don't move cursor */
10136#define SP_REPEAT 0x02 /* repeat to find outer pair */
10137#define SP_RETCOUNT 0x04 /* return matchcount */
10138#define SP_SETPCMARK 0x08 /* set previous context mark */
10139#define SP_START 0x10 /* accept match at start position */
10140#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10141#define SP_END 0x40 /* leave cursor at end of match */
10142#define SP_COLUMN 0x80 /* start at cursor column */
10143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144/*
10145 * Get flags for a search function.
10146 * Possibly sets "p_ws".
10147 * Returns BACKWARD, FORWARD or zero (for an error).
10148 */
10149 static int
10150get_search_arg(typval_T *varp, int *flagsp)
10151{
10152 int dir = FORWARD;
10153 char_u *flags;
10154 char_u nbuf[NUMBUFLEN];
10155 int mask;
10156
10157 if (varp->v_type != VAR_UNKNOWN)
10158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010159 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 if (flags == NULL)
10161 return 0; /* type error; errmsg already given */
10162 while (*flags != NUL)
10163 {
10164 switch (*flags)
10165 {
10166 case 'b': dir = BACKWARD; break;
10167 case 'w': p_ws = TRUE; break;
10168 case 'W': p_ws = FALSE; break;
10169 default: mask = 0;
10170 if (flagsp != NULL)
10171 switch (*flags)
10172 {
10173 case 'c': mask = SP_START; break;
10174 case 'e': mask = SP_END; break;
10175 case 'm': mask = SP_RETCOUNT; break;
10176 case 'n': mask = SP_NOMOVE; break;
10177 case 'p': mask = SP_SUBPAT; break;
10178 case 'r': mask = SP_REPEAT; break;
10179 case 's': mask = SP_SETPCMARK; break;
10180 case 'z': mask = SP_COLUMN; break;
10181 }
10182 if (mask == 0)
10183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010184 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 dir = 0;
10186 }
10187 else
10188 *flagsp |= mask;
10189 }
10190 if (dir == 0)
10191 break;
10192 ++flags;
10193 }
10194 }
10195 return dir;
10196}
10197
10198/*
10199 * Shared by search() and searchpos() functions.
10200 */
10201 static int
10202search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10203{
10204 int flags;
10205 char_u *pat;
10206 pos_T pos;
10207 pos_T save_cursor;
10208 int save_p_ws = p_ws;
10209 int dir;
10210 int retval = 0; /* default: FAIL */
10211 long lnum_stop = 0;
10212 proftime_T tm;
10213#ifdef FEAT_RELTIME
10214 long time_limit = 0;
10215#endif
10216 int options = SEARCH_KEEP;
10217 int subpatnum;
10218
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010219 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010220 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10221 if (dir == 0)
10222 goto theend;
10223 flags = *flagsp;
10224 if (flags & SP_START)
10225 options |= SEARCH_START;
10226 if (flags & SP_END)
10227 options |= SEARCH_END;
10228 if (flags & SP_COLUMN)
10229 options |= SEARCH_COL;
10230
10231 /* Optional arguments: line number to stop searching and timeout. */
10232 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10233 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010234 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010235 if (lnum_stop < 0)
10236 goto theend;
10237#ifdef FEAT_RELTIME
10238 if (argvars[3].v_type != VAR_UNKNOWN)
10239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010240 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 if (time_limit < 0)
10242 goto theend;
10243 }
10244#endif
10245 }
10246
10247#ifdef FEAT_RELTIME
10248 /* Set the time limit, if there is one. */
10249 profile_setlimit(time_limit, &tm);
10250#endif
10251
10252 /*
10253 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10254 * Check to make sure only those flags are set.
10255 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10256 * flags cannot be set. Check for that condition also.
10257 */
10258 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10259 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010261 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 goto theend;
10263 }
10264
10265 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010266 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010267 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 if (subpatnum != FAIL)
10269 {
10270 if (flags & SP_SUBPAT)
10271 retval = subpatnum;
10272 else
10273 retval = pos.lnum;
10274 if (flags & SP_SETPCMARK)
10275 setpcmark();
10276 curwin->w_cursor = pos;
10277 if (match_pos != NULL)
10278 {
10279 /* Store the match cursor position */
10280 match_pos->lnum = pos.lnum;
10281 match_pos->col = pos.col + 1;
10282 }
10283 /* "/$" will put the cursor after the end of the line, may need to
10284 * correct that here */
10285 check_cursor();
10286 }
10287
10288 /* If 'n' flag is used: restore cursor position. */
10289 if (flags & SP_NOMOVE)
10290 curwin->w_cursor = save_cursor;
10291 else
10292 curwin->w_set_curswant = TRUE;
10293theend:
10294 p_ws = save_p_ws;
10295
10296 return retval;
10297}
10298
10299#ifdef FEAT_FLOAT
10300
10301/*
10302 * round() is not in C90, use ceil() or floor() instead.
10303 */
10304 float_T
10305vim_round(float_T f)
10306{
10307 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10308}
10309
10310/*
10311 * "round({float})" function
10312 */
10313 static void
10314f_round(typval_T *argvars, typval_T *rettv)
10315{
10316 float_T f = 0.0;
10317
10318 rettv->v_type = VAR_FLOAT;
10319 if (get_float_arg(argvars, &f) == OK)
10320 rettv->vval.v_float = vim_round(f);
10321 else
10322 rettv->vval.v_float = 0.0;
10323}
10324#endif
10325
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010326#ifdef FEAT_RUBY
10327/*
10328 * "rubyeval()" function
10329 */
10330 static void
10331f_rubyeval(typval_T *argvars, typval_T *rettv)
10332{
10333 char_u *str;
10334 char_u buf[NUMBUFLEN];
10335
10336 str = tv_get_string_buf(&argvars[0], buf);
10337 do_rubyeval(str, rettv);
10338}
10339#endif
10340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010341/*
10342 * "screenattr()" function
10343 */
10344 static void
10345f_screenattr(typval_T *argvars, typval_T *rettv)
10346{
10347 int row;
10348 int col;
10349 int c;
10350
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010351 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10352 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010353 if (row < 0 || row >= screen_Rows
10354 || col < 0 || col >= screen_Columns)
10355 c = -1;
10356 else
10357 c = ScreenAttrs[LineOffset[row] + col];
10358 rettv->vval.v_number = c;
10359}
10360
10361/*
10362 * "screenchar()" function
10363 */
10364 static void
10365f_screenchar(typval_T *argvars, typval_T *rettv)
10366{
10367 int row;
10368 int col;
10369 int off;
10370 int c;
10371
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010372 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10373 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010374 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010375 c = -1;
10376 else
10377 {
10378 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379 if (enc_utf8 && ScreenLinesUC[off] != 0)
10380 c = ScreenLinesUC[off];
10381 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010382 c = ScreenLines[off];
10383 }
10384 rettv->vval.v_number = c;
10385}
10386
10387/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010388 * "screenchars()" function
10389 */
10390 static void
10391f_screenchars(typval_T *argvars, typval_T *rettv)
10392{
10393 int row;
10394 int col;
10395 int off;
10396 int c;
10397 int i;
10398
10399 if (rettv_list_alloc(rettv) == FAIL)
10400 return;
10401 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10402 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10403 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10404 return;
10405
10406 off = LineOffset[row] + col;
10407 if (enc_utf8 && ScreenLinesUC[off] != 0)
10408 c = ScreenLinesUC[off];
10409 else
10410 c = ScreenLines[off];
10411 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10412
10413 if (enc_utf8)
10414
10415 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10416 list_append_number(rettv->vval.v_list,
10417 (varnumber_T)ScreenLinesC[i][off]);
10418}
10419
10420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010421 * "screencol()" function
10422 *
10423 * First column is 1 to be consistent with virtcol().
10424 */
10425 static void
10426f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10427{
10428 rettv->vval.v_number = screen_screencol() + 1;
10429}
10430
10431/*
10432 * "screenrow()" function
10433 */
10434 static void
10435f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10436{
10437 rettv->vval.v_number = screen_screenrow() + 1;
10438}
10439
10440/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010441 * "screenstring()" function
10442 */
10443 static void
10444f_screenstring(typval_T *argvars, typval_T *rettv)
10445{
10446 int row;
10447 int col;
10448 int off;
10449 int c;
10450 int i;
10451 char_u buf[MB_MAXBYTES + 1];
10452 int buflen = 0;
10453
10454 rettv->vval.v_string = NULL;
10455 rettv->v_type = VAR_STRING;
10456
10457 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10458 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10459 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10460 return;
10461
10462 off = LineOffset[row] + col;
10463 if (enc_utf8 && ScreenLinesUC[off] != 0)
10464 c = ScreenLinesUC[off];
10465 else
10466 c = ScreenLines[off];
10467 buflen += mb_char2bytes(c, buf);
10468
10469 if (enc_utf8)
10470 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10471 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10472
10473 buf[buflen] = NUL;
10474 rettv->vval.v_string = vim_strsave(buf);
10475}
10476
10477/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010478 * "search()" function
10479 */
10480 static void
10481f_search(typval_T *argvars, typval_T *rettv)
10482{
10483 int flags = 0;
10484
10485 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10486}
10487
10488/*
10489 * "searchdecl()" function
10490 */
10491 static void
10492f_searchdecl(typval_T *argvars, typval_T *rettv)
10493{
10494 int locally = 1;
10495 int thisblock = 0;
10496 int error = FALSE;
10497 char_u *name;
10498
10499 rettv->vval.v_number = 1; /* default: FAIL */
10500
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010501 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010502 if (argvars[1].v_type != VAR_UNKNOWN)
10503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010504 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010505 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010506 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010507 }
10508 if (!error && name != NULL)
10509 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10510 locally, thisblock, SEARCH_KEEP) == FAIL;
10511}
10512
10513/*
10514 * Used by searchpair() and searchpairpos()
10515 */
10516 static int
10517searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10518{
10519 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010520 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010521 int save_p_ws = p_ws;
10522 int dir;
10523 int flags = 0;
10524 char_u nbuf1[NUMBUFLEN];
10525 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010526 int retval = 0; /* default: FAIL */
10527 long lnum_stop = 0;
10528 long time_limit = 0;
10529
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010530 /* Get the three pattern arguments: start, middle, end. Will result in an
10531 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010532 spat = tv_get_string_chk(&argvars[0]);
10533 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10534 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010535 if (spat == NULL || mpat == NULL || epat == NULL)
10536 goto theend; /* type error */
10537
10538 /* Handle the optional fourth argument: flags */
10539 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10540 if (dir == 0)
10541 goto theend;
10542
10543 /* Don't accept SP_END or SP_SUBPAT.
10544 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10545 */
10546 if ((flags & (SP_END | SP_SUBPAT)) != 0
10547 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010549 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010550 goto theend;
10551 }
10552
10553 /* Using 'r' implies 'W', otherwise it doesn't work. */
10554 if (flags & SP_REPEAT)
10555 p_ws = FALSE;
10556
10557 /* Optional fifth argument: skip expression */
10558 if (argvars[3].v_type == VAR_UNKNOWN
10559 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010560 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010561 else
10562 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010563 skip = &argvars[4];
10564 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10565 && skip->v_type != VAR_STRING)
10566 {
10567 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010568 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010569 goto theend;
10570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010571 if (argvars[5].v_type != VAR_UNKNOWN)
10572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010573 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010574 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010576 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010578 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579#ifdef FEAT_RELTIME
10580 if (argvars[6].v_type != VAR_UNKNOWN)
10581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010582 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010583 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010585 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010587 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 }
10589#endif
10590 }
10591 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592
10593 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10594 match_pos, lnum_stop, time_limit);
10595
10596theend:
10597 p_ws = save_p_ws;
10598
10599 return retval;
10600}
10601
10602/*
10603 * "searchpair()" function
10604 */
10605 static void
10606f_searchpair(typval_T *argvars, typval_T *rettv)
10607{
10608 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10609}
10610
10611/*
10612 * "searchpairpos()" function
10613 */
10614 static void
10615f_searchpairpos(typval_T *argvars, typval_T *rettv)
10616{
10617 pos_T match_pos;
10618 int lnum = 0;
10619 int col = 0;
10620
10621 if (rettv_list_alloc(rettv) == FAIL)
10622 return;
10623
10624 if (searchpair_cmn(argvars, &match_pos) > 0)
10625 {
10626 lnum = match_pos.lnum;
10627 col = match_pos.col;
10628 }
10629
10630 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10631 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10632}
10633
10634/*
10635 * Search for a start/middle/end thing.
10636 * Used by searchpair(), see its documentation for the details.
10637 * Returns 0 or -1 for no match,
10638 */
10639 long
10640do_searchpair(
10641 char_u *spat, /* start pattern */
10642 char_u *mpat, /* middle pattern */
10643 char_u *epat, /* end pattern */
10644 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010645 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010646 int flags, /* SP_SETPCMARK and other SP_ values */
10647 pos_T *match_pos,
10648 linenr_T lnum_stop, /* stop at this line if not zero */
10649 long time_limit UNUSED) /* stop after this many msec */
10650{
10651 char_u *save_cpo;
10652 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10653 long retval = 0;
10654 pos_T pos;
10655 pos_T firstpos;
10656 pos_T foundpos;
10657 pos_T save_cursor;
10658 pos_T save_pos;
10659 int n;
10660 int r;
10661 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010662 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 int err;
10664 int options = SEARCH_KEEP;
10665 proftime_T tm;
10666
10667 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10668 save_cpo = p_cpo;
10669 p_cpo = empty_option;
10670
10671#ifdef FEAT_RELTIME
10672 /* Set the time limit, if there is one. */
10673 profile_setlimit(time_limit, &tm);
10674#endif
10675
10676 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10677 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010678 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
10679 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010680 if (pat2 == NULL || pat3 == NULL)
10681 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010682 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010683 if (*mpat == NUL)
10684 STRCPY(pat3, pat2);
10685 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010686 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 spat, epat, mpat);
10688 if (flags & SP_START)
10689 options |= SEARCH_START;
10690
Bram Moolenaar48570482017-10-30 21:48:41 +010010691 if (skip != NULL)
10692 {
10693 /* Empty string means to not use the skip expression. */
10694 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10695 use_skip = skip->vval.v_string != NULL
10696 && *skip->vval.v_string != NUL;
10697 }
10698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010699 save_cursor = curwin->w_cursor;
10700 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010701 CLEAR_POS(&firstpos);
10702 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 pat = pat3;
10704 for (;;)
10705 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010706 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010707 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010708 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010709 /* didn't find it or found the first match again: FAIL */
10710 break;
10711
10712 if (firstpos.lnum == 0)
10713 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010714 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010715 {
10716 /* Found the same position again. Can happen with a pattern that
10717 * has "\zs" at the end and searching backwards. Advance one
10718 * character and try again. */
10719 if (dir == BACKWARD)
10720 decl(&pos);
10721 else
10722 incl(&pos);
10723 }
10724 foundpos = pos;
10725
10726 /* clear the start flag to avoid getting stuck here */
10727 options &= ~SEARCH_START;
10728
10729 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010730 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010731 {
10732 save_pos = curwin->w_cursor;
10733 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010734 err = FALSE;
10735 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736 curwin->w_cursor = save_pos;
10737 if (err)
10738 {
10739 /* Evaluating {skip} caused an error, break here. */
10740 curwin->w_cursor = save_cursor;
10741 retval = -1;
10742 break;
10743 }
10744 if (r)
10745 continue;
10746 }
10747
10748 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10749 {
10750 /* Found end when searching backwards or start when searching
10751 * forward: nested pair. */
10752 ++nest;
10753 pat = pat2; /* nested, don't search for middle */
10754 }
10755 else
10756 {
10757 /* Found end when searching forward or start when searching
10758 * backward: end of (nested) pair; or found middle in outer pair. */
10759 if (--nest == 1)
10760 pat = pat3; /* outer level, search for middle */
10761 }
10762
10763 if (nest == 0)
10764 {
10765 /* Found the match: return matchcount or line number. */
10766 if (flags & SP_RETCOUNT)
10767 ++retval;
10768 else
10769 retval = pos.lnum;
10770 if (flags & SP_SETPCMARK)
10771 setpcmark();
10772 curwin->w_cursor = pos;
10773 if (!(flags & SP_REPEAT))
10774 break;
10775 nest = 1; /* search for next unmatched */
10776 }
10777 }
10778
10779 if (match_pos != NULL)
10780 {
10781 /* Store the match cursor position */
10782 match_pos->lnum = curwin->w_cursor.lnum;
10783 match_pos->col = curwin->w_cursor.col + 1;
10784 }
10785
10786 /* If 'n' flag is used or search failed: restore cursor position. */
10787 if ((flags & SP_NOMOVE) || retval == 0)
10788 curwin->w_cursor = save_cursor;
10789
10790theend:
10791 vim_free(pat2);
10792 vim_free(pat3);
10793 if (p_cpo == empty_option)
10794 p_cpo = save_cpo;
10795 else
10796 /* Darn, evaluating the {skip} expression changed the value. */
10797 free_string_option(save_cpo);
10798
10799 return retval;
10800}
10801
10802/*
10803 * "searchpos()" function
10804 */
10805 static void
10806f_searchpos(typval_T *argvars, typval_T *rettv)
10807{
10808 pos_T match_pos;
10809 int lnum = 0;
10810 int col = 0;
10811 int n;
10812 int flags = 0;
10813
10814 if (rettv_list_alloc(rettv) == FAIL)
10815 return;
10816
10817 n = search_cmn(argvars, &match_pos, &flags);
10818 if (n > 0)
10819 {
10820 lnum = match_pos.lnum;
10821 col = match_pos.col;
10822 }
10823
10824 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10825 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10826 if (flags & SP_SUBPAT)
10827 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10828}
10829
10830 static void
10831f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10832{
10833#ifdef FEAT_CLIENTSERVER
10834 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010835 char_u *server = tv_get_string_chk(&argvars[0]);
10836 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837
10838 rettv->vval.v_number = -1;
10839 if (server == NULL || reply == NULL)
10840 return;
10841 if (check_restricted() || check_secure())
10842 return;
10843# ifdef FEAT_X11
10844 if (check_connection() == FAIL)
10845 return;
10846# endif
10847
10848 if (serverSendReply(server, reply) < 0)
10849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010850 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 return;
10852 }
10853 rettv->vval.v_number = 0;
10854#else
10855 rettv->vval.v_number = -1;
10856#endif
10857}
10858
10859 static void
10860f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10861{
10862 char_u *r = NULL;
10863
10864#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010010865# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010866 r = serverGetVimNames();
10867# else
10868 make_connection();
10869 if (X_DISPLAY != NULL)
10870 r = serverGetVimNames(X_DISPLAY);
10871# endif
10872#endif
10873 rettv->v_type = VAR_STRING;
10874 rettv->vval.v_string = r;
10875}
10876
10877/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010878 * "setbufline()" function
10879 */
10880 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010881f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010882{
10883 linenr_T lnum;
10884 buf_T *buf;
10885
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010886 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010887 if (buf == NULL)
10888 rettv->vval.v_number = 1; /* FAIL */
10889 else
10890 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010891 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010892 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010893 }
10894}
10895
10896/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 * "setbufvar()" function
10898 */
10899 static void
10900f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10901{
10902 buf_T *buf;
10903 char_u *varname, *bufvarname;
10904 typval_T *varp;
10905 char_u nbuf[NUMBUFLEN];
10906
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010907 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010909 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10910 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010911 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 varp = &argvars[2];
10913
10914 if (buf != NULL && varname != NULL && varp != NULL)
10915 {
10916 if (*varname == '&')
10917 {
10918 long numval;
10919 char_u *strval;
10920 int error = FALSE;
10921 aco_save_T aco;
10922
10923 /* set curbuf to be our buf, temporarily */
10924 aucmd_prepbuf(&aco, buf);
10925
10926 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010927 numval = (long)tv_get_number_chk(varp, &error);
10928 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 if (!error && strval != NULL)
10930 set_option_value(varname, numval, strval, OPT_LOCAL);
10931
10932 /* reset notion of buffer */
10933 aucmd_restbuf(&aco);
10934 }
10935 else
10936 {
10937 buf_T *save_curbuf = curbuf;
10938
Bram Moolenaar964b3742019-05-24 18:54:09 +020010939 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 if (bufvarname != NULL)
10941 {
10942 curbuf = buf;
10943 STRCPY(bufvarname, "b:");
10944 STRCPY(bufvarname + 2, varname);
10945 set_var(bufvarname, varp, TRUE);
10946 vim_free(bufvarname);
10947 curbuf = save_curbuf;
10948 }
10949 }
10950 }
10951}
10952
10953 static void
10954f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10955{
10956 dict_T *d;
10957 dictitem_T *di;
10958 char_u *csearch;
10959
10960 if (argvars[0].v_type != VAR_DICT)
10961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010962 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963 return;
10964 }
10965
10966 if ((d = argvars[0].vval.v_dict) != NULL)
10967 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010968 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 if (csearch != NULL)
10970 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 if (enc_utf8)
10972 {
10973 int pcc[MAX_MCO];
10974 int c = utfc_ptr2char(csearch, pcc);
10975
10976 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10977 }
10978 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979 set_last_csearch(PTR2CHAR(csearch),
10980 csearch, MB_PTR2LEN(csearch));
10981 }
10982
10983 di = dict_find(d, (char_u *)"forward", -1);
10984 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010985 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 ? FORWARD : BACKWARD);
10987
10988 di = dict_find(d, (char_u *)"until", -1);
10989 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010990 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991 }
10992}
10993
10994/*
10995 * "setcmdpos()" function
10996 */
10997 static void
10998f_setcmdpos(typval_T *argvars, typval_T *rettv)
10999{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011000 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001
11002 if (pos >= 0)
11003 rettv->vval.v_number = set_cmdline_pos(pos);
11004}
11005
11006/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011007 * "setenv()" function
11008 */
11009 static void
11010f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11011{
11012 char_u namebuf[NUMBUFLEN];
11013 char_u valbuf[NUMBUFLEN];
11014 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11015
11016 if (argvars[1].v_type == VAR_SPECIAL
11017 && argvars[1].vval.v_number == VVAL_NULL)
11018 vim_unsetenv(name);
11019 else
11020 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11021}
11022
11023/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 * "setfperm({fname}, {mode})" function
11025 */
11026 static void
11027f_setfperm(typval_T *argvars, typval_T *rettv)
11028{
11029 char_u *fname;
11030 char_u modebuf[NUMBUFLEN];
11031 char_u *mode_str;
11032 int i;
11033 int mask;
11034 int mode = 0;
11035
11036 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011037 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011038 if (fname == NULL)
11039 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011040 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041 if (mode_str == NULL)
11042 return;
11043 if (STRLEN(mode_str) != 9)
11044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011045 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 return;
11047 }
11048
11049 mask = 1;
11050 for (i = 8; i >= 0; --i)
11051 {
11052 if (mode_str[i] != '-')
11053 mode |= mask;
11054 mask = mask << 1;
11055 }
11056 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11057}
11058
11059/*
11060 * "setline()" function
11061 */
11062 static void
11063f_setline(typval_T *argvars, typval_T *rettv)
11064{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011065 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066
Bram Moolenaarca851592018-06-06 21:04:07 +020011067 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068}
11069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070/*
11071 * Used by "setqflist()" and "setloclist()" functions
11072 */
11073 static void
11074set_qf_ll_list(
11075 win_T *wp UNUSED,
11076 typval_T *list_arg UNUSED,
11077 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011078 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 typval_T *rettv)
11080{
11081#ifdef FEAT_QUICKFIX
11082 static char *e_invact = N_("E927: Invalid action: '%s'");
11083 char_u *act;
11084 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011085 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011086#endif
11087
11088 rettv->vval.v_number = -1;
11089
11090#ifdef FEAT_QUICKFIX
11091 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011092 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011093 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011094 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011095 else
11096 {
11097 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011098 dict_T *d = NULL;
11099 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011100
11101 if (action_arg->v_type == VAR_STRING)
11102 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011103 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011104 if (act == NULL)
11105 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011106 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11107 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108 action = *act;
11109 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011110 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111 }
11112 else if (action_arg->v_type == VAR_UNKNOWN)
11113 action = ' ';
11114 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011115 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116
Bram Moolenaard823fa92016-08-12 16:29:27 +020011117 if (action_arg->v_type != VAR_UNKNOWN
11118 && what_arg->v_type != VAR_UNKNOWN)
11119 {
11120 if (what_arg->v_type == VAR_DICT)
11121 d = what_arg->vval.v_dict;
11122 else
11123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011124 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011125 valid_dict = FALSE;
11126 }
11127 }
11128
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011129 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011130 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011131 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11132 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011134 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011135 }
11136#endif
11137}
11138
11139/*
11140 * "setloclist()" function
11141 */
11142 static void
11143f_setloclist(typval_T *argvars, typval_T *rettv)
11144{
11145 win_T *win;
11146
11147 rettv->vval.v_number = -1;
11148
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011149 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011151 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011152}
11153
11154/*
11155 * "setmatches()" function
11156 */
11157 static void
11158f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11159{
11160#ifdef FEAT_SEARCH_EXTRA
11161 list_T *l;
11162 listitem_T *li;
11163 dict_T *d;
11164 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011165 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166
11167 rettv->vval.v_number = -1;
11168 if (argvars[0].v_type != VAR_LIST)
11169 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011170 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 return;
11172 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011173 if (win == NULL)
11174 return;
11175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 if ((l = argvars[0].vval.v_list) != NULL)
11177 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 /* To some extent make sure that we are dealing with a list from
11179 * "getmatches()". */
11180 li = l->lv_first;
11181 while (li != NULL)
11182 {
11183 if (li->li_tv.v_type != VAR_DICT
11184 || (d = li->li_tv.vval.v_dict) == NULL)
11185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011186 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011187 return;
11188 }
11189 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11190 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11191 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11192 && dict_find(d, (char_u *)"priority", -1) != NULL
11193 && dict_find(d, (char_u *)"id", -1) != NULL))
11194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011195 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011196 return;
11197 }
11198 li = li->li_next;
11199 }
11200
Bram Moolenaaraff74912019-03-30 18:11:49 +010011201 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011202 li = l->lv_first;
11203 while (li != NULL)
11204 {
11205 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011206 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011207 dictitem_T *di;
11208 char_u *group;
11209 int priority;
11210 int id;
11211 char_u *conceal;
11212
11213 d = li->li_tv.vval.v_dict;
11214 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11215 {
11216 if (s == NULL)
11217 {
11218 s = list_alloc();
11219 if (s == NULL)
11220 return;
11221 }
11222
11223 /* match from matchaddpos() */
11224 for (i = 1; i < 9; i++)
11225 {
11226 sprintf((char *)buf, (char *)"pos%d", i);
11227 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11228 {
11229 if (di->di_tv.v_type != VAR_LIST)
11230 return;
11231
11232 list_append_tv(s, &di->di_tv);
11233 s->lv_refcount++;
11234 }
11235 else
11236 break;
11237 }
11238 }
11239
Bram Moolenaar8f667172018-12-14 15:38:31 +010011240 group = dict_get_string(d, (char_u *)"group", TRUE);
11241 priority = (int)dict_get_number(d, (char_u *)"priority");
11242 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011243 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011244 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 : NULL;
11246 if (i == 0)
11247 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011248 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011249 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250 priority, id, NULL, conceal);
11251 }
11252 else
11253 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011254 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011255 list_unref(s);
11256 s = NULL;
11257 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011258 vim_free(group);
11259 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260
11261 li = li->li_next;
11262 }
11263 rettv->vval.v_number = 0;
11264 }
11265#endif
11266}
11267
11268/*
11269 * "setpos()" function
11270 */
11271 static void
11272f_setpos(typval_T *argvars, typval_T *rettv)
11273{
11274 pos_T pos;
11275 int fnum;
11276 char_u *name;
11277 colnr_T curswant = -1;
11278
11279 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011280 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011281 if (name != NULL)
11282 {
11283 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11284 {
11285 if (--pos.col < 0)
11286 pos.col = 0;
11287 if (name[0] == '.' && name[1] == NUL)
11288 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011289 /* set cursor; "fnum" is ignored */
11290 curwin->w_cursor = pos;
11291 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011292 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011293 curwin->w_curswant = curswant - 1;
11294 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011296 check_cursor();
11297 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011298 }
11299 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11300 {
11301 /* set mark */
11302 if (setmark_pos(name[1], &pos, fnum) == OK)
11303 rettv->vval.v_number = 0;
11304 }
11305 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011306 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011307 }
11308 }
11309}
11310
11311/*
11312 * "setqflist()" function
11313 */
11314 static void
11315f_setqflist(typval_T *argvars, typval_T *rettv)
11316{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011317 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011318}
11319
11320/*
11321 * "setreg()" function
11322 */
11323 static void
11324f_setreg(typval_T *argvars, typval_T *rettv)
11325{
11326 int regname;
11327 char_u *strregname;
11328 char_u *stropt;
11329 char_u *strval;
11330 int append;
11331 char_u yank_type;
11332 long block_len;
11333
11334 block_len = -1;
11335 yank_type = MAUTO;
11336 append = FALSE;
11337
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011338 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 rettv->vval.v_number = 1; /* FAIL is default */
11340
11341 if (strregname == NULL)
11342 return; /* type error; errmsg already given */
11343 regname = *strregname;
11344 if (regname == 0 || regname == '@')
11345 regname = '"';
11346
11347 if (argvars[2].v_type != VAR_UNKNOWN)
11348 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011349 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011350 if (stropt == NULL)
11351 return; /* type error */
11352 for (; *stropt != NUL; ++stropt)
11353 switch (*stropt)
11354 {
11355 case 'a': case 'A': /* append */
11356 append = TRUE;
11357 break;
11358 case 'v': case 'c': /* character-wise selection */
11359 yank_type = MCHAR;
11360 break;
11361 case 'V': case 'l': /* line-wise selection */
11362 yank_type = MLINE;
11363 break;
11364 case 'b': case Ctrl_V: /* block-wise selection */
11365 yank_type = MBLOCK;
11366 if (VIM_ISDIGIT(stropt[1]))
11367 {
11368 ++stropt;
11369 block_len = getdigits(&stropt) - 1;
11370 --stropt;
11371 }
11372 break;
11373 }
11374 }
11375
11376 if (argvars[1].v_type == VAR_LIST)
11377 {
11378 char_u **lstval;
11379 char_u **allocval;
11380 char_u buf[NUMBUFLEN];
11381 char_u **curval;
11382 char_u **curallocval;
11383 list_T *ll = argvars[1].vval.v_list;
11384 listitem_T *li;
11385 int len;
11386
11387 /* If the list is NULL handle like an empty list. */
11388 len = ll == NULL ? 0 : ll->lv_len;
11389
11390 /* First half: use for pointers to result lines; second half: use for
11391 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011392 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 if (lstval == NULL)
11394 return;
11395 curval = lstval;
11396 allocval = lstval + len + 2;
11397 curallocval = allocval;
11398
11399 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11400 li = li->li_next)
11401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011402 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011403 if (strval == NULL)
11404 goto free_lstval;
11405 if (strval == buf)
11406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011407 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011408 * overwrite the string. */
11409 strval = vim_strsave(buf);
11410 if (strval == NULL)
11411 goto free_lstval;
11412 *curallocval++ = strval;
11413 }
11414 *curval++ = strval;
11415 }
11416 *curval++ = NULL;
11417
11418 write_reg_contents_lst(regname, lstval, -1,
11419 append, yank_type, block_len);
11420free_lstval:
11421 while (curallocval > allocval)
11422 vim_free(*--curallocval);
11423 vim_free(lstval);
11424 }
11425 else
11426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011427 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011428 if (strval == NULL)
11429 return;
11430 write_reg_contents_ex(regname, strval, -1,
11431 append, yank_type, block_len);
11432 }
11433 rettv->vval.v_number = 0;
11434}
11435
11436/*
11437 * "settabvar()" function
11438 */
11439 static void
11440f_settabvar(typval_T *argvars, typval_T *rettv)
11441{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442 tabpage_T *save_curtab;
11443 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 char_u *varname, *tabvarname;
11445 typval_T *varp;
11446
11447 rettv->vval.v_number = 0;
11448
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011449 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011450 return;
11451
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011452 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11453 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011454 varp = &argvars[2];
11455
Bram Moolenaar4033c552017-09-16 20:54:51 +020011456 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458 save_curtab = curtab;
11459 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460
Bram Moolenaar964b3742019-05-24 18:54:09 +020011461 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011462 if (tabvarname != NULL)
11463 {
11464 STRCPY(tabvarname, "t:");
11465 STRCPY(tabvarname + 2, varname);
11466 set_var(tabvarname, varp, TRUE);
11467 vim_free(tabvarname);
11468 }
11469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470 /* Restore current tabpage */
11471 if (valid_tabpage(save_curtab))
11472 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 }
11474}
11475
11476/*
11477 * "settabwinvar()" function
11478 */
11479 static void
11480f_settabwinvar(typval_T *argvars, typval_T *rettv)
11481{
11482 setwinvar(argvars, rettv, 1);
11483}
11484
11485/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011486 * "settagstack()" function
11487 */
11488 static void
11489f_settagstack(typval_T *argvars, typval_T *rettv)
11490{
11491 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11492 win_T *wp;
11493 dict_T *d;
11494 int action = 'r';
11495
11496 rettv->vval.v_number = -1;
11497
11498 // first argument: window number or id
11499 wp = find_win_by_nr_or_id(&argvars[0]);
11500 if (wp == NULL)
11501 return;
11502
11503 // second argument: dict with items to set in the tag stack
11504 if (argvars[1].v_type != VAR_DICT)
11505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011506 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011507 return;
11508 }
11509 d = argvars[1].vval.v_dict;
11510 if (d == NULL)
11511 return;
11512
11513 // third argument: action - 'a' for append and 'r' for replace.
11514 // default is to replace the stack.
11515 if (argvars[2].v_type == VAR_UNKNOWN)
11516 action = 'r';
11517 else if (argvars[2].v_type == VAR_STRING)
11518 {
11519 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011520 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011521 if (actstr == NULL)
11522 return;
11523 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11524 action = *actstr;
11525 else
11526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011527 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011528 return;
11529 }
11530 }
11531 else
11532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011533 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011534 return;
11535 }
11536
11537 if (set_tagstack(wp, d, action) == OK)
11538 rettv->vval.v_number = 0;
11539}
11540
11541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 * "setwinvar()" function
11543 */
11544 static void
11545f_setwinvar(typval_T *argvars, typval_T *rettv)
11546{
11547 setwinvar(argvars, rettv, 0);
11548}
11549
11550#ifdef FEAT_CRYPT
11551/*
11552 * "sha256({string})" function
11553 */
11554 static void
11555f_sha256(typval_T *argvars, typval_T *rettv)
11556{
11557 char_u *p;
11558
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011559 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011560 rettv->vval.v_string = vim_strsave(
11561 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11562 rettv->v_type = VAR_STRING;
11563}
11564#endif /* FEAT_CRYPT */
11565
11566/*
11567 * "shellescape({string})" function
11568 */
11569 static void
11570f_shellescape(typval_T *argvars, typval_T *rettv)
11571{
Bram Moolenaar20615522017-06-05 18:46:26 +020011572 int do_special = non_zero_arg(&argvars[1]);
11573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011575 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 rettv->v_type = VAR_STRING;
11577}
11578
11579/*
11580 * shiftwidth() function
11581 */
11582 static void
11583f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11584{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011585 rettv->vval.v_number = 0;
11586
11587 if (argvars[0].v_type != VAR_UNKNOWN)
11588 {
11589 long col;
11590
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011591 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011592 if (col < 0)
11593 return; // type error; errmsg already given
11594#ifdef FEAT_VARTABS
11595 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11596 return;
11597#endif
11598 }
11599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 rettv->vval.v_number = get_sw_value(curbuf);
11601}
11602
11603/*
11604 * "simplify()" function
11605 */
11606 static void
11607f_simplify(typval_T *argvars, typval_T *rettv)
11608{
11609 char_u *p;
11610
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011611 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011612 rettv->vval.v_string = vim_strsave(p);
11613 simplify_filename(rettv->vval.v_string); /* simplify in place */
11614 rettv->v_type = VAR_STRING;
11615}
11616
11617#ifdef FEAT_FLOAT
11618/*
11619 * "sin()" function
11620 */
11621 static void
11622f_sin(typval_T *argvars, typval_T *rettv)
11623{
11624 float_T f = 0.0;
11625
11626 rettv->v_type = VAR_FLOAT;
11627 if (get_float_arg(argvars, &f) == OK)
11628 rettv->vval.v_float = sin(f);
11629 else
11630 rettv->vval.v_float = 0.0;
11631}
11632
11633/*
11634 * "sinh()" function
11635 */
11636 static void
11637f_sinh(typval_T *argvars, typval_T *rettv)
11638{
11639 float_T f = 0.0;
11640
11641 rettv->v_type = VAR_FLOAT;
11642 if (get_float_arg(argvars, &f) == OK)
11643 rettv->vval.v_float = sinh(f);
11644 else
11645 rettv->vval.v_float = 0.0;
11646}
11647#endif
11648
Bram Moolenaareae1b912019-05-09 15:12:55 +020011649static int item_compare(const void *s1, const void *s2);
11650static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011651
11652/* struct used in the array that's given to qsort() */
11653typedef struct
11654{
11655 listitem_T *item;
11656 int idx;
11657} sortItem_T;
11658
11659/* struct storing information about current sort */
11660typedef struct
11661{
11662 int item_compare_ic;
11663 int item_compare_numeric;
11664 int item_compare_numbers;
11665#ifdef FEAT_FLOAT
11666 int item_compare_float;
11667#endif
11668 char_u *item_compare_func;
11669 partial_T *item_compare_partial;
11670 dict_T *item_compare_selfdict;
11671 int item_compare_func_err;
11672 int item_compare_keep_zero;
11673} sortinfo_T;
11674static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675#define ITEM_COMPARE_FAIL 999
11676
11677/*
11678 * Compare functions for f_sort() and f_uniq() below.
11679 */
11680 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011681item_compare(const void *s1, const void *s2)
11682{
11683 sortItem_T *si1, *si2;
11684 typval_T *tv1, *tv2;
11685 char_u *p1, *p2;
11686 char_u *tofree1 = NULL, *tofree2 = NULL;
11687 int res;
11688 char_u numbuf1[NUMBUFLEN];
11689 char_u numbuf2[NUMBUFLEN];
11690
11691 si1 = (sortItem_T *)s1;
11692 si2 = (sortItem_T *)s2;
11693 tv1 = &si1->item->li_tv;
11694 tv2 = &si2->item->li_tv;
11695
11696 if (sortinfo->item_compare_numbers)
11697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011698 varnumber_T v1 = tv_get_number(tv1);
11699 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011700
11701 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11702 }
11703
11704#ifdef FEAT_FLOAT
11705 if (sortinfo->item_compare_float)
11706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011707 float_T v1 = tv_get_float(tv1);
11708 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011709
11710 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11711 }
11712#endif
11713
11714 /* tv2string() puts quotes around a string and allocates memory. Don't do
11715 * that for string variables. Use a single quote when comparing with a
11716 * non-string to do what the docs promise. */
11717 if (tv1->v_type == VAR_STRING)
11718 {
11719 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11720 p1 = (char_u *)"'";
11721 else
11722 p1 = tv1->vval.v_string;
11723 }
11724 else
11725 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11726 if (tv2->v_type == VAR_STRING)
11727 {
11728 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11729 p2 = (char_u *)"'";
11730 else
11731 p2 = tv2->vval.v_string;
11732 }
11733 else
11734 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11735 if (p1 == NULL)
11736 p1 = (char_u *)"";
11737 if (p2 == NULL)
11738 p2 = (char_u *)"";
11739 if (!sortinfo->item_compare_numeric)
11740 {
11741 if (sortinfo->item_compare_ic)
11742 res = STRICMP(p1, p2);
11743 else
11744 res = STRCMP(p1, p2);
11745 }
11746 else
11747 {
11748 double n1, n2;
11749 n1 = strtod((char *)p1, (char **)&p1);
11750 n2 = strtod((char *)p2, (char **)&p2);
11751 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11752 }
11753
11754 /* When the result would be zero, compare the item indexes. Makes the
11755 * sort stable. */
11756 if (res == 0 && !sortinfo->item_compare_keep_zero)
11757 res = si1->idx > si2->idx ? 1 : -1;
11758
11759 vim_free(tofree1);
11760 vim_free(tofree2);
11761 return res;
11762}
11763
11764 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011765item_compare2(const void *s1, const void *s2)
11766{
11767 sortItem_T *si1, *si2;
11768 int res;
11769 typval_T rettv;
11770 typval_T argv[3];
11771 int dummy;
11772 char_u *func_name;
11773 partial_T *partial = sortinfo->item_compare_partial;
11774
11775 /* shortcut after failure in previous call; compare all items equal */
11776 if (sortinfo->item_compare_func_err)
11777 return 0;
11778
11779 si1 = (sortItem_T *)s1;
11780 si2 = (sortItem_T *)s2;
11781
11782 if (partial == NULL)
11783 func_name = sortinfo->item_compare_func;
11784 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011785 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011786
11787 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11788 * in the copy without changing the original list items. */
11789 copy_tv(&si1->item->li_tv, &argv[0]);
11790 copy_tv(&si2->item->li_tv, &argv[1]);
11791
11792 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020011793 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794 partial, sortinfo->item_compare_selfdict);
11795 clear_tv(&argv[0]);
11796 clear_tv(&argv[1]);
11797
11798 if (res == FAIL)
11799 res = ITEM_COMPARE_FAIL;
11800 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011801 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011802 if (sortinfo->item_compare_func_err)
11803 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11804 clear_tv(&rettv);
11805
11806 /* When the result would be zero, compare the pointers themselves. Makes
11807 * the sort stable. */
11808 if (res == 0 && !sortinfo->item_compare_keep_zero)
11809 res = si1->idx > si2->idx ? 1 : -1;
11810
11811 return res;
11812}
11813
11814/*
11815 * "sort({list})" function
11816 */
11817 static void
11818do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11819{
11820 list_T *l;
11821 listitem_T *li;
11822 sortItem_T *ptrs;
11823 sortinfo_T *old_sortinfo;
11824 sortinfo_T info;
11825 long len;
11826 long i;
11827
11828 /* Pointer to current info struct used in compare function. Save and
11829 * restore the current one for nested calls. */
11830 old_sortinfo = sortinfo;
11831 sortinfo = &info;
11832
11833 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011834 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011835 else
11836 {
11837 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010011838 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11840 TRUE))
11841 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011842 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843
11844 len = list_len(l);
11845 if (len <= 1)
11846 goto theend; /* short list sorts pretty quickly */
11847
11848 info.item_compare_ic = FALSE;
11849 info.item_compare_numeric = FALSE;
11850 info.item_compare_numbers = FALSE;
11851#ifdef FEAT_FLOAT
11852 info.item_compare_float = FALSE;
11853#endif
11854 info.item_compare_func = NULL;
11855 info.item_compare_partial = NULL;
11856 info.item_compare_selfdict = NULL;
11857 if (argvars[1].v_type != VAR_UNKNOWN)
11858 {
11859 /* optional second argument: {func} */
11860 if (argvars[1].v_type == VAR_FUNC)
11861 info.item_compare_func = argvars[1].vval.v_string;
11862 else if (argvars[1].v_type == VAR_PARTIAL)
11863 info.item_compare_partial = argvars[1].vval.v_partial;
11864 else
11865 {
11866 int error = FALSE;
11867
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011868 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 if (error)
11870 goto theend; /* type error; errmsg already given */
11871 if (i == 1)
11872 info.item_compare_ic = TRUE;
11873 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011874 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 else if (i != 0)
11876 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011877 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 goto theend;
11879 }
11880 if (info.item_compare_func != NULL)
11881 {
11882 if (*info.item_compare_func == NUL)
11883 {
11884 /* empty string means default sort */
11885 info.item_compare_func = NULL;
11886 }
11887 else if (STRCMP(info.item_compare_func, "n") == 0)
11888 {
11889 info.item_compare_func = NULL;
11890 info.item_compare_numeric = TRUE;
11891 }
11892 else if (STRCMP(info.item_compare_func, "N") == 0)
11893 {
11894 info.item_compare_func = NULL;
11895 info.item_compare_numbers = TRUE;
11896 }
11897#ifdef FEAT_FLOAT
11898 else if (STRCMP(info.item_compare_func, "f") == 0)
11899 {
11900 info.item_compare_func = NULL;
11901 info.item_compare_float = TRUE;
11902 }
11903#endif
11904 else if (STRCMP(info.item_compare_func, "i") == 0)
11905 {
11906 info.item_compare_func = NULL;
11907 info.item_compare_ic = TRUE;
11908 }
11909 }
11910 }
11911
11912 if (argvars[2].v_type != VAR_UNKNOWN)
11913 {
11914 /* optional third argument: {dict} */
11915 if (argvars[2].v_type != VAR_DICT)
11916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011917 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011918 goto theend;
11919 }
11920 info.item_compare_selfdict = argvars[2].vval.v_dict;
11921 }
11922 }
11923
11924 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011925 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011926 if (ptrs == NULL)
11927 goto theend;
11928
11929 i = 0;
11930 if (sort)
11931 {
11932 /* sort(): ptrs will be the list to sort */
11933 for (li = l->lv_first; li != NULL; li = li->li_next)
11934 {
11935 ptrs[i].item = li;
11936 ptrs[i].idx = i;
11937 ++i;
11938 }
11939
11940 info.item_compare_func_err = FALSE;
11941 info.item_compare_keep_zero = FALSE;
11942 /* test the compare function */
11943 if ((info.item_compare_func != NULL
11944 || info.item_compare_partial != NULL)
11945 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11946 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011947 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011948 else
11949 {
11950 /* Sort the array with item pointers. */
11951 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11952 info.item_compare_func == NULL
11953 && info.item_compare_partial == NULL
11954 ? item_compare : item_compare2);
11955
11956 if (!info.item_compare_func_err)
11957 {
11958 /* Clear the List and append the items in sorted order. */
11959 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11960 l->lv_len = 0;
11961 for (i = 0; i < len; ++i)
11962 list_append(l, ptrs[i].item);
11963 }
11964 }
11965 }
11966 else
11967 {
11968 int (*item_compare_func_ptr)(const void *, const void *);
11969
11970 /* f_uniq(): ptrs will be a stack of items to remove */
11971 info.item_compare_func_err = FALSE;
11972 info.item_compare_keep_zero = TRUE;
11973 item_compare_func_ptr = info.item_compare_func != NULL
11974 || info.item_compare_partial != NULL
11975 ? item_compare2 : item_compare;
11976
11977 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11978 li = li->li_next)
11979 {
11980 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11981 == 0)
11982 ptrs[i++].item = li;
11983 if (info.item_compare_func_err)
11984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011985 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011986 break;
11987 }
11988 }
11989
11990 if (!info.item_compare_func_err)
11991 {
11992 while (--i >= 0)
11993 {
11994 li = ptrs[i].item->li_next;
11995 ptrs[i].item->li_next = li->li_next;
11996 if (li->li_next != NULL)
11997 li->li_next->li_prev = ptrs[i].item;
11998 else
11999 l->lv_last = ptrs[i].item;
12000 list_fix_watch(l, li);
12001 listitem_free(li);
12002 l->lv_len--;
12003 }
12004 }
12005 }
12006
12007 vim_free(ptrs);
12008 }
12009theend:
12010 sortinfo = old_sortinfo;
12011}
12012
12013/*
12014 * "sort({list})" function
12015 */
12016 static void
12017f_sort(typval_T *argvars, typval_T *rettv)
12018{
12019 do_sort_uniq(argvars, rettv, TRUE);
12020}
12021
12022/*
12023 * "uniq({list})" function
12024 */
12025 static void
12026f_uniq(typval_T *argvars, typval_T *rettv)
12027{
12028 do_sort_uniq(argvars, rettv, FALSE);
12029}
12030
12031/*
12032 * "soundfold({word})" function
12033 */
12034 static void
12035f_soundfold(typval_T *argvars, typval_T *rettv)
12036{
12037 char_u *s;
12038
12039 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012040 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012041#ifdef FEAT_SPELL
12042 rettv->vval.v_string = eval_soundfold(s);
12043#else
12044 rettv->vval.v_string = vim_strsave(s);
12045#endif
12046}
12047
12048/*
12049 * "spellbadword()" function
12050 */
12051 static void
12052f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12053{
12054 char_u *word = (char_u *)"";
12055 hlf_T attr = HLF_COUNT;
12056 int len = 0;
12057
12058 if (rettv_list_alloc(rettv) == FAIL)
12059 return;
12060
12061#ifdef FEAT_SPELL
12062 if (argvars[0].v_type == VAR_UNKNOWN)
12063 {
12064 /* Find the start and length of the badly spelled word. */
12065 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12066 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012067 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012068 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012069 curwin->w_set_curswant = TRUE;
12070 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012071 }
12072 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012074 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012075 int capcol = -1;
12076
12077 if (str != NULL)
12078 {
12079 /* Check the argument for spelling. */
12080 while (*str != NUL)
12081 {
12082 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12083 if (attr != HLF_COUNT)
12084 {
12085 word = str;
12086 break;
12087 }
12088 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012089 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012090 }
12091 }
12092 }
12093#endif
12094
12095 list_append_string(rettv->vval.v_list, word, len);
12096 list_append_string(rettv->vval.v_list, (char_u *)(
12097 attr == HLF_SPB ? "bad" :
12098 attr == HLF_SPR ? "rare" :
12099 attr == HLF_SPL ? "local" :
12100 attr == HLF_SPC ? "caps" :
12101 ""), -1);
12102}
12103
12104/*
12105 * "spellsuggest()" function
12106 */
12107 static void
12108f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12109{
12110#ifdef FEAT_SPELL
12111 char_u *str;
12112 int typeerr = FALSE;
12113 int maxcount;
12114 garray_T ga;
12115 int i;
12116 listitem_T *li;
12117 int need_capital = FALSE;
12118#endif
12119
12120 if (rettv_list_alloc(rettv) == FAIL)
12121 return;
12122
12123#ifdef FEAT_SPELL
12124 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012126 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012127 if (argvars[1].v_type != VAR_UNKNOWN)
12128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012129 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012130 if (maxcount <= 0)
12131 return;
12132 if (argvars[2].v_type != VAR_UNKNOWN)
12133 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012134 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012135 if (typeerr)
12136 return;
12137 }
12138 }
12139 else
12140 maxcount = 25;
12141
12142 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12143
12144 for (i = 0; i < ga.ga_len; ++i)
12145 {
12146 str = ((char_u **)ga.ga_data)[i];
12147
12148 li = listitem_alloc();
12149 if (li == NULL)
12150 vim_free(str);
12151 else
12152 {
12153 li->li_tv.v_type = VAR_STRING;
12154 li->li_tv.v_lock = 0;
12155 li->li_tv.vval.v_string = str;
12156 list_append(rettv->vval.v_list, li);
12157 }
12158 }
12159 ga_clear(&ga);
12160 }
12161#endif
12162}
12163
12164 static void
12165f_split(typval_T *argvars, typval_T *rettv)
12166{
12167 char_u *str;
12168 char_u *end;
12169 char_u *pat = NULL;
12170 regmatch_T regmatch;
12171 char_u patbuf[NUMBUFLEN];
12172 char_u *save_cpo;
12173 int match;
12174 colnr_T col = 0;
12175 int keepempty = FALSE;
12176 int typeerr = FALSE;
12177
12178 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12179 save_cpo = p_cpo;
12180 p_cpo = (char_u *)"";
12181
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012182 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012183 if (argvars[1].v_type != VAR_UNKNOWN)
12184 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012185 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012186 if (pat == NULL)
12187 typeerr = TRUE;
12188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012189 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012190 }
12191 if (pat == NULL || *pat == NUL)
12192 pat = (char_u *)"[\\x01- ]\\+";
12193
12194 if (rettv_list_alloc(rettv) == FAIL)
12195 return;
12196 if (typeerr)
12197 return;
12198
12199 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12200 if (regmatch.regprog != NULL)
12201 {
12202 regmatch.rm_ic = FALSE;
12203 while (*str != NUL || keepempty)
12204 {
12205 if (*str == NUL)
12206 match = FALSE; /* empty item at the end */
12207 else
12208 match = vim_regexec_nl(&regmatch, str, col);
12209 if (match)
12210 end = regmatch.startp[0];
12211 else
12212 end = str + STRLEN(str);
12213 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12214 && *str != NUL && match && end < regmatch.endp[0]))
12215 {
12216 if (list_append_string(rettv->vval.v_list, str,
12217 (int)(end - str)) == FAIL)
12218 break;
12219 }
12220 if (!match)
12221 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012222 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012223 if (regmatch.endp[0] > str)
12224 col = 0;
12225 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012226 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012227 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012228 str = regmatch.endp[0];
12229 }
12230
12231 vim_regfree(regmatch.regprog);
12232 }
12233
12234 p_cpo = save_cpo;
12235}
12236
12237#ifdef FEAT_FLOAT
12238/*
12239 * "sqrt()" function
12240 */
12241 static void
12242f_sqrt(typval_T *argvars, typval_T *rettv)
12243{
12244 float_T f = 0.0;
12245
12246 rettv->v_type = VAR_FLOAT;
12247 if (get_float_arg(argvars, &f) == OK)
12248 rettv->vval.v_float = sqrt(f);
12249 else
12250 rettv->vval.v_float = 0.0;
12251}
12252
12253/*
12254 * "str2float()" function
12255 */
12256 static void
12257f_str2float(typval_T *argvars, typval_T *rettv)
12258{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012259 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012260 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261
Bram Moolenaar08243d22017-01-10 16:12:29 +010012262 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012263 p = skipwhite(p + 1);
12264 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012265 if (isneg)
12266 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 rettv->v_type = VAR_FLOAT;
12268}
12269#endif
12270
12271/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020012272 * "str2list()" function
12273 */
12274 static void
12275f_str2list(typval_T *argvars, typval_T *rettv)
12276{
12277 char_u *p;
12278 int utf8 = FALSE;
12279
12280 if (rettv_list_alloc(rettv) == FAIL)
12281 return;
12282
12283 if (argvars[1].v_type != VAR_UNKNOWN)
12284 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
12285
12286 p = tv_get_string(&argvars[0]);
12287
12288 if (has_mbyte || utf8)
12289 {
12290 int (*ptr2len)(char_u *);
12291 int (*ptr2char)(char_u *);
12292
12293 if (utf8 || enc_utf8)
12294 {
12295 ptr2len = utf_ptr2len;
12296 ptr2char = utf_ptr2char;
12297 }
12298 else
12299 {
12300 ptr2len = mb_ptr2len;
12301 ptr2char = mb_ptr2char;
12302 }
12303
12304 for ( ; *p != NUL; p += (*ptr2len)(p))
12305 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
12306 }
12307 else
12308 for ( ; *p != NUL; ++p)
12309 list_append_number(rettv->vval.v_list, *p);
12310}
12311
12312/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012313 * "str2nr()" function
12314 */
12315 static void
12316f_str2nr(typval_T *argvars, typval_T *rettv)
12317{
12318 int base = 10;
12319 char_u *p;
12320 varnumber_T n;
12321 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012322 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012323
12324 if (argvars[1].v_type != VAR_UNKNOWN)
12325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012326 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012327 if (base != 2 && base != 8 && base != 10 && base != 16)
12328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012329 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012330 return;
12331 }
12332 }
12333
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012334 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012335 isneg = (*p == '-');
12336 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012337 p = skipwhite(p + 1);
12338 switch (base)
12339 {
12340 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12341 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12342 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12343 default: what = 0;
12344 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020012345 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
12346 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010012347 if (isneg)
12348 rettv->vval.v_number = -n;
12349 else
12350 rettv->vval.v_number = n;
12351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352}
12353
12354#ifdef HAVE_STRFTIME
12355/*
12356 * "strftime({format}[, {time}])" function
12357 */
12358 static void
12359f_strftime(typval_T *argvars, typval_T *rettv)
12360{
12361 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020012362 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 struct tm *curtime;
12364 time_t seconds;
12365 char_u *p;
12366
12367 rettv->v_type = VAR_STRING;
12368
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012369 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370 if (argvars[1].v_type == VAR_UNKNOWN)
12371 seconds = time(NULL);
12372 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012373 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020012374 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012375 /* MSVC returns NULL for an invalid value of seconds. */
12376 if (curtime == NULL)
12377 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12378 else
12379 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012380 vimconv_T conv;
12381 char_u *enc;
12382
12383 conv.vc_type = CONV_NONE;
12384 enc = enc_locale();
12385 convert_setup(&conv, p_enc, enc);
12386 if (conv.vc_type != CONV_NONE)
12387 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012388 if (p != NULL)
12389 (void)strftime((char *)result_buf, sizeof(result_buf),
12390 (char *)p, curtime);
12391 else
12392 result_buf[0] = NUL;
12393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012394 if (conv.vc_type != CONV_NONE)
12395 vim_free(p);
12396 convert_setup(&conv, enc, p_enc);
12397 if (conv.vc_type != CONV_NONE)
12398 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12399 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012400 rettv->vval.v_string = vim_strsave(result_buf);
12401
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402 /* Release conversion descriptors */
12403 convert_setup(&conv, NULL, NULL);
12404 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012405 }
12406}
12407#endif
12408
12409/*
12410 * "strgetchar()" function
12411 */
12412 static void
12413f_strgetchar(typval_T *argvars, typval_T *rettv)
12414{
12415 char_u *str;
12416 int len;
12417 int error = FALSE;
12418 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012419 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012420
12421 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012422 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 if (str == NULL)
12424 return;
12425 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012426 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427 if (error)
12428 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012429
Bram Moolenaar13505972019-01-24 15:04:48 +010012430 while (charidx >= 0 && byteidx < len)
12431 {
12432 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012433 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012434 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12435 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012437 --charidx;
12438 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440}
12441
12442/*
12443 * "stridx()" function
12444 */
12445 static void
12446f_stridx(typval_T *argvars, typval_T *rettv)
12447{
12448 char_u buf[NUMBUFLEN];
12449 char_u *needle;
12450 char_u *haystack;
12451 char_u *save_haystack;
12452 char_u *pos;
12453 int start_idx;
12454
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012455 needle = tv_get_string_chk(&argvars[1]);
12456 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012457 rettv->vval.v_number = -1;
12458 if (needle == NULL || haystack == NULL)
12459 return; /* type error; errmsg already given */
12460
12461 if (argvars[2].v_type != VAR_UNKNOWN)
12462 {
12463 int error = FALSE;
12464
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012465 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466 if (error || start_idx >= (int)STRLEN(haystack))
12467 return;
12468 if (start_idx >= 0)
12469 haystack += start_idx;
12470 }
12471
12472 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12473 if (pos != NULL)
12474 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12475}
12476
12477/*
12478 * "string()" function
12479 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012480 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012481f_string(typval_T *argvars, typval_T *rettv)
12482{
12483 char_u *tofree;
12484 char_u numbuf[NUMBUFLEN];
12485
12486 rettv->v_type = VAR_STRING;
12487 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12488 get_copyID());
12489 /* Make a copy if we have a value but it's not in allocated memory. */
12490 if (rettv->vval.v_string != NULL && tofree == NULL)
12491 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12492}
12493
12494/*
12495 * "strlen()" function
12496 */
12497 static void
12498f_strlen(typval_T *argvars, typval_T *rettv)
12499{
12500 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012501 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012502}
12503
12504/*
12505 * "strchars()" function
12506 */
12507 static void
12508f_strchars(typval_T *argvars, typval_T *rettv)
12509{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012510 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012512 varnumber_T len = 0;
12513 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012514
12515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012516 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012518 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012519 else
12520 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12522 while (*s != NUL)
12523 {
12524 func_mb_ptr2char_adv(&s);
12525 ++len;
12526 }
12527 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012528 }
12529}
12530
12531/*
12532 * "strdisplaywidth()" function
12533 */
12534 static void
12535f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12536{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012537 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538 int col = 0;
12539
12540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012541 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542
12543 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12544}
12545
12546/*
12547 * "strwidth()" function
12548 */
12549 static void
12550f_strwidth(typval_T *argvars, typval_T *rettv)
12551{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012552 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553
Bram Moolenaar13505972019-01-24 15:04:48 +010012554 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555}
12556
12557/*
12558 * "strcharpart()" function
12559 */
12560 static void
12561f_strcharpart(typval_T *argvars, typval_T *rettv)
12562{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012563 char_u *p;
12564 int nchar;
12565 int nbyte = 0;
12566 int charlen;
12567 int len = 0;
12568 int slen;
12569 int error = FALSE;
12570
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012571 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012572 slen = (int)STRLEN(p);
12573
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012574 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575 if (!error)
12576 {
12577 if (nchar > 0)
12578 while (nchar > 0 && nbyte < slen)
12579 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012580 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 --nchar;
12582 }
12583 else
12584 nbyte = nchar;
12585 if (argvars[2].v_type != VAR_UNKNOWN)
12586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012587 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588 while (charlen > 0 && nbyte + len < slen)
12589 {
12590 int off = nbyte + len;
12591
12592 if (off < 0)
12593 len += 1;
12594 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012595 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 --charlen;
12597 }
12598 }
12599 else
12600 len = slen - nbyte; /* default: all bytes that are available. */
12601 }
12602
12603 /*
12604 * Only return the overlap between the specified part and the actual
12605 * string.
12606 */
12607 if (nbyte < 0)
12608 {
12609 len += nbyte;
12610 nbyte = 0;
12611 }
12612 else if (nbyte > slen)
12613 nbyte = slen;
12614 if (len < 0)
12615 len = 0;
12616 else if (nbyte + len > slen)
12617 len = slen - nbyte;
12618
12619 rettv->v_type = VAR_STRING;
12620 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621}
12622
12623/*
12624 * "strpart()" function
12625 */
12626 static void
12627f_strpart(typval_T *argvars, typval_T *rettv)
12628{
12629 char_u *p;
12630 int n;
12631 int len;
12632 int slen;
12633 int error = FALSE;
12634
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012635 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012636 slen = (int)STRLEN(p);
12637
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012638 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012639 if (error)
12640 len = 0;
12641 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012642 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012643 else
12644 len = slen - n; /* default len: all bytes that are available. */
12645
12646 /*
12647 * Only return the overlap between the specified part and the actual
12648 * string.
12649 */
12650 if (n < 0)
12651 {
12652 len += n;
12653 n = 0;
12654 }
12655 else if (n > slen)
12656 n = slen;
12657 if (len < 0)
12658 len = 0;
12659 else if (n + len > slen)
12660 len = slen - n;
12661
12662 rettv->v_type = VAR_STRING;
12663 rettv->vval.v_string = vim_strnsave(p + n, len);
12664}
12665
12666/*
12667 * "strridx()" function
12668 */
12669 static void
12670f_strridx(typval_T *argvars, typval_T *rettv)
12671{
12672 char_u buf[NUMBUFLEN];
12673 char_u *needle;
12674 char_u *haystack;
12675 char_u *rest;
12676 char_u *lastmatch = NULL;
12677 int haystack_len, end_idx;
12678
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012679 needle = tv_get_string_chk(&argvars[1]);
12680 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012681
12682 rettv->vval.v_number = -1;
12683 if (needle == NULL || haystack == NULL)
12684 return; /* type error; errmsg already given */
12685
12686 haystack_len = (int)STRLEN(haystack);
12687 if (argvars[2].v_type != VAR_UNKNOWN)
12688 {
12689 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012690 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691 if (end_idx < 0)
12692 return; /* can never find a match */
12693 }
12694 else
12695 end_idx = haystack_len;
12696
12697 if (*needle == NUL)
12698 {
12699 /* Empty string matches past the end. */
12700 lastmatch = haystack + end_idx;
12701 }
12702 else
12703 {
12704 for (rest = haystack; *rest != '\0'; ++rest)
12705 {
12706 rest = (char_u *)strstr((char *)rest, (char *)needle);
12707 if (rest == NULL || rest > haystack + end_idx)
12708 break;
12709 lastmatch = rest;
12710 }
12711 }
12712
12713 if (lastmatch == NULL)
12714 rettv->vval.v_number = -1;
12715 else
12716 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12717}
12718
12719/*
12720 * "strtrans()" function
12721 */
12722 static void
12723f_strtrans(typval_T *argvars, typval_T *rettv)
12724{
12725 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012726 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012727}
12728
12729/*
12730 * "submatch()" function
12731 */
12732 static void
12733f_submatch(typval_T *argvars, typval_T *rettv)
12734{
12735 int error = FALSE;
12736 int no;
12737 int retList = 0;
12738
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012739 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 if (error)
12741 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012742 if (no < 0 || no >= NSUBEXP)
12743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012744 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012745 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012746 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012748 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749 if (error)
12750 return;
12751
12752 if (retList == 0)
12753 {
12754 rettv->v_type = VAR_STRING;
12755 rettv->vval.v_string = reg_submatch(no);
12756 }
12757 else
12758 {
12759 rettv->v_type = VAR_LIST;
12760 rettv->vval.v_list = reg_submatch_list(no);
12761 }
12762}
12763
12764/*
12765 * "substitute()" function
12766 */
12767 static void
12768f_substitute(typval_T *argvars, typval_T *rettv)
12769{
12770 char_u patbuf[NUMBUFLEN];
12771 char_u subbuf[NUMBUFLEN];
12772 char_u flagsbuf[NUMBUFLEN];
12773
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012774 char_u *str = tv_get_string_chk(&argvars[0]);
12775 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012776 char_u *sub = NULL;
12777 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012778 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012780 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12781 expr = &argvars[2];
12782 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012783 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012784
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012785 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012786 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12787 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788 rettv->vval.v_string = NULL;
12789 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012790 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791}
12792
12793/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012794 * "swapinfo(swap_filename)" function
12795 */
12796 static void
12797f_swapinfo(typval_T *argvars, typval_T *rettv)
12798{
12799 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012800 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012801}
12802
12803/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012804 * "swapname(expr)" function
12805 */
12806 static void
12807f_swapname(typval_T *argvars, typval_T *rettv)
12808{
12809 buf_T *buf;
12810
12811 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010012812 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020012813 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12814 || buf->b_ml.ml_mfp->mf_fname == NULL)
12815 rettv->vval.v_string = NULL;
12816 else
12817 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12818}
12819
12820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821 * "synID(lnum, col, trans)" function
12822 */
12823 static void
12824f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12825{
12826 int id = 0;
12827#ifdef FEAT_SYN_HL
12828 linenr_T lnum;
12829 colnr_T col;
12830 int trans;
12831 int transerr = FALSE;
12832
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012833 lnum = tv_get_lnum(argvars); /* -1 on type error */
12834 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
12835 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836
12837 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12838 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12839 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12840#endif
12841
12842 rettv->vval.v_number = id;
12843}
12844
12845/*
12846 * "synIDattr(id, what [, mode])" function
12847 */
12848 static void
12849f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12850{
12851 char_u *p = NULL;
12852#ifdef FEAT_SYN_HL
12853 int id;
12854 char_u *what;
12855 char_u *mode;
12856 char_u modebuf[NUMBUFLEN];
12857 int modec;
12858
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012859 id = (int)tv_get_number(&argvars[0]);
12860 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861 if (argvars[2].v_type != VAR_UNKNOWN)
12862 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012863 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012864 modec = TOLOWER_ASC(mode[0]);
12865 if (modec != 't' && modec != 'c' && modec != 'g')
12866 modec = 0; /* replace invalid with current */
12867 }
12868 else
12869 {
12870#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12871 if (USE_24BIT)
12872 modec = 'g';
12873 else
12874#endif
12875 if (t_colors > 1)
12876 modec = 'c';
12877 else
12878 modec = 't';
12879 }
12880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881 switch (TOLOWER_ASC(what[0]))
12882 {
12883 case 'b':
12884 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12885 p = highlight_color(id, what, modec);
12886 else /* bold */
12887 p = highlight_has_attr(id, HL_BOLD, modec);
12888 break;
12889
12890 case 'f': /* fg[#] or font */
12891 p = highlight_color(id, what, modec);
12892 break;
12893
12894 case 'i':
12895 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12896 p = highlight_has_attr(id, HL_INVERSE, modec);
12897 else /* italic */
12898 p = highlight_has_attr(id, HL_ITALIC, modec);
12899 break;
12900
12901 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012902 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903 break;
12904
12905 case 'r': /* reverse */
12906 p = highlight_has_attr(id, HL_INVERSE, modec);
12907 break;
12908
12909 case 's':
12910 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12911 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012912 /* strikeout */
12913 else if (TOLOWER_ASC(what[1]) == 't' &&
12914 TOLOWER_ASC(what[2]) == 'r')
12915 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 else /* standout */
12917 p = highlight_has_attr(id, HL_STANDOUT, modec);
12918 break;
12919
12920 case 'u':
12921 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12922 /* underline */
12923 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12924 else
12925 /* undercurl */
12926 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12927 break;
12928 }
12929
12930 if (p != NULL)
12931 p = vim_strsave(p);
12932#endif
12933 rettv->v_type = VAR_STRING;
12934 rettv->vval.v_string = p;
12935}
12936
12937/*
12938 * "synIDtrans(id)" function
12939 */
12940 static void
12941f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12942{
12943 int id;
12944
12945#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012946 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947
12948 if (id > 0)
12949 id = syn_get_final_id(id);
12950 else
12951#endif
12952 id = 0;
12953
12954 rettv->vval.v_number = id;
12955}
12956
12957/*
12958 * "synconcealed(lnum, col)" function
12959 */
12960 static void
12961f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12962{
12963#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12964 linenr_T lnum;
12965 colnr_T col;
12966 int syntax_flags = 0;
12967 int cchar;
12968 int matchid = 0;
12969 char_u str[NUMBUFLEN];
12970#endif
12971
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012972 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973
12974#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012975 lnum = tv_get_lnum(argvars); /* -1 on type error */
12976 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977
12978 vim_memset(str, NUL, sizeof(str));
12979
12980 if (rettv_list_alloc(rettv) != FAIL)
12981 {
12982 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12983 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12984 && curwin->w_p_cole > 0)
12985 {
12986 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12987 syntax_flags = get_syntax_info(&matchid);
12988
12989 /* get the conceal character */
12990 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12991 {
12992 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012993 if (cchar == NUL && curwin->w_p_cole == 1)
12994 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012995 if (cchar != NUL)
12996 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012997 if (has_mbyte)
12998 (*mb_char2bytes)(cchar, str);
12999 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000 str[0] = cchar;
13001 }
13002 }
13003 }
13004
13005 list_append_number(rettv->vval.v_list,
13006 (syntax_flags & HL_CONCEAL) != 0);
13007 /* -1 to auto-determine strlen */
13008 list_append_string(rettv->vval.v_list, str, -1);
13009 list_append_number(rettv->vval.v_list, matchid);
13010 }
13011#endif
13012}
13013
13014/*
13015 * "synstack(lnum, col)" function
13016 */
13017 static void
13018f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13019{
13020#ifdef FEAT_SYN_HL
13021 linenr_T lnum;
13022 colnr_T col;
13023 int i;
13024 int id;
13025#endif
13026
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013027 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013028
13029#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013030 lnum = tv_get_lnum(argvars); /* -1 on type error */
13031 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032
13033 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13034 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13035 && rettv_list_alloc(rettv) != FAIL)
13036 {
13037 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13038 for (i = 0; ; ++i)
13039 {
13040 id = syn_get_stack_item(i);
13041 if (id < 0)
13042 break;
13043 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13044 break;
13045 }
13046 }
13047#endif
13048}
13049
13050 static void
13051get_cmd_output_as_rettv(
13052 typval_T *argvars,
13053 typval_T *rettv,
13054 int retlist)
13055{
13056 char_u *res = NULL;
13057 char_u *p;
13058 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059 int err = FALSE;
13060 FILE *fd;
13061 list_T *list = NULL;
13062 int flags = SHELL_SILENT;
13063
13064 rettv->v_type = VAR_STRING;
13065 rettv->vval.v_string = NULL;
13066 if (check_restricted() || check_secure())
13067 goto errret;
13068
13069 if (argvars[1].v_type != VAR_UNKNOWN)
13070 {
13071 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013072 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073 * command.
13074 */
13075 if ((infile = vim_tempname('i', TRUE)) == NULL)
13076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013077 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 goto errret;
13079 }
13080
13081 fd = mch_fopen((char *)infile, WRITEBIN);
13082 if (fd == NULL)
13083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013084 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013085 goto errret;
13086 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013087 if (argvars[1].v_type == VAR_NUMBER)
13088 {
13089 linenr_T lnum;
13090 buf_T *buf;
13091
13092 buf = buflist_findnr(argvars[1].vval.v_number);
13093 if (buf == NULL)
13094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013095 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013096 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013097 goto errret;
13098 }
13099
13100 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13101 {
13102 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13103 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13104 {
13105 err = TRUE;
13106 break;
13107 }
13108 if (putc(NL, fd) == EOF)
13109 {
13110 err = TRUE;
13111 break;
13112 }
13113 }
13114 }
13115 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 {
13117 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13118 err = TRUE;
13119 }
13120 else
13121 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013122 size_t len;
13123 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013125 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013126 if (p == NULL)
13127 {
13128 fclose(fd);
13129 goto errret; /* type error; errmsg already given */
13130 }
13131 len = STRLEN(p);
13132 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13133 err = TRUE;
13134 }
13135 if (fclose(fd) != 0)
13136 err = TRUE;
13137 if (err)
13138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013139 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013140 goto errret;
13141 }
13142 }
13143
13144 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13145 * echoes typeahead, that messes up the display. */
13146 if (!msg_silent)
13147 flags += SHELL_COOKED;
13148
13149 if (retlist)
13150 {
13151 int len;
13152 listitem_T *li;
13153 char_u *s = NULL;
13154 char_u *start;
13155 char_u *end;
13156 int i;
13157
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013158 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 if (res == NULL)
13160 goto errret;
13161
13162 list = list_alloc();
13163 if (list == NULL)
13164 goto errret;
13165
13166 for (i = 0; i < len; ++i)
13167 {
13168 start = res + i;
13169 while (i < len && res[i] != NL)
13170 ++i;
13171 end = res + i;
13172
Bram Moolenaar964b3742019-05-24 18:54:09 +020013173 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 if (s == NULL)
13175 goto errret;
13176
13177 for (p = s; start < end; ++p, ++start)
13178 *p = *start == NUL ? NL : *start;
13179 *p = NUL;
13180
13181 li = listitem_alloc();
13182 if (li == NULL)
13183 {
13184 vim_free(s);
13185 goto errret;
13186 }
13187 li->li_tv.v_type = VAR_STRING;
13188 li->li_tv.v_lock = 0;
13189 li->li_tv.vval.v_string = s;
13190 list_append(list, li);
13191 }
13192
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013193 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 list = NULL;
13195 }
13196 else
13197 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013198 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013199#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 /* translate <CR><NL> into <NL> */
13201 if (res != NULL)
13202 {
13203 char_u *s, *d;
13204
13205 d = res;
13206 for (s = res; *s; ++s)
13207 {
13208 if (s[0] == CAR && s[1] == NL)
13209 ++s;
13210 *d++ = *s;
13211 }
13212 *d = NUL;
13213 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214#endif
13215 rettv->vval.v_string = res;
13216 res = NULL;
13217 }
13218
13219errret:
13220 if (infile != NULL)
13221 {
13222 mch_remove(infile);
13223 vim_free(infile);
13224 }
13225 if (res != NULL)
13226 vim_free(res);
13227 if (list != NULL)
13228 list_free(list);
13229}
13230
13231/*
13232 * "system()" function
13233 */
13234 static void
13235f_system(typval_T *argvars, typval_T *rettv)
13236{
13237 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13238}
13239
13240/*
13241 * "systemlist()" function
13242 */
13243 static void
13244f_systemlist(typval_T *argvars, typval_T *rettv)
13245{
13246 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13247}
13248
13249/*
13250 * "tabpagebuflist()" function
13251 */
13252 static void
13253f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13254{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 tabpage_T *tp;
13256 win_T *wp = NULL;
13257
13258 if (argvars[0].v_type == VAR_UNKNOWN)
13259 wp = firstwin;
13260 else
13261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013262 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263 if (tp != NULL)
13264 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13265 }
13266 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13267 {
13268 for (; wp != NULL; wp = wp->w_next)
13269 if (list_append_number(rettv->vval.v_list,
13270 wp->w_buffer->b_fnum) == FAIL)
13271 break;
13272 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273}
13274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013275/*
13276 * "tabpagenr()" function
13277 */
13278 static void
13279f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13280{
13281 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013282 char_u *arg;
13283
13284 if (argvars[0].v_type != VAR_UNKNOWN)
13285 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013286 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013287 nr = 0;
13288 if (arg != NULL)
13289 {
13290 if (STRCMP(arg, "$") == 0)
13291 nr = tabpage_index(NULL) - 1;
13292 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013293 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 }
13295 }
13296 else
13297 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 rettv->vval.v_number = nr;
13299}
13300
13301
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302/*
13303 * Common code for tabpagewinnr() and winnr().
13304 */
13305 static int
13306get_winnr(tabpage_T *tp, typval_T *argvar)
13307{
13308 win_T *twin;
13309 int nr = 1;
13310 win_T *wp;
13311 char_u *arg;
13312
13313 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13314 if (argvar->v_type != VAR_UNKNOWN)
13315 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013316 int invalid_arg = FALSE;
13317
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013318 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 if (arg == NULL)
13320 nr = 0; /* type error; errmsg already given */
13321 else if (STRCMP(arg, "$") == 0)
13322 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13323 else if (STRCMP(arg, "#") == 0)
13324 {
13325 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13326 if (twin == NULL)
13327 nr = 0;
13328 }
13329 else
13330 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020013331 long count;
13332 char_u *endp;
13333
13334 // Extract the window count (if specified). e.g. winnr('3j')
13335 count = strtol((char *)arg, (char **)&endp, 10);
13336 if (count <= 0)
13337 count = 1; // if count is not specified, default to 1
13338 if (endp != NULL && *endp != '\0')
13339 {
13340 if (STRCMP(endp, "j") == 0)
13341 twin = win_vert_neighbor(tp, twin, FALSE, count);
13342 else if (STRCMP(endp, "k") == 0)
13343 twin = win_vert_neighbor(tp, twin, TRUE, count);
13344 else if (STRCMP(endp, "h") == 0)
13345 twin = win_horz_neighbor(tp, twin, TRUE, count);
13346 else if (STRCMP(endp, "l") == 0)
13347 twin = win_horz_neighbor(tp, twin, FALSE, count);
13348 else
13349 invalid_arg = TRUE;
13350 }
13351 else
13352 invalid_arg = TRUE;
13353 }
13354
13355 if (invalid_arg)
13356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013357 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013358 nr = 0;
13359 }
13360 }
13361
13362 if (nr > 0)
13363 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13364 wp != twin; wp = wp->w_next)
13365 {
13366 if (wp == NULL)
13367 {
13368 /* didn't find it in this tabpage */
13369 nr = 0;
13370 break;
13371 }
13372 ++nr;
13373 }
13374 return nr;
13375}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013376
13377/*
13378 * "tabpagewinnr()" function
13379 */
13380 static void
13381f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13382{
13383 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384 tabpage_T *tp;
13385
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013386 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387 if (tp == NULL)
13388 nr = 0;
13389 else
13390 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013391 rettv->vval.v_number = nr;
13392}
13393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394/*
13395 * "tagfiles()" function
13396 */
13397 static void
13398f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13399{
13400 char_u *fname;
13401 tagname_T tn;
13402 int first;
13403
13404 if (rettv_list_alloc(rettv) == FAIL)
13405 return;
13406 fname = alloc(MAXPATHL);
13407 if (fname == NULL)
13408 return;
13409
13410 for (first = TRUE; ; first = FALSE)
13411 if (get_tagfname(&tn, first, fname) == FAIL
13412 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13413 break;
13414 tagname_free(&tn);
13415 vim_free(fname);
13416}
13417
13418/*
13419 * "taglist()" function
13420 */
13421 static void
13422f_taglist(typval_T *argvars, typval_T *rettv)
13423{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013424 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013425 char_u *tag_pattern;
13426
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013427 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428
13429 rettv->vval.v_number = FALSE;
13430 if (*tag_pattern == NUL)
13431 return;
13432
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013433 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013434 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013436 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437}
13438
13439/*
13440 * "tempname()" function
13441 */
13442 static void
13443f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13444{
13445 static int x = 'A';
13446
13447 rettv->v_type = VAR_STRING;
13448 rettv->vval.v_string = vim_tempname(x, FALSE);
13449
13450 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13451 * names. Skip 'I' and 'O', they are used for shell redirection. */
13452 do
13453 {
13454 if (x == 'Z')
13455 x = '0';
13456 else if (x == '9')
13457 x = 'A';
13458 else
13459 {
13460#ifdef EBCDIC
13461 if (x == 'I')
13462 x = 'J';
13463 else if (x == 'R')
13464 x = 'S';
13465 else
13466#endif
13467 ++x;
13468 }
13469 } while (x == 'I' || x == 'O');
13470}
13471
13472#ifdef FEAT_FLOAT
13473/*
13474 * "tan()" function
13475 */
13476 static void
13477f_tan(typval_T *argvars, typval_T *rettv)
13478{
13479 float_T f = 0.0;
13480
13481 rettv->v_type = VAR_FLOAT;
13482 if (get_float_arg(argvars, &f) == OK)
13483 rettv->vval.v_float = tan(f);
13484 else
13485 rettv->vval.v_float = 0.0;
13486}
13487
13488/*
13489 * "tanh()" function
13490 */
13491 static void
13492f_tanh(typval_T *argvars, typval_T *rettv)
13493{
13494 float_T f = 0.0;
13495
13496 rettv->v_type = VAR_FLOAT;
13497 if (get_float_arg(argvars, &f) == OK)
13498 rettv->vval.v_float = tanh(f);
13499 else
13500 rettv->vval.v_float = 0.0;
13501}
13502#endif
13503
13504/*
13505 * "test_alloc_fail(id, countdown, repeat)" function
13506 */
13507 static void
13508f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13509{
13510 if (argvars[0].v_type != VAR_NUMBER
13511 || argvars[0].vval.v_number <= 0
13512 || argvars[1].v_type != VAR_NUMBER
13513 || argvars[1].vval.v_number < 0
13514 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013515 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 else
13517 {
13518 alloc_fail_id = argvars[0].vval.v_number;
13519 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013520 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 alloc_fail_countdown = argvars[1].vval.v_number;
13522 alloc_fail_repeat = argvars[2].vval.v_number;
13523 did_outofmem_msg = FALSE;
13524 }
13525}
13526
13527/*
13528 * "test_autochdir()"
13529 */
13530 static void
13531f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13532{
13533#if defined(FEAT_AUTOCHDIR)
13534 test_autochdir = TRUE;
13535#endif
13536}
13537
13538/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013539 * "test_feedinput()"
13540 */
13541 static void
13542f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13543{
13544#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013545 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013546
13547 if (val != NULL)
13548 {
13549 trash_input_buf();
13550 add_to_input_buf_csi(val, (int)STRLEN(val));
13551 }
13552#endif
13553}
13554
13555/*
Bram Moolenaareda65222019-05-16 20:29:44 +020013556 * "test_getvalue({name})" function
13557 */
13558 static void
13559f_test_getvalue(typval_T *argvars, typval_T *rettv)
13560{
13561 if (argvars[0].v_type != VAR_STRING)
13562 emsg(_(e_invarg));
13563 else
13564 {
13565 char_u *name = tv_get_string(&argvars[0]);
13566
13567 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
13568 rettv->vval.v_number = need_fileinfo;
13569 else
13570 semsg(_(e_invarg2), name);
13571 }
13572}
13573
13574/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013575 * "test_option_not_set({name})" function
13576 */
13577 static void
13578f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13579{
13580 char_u *name = (char_u *)"";
13581
13582 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013583 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013584 else
13585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013586 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013587 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013588 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013589 }
13590}
13591
13592/*
13593 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013594 */
13595 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013596f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013598 char_u *name = (char_u *)"";
13599 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013600 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013601
13602 if (argvars[0].v_type != VAR_STRING
13603 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013604 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013605 else
13606 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013607 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013608 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013609
13610 if (STRCMP(name, (char_u *)"redraw") == 0)
13611 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013612 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13613 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013614 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13615 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013616 else if (STRCMP(name, (char_u *)"starting") == 0)
13617 {
13618 if (val)
13619 {
13620 if (save_starting < 0)
13621 save_starting = starting;
13622 starting = 0;
13623 }
13624 else
13625 {
13626 starting = save_starting;
13627 save_starting = -1;
13628 }
13629 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013630 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13631 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013632 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
13633 no_query_mouse_for_testing = val;
Bram Moolenaaradc67142019-06-22 01:40:42 +020013634 else if (STRCMP(name, (char_u *)"no_wait_return") == 0)
13635 no_wait_return = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013636 else if (STRCMP(name, (char_u *)"ALL") == 0)
13637 {
13638 disable_char_avail_for_testing = FALSE;
13639 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013640 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013641 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020013642 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013643 if (save_starting >= 0)
13644 {
13645 starting = save_starting;
13646 save_starting = -1;
13647 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013648 }
13649 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013650 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013651 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013652}
13653
13654/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010013655 * "test_refcount({expr})" function
13656 */
13657 static void
13658f_test_refcount(typval_T *argvars, typval_T *rettv)
13659{
13660 int retval = -1;
13661
13662 switch (argvars[0].v_type)
13663 {
13664 case VAR_UNKNOWN:
13665 case VAR_NUMBER:
13666 case VAR_FLOAT:
13667 case VAR_SPECIAL:
13668 case VAR_STRING:
13669 break;
13670 case VAR_JOB:
13671#ifdef FEAT_JOB_CHANNEL
13672 if (argvars[0].vval.v_job != NULL)
13673 retval = argvars[0].vval.v_job->jv_refcount - 1;
13674#endif
13675 break;
13676 case VAR_CHANNEL:
13677#ifdef FEAT_JOB_CHANNEL
13678 if (argvars[0].vval.v_channel != NULL)
13679 retval = argvars[0].vval.v_channel->ch_refcount - 1;
13680#endif
13681 break;
13682 case VAR_FUNC:
13683 if (argvars[0].vval.v_string != NULL)
13684 {
13685 ufunc_T *fp;
13686
13687 fp = find_func(argvars[0].vval.v_string);
13688 if (fp != NULL)
13689 retval = fp->uf_refcount;
13690 }
13691 break;
13692 case VAR_PARTIAL:
13693 if (argvars[0].vval.v_partial != NULL)
13694 retval = argvars[0].vval.v_partial->pt_refcount - 1;
13695 break;
13696 case VAR_BLOB:
13697 if (argvars[0].vval.v_blob != NULL)
13698 retval = argvars[0].vval.v_blob->bv_refcount - 1;
13699 break;
13700 case VAR_LIST:
13701 if (argvars[0].vval.v_list != NULL)
13702 retval = argvars[0].vval.v_list->lv_refcount - 1;
13703 break;
13704 case VAR_DICT:
13705 if (argvars[0].vval.v_dict != NULL)
13706 retval = argvars[0].vval.v_dict->dv_refcount - 1;
13707 break;
13708 }
13709
13710 rettv->v_type = VAR_NUMBER;
13711 rettv->vval.v_number = retval;
13712
13713}
13714
13715/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013716 * "test_garbagecollect_now()" function
13717 */
13718 static void
13719f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13720{
13721 /* This is dangerous, any Lists and Dicts used internally may be freed
13722 * while still in use. */
13723 garbage_collect(TRUE);
13724}
13725
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013726/*
Bram Moolenaaradc67142019-06-22 01:40:42 +020013727 * "test_garbagecollect_soon()" function
13728 */
13729 static void
13730f_test_garbagecollect_soon(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13731{
13732 may_garbage_collect = TRUE;
13733}
13734
13735/*
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013736 * "test_ignore_error()" function
13737 */
13738 static void
13739f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13740{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013741 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013742}
13743
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013744 static void
13745f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13746{
13747 rettv->v_type = VAR_BLOB;
13748 rettv->vval.v_blob = NULL;
13749}
13750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013751#ifdef FEAT_JOB_CHANNEL
13752 static void
13753f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13754{
13755 rettv->v_type = VAR_CHANNEL;
13756 rettv->vval.v_channel = NULL;
13757}
13758#endif
13759
13760 static void
13761f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13762{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013763 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013764}
13765
13766#ifdef FEAT_JOB_CHANNEL
13767 static void
13768f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13769{
13770 rettv->v_type = VAR_JOB;
13771 rettv->vval.v_job = NULL;
13772}
13773#endif
13774
13775 static void
13776f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13777{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013778 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779}
13780
13781 static void
13782f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13783{
13784 rettv->v_type = VAR_PARTIAL;
13785 rettv->vval.v_partial = NULL;
13786}
13787
13788 static void
13789f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13790{
13791 rettv->v_type = VAR_STRING;
13792 rettv->vval.v_string = NULL;
13793}
13794
Bram Moolenaarab186732018-09-14 21:27:06 +020013795#ifdef FEAT_GUI
13796 static void
13797f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13798{
13799 char_u *which;
13800 long value;
13801 int dragging;
13802 scrollbar_T *sb = NULL;
13803
13804 if (argvars[0].v_type != VAR_STRING
13805 || (argvars[1].v_type) != VAR_NUMBER
13806 || (argvars[2].v_type) != VAR_NUMBER)
13807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013808 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013809 return;
13810 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013811 which = tv_get_string(&argvars[0]);
13812 value = tv_get_number(&argvars[1]);
13813 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013814
13815 if (STRCMP(which, "left") == 0)
13816 sb = &curwin->w_scrollbars[SBAR_LEFT];
13817 else if (STRCMP(which, "right") == 0)
13818 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13819 else if (STRCMP(which, "hor") == 0)
13820 sb = &gui.bottom_sbar;
13821 if (sb == NULL)
13822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013823 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013824 return;
13825 }
13826 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013827# ifndef USE_ON_FLY_SCROLL
13828 // need to loop through normal_cmd() to handle the scroll events
13829 exec_normal(FALSE, TRUE, FALSE);
13830# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013831}
13832#endif
13833
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013834#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013836f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
13837{
13838 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
13839 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
13840}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020013841#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020013842
13843 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013844f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13845{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013846 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013847}
13848
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849/*
13850 * Get a callback from "arg". It can be a Funcref or a function name.
13851 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013852 * "cb_name" is not allocated.
13853 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013854 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013855 callback_T
13856get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013858 callback_T res;
13859
13860 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013861 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13862 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013863 res.cb_partial = arg->vval.v_partial;
13864 ++res.cb_partial->pt_refcount;
13865 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013867 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013868 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013869 res.cb_partial = NULL;
13870 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
13871 {
13872 // Note that we don't make a copy of the string.
13873 res.cb_name = arg->vval.v_string;
13874 func_ref(res.cb_name);
13875 }
13876 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13877 {
13878 res.cb_name = (char_u *)"";
13879 }
13880 else
13881 {
13882 emsg(_("E921: Invalid callback argument"));
13883 res.cb_name = NULL;
13884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013885 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013886 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887}
13888
13889/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013890 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 */
13892 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013893put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013895 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013897 tv->v_type = VAR_PARTIAL;
13898 tv->vval.v_partial = cb->cb_partial;
13899 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013900 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020013901 else
13902 {
13903 tv->v_type = VAR_FUNC;
13904 tv->vval.v_string = vim_strsave(cb->cb_name);
13905 func_ref(cb->cb_name);
13906 }
13907}
13908
13909/*
13910 * Make a copy of "src" into "dest", allocating the function name if needed,
13911 * without incrementing the refcount.
13912 */
13913 void
13914set_callback(callback_T *dest, callback_T *src)
13915{
13916 if (src->cb_partial == NULL)
13917 {
13918 // just a function name, make a copy
13919 dest->cb_name = vim_strsave(src->cb_name);
13920 dest->cb_free_name = TRUE;
13921 }
13922 else
13923 {
13924 // cb_name is a pointer into cb_partial
13925 dest->cb_name = src->cb_name;
13926 dest->cb_free_name = FALSE;
13927 }
13928 dest->cb_partial = src->cb_partial;
13929}
13930
13931/*
13932 * Unref/free "callback" returned by get_callback() or set_callback().
13933 */
13934 void
13935free_callback(callback_T *callback)
13936{
13937 if (callback->cb_partial != NULL)
13938 {
13939 partial_unref(callback->cb_partial);
13940 callback->cb_partial = NULL;
13941 }
13942 else if (callback->cb_name != NULL)
13943 func_unref(callback->cb_name);
13944 if (callback->cb_free_name)
13945 {
13946 vim_free(callback->cb_name);
13947 callback->cb_free_name = FALSE;
13948 }
13949 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013950}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013951
13952#ifdef FEAT_TIMERS
13953/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013954 * "timer_info([timer])" function
13955 */
13956 static void
13957f_timer_info(typval_T *argvars, typval_T *rettv)
13958{
13959 timer_T *timer = NULL;
13960
13961 if (rettv_list_alloc(rettv) != OK)
13962 return;
13963 if (argvars[0].v_type != VAR_UNKNOWN)
13964 {
13965 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013966 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013967 else
13968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013969 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013970 if (timer != NULL)
13971 add_timer_info(rettv, timer);
13972 }
13973 }
13974 else
13975 add_timer_info_all(rettv);
13976}
13977
13978/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013979 * "timer_pause(timer, paused)" function
13980 */
13981 static void
13982f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13983{
13984 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013985 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013986
13987 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013988 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013989 else
13990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013991 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013992 if (timer != NULL)
13993 timer->tr_paused = paused;
13994 }
13995}
13996
13997/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013998 * "timer_start(time, callback [, options])" function
13999 */
14000 static void
14001f_timer_start(typval_T *argvars, typval_T *rettv)
14002{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014003 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014004 timer_T *timer;
14005 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014006 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014007 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014008
Bram Moolenaar75537a92016-09-05 22:45:28 +020014009 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014010 if (check_secure())
14011 return;
14012 if (argvars[2].v_type != VAR_UNKNOWN)
14013 {
14014 if (argvars[2].v_type != VAR_DICT
14015 || (dict = argvars[2].vval.v_dict) == NULL)
14016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014017 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014018 return;
14019 }
14020 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014021 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022 }
14023
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014024 callback = get_callback(&argvars[1]);
14025 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014026 return;
14027
14028 timer = create_timer(msec, repeat);
14029 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014030 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031 else
14032 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014033 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014034 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014035 }
14036}
14037
14038/*
14039 * "timer_stop(timer)" function
14040 */
14041 static void
14042f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14043{
14044 timer_T *timer;
14045
14046 if (argvars[0].v_type != VAR_NUMBER)
14047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014048 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 return;
14050 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014051 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014052 if (timer != NULL)
14053 stop_timer(timer);
14054}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014055
14056/*
14057 * "timer_stopall()" function
14058 */
14059 static void
14060f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14061{
14062 stop_all_timers();
14063}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014064#endif
14065
14066/*
14067 * "tolower(string)" function
14068 */
14069 static void
14070f_tolower(typval_T *argvars, typval_T *rettv)
14071{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014072 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014073 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014074}
14075
14076/*
14077 * "toupper(string)" function
14078 */
14079 static void
14080f_toupper(typval_T *argvars, typval_T *rettv)
14081{
14082 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014083 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084}
14085
14086/*
14087 * "tr(string, fromstr, tostr)" function
14088 */
14089 static void
14090f_tr(typval_T *argvars, typval_T *rettv)
14091{
14092 char_u *in_str;
14093 char_u *fromstr;
14094 char_u *tostr;
14095 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096 int inlen;
14097 int fromlen;
14098 int tolen;
14099 int idx;
14100 char_u *cpstr;
14101 int cplen;
14102 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014103 char_u buf[NUMBUFLEN];
14104 char_u buf2[NUMBUFLEN];
14105 garray_T ga;
14106
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014107 in_str = tv_get_string(&argvars[0]);
14108 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14109 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014110
14111 /* Default return value: empty string. */
14112 rettv->v_type = VAR_STRING;
14113 rettv->vval.v_string = NULL;
14114 if (fromstr == NULL || tostr == NULL)
14115 return; /* type error; errmsg already given */
14116 ga_init2(&ga, (int)sizeof(char), 80);
14117
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014118 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119 /* not multi-byte: fromstr and tostr must be the same length */
14120 if (STRLEN(fromstr) != STRLEN(tostr))
14121 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014122error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014123 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124 ga_clear(&ga);
14125 return;
14126 }
14127
14128 /* fromstr and tostr have to contain the same number of chars */
14129 while (*in_str != NUL)
14130 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014131 if (has_mbyte)
14132 {
14133 inlen = (*mb_ptr2len)(in_str);
14134 cpstr = in_str;
14135 cplen = inlen;
14136 idx = 0;
14137 for (p = fromstr; *p != NUL; p += fromlen)
14138 {
14139 fromlen = (*mb_ptr2len)(p);
14140 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14141 {
14142 for (p = tostr; *p != NUL; p += tolen)
14143 {
14144 tolen = (*mb_ptr2len)(p);
14145 if (idx-- == 0)
14146 {
14147 cplen = tolen;
14148 cpstr = p;
14149 break;
14150 }
14151 }
14152 if (*p == NUL) /* tostr is shorter than fromstr */
14153 goto error;
14154 break;
14155 }
14156 ++idx;
14157 }
14158
14159 if (first && cpstr == in_str)
14160 {
14161 /* Check that fromstr and tostr have the same number of
14162 * (multi-byte) characters. Done only once when a character
14163 * of in_str doesn't appear in fromstr. */
14164 first = FALSE;
14165 for (p = tostr; *p != NUL; p += tolen)
14166 {
14167 tolen = (*mb_ptr2len)(p);
14168 --idx;
14169 }
14170 if (idx != 0)
14171 goto error;
14172 }
14173
14174 (void)ga_grow(&ga, cplen);
14175 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14176 ga.ga_len += cplen;
14177
14178 in_str += inlen;
14179 }
14180 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014181 {
14182 /* When not using multi-byte chars we can do it faster. */
14183 p = vim_strchr(fromstr, *in_str);
14184 if (p != NULL)
14185 ga_append(&ga, tostr[p - fromstr]);
14186 else
14187 ga_append(&ga, *in_str);
14188 ++in_str;
14189 }
14190 }
14191
14192 /* add a terminating NUL */
14193 (void)ga_grow(&ga, 1);
14194 ga_append(&ga, NUL);
14195
14196 rettv->vval.v_string = ga.ga_data;
14197}
14198
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014199/*
14200 * "trim({expr})" function
14201 */
14202 static void
14203f_trim(typval_T *argvars, typval_T *rettv)
14204{
14205 char_u buf1[NUMBUFLEN];
14206 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014207 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014208 char_u *mask = NULL;
14209 char_u *tail;
14210 char_u *prev;
14211 char_u *p;
14212 int c1;
14213
14214 rettv->v_type = VAR_STRING;
14215 if (head == NULL)
14216 {
14217 rettv->vval.v_string = NULL;
14218 return;
14219 }
14220
14221 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014222 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014223
14224 while (*head != NUL)
14225 {
14226 c1 = PTR2CHAR(head);
14227 if (mask == NULL)
14228 {
14229 if (c1 > ' ' && c1 != 0xa0)
14230 break;
14231 }
14232 else
14233 {
14234 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14235 if (c1 == PTR2CHAR(p))
14236 break;
14237 if (*p == NUL)
14238 break;
14239 }
14240 MB_PTR_ADV(head);
14241 }
14242
14243 for (tail = head + STRLEN(head); tail > head; tail = prev)
14244 {
14245 prev = tail;
14246 MB_PTR_BACK(head, prev);
14247 c1 = PTR2CHAR(prev);
14248 if (mask == NULL)
14249 {
14250 if (c1 > ' ' && c1 != 0xa0)
14251 break;
14252 }
14253 else
14254 {
14255 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14256 if (c1 == PTR2CHAR(p))
14257 break;
14258 if (*p == NUL)
14259 break;
14260 }
14261 }
14262 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14263}
14264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014265#ifdef FEAT_FLOAT
14266/*
14267 * "trunc({float})" function
14268 */
14269 static void
14270f_trunc(typval_T *argvars, typval_T *rettv)
14271{
14272 float_T f = 0.0;
14273
14274 rettv->v_type = VAR_FLOAT;
14275 if (get_float_arg(argvars, &f) == OK)
14276 /* trunc() is not in C90, use floor() or ceil() instead. */
14277 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14278 else
14279 rettv->vval.v_float = 0.0;
14280}
14281#endif
14282
14283/*
14284 * "type(expr)" function
14285 */
14286 static void
14287f_type(typval_T *argvars, typval_T *rettv)
14288{
14289 int n = -1;
14290
14291 switch (argvars[0].v_type)
14292 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014293 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14294 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014295 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014296 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14297 case VAR_LIST: n = VAR_TYPE_LIST; break;
14298 case VAR_DICT: n = VAR_TYPE_DICT; break;
14299 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014300 case VAR_SPECIAL:
14301 if (argvars[0].vval.v_number == VVAL_FALSE
14302 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014303 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014304 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014305 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014306 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014307 case VAR_JOB: n = VAR_TYPE_JOB; break;
14308 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014309 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014310 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014311 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014312 n = -1;
14313 break;
14314 }
14315 rettv->vval.v_number = n;
14316}
14317
14318/*
14319 * "undofile(name)" function
14320 */
14321 static void
14322f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14323{
14324 rettv->v_type = VAR_STRING;
14325#ifdef FEAT_PERSISTENT_UNDO
14326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014327 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014328
14329 if (*fname == NUL)
14330 {
14331 /* If there is no file name there will be no undo file. */
14332 rettv->vval.v_string = NULL;
14333 }
14334 else
14335 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020014336 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014337
14338 if (ffname != NULL)
14339 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14340 vim_free(ffname);
14341 }
14342 }
14343#else
14344 rettv->vval.v_string = NULL;
14345#endif
14346}
14347
14348/*
14349 * "undotree()" function
14350 */
14351 static void
14352f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14353{
14354 if (rettv_dict_alloc(rettv) == OK)
14355 {
14356 dict_T *dict = rettv->vval.v_dict;
14357 list_T *list;
14358
Bram Moolenaare0be1672018-07-08 16:50:37 +020014359 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14360 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14361 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14362 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14363 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14364 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014365
14366 list = list_alloc();
14367 if (list != NULL)
14368 {
14369 u_eval_tree(curbuf->b_u_oldhead, list);
14370 dict_add_list(dict, "entries", list);
14371 }
14372 }
14373}
14374
14375/*
14376 * "values(dict)" function
14377 */
14378 static void
14379f_values(typval_T *argvars, typval_T *rettv)
14380{
14381 dict_list(argvars, rettv, 1);
14382}
14383
14384/*
14385 * "virtcol(string)" function
14386 */
14387 static void
14388f_virtcol(typval_T *argvars, typval_T *rettv)
14389{
14390 colnr_T vcol = 0;
14391 pos_T *fp;
14392 int fnum = curbuf->b_fnum;
14393
14394 fp = var2fpos(&argvars[0], FALSE, &fnum);
14395 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14396 && fnum == curbuf->b_fnum)
14397 {
14398 getvvcol(curwin, fp, NULL, NULL, &vcol);
14399 ++vcol;
14400 }
14401
14402 rettv->vval.v_number = vcol;
14403}
14404
14405/*
14406 * "visualmode()" function
14407 */
14408 static void
14409f_visualmode(typval_T *argvars, typval_T *rettv)
14410{
14411 char_u str[2];
14412
14413 rettv->v_type = VAR_STRING;
14414 str[0] = curbuf->b_visual_mode_eval;
14415 str[1] = NUL;
14416 rettv->vval.v_string = vim_strsave(str);
14417
14418 /* A non-zero number or non-empty string argument: reset mode. */
14419 if (non_zero_arg(&argvars[0]))
14420 curbuf->b_visual_mode_eval = NUL;
14421}
14422
14423/*
14424 * "wildmenumode()" function
14425 */
14426 static void
14427f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14428{
14429#ifdef FEAT_WILDMENU
14430 if (wild_menu_showing)
14431 rettv->vval.v_number = 1;
14432#endif
14433}
14434
14435/*
14436 * "winbufnr(nr)" function
14437 */
14438 static void
14439f_winbufnr(typval_T *argvars, typval_T *rettv)
14440{
14441 win_T *wp;
14442
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014443 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014444 if (wp == NULL)
14445 rettv->vval.v_number = -1;
14446 else
14447 rettv->vval.v_number = wp->w_buffer->b_fnum;
14448}
14449
14450/*
14451 * "wincol()" function
14452 */
14453 static void
14454f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14455{
14456 validate_cursor();
14457 rettv->vval.v_number = curwin->w_wcol + 1;
14458}
14459
14460/*
14461 * "winheight(nr)" function
14462 */
14463 static void
14464f_winheight(typval_T *argvars, typval_T *rettv)
14465{
14466 win_T *wp;
14467
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014468 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014469 if (wp == NULL)
14470 rettv->vval.v_number = -1;
14471 else
14472 rettv->vval.v_number = wp->w_height;
14473}
14474
14475/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014476 * "winlayout()" function
14477 */
14478 static void
14479f_winlayout(typval_T *argvars, typval_T *rettv)
14480{
14481 tabpage_T *tp;
14482
14483 if (rettv_list_alloc(rettv) != OK)
14484 return;
14485
14486 if (argvars[0].v_type == VAR_UNKNOWN)
14487 tp = curtab;
14488 else
14489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014490 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014491 if (tp == NULL)
14492 return;
14493 }
14494
14495 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14496}
14497
14498/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014499 * "winline()" function
14500 */
14501 static void
14502f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14503{
14504 validate_cursor();
14505 rettv->vval.v_number = curwin->w_wrow + 1;
14506}
14507
14508/*
14509 * "winnr()" function
14510 */
14511 static void
14512f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14513{
14514 int nr = 1;
14515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014516 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014517 rettv->vval.v_number = nr;
14518}
14519
14520/*
14521 * "winrestcmd()" function
14522 */
14523 static void
14524f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14525{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014526 win_T *wp;
14527 int winnr = 1;
14528 garray_T ga;
14529 char_u buf[50];
14530
14531 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014532 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014533 {
14534 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14535 ga_concat(&ga, buf);
14536 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14537 ga_concat(&ga, buf);
14538 ++winnr;
14539 }
14540 ga_append(&ga, NUL);
14541
14542 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014543 rettv->v_type = VAR_STRING;
14544}
14545
14546/*
14547 * "winrestview()" function
14548 */
14549 static void
14550f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14551{
14552 dict_T *dict;
14553
14554 if (argvars[0].v_type != VAR_DICT
14555 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014556 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014557 else
14558 {
14559 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014560 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014562 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014563 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014564 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014565 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14566 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014567 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568 curwin->w_set_curswant = FALSE;
14569 }
14570
14571 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014572 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014573#ifdef FEAT_DIFF
14574 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014575 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014576#endif
14577 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014578 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014579 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014580 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581
14582 check_cursor();
14583 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014584 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014585 changed_window_setting();
14586
14587 if (curwin->w_topline <= 0)
14588 curwin->w_topline = 1;
14589 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14590 curwin->w_topline = curbuf->b_ml.ml_line_count;
14591#ifdef FEAT_DIFF
14592 check_topfill(curwin, TRUE);
14593#endif
14594 }
14595}
14596
14597/*
14598 * "winsaveview()" function
14599 */
14600 static void
14601f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14602{
14603 dict_T *dict;
14604
14605 if (rettv_dict_alloc(rettv) == FAIL)
14606 return;
14607 dict = rettv->vval.v_dict;
14608
Bram Moolenaare0be1672018-07-08 16:50:37 +020014609 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14610 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020014611 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014612 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014613 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014614
Bram Moolenaare0be1672018-07-08 16:50:37 +020014615 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014617 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014619 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14620 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621}
14622
14623/*
14624 * "winwidth(nr)" function
14625 */
14626 static void
14627f_winwidth(typval_T *argvars, typval_T *rettv)
14628{
14629 win_T *wp;
14630
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014631 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014632 if (wp == NULL)
14633 rettv->vval.v_number = -1;
14634 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014636}
14637
14638/*
14639 * "wordcount()" function
14640 */
14641 static void
14642f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14643{
14644 if (rettv_dict_alloc(rettv) == FAIL)
14645 return;
14646 cursor_pos_info(rettv->vval.v_dict);
14647}
14648
14649/*
14650 * "writefile()" function
14651 */
14652 static void
14653f_writefile(typval_T *argvars, typval_T *rettv)
14654{
14655 int binary = FALSE;
14656 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014657#ifdef HAVE_FSYNC
14658 int do_fsync = p_fs;
14659#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660 char_u *fname;
14661 FILE *fd;
14662 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014663 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014664 list_T *list = NULL;
14665 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014667 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010014668 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014669 return;
14670
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014671 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014673 list = argvars[0].vval.v_list;
14674 if (list == NULL)
14675 return;
14676 for (li = list->lv_first; li != NULL; li = li->li_next)
14677 if (tv_get_string_chk(&li->li_tv) == NULL)
14678 return;
14679 }
14680 else if (argvars[0].v_type == VAR_BLOB)
14681 {
14682 blob = argvars[0].vval.v_blob;
14683 if (blob == NULL)
14684 return;
14685 }
14686 else
14687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014688 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689 return;
14690 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691
14692 if (argvars[2].v_type != VAR_UNKNOWN)
14693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014694 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014695
14696 if (arg2 == NULL)
14697 return;
14698 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014699 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014700 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014702#ifdef HAVE_FSYNC
14703 if (vim_strchr(arg2, 's') != NULL)
14704 do_fsync = TRUE;
14705 else if (vim_strchr(arg2, 'S') != NULL)
14706 do_fsync = FALSE;
14707#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708 }
14709
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014710 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014711 if (fname == NULL)
14712 return;
14713
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 /* Always open the file in binary mode, library functions have a mind of
14715 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14717 append ? APPENDBIN : WRITEBIN)) == NULL)
14718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014719 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720 ret = -1;
14721 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014722 else if (blob)
14723 {
14724 if (write_blob(fd, blob) == FAIL)
14725 ret = -1;
14726#ifdef HAVE_FSYNC
14727 else if (do_fsync)
14728 // Ignore the error, the user wouldn't know what to do about it.
14729 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010014730 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014731#endif
14732 fclose(fd);
14733 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014734 else
14735 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014736 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014737 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014738#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014739 else if (do_fsync)
14740 /* Ignore the error, the user wouldn't know what to do about it.
14741 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010014742 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014744 fclose(fd);
14745 }
14746
14747 rettv->vval.v_number = ret;
14748}
14749
14750/*
14751 * "xor(expr, expr)" function
14752 */
14753 static void
14754f_xor(typval_T *argvars, typval_T *rettv)
14755{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014756 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14757 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758}
14759
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760#endif /* FEAT_EVAL */