blob: f11756340614a73205549916df4c4bb233bf2bda [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaard0573012017-10-28 21:11:06 +020023#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020024# include <time.h> /* for time_t */
25#endif
26
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010027static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020029
30#ifdef FEAT_FLOAT
31static void f_abs(typval_T *argvars, typval_T *rettv);
32static void f_acos(typval_T *argvars, typval_T *rettv);
33#endif
34static void f_add(typval_T *argvars, typval_T *rettv);
35static void f_and(typval_T *argvars, typval_T *rettv);
36static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020037static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020038#ifdef FEAT_FLOAT
39static void f_asin(typval_T *argvars, typval_T *rettv);
40static void f_atan(typval_T *argvars, typval_T *rettv);
41static void f_atan2(typval_T *argvars, typval_T *rettv);
42#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010043#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020044static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010045static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010046# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010047static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010048# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010049#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_browse(typval_T *argvars, typval_T *rettv);
51static void f_browsedir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020052static void f_bufadd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020053static void f_bufexists(typval_T *argvars, typval_T *rettv);
54static void f_buflisted(typval_T *argvars, typval_T *rettv);
Bram Moolenaar15e248e2019-06-30 20:21:37 +020055static void f_bufload(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_bufloaded(typval_T *argvars, typval_T *rettv);
57static void f_bufname(typval_T *argvars, typval_T *rettv);
58static void f_bufnr(typval_T *argvars, typval_T *rettv);
59static void f_bufwinid(typval_T *argvars, typval_T *rettv);
60static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
61static void f_byte2line(typval_T *argvars, typval_T *rettv);
62static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
63static void f_byteidx(typval_T *argvars, typval_T *rettv);
64static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
65static void f_call(typval_T *argvars, typval_T *rettv);
66#ifdef FEAT_FLOAT
67static void f_ceil(typval_T *argvars, typval_T *rettv);
68#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020069static void f_changenr(typval_T *argvars, typval_T *rettv);
70static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +020071static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_cindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020073static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020074static void f_confirm(typval_T *argvars, typval_T *rettv);
75static void f_copy(typval_T *argvars, typval_T *rettv);
76#ifdef FEAT_FLOAT
77static void f_cos(typval_T *argvars, typval_T *rettv);
78static void f_cosh(typval_T *argvars, typval_T *rettv);
79#endif
80static void f_count(typval_T *argvars, typval_T *rettv);
81static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
82static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +010083#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +020084static void f_debugbreak(typval_T *argvars, typval_T *rettv);
85#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020086static void f_deepcopy(typval_T *argvars, typval_T *rettv);
87static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +020088static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_did_filetype(typval_T *argvars, typval_T *rettv);
90static void f_diff_filler(typval_T *argvars, typval_T *rettv);
91static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
92static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +020093static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020094static void f_escape(typval_T *argvars, typval_T *rettv);
95static void f_eval(typval_T *argvars, typval_T *rettv);
96static void f_eventhandler(typval_T *argvars, typval_T *rettv);
97static void f_executable(typval_T *argvars, typval_T *rettv);
98static void f_execute(typval_T *argvars, typval_T *rettv);
99static void f_exepath(typval_T *argvars, typval_T *rettv);
100static void f_exists(typval_T *argvars, typval_T *rettv);
101#ifdef FEAT_FLOAT
102static void f_exp(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200105static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200106static void f_extend(typval_T *argvars, typval_T *rettv);
107static void f_feedkeys(typval_T *argvars, typval_T *rettv);
108static void f_filereadable(typval_T *argvars, typval_T *rettv);
109static void f_filewritable(typval_T *argvars, typval_T *rettv);
110static void f_filter(typval_T *argvars, typval_T *rettv);
111static void f_finddir(typval_T *argvars, typval_T *rettv);
112static void f_findfile(typval_T *argvars, typval_T *rettv);
113#ifdef FEAT_FLOAT
114static void f_float2nr(typval_T *argvars, typval_T *rettv);
115static void f_floor(typval_T *argvars, typval_T *rettv);
116static void f_fmod(typval_T *argvars, typval_T *rettv);
117#endif
118static void f_fnameescape(typval_T *argvars, typval_T *rettv);
119static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
120static void f_foldclosed(typval_T *argvars, typval_T *rettv);
121static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
122static void f_foldlevel(typval_T *argvars, typval_T *rettv);
123static void f_foldtext(typval_T *argvars, typval_T *rettv);
124static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
125static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200126static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200127static void f_function(typval_T *argvars, typval_T *rettv);
128static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
129static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200130static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200131static void f_getbufline(typval_T *argvars, typval_T *rettv);
132static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100133static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200134static void f_getchar(typval_T *argvars, typval_T *rettv);
135static void f_getcharmod(typval_T *argvars, typval_T *rettv);
136static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
137static void f_getcmdline(typval_T *argvars, typval_T *rettv);
138#if defined(FEAT_CMDL_COMPL)
139static void f_getcompletion(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
142static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
143static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
144static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200145static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200146static void f_getfontname(typval_T *argvars, typval_T *rettv);
147static void f_getfperm(typval_T *argvars, typval_T *rettv);
148static void f_getfsize(typval_T *argvars, typval_T *rettv);
149static void f_getftime(typval_T *argvars, typval_T *rettv);
150static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100151static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200152static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200153static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200154static void f_getpid(typval_T *argvars, typval_T *rettv);
155static void f_getcurpos(typval_T *argvars, typval_T *rettv);
156static void f_getpos(typval_T *argvars, typval_T *rettv);
157static void f_getqflist(typval_T *argvars, typval_T *rettv);
158static void f_getreg(typval_T *argvars, typval_T *rettv);
159static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200160static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_gettabvar(typval_T *argvars, typval_T *rettv);
162static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100163static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100165static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200166static void f_getwinposx(typval_T *argvars, typval_T *rettv);
167static void f_getwinposy(typval_T *argvars, typval_T *rettv);
168static void f_getwinvar(typval_T *argvars, typval_T *rettv);
169static void f_glob(typval_T *argvars, typval_T *rettv);
170static void f_globpath(typval_T *argvars, typval_T *rettv);
171static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
172static void f_has(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
174static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_hlID(typval_T *argvars, typval_T *rettv);
176static void f_hlexists(typval_T *argvars, typval_T *rettv);
177static void f_hostname(typval_T *argvars, typval_T *rettv);
178static void f_iconv(typval_T *argvars, typval_T *rettv);
179static void f_indent(typval_T *argvars, typval_T *rettv);
180static void f_index(typval_T *argvars, typval_T *rettv);
181static void f_input(typval_T *argvars, typval_T *rettv);
182static void f_inputdialog(typval_T *argvars, typval_T *rettv);
183static void f_inputlist(typval_T *argvars, typval_T *rettv);
184static void f_inputrestore(typval_T *argvars, typval_T *rettv);
185static void f_inputsave(typval_T *argvars, typval_T *rettv);
186static void f_inputsecret(typval_T *argvars, typval_T *rettv);
187static void f_insert(typval_T *argvars, typval_T *rettv);
188static void f_invert(typval_T *argvars, typval_T *rettv);
189static void f_isdirectory(typval_T *argvars, typval_T *rettv);
190static void f_islocked(typval_T *argvars, typval_T *rettv);
191#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200192static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_isnan(typval_T *argvars, typval_T *rettv);
194#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200195static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
196static void f_len(typval_T *argvars, typval_T *rettv);
197static void f_libcall(typval_T *argvars, typval_T *rettv);
198static void f_libcallnr(typval_T *argvars, typval_T *rettv);
199static void f_line(typval_T *argvars, typval_T *rettv);
200static void f_line2byte(typval_T *argvars, typval_T *rettv);
201static void f_lispindent(typval_T *argvars, typval_T *rettv);
202static void f_localtime(typval_T *argvars, typval_T *rettv);
203#ifdef FEAT_FLOAT
204static void f_log(typval_T *argvars, typval_T *rettv);
205static void f_log10(typval_T *argvars, typval_T *rettv);
206#endif
207#ifdef FEAT_LUA
208static void f_luaeval(typval_T *argvars, typval_T *rettv);
209#endif
210static void f_map(typval_T *argvars, typval_T *rettv);
211static void f_maparg(typval_T *argvars, typval_T *rettv);
212static void f_mapcheck(typval_T *argvars, typval_T *rettv);
213static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_matchend(typval_T *argvars, typval_T *rettv);
215static void f_matchlist(typval_T *argvars, typval_T *rettv);
216static void f_matchstr(typval_T *argvars, typval_T *rettv);
217static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
218static void f_max(typval_T *argvars, typval_T *rettv);
219static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200220static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200221static void f_mode(typval_T *argvars, typval_T *rettv);
222#ifdef FEAT_MZSCHEME
223static void f_mzeval(typval_T *argvars, typval_T *rettv);
224#endif
225static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
226static void f_nr2char(typval_T *argvars, typval_T *rettv);
227static void f_or(typval_T *argvars, typval_T *rettv);
228static void f_pathshorten(typval_T *argvars, typval_T *rettv);
229#ifdef FEAT_PERL
230static void f_perleval(typval_T *argvars, typval_T *rettv);
231#endif
232#ifdef FEAT_FLOAT
233static void f_pow(typval_T *argvars, typval_T *rettv);
234#endif
235static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
236static void f_printf(typval_T *argvars, typval_T *rettv);
237static void f_pumvisible(typval_T *argvars, typval_T *rettv);
238#ifdef FEAT_PYTHON3
239static void f_py3eval(typval_T *argvars, typval_T *rettv);
240#endif
241#ifdef FEAT_PYTHON
242static void f_pyeval(typval_T *argvars, typval_T *rettv);
243#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100244#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
245static void f_pyxeval(typval_T *argvars, typval_T *rettv);
246#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200248static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200249static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200250static void f_reg_executing(typval_T *argvars, typval_T *rettv);
251static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200252static void f_reltime(typval_T *argvars, typval_T *rettv);
253#ifdef FEAT_FLOAT
254static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_reltimestr(typval_T *argvars, typval_T *rettv);
257static void f_remote_expr(typval_T *argvars, typval_T *rettv);
258static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
259static void f_remote_peek(typval_T *argvars, typval_T *rettv);
260static void f_remote_read(typval_T *argvars, typval_T *rettv);
261static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100262static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200263static void f_remove(typval_T *argvars, typval_T *rettv);
264static void f_rename(typval_T *argvars, typval_T *rettv);
265static void f_repeat(typval_T *argvars, typval_T *rettv);
266static void f_resolve(typval_T *argvars, typval_T *rettv);
267static void f_reverse(typval_T *argvars, typval_T *rettv);
268#ifdef FEAT_FLOAT
269static void f_round(typval_T *argvars, typval_T *rettv);
270#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100271#ifdef FEAT_RUBY
272static void f_rubyeval(typval_T *argvars, typval_T *rettv);
273#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200274static void f_screenattr(typval_T *argvars, typval_T *rettv);
275static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100276static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200277static void f_screencol(typval_T *argvars, typval_T *rettv);
278static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100279static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200280static void f_search(typval_T *argvars, typval_T *rettv);
281static void f_searchdecl(typval_T *argvars, typval_T *rettv);
282static void f_searchpair(typval_T *argvars, typval_T *rettv);
283static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
284static void f_searchpos(typval_T *argvars, typval_T *rettv);
285static void f_server2client(typval_T *argvars, typval_T *rettv);
286static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200287static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200288static void f_setbufvar(typval_T *argvars, typval_T *rettv);
289static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
290static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200291static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200292static void f_setfperm(typval_T *argvars, typval_T *rettv);
293static void f_setline(typval_T *argvars, typval_T *rettv);
294static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200295static void f_setpos(typval_T *argvars, typval_T *rettv);
296static void f_setqflist(typval_T *argvars, typval_T *rettv);
297static void f_setreg(typval_T *argvars, typval_T *rettv);
298static void f_settabvar(typval_T *argvars, typval_T *rettv);
299static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100300static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200301static void f_setwinvar(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_CRYPT
303static void f_sha256(typval_T *argvars, typval_T *rettv);
304#endif /* FEAT_CRYPT */
305static void f_shellescape(typval_T *argvars, typval_T *rettv);
306static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
307static void f_simplify(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_FLOAT
309static void f_sin(typval_T *argvars, typval_T *rettv);
310static void f_sinh(typval_T *argvars, typval_T *rettv);
311#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_soundfold(typval_T *argvars, typval_T *rettv);
313static void f_spellbadword(typval_T *argvars, typval_T *rettv);
314static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
315static void f_split(typval_T *argvars, typval_T *rettv);
316#ifdef FEAT_FLOAT
317static void f_sqrt(typval_T *argvars, typval_T *rettv);
318static void f_str2float(typval_T *argvars, typval_T *rettv);
319#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200320static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_str2nr(typval_T *argvars, typval_T *rettv);
322static void f_strchars(typval_T *argvars, typval_T *rettv);
323#ifdef HAVE_STRFTIME
324static void f_strftime(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_strgetchar(typval_T *argvars, typval_T *rettv);
327static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_strlen(typval_T *argvars, typval_T *rettv);
329static void f_strcharpart(typval_T *argvars, typval_T *rettv);
330static void f_strpart(typval_T *argvars, typval_T *rettv);
331static void f_strridx(typval_T *argvars, typval_T *rettv);
332static void f_strtrans(typval_T *argvars, typval_T *rettv);
333static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
334static void f_strwidth(typval_T *argvars, typval_T *rettv);
335static void f_submatch(typval_T *argvars, typval_T *rettv);
336static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200337static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200338static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200339static void f_synID(typval_T *argvars, typval_T *rettv);
340static void f_synIDattr(typval_T *argvars, typval_T *rettv);
341static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
342static void f_synstack(typval_T *argvars, typval_T *rettv);
343static void f_synconcealed(typval_T *argvars, typval_T *rettv);
344static void f_system(typval_T *argvars, typval_T *rettv);
345static void f_systemlist(typval_T *argvars, typval_T *rettv);
346static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
347static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
348static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
349static void f_taglist(typval_T *argvars, typval_T *rettv);
350static void f_tagfiles(typval_T *argvars, typval_T *rettv);
351static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352#ifdef FEAT_FLOAT
353static void f_tan(typval_T *argvars, typval_T *rettv);
354static void f_tanh(typval_T *argvars, typval_T *rettv);
355#endif
356#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200357static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200358static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_timer_start(typval_T *argvars, typval_T *rettv);
360static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200361static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200362#endif
363static void f_tolower(typval_T *argvars, typval_T *rettv);
364static void f_toupper(typval_T *argvars, typval_T *rettv);
365static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100366static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200367#ifdef FEAT_FLOAT
368static void f_trunc(typval_T *argvars, typval_T *rettv);
369#endif
370static void f_type(typval_T *argvars, typval_T *rettv);
371static void f_undofile(typval_T *argvars, typval_T *rettv);
372static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200373static void f_virtcol(typval_T *argvars, typval_T *rettv);
374static void f_visualmode(typval_T *argvars, typval_T *rettv);
375static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200376static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200377static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
378static void f_win_getid(typval_T *argvars, typval_T *rettv);
379static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
380static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
381static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100382static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200383static void f_winbufnr(typval_T *argvars, typval_T *rettv);
384static void f_wincol(typval_T *argvars, typval_T *rettv);
385static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200386static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200387static void f_winline(typval_T *argvars, typval_T *rettv);
388static void f_winnr(typval_T *argvars, typval_T *rettv);
389static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
390static void f_winrestview(typval_T *argvars, typval_T *rettv);
391static void f_winsaveview(typval_T *argvars, typval_T *rettv);
392static void f_winwidth(typval_T *argvars, typval_T *rettv);
393static void f_writefile(typval_T *argvars, typval_T *rettv);
394static void f_wordcount(typval_T *argvars, typval_T *rettv);
395static void f_xor(typval_T *argvars, typval_T *rettv);
396
397/*
398 * Array with names and number of arguments of all internal functions
399 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
400 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200401typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200403 char *f_name; // function name
404 char f_min_argc; // minimal number of arguments
405 char f_max_argc; // maximal number of arguments
406 char f_argtype; // for method: FEARG_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200408 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200409} funcentry_T;
410
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200411// values for f_argtype; zero means it cannot be used as a method
412#define FEARG_1 1 // base is the first argument
413#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200414#define FEARG_3 3 // base is the third argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200415#define FEARG_LAST 9 // base is the last argument
416
Bram Moolenaarac92e252019-08-03 21:58:38 +0200417static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418{
419#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200420 {"abs", 1, 1, 0, f_abs},
421 {"acos", 1, 1, 0, f_acos}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200423 {"add", 2, 2, FEARG_1, f_add},
424 {"and", 2, 2, 0, f_and},
425 {"append", 2, 2, FEARG_LAST, f_append},
426 {"appendbufline", 3, 3, FEARG_LAST, f_appendbufline},
427 {"argc", 0, 1, 0, f_argc},
428 {"argidx", 0, 0, 0, f_argidx},
429 {"arglistid", 0, 2, 0, f_arglistid},
430 {"argv", 0, 2, 0, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200432 {"asin", 1, 1, 0, f_asin}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200433#endif
Bram Moolenaar24278d22019-08-16 21:49:22 +0200434 {"assert_beeps", 1, 2, FEARG_1, f_assert_beeps},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200435 {"assert_equal", 2, 3, FEARG_2, f_assert_equal},
436 {"assert_equalfile", 2, 2, 0, f_assert_equalfile},
437 {"assert_exception", 1, 2, 0, f_assert_exception},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200438 {"assert_fails", 1, 3, FEARG_1, f_assert_fails},
439 {"assert_false", 1, 2, FEARG_1, f_assert_false},
440 {"assert_inrange", 3, 4, FEARG_3, f_assert_inrange},
441 {"assert_match", 2, 3, FEARG_2, f_assert_match},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200442 {"assert_notequal", 2, 3, FEARG_2, f_assert_notequal},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200443 {"assert_notmatch", 2, 3, FEARG_2, f_assert_notmatch},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200444 {"assert_report", 1, 1, 0, f_assert_report},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200445 {"assert_true", 1, 2, FEARG_1, f_assert_true},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200447 {"atan", 1, 1, 0, f_atan},
448 {"atan2", 2, 2, 0, f_atan2},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100450#ifdef FEAT_BEVAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200451 {"balloon_gettext", 0, 0, 0, f_balloon_gettext},
452 {"balloon_show", 1, 1, 0, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100453# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200454 {"balloon_split", 1, 1, 0, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100455# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100456#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200457 {"browse", 4, 4, 0, f_browse},
458 {"browsedir", 2, 2, 0, f_browsedir},
459 {"bufadd", 1, 1, 0, f_bufadd},
460 {"bufexists", 1, 1, 0, f_bufexists},
461 {"buffer_exists", 1, 1, 0, f_bufexists}, // obsolete
462 {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
463 {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
464 {"buflisted", 1, 1, 0, f_buflisted},
465 {"bufload", 1, 1, 0, f_bufload},
466 {"bufloaded", 1, 1, 0, f_bufloaded},
467 {"bufname", 1, 1, 0, f_bufname},
468 {"bufnr", 1, 2, 0, f_bufnr},
469 {"bufwinid", 1, 1, 0, f_bufwinid},
470 {"bufwinnr", 1, 1, 0, f_bufwinnr},
471 {"byte2line", 1, 1, 0, f_byte2line},
472 {"byteidx", 2, 2, 0, f_byteidx},
473 {"byteidxcomp", 2, 2, 0, f_byteidxcomp},
474 {"call", 2, 3, 0, f_call},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200476 {"ceil", 1, 1, 0, f_ceil},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477#endif
478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200479 {"ch_canread", 1, 1, 0, f_ch_canread},
480 {"ch_close", 1, 1, 0, f_ch_close},
481 {"ch_close_in", 1, 1, 0, f_ch_close_in},
482 {"ch_evalexpr", 2, 3, 0, f_ch_evalexpr},
483 {"ch_evalraw", 2, 3, 0, f_ch_evalraw},
484 {"ch_getbufnr", 2, 2, 0, f_ch_getbufnr},
485 {"ch_getjob", 1, 1, 0, f_ch_getjob},
486 {"ch_info", 1, 1, 0, f_ch_info},
487 {"ch_log", 1, 2, 0, f_ch_log},
488 {"ch_logfile", 1, 2, 0, f_ch_logfile},
489 {"ch_open", 1, 2, 0, f_ch_open},
490 {"ch_read", 1, 2, 0, f_ch_read},
491 {"ch_readblob", 1, 2, 0, f_ch_readblob},
492 {"ch_readraw", 1, 2, 0, f_ch_readraw},
493 {"ch_sendexpr", 2, 3, 0, f_ch_sendexpr},
494 {"ch_sendraw", 2, 3, 0, f_ch_sendraw},
495 {"ch_setoptions", 2, 2, 0, f_ch_setoptions},
496 {"ch_status", 1, 2, 0, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200498 {"changenr", 0, 0, 0, f_changenr},
499 {"char2nr", 1, 2, 0, f_char2nr},
500 {"chdir", 1, 1, 0, f_chdir},
501 {"cindent", 1, 1, 0, f_cindent},
502 {"clearmatches", 0, 1, 0, f_clearmatches},
503 {"col", 1, 1, 0, f_col},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504#if defined(FEAT_INS_EXPAND)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200505 {"complete", 2, 2, 0, f_complete},
506 {"complete_add", 1, 1, 0, f_complete_add},
507 {"complete_check", 0, 0, 0, f_complete_check},
508 {"complete_info", 0, 1, 0, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200509#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200510 {"confirm", 1, 4, 0, f_confirm},
511 {"copy", 1, 1, FEARG_1, f_copy},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200513 {"cos", 1, 1, 0, f_cos},
514 {"cosh", 1, 1, 0, f_cosh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200515#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200516 {"count", 2, 4, FEARG_1, f_count},
517 {"cscope_connection",0,3, 0, f_cscope_connection},
518 {"cursor", 1, 3, 0, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100519#ifdef MSWIN
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200520 {"debugbreak", 1, 1, 0, f_debugbreak},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200521#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200522 {"deepcopy", 1, 2, 0, f_deepcopy},
523 {"delete", 1, 2, 0, f_delete},
524 {"deletebufline", 2, 3, 0, f_deletebufline},
525 {"did_filetype", 0, 0, 0, f_did_filetype},
526 {"diff_filler", 1, 1, 0, f_diff_filler},
527 {"diff_hlID", 2, 2, 0, f_diff_hlID},
528 {"empty", 1, 1, FEARG_1, f_empty},
529 {"environ", 0, 0, 0, f_environ},
530 {"escape", 2, 2, 0, f_escape},
531 {"eval", 1, 1, FEARG_1, f_eval},
532 {"eventhandler", 0, 0, 0, f_eventhandler},
533 {"executable", 1, 1, 0, f_executable},
534 {"execute", 1, 2, 0, f_execute},
535 {"exepath", 1, 1, 0, f_exepath},
536 {"exists", 1, 1, 0, f_exists},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200538 {"exp", 1, 1, 0, f_exp},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200540 {"expand", 1, 3, 0, f_expand},
541 {"expandcmd", 1, 1, 0, f_expandcmd},
542 {"extend", 2, 3, FEARG_1, f_extend},
543 {"feedkeys", 1, 2, 0, f_feedkeys},
544 {"file_readable", 1, 1, 0, f_filereadable}, // obsolete
545 {"filereadable", 1, 1, 0, f_filereadable},
546 {"filewritable", 1, 1, 0, f_filewritable},
547 {"filter", 2, 2, FEARG_1, f_filter},
548 {"finddir", 1, 3, 0, f_finddir},
549 {"findfile", 1, 3, 0, f_findfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200551 {"float2nr", 1, 1, 0, f_float2nr},
552 {"floor", 1, 1, 0, f_floor},
553 {"fmod", 2, 2, 0, f_fmod},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200555 {"fnameescape", 1, 1, 0, f_fnameescape},
556 {"fnamemodify", 2, 2, 0, f_fnamemodify},
557 {"foldclosed", 1, 1, 0, f_foldclosed},
558 {"foldclosedend", 1, 1, 0, f_foldclosedend},
559 {"foldlevel", 1, 1, 0, f_foldlevel},
560 {"foldtext", 0, 0, 0, f_foldtext},
561 {"foldtextresult", 1, 1, 0, f_foldtextresult},
562 {"foreground", 0, 0, 0, f_foreground},
563 {"funcref", 1, 3, 0, f_funcref},
564 {"function", 1, 3, 0, f_function},
565 {"garbagecollect", 0, 1, 0, f_garbagecollect},
566 {"get", 2, 3, FEARG_1, f_get},
567 {"getbufinfo", 0, 1, 0, f_getbufinfo},
568 {"getbufline", 2, 3, 0, f_getbufline},
569 {"getbufvar", 2, 3, 0, f_getbufvar},
570 {"getchangelist", 1, 1, 0, f_getchangelist},
571 {"getchar", 0, 1, 0, f_getchar},
572 {"getcharmod", 0, 0, 0, f_getcharmod},
573 {"getcharsearch", 0, 0, 0, f_getcharsearch},
574 {"getcmdline", 0, 0, 0, f_getcmdline},
575 {"getcmdpos", 0, 0, 0, f_getcmdpos},
576 {"getcmdtype", 0, 0, 0, f_getcmdtype},
577 {"getcmdwintype", 0, 0, 0, f_getcmdwintype},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200578#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200579 {"getcompletion", 2, 3, 0, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200580#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200581 {"getcurpos", 0, 0, 0, f_getcurpos},
582 {"getcwd", 0, 2, 0, f_getcwd},
583 {"getenv", 1, 1, 0, f_getenv},
584 {"getfontname", 0, 1, 0, f_getfontname},
585 {"getfperm", 1, 1, 0, f_getfperm},
586 {"getfsize", 1, 1, 0, f_getfsize},
587 {"getftime", 1, 1, 0, f_getftime},
588 {"getftype", 1, 1, 0, f_getftype},
589 {"getjumplist", 0, 2, 0, f_getjumplist},
590 {"getline", 1, 2, 0, f_getline},
591 {"getloclist", 1, 2, 0, f_getloclist},
592 {"getmatches", 0, 1, 0, f_getmatches},
593 {"getpid", 0, 0, 0, f_getpid},
594 {"getpos", 1, 1, 0, f_getpos},
595 {"getqflist", 0, 1, 0, f_getqflist},
596 {"getreg", 0, 3, 0, f_getreg},
597 {"getregtype", 0, 1, 0, f_getregtype},
598 {"gettabinfo", 0, 1, 0, f_gettabinfo},
599 {"gettabvar", 2, 3, 0, f_gettabvar},
600 {"gettabwinvar", 3, 4, 0, f_gettabwinvar},
601 {"gettagstack", 0, 1, 0, f_gettagstack},
602 {"getwininfo", 0, 1, 0, f_getwininfo},
603 {"getwinpos", 0, 1, 0, f_getwinpos},
604 {"getwinposx", 0, 0, 0, f_getwinposx},
605 {"getwinposy", 0, 0, 0, f_getwinposy},
606 {"getwinvar", 2, 3, 0, f_getwinvar},
607 {"glob", 1, 4, 0, f_glob},
608 {"glob2regpat", 1, 1, 0, f_glob2regpat},
609 {"globpath", 2, 5, 0, f_globpath},
610 {"has", 1, 1, 0, f_has},
611 {"has_key", 2, 2, FEARG_1, f_has_key},
612 {"haslocaldir", 0, 2, 0, f_haslocaldir},
613 {"hasmapto", 1, 3, 0, f_hasmapto},
614 {"highlightID", 1, 1, 0, f_hlID}, // obsolete
615 {"highlight_exists",1, 1, 0, f_hlexists}, // obsolete
616 {"histadd", 2, 2, 0, f_histadd},
617 {"histdel", 1, 2, 0, f_histdel},
618 {"histget", 1, 2, 0, f_histget},
619 {"histnr", 1, 1, 0, f_histnr},
620 {"hlID", 1, 1, 0, f_hlID},
621 {"hlexists", 1, 1, 0, f_hlexists},
622 {"hostname", 0, 0, 0, f_hostname},
623 {"iconv", 3, 3, 0, f_iconv},
624 {"indent", 1, 1, 0, f_indent},
625 {"index", 2, 4, FEARG_1, f_index},
626 {"input", 1, 3, 0, f_input},
627 {"inputdialog", 1, 3, 0, f_inputdialog},
628 {"inputlist", 1, 1, 0, f_inputlist},
629 {"inputrestore", 0, 0, 0, f_inputrestore},
630 {"inputsave", 0, 0, 0, f_inputsave},
631 {"inputsecret", 1, 2, 0, f_inputsecret},
632 {"insert", 2, 3, FEARG_1, f_insert},
633 {"invert", 1, 1, 0, f_invert},
634 {"isdirectory", 1, 1, 0, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200635#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200636 {"isinf", 1, 1, 0, f_isinf},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200637#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200638 {"islocked", 1, 1, 0, f_islocked},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200640 {"isnan", 1, 1, 0, f_isnan},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200642 {"items", 1, 1, FEARG_1, f_items},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200644 {"job_getchannel", 1, 1, 0, f_job_getchannel},
645 {"job_info", 0, 1, 0, f_job_info},
646 {"job_setoptions", 2, 2, 0, f_job_setoptions},
647 {"job_start", 1, 2, 0, f_job_start},
648 {"job_status", 1, 1, 0, f_job_status},
649 {"job_stop", 1, 2, 0, f_job_stop},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200650#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200651 {"join", 1, 2, FEARG_1, f_join},
652 {"js_decode", 1, 1, 0, f_js_decode},
653 {"js_encode", 1, 1, 0, f_js_encode},
654 {"json_decode", 1, 1, 0, f_json_decode},
655 {"json_encode", 1, 1, 0, f_json_encode},
656 {"keys", 1, 1, FEARG_1, f_keys},
657 {"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
658 {"len", 1, 1, FEARG_1, f_len},
659 {"libcall", 3, 3, 0, f_libcall},
660 {"libcallnr", 3, 3, 0, f_libcallnr},
661 {"line", 1, 1, 0, f_line},
662 {"line2byte", 1, 1, 0, f_line2byte},
663 {"lispindent", 1, 1, 0, f_lispindent},
664 {"list2str", 1, 2, 0, f_list2str},
665 {"listener_add", 1, 2, 0, f_listener_add},
666 {"listener_flush", 0, 1, 0, f_listener_flush},
667 {"listener_remove", 1, 1, 0, f_listener_remove},
668 {"localtime", 0, 0, 0, f_localtime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200669#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200670 {"log", 1, 1, 0, f_log},
671 {"log10", 1, 1, 0, f_log10},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672#endif
673#ifdef FEAT_LUA
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200674 {"luaeval", 1, 2, 0, f_luaeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200676 {"map", 2, 2, FEARG_1, f_map},
677 {"maparg", 1, 4, 0, f_maparg},
678 {"mapcheck", 1, 3, 0, f_mapcheck},
679 {"match", 2, 4, 0, f_match},
680 {"matchadd", 2, 5, 0, f_matchadd},
681 {"matchaddpos", 2, 5, 0, f_matchaddpos},
682 {"matcharg", 1, 1, 0, f_matcharg},
683 {"matchdelete", 1, 2, 0, f_matchdelete},
684 {"matchend", 2, 4, 0, f_matchend},
685 {"matchlist", 2, 4, 0, f_matchlist},
686 {"matchstr", 2, 4, 0, f_matchstr},
687 {"matchstrpos", 2, 4, 0, f_matchstrpos},
688 {"max", 1, 1, FEARG_1, f_max},
689 {"min", 1, 1, FEARG_1, f_min},
690 {"mkdir", 1, 3, 0, f_mkdir},
691 {"mode", 0, 1, 0, f_mode},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692#ifdef FEAT_MZSCHEME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200693 {"mzeval", 1, 1, 0, f_mzeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200694#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200695 {"nextnonblank", 1, 1, 0, f_nextnonblank},
696 {"nr2char", 1, 2, 0, f_nr2char},
697 {"or", 2, 2, 0, f_or},
698 {"pathshorten", 1, 1, 0, f_pathshorten},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699#ifdef FEAT_PERL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200700 {"perleval", 1, 1, 0, f_perleval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200701#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200702#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200703 {"popup_atcursor", 2, 2, 0, f_popup_atcursor},
704 {"popup_beval", 2, 2, 0, f_popup_beval},
705 {"popup_clear", 0, 0, 0, f_popup_clear},
706 {"popup_close", 1, 2, 0, f_popup_close},
707 {"popup_create", 2, 2, 0, f_popup_create},
708 {"popup_dialog", 2, 2, 0, f_popup_dialog},
709 {"popup_filter_menu", 2, 2, 0, f_popup_filter_menu},
710 {"popup_filter_yesno", 2, 2, 0, f_popup_filter_yesno},
711 {"popup_getoptions", 1, 1, 0, f_popup_getoptions},
712 {"popup_getpos", 1, 1, 0, f_popup_getpos},
713 {"popup_getpreview", 0, 0, 0, f_popup_getpreview},
714 {"popup_hide", 1, 1, 0, f_popup_hide},
715 {"popup_locate", 2, 2, 0, f_popup_locate},
716 {"popup_menu", 2, 2, 0, f_popup_menu},
717 {"popup_move", 2, 2, 0, f_popup_move},
718 {"popup_notification", 2, 2, 0, f_popup_notification},
719 {"popup_setoptions", 2, 2, 0, f_popup_setoptions},
720 {"popup_settext", 2, 2, 0, f_popup_settext},
721 {"popup_show", 1, 1, 0, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200722#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200723#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200724 {"pow", 2, 2, 0, f_pow},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200725#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200726 {"prevnonblank", 1, 1, 0, f_prevnonblank},
Bram Moolenaarfd8ca212019-08-10 00:13:30 +0200727 {"printf", 1, 19, FEARG_2, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200728#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200729 {"prompt_setcallback", 2, 2, 0, f_prompt_setcallback},
730 {"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt},
731 {"prompt_setprompt", 2, 2, 0, f_prompt_setprompt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200732#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100733#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200734 {"prop_add", 3, 3, 0, f_prop_add},
735 {"prop_clear", 1, 3, 0, f_prop_clear},
736 {"prop_list", 1, 2, 0, f_prop_list},
737 {"prop_remove", 1, 3, 0, f_prop_remove},
738 {"prop_type_add", 2, 2, 0, f_prop_type_add},
739 {"prop_type_change", 2, 2, 0, f_prop_type_change},
740 {"prop_type_delete", 1, 2, 0, f_prop_type_delete},
741 {"prop_type_get", 1, 2, 0, f_prop_type_get},
742 {"prop_type_list", 0, 1, 0, f_prop_type_list},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100743#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200744 {"pumvisible", 0, 0, 0, f_pumvisible},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745#ifdef FEAT_PYTHON3
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200746 {"py3eval", 1, 1, 0, f_py3eval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747#endif
748#ifdef FEAT_PYTHON
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200749 {"pyeval", 1, 1, 0, f_pyeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200750#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100751#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200752 {"pyxeval", 1, 1, 0, f_pyxeval},
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100753#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200754 {"range", 1, 3, 0, f_range},
755 {"readdir", 1, 2, 0, f_readdir},
756 {"readfile", 1, 3, 0, f_readfile},
757 {"reg_executing", 0, 0, 0, f_reg_executing},
758 {"reg_recording", 0, 0, 0, f_reg_recording},
759 {"reltime", 0, 2, 0, f_reltime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200760#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200761 {"reltimefloat", 1, 1, 0, f_reltimefloat},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200763 {"reltimestr", 1, 1, 0, f_reltimestr},
764 {"remote_expr", 2, 4, 0, f_remote_expr},
765 {"remote_foreground", 1, 1, 0, f_remote_foreground},
766 {"remote_peek", 1, 2, 0, f_remote_peek},
767 {"remote_read", 1, 2, 0, f_remote_read},
768 {"remote_send", 2, 3, 0, f_remote_send},
769 {"remote_startserver", 1, 1, 0, f_remote_startserver},
770 {"remove", 2, 3, FEARG_1, f_remove},
771 {"rename", 2, 2, 0, f_rename},
772 {"repeat", 2, 2, FEARG_1, f_repeat},
773 {"resolve", 1, 1, 0, f_resolve},
774 {"reverse", 1, 1, FEARG_1, f_reverse},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200776 {"round", 1, 1, 0, f_round},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100778#ifdef FEAT_RUBY
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200779 {"rubyeval", 1, 1, 0, f_rubyeval},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100780#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200781 {"screenattr", 2, 2, 0, f_screenattr},
782 {"screenchar", 2, 2, 0, f_screenchar},
783 {"screenchars", 2, 2, 0, f_screenchars},
784 {"screencol", 0, 0, 0, f_screencol},
785 {"screenpos", 3, 3, 0, f_screenpos},
786 {"screenrow", 0, 0, 0, f_screenrow},
787 {"screenstring", 2, 2, 0, f_screenstring},
788 {"search", 1, 4, 0, f_search},
789 {"searchdecl", 1, 3, 0, f_searchdecl},
790 {"searchpair", 3, 7, 0, f_searchpair},
791 {"searchpairpos", 3, 7, 0, f_searchpairpos},
792 {"searchpos", 1, 4, 0, f_searchpos},
793 {"server2client", 2, 2, 0, f_server2client},
794 {"serverlist", 0, 0, 0, f_serverlist},
795 {"setbufline", 3, 3, 0, f_setbufline},
796 {"setbufvar", 3, 3, 0, f_setbufvar},
797 {"setcharsearch", 1, 1, 0, f_setcharsearch},
798 {"setcmdpos", 1, 1, 0, f_setcmdpos},
799 {"setenv", 2, 2, 0, f_setenv},
800 {"setfperm", 2, 2, 0, f_setfperm},
801 {"setline", 2, 2, 0, f_setline},
802 {"setloclist", 2, 4, 0, f_setloclist},
803 {"setmatches", 1, 2, 0, f_setmatches},
804 {"setpos", 2, 2, 0, f_setpos},
805 {"setqflist", 1, 3, 0, f_setqflist},
806 {"setreg", 2, 3, 0, f_setreg},
807 {"settabvar", 3, 3, 0, f_settabvar},
808 {"settabwinvar", 4, 4, 0, f_settabwinvar},
809 {"settagstack", 2, 3, 0, f_settagstack},
810 {"setwinvar", 3, 3, 0, f_setwinvar},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200811#ifdef FEAT_CRYPT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200812 {"sha256", 1, 1, 0, f_sha256},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200813#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200814 {"shellescape", 1, 2, 0, f_shellescape},
815 {"shiftwidth", 0, 1, 0, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100816#ifdef FEAT_SIGNS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200817 {"sign_define", 1, 2, 0, f_sign_define},
818 {"sign_getdefined", 0, 1, 0, f_sign_getdefined},
819 {"sign_getplaced", 0, 2, 0, f_sign_getplaced},
820 {"sign_jump", 3, 3, 0, f_sign_jump},
821 {"sign_place", 4, 5, 0, f_sign_place},
822 {"sign_placelist", 1, 1, 0, f_sign_placelist},
823 {"sign_undefine", 0, 1, 0, f_sign_undefine},
824 {"sign_unplace", 1, 2, 0, f_sign_unplace},
825 {"sign_unplacelist", 1, 2, 0, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100826#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200827 {"simplify", 1, 1, 0, f_simplify},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200829 {"sin", 1, 1, 0, f_sin},
830 {"sinh", 1, 1, 0, f_sinh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200832 {"sort", 1, 3, FEARG_1, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200833#ifdef FEAT_SOUND
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200834 {"sound_clear", 0, 0, 0, f_sound_clear},
835 {"sound_playevent", 1, 2, 0, f_sound_playevent},
836 {"sound_playfile", 1, 2, 0, f_sound_playfile},
837 {"sound_stop", 1, 1, 0, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200838#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200839 {"soundfold", 1, 1, 0, f_soundfold},
840 {"spellbadword", 0, 1, 0, f_spellbadword},
841 {"spellsuggest", 1, 3, 0, f_spellsuggest},
842 {"split", 1, 3, FEARG_1, f_split},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200844 {"sqrt", 1, 1, 0, f_sqrt},
845 {"str2float", 1, 1, 0, f_str2float},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200847 {"str2list", 1, 2, FEARG_1, f_str2list},
848 {"str2nr", 1, 2, 0, f_str2nr},
849 {"strcharpart", 2, 3, 0, f_strcharpart},
850 {"strchars", 1, 2, 0, f_strchars},
851 {"strdisplaywidth", 1, 2, 0, f_strdisplaywidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852#ifdef HAVE_STRFTIME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200853 {"strftime", 1, 2, 0, f_strftime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200855 {"strgetchar", 2, 2, 0, f_strgetchar},
856 {"stridx", 2, 3, 0, f_stridx},
857 {"string", 1, 1, FEARG_1, f_string},
858 {"strlen", 1, 1, FEARG_1, f_strlen},
859 {"strpart", 2, 3, 0, f_strpart},
860 {"strridx", 2, 3, 0, f_strridx},
861 {"strtrans", 1, 1, FEARG_1, f_strtrans},
862 {"strwidth", 1, 1, FEARG_1, f_strwidth},
863 {"submatch", 1, 2, 0, f_submatch},
864 {"substitute", 4, 4, FEARG_1, f_substitute},
865 {"swapinfo", 1, 1, 0, f_swapinfo},
866 {"swapname", 1, 1, 0, f_swapname},
867 {"synID", 3, 3, 0, f_synID},
868 {"synIDattr", 2, 3, FEARG_1, f_synIDattr},
869 {"synIDtrans", 1, 1, FEARG_1, f_synIDtrans},
870 {"synconcealed", 2, 2, 0, f_synconcealed},
871 {"synstack", 2, 2, 0, f_synstack},
872 {"system", 1, 2, FEARG_1, f_system},
873 {"systemlist", 1, 2, FEARG_1, f_systemlist},
874 {"tabpagebuflist", 0, 1, 0, f_tabpagebuflist},
875 {"tabpagenr", 0, 1, 0, f_tabpagenr},
876 {"tabpagewinnr", 1, 2, 0, f_tabpagewinnr},
877 {"tagfiles", 0, 0, 0, f_tagfiles},
878 {"taglist", 1, 2, 0, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200880 {"tan", 1, 1, 0, f_tan},
881 {"tanh", 1, 1, 0, f_tanh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200883 {"tempname", 0, 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200884#ifdef FEAT_TERMINAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200885 {"term_dumpdiff", 2, 3, 0, f_term_dumpdiff},
886 {"term_dumpload", 1, 2, 0, f_term_dumpload},
887 {"term_dumpwrite", 2, 3, 0, f_term_dumpwrite},
888 {"term_getaltscreen", 1, 1, 0, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200889# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200890 {"term_getansicolors", 1, 1, 0, f_term_getansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200891# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200892 {"term_getattr", 2, 2, 0, f_term_getattr},
893 {"term_getcursor", 1, 1, 0, f_term_getcursor},
894 {"term_getjob", 1, 1, 0, f_term_getjob},
895 {"term_getline", 2, 2, 0, f_term_getline},
896 {"term_getscrolled", 1, 1, 0, f_term_getscrolled},
897 {"term_getsize", 1, 1, 0, f_term_getsize},
898 {"term_getstatus", 1, 1, 0, f_term_getstatus},
899 {"term_gettitle", 1, 1, 0, f_term_gettitle},
900 {"term_gettty", 1, 2, 0, f_term_gettty},
901 {"term_list", 0, 0, 0, f_term_list},
902 {"term_scrape", 2, 2, 0, f_term_scrape},
903 {"term_sendkeys", 2, 2, 0, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200904# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200905 {"term_setansicolors", 2, 2, 0, f_term_setansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200906# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200907 {"term_setkill", 2, 2, 0, f_term_setkill},
908 {"term_setrestore", 2, 2, 0, f_term_setrestore},
909 {"term_setsize", 3, 3, 0, f_term_setsize},
910 {"term_start", 1, 2, 0, f_term_start},
911 {"term_wait", 1, 2, 0, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200912#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200913 {"test_alloc_fail", 3, 3, 0, f_test_alloc_fail},
914 {"test_autochdir", 0, 0, 0, f_test_autochdir},
915 {"test_feedinput", 1, 1, 0, f_test_feedinput},
916 {"test_garbagecollect_now", 0, 0, 0, f_test_garbagecollect_now},
917 {"test_garbagecollect_soon", 0, 0, 0, f_test_garbagecollect_soon},
918 {"test_getvalue", 1, 1, 0, f_test_getvalue},
919 {"test_ignore_error", 1, 1, 0, f_test_ignore_error},
920 {"test_null_blob", 0, 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200922 {"test_null_channel", 0, 0, 0, f_test_null_channel},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200923#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200924 {"test_null_dict", 0, 0, 0, f_test_null_dict},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200926 {"test_null_job", 0, 0, 0, f_test_null_job},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200927#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200928 {"test_null_list", 0, 0, 0, f_test_null_list},
929 {"test_null_partial", 0, 0, 0, f_test_null_partial},
930 {"test_null_string", 0, 0, 0, f_test_null_string},
931 {"test_option_not_set", 1, 1, 0, f_test_option_not_set},
932 {"test_override", 2, 2, 0, f_test_override},
933 {"test_refcount", 1, 1, 0, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200934#ifdef FEAT_GUI
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200935 {"test_scrollbar", 3, 3, 0, f_test_scrollbar},
Bram Moolenaarab186732018-09-14 21:27:06 +0200936#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200937#ifdef FEAT_MOUSE
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200938 {"test_setmouse", 2, 2, 0, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200939#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200940 {"test_settime", 1, 1, 0, f_test_settime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200941#ifdef FEAT_TIMERS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200942 {"timer_info", 0, 1, 0, f_timer_info},
943 {"timer_pause", 2, 2, 0, f_timer_pause},
944 {"timer_start", 2, 3, 0, f_timer_start},
945 {"timer_stop", 1, 1, 0, f_timer_stop},
946 {"timer_stopall", 0, 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200948 {"tolower", 1, 1, 0, f_tolower},
949 {"toupper", 1, 1, 0, f_toupper},
950 {"tr", 3, 3, 0, f_tr},
951 {"trim", 1, 2, 0, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200953 {"trunc", 1, 1, 0, f_trunc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200954#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200955 {"type", 1, 1, FEARG_1, f_type},
956 {"undofile", 1, 1, 0, f_undofile},
957 {"undotree", 0, 0, 0, f_undotree},
958 {"uniq", 1, 3, FEARG_1, f_uniq},
959 {"values", 1, 1, FEARG_1, f_values},
960 {"virtcol", 1, 1, 0, f_virtcol},
961 {"visualmode", 0, 1, 0, f_visualmode},
962 {"wildmenumode", 0, 0, 0, f_wildmenumode},
963 {"win_execute", 2, 3, 0, f_win_execute},
964 {"win_findbuf", 1, 1, 0, f_win_findbuf},
965 {"win_getid", 0, 2, 0, f_win_getid},
966 {"win_gotoid", 1, 1, 0, f_win_gotoid},
967 {"win_id2tabwin", 1, 1, 0, f_win_id2tabwin},
968 {"win_id2win", 1, 1, 0, f_win_id2win},
969 {"win_screenpos", 1, 1, 0, f_win_screenpos},
970 {"winbufnr", 1, 1, 0, f_winbufnr},
971 {"wincol", 0, 0, 0, f_wincol},
972 {"winheight", 1, 1, 0, f_winheight},
973 {"winlayout", 0, 1, 0, f_winlayout},
974 {"winline", 0, 0, 0, f_winline},
975 {"winnr", 0, 1, 0, f_winnr},
976 {"winrestcmd", 0, 0, 0, f_winrestcmd},
977 {"winrestview", 1, 1, 0, f_winrestview},
978 {"winsaveview", 0, 0, 0, f_winsaveview},
979 {"winwidth", 1, 1, 0, f_winwidth},
980 {"wordcount", 0, 0, 0, f_wordcount},
981 {"writefile", 2, 3, 0, f_writefile},
982 {"xor", 2, 2, 0, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +0200983};
984
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
986
987/*
988 * Function given to ExpandGeneric() to obtain the list of internal
989 * or user defined function names.
990 */
991 char_u *
992get_function_name(expand_T *xp, int idx)
993{
994 static int intidx = -1;
995 char_u *name;
996
997 if (idx == 0)
998 intidx = -1;
999 if (intidx < 0)
1000 {
1001 name = get_user_func_name(xp, idx);
1002 if (name != NULL)
1003 return name;
1004 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001005 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001006 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001007 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001008 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001009 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001010 STRCAT(IObuff, ")");
1011 return IObuff;
1012 }
1013
1014 return NULL;
1015}
1016
1017/*
1018 * Function given to ExpandGeneric() to obtain the list of internal or
1019 * user defined variable or function names.
1020 */
1021 char_u *
1022get_expr_name(expand_T *xp, int idx)
1023{
1024 static int intidx = -1;
1025 char_u *name;
1026
1027 if (idx == 0)
1028 intidx = -1;
1029 if (intidx < 0)
1030 {
1031 name = get_function_name(xp, idx);
1032 if (name != NULL)
1033 return name;
1034 }
1035 return get_user_var_name(xp, ++intidx);
1036}
1037
1038#endif /* FEAT_CMDL_COMPL */
1039
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001041 * Find internal function "name" in table "global_functions".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001042 * Return index, or -1 if not found
1043 */
Bram Moolenaarac92e252019-08-03 21:58:38 +02001044 static int
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001045find_internal_func(char_u *name)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001046{
1047 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001048 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001049 int cmp;
1050 int x;
1051
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001052 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001053
1054 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001055 while (first <= last)
1056 {
1057 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001058 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001059 if (cmp < 0)
1060 last = x - 1;
1061 else if (cmp > 0)
1062 first = x + 1;
1063 else
1064 return x;
1065 }
1066 return -1;
1067}
1068
1069 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001070has_internal_func(char_u *name)
1071{
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001072 return find_internal_func(name) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001073}
1074
1075 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001076call_internal_func(
1077 char_u *name,
1078 int argcount,
1079 typval_T *argvars,
1080 typval_T *rettv)
1081{
1082 int i;
1083
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001084 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001085 if (i < 0)
1086 return ERROR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001087 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001088 return ERROR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001089 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001090 return ERROR_TOOMANY;
1091 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001092 global_functions[i].f_func(argvars, rettv);
1093 return ERROR_NONE;
1094}
1095
1096/*
1097 * Invoke a method for base->method().
1098 */
1099 int
1100call_internal_method(
1101 char_u *name,
1102 int argcount,
1103 typval_T *argvars,
1104 typval_T *rettv,
1105 typval_T *basetv)
1106{
1107 int i;
1108 int fi;
1109 typval_T argv[MAX_FUNC_ARGS + 1];
1110
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001111 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001112 if (fi < 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001113 return ERROR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001114 if (global_functions[fi].f_argtype == 0)
1115 return ERROR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001116 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001117 return ERROR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001118 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001119 return ERROR_TOOMANY;
1120
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001121 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001122 {
1123 // base value goes last
1124 for (i = 0; i < argcount; ++i)
1125 argv[i] = argvars[i];
1126 argv[argcount] = *basetv;
1127 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001128 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001129 {
1130 // base value goes second
1131 argv[0] = argvars[0];
1132 argv[1] = *basetv;
1133 for (i = 1; i < argcount; ++i)
1134 argv[i + 1] = argvars[i];
1135 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001136 else if (global_functions[fi].f_argtype == FEARG_3)
1137 {
1138 // base value goes third
1139 argv[0] = argvars[0];
1140 argv[1] = argvars[1];
1141 argv[2] = *basetv;
1142 for (i = 2; i < argcount; ++i)
1143 argv[i + 1] = argvars[i];
1144 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001145 else
1146 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001147 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001148 argv[0] = *basetv;
1149 for (i = 0; i < argcount; ++i)
1150 argv[i + 1] = argvars[i];
1151 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001152 argv[argcount + 1].v_type = VAR_UNKNOWN;
1153
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001154 global_functions[fi].f_func(argv, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001155 return ERROR_NONE;
1156}
1157
1158/*
1159 * Return TRUE for a non-zero Number and a non-empty String.
1160 */
1161 static int
1162non_zero_arg(typval_T *argvars)
1163{
1164 return ((argvars[0].v_type == VAR_NUMBER
1165 && argvars[0].vval.v_number != 0)
1166 || (argvars[0].v_type == VAR_SPECIAL
1167 && argvars[0].vval.v_number == VVAL_TRUE)
1168 || (argvars[0].v_type == VAR_STRING
1169 && argvars[0].vval.v_string != NULL
1170 && *argvars[0].vval.v_string != NUL));
1171}
1172
1173/*
1174 * Get the lnum from the first argument.
1175 * Also accepts ".", "$", etc., but that only works for the current buffer.
1176 * Returns -1 on error.
1177 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001178 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001179tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001180{
1181 typval_T rettv;
1182 linenr_T lnum;
1183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001184 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001185 if (lnum == 0) /* no valid number, try using line() */
1186 {
1187 rettv.v_type = VAR_NUMBER;
1188 f_line(argvars, &rettv);
1189 lnum = (linenr_T)rettv.vval.v_number;
1190 clear_tv(&rettv);
1191 }
1192 return lnum;
1193}
1194
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001195/*
1196 * Get the lnum from the first argument.
1197 * Also accepts "$", then "buf" is used.
1198 * Returns 0 on error.
1199 */
1200 static linenr_T
1201tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1202{
1203 if (argvars[0].v_type == VAR_STRING
1204 && argvars[0].vval.v_string != NULL
1205 && argvars[0].vval.v_string[0] == '$'
1206 && buf != NULL)
1207 return buf->b_ml.ml_line_count;
1208 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1209}
1210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001212/*
1213 * Get the float value of "argvars[0]" into "f".
1214 * Returns FAIL when the argument is not a Number or Float.
1215 */
1216 static int
1217get_float_arg(typval_T *argvars, float_T *f)
1218{
1219 if (argvars[0].v_type == VAR_FLOAT)
1220 {
1221 *f = argvars[0].vval.v_float;
1222 return OK;
1223 }
1224 if (argvars[0].v_type == VAR_NUMBER)
1225 {
1226 *f = (float_T)argvars[0].vval.v_number;
1227 return OK;
1228 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001229 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001230 return FAIL;
1231}
1232
1233/*
1234 * "abs(expr)" function
1235 */
1236 static void
1237f_abs(typval_T *argvars, typval_T *rettv)
1238{
1239 if (argvars[0].v_type == VAR_FLOAT)
1240 {
1241 rettv->v_type = VAR_FLOAT;
1242 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1243 }
1244 else
1245 {
1246 varnumber_T n;
1247 int error = FALSE;
1248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001249 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001250 if (error)
1251 rettv->vval.v_number = -1;
1252 else if (n > 0)
1253 rettv->vval.v_number = n;
1254 else
1255 rettv->vval.v_number = -n;
1256 }
1257}
1258
1259/*
1260 * "acos()" function
1261 */
1262 static void
1263f_acos(typval_T *argvars, typval_T *rettv)
1264{
1265 float_T f = 0.0;
1266
1267 rettv->v_type = VAR_FLOAT;
1268 if (get_float_arg(argvars, &f) == OK)
1269 rettv->vval.v_float = acos(f);
1270 else
1271 rettv->vval.v_float = 0.0;
1272}
1273#endif
1274
1275/*
1276 * "add(list, item)" function
1277 */
1278 static void
1279f_add(typval_T *argvars, typval_T *rettv)
1280{
1281 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283
1284 rettv->vval.v_number = 1; /* Default: Failed */
1285 if (argvars[0].v_type == VAR_LIST)
1286 {
1287 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001288 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001289 (char_u *)N_("add() argument"), TRUE)
1290 && list_append_tv(l, &argvars[1]) == OK)
1291 copy_tv(&argvars[0], rettv);
1292 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 else if (argvars[0].v_type == VAR_BLOB)
1294 {
1295 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001296 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297 (char_u *)N_("add() argument"), TRUE))
1298 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001299 int error = FALSE;
1300 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1301
1302 if (!error)
1303 {
1304 ga_append(&b->bv_ga, (int)n);
1305 copy_tv(&argvars[0], rettv);
1306 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
1308 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001309 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001310 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001311}
1312
1313/*
1314 * "and(expr, expr)" function
1315 */
1316 static void
1317f_and(typval_T *argvars, typval_T *rettv)
1318{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1320 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001321}
1322
1323/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001324 * If there is a window for "curbuf", make it the current window.
1325 */
1326 static void
1327find_win_for_curbuf(void)
1328{
1329 wininfo_T *wip;
1330
1331 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1332 {
1333 if (wip->wi_win != NULL)
1334 {
1335 curwin = wip->wi_win;
1336 break;
1337 }
1338 }
1339}
1340
1341/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001342 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 */
1344 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001345set_buffer_lines(
1346 buf_T *buf,
1347 linenr_T lnum_arg,
1348 int append,
1349 typval_T *lines,
1350 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001351{
Bram Moolenaarca851592018-06-06 21:04:07 +02001352 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1353 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001354 list_T *l = NULL;
1355 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001356 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001357 linenr_T append_lnum;
1358 buf_T *curbuf_save = NULL;
1359 win_T *curwin_save = NULL;
1360 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001361
Bram Moolenaarca851592018-06-06 21:04:07 +02001362 /* When using the current buffer ml_mfp will be set if needed. Useful when
1363 * setline() is used on startup. For other buffers the buffer must be
1364 * loaded. */
1365 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001366 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001367 rettv->vval.v_number = 1; /* FAIL */
1368 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001369 }
1370
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 curbuf_save = curbuf;
1374 curwin_save = curwin;
1375 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001376 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001377 }
1378
1379 if (append)
1380 // appendbufline() uses the line number below which we insert
1381 append_lnum = lnum - 1;
1382 else
1383 // setbufline() uses the line number above which we insert, we only
1384 // append if it's below the last line
1385 append_lnum = curbuf->b_ml.ml_line_count;
1386
1387 if (lines->v_type == VAR_LIST)
1388 {
1389 l = lines->vval.v_list;
1390 li = l->lv_first;
1391 }
1392 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001393 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001394
1395 /* default result is zero == OK */
1396 for (;;)
1397 {
1398 if (l != NULL)
1399 {
1400 /* list argument, get next string */
1401 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001403 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 li = li->li_next;
1405 }
1406
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 rettv->vval.v_number = 1; /* FAIL */
1408 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1409 break;
1410
1411 /* When coming here from Insert mode, sync undo, so that this can be
1412 * undone separately from what was previously inserted. */
1413 if (u_sync_once == 2)
1414 {
1415 u_sync_once = 1; /* notify that u_sync() was called */
1416 u_sync(TRUE);
1417 }
1418
1419 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1420 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001421 // Existing line, replace it.
1422 // Removes any existing text properties.
1423 if (u_savesub(lnum) == OK && ml_replace_len(
1424 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001425 {
1426 changed_bytes(lnum, 0);
1427 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1428 check_cursor_col();
1429 rettv->vval.v_number = 0; /* OK */
1430 }
1431 }
1432 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1433 {
1434 /* append the line */
1435 ++added;
1436 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1437 rettv->vval.v_number = 0; /* OK */
1438 }
1439
1440 if (l == NULL) /* only one string argument */
1441 break;
1442 ++lnum;
1443 }
1444
1445 if (added > 0)
1446 {
1447 win_T *wp;
1448 tabpage_T *tp;
1449
1450 appended_lines_mark(append_lnum, added);
1451 FOR_ALL_TAB_WINDOWS(tp, wp)
1452 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1453 wp->w_cursor.lnum += added;
1454 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001455 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001457
1458 if (!is_curbuf)
1459 {
1460 curbuf = curbuf_save;
1461 curwin = curwin_save;
1462 }
1463}
1464
1465/*
1466 * "append(lnum, string/list)" function
1467 */
1468 static void
1469f_append(typval_T *argvars, typval_T *rettv)
1470{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001471 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001472
1473 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1474}
1475
1476/*
1477 * "appendbufline(buf, lnum, string/list)" function
1478 */
1479 static void
1480f_appendbufline(typval_T *argvars, typval_T *rettv)
1481{
1482 linenr_T lnum;
1483 buf_T *buf;
1484
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001485 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001486 if (buf == NULL)
1487 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001488 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001490 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001491 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1492 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493}
1494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495#ifdef FEAT_FLOAT
1496/*
1497 * "asin()" function
1498 */
1499 static void
1500f_asin(typval_T *argvars, typval_T *rettv)
1501{
1502 float_T f = 0.0;
1503
1504 rettv->v_type = VAR_FLOAT;
1505 if (get_float_arg(argvars, &f) == OK)
1506 rettv->vval.v_float = asin(f);
1507 else
1508 rettv->vval.v_float = 0.0;
1509}
1510
1511/*
1512 * "atan()" function
1513 */
1514 static void
1515f_atan(typval_T *argvars, typval_T *rettv)
1516{
1517 float_T f = 0.0;
1518
1519 rettv->v_type = VAR_FLOAT;
1520 if (get_float_arg(argvars, &f) == OK)
1521 rettv->vval.v_float = atan(f);
1522 else
1523 rettv->vval.v_float = 0.0;
1524}
1525
1526/*
1527 * "atan2()" function
1528 */
1529 static void
1530f_atan2(typval_T *argvars, typval_T *rettv)
1531{
1532 float_T fx = 0.0, fy = 0.0;
1533
1534 rettv->v_type = VAR_FLOAT;
1535 if (get_float_arg(argvars, &fx) == OK
1536 && get_float_arg(&argvars[1], &fy) == OK)
1537 rettv->vval.v_float = atan2(fx, fy);
1538 else
1539 rettv->vval.v_float = 0.0;
1540}
1541#endif
1542
1543/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001544 * "balloon_show()" function
1545 */
1546#ifdef FEAT_BEVAL
1547 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001548f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1549{
1550 rettv->v_type = VAR_STRING;
1551 if (balloonEval != NULL)
1552 {
1553 if (balloonEval->msg == NULL)
1554 rettv->vval.v_string = NULL;
1555 else
1556 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1557 }
1558}
1559
1560 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001561f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1562{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001563 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001564 {
1565 if (argvars[0].v_type == VAR_LIST
1566# ifdef FEAT_GUI
1567 && !gui.in_use
1568# endif
1569 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001570 {
1571 list_T *l = argvars[0].vval.v_list;
1572
1573 // empty list removes the balloon
1574 post_balloon(balloonEval, NULL,
1575 l == NULL || l->lv_len == 0 ? NULL : l);
1576 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001577 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001578 {
1579 char_u *mesg = tv_get_string_chk(&argvars[0]);
1580
1581 if (mesg != NULL)
1582 // empty string removes the balloon
1583 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1584 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001585 }
1586}
1587
Bram Moolenaar669a8282017-11-19 20:13:05 +01001588# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001589 static void
1590f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1591{
1592 if (rettv_list_alloc(rettv) == OK)
1593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001594 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001595
1596 if (msg != NULL)
1597 {
1598 pumitem_T *array;
1599 int size = split_message(msg, &array);
1600 int i;
1601
1602 /* Skip the first and last item, they are always empty. */
1603 for (i = 1; i < size - 1; ++i)
1604 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001605 while (size > 0)
1606 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001607 vim_free(array);
1608 }
1609 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001610}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001611# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001612#endif
1613
1614/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615 * "browse(save, title, initdir, default)" function
1616 */
1617 static void
1618f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1619{
1620#ifdef FEAT_BROWSE
1621 int save;
1622 char_u *title;
1623 char_u *initdir;
1624 char_u *defname;
1625 char_u buf[NUMBUFLEN];
1626 char_u buf2[NUMBUFLEN];
1627 int error = FALSE;
1628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 save = (int)tv_get_number_chk(&argvars[0], &error);
1630 title = tv_get_string_chk(&argvars[1]);
1631 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1632 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633
1634 if (error || title == NULL || initdir == NULL || defname == NULL)
1635 rettv->vval.v_string = NULL;
1636 else
1637 rettv->vval.v_string =
1638 do_browse(save ? BROWSE_SAVE : 0,
1639 title, defname, NULL, initdir, NULL, curbuf);
1640#else
1641 rettv->vval.v_string = NULL;
1642#endif
1643 rettv->v_type = VAR_STRING;
1644}
1645
1646/*
1647 * "browsedir(title, initdir)" function
1648 */
1649 static void
1650f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1651{
1652#ifdef FEAT_BROWSE
1653 char_u *title;
1654 char_u *initdir;
1655 char_u buf[NUMBUFLEN];
1656
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001657 title = tv_get_string_chk(&argvars[0]);
1658 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659
1660 if (title == NULL || initdir == NULL)
1661 rettv->vval.v_string = NULL;
1662 else
1663 rettv->vval.v_string = do_browse(BROWSE_DIR,
1664 title, NULL, NULL, initdir, NULL, curbuf);
1665#else
1666 rettv->vval.v_string = NULL;
1667#endif
1668 rettv->v_type = VAR_STRING;
1669}
1670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671/*
1672 * Find a buffer by number or exact name.
1673 */
1674 static buf_T *
1675find_buffer(typval_T *avar)
1676{
1677 buf_T *buf = NULL;
1678
1679 if (avar->v_type == VAR_NUMBER)
1680 buf = buflist_findnr((int)avar->vval.v_number);
1681 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1682 {
1683 buf = buflist_findname_exp(avar->vval.v_string);
1684 if (buf == NULL)
1685 {
1686 /* No full path name match, try a match with a URL or a "nofile"
1687 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001688 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689 if (buf->b_fname != NULL
1690 && (path_with_url(buf->b_fname)
1691#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001692 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693#endif
1694 )
1695 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1696 break;
1697 }
1698 }
1699 return buf;
1700}
1701
1702/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001703 * "bufadd(expr)" function
1704 */
1705 static void
1706f_bufadd(typval_T *argvars, typval_T *rettv)
1707{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001708 char_u *name = tv_get_string(&argvars[0]);
1709
1710 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001711}
1712
1713/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 * "bufexists(expr)" function
1715 */
1716 static void
1717f_bufexists(typval_T *argvars, typval_T *rettv)
1718{
1719 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1720}
1721
1722/*
1723 * "buflisted(expr)" function
1724 */
1725 static void
1726f_buflisted(typval_T *argvars, typval_T *rettv)
1727{
1728 buf_T *buf;
1729
1730 buf = find_buffer(&argvars[0]);
1731 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1732}
1733
1734/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001735 * "bufload(expr)" function
1736 */
1737 static void
1738f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1739{
1740 buf_T *buf = get_buf_arg(&argvars[0]);
1741
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001742 if (buf != NULL)
1743 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001744}
1745
1746/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001747 * "bufloaded(expr)" function
1748 */
1749 static void
1750f_bufloaded(typval_T *argvars, typval_T *rettv)
1751{
1752 buf_T *buf;
1753
1754 buf = find_buffer(&argvars[0]);
1755 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1756}
1757
1758 buf_T *
1759buflist_find_by_name(char_u *name, int curtab_only)
1760{
1761 int save_magic;
1762 char_u *save_cpo;
1763 buf_T *buf;
1764
1765 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1766 save_magic = p_magic;
1767 p_magic = TRUE;
1768 save_cpo = p_cpo;
1769 p_cpo = (char_u *)"";
1770
1771 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1772 TRUE, FALSE, curtab_only));
1773
1774 p_magic = save_magic;
1775 p_cpo = save_cpo;
1776 return buf;
1777}
1778
1779/*
1780 * Get buffer by number or pattern.
1781 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001782 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001783tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001784{
1785 char_u *name = tv->vval.v_string;
1786 buf_T *buf;
1787
1788 if (tv->v_type == VAR_NUMBER)
1789 return buflist_findnr((int)tv->vval.v_number);
1790 if (tv->v_type != VAR_STRING)
1791 return NULL;
1792 if (name == NULL || *name == NUL)
1793 return curbuf;
1794 if (name[0] == '$' && name[1] == NUL)
1795 return lastbuf;
1796
1797 buf = buflist_find_by_name(name, curtab_only);
1798
1799 /* If not found, try expanding the name, like done for bufexists(). */
1800 if (buf == NULL)
1801 buf = find_buffer(tv);
1802
1803 return buf;
1804}
1805
1806/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001807 * Get the buffer from "arg" and give an error and return NULL if it is not
1808 * valid.
1809 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001810 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001811get_buf_arg(typval_T *arg)
1812{
1813 buf_T *buf;
1814
1815 ++emsg_off;
1816 buf = tv_get_buf(arg, FALSE);
1817 --emsg_off;
1818 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001819 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001820 return buf;
1821}
1822
1823/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001824 * "bufname(expr)" function
1825 */
1826 static void
1827f_bufname(typval_T *argvars, typval_T *rettv)
1828{
1829 buf_T *buf;
1830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001831 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001833 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001834 rettv->v_type = VAR_STRING;
1835 if (buf != NULL && buf->b_fname != NULL)
1836 rettv->vval.v_string = vim_strsave(buf->b_fname);
1837 else
1838 rettv->vval.v_string = NULL;
1839 --emsg_off;
1840}
1841
1842/*
1843 * "bufnr(expr)" function
1844 */
1845 static void
1846f_bufnr(typval_T *argvars, typval_T *rettv)
1847{
1848 buf_T *buf;
1849 int error = FALSE;
1850 char_u *name;
1851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001852 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001853 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001854 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 --emsg_off;
1856
1857 /* If the buffer isn't found and the second argument is not zero create a
1858 * new buffer. */
1859 if (buf == NULL
1860 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001861 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001862 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001863 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001864 && !error)
1865 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1866
1867 if (buf != NULL)
1868 rettv->vval.v_number = buf->b_fnum;
1869 else
1870 rettv->vval.v_number = -1;
1871}
1872
1873 static void
1874buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1875{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001876 win_T *wp;
1877 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001878 buf_T *buf;
1879
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001880 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001881 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001882 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001883 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001884 {
1885 ++winnr;
1886 if (wp->w_buffer == buf)
1887 break;
1888 }
1889 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890 --emsg_off;
1891}
1892
1893/*
1894 * "bufwinid(nr)" function
1895 */
1896 static void
1897f_bufwinid(typval_T *argvars, typval_T *rettv)
1898{
1899 buf_win_common(argvars, rettv, FALSE);
1900}
1901
1902/*
1903 * "bufwinnr(nr)" function
1904 */
1905 static void
1906f_bufwinnr(typval_T *argvars, typval_T *rettv)
1907{
1908 buf_win_common(argvars, rettv, TRUE);
1909}
1910
1911/*
1912 * "byte2line(byte)" function
1913 */
1914 static void
1915f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1916{
1917#ifndef FEAT_BYTEOFF
1918 rettv->vval.v_number = -1;
1919#else
1920 long boff = 0;
1921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001922 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923 if (boff < 0)
1924 rettv->vval.v_number = -1;
1925 else
1926 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1927 (linenr_T)0, &boff);
1928#endif
1929}
1930
1931 static void
1932byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1933{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001935 char_u *str;
1936 varnumber_T idx;
1937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001938 str = tv_get_string_chk(&argvars[0]);
1939 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001940 rettv->vval.v_number = -1;
1941 if (str == NULL || idx < 0)
1942 return;
1943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001944 t = str;
1945 for ( ; idx > 0; idx--)
1946 {
1947 if (*t == NUL) /* EOL reached */
1948 return;
1949 if (enc_utf8 && comp)
1950 t += utf_ptr2len(t);
1951 else
1952 t += (*mb_ptr2len)(t);
1953 }
1954 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001955}
1956
1957/*
1958 * "byteidx()" function
1959 */
1960 static void
1961f_byteidx(typval_T *argvars, typval_T *rettv)
1962{
1963 byteidx(argvars, rettv, FALSE);
1964}
1965
1966/*
1967 * "byteidxcomp()" function
1968 */
1969 static void
1970f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1971{
1972 byteidx(argvars, rettv, TRUE);
1973}
1974
1975/*
1976 * "call(func, arglist [, dict])" function
1977 */
1978 static void
1979f_call(typval_T *argvars, typval_T *rettv)
1980{
1981 char_u *func;
1982 partial_T *partial = NULL;
1983 dict_T *selfdict = NULL;
1984
1985 if (argvars[1].v_type != VAR_LIST)
1986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001987 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001988 return;
1989 }
1990 if (argvars[1].vval.v_list == NULL)
1991 return;
1992
1993 if (argvars[0].v_type == VAR_FUNC)
1994 func = argvars[0].vval.v_string;
1995 else if (argvars[0].v_type == VAR_PARTIAL)
1996 {
1997 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001998 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001999 }
2000 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002001 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 if (*func == NUL)
2003 return; /* type error or empty name */
2004
2005 if (argvars[2].v_type != VAR_UNKNOWN)
2006 {
2007 if (argvars[2].v_type != VAR_DICT)
2008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002010 return;
2011 }
2012 selfdict = argvars[2].vval.v_dict;
2013 }
2014
2015 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2016}
2017
2018#ifdef FEAT_FLOAT
2019/*
2020 * "ceil({float})" function
2021 */
2022 static void
2023f_ceil(typval_T *argvars, typval_T *rettv)
2024{
2025 float_T f = 0.0;
2026
2027 rettv->v_type = VAR_FLOAT;
2028 if (get_float_arg(argvars, &f) == OK)
2029 rettv->vval.v_float = ceil(f);
2030 else
2031 rettv->vval.v_float = 0.0;
2032}
2033#endif
2034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002035/*
2036 * "changenr()" function
2037 */
2038 static void
2039f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2040{
2041 rettv->vval.v_number = curbuf->b_u_seq_cur;
2042}
2043
2044/*
2045 * "char2nr(string)" function
2046 */
2047 static void
2048f_char2nr(typval_T *argvars, typval_T *rettv)
2049{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 if (has_mbyte)
2051 {
2052 int utf8 = 0;
2053
2054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002055 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002056
2057 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002058 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002059 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002060 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 }
2062 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002063 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064}
2065
2066/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002067 * "chdir(dir)" function
2068 */
2069 static void
2070f_chdir(typval_T *argvars, typval_T *rettv)
2071{
2072 char_u *cwd;
2073 cdscope_T scope = CDSCOPE_GLOBAL;
2074
2075 rettv->v_type = VAR_STRING;
2076 rettv->vval.v_string = NULL;
2077
2078 if (argvars[0].v_type != VAR_STRING)
2079 return;
2080
2081 // Return the current directory
2082 cwd = alloc(MAXPATHL);
2083 if (cwd != NULL)
2084 {
2085 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2086 {
2087#ifdef BACKSLASH_IN_FILENAME
2088 slash_adjust(cwd);
2089#endif
2090 rettv->vval.v_string = vim_strsave(cwd);
2091 }
2092 vim_free(cwd);
2093 }
2094
2095 if (curwin->w_localdir != NULL)
2096 scope = CDSCOPE_WINDOW;
2097 else if (curtab->tp_localdir != NULL)
2098 scope = CDSCOPE_TABPAGE;
2099
2100 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2101 // Directory change failed
2102 VIM_CLEAR(rettv->vval.v_string);
2103}
2104
2105/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 * "cindent(lnum)" function
2107 */
2108 static void
2109f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2110{
2111#ifdef FEAT_CINDENT
2112 pos_T pos;
2113 linenr_T lnum;
2114
2115 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002116 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002117 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2118 {
2119 curwin->w_cursor.lnum = lnum;
2120 rettv->vval.v_number = get_c_indent();
2121 curwin->w_cursor = pos;
2122 }
2123 else
2124#endif
2125 rettv->vval.v_number = -1;
2126}
2127
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002128 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002129get_optional_window(typval_T *argvars, int idx)
2130{
2131 win_T *win = curwin;
2132
2133 if (argvars[idx].v_type != VAR_UNKNOWN)
2134 {
2135 win = find_win_by_nr_or_id(&argvars[idx]);
2136 if (win == NULL)
2137 {
2138 emsg(_(e_invalwindow));
2139 return NULL;
2140 }
2141 }
2142 return win;
2143}
2144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146 * "col(string)" function
2147 */
2148 static void
2149f_col(typval_T *argvars, typval_T *rettv)
2150{
2151 colnr_T col = 0;
2152 pos_T *fp;
2153 int fnum = curbuf->b_fnum;
2154
2155 fp = var2fpos(&argvars[0], FALSE, &fnum);
2156 if (fp != NULL && fnum == curbuf->b_fnum)
2157 {
2158 if (fp->col == MAXCOL)
2159 {
2160 /* '> can be MAXCOL, get the length of the line then */
2161 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2162 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2163 else
2164 col = MAXCOL;
2165 }
2166 else
2167 {
2168 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 /* col(".") when the cursor is on the NUL at the end of the line
2170 * because of "coladd" can be seen as an extra column. */
2171 if (virtual_active() && fp == &curwin->w_cursor)
2172 {
2173 char_u *p = ml_get_cursor();
2174
2175 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2176 curwin->w_virtcol - curwin->w_cursor.coladd))
2177 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178 int l;
2179
2180 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2181 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182 }
2183 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184 }
2185 }
2186 rettv->vval.v_number = col;
2187}
2188
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002189/*
2190 * "confirm(message, buttons[, default [, type]])" function
2191 */
2192 static void
2193f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2194{
2195#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2196 char_u *message;
2197 char_u *buttons = NULL;
2198 char_u buf[NUMBUFLEN];
2199 char_u buf2[NUMBUFLEN];
2200 int def = 1;
2201 int type = VIM_GENERIC;
2202 char_u *typestr;
2203 int error = FALSE;
2204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002205 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002206 if (message == NULL)
2207 error = TRUE;
2208 if (argvars[1].v_type != VAR_UNKNOWN)
2209 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002210 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 if (buttons == NULL)
2212 error = TRUE;
2213 if (argvars[2].v_type != VAR_UNKNOWN)
2214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002215 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 if (argvars[3].v_type != VAR_UNKNOWN)
2217 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 if (typestr == NULL)
2220 error = TRUE;
2221 else
2222 {
2223 switch (TOUPPER_ASC(*typestr))
2224 {
2225 case 'E': type = VIM_ERROR; break;
2226 case 'Q': type = VIM_QUESTION; break;
2227 case 'I': type = VIM_INFO; break;
2228 case 'W': type = VIM_WARNING; break;
2229 case 'G': type = VIM_GENERIC; break;
2230 }
2231 }
2232 }
2233 }
2234 }
2235
2236 if (buttons == NULL || *buttons == NUL)
2237 buttons = (char_u *)_("&Ok");
2238
2239 if (!error)
2240 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2241 def, NULL, FALSE);
2242#endif
2243}
2244
2245/*
2246 * "copy()" function
2247 */
2248 static void
2249f_copy(typval_T *argvars, typval_T *rettv)
2250{
2251 item_copy(&argvars[0], rettv, FALSE, 0);
2252}
2253
2254#ifdef FEAT_FLOAT
2255/*
2256 * "cos()" function
2257 */
2258 static void
2259f_cos(typval_T *argvars, typval_T *rettv)
2260{
2261 float_T f = 0.0;
2262
2263 rettv->v_type = VAR_FLOAT;
2264 if (get_float_arg(argvars, &f) == OK)
2265 rettv->vval.v_float = cos(f);
2266 else
2267 rettv->vval.v_float = 0.0;
2268}
2269
2270/*
2271 * "cosh()" function
2272 */
2273 static void
2274f_cosh(typval_T *argvars, typval_T *rettv)
2275{
2276 float_T f = 0.0;
2277
2278 rettv->v_type = VAR_FLOAT;
2279 if (get_float_arg(argvars, &f) == OK)
2280 rettv->vval.v_float = cosh(f);
2281 else
2282 rettv->vval.v_float = 0.0;
2283}
2284#endif
2285
2286/*
2287 * "count()" function
2288 */
2289 static void
2290f_count(typval_T *argvars, typval_T *rettv)
2291{
2292 long n = 0;
2293 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002294 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295
Bram Moolenaar9966b212017-07-28 16:46:57 +02002296 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002297 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002298
2299 if (argvars[0].v_type == VAR_STRING)
2300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002301 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002302 char_u *p = argvars[0].vval.v_string;
2303 char_u *next;
2304
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002305 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002306 {
2307 if (ic)
2308 {
2309 size_t len = STRLEN(expr);
2310
2311 while (*p != NUL)
2312 {
2313 if (MB_STRNICMP(p, expr, len) == 0)
2314 {
2315 ++n;
2316 p += len;
2317 }
2318 else
2319 MB_PTR_ADV(p);
2320 }
2321 }
2322 else
2323 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2324 != NULL)
2325 {
2326 ++n;
2327 p = next + STRLEN(expr);
2328 }
2329 }
2330
2331 }
2332 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002333 {
2334 listitem_T *li;
2335 list_T *l;
2336 long idx;
2337
2338 if ((l = argvars[0].vval.v_list) != NULL)
2339 {
2340 li = l->lv_first;
2341 if (argvars[2].v_type != VAR_UNKNOWN)
2342 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002343 if (argvars[3].v_type != VAR_UNKNOWN)
2344 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002345 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346 if (!error)
2347 {
2348 li = list_find(l, idx);
2349 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002350 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351 }
2352 }
2353 if (error)
2354 li = NULL;
2355 }
2356
2357 for ( ; li != NULL; li = li->li_next)
2358 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2359 ++n;
2360 }
2361 }
2362 else if (argvars[0].v_type == VAR_DICT)
2363 {
2364 int todo;
2365 dict_T *d;
2366 hashitem_T *hi;
2367
2368 if ((d = argvars[0].vval.v_dict) != NULL)
2369 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002370 if (argvars[2].v_type != VAR_UNKNOWN)
2371 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002373 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002374 }
2375
2376 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2377 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2378 {
2379 if (!HASHITEM_EMPTY(hi))
2380 {
2381 --todo;
2382 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2383 ++n;
2384 }
2385 }
2386 }
2387 }
2388 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002389 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002390 rettv->vval.v_number = n;
2391}
2392
2393/*
2394 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2395 *
2396 * Checks the existence of a cscope connection.
2397 */
2398 static void
2399f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2400{
2401#ifdef FEAT_CSCOPE
2402 int num = 0;
2403 char_u *dbpath = NULL;
2404 char_u *prepend = NULL;
2405 char_u buf[NUMBUFLEN];
2406
2407 if (argvars[0].v_type != VAR_UNKNOWN
2408 && argvars[1].v_type != VAR_UNKNOWN)
2409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002410 num = (int)tv_get_number(&argvars[0]);
2411 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002413 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414 }
2415
2416 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2417#endif
2418}
2419
2420/*
2421 * "cursor(lnum, col)" function, or
2422 * "cursor(list)"
2423 *
2424 * Moves the cursor to the specified line and column.
2425 * Returns 0 when the position could be set, -1 otherwise.
2426 */
2427 static void
2428f_cursor(typval_T *argvars, typval_T *rettv)
2429{
2430 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002432 int set_curswant = TRUE;
2433
2434 rettv->vval.v_number = -1;
2435 if (argvars[1].v_type == VAR_UNKNOWN)
2436 {
2437 pos_T pos;
2438 colnr_T curswant = -1;
2439
2440 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002442 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002443 return;
2444 }
2445 line = pos.lnum;
2446 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002447 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 if (curswant >= 0)
2449 {
2450 curwin->w_curswant = curswant - 1;
2451 set_curswant = FALSE;
2452 }
2453 }
2454 else
2455 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002456 line = tv_get_lnum(argvars);
2457 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002459 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002461 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002462 return; /* type error; errmsg already given */
2463 if (line > 0)
2464 curwin->w_cursor.lnum = line;
2465 if (col > 0)
2466 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468
2469 /* Make sure the cursor is in a valid position. */
2470 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 /* Correct cursor for multi-byte character. */
2472 if (has_mbyte)
2473 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474
2475 curwin->w_set_curswant = set_curswant;
2476 rettv->vval.v_number = 0;
2477}
2478
Bram Moolenaar4f974752019-02-17 17:44:42 +01002479#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002480/*
2481 * "debugbreak()" function
2482 */
2483 static void
2484f_debugbreak(typval_T *argvars, typval_T *rettv)
2485{
2486 int pid;
2487
2488 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002489 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002490 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002491 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002492 else
2493 {
2494 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2495
2496 if (hProcess != NULL)
2497 {
2498 DebugBreakProcess(hProcess);
2499 CloseHandle(hProcess);
2500 rettv->vval.v_number = OK;
2501 }
2502 }
2503}
2504#endif
2505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002506/*
2507 * "deepcopy()" function
2508 */
2509 static void
2510f_deepcopy(typval_T *argvars, typval_T *rettv)
2511{
2512 int noref = 0;
2513 int copyID;
2514
2515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002516 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002518 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519 else
2520 {
2521 copyID = get_copyID();
2522 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2523 }
2524}
2525
2526/*
2527 * "delete()" function
2528 */
2529 static void
2530f_delete(typval_T *argvars, typval_T *rettv)
2531{
2532 char_u nbuf[NUMBUFLEN];
2533 char_u *name;
2534 char_u *flags;
2535
2536 rettv->vval.v_number = -1;
2537 if (check_restricted() || check_secure())
2538 return;
2539
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541 if (name == NULL || *name == NUL)
2542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002543 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002544 return;
2545 }
2546
2547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002548 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 else
2550 flags = (char_u *)"";
2551
2552 if (*flags == NUL)
2553 /* delete a file */
2554 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2555 else if (STRCMP(flags, "d") == 0)
2556 /* delete an empty directory */
2557 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2558 else if (STRCMP(flags, "rf") == 0)
2559 /* delete a directory recursively */
2560 rettv->vval.v_number = delete_recursive(name);
2561 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563}
2564
2565/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002566 * "deletebufline()" function
2567 */
2568 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002569f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002570{
2571 buf_T *buf;
2572 linenr_T first, last;
2573 linenr_T lnum;
2574 long count;
2575 int is_curbuf;
2576 buf_T *curbuf_save = NULL;
2577 win_T *curwin_save = NULL;
2578 tabpage_T *tp;
2579 win_T *wp;
2580
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002581 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002582 if (buf == NULL)
2583 {
2584 rettv->vval.v_number = 1; /* FAIL */
2585 return;
2586 }
2587 is_curbuf = buf == curbuf;
2588
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002589 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002590 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002591 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002592 else
2593 last = first;
2594
2595 if (buf->b_ml.ml_mfp == NULL || first < 1
2596 || first > buf->b_ml.ml_line_count || last < first)
2597 {
2598 rettv->vval.v_number = 1; /* FAIL */
2599 return;
2600 }
2601
2602 if (!is_curbuf)
2603 {
2604 curbuf_save = curbuf;
2605 curwin_save = curwin;
2606 curbuf = buf;
2607 find_win_for_curbuf();
2608 }
2609 if (last > curbuf->b_ml.ml_line_count)
2610 last = curbuf->b_ml.ml_line_count;
2611 count = last - first + 1;
2612
2613 // When coming here from Insert mode, sync undo, so that this can be
2614 // undone separately from what was previously inserted.
2615 if (u_sync_once == 2)
2616 {
2617 u_sync_once = 1; // notify that u_sync() was called
2618 u_sync(TRUE);
2619 }
2620
2621 if (u_save(first - 1, last + 1) == FAIL)
2622 {
2623 rettv->vval.v_number = 1; /* FAIL */
2624 return;
2625 }
2626
2627 for (lnum = first; lnum <= last; ++lnum)
2628 ml_delete(first, TRUE);
2629
2630 FOR_ALL_TAB_WINDOWS(tp, wp)
2631 if (wp->w_buffer == buf)
2632 {
2633 if (wp->w_cursor.lnum > last)
2634 wp->w_cursor.lnum -= count;
2635 else if (wp->w_cursor.lnum> first)
2636 wp->w_cursor.lnum = first;
2637 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2638 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2639 }
2640 check_cursor_col();
2641 deleted_lines_mark(first, count);
2642
2643 if (!is_curbuf)
2644 {
2645 curbuf = curbuf_save;
2646 curwin = curwin_save;
2647 }
2648}
2649
2650/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002651 * "did_filetype()" function
2652 */
2653 static void
2654f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2655{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657}
2658
2659/*
2660 * "diff_filler()" function
2661 */
2662 static void
2663f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2664{
2665#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002666 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667#endif
2668}
2669
2670/*
2671 * "diff_hlID()" function
2672 */
2673 static void
2674f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2675{
2676#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002677 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002679 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 static int fnum = 0;
2681 static int change_start = 0;
2682 static int change_end = 0;
2683 static hlf_T hlID = (hlf_T)0;
2684 int filler_lines;
2685 int col;
2686
2687 if (lnum < 0) /* ignore type error in {lnum} arg */
2688 lnum = 0;
2689 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002690 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 || fnum != curbuf->b_fnum)
2692 {
2693 /* New line, buffer, change: need to get the values. */
2694 filler_lines = diff_check(curwin, lnum);
2695 if (filler_lines < 0)
2696 {
2697 if (filler_lines == -1)
2698 {
2699 change_start = MAXCOL;
2700 change_end = -1;
2701 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2702 hlID = HLF_ADD; /* added line */
2703 else
2704 hlID = HLF_CHD; /* changed line */
2705 }
2706 else
2707 hlID = HLF_ADD; /* added line */
2708 }
2709 else
2710 hlID = (hlf_T)0;
2711 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002712 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 fnum = curbuf->b_fnum;
2714 }
2715
2716 if (hlID == HLF_CHD || hlID == HLF_TXD)
2717 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002718 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719 if (col >= change_start && col <= change_end)
2720 hlID = HLF_TXD; /* changed text */
2721 else
2722 hlID = HLF_CHD; /* changed line */
2723 }
2724 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2725#endif
2726}
2727
2728/*
2729 * "empty({expr})" function
2730 */
2731 static void
2732f_empty(typval_T *argvars, typval_T *rettv)
2733{
2734 int n = FALSE;
2735
2736 switch (argvars[0].v_type)
2737 {
2738 case VAR_STRING:
2739 case VAR_FUNC:
2740 n = argvars[0].vval.v_string == NULL
2741 || *argvars[0].vval.v_string == NUL;
2742 break;
2743 case VAR_PARTIAL:
2744 n = FALSE;
2745 break;
2746 case VAR_NUMBER:
2747 n = argvars[0].vval.v_number == 0;
2748 break;
2749 case VAR_FLOAT:
2750#ifdef FEAT_FLOAT
2751 n = argvars[0].vval.v_float == 0.0;
2752 break;
2753#endif
2754 case VAR_LIST:
2755 n = argvars[0].vval.v_list == NULL
2756 || argvars[0].vval.v_list->lv_first == NULL;
2757 break;
2758 case VAR_DICT:
2759 n = argvars[0].vval.v_dict == NULL
2760 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2761 break;
2762 case VAR_SPECIAL:
2763 n = argvars[0].vval.v_number != VVAL_TRUE;
2764 break;
2765
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002766 case VAR_BLOB:
2767 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002768 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2769 break;
2770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771 case VAR_JOB:
2772#ifdef FEAT_JOB_CHANNEL
2773 n = argvars[0].vval.v_job == NULL
2774 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2775 break;
2776#endif
2777 case VAR_CHANNEL:
2778#ifdef FEAT_JOB_CHANNEL
2779 n = argvars[0].vval.v_channel == NULL
2780 || !channel_is_open(argvars[0].vval.v_channel);
2781 break;
2782#endif
2783 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002784 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 n = TRUE;
2786 break;
2787 }
2788
2789 rettv->vval.v_number = n;
2790}
2791
2792/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002793 * "environ()" function
2794 */
2795 static void
2796f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2797{
2798#if !defined(AMIGA)
2799 int i = 0;
2800 char_u *entry, *value;
2801# ifdef MSWIN
2802 extern wchar_t **_wenviron;
2803# else
2804 extern char **environ;
2805# endif
2806
2807 if (rettv_dict_alloc(rettv) != OK)
2808 return;
2809
2810# ifdef MSWIN
2811 if (*_wenviron == NULL)
2812 return;
2813# else
2814 if (*environ == NULL)
2815 return;
2816# endif
2817
2818 for (i = 0; ; ++i)
2819 {
2820# ifdef MSWIN
2821 short_u *p;
2822
2823 if ((p = (short_u *)_wenviron[i]) == NULL)
2824 return;
2825 entry = utf16_to_enc(p, NULL);
2826# else
2827 if ((entry = (char_u *)environ[i]) == NULL)
2828 return;
2829 entry = vim_strsave(entry);
2830# endif
2831 if (entry == NULL) // out of memory
2832 return;
2833 if ((value = vim_strchr(entry, '=')) == NULL)
2834 {
2835 vim_free(entry);
2836 continue;
2837 }
2838 *value++ = NUL;
2839 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2840 vim_free(entry);
2841 }
2842#endif
2843}
2844
2845/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002846 * "escape({string}, {chars})" function
2847 */
2848 static void
2849f_escape(typval_T *argvars, typval_T *rettv)
2850{
2851 char_u buf[NUMBUFLEN];
2852
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002853 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2854 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 rettv->v_type = VAR_STRING;
2856}
2857
2858/*
2859 * "eval()" function
2860 */
2861 static void
2862f_eval(typval_T *argvars, typval_T *rettv)
2863{
2864 char_u *s, *p;
2865
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002866 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002867 if (s != NULL)
2868 s = skipwhite(s);
2869
2870 p = s;
2871 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2872 {
2873 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002874 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 need_clr_eos = FALSE;
2876 rettv->v_type = VAR_NUMBER;
2877 rettv->vval.v_number = 0;
2878 }
2879 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881}
2882
2883/*
2884 * "eventhandler()" function
2885 */
2886 static void
2887f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2888{
2889 rettv->vval.v_number = vgetc_busy;
2890}
2891
2892/*
2893 * "executable()" function
2894 */
2895 static void
2896f_executable(typval_T *argvars, typval_T *rettv)
2897{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002898 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899
2900 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02002901 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902}
2903
2904static garray_T redir_execute_ga;
2905
2906/*
2907 * Append "value[value_len]" to the execute() output.
2908 */
2909 void
2910execute_redir_str(char_u *value, int value_len)
2911{
2912 int len;
2913
2914 if (value_len == -1)
2915 len = (int)STRLEN(value); /* Append the entire string */
2916 else
2917 len = value_len; /* Append only "value_len" characters */
2918 if (ga_grow(&redir_execute_ga, len) == OK)
2919 {
2920 mch_memmove((char *)redir_execute_ga.ga_data
2921 + redir_execute_ga.ga_len, value, len);
2922 redir_execute_ga.ga_len += len;
2923 }
2924}
2925
2926/*
2927 * Get next line from a list.
2928 * Called by do_cmdline() to get the next line.
2929 * Returns allocated string, or NULL for end of function.
2930 */
2931
2932 static char_u *
2933get_list_line(
2934 int c UNUSED,
2935 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002936 int indent UNUSED,
2937 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938{
2939 listitem_T **p = (listitem_T **)cookie;
2940 listitem_T *item = *p;
2941 char_u buf[NUMBUFLEN];
2942 char_u *s;
2943
2944 if (item == NULL)
2945 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002946 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002947 *p = item->li_next;
2948 return s == NULL ? NULL : vim_strsave(s);
2949}
2950
2951/*
2952 * "execute()" function
2953 */
2954 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002955execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956{
2957 char_u *cmd = NULL;
2958 list_T *list = NULL;
2959 int save_msg_silent = msg_silent;
2960 int save_emsg_silent = emsg_silent;
2961 int save_emsg_noredir = emsg_noredir;
2962 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002963 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002964 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002965 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002966 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002967
2968 rettv->vval.v_string = NULL;
2969 rettv->v_type = VAR_STRING;
2970
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002971 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002972 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002973 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974 if (list == NULL || list->lv_first == NULL)
2975 /* empty list, no commands, empty output */
2976 return;
2977 ++list->lv_refcount;
2978 }
2979 else
2980 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002981 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 if (cmd == NULL)
2983 return;
2984 }
2985
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002986 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 {
2988 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002989 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990
2991 if (s == NULL)
2992 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002993 if (*s == NUL)
2994 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 if (STRNCMP(s, "silent", 6) == 0)
2996 ++msg_silent;
2997 if (STRCMP(s, "silent!") == 0)
2998 {
2999 emsg_silent = TRUE;
3000 emsg_noredir = TRUE;
3001 }
3002 }
3003 else
3004 ++msg_silent;
3005
3006 if (redir_execute)
3007 save_ga = redir_execute_ga;
3008 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3009 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003010 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003011 if (!echo_output)
3012 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013
3014 if (cmd != NULL)
3015 do_cmdline_cmd(cmd);
3016 else
3017 {
3018 listitem_T *item = list->lv_first;
3019
3020 do_cmdline(NULL, get_list_line, (void *)&item,
3021 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3022 --list->lv_refcount;
3023 }
3024
Bram Moolenaard297f352017-01-29 20:31:21 +01003025 /* Need to append a NUL to the result. */
3026 if (ga_grow(&redir_execute_ga, 1) == OK)
3027 {
3028 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3029 rettv->vval.v_string = redir_execute_ga.ga_data;
3030 }
3031 else
3032 {
3033 ga_clear(&redir_execute_ga);
3034 rettv->vval.v_string = NULL;
3035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 msg_silent = save_msg_silent;
3037 emsg_silent = save_emsg_silent;
3038 emsg_noredir = save_emsg_noredir;
3039
3040 redir_execute = save_redir_execute;
3041 if (redir_execute)
3042 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003043 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003045 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003046 if (echo_output)
3047 // When not working silently: put it in column zero. A following
3048 // "echon" will overwrite the message, unavoidably.
3049 msg_col = 0;
3050 else
3051 // When working silently: Put it back where it was, since nothing
3052 // should have been written.
3053 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054}
3055
3056/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003057 * "execute()" function
3058 */
3059 static void
3060f_execute(typval_T *argvars, typval_T *rettv)
3061{
3062 execute_common(argvars, rettv, 0);
3063}
3064
3065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003066 * "exepath()" function
3067 */
3068 static void
3069f_exepath(typval_T *argvars, typval_T *rettv)
3070{
3071 char_u *p = NULL;
3072
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003074 rettv->v_type = VAR_STRING;
3075 rettv->vval.v_string = p;
3076}
3077
3078/*
3079 * "exists()" function
3080 */
3081 static void
3082f_exists(typval_T *argvars, typval_T *rettv)
3083{
3084 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 if (*p == '$') /* environment variable */
3089 {
3090 /* first try "normal" environment variables (fast) */
3091 if (mch_getenv(p + 1) != NULL)
3092 n = TRUE;
3093 else
3094 {
3095 /* try expanding things like $VIM and ${HOME} */
3096 p = expand_env_save(p);
3097 if (p != NULL && *p != '$')
3098 n = TRUE;
3099 vim_free(p);
3100 }
3101 }
3102 else if (*p == '&' || *p == '+') /* option */
3103 {
3104 n = (get_option_tv(&p, NULL, TRUE) == OK);
3105 if (*skipwhite(p) != NUL)
3106 n = FALSE; /* trailing garbage */
3107 }
3108 else if (*p == '*') /* internal or user defined function */
3109 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003110 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 }
3112 else if (*p == ':')
3113 {
3114 n = cmd_exists(p + 1);
3115 }
3116 else if (*p == '#')
3117 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118 if (p[1] == '#')
3119 n = autocmd_supported(p + 2);
3120 else
3121 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122 }
3123 else /* internal variable */
3124 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003125 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003126 }
3127
3128 rettv->vval.v_number = n;
3129}
3130
3131#ifdef FEAT_FLOAT
3132/*
3133 * "exp()" function
3134 */
3135 static void
3136f_exp(typval_T *argvars, typval_T *rettv)
3137{
3138 float_T f = 0.0;
3139
3140 rettv->v_type = VAR_FLOAT;
3141 if (get_float_arg(argvars, &f) == OK)
3142 rettv->vval.v_float = exp(f);
3143 else
3144 rettv->vval.v_float = 0.0;
3145}
3146#endif
3147
3148/*
3149 * "expand()" function
3150 */
3151 static void
3152f_expand(typval_T *argvars, typval_T *rettv)
3153{
3154 char_u *s;
3155 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003156 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3158 expand_T xpc;
3159 int error = FALSE;
3160 char_u *result;
3161
3162 rettv->v_type = VAR_STRING;
3163 if (argvars[1].v_type != VAR_UNKNOWN
3164 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003165 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003166 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003167 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003170 if (*s == '%' || *s == '#' || *s == '<')
3171 {
3172 ++emsg_off;
3173 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3174 --emsg_off;
3175 if (rettv->v_type == VAR_LIST)
3176 {
3177 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3178 list_append_string(rettv->vval.v_list, result, -1);
3179 else
3180 vim_free(result);
3181 }
3182 else
3183 rettv->vval.v_string = result;
3184 }
3185 else
3186 {
3187 /* When the optional second argument is non-zero, don't remove matches
3188 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3189 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003190 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003191 options |= WILD_KEEP_ALL;
3192 if (!error)
3193 {
3194 ExpandInit(&xpc);
3195 xpc.xp_context = EXPAND_FILES;
3196 if (p_wic)
3197 options += WILD_ICASE;
3198 if (rettv->v_type == VAR_STRING)
3199 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3200 options, WILD_ALL);
3201 else if (rettv_list_alloc(rettv) != FAIL)
3202 {
3203 int i;
3204
3205 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3206 for (i = 0; i < xpc.xp_numfiles; i++)
3207 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3208 ExpandCleanup(&xpc);
3209 }
3210 }
3211 else
3212 rettv->vval.v_string = NULL;
3213 }
3214}
3215
3216/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003217 * "expandcmd()" function
3218 * Expand all the special characters in a command string.
3219 */
3220 static void
3221f_expandcmd(typval_T *argvars, typval_T *rettv)
3222{
3223 exarg_T eap;
3224 char_u *cmdstr;
3225 char *errormsg = NULL;
3226
3227 rettv->v_type = VAR_STRING;
3228 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3229
3230 memset(&eap, 0, sizeof(eap));
3231 eap.cmd = cmdstr;
3232 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003233 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003234 eap.usefilter = FALSE;
3235 eap.nextcmd = NULL;
3236 eap.cmdidx = CMD_USER;
3237
3238 expand_filename(&eap, &cmdstr, &errormsg);
3239 if (errormsg != NULL && *errormsg != NUL)
3240 emsg(errormsg);
3241
3242 rettv->vval.v_string = cmdstr;
3243}
3244
3245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 * "extend(list, list [, idx])" function
3247 * "extend(dict, dict [, action])" function
3248 */
3249 static void
3250f_extend(typval_T *argvars, typval_T *rettv)
3251{
3252 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3253
3254 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3255 {
3256 list_T *l1, *l2;
3257 listitem_T *item;
3258 long before;
3259 int error = FALSE;
3260
3261 l1 = argvars[0].vval.v_list;
3262 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003263 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 && l2 != NULL)
3265 {
3266 if (argvars[2].v_type != VAR_UNKNOWN)
3267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003268 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003269 if (error)
3270 return; /* type error; errmsg already given */
3271
3272 if (before == l1->lv_len)
3273 item = NULL;
3274 else
3275 {
3276 item = list_find(l1, before);
3277 if (item == NULL)
3278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003279 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 return;
3281 }
3282 }
3283 }
3284 else
3285 item = NULL;
3286 list_extend(l1, l2, item);
3287
3288 copy_tv(&argvars[0], rettv);
3289 }
3290 }
3291 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3292 {
3293 dict_T *d1, *d2;
3294 char_u *action;
3295 int i;
3296
3297 d1 = argvars[0].vval.v_dict;
3298 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003299 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 && d2 != NULL)
3301 {
3302 /* Check the third argument. */
3303 if (argvars[2].v_type != VAR_UNKNOWN)
3304 {
3305 static char *(av[]) = {"keep", "force", "error"};
3306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003307 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308 if (action == NULL)
3309 return; /* type error; errmsg already given */
3310 for (i = 0; i < 3; ++i)
3311 if (STRCMP(action, av[i]) == 0)
3312 break;
3313 if (i == 3)
3314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 return;
3317 }
3318 }
3319 else
3320 action = (char_u *)"force";
3321
3322 dict_extend(d1, d2, action);
3323
3324 copy_tv(&argvars[0], rettv);
3325 }
3326 }
3327 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003328 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003329}
3330
3331/*
3332 * "feedkeys()" function
3333 */
3334 static void
3335f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3336{
3337 int remap = TRUE;
3338 int insert = FALSE;
3339 char_u *keys, *flags;
3340 char_u nbuf[NUMBUFLEN];
3341 int typed = FALSE;
3342 int execute = FALSE;
3343 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003344 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345 char_u *keys_esc;
3346
3347 /* This is not allowed in the sandbox. If the commands would still be
3348 * executed in the sandbox it would be OK, but it probably happens later,
3349 * when "sandbox" is no longer set. */
3350 if (check_secure())
3351 return;
3352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003353 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354
3355 if (argvars[1].v_type != VAR_UNKNOWN)
3356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003357 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003358 for ( ; *flags != NUL; ++flags)
3359 {
3360 switch (*flags)
3361 {
3362 case 'n': remap = FALSE; break;
3363 case 'm': remap = TRUE; break;
3364 case 't': typed = TRUE; break;
3365 case 'i': insert = TRUE; break;
3366 case 'x': execute = TRUE; break;
3367 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003368 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 }
3370 }
3371 }
3372
3373 if (*keys != NUL || execute)
3374 {
3375 /* Need to escape K_SPECIAL and CSI before putting the string in the
3376 * typeahead buffer. */
3377 keys_esc = vim_strsave_escape_csi(keys);
3378 if (keys_esc != NULL)
3379 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003380 if (lowlevel)
3381 {
3382#ifdef USE_INPUT_BUF
3383 add_to_input_buf(keys, (int)STRLEN(keys));
3384#else
3385 emsg(_("E980: lowlevel input not supported"));
3386#endif
3387 }
3388 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003389 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003390 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003392 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003393#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003394 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003395#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003396 )
3397 typebuf_was_filled = TRUE;
3398 }
3399 vim_free(keys_esc);
3400
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003401 if (execute)
3402 {
3403 int save_msg_scroll = msg_scroll;
3404
3405 /* Avoid a 1 second delay when the keys start Insert mode. */
3406 msg_scroll = FALSE;
3407
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003408 if (!dangerous)
3409 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003410 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003411 if (!dangerous)
3412 --ex_normal_busy;
3413
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 msg_scroll |= save_msg_scroll;
3415 }
3416 }
3417 }
3418}
3419
3420/*
3421 * "filereadable()" function
3422 */
3423 static void
3424f_filereadable(typval_T *argvars, typval_T *rettv)
3425{
3426 int fd;
3427 char_u *p;
3428 int n;
3429
3430#ifndef O_NONBLOCK
3431# define O_NONBLOCK 0
3432#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3435 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3436 {
3437 n = TRUE;
3438 close(fd);
3439 }
3440 else
3441 n = FALSE;
3442
3443 rettv->vval.v_number = n;
3444}
3445
3446/*
3447 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3448 * rights to write into.
3449 */
3450 static void
3451f_filewritable(typval_T *argvars, typval_T *rettv)
3452{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003453 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003454}
3455
3456 static void
3457findfilendir(
3458 typval_T *argvars UNUSED,
3459 typval_T *rettv,
3460 int find_what UNUSED)
3461{
3462#ifdef FEAT_SEARCHPATH
3463 char_u *fname;
3464 char_u *fresult = NULL;
3465 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3466 char_u *p;
3467 char_u pathbuf[NUMBUFLEN];
3468 int count = 1;
3469 int first = TRUE;
3470 int error = FALSE;
3471#endif
3472
3473 rettv->vval.v_string = NULL;
3474 rettv->v_type = VAR_STRING;
3475
3476#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003477 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478
3479 if (argvars[1].v_type != VAR_UNKNOWN)
3480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003481 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 if (p == NULL)
3483 error = TRUE;
3484 else
3485 {
3486 if (*p != NUL)
3487 path = p;
3488
3489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003490 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003491 }
3492 }
3493
3494 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3495 error = TRUE;
3496
3497 if (*fname != NUL && !error)
3498 {
3499 do
3500 {
3501 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3502 vim_free(fresult);
3503 fresult = find_file_in_path_option(first ? fname : NULL,
3504 first ? (int)STRLEN(fname) : 0,
3505 0, first, path,
3506 find_what,
3507 curbuf->b_ffname,
3508 find_what == FINDFILE_DIR
3509 ? (char_u *)"" : curbuf->b_p_sua);
3510 first = FALSE;
3511
3512 if (fresult != NULL && rettv->v_type == VAR_LIST)
3513 list_append_string(rettv->vval.v_list, fresult, -1);
3514
3515 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3516 }
3517
3518 if (rettv->v_type == VAR_STRING)
3519 rettv->vval.v_string = fresult;
3520#endif
3521}
3522
3523/*
3524 * "filter()" function
3525 */
3526 static void
3527f_filter(typval_T *argvars, typval_T *rettv)
3528{
3529 filter_map(argvars, rettv, FALSE);
3530}
3531
3532/*
3533 * "finddir({fname}[, {path}[, {count}]])" function
3534 */
3535 static void
3536f_finddir(typval_T *argvars, typval_T *rettv)
3537{
3538 findfilendir(argvars, rettv, FINDFILE_DIR);
3539}
3540
3541/*
3542 * "findfile({fname}[, {path}[, {count}]])" function
3543 */
3544 static void
3545f_findfile(typval_T *argvars, typval_T *rettv)
3546{
3547 findfilendir(argvars, rettv, FINDFILE_FILE);
3548}
3549
3550#ifdef FEAT_FLOAT
3551/*
3552 * "float2nr({float})" function
3553 */
3554 static void
3555f_float2nr(typval_T *argvars, typval_T *rettv)
3556{
3557 float_T f = 0.0;
3558
3559 if (get_float_arg(argvars, &f) == OK)
3560 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003561 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003562 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003563 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003564 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003565 else
3566 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 }
3568}
3569
3570/*
3571 * "floor({float})" function
3572 */
3573 static void
3574f_floor(typval_T *argvars, typval_T *rettv)
3575{
3576 float_T f = 0.0;
3577
3578 rettv->v_type = VAR_FLOAT;
3579 if (get_float_arg(argvars, &f) == OK)
3580 rettv->vval.v_float = floor(f);
3581 else
3582 rettv->vval.v_float = 0.0;
3583}
3584
3585/*
3586 * "fmod()" function
3587 */
3588 static void
3589f_fmod(typval_T *argvars, typval_T *rettv)
3590{
3591 float_T fx = 0.0, fy = 0.0;
3592
3593 rettv->v_type = VAR_FLOAT;
3594 if (get_float_arg(argvars, &fx) == OK
3595 && get_float_arg(&argvars[1], &fy) == OK)
3596 rettv->vval.v_float = fmod(fx, fy);
3597 else
3598 rettv->vval.v_float = 0.0;
3599}
3600#endif
3601
3602/*
3603 * "fnameescape({string})" function
3604 */
3605 static void
3606f_fnameescape(typval_T *argvars, typval_T *rettv)
3607{
3608 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003609 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610 rettv->v_type = VAR_STRING;
3611}
3612
3613/*
3614 * "fnamemodify({fname}, {mods})" function
3615 */
3616 static void
3617f_fnamemodify(typval_T *argvars, typval_T *rettv)
3618{
3619 char_u *fname;
3620 char_u *mods;
3621 int usedlen = 0;
3622 int len;
3623 char_u *fbuf = NULL;
3624 char_u buf[NUMBUFLEN];
3625
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003626 fname = tv_get_string_chk(&argvars[0]);
3627 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628 if (fname == NULL || mods == NULL)
3629 fname = NULL;
3630 else
3631 {
3632 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003633 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634 }
3635
3636 rettv->v_type = VAR_STRING;
3637 if (fname == NULL)
3638 rettv->vval.v_string = NULL;
3639 else
3640 rettv->vval.v_string = vim_strnsave(fname, len);
3641 vim_free(fbuf);
3642}
3643
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644/*
3645 * "foldclosed()" function
3646 */
3647 static void
3648foldclosed_both(
3649 typval_T *argvars UNUSED,
3650 typval_T *rettv,
3651 int end UNUSED)
3652{
3653#ifdef FEAT_FOLDING
3654 linenr_T lnum;
3655 linenr_T first, last;
3656
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003657 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3659 {
3660 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3661 {
3662 if (end)
3663 rettv->vval.v_number = (varnumber_T)last;
3664 else
3665 rettv->vval.v_number = (varnumber_T)first;
3666 return;
3667 }
3668 }
3669#endif
3670 rettv->vval.v_number = -1;
3671}
3672
3673/*
3674 * "foldclosed()" function
3675 */
3676 static void
3677f_foldclosed(typval_T *argvars, typval_T *rettv)
3678{
3679 foldclosed_both(argvars, rettv, FALSE);
3680}
3681
3682/*
3683 * "foldclosedend()" function
3684 */
3685 static void
3686f_foldclosedend(typval_T *argvars, typval_T *rettv)
3687{
3688 foldclosed_both(argvars, rettv, TRUE);
3689}
3690
3691/*
3692 * "foldlevel()" function
3693 */
3694 static void
3695f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3696{
3697#ifdef FEAT_FOLDING
3698 linenr_T lnum;
3699
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003700 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3702 rettv->vval.v_number = foldLevel(lnum);
3703#endif
3704}
3705
3706/*
3707 * "foldtext()" function
3708 */
3709 static void
3710f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3711{
3712#ifdef FEAT_FOLDING
3713 linenr_T foldstart;
3714 linenr_T foldend;
3715 char_u *dashes;
3716 linenr_T lnum;
3717 char_u *s;
3718 char_u *r;
3719 int len;
3720 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003721 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003722#endif
3723
3724 rettv->v_type = VAR_STRING;
3725 rettv->vval.v_string = NULL;
3726#ifdef FEAT_FOLDING
3727 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3728 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3729 dashes = get_vim_var_str(VV_FOLDDASHES);
3730 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3731 && dashes != NULL)
3732 {
3733 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003734 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 if (!linewhite(lnum))
3736 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737
3738 /* Find interesting text in this line. */
3739 s = skipwhite(ml_get(lnum));
3740 /* skip C comment-start */
3741 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3742 {
3743 s = skipwhite(s + 2);
3744 if (*skipwhite(s) == NUL
3745 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3746 {
3747 s = skipwhite(ml_get(lnum + 1));
3748 if (*s == '*')
3749 s = skipwhite(s + 1);
3750 }
3751 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003752 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003753 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003754 r = alloc(STRLEN(txt)
3755 + STRLEN(dashes) // for %s
3756 + 20 // for %3ld
3757 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003758 if (r != NULL)
3759 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003760 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003761 len = (int)STRLEN(r);
3762 STRCAT(r, s);
3763 /* remove 'foldmarker' and 'commentstring' */
3764 foldtext_cleanup(r + len);
3765 rettv->vval.v_string = r;
3766 }
3767 }
3768#endif
3769}
3770
3771/*
3772 * "foldtextresult(lnum)" function
3773 */
3774 static void
3775f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3776{
3777#ifdef FEAT_FOLDING
3778 linenr_T lnum;
3779 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003780 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003781 foldinfo_T foldinfo;
3782 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003783 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784#endif
3785
3786 rettv->v_type = VAR_STRING;
3787 rettv->vval.v_string = NULL;
3788#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003789 if (entered)
3790 return; /* reject recursive use */
3791 entered = TRUE;
3792
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003793 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003794 /* treat illegal types and illegal string values for {lnum} the same */
3795 if (lnum < 0)
3796 lnum = 0;
3797 fold_count = foldedCount(curwin, lnum, &foldinfo);
3798 if (fold_count > 0)
3799 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003800 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3801 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 if (text == buf)
3803 text = vim_strsave(text);
3804 rettv->vval.v_string = text;
3805 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003806
3807 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808#endif
3809}
3810
3811/*
3812 * "foreground()" function
3813 */
3814 static void
3815f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3816{
3817#ifdef FEAT_GUI
3818 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003819 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003820 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003821 return;
3822 }
3823#endif
3824#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826#endif
3827}
3828
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003830common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003831{
3832 char_u *s;
3833 char_u *name;
3834 int use_string = FALSE;
3835 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837
3838 if (argvars[0].v_type == VAR_FUNC)
3839 {
3840 /* function(MyFunc, [arg], dict) */
3841 s = argvars[0].vval.v_string;
3842 }
3843 else if (argvars[0].v_type == VAR_PARTIAL
3844 && argvars[0].vval.v_partial != NULL)
3845 {
3846 /* function(dict.MyFunc, [arg]) */
3847 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003848 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003849 }
3850 else
3851 {
3852 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003853 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003854 use_string = TRUE;
3855 }
3856
Bram Moolenaar843b8842016-08-21 14:36:15 +02003857 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003858 {
3859 name = s;
3860 trans_name = trans_function_name(&name, FALSE,
3861 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3862 if (*name != NUL)
3863 s = NULL;
3864 }
3865
Bram Moolenaar843b8842016-08-21 14:36:15 +02003866 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3867 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 else if (trans_name != NULL && (is_funcref
3871 ? find_func(trans_name) == NULL
3872 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003873 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 else
3875 {
3876 int dict_idx = 0;
3877 int arg_idx = 0;
3878 list_T *list = NULL;
3879
3880 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3881 {
3882 char sid_buf[25];
3883 int off = *s == 's' ? 2 : 5;
3884
3885 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3886 * also be called from another script. Using trans_function_name()
3887 * would also work, but some plugins depend on the name being
3888 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003889 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003890 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003891 if (name != NULL)
3892 {
3893 STRCPY(name, sid_buf);
3894 STRCAT(name, s + off);
3895 }
3896 }
3897 else
3898 name = vim_strsave(s);
3899
3900 if (argvars[1].v_type != VAR_UNKNOWN)
3901 {
3902 if (argvars[2].v_type != VAR_UNKNOWN)
3903 {
3904 /* function(name, [args], dict) */
3905 arg_idx = 1;
3906 dict_idx = 2;
3907 }
3908 else if (argvars[1].v_type == VAR_DICT)
3909 /* function(name, dict) */
3910 dict_idx = 1;
3911 else
3912 /* function(name, [args]) */
3913 arg_idx = 1;
3914 if (dict_idx > 0)
3915 {
3916 if (argvars[dict_idx].v_type != VAR_DICT)
3917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003920 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003921 }
3922 if (argvars[dict_idx].vval.v_dict == NULL)
3923 dict_idx = 0;
3924 }
3925 if (arg_idx > 0)
3926 {
3927 if (argvars[arg_idx].v_type != VAR_LIST)
3928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003929 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003931 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003932 }
3933 list = argvars[arg_idx].vval.v_list;
3934 if (list == NULL || list->lv_len == 0)
3935 arg_idx = 0;
3936 }
3937 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003938 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003940 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941
3942 /* result is a VAR_PARTIAL */
3943 if (pt == NULL)
3944 vim_free(name);
3945 else
3946 {
3947 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3948 {
3949 listitem_T *li;
3950 int i = 0;
3951 int arg_len = 0;
3952 int lv_len = 0;
3953
3954 if (arg_pt != NULL)
3955 arg_len = arg_pt->pt_argc;
3956 if (list != NULL)
3957 lv_len = list->lv_len;
3958 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003959 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003960 if (pt->pt_argv == NULL)
3961 {
3962 vim_free(pt);
3963 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003964 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003966 for (i = 0; i < arg_len; i++)
3967 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3968 if (lv_len > 0)
3969 for (li = list->lv_first; li != NULL;
3970 li = li->li_next)
3971 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003972 }
3973
3974 /* For "function(dict.func, [], dict)" and "func" is a partial
3975 * use "dict". That is backwards compatible. */
3976 if (dict_idx > 0)
3977 {
3978 /* The dict is bound explicitly, pt_auto is FALSE. */
3979 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3980 ++pt->pt_dict->dv_refcount;
3981 }
3982 else if (arg_pt != NULL)
3983 {
3984 /* If the dict was bound automatically the result is also
3985 * bound automatically. */
3986 pt->pt_dict = arg_pt->pt_dict;
3987 pt->pt_auto = arg_pt->pt_auto;
3988 if (pt->pt_dict != NULL)
3989 ++pt->pt_dict->dv_refcount;
3990 }
3991
3992 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003993 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3994 {
3995 pt->pt_func = arg_pt->pt_func;
3996 func_ptr_ref(pt->pt_func);
3997 vim_free(name);
3998 }
3999 else if (is_funcref)
4000 {
4001 pt->pt_func = find_func(trans_name);
4002 func_ptr_ref(pt->pt_func);
4003 vim_free(name);
4004 }
4005 else
4006 {
4007 pt->pt_name = name;
4008 func_ref(name);
4009 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010 }
4011 rettv->v_type = VAR_PARTIAL;
4012 rettv->vval.v_partial = pt;
4013 }
4014 else
4015 {
4016 /* result is a VAR_FUNC */
4017 rettv->v_type = VAR_FUNC;
4018 rettv->vval.v_string = name;
4019 func_ref(name);
4020 }
4021 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022theend:
4023 vim_free(trans_name);
4024}
4025
4026/*
4027 * "funcref()" function
4028 */
4029 static void
4030f_funcref(typval_T *argvars, typval_T *rettv)
4031{
4032 common_function(argvars, rettv, TRUE);
4033}
4034
4035/*
4036 * "function()" function
4037 */
4038 static void
4039f_function(typval_T *argvars, typval_T *rettv)
4040{
4041 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004042}
4043
4044/*
4045 * "garbagecollect()" function
4046 */
4047 static void
4048f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4049{
4050 /* This is postponed until we are back at the toplevel, because we may be
4051 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4052 want_garbage_collect = TRUE;
4053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004054 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004055 garbage_collect_at_exit = TRUE;
4056}
4057
4058/*
4059 * "get()" function
4060 */
4061 static void
4062f_get(typval_T *argvars, typval_T *rettv)
4063{
4064 listitem_T *li;
4065 list_T *l;
4066 dictitem_T *di;
4067 dict_T *d;
4068 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004069 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004071 if (argvars[0].v_type == VAR_BLOB)
4072 {
4073 int error = FALSE;
4074 int idx = tv_get_number_chk(&argvars[1], &error);
4075
4076 if (!error)
4077 {
4078 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004079 if (idx < 0)
4080 idx = blob_len(argvars[0].vval.v_blob) + idx;
4081 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4082 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004083 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004084 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004085 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004086 tv = rettv;
4087 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004088 }
4089 }
4090 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 {
4092 if ((l = argvars[0].vval.v_list) != NULL)
4093 {
4094 int error = FALSE;
4095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004096 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004097 if (!error && li != NULL)
4098 tv = &li->li_tv;
4099 }
4100 }
4101 else if (argvars[0].v_type == VAR_DICT)
4102 {
4103 if ((d = argvars[0].vval.v_dict) != NULL)
4104 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004105 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004106 if (di != NULL)
4107 tv = &di->di_tv;
4108 }
4109 }
4110 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4111 {
4112 partial_T *pt;
4113 partial_T fref_pt;
4114
4115 if (argvars[0].v_type == VAR_PARTIAL)
4116 pt = argvars[0].vval.v_partial;
4117 else
4118 {
4119 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4120 fref_pt.pt_name = argvars[0].vval.v_string;
4121 pt = &fref_pt;
4122 }
4123
4124 if (pt != NULL)
4125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004126 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004127 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004128
4129 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4130 {
4131 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 n = partial_name(pt);
4133 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004134 rettv->vval.v_string = NULL;
4135 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 {
4137 rettv->vval.v_string = vim_strsave(n);
4138 if (rettv->v_type == VAR_FUNC)
4139 func_ref(rettv->vval.v_string);
4140 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141 }
4142 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004143 {
4144 what_is_dict = TRUE;
4145 if (pt->pt_dict != NULL)
4146 rettv_dict_set(rettv, pt->pt_dict);
4147 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 else if (STRCMP(what, "args") == 0)
4149 {
4150 rettv->v_type = VAR_LIST;
4151 if (rettv_list_alloc(rettv) == OK)
4152 {
4153 int i;
4154
4155 for (i = 0; i < pt->pt_argc; ++i)
4156 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4157 }
4158 }
4159 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004161
4162 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4163 // third argument
4164 if (!what_is_dict)
4165 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 }
4167 }
4168 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004169 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170
4171 if (tv == NULL)
4172 {
4173 if (argvars[2].v_type != VAR_UNKNOWN)
4174 copy_tv(&argvars[2], rettv);
4175 }
4176 else
4177 copy_tv(tv, rettv);
4178}
4179
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004180/*
4181 * Returns buffer options, variables and other attributes in a dictionary.
4182 */
4183 static dict_T *
4184get_buffer_info(buf_T *buf)
4185{
4186 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004187 tabpage_T *tp;
4188 win_T *wp;
4189 list_T *windows;
4190
4191 dict = dict_alloc();
4192 if (dict == NULL)
4193 return NULL;
4194
Bram Moolenaare0be1672018-07-08 16:50:37 +02004195 dict_add_number(dict, "bufnr", buf->b_fnum);
4196 dict_add_string(dict, "name", buf->b_ffname);
4197 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4198 : buflist_findlnum(buf));
4199 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4200 dict_add_number(dict, "listed", buf->b_p_bl);
4201 dict_add_number(dict, "changed", bufIsChanged(buf));
4202 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4203 dict_add_number(dict, "hidden",
4204 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004205
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004206 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004207 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004208
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004209 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004210 windows = list_alloc();
4211 if (windows != NULL)
4212 {
4213 FOR_ALL_TAB_WINDOWS(tp, wp)
4214 if (wp->w_buffer == buf)
4215 list_append_number(windows, (varnumber_T)wp->w_id);
4216 dict_add_list(dict, "windows", windows);
4217 }
4218
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004219#ifdef FEAT_TEXT_PROP
4220 // List of popup windows displaying this buffer
4221 windows = list_alloc();
4222 if (windows != NULL)
4223 {
4224 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4225 if (wp->w_buffer == buf)
4226 list_append_number(windows, (varnumber_T)wp->w_id);
4227 FOR_ALL_TABPAGES(tp)
4228 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4229 if (wp->w_buffer == buf)
4230 list_append_number(windows, (varnumber_T)wp->w_id);
4231
4232 dict_add_list(dict, "popups", windows);
4233 }
4234#endif
4235
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004236#ifdef FEAT_SIGNS
4237 if (buf->b_signlist != NULL)
4238 {
4239 /* List of signs placed in this buffer */
4240 list_T *signs = list_alloc();
4241 if (signs != NULL)
4242 {
4243 get_buffer_signs(buf, signs);
4244 dict_add_list(dict, "signs", signs);
4245 }
4246 }
4247#endif
4248
4249 return dict;
4250}
4251
4252/*
4253 * "getbufinfo()" function
4254 */
4255 static void
4256f_getbufinfo(typval_T *argvars, typval_T *rettv)
4257{
4258 buf_T *buf = NULL;
4259 buf_T *argbuf = NULL;
4260 dict_T *d;
4261 int filtered = FALSE;
4262 int sel_buflisted = FALSE;
4263 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004264 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004265
4266 if (rettv_list_alloc(rettv) != OK)
4267 return;
4268
4269 /* List of all the buffers or selected buffers */
4270 if (argvars[0].v_type == VAR_DICT)
4271 {
4272 dict_T *sel_d = argvars[0].vval.v_dict;
4273
4274 if (sel_d != NULL)
4275 {
4276 dictitem_T *di;
4277
4278 filtered = TRUE;
4279
4280 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004281 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004282 sel_buflisted = TRUE;
4283
4284 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004285 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004286 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004287
4288 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004289 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004290 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004291 }
4292 }
4293 else if (argvars[0].v_type != VAR_UNKNOWN)
4294 {
4295 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004296 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004297 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004298 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004299 --emsg_off;
4300 if (argbuf == NULL)
4301 return;
4302 }
4303
4304 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004305 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004306 {
4307 if (argbuf != NULL && argbuf != buf)
4308 continue;
4309 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004310 || (sel_buflisted && !buf->b_p_bl)
4311 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004312 continue;
4313
4314 d = get_buffer_info(buf);
4315 if (d != NULL)
4316 list_append_dict(rettv->vval.v_list, d);
4317 if (argbuf != NULL)
4318 return;
4319 }
4320}
4321
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322/*
4323 * Get line or list of lines from buffer "buf" into "rettv".
4324 * Return a range (from start to end) of lines in rettv from the specified
4325 * buffer.
4326 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4327 */
4328 static void
4329get_buffer_lines(
4330 buf_T *buf,
4331 linenr_T start,
4332 linenr_T end,
4333 int retlist,
4334 typval_T *rettv)
4335{
4336 char_u *p;
4337
4338 rettv->v_type = VAR_STRING;
4339 rettv->vval.v_string = NULL;
4340 if (retlist && rettv_list_alloc(rettv) == FAIL)
4341 return;
4342
4343 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4344 return;
4345
4346 if (!retlist)
4347 {
4348 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4349 p = ml_get_buf(buf, start, FALSE);
4350 else
4351 p = (char_u *)"";
4352 rettv->vval.v_string = vim_strsave(p);
4353 }
4354 else
4355 {
4356 if (end < start)
4357 return;
4358
4359 if (start < 1)
4360 start = 1;
4361 if (end > buf->b_ml.ml_line_count)
4362 end = buf->b_ml.ml_line_count;
4363 while (start <= end)
4364 if (list_append_string(rettv->vval.v_list,
4365 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4366 break;
4367 }
4368}
4369
4370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371 * "getbufline()" function
4372 */
4373 static void
4374f_getbufline(typval_T *argvars, typval_T *rettv)
4375{
4376 linenr_T lnum;
4377 linenr_T end;
4378 buf_T *buf;
4379
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004380 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004382 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383 --emsg_off;
4384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004385 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004386 if (argvars[2].v_type == VAR_UNKNOWN)
4387 end = lnum;
4388 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004389 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390
4391 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4392}
4393
4394/*
4395 * "getbufvar()" function
4396 */
4397 static void
4398f_getbufvar(typval_T *argvars, typval_T *rettv)
4399{
4400 buf_T *buf;
4401 buf_T *save_curbuf;
4402 char_u *varname;
4403 dictitem_T *v;
4404 int done = FALSE;
4405
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004406 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4407 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004409 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410
4411 rettv->v_type = VAR_STRING;
4412 rettv->vval.v_string = NULL;
4413
4414 if (buf != NULL && varname != NULL)
4415 {
4416 /* set curbuf to be our buf, temporarily */
4417 save_curbuf = curbuf;
4418 curbuf = buf;
4419
Bram Moolenaar30567352016-08-27 21:25:44 +02004420 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004422 if (varname[1] == NUL)
4423 {
4424 /* get all buffer-local options in a dict */
4425 dict_T *opts = get_winbuf_options(TRUE);
4426
4427 if (opts != NULL)
4428 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004429 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004430 done = TRUE;
4431 }
4432 }
4433 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4434 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 done = TRUE;
4436 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004437 else
4438 {
4439 /* Look up the variable. */
4440 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4441 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4442 'b', varname, FALSE);
4443 if (v != NULL)
4444 {
4445 copy_tv(&v->di_tv, rettv);
4446 done = TRUE;
4447 }
4448 }
4449
4450 /* restore previous notion of curbuf */
4451 curbuf = save_curbuf;
4452 }
4453
4454 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4455 /* use the default value */
4456 copy_tv(&argvars[2], rettv);
4457
4458 --emsg_off;
4459}
4460
4461/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004462 * "getchangelist()" function
4463 */
4464 static void
4465f_getchangelist(typval_T *argvars, typval_T *rettv)
4466{
4467#ifdef FEAT_JUMPLIST
4468 buf_T *buf;
4469 int i;
4470 list_T *l;
4471 dict_T *d;
4472#endif
4473
4474 if (rettv_list_alloc(rettv) != OK)
4475 return;
4476
4477#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004478 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004479 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004480 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004481 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004482 if (buf == NULL)
4483 return;
4484
4485 l = list_alloc();
4486 if (l == NULL)
4487 return;
4488
4489 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4490 return;
4491 /*
4492 * The current window change list index tracks only the position in the
4493 * current buffer change list. For other buffers, use the change list
4494 * length as the current index.
4495 */
4496 list_append_number(rettv->vval.v_list,
4497 (varnumber_T)((buf == curwin->w_buffer)
4498 ? curwin->w_changelistidx : buf->b_changelistlen));
4499
4500 for (i = 0; i < buf->b_changelistlen; ++i)
4501 {
4502 if (buf->b_changelist[i].lnum == 0)
4503 continue;
4504 if ((d = dict_alloc()) == NULL)
4505 return;
4506 if (list_append_dict(l, d) == FAIL)
4507 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004508 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4509 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004510 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004511 }
4512#endif
4513}
4514/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515 * "getchar()" function
4516 */
4517 static void
4518f_getchar(typval_T *argvars, typval_T *rettv)
4519{
4520 varnumber_T n;
4521 int error = FALSE;
4522
Bram Moolenaar84d93902018-09-11 20:10:20 +02004523#ifdef MESSAGE_QUEUE
4524 // vpeekc() used to check for messages, but that caused problems, invoking
4525 // a callback where it was not expected. Some plugins use getchar(1) in a
4526 // loop to await a message, therefore make sure we check for messages here.
4527 parse_queued_messages();
4528#endif
4529
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004530 /* Position the cursor. Needed after a message that ends in a space. */
4531 windgoto(msg_row, msg_col);
4532
4533 ++no_mapping;
4534 ++allow_keys;
4535 for (;;)
4536 {
4537 if (argvars[0].v_type == VAR_UNKNOWN)
4538 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004539 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004540 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541 /* getchar(1): only check if char avail */
4542 n = vpeekc_any();
4543 else if (error || vpeekc_any() == NUL)
4544 /* illegal argument or getchar(0) and no char avail: return zero */
4545 n = 0;
4546 else
4547 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004548 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549
4550 if (n == K_IGNORE)
4551 continue;
4552 break;
4553 }
4554 --no_mapping;
4555 --allow_keys;
4556
4557 set_vim_var_nr(VV_MOUSE_WIN, 0);
4558 set_vim_var_nr(VV_MOUSE_WINID, 0);
4559 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4560 set_vim_var_nr(VV_MOUSE_COL, 0);
4561
4562 rettv->vval.v_number = n;
4563 if (IS_SPECIAL(n) || mod_mask != 0)
4564 {
4565 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4566 int i = 0;
4567
4568 /* Turn a special key into three bytes, plus modifier. */
4569 if (mod_mask != 0)
4570 {
4571 temp[i++] = K_SPECIAL;
4572 temp[i++] = KS_MODIFIER;
4573 temp[i++] = mod_mask;
4574 }
4575 if (IS_SPECIAL(n))
4576 {
4577 temp[i++] = K_SPECIAL;
4578 temp[i++] = K_SECOND(n);
4579 temp[i++] = K_THIRD(n);
4580 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581 else if (has_mbyte)
4582 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583 else
4584 temp[i++] = n;
4585 temp[i++] = NUL;
4586 rettv->v_type = VAR_STRING;
4587 rettv->vval.v_string = vim_strsave(temp);
4588
4589#ifdef FEAT_MOUSE
4590 if (is_mouse_key(n))
4591 {
4592 int row = mouse_row;
4593 int col = mouse_col;
4594 win_T *win;
4595 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597 int winnr = 1;
4598
4599 if (row >= 0 && col >= 0)
4600 {
4601 /* Find the window at the mouse coordinates and compute the
4602 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004603 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004604 if (win == NULL)
4605 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004606 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004607# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004608 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004609 winnr = 0;
4610 else
4611# endif
4612 for (wp = firstwin; wp != win && wp != NULL;
4613 wp = wp->w_next)
4614 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004615 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4616 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4617 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4618 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4619 }
4620 }
4621#endif
4622 }
4623}
4624
4625/*
4626 * "getcharmod()" function
4627 */
4628 static void
4629f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4630{
4631 rettv->vval.v_number = mod_mask;
4632}
4633
4634/*
4635 * "getcharsearch()" function
4636 */
4637 static void
4638f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4639{
4640 if (rettv_dict_alloc(rettv) != FAIL)
4641 {
4642 dict_T *dict = rettv->vval.v_dict;
4643
Bram Moolenaare0be1672018-07-08 16:50:37 +02004644 dict_add_string(dict, "char", last_csearch());
4645 dict_add_number(dict, "forward", last_csearch_forward());
4646 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647 }
4648}
4649
4650/*
4651 * "getcmdline()" function
4652 */
4653 static void
4654f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4655{
4656 rettv->v_type = VAR_STRING;
4657 rettv->vval.v_string = get_cmdline_str();
4658}
4659
4660/*
4661 * "getcmdpos()" function
4662 */
4663 static void
4664f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4665{
4666 rettv->vval.v_number = get_cmdline_pos() + 1;
4667}
4668
4669/*
4670 * "getcmdtype()" function
4671 */
4672 static void
4673f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4674{
4675 rettv->v_type = VAR_STRING;
4676 rettv->vval.v_string = alloc(2);
4677 if (rettv->vval.v_string != NULL)
4678 {
4679 rettv->vval.v_string[0] = get_cmdline_type();
4680 rettv->vval.v_string[1] = NUL;
4681 }
4682}
4683
4684/*
4685 * "getcmdwintype()" function
4686 */
4687 static void
4688f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4689{
4690 rettv->v_type = VAR_STRING;
4691 rettv->vval.v_string = NULL;
4692#ifdef FEAT_CMDWIN
4693 rettv->vval.v_string = alloc(2);
4694 if (rettv->vval.v_string != NULL)
4695 {
4696 rettv->vval.v_string[0] = cmdwin_type;
4697 rettv->vval.v_string[1] = NUL;
4698 }
4699#endif
4700}
4701
4702#if defined(FEAT_CMDL_COMPL)
4703/*
4704 * "getcompletion()" function
4705 */
4706 static void
4707f_getcompletion(typval_T *argvars, typval_T *rettv)
4708{
4709 char_u *pat;
4710 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004711 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004712 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4713 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004714
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004716 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718 if (p_wic)
4719 options |= WILD_ICASE;
4720
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004721 /* For filtered results, 'wildignore' is used */
4722 if (!filtered)
4723 options |= WILD_KEEP_ALL;
4724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004725 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004726 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004728 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 if (xpc.xp_context == EXPAND_NOTHING)
4730 {
4731 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004732 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004733 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004734 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004735 return;
4736 }
4737
4738# if defined(FEAT_MENU)
4739 if (xpc.xp_context == EXPAND_MENUS)
4740 {
4741 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4742 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4743 }
4744# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004745#ifdef FEAT_CSCOPE
4746 if (xpc.xp_context == EXPAND_CSCOPE)
4747 {
4748 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4749 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4750 }
4751#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004752#ifdef FEAT_SIGNS
4753 if (xpc.xp_context == EXPAND_SIGN)
4754 {
4755 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4756 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4757 }
4758#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759
4760 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4761 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4762 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004763 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004764
4765 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4766
4767 for (i = 0; i < xpc.xp_numfiles; i++)
4768 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4769 }
4770 vim_free(pat);
4771 ExpandCleanup(&xpc);
4772}
4773#endif
4774
4775/*
4776 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004777 *
4778 * Return the current working directory of a window in a tab page.
4779 * First optional argument 'winnr' is the window number or -1 and the second
4780 * optional argument 'tabnr' is the tab page number.
4781 *
4782 * If no arguments are supplied, then return the directory of the current
4783 * window.
4784 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4785 * the specified window.
4786 * If 'winnr' is 0 then return the directory of the current window.
4787 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4788 * directory of the specified tab page. Otherwise return the directory of the
4789 * specified window in the specified tab page.
4790 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791 */
4792 static void
4793f_getcwd(typval_T *argvars, typval_T *rettv)
4794{
4795 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004796 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004797 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004798 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004799
4800 rettv->v_type = VAR_STRING;
4801 rettv->vval.v_string = NULL;
4802
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004803 if (argvars[0].v_type == VAR_NUMBER
4804 && argvars[0].vval.v_number == -1
4805 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004806 global = TRUE;
4807 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004808 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004809
4810 if (wp != NULL && wp->w_localdir != NULL)
4811 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004812 else if (tp != NULL && tp->tp_localdir != NULL)
4813 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4814 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004816 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004817 rettv->vval.v_string = vim_strsave(globaldir);
4818 else
4819 {
4820 cwd = alloc(MAXPATHL);
4821 if (cwd != NULL)
4822 {
4823 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4824 rettv->vval.v_string = vim_strsave(cwd);
4825 vim_free(cwd);
4826 }
4827 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004828 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004829#ifdef BACKSLASH_IN_FILENAME
4830 if (rettv->vval.v_string != NULL)
4831 slash_adjust(rettv->vval.v_string);
4832#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004833}
4834
4835/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004836 * "getenv()" function
4837 */
4838 static void
4839f_getenv(typval_T *argvars, typval_T *rettv)
4840{
4841 int mustfree = FALSE;
4842 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4843
4844 if (p == NULL)
4845 {
4846 rettv->v_type = VAR_SPECIAL;
4847 rettv->vval.v_number = VVAL_NULL;
4848 return;
4849 }
4850 if (!mustfree)
4851 p = vim_strsave(p);
4852 rettv->vval.v_string = p;
4853 rettv->v_type = VAR_STRING;
4854}
4855
4856/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004857 * "getfontname()" function
4858 */
4859 static void
4860f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4861{
4862 rettv->v_type = VAR_STRING;
4863 rettv->vval.v_string = NULL;
4864#ifdef FEAT_GUI
4865 if (gui.in_use)
4866 {
4867 GuiFont font;
4868 char_u *name = NULL;
4869
4870 if (argvars[0].v_type == VAR_UNKNOWN)
4871 {
4872 /* Get the "Normal" font. Either the name saved by
4873 * hl_set_font_name() or from the font ID. */
4874 font = gui.norm_font;
4875 name = hl_get_font_name();
4876 }
4877 else
4878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004879 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4881 return;
4882 font = gui_mch_get_font(name, FALSE);
4883 if (font == NOFONT)
4884 return; /* Invalid font name, return empty string. */
4885 }
4886 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4887 if (argvars[0].v_type != VAR_UNKNOWN)
4888 gui_mch_free_font(font);
4889 }
4890#endif
4891}
4892
4893/*
4894 * "getfperm({fname})" function
4895 */
4896 static void
4897f_getfperm(typval_T *argvars, typval_T *rettv)
4898{
4899 char_u *fname;
4900 stat_T st;
4901 char_u *perm = NULL;
4902 char_u flags[] = "rwx";
4903 int i;
4904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004905 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 rettv->v_type = VAR_STRING;
4908 if (mch_stat((char *)fname, &st) >= 0)
4909 {
4910 perm = vim_strsave((char_u *)"---------");
4911 if (perm != NULL)
4912 {
4913 for (i = 0; i < 9; i++)
4914 {
4915 if (st.st_mode & (1 << (8 - i)))
4916 perm[i] = flags[i % 3];
4917 }
4918 }
4919 }
4920 rettv->vval.v_string = perm;
4921}
4922
4923/*
4924 * "getfsize({fname})" function
4925 */
4926 static void
4927f_getfsize(typval_T *argvars, typval_T *rettv)
4928{
4929 char_u *fname;
4930 stat_T st;
4931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004932 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933
4934 rettv->v_type = VAR_NUMBER;
4935
4936 if (mch_stat((char *)fname, &st) >= 0)
4937 {
4938 if (mch_isdir(fname))
4939 rettv->vval.v_number = 0;
4940 else
4941 {
4942 rettv->vval.v_number = (varnumber_T)st.st_size;
4943
4944 /* non-perfect check for overflow */
4945 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4946 rettv->vval.v_number = -2;
4947 }
4948 }
4949 else
4950 rettv->vval.v_number = -1;
4951}
4952
4953/*
4954 * "getftime({fname})" function
4955 */
4956 static void
4957f_getftime(typval_T *argvars, typval_T *rettv)
4958{
4959 char_u *fname;
4960 stat_T st;
4961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004962 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963
4964 if (mch_stat((char *)fname, &st) >= 0)
4965 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4966 else
4967 rettv->vval.v_number = -1;
4968}
4969
4970/*
4971 * "getftype({fname})" function
4972 */
4973 static void
4974f_getftype(typval_T *argvars, typval_T *rettv)
4975{
4976 char_u *fname;
4977 stat_T st;
4978 char_u *type = NULL;
4979 char *t;
4980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004982
4983 rettv->v_type = VAR_STRING;
4984 if (mch_lstat((char *)fname, &st) >= 0)
4985 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 if (S_ISREG(st.st_mode))
4987 t = "file";
4988 else if (S_ISDIR(st.st_mode))
4989 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004990 else if (S_ISLNK(st.st_mode))
4991 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 else if (S_ISBLK(st.st_mode))
4993 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004994 else if (S_ISCHR(st.st_mode))
4995 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004996 else if (S_ISFIFO(st.st_mode))
4997 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004998 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02004999 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005000 else
5001 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002 type = vim_strsave((char_u *)t);
5003 }
5004 rettv->vval.v_string = type;
5005}
5006
5007/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005008 * "getjumplist()" function
5009 */
5010 static void
5011f_getjumplist(typval_T *argvars, typval_T *rettv)
5012{
5013#ifdef FEAT_JUMPLIST
5014 win_T *wp;
5015 int i;
5016 list_T *l;
5017 dict_T *d;
5018#endif
5019
5020 if (rettv_list_alloc(rettv) != OK)
5021 return;
5022
5023#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005024 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005025 if (wp == NULL)
5026 return;
5027
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005028 cleanup_jumplist(wp, TRUE);
5029
Bram Moolenaar4f505882018-02-10 21:06:32 +01005030 l = list_alloc();
5031 if (l == NULL)
5032 return;
5033
5034 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5035 return;
5036 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5037
5038 for (i = 0; i < wp->w_jumplistlen; ++i)
5039 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005040 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5041 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005042 if ((d = dict_alloc()) == NULL)
5043 return;
5044 if (list_append_dict(l, d) == FAIL)
5045 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005046 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5047 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005048 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005049 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005050 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005051 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005052 }
5053#endif
5054}
5055
5056/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 * "getline(lnum, [end])" function
5058 */
5059 static void
5060f_getline(typval_T *argvars, typval_T *rettv)
5061{
5062 linenr_T lnum;
5063 linenr_T end;
5064 int retlist;
5065
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005066 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005067 if (argvars[1].v_type == VAR_UNKNOWN)
5068 {
5069 end = 0;
5070 retlist = FALSE;
5071 }
5072 else
5073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005074 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 retlist = TRUE;
5076 }
5077
5078 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5079}
5080
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005081#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005082 static void
5083get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5084{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005085 if (what_arg->v_type == VAR_UNKNOWN)
5086 {
5087 if (rettv_list_alloc(rettv) == OK)
5088 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005089 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005090 }
5091 else
5092 {
5093 if (rettv_dict_alloc(rettv) == OK)
5094 if (is_qf || (wp != NULL))
5095 {
5096 if (what_arg->v_type == VAR_DICT)
5097 {
5098 dict_T *d = what_arg->vval.v_dict;
5099
5100 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005101 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005102 }
5103 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005104 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005105 }
5106 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005107}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005108#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005109
5110/*
5111 * "getloclist()" function
5112 */
5113 static void
5114f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5115{
5116#ifdef FEAT_QUICKFIX
5117 win_T *wp;
5118
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005119 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005120 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5121#endif
5122}
5123
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 * "getpid()" function
5126 */
5127 static void
5128f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5129{
5130 rettv->vval.v_number = mch_get_pid();
5131}
5132
5133 static void
5134getpos_both(
5135 typval_T *argvars,
5136 typval_T *rettv,
5137 int getcurpos)
5138{
5139 pos_T *fp;
5140 list_T *l;
5141 int fnum = -1;
5142
5143 if (rettv_list_alloc(rettv) == OK)
5144 {
5145 l = rettv->vval.v_list;
5146 if (getcurpos)
5147 fp = &curwin->w_cursor;
5148 else
5149 fp = var2fpos(&argvars[0], TRUE, &fnum);
5150 if (fnum != -1)
5151 list_append_number(l, (varnumber_T)fnum);
5152 else
5153 list_append_number(l, (varnumber_T)0);
5154 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5155 : (varnumber_T)0);
5156 list_append_number(l, (fp != NULL)
5157 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5158 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005159 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 (varnumber_T)0);
5161 if (getcurpos)
5162 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005163 int save_set_curswant = curwin->w_set_curswant;
5164 colnr_T save_curswant = curwin->w_curswant;
5165 colnr_T save_virtcol = curwin->w_virtcol;
5166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005167 update_curswant();
5168 list_append_number(l, curwin->w_curswant == MAXCOL ?
5169 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005170
5171 // Do not change "curswant", as it is unexpected that a get
5172 // function has a side effect.
5173 if (save_set_curswant)
5174 {
5175 curwin->w_set_curswant = save_set_curswant;
5176 curwin->w_curswant = save_curswant;
5177 curwin->w_virtcol = save_virtcol;
5178 curwin->w_valid &= ~VALID_VIRTCOL;
5179 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005180 }
5181 }
5182 else
5183 rettv->vval.v_number = FALSE;
5184}
5185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005186/*
5187 * "getcurpos()" function
5188 */
5189 static void
5190f_getcurpos(typval_T *argvars, typval_T *rettv)
5191{
5192 getpos_both(argvars, rettv, TRUE);
5193}
5194
5195/*
5196 * "getpos(string)" function
5197 */
5198 static void
5199f_getpos(typval_T *argvars, typval_T *rettv)
5200{
5201 getpos_both(argvars, rettv, FALSE);
5202}
5203
5204/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005205 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 */
5207 static void
5208f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5209{
5210#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005211 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005212#endif
5213}
5214
5215/*
5216 * "getreg()" function
5217 */
5218 static void
5219f_getreg(typval_T *argvars, typval_T *rettv)
5220{
5221 char_u *strregname;
5222 int regname;
5223 int arg2 = FALSE;
5224 int return_list = FALSE;
5225 int error = FALSE;
5226
5227 if (argvars[0].v_type != VAR_UNKNOWN)
5228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005229 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 error = strregname == NULL;
5231 if (argvars[1].v_type != VAR_UNKNOWN)
5232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005233 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005235 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236 }
5237 }
5238 else
5239 strregname = get_vim_var_str(VV_REG);
5240
5241 if (error)
5242 return;
5243
5244 regname = (strregname == NULL ? '"' : *strregname);
5245 if (regname == 0)
5246 regname = '"';
5247
5248 if (return_list)
5249 {
5250 rettv->v_type = VAR_LIST;
5251 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5252 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5253 if (rettv->vval.v_list == NULL)
5254 (void)rettv_list_alloc(rettv);
5255 else
5256 ++rettv->vval.v_list->lv_refcount;
5257 }
5258 else
5259 {
5260 rettv->v_type = VAR_STRING;
5261 rettv->vval.v_string = get_reg_contents(regname,
5262 arg2 ? GREG_EXPR_SRC : 0);
5263 }
5264}
5265
5266/*
5267 * "getregtype()" function
5268 */
5269 static void
5270f_getregtype(typval_T *argvars, typval_T *rettv)
5271{
5272 char_u *strregname;
5273 int regname;
5274 char_u buf[NUMBUFLEN + 2];
5275 long reglen = 0;
5276
5277 if (argvars[0].v_type != VAR_UNKNOWN)
5278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005279 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280 if (strregname == NULL) /* type error; errmsg already given */
5281 {
5282 rettv->v_type = VAR_STRING;
5283 rettv->vval.v_string = NULL;
5284 return;
5285 }
5286 }
5287 else
5288 /* Default to v:register */
5289 strregname = get_vim_var_str(VV_REG);
5290
5291 regname = (strregname == NULL ? '"' : *strregname);
5292 if (regname == 0)
5293 regname = '"';
5294
5295 buf[0] = NUL;
5296 buf[1] = NUL;
5297 switch (get_reg_type(regname, &reglen))
5298 {
5299 case MLINE: buf[0] = 'V'; break;
5300 case MCHAR: buf[0] = 'v'; break;
5301 case MBLOCK:
5302 buf[0] = Ctrl_V;
5303 sprintf((char *)buf + 1, "%ld", reglen + 1);
5304 break;
5305 }
5306 rettv->v_type = VAR_STRING;
5307 rettv->vval.v_string = vim_strsave(buf);
5308}
5309
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005310/*
5311 * Returns information (variables, options, etc.) about a tab page
5312 * as a dictionary.
5313 */
5314 static dict_T *
5315get_tabpage_info(tabpage_T *tp, int tp_idx)
5316{
5317 win_T *wp;
5318 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005319 list_T *l;
5320
5321 dict = dict_alloc();
5322 if (dict == NULL)
5323 return NULL;
5324
Bram Moolenaare0be1672018-07-08 16:50:37 +02005325 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005326
5327 l = list_alloc();
5328 if (l != NULL)
5329 {
5330 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005331 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332 list_append_number(l, (varnumber_T)wp->w_id);
5333 dict_add_list(dict, "windows", l);
5334 }
5335
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005336 /* Make a reference to tabpage variables */
5337 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005338
5339 return dict;
5340}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005341
5342/*
5343 * "gettabinfo()" function
5344 */
5345 static void
5346f_gettabinfo(typval_T *argvars, typval_T *rettv)
5347{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005348 tabpage_T *tp, *tparg = NULL;
5349 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005350 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005351
5352 if (rettv_list_alloc(rettv) != OK)
5353 return;
5354
5355 if (argvars[0].v_type != VAR_UNKNOWN)
5356 {
5357 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005358 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005359 if (tparg == NULL)
5360 return;
5361 }
5362
5363 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005364 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005365 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005366 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005367 if (tparg != NULL && tp != tparg)
5368 continue;
5369 d = get_tabpage_info(tp, tpnr);
5370 if (d != NULL)
5371 list_append_dict(rettv->vval.v_list, d);
5372 if (tparg != NULL)
5373 return;
5374 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005375}
5376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377/*
5378 * "gettabvar()" function
5379 */
5380 static void
5381f_gettabvar(typval_T *argvars, typval_T *rettv)
5382{
5383 win_T *oldcurwin;
5384 tabpage_T *tp, *oldtabpage;
5385 dictitem_T *v;
5386 char_u *varname;
5387 int done = FALSE;
5388
5389 rettv->v_type = VAR_STRING;
5390 rettv->vval.v_string = NULL;
5391
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005392 varname = tv_get_string_chk(&argvars[1]);
5393 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394 if (tp != NULL && varname != NULL)
5395 {
5396 /* Set tp to be our tabpage, temporarily. Also set the window to the
5397 * first window in the tabpage, otherwise the window is not valid. */
5398 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005399 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5400 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005401 {
5402 /* look up the variable */
5403 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5404 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5405 if (v != NULL)
5406 {
5407 copy_tv(&v->di_tv, rettv);
5408 done = TRUE;
5409 }
5410 }
5411
5412 /* restore previous notion of curwin */
5413 restore_win(oldcurwin, oldtabpage, TRUE);
5414 }
5415
5416 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5417 copy_tv(&argvars[2], rettv);
5418}
5419
5420/*
5421 * "gettabwinvar()" function
5422 */
5423 static void
5424f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5425{
5426 getwinvar(argvars, rettv, 1);
5427}
5428
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005429/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005430 * "gettagstack()" function
5431 */
5432 static void
5433f_gettagstack(typval_T *argvars, typval_T *rettv)
5434{
5435 win_T *wp = curwin; // default is current window
5436
5437 if (rettv_dict_alloc(rettv) != OK)
5438 return;
5439
5440 if (argvars[0].v_type != VAR_UNKNOWN)
5441 {
5442 wp = find_win_by_nr_or_id(&argvars[0]);
5443 if (wp == NULL)
5444 return;
5445 }
5446
5447 get_tagstack(wp, rettv->vval.v_dict);
5448}
5449
5450/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005451 * Returns information about a window as a dictionary.
5452 */
5453 static dict_T *
5454get_win_info(win_T *wp, short tpnr, short winnr)
5455{
5456 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005457
5458 dict = dict_alloc();
5459 if (dict == NULL)
5460 return NULL;
5461
Bram Moolenaare0be1672018-07-08 16:50:37 +02005462 dict_add_number(dict, "tabnr", tpnr);
5463 dict_add_number(dict, "winnr", winnr);
5464 dict_add_number(dict, "winid", wp->w_id);
5465 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005466 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005467 dict_add_number(dict, "topline", wp->w_topline);
5468 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005469#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005470 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005471#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005472 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005473 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005474 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005475
Bram Moolenaar69905d12017-08-13 18:14:47 +02005476#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005477 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005478#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005479#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005480 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5481 dict_add_number(dict, "loclist",
5482 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005483#endif
5484
Bram Moolenaar30567352016-08-27 21:25:44 +02005485 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005486 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005487
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005488 return dict;
5489}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005490
5491/*
5492 * "getwininfo()" function
5493 */
5494 static void
5495f_getwininfo(typval_T *argvars, typval_T *rettv)
5496{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005497 tabpage_T *tp;
5498 win_T *wp = NULL, *wparg = NULL;
5499 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005500 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005501
5502 if (rettv_list_alloc(rettv) != OK)
5503 return;
5504
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005505 if (argvars[0].v_type != VAR_UNKNOWN)
5506 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005507 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005508 if (wparg == NULL)
5509 return;
5510 }
5511
5512 /* Collect information about either all the windows across all the tab
5513 * pages or one particular window.
5514 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005515 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005516 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005517 tabnr++;
5518 winnr = 0;
5519 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005520 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005521 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005522 if (wparg != NULL && wp != wparg)
5523 continue;
5524 d = get_win_info(wp, tabnr, winnr);
5525 if (d != NULL)
5526 list_append_dict(rettv->vval.v_list, d);
5527 if (wparg != NULL)
5528 /* found information about a specific window */
5529 return;
5530 }
5531 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005532}
5533
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005535 * "win_execute()" function
5536 */
5537 static void
5538f_win_execute(typval_T *argvars, typval_T *rettv)
5539{
5540 int id = (int)tv_get_number(argvars);
Bram Moolenaar820680b2019-08-09 14:56:22 +02005541 tabpage_T *tp;
5542 win_T *wp = win_id2wp_tp(id, &tp);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005543 win_T *save_curwin;
5544 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005545
Bram Moolenaar820680b2019-08-09 14:56:22 +02005546 if (wp != NULL && tp != NULL)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005547 {
Bram Moolenaar820680b2019-08-09 14:56:22 +02005548 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005549 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005550 check_cursor();
5551 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005552 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005553 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005554 }
5555}
5556
5557/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558 * "win_findbuf()" function
5559 */
5560 static void
5561f_win_findbuf(typval_T *argvars, typval_T *rettv)
5562{
5563 if (rettv_list_alloc(rettv) != FAIL)
5564 win_findbuf(argvars, rettv->vval.v_list);
5565}
5566
5567/*
5568 * "win_getid()" function
5569 */
5570 static void
5571f_win_getid(typval_T *argvars, typval_T *rettv)
5572{
5573 rettv->vval.v_number = win_getid(argvars);
5574}
5575
5576/*
5577 * "win_gotoid()" function
5578 */
5579 static void
5580f_win_gotoid(typval_T *argvars, typval_T *rettv)
5581{
5582 rettv->vval.v_number = win_gotoid(argvars);
5583}
5584
5585/*
5586 * "win_id2tabwin()" function
5587 */
5588 static void
5589f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5590{
5591 if (rettv_list_alloc(rettv) != FAIL)
5592 win_id2tabwin(argvars, rettv->vval.v_list);
5593}
5594
5595/*
5596 * "win_id2win()" function
5597 */
5598 static void
5599f_win_id2win(typval_T *argvars, typval_T *rettv)
5600{
5601 rettv->vval.v_number = win_id2win(argvars);
5602}
5603
5604/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005605 * "win_screenpos()" function
5606 */
5607 static void
5608f_win_screenpos(typval_T *argvars, typval_T *rettv)
5609{
5610 win_T *wp;
5611
5612 if (rettv_list_alloc(rettv) == FAIL)
5613 return;
5614
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005615 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005616 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5617 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5618}
5619
5620/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005621 * "getwinpos({timeout})" function
5622 */
5623 static void
5624f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5625{
5626 int x = -1;
5627 int y = -1;
5628
5629 if (rettv_list_alloc(rettv) == FAIL)
5630 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005631#if defined(FEAT_GUI) \
5632 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5633 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005634 {
5635 varnumber_T timeout = 100;
5636
5637 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005638 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005639
5640 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005641 }
5642#endif
5643 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5644 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5645}
5646
5647
5648/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005649 * "getwinposx()" function
5650 */
5651 static void
5652f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5653{
5654 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005655#if defined(FEAT_GUI) \
5656 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5657 || defined(MSWIN)
5658
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005659 {
5660 int x, y;
5661
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005662 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005663 rettv->vval.v_number = x;
5664 }
5665#endif
5666}
5667
5668/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005669 * "getwinposy()" function
5670 */
5671 static void
5672f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5673{
5674 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005675#if defined(FEAT_GUI) \
5676 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5677 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 {
5679 int x, y;
5680
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005681 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005682 rettv->vval.v_number = y;
5683 }
5684#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685}
5686
5687/*
5688 * "getwinvar()" function
5689 */
5690 static void
5691f_getwinvar(typval_T *argvars, typval_T *rettv)
5692{
5693 getwinvar(argvars, rettv, 0);
5694}
5695
5696/*
5697 * "glob()" function
5698 */
5699 static void
5700f_glob(typval_T *argvars, typval_T *rettv)
5701{
5702 int options = WILD_SILENT|WILD_USE_NL;
5703 expand_T xpc;
5704 int error = FALSE;
5705
5706 /* When the optional second argument is non-zero, don't remove matches
5707 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5708 rettv->v_type = VAR_STRING;
5709 if (argvars[1].v_type != VAR_UNKNOWN)
5710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005711 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005712 options |= WILD_KEEP_ALL;
5713 if (argvars[2].v_type != VAR_UNKNOWN)
5714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005716 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005718 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719 options |= WILD_ALLLINKS;
5720 }
5721 }
5722 if (!error)
5723 {
5724 ExpandInit(&xpc);
5725 xpc.xp_context = EXPAND_FILES;
5726 if (p_wic)
5727 options += WILD_ICASE;
5728 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005729 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005730 NULL, options, WILD_ALL);
5731 else if (rettv_list_alloc(rettv) != FAIL)
5732 {
5733 int i;
5734
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005735 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 NULL, options, WILD_ALL_KEEP);
5737 for (i = 0; i < xpc.xp_numfiles; i++)
5738 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5739
5740 ExpandCleanup(&xpc);
5741 }
5742 }
5743 else
5744 rettv->vval.v_string = NULL;
5745}
5746
5747/*
5748 * "globpath()" function
5749 */
5750 static void
5751f_globpath(typval_T *argvars, typval_T *rettv)
5752{
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005753 int flags = WILD_IGNORE_COMPLETESLASH;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005754 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005755 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756 int error = FALSE;
5757 garray_T ga;
5758 int i;
5759
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005760 // When the optional second argument is non-zero, don't remove matches
5761 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005762 rettv->v_type = VAR_STRING;
5763 if (argvars[2].v_type != VAR_UNKNOWN)
5764 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005765 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766 flags |= WILD_KEEP_ALL;
5767 if (argvars[3].v_type != VAR_UNKNOWN)
5768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005769 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005770 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005772 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773 flags |= WILD_ALLLINKS;
5774 }
5775 }
5776 if (file != NULL && !error)
5777 {
5778 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005779 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005780 if (rettv->v_type == VAR_STRING)
5781 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5782 else if (rettv_list_alloc(rettv) != FAIL)
5783 for (i = 0; i < ga.ga_len; ++i)
5784 list_append_string(rettv->vval.v_list,
5785 ((char_u **)(ga.ga_data))[i], -1);
5786 ga_clear_strings(&ga);
5787 }
5788 else
5789 rettv->vval.v_string = NULL;
5790}
5791
5792/*
5793 * "glob2regpat()" function
5794 */
5795 static void
5796f_glob2regpat(typval_T *argvars, typval_T *rettv)
5797{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005798 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799
5800 rettv->v_type = VAR_STRING;
5801 rettv->vval.v_string = (pat == NULL)
5802 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5803}
5804
5805/* for VIM_VERSION_ defines */
5806#include "version.h"
5807
5808/*
5809 * "has()" function
5810 */
5811 static void
5812f_has(typval_T *argvars, typval_T *rettv)
5813{
5814 int i;
5815 char_u *name;
5816 int n = FALSE;
5817 static char *(has_list[]) =
5818 {
5819#ifdef AMIGA
5820 "amiga",
5821# ifdef FEAT_ARP
5822 "arp",
5823# endif
5824#endif
5825#ifdef __BEOS__
5826 "beos",
5827#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005828#if defined(BSD) && !defined(MACOS_X)
5829 "bsd",
5830#endif
5831#ifdef hpux
5832 "hpux",
5833#endif
5834#ifdef __linux__
5835 "linux",
5836#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005837#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005838 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5839 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005840# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005841 "macunix", /* Mac OS X, with the darwin feature */
5842 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005843# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844#endif
5845#ifdef __QNX__
5846 "qnx",
5847#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005848#ifdef SUN_SYSTEM
5849 "sun",
5850#else
5851 "moon",
5852#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853#ifdef UNIX
5854 "unix",
5855#endif
5856#ifdef VMS
5857 "vms",
5858#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005859#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 "win32",
5861#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005862#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005863 "win32unix",
5864#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005865#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 "win64",
5867#endif
5868#ifdef EBCDIC
5869 "ebcdic",
5870#endif
5871#ifndef CASE_INSENSITIVE_FILENAME
5872 "fname_case",
5873#endif
5874#ifdef HAVE_ACL
5875 "acl",
5876#endif
5877#ifdef FEAT_ARABIC
5878 "arabic",
5879#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005880 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005881#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005882 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005883#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005884#ifdef FEAT_AUTOSERVERNAME
5885 "autoservername",
5886#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005887#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005888 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01005889# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005890 "balloon_multiline",
5891# endif
5892#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005893#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005894 "balloon_eval_term",
5895#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5897 "builtin_terms",
5898# ifdef ALL_BUILTIN_TCAPS
5899 "all_builtin_terms",
5900# endif
5901#endif
5902#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005903 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005904 || defined(FEAT_GUI_MOTIF))
5905 "browsefilter",
5906#endif
5907#ifdef FEAT_BYTEOFF
5908 "byte_offset",
5909#endif
5910#ifdef FEAT_JOB_CHANNEL
5911 "channel",
5912#endif
5913#ifdef FEAT_CINDENT
5914 "cindent",
5915#endif
5916#ifdef FEAT_CLIENTSERVER
5917 "clientserver",
5918#endif
5919#ifdef FEAT_CLIPBOARD
5920 "clipboard",
5921#endif
5922#ifdef FEAT_CMDL_COMPL
5923 "cmdline_compl",
5924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005925 "cmdline_hist",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005926#ifdef FEAT_COMMENTS
5927 "comments",
5928#endif
5929#ifdef FEAT_CONCEAL
5930 "conceal",
5931#endif
5932#ifdef FEAT_CRYPT
5933 "cryptv",
5934 "crypt-blowfish",
5935 "crypt-blowfish2",
5936#endif
5937#ifdef FEAT_CSCOPE
5938 "cscope",
5939#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005940 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941#ifdef CURSOR_SHAPE
5942 "cursorshape",
5943#endif
5944#ifdef DEBUG
5945 "debug",
5946#endif
5947#ifdef FEAT_CON_DIALOG
5948 "dialog_con",
5949#endif
5950#ifdef FEAT_GUI_DIALOG
5951 "dialog_gui",
5952#endif
5953#ifdef FEAT_DIFF
5954 "diff",
5955#endif
5956#ifdef FEAT_DIGRAPHS
5957 "digraphs",
5958#endif
5959#ifdef FEAT_DIRECTX
5960 "directx",
5961#endif
5962#ifdef FEAT_DND
5963 "dnd",
5964#endif
5965#ifdef FEAT_EMACS_TAGS
5966 "emacs_tags",
5967#endif
5968 "eval", /* always present, of course! */
5969 "ex_extra", /* graduated feature */
5970#ifdef FEAT_SEARCH_EXTRA
5971 "extra_search",
5972#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973#ifdef FEAT_SEARCHPATH
5974 "file_in_path",
5975#endif
5976#ifdef FEAT_FILTERPIPE
5977 "filterpipe",
5978#endif
5979#ifdef FEAT_FIND_ID
5980 "find_in_path",
5981#endif
5982#ifdef FEAT_FLOAT
5983 "float",
5984#endif
5985#ifdef FEAT_FOLDING
5986 "folding",
5987#endif
5988#ifdef FEAT_FOOTER
5989 "footer",
5990#endif
5991#if !defined(USE_SYSTEM) && defined(UNIX)
5992 "fork",
5993#endif
5994#ifdef FEAT_GETTEXT
5995 "gettext",
5996#endif
5997#ifdef FEAT_GUI
5998 "gui",
5999#endif
6000#ifdef FEAT_GUI_ATHENA
6001# ifdef FEAT_GUI_NEXTAW
6002 "gui_neXtaw",
6003# else
6004 "gui_athena",
6005# endif
6006#endif
6007#ifdef FEAT_GUI_GTK
6008 "gui_gtk",
6009# ifdef USE_GTK3
6010 "gui_gtk3",
6011# else
6012 "gui_gtk2",
6013# endif
6014#endif
6015#ifdef FEAT_GUI_GNOME
6016 "gui_gnome",
6017#endif
6018#ifdef FEAT_GUI_MAC
6019 "gui_mac",
6020#endif
6021#ifdef FEAT_GUI_MOTIF
6022 "gui_motif",
6023#endif
6024#ifdef FEAT_GUI_PHOTON
6025 "gui_photon",
6026#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006027#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 "gui_win32",
6029#endif
6030#ifdef FEAT_HANGULIN
6031 "hangul_input",
6032#endif
6033#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6034 "iconv",
6035#endif
6036#ifdef FEAT_INS_EXPAND
6037 "insert_expand",
6038#endif
6039#ifdef FEAT_JOB_CHANNEL
6040 "job",
6041#endif
6042#ifdef FEAT_JUMPLIST
6043 "jumplist",
6044#endif
6045#ifdef FEAT_KEYMAP
6046 "keymap",
6047#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006048 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049#ifdef FEAT_LANGMAP
6050 "langmap",
6051#endif
6052#ifdef FEAT_LIBCALL
6053 "libcall",
6054#endif
6055#ifdef FEAT_LINEBREAK
6056 "linebreak",
6057#endif
6058#ifdef FEAT_LISP
6059 "lispindent",
6060#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006062 "localmap",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063#ifdef FEAT_LUA
6064# ifndef DYNAMIC_LUA
6065 "lua",
6066# endif
6067#endif
6068#ifdef FEAT_MENU
6069 "menu",
6070#endif
6071#ifdef FEAT_SESSION
6072 "mksession",
6073#endif
6074#ifdef FEAT_MODIFY_FNAME
6075 "modify_fname",
6076#endif
6077#ifdef FEAT_MOUSE
6078 "mouse",
6079#endif
6080#ifdef FEAT_MOUSESHAPE
6081 "mouseshape",
6082#endif
6083#if defined(UNIX) || defined(VMS)
6084# ifdef FEAT_MOUSE_DEC
6085 "mouse_dec",
6086# endif
6087# ifdef FEAT_MOUSE_GPM
6088 "mouse_gpm",
6089# endif
6090# ifdef FEAT_MOUSE_JSB
6091 "mouse_jsbterm",
6092# endif
6093# ifdef FEAT_MOUSE_NET
6094 "mouse_netterm",
6095# endif
6096# ifdef FEAT_MOUSE_PTERM
6097 "mouse_pterm",
6098# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006099# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 "mouse_sgr",
6101# endif
6102# ifdef FEAT_SYSMOUSE
6103 "mouse_sysmouse",
6104# endif
6105# ifdef FEAT_MOUSE_URXVT
6106 "mouse_urxvt",
6107# endif
6108# ifdef FEAT_MOUSE_XTERM
6109 "mouse_xterm",
6110# endif
6111#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113#ifdef FEAT_MBYTE_IME
6114 "multi_byte_ime",
6115#endif
6116#ifdef FEAT_MULTI_LANG
6117 "multi_lang",
6118#endif
6119#ifdef FEAT_MZSCHEME
6120#ifndef DYNAMIC_MZSCHEME
6121 "mzscheme",
6122#endif
6123#endif
6124#ifdef FEAT_NUM64
6125 "num64",
6126#endif
6127#ifdef FEAT_OLE
6128 "ole",
6129#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006130#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006131 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133#ifdef FEAT_PATH_EXTRA
6134 "path_extra",
6135#endif
6136#ifdef FEAT_PERL
6137#ifndef DYNAMIC_PERL
6138 "perl",
6139#endif
6140#endif
6141#ifdef FEAT_PERSISTENT_UNDO
6142 "persistent_undo",
6143#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006144#if defined(FEAT_PYTHON)
6145 "python_compiled",
6146# if defined(DYNAMIC_PYTHON)
6147 "python_dynamic",
6148# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006150 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006151# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006152#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006153#if defined(FEAT_PYTHON3)
6154 "python3_compiled",
6155# if defined(DYNAMIC_PYTHON3)
6156 "python3_dynamic",
6157# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006159 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006160# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006161#endif
6162#ifdef FEAT_POSTSCRIPT
6163 "postscript",
6164#endif
6165#ifdef FEAT_PRINTER
6166 "printer",
6167#endif
6168#ifdef FEAT_PROFILE
6169 "profile",
6170#endif
6171#ifdef FEAT_RELTIME
6172 "reltime",
6173#endif
6174#ifdef FEAT_QUICKFIX
6175 "quickfix",
6176#endif
6177#ifdef FEAT_RIGHTLEFT
6178 "rightleft",
6179#endif
6180#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6181 "ruby",
6182#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184#ifdef FEAT_CMDL_INFO
6185 "showcmd",
6186 "cmdline_info",
6187#endif
6188#ifdef FEAT_SIGNS
6189 "signs",
6190#endif
6191#ifdef FEAT_SMARTINDENT
6192 "smartindent",
6193#endif
6194#ifdef STARTUPTIME
6195 "startuptime",
6196#endif
6197#ifdef FEAT_STL_OPT
6198 "statusline",
6199#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006200#ifdef FEAT_NETBEANS_INTG
6201 "netbeans_intg",
6202#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006203#ifdef FEAT_SOUND
6204 "sound",
6205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206#ifdef FEAT_SPELL
6207 "spell",
6208#endif
6209#ifdef FEAT_SYN_HL
6210 "syntax",
6211#endif
6212#if defined(USE_SYSTEM) || !defined(UNIX)
6213 "system",
6214#endif
6215#ifdef FEAT_TAG_BINS
6216 "tag_binary",
6217#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006218#ifdef FEAT_TCL
6219# ifndef DYNAMIC_TCL
6220 "tcl",
6221# endif
6222#endif
6223#ifdef FEAT_TERMGUICOLORS
6224 "termguicolors",
6225#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006226#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006227 "terminal",
6228#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006229#ifdef TERMINFO
6230 "terminfo",
6231#endif
6232#ifdef FEAT_TERMRESPONSE
6233 "termresponse",
6234#endif
6235#ifdef FEAT_TEXTOBJ
6236 "textobjects",
6237#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006238#ifdef FEAT_TEXT_PROP
6239 "textprop",
6240#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006241#ifdef HAVE_TGETENT
6242 "tgetent",
6243#endif
6244#ifdef FEAT_TIMERS
6245 "timers",
6246#endif
6247#ifdef FEAT_TITLE
6248 "title",
6249#endif
6250#ifdef FEAT_TOOLBAR
6251 "toolbar",
6252#endif
6253#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6254 "unnamedplus",
6255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 "user-commands", /* was accidentally included in 5.4 */
6257 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006258#ifdef FEAT_VARTABS
6259 "vartabs",
6260#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006261 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262#ifdef FEAT_VIMINFO
6263 "viminfo",
6264#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006265 "vimscript-1",
6266 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006267 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006268 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006269 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006270 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006272#ifdef FEAT_VTP
6273 "vtp",
6274#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006275#ifdef FEAT_WILDIGN
6276 "wildignore",
6277#endif
6278#ifdef FEAT_WILDMENU
6279 "wildmenu",
6280#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282#ifdef FEAT_WAK
6283 "winaltkeys",
6284#endif
6285#ifdef FEAT_WRITEBACKUP
6286 "writebackup",
6287#endif
6288#ifdef FEAT_XIM
6289 "xim",
6290#endif
6291#ifdef FEAT_XFONTSET
6292 "xfontset",
6293#endif
6294#ifdef FEAT_XPM_W32
6295 "xpm",
6296 "xpm_w32", /* for backward compatibility */
6297#else
6298# if defined(HAVE_XPM)
6299 "xpm",
6300# endif
6301#endif
6302#ifdef USE_XSMP
6303 "xsmp",
6304#endif
6305#ifdef USE_XSMP_INTERACT
6306 "xsmp_interact",
6307#endif
6308#ifdef FEAT_XCLIPBOARD
6309 "xterm_clipboard",
6310#endif
6311#ifdef FEAT_XTERM_SAVE
6312 "xterm_save",
6313#endif
6314#if defined(UNIX) && defined(FEAT_X11)
6315 "X11",
6316#endif
6317 NULL
6318 };
6319
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006320 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 for (i = 0; has_list[i] != NULL; ++i)
6322 if (STRICMP(name, has_list[i]) == 0)
6323 {
6324 n = TRUE;
6325 break;
6326 }
6327
6328 if (n == FALSE)
6329 {
6330 if (STRNICMP(name, "patch", 5) == 0)
6331 {
6332 if (name[5] == '-'
6333 && STRLEN(name) >= 11
6334 && vim_isdigit(name[6])
6335 && vim_isdigit(name[8])
6336 && vim_isdigit(name[10]))
6337 {
6338 int major = atoi((char *)name + 6);
6339 int minor = atoi((char *)name + 8);
6340
6341 /* Expect "patch-9.9.01234". */
6342 n = (major < VIM_VERSION_MAJOR
6343 || (major == VIM_VERSION_MAJOR
6344 && (minor < VIM_VERSION_MINOR
6345 || (minor == VIM_VERSION_MINOR
6346 && has_patch(atoi((char *)name + 10))))));
6347 }
6348 else
6349 n = has_patch(atoi((char *)name + 5));
6350 }
6351 else if (STRICMP(name, "vim_starting") == 0)
6352 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006353 else if (STRICMP(name, "ttyin") == 0)
6354 n = mch_input_isatty();
6355 else if (STRICMP(name, "ttyout") == 0)
6356 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006357 else if (STRICMP(name, "multi_byte_encoding") == 0)
6358 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006359#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360 else if (STRICMP(name, "balloon_multiline") == 0)
6361 n = multiline_balloon_available();
6362#endif
6363#ifdef DYNAMIC_TCL
6364 else if (STRICMP(name, "tcl") == 0)
6365 n = tcl_enabled(FALSE);
6366#endif
6367#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6368 else if (STRICMP(name, "iconv") == 0)
6369 n = iconv_enabled(FALSE);
6370#endif
6371#ifdef DYNAMIC_LUA
6372 else if (STRICMP(name, "lua") == 0)
6373 n = lua_enabled(FALSE);
6374#endif
6375#ifdef DYNAMIC_MZSCHEME
6376 else if (STRICMP(name, "mzscheme") == 0)
6377 n = mzscheme_enabled(FALSE);
6378#endif
6379#ifdef DYNAMIC_RUBY
6380 else if (STRICMP(name, "ruby") == 0)
6381 n = ruby_enabled(FALSE);
6382#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383#ifdef DYNAMIC_PYTHON
6384 else if (STRICMP(name, "python") == 0)
6385 n = python_enabled(FALSE);
6386#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387#ifdef DYNAMIC_PYTHON3
6388 else if (STRICMP(name, "python3") == 0)
6389 n = python3_enabled(FALSE);
6390#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006391#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6392 else if (STRICMP(name, "pythonx") == 0)
6393 {
6394# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6395 if (p_pyx == 0)
6396 n = python3_enabled(FALSE) || python_enabled(FALSE);
6397 else if (p_pyx == 3)
6398 n = python3_enabled(FALSE);
6399 else if (p_pyx == 2)
6400 n = python_enabled(FALSE);
6401# elif defined(DYNAMIC_PYTHON)
6402 n = python_enabled(FALSE);
6403# elif defined(DYNAMIC_PYTHON3)
6404 n = python3_enabled(FALSE);
6405# endif
6406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407#endif
6408#ifdef DYNAMIC_PERL
6409 else if (STRICMP(name, "perl") == 0)
6410 n = perl_enabled(FALSE);
6411#endif
6412#ifdef FEAT_GUI
6413 else if (STRICMP(name, "gui_running") == 0)
6414 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415# ifdef FEAT_BROWSE
6416 else if (STRICMP(name, "browse") == 0)
6417 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6418# endif
6419#endif
6420#ifdef FEAT_SYN_HL
6421 else if (STRICMP(name, "syntax_items") == 0)
6422 n = syntax_present(curwin);
6423#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006424#ifdef FEAT_VTP
6425 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006426 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427#endif
6428#ifdef FEAT_NETBEANS_INTG
6429 else if (STRICMP(name, "netbeans_enabled") == 0)
6430 n = netbeans_active();
6431#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006432#ifdef FEAT_MOUSE_GPM
6433 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6434 n = gpm_enabled();
6435#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006436#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006437 else if (STRICMP(name, "terminal") == 0)
6438 n = terminal_enabled();
6439#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006440#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006441 else if (STRICMP(name, "conpty") == 0)
6442 n = use_conpty();
6443#endif
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02006444#ifdef FEAT_CLIPBOARD
6445 else if (STRICMP(name, "clipboard_working") == 0)
6446 n = clip_star.available;
6447#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448 }
6449
6450 rettv->vval.v_number = n;
6451}
6452
6453/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006454 * "haslocaldir()" function
6455 */
6456 static void
6457f_haslocaldir(typval_T *argvars, typval_T *rettv)
6458{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006459 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460 win_T *wp = NULL;
6461
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006462 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6463
6464 // Check for window-local and tab-local directories
6465 if (wp != NULL && wp->w_localdir != NULL)
6466 rettv->vval.v_number = 1;
6467 else if (tp != NULL && tp->tp_localdir != NULL)
6468 rettv->vval.v_number = 2;
6469 else
6470 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006471}
6472
6473/*
6474 * "hasmapto()" function
6475 */
6476 static void
6477f_hasmapto(typval_T *argvars, typval_T *rettv)
6478{
6479 char_u *name;
6480 char_u *mode;
6481 char_u buf[NUMBUFLEN];
6482 int abbr = FALSE;
6483
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006484 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 if (argvars[1].v_type == VAR_UNKNOWN)
6486 mode = (char_u *)"nvo";
6487 else
6488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006489 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006491 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 }
6493
6494 if (map_to_exists(name, mode, abbr))
6495 rettv->vval.v_number = TRUE;
6496 else
6497 rettv->vval.v_number = FALSE;
6498}
6499
6500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006501 * "highlightID(name)" function
6502 */
6503 static void
6504f_hlID(typval_T *argvars, typval_T *rettv)
6505{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006506 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507}
6508
6509/*
6510 * "highlight_exists()" function
6511 */
6512 static void
6513f_hlexists(typval_T *argvars, typval_T *rettv)
6514{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006515 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516}
6517
6518/*
6519 * "hostname()" function
6520 */
6521 static void
6522f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6523{
6524 char_u hostname[256];
6525
6526 mch_get_host_name(hostname, 256);
6527 rettv->v_type = VAR_STRING;
6528 rettv->vval.v_string = vim_strsave(hostname);
6529}
6530
6531/*
6532 * iconv() function
6533 */
6534 static void
6535f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6536{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006537 char_u buf1[NUMBUFLEN];
6538 char_u buf2[NUMBUFLEN];
6539 char_u *from, *to, *str;
6540 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006541
6542 rettv->v_type = VAR_STRING;
6543 rettv->vval.v_string = NULL;
6544
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006545 str = tv_get_string(&argvars[0]);
6546 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6547 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548 vimconv.vc_type = CONV_NONE;
6549 convert_setup(&vimconv, from, to);
6550
6551 /* If the encodings are equal, no conversion needed. */
6552 if (vimconv.vc_type == CONV_NONE)
6553 rettv->vval.v_string = vim_strsave(str);
6554 else
6555 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6556
6557 convert_setup(&vimconv, NULL, NULL);
6558 vim_free(from);
6559 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560}
6561
6562/*
6563 * "indent()" function
6564 */
6565 static void
6566f_indent(typval_T *argvars, typval_T *rettv)
6567{
6568 linenr_T lnum;
6569
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006570 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6572 rettv->vval.v_number = get_indent_lnum(lnum);
6573 else
6574 rettv->vval.v_number = -1;
6575}
6576
6577/*
6578 * "index()" function
6579 */
6580 static void
6581f_index(typval_T *argvars, typval_T *rettv)
6582{
6583 list_T *l;
6584 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006585 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586 long idx = 0;
6587 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006588 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589
6590 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006591 if (argvars[0].v_type == VAR_BLOB)
6592 {
6593 typval_T tv;
6594 int start = 0;
6595
6596 if (argvars[2].v_type != VAR_UNKNOWN)
6597 {
6598 start = tv_get_number_chk(&argvars[2], &error);
6599 if (error)
6600 return;
6601 }
6602 b = argvars[0].vval.v_blob;
6603 if (b == NULL)
6604 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006605 if (start < 0)
6606 {
6607 start = blob_len(b) + start;
6608 if (start < 0)
6609 start = 0;
6610 }
6611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006612 for (idx = start; idx < blob_len(b); ++idx)
6613 {
6614 tv.v_type = VAR_NUMBER;
6615 tv.vval.v_number = blob_get(b, idx);
6616 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6617 {
6618 rettv->vval.v_number = idx;
6619 return;
6620 }
6621 }
6622 return;
6623 }
6624 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006625 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006626 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006627 return;
6628 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006630 l = argvars[0].vval.v_list;
6631 if (l != NULL)
6632 {
6633 item = l->lv_first;
6634 if (argvars[2].v_type != VAR_UNKNOWN)
6635 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006636 /* Start at specified item. Use the cached index that list_find()
6637 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006638 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 idx = l->lv_idx;
6640 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006641 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642 if (error)
6643 item = NULL;
6644 }
6645
6646 for ( ; item != NULL; item = item->li_next, ++idx)
6647 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6648 {
6649 rettv->vval.v_number = idx;
6650 break;
6651 }
6652 }
6653}
6654
6655static int inputsecret_flag = 0;
6656
6657/*
6658 * "input()" function
6659 * Also handles inputsecret() when inputsecret is set.
6660 */
6661 static void
6662f_input(typval_T *argvars, typval_T *rettv)
6663{
6664 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6665}
6666
6667/*
6668 * "inputdialog()" function
6669 */
6670 static void
6671f_inputdialog(typval_T *argvars, typval_T *rettv)
6672{
6673#if defined(FEAT_GUI_TEXTDIALOG)
6674 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6675 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6676 {
6677 char_u *message;
6678 char_u buf[NUMBUFLEN];
6679 char_u *defstr = (char_u *)"";
6680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006681 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006683 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006684 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6685 else
6686 IObuff[0] = NUL;
6687 if (message != NULL && defstr != NULL
6688 && do_dialog(VIM_QUESTION, NULL, message,
6689 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6690 rettv->vval.v_string = vim_strsave(IObuff);
6691 else
6692 {
6693 if (message != NULL && defstr != NULL
6694 && argvars[1].v_type != VAR_UNKNOWN
6695 && argvars[2].v_type != VAR_UNKNOWN)
6696 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006697 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698 else
6699 rettv->vval.v_string = NULL;
6700 }
6701 rettv->v_type = VAR_STRING;
6702 }
6703 else
6704#endif
6705 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6706}
6707
6708/*
6709 * "inputlist()" function
6710 */
6711 static void
6712f_inputlist(typval_T *argvars, typval_T *rettv)
6713{
6714 listitem_T *li;
6715 int selected;
6716 int mouse_used;
6717
6718#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006719 /* While starting up, there is no place to enter text. When running tests
6720 * with --not-a-term we assume feedkeys() will be used. */
6721 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006722 return;
6723#endif
6724 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006726 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 return;
6728 }
6729
6730 msg_start();
6731 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6732 lines_left = Rows; /* avoid more prompt */
6733 msg_scroll = TRUE;
6734 msg_clr_eos();
6735
6736 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6737 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006738 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 msg_putchar('\n');
6740 }
6741
6742 /* Ask for choice. */
6743 selected = prompt_for_number(&mouse_used);
6744 if (mouse_used)
6745 selected -= lines_left;
6746
6747 rettv->vval.v_number = selected;
6748}
6749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6751
6752/*
6753 * "inputrestore()" function
6754 */
6755 static void
6756f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6757{
6758 if (ga_userinput.ga_len > 0)
6759 {
6760 --ga_userinput.ga_len;
6761 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6762 + ga_userinput.ga_len);
6763 /* default return is zero == OK */
6764 }
6765 else if (p_verbose > 1)
6766 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006767 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 rettv->vval.v_number = 1; /* Failed */
6769 }
6770}
6771
6772/*
6773 * "inputsave()" function
6774 */
6775 static void
6776f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6777{
6778 /* Add an entry to the stack of typeahead storage. */
6779 if (ga_grow(&ga_userinput, 1) == OK)
6780 {
6781 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6782 + ga_userinput.ga_len);
6783 ++ga_userinput.ga_len;
6784 /* default return is zero == OK */
6785 }
6786 else
6787 rettv->vval.v_number = 1; /* Failed */
6788}
6789
6790/*
6791 * "inputsecret()" function
6792 */
6793 static void
6794f_inputsecret(typval_T *argvars, typval_T *rettv)
6795{
6796 ++cmdline_star;
6797 ++inputsecret_flag;
6798 f_input(argvars, rettv);
6799 --cmdline_star;
6800 --inputsecret_flag;
6801}
6802
6803/*
6804 * "insert()" function
6805 */
6806 static void
6807f_insert(typval_T *argvars, typval_T *rettv)
6808{
6809 long before = 0;
6810 listitem_T *item;
6811 list_T *l;
6812 int error = FALSE;
6813
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006814 if (argvars[0].v_type == VAR_BLOB)
6815 {
6816 int val, len;
6817 char_u *p;
6818
6819 len = blob_len(argvars[0].vval.v_blob);
6820 if (argvars[2].v_type != VAR_UNKNOWN)
6821 {
6822 before = (long)tv_get_number_chk(&argvars[2], &error);
6823 if (error)
6824 return; // type error; errmsg already given
6825 if (before < 0 || before > len)
6826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006827 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006828 return;
6829 }
6830 }
6831 val = tv_get_number_chk(&argvars[1], &error);
6832 if (error)
6833 return;
6834 if (val < 0 || val > 255)
6835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006836 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006837 return;
6838 }
6839
6840 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
6841 return;
6842 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
6843 mch_memmove(p + before + 1, p + before, (size_t)len - before);
6844 *(p + before) = val;
6845 ++argvars[0].vval.v_blob->bv_ga.ga_len;
6846
6847 copy_tv(&argvars[0], rettv);
6848 }
6849 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006850 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01006851 else if ((l = argvars[0].vval.v_list) != NULL
6852 && !var_check_lock(l->lv_lock,
6853 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854 {
6855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006856 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 if (error)
6858 return; /* type error; errmsg already given */
6859
6860 if (before == l->lv_len)
6861 item = NULL;
6862 else
6863 {
6864 item = list_find(l, before);
6865 if (item == NULL)
6866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006867 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868 l = NULL;
6869 }
6870 }
6871 if (l != NULL)
6872 {
6873 list_insert_tv(l, &argvars[1], item);
6874 copy_tv(&argvars[0], rettv);
6875 }
6876 }
6877}
6878
6879/*
6880 * "invert(expr)" function
6881 */
6882 static void
6883f_invert(typval_T *argvars, typval_T *rettv)
6884{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006885 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886}
6887
6888/*
6889 * "isdirectory()" function
6890 */
6891 static void
6892f_isdirectory(typval_T *argvars, typval_T *rettv)
6893{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006894 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895}
6896
6897/*
6898 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6899 * or it refers to a List or Dictionary that is locked.
6900 */
6901 static int
6902tv_islocked(typval_T *tv)
6903{
6904 return (tv->v_lock & VAR_LOCKED)
6905 || (tv->v_type == VAR_LIST
6906 && tv->vval.v_list != NULL
6907 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6908 || (tv->v_type == VAR_DICT
6909 && tv->vval.v_dict != NULL
6910 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6911}
6912
6913/*
6914 * "islocked()" function
6915 */
6916 static void
6917f_islocked(typval_T *argvars, typval_T *rettv)
6918{
6919 lval_T lv;
6920 char_u *end;
6921 dictitem_T *di;
6922
6923 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006925 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926 if (end != NULL && lv.ll_name != NULL)
6927 {
6928 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006929 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930 else
6931 {
6932 if (lv.ll_tv == NULL)
6933 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006934 di = find_var(lv.ll_name, NULL, TRUE);
6935 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006937 /* Consider a variable locked when:
6938 * 1. the variable itself is locked
6939 * 2. the value of the variable is locked.
6940 * 3. the List or Dict value is locked.
6941 */
6942 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6943 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006944 }
6945 }
6946 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006947 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006949 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950 else if (lv.ll_list != NULL)
6951 /* List item. */
6952 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6953 else
6954 /* Dictionary item. */
6955 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6956 }
6957 }
6958
6959 clear_lval(&lv);
6960}
6961
6962#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6963/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006964 * "isinf()" function
6965 */
6966 static void
6967f_isinf(typval_T *argvars, typval_T *rettv)
6968{
6969 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6970 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6971}
6972
6973/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974 * "isnan()" function
6975 */
6976 static void
6977f_isnan(typval_T *argvars, typval_T *rettv)
6978{
6979 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6980 && isnan(argvars[0].vval.v_float);
6981}
6982#endif
6983
6984/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985 * "last_buffer_nr()" function.
6986 */
6987 static void
6988f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6989{
6990 int n = 0;
6991 buf_T *buf;
6992
Bram Moolenaar29323592016-07-24 22:04:11 +02006993 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 if (n < buf->b_fnum)
6995 n = buf->b_fnum;
6996
6997 rettv->vval.v_number = n;
6998}
6999
7000/*
7001 * "len()" function
7002 */
7003 static void
7004f_len(typval_T *argvars, typval_T *rettv)
7005{
7006 switch (argvars[0].v_type)
7007 {
7008 case VAR_STRING:
7009 case VAR_NUMBER:
7010 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007011 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007012 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007013 case VAR_BLOB:
7014 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7015 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016 case VAR_LIST:
7017 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7018 break;
7019 case VAR_DICT:
7020 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7021 break;
7022 case VAR_UNKNOWN:
7023 case VAR_SPECIAL:
7024 case VAR_FLOAT:
7025 case VAR_FUNC:
7026 case VAR_PARTIAL:
7027 case VAR_JOB:
7028 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007029 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007030 break;
7031 }
7032}
7033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007035libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036{
7037#ifdef FEAT_LIBCALL
7038 char_u *string_in;
7039 char_u **string_result;
7040 int nr_result;
7041#endif
7042
7043 rettv->v_type = type;
7044 if (type != VAR_NUMBER)
7045 rettv->vval.v_string = NULL;
7046
7047 if (check_restricted() || check_secure())
7048 return;
7049
7050#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007051 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7053 {
7054 string_in = NULL;
7055 if (argvars[2].v_type == VAR_STRING)
7056 string_in = argvars[2].vval.v_string;
7057 if (type == VAR_NUMBER)
7058 string_result = NULL;
7059 else
7060 string_result = &rettv->vval.v_string;
7061 if (mch_libcall(argvars[0].vval.v_string,
7062 argvars[1].vval.v_string,
7063 string_in,
7064 argvars[2].vval.v_number,
7065 string_result,
7066 &nr_result) == OK
7067 && type == VAR_NUMBER)
7068 rettv->vval.v_number = nr_result;
7069 }
7070#endif
7071}
7072
7073/*
7074 * "libcall()" function
7075 */
7076 static void
7077f_libcall(typval_T *argvars, typval_T *rettv)
7078{
7079 libcall_common(argvars, rettv, VAR_STRING);
7080}
7081
7082/*
7083 * "libcallnr()" function
7084 */
7085 static void
7086f_libcallnr(typval_T *argvars, typval_T *rettv)
7087{
7088 libcall_common(argvars, rettv, VAR_NUMBER);
7089}
7090
7091/*
7092 * "line(string)" function
7093 */
7094 static void
7095f_line(typval_T *argvars, typval_T *rettv)
7096{
7097 linenr_T lnum = 0;
7098 pos_T *fp;
7099 int fnum;
7100
7101 fp = var2fpos(&argvars[0], TRUE, &fnum);
7102 if (fp != NULL)
7103 lnum = fp->lnum;
7104 rettv->vval.v_number = lnum;
7105}
7106
7107/*
7108 * "line2byte(lnum)" function
7109 */
7110 static void
7111f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7112{
7113#ifndef FEAT_BYTEOFF
7114 rettv->vval.v_number = -1;
7115#else
7116 linenr_T lnum;
7117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7120 rettv->vval.v_number = -1;
7121 else
7122 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7123 if (rettv->vval.v_number >= 0)
7124 ++rettv->vval.v_number;
7125#endif
7126}
7127
7128/*
7129 * "lispindent(lnum)" function
7130 */
7131 static void
7132f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7133{
7134#ifdef FEAT_LISP
7135 pos_T pos;
7136 linenr_T lnum;
7137
7138 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007139 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007140 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7141 {
7142 curwin->w_cursor.lnum = lnum;
7143 rettv->vval.v_number = get_lisp_indent();
7144 curwin->w_cursor = pos;
7145 }
7146 else
7147#endif
7148 rettv->vval.v_number = -1;
7149}
7150
7151/*
7152 * "localtime()" function
7153 */
7154 static void
7155f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7156{
7157 rettv->vval.v_number = (varnumber_T)time(NULL);
7158}
7159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160#ifdef FEAT_FLOAT
7161/*
7162 * "log()" function
7163 */
7164 static void
7165f_log(typval_T *argvars, typval_T *rettv)
7166{
7167 float_T f = 0.0;
7168
7169 rettv->v_type = VAR_FLOAT;
7170 if (get_float_arg(argvars, &f) == OK)
7171 rettv->vval.v_float = log(f);
7172 else
7173 rettv->vval.v_float = 0.0;
7174}
7175
7176/*
7177 * "log10()" function
7178 */
7179 static void
7180f_log10(typval_T *argvars, typval_T *rettv)
7181{
7182 float_T f = 0.0;
7183
7184 rettv->v_type = VAR_FLOAT;
7185 if (get_float_arg(argvars, &f) == OK)
7186 rettv->vval.v_float = log10(f);
7187 else
7188 rettv->vval.v_float = 0.0;
7189}
7190#endif
7191
7192#ifdef FEAT_LUA
7193/*
7194 * "luaeval()" function
7195 */
7196 static void
7197f_luaeval(typval_T *argvars, typval_T *rettv)
7198{
7199 char_u *str;
7200 char_u buf[NUMBUFLEN];
7201
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007202 if (check_restricted() || check_secure())
7203 return;
7204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007205 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007206 do_luaeval(str, argvars + 1, rettv);
7207}
7208#endif
7209
7210/*
7211 * "map()" function
7212 */
7213 static void
7214f_map(typval_T *argvars, typval_T *rettv)
7215{
7216 filter_map(argvars, rettv, TRUE);
7217}
7218
7219/*
7220 * "maparg()" function
7221 */
7222 static void
7223f_maparg(typval_T *argvars, typval_T *rettv)
7224{
7225 get_maparg(argvars, rettv, TRUE);
7226}
7227
7228/*
7229 * "mapcheck()" function
7230 */
7231 static void
7232f_mapcheck(typval_T *argvars, typval_T *rettv)
7233{
7234 get_maparg(argvars, rettv, FALSE);
7235}
7236
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007237typedef enum
7238{
7239 MATCH_END, /* matchend() */
7240 MATCH_MATCH, /* match() */
7241 MATCH_STR, /* matchstr() */
7242 MATCH_LIST, /* matchlist() */
7243 MATCH_POS /* matchstrpos() */
7244} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245
7246 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007247find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007248{
7249 char_u *str = NULL;
7250 long len = 0;
7251 char_u *expr = NULL;
7252 char_u *pat;
7253 regmatch_T regmatch;
7254 char_u patbuf[NUMBUFLEN];
7255 char_u strbuf[NUMBUFLEN];
7256 char_u *save_cpo;
7257 long start = 0;
7258 long nth = 1;
7259 colnr_T startcol = 0;
7260 int match = 0;
7261 list_T *l = NULL;
7262 listitem_T *li = NULL;
7263 long idx = 0;
7264 char_u *tofree = NULL;
7265
7266 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7267 save_cpo = p_cpo;
7268 p_cpo = (char_u *)"";
7269
7270 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007271 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007273 /* type MATCH_LIST: return empty list when there are no matches.
7274 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (rettv_list_alloc(rettv) == FAIL)
7276 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007277 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 && (list_append_string(rettv->vval.v_list,
7279 (char_u *)"", 0) == FAIL
7280 || list_append_number(rettv->vval.v_list,
7281 (varnumber_T)-1) == FAIL
7282 || list_append_number(rettv->vval.v_list,
7283 (varnumber_T)-1) == FAIL
7284 || list_append_number(rettv->vval.v_list,
7285 (varnumber_T)-1) == FAIL))
7286 {
7287 list_free(rettv->vval.v_list);
7288 rettv->vval.v_list = NULL;
7289 goto theend;
7290 }
7291 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007292 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 {
7294 rettv->v_type = VAR_STRING;
7295 rettv->vval.v_string = NULL;
7296 }
7297
7298 if (argvars[0].v_type == VAR_LIST)
7299 {
7300 if ((l = argvars[0].vval.v_list) == NULL)
7301 goto theend;
7302 li = l->lv_first;
7303 }
7304 else
7305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 len = (long)STRLEN(str);
7308 }
7309
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007310 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007311 if (pat == NULL)
7312 goto theend;
7313
7314 if (argvars[2].v_type != VAR_UNKNOWN)
7315 {
7316 int error = FALSE;
7317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007318 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007319 if (error)
7320 goto theend;
7321 if (l != NULL)
7322 {
7323 li = list_find(l, start);
7324 if (li == NULL)
7325 goto theend;
7326 idx = l->lv_idx; /* use the cached index */
7327 }
7328 else
7329 {
7330 if (start < 0)
7331 start = 0;
7332 if (start > len)
7333 goto theend;
7334 /* When "count" argument is there ignore matches before "start",
7335 * otherwise skip part of the string. Differs when pattern is "^"
7336 * or "\<". */
7337 if (argvars[3].v_type != VAR_UNKNOWN)
7338 startcol = start;
7339 else
7340 {
7341 str += start;
7342 len -= start;
7343 }
7344 }
7345
7346 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007347 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 if (error)
7349 goto theend;
7350 }
7351
7352 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7353 if (regmatch.regprog != NULL)
7354 {
7355 regmatch.rm_ic = p_ic;
7356
7357 for (;;)
7358 {
7359 if (l != NULL)
7360 {
7361 if (li == NULL)
7362 {
7363 match = FALSE;
7364 break;
7365 }
7366 vim_free(tofree);
7367 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7368 if (str == NULL)
7369 break;
7370 }
7371
7372 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7373
7374 if (match && --nth <= 0)
7375 break;
7376 if (l == NULL && !match)
7377 break;
7378
7379 /* Advance to just after the match. */
7380 if (l != NULL)
7381 {
7382 li = li->li_next;
7383 ++idx;
7384 }
7385 else
7386 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 startcol = (colnr_T)(regmatch.startp[0]
7388 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389 if (startcol > (colnr_T)len
7390 || str + startcol <= regmatch.startp[0])
7391 {
7392 match = FALSE;
7393 break;
7394 }
7395 }
7396 }
7397
7398 if (match)
7399 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007400 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401 {
7402 listitem_T *li1 = rettv->vval.v_list->lv_first;
7403 listitem_T *li2 = li1->li_next;
7404 listitem_T *li3 = li2->li_next;
7405 listitem_T *li4 = li3->li_next;
7406
7407 vim_free(li1->li_tv.vval.v_string);
7408 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7409 (int)(regmatch.endp[0] - regmatch.startp[0]));
7410 li3->li_tv.vval.v_number =
7411 (varnumber_T)(regmatch.startp[0] - expr);
7412 li4->li_tv.vval.v_number =
7413 (varnumber_T)(regmatch.endp[0] - expr);
7414 if (l != NULL)
7415 li2->li_tv.vval.v_number = (varnumber_T)idx;
7416 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007417 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418 {
7419 int i;
7420
7421 /* return list with matched string and submatches */
7422 for (i = 0; i < NSUBEXP; ++i)
7423 {
7424 if (regmatch.endp[i] == NULL)
7425 {
7426 if (list_append_string(rettv->vval.v_list,
7427 (char_u *)"", 0) == FAIL)
7428 break;
7429 }
7430 else if (list_append_string(rettv->vval.v_list,
7431 regmatch.startp[i],
7432 (int)(regmatch.endp[i] - regmatch.startp[i]))
7433 == FAIL)
7434 break;
7435 }
7436 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007437 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 {
7439 /* return matched string */
7440 if (l != NULL)
7441 copy_tv(&li->li_tv, rettv);
7442 else
7443 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7444 (int)(regmatch.endp[0] - regmatch.startp[0]));
7445 }
7446 else if (l != NULL)
7447 rettv->vval.v_number = idx;
7448 else
7449 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007450 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007451 rettv->vval.v_number =
7452 (varnumber_T)(regmatch.startp[0] - str);
7453 else
7454 rettv->vval.v_number =
7455 (varnumber_T)(regmatch.endp[0] - str);
7456 rettv->vval.v_number += (varnumber_T)(str - expr);
7457 }
7458 }
7459 vim_regfree(regmatch.regprog);
7460 }
7461
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007462theend:
7463 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007464 /* matchstrpos() without a list: drop the second item. */
7465 listitem_remove(rettv->vval.v_list,
7466 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 vim_free(tofree);
7468 p_cpo = save_cpo;
7469}
7470
7471/*
7472 * "match()" function
7473 */
7474 static void
7475f_match(typval_T *argvars, typval_T *rettv)
7476{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007477 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478}
7479
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007480/*
7481 * "matchend()" function
7482 */
7483 static void
7484f_matchend(typval_T *argvars, typval_T *rettv)
7485{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007486 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487}
7488
7489/*
7490 * "matchlist()" function
7491 */
7492 static void
7493f_matchlist(typval_T *argvars, typval_T *rettv)
7494{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007495 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496}
7497
7498/*
7499 * "matchstr()" function
7500 */
7501 static void
7502f_matchstr(typval_T *argvars, typval_T *rettv)
7503{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007504 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007505}
7506
7507/*
7508 * "matchstrpos()" function
7509 */
7510 static void
7511f_matchstrpos(typval_T *argvars, typval_T *rettv)
7512{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007513 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514}
7515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 static void
7517max_min(typval_T *argvars, typval_T *rettv, int domax)
7518{
7519 varnumber_T n = 0;
7520 varnumber_T i;
7521 int error = FALSE;
7522
7523 if (argvars[0].v_type == VAR_LIST)
7524 {
7525 list_T *l;
7526 listitem_T *li;
7527
7528 l = argvars[0].vval.v_list;
7529 if (l != NULL)
7530 {
7531 li = l->lv_first;
7532 if (li != NULL)
7533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007534 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 for (;;)
7536 {
7537 li = li->li_next;
7538 if (li == NULL)
7539 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007540 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 if (domax ? i > n : i < n)
7542 n = i;
7543 }
7544 }
7545 }
7546 }
7547 else if (argvars[0].v_type == VAR_DICT)
7548 {
7549 dict_T *d;
7550 int first = TRUE;
7551 hashitem_T *hi;
7552 int todo;
7553
7554 d = argvars[0].vval.v_dict;
7555 if (d != NULL)
7556 {
7557 todo = (int)d->dv_hashtab.ht_used;
7558 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7559 {
7560 if (!HASHITEM_EMPTY(hi))
7561 {
7562 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007563 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007564 if (first)
7565 {
7566 n = i;
7567 first = FALSE;
7568 }
7569 else if (domax ? i > n : i < n)
7570 n = i;
7571 }
7572 }
7573 }
7574 }
7575 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007576 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007577 rettv->vval.v_number = error ? 0 : n;
7578}
7579
7580/*
7581 * "max()" function
7582 */
7583 static void
7584f_max(typval_T *argvars, typval_T *rettv)
7585{
7586 max_min(argvars, rettv, TRUE);
7587}
7588
7589/*
7590 * "min()" function
7591 */
7592 static void
7593f_min(typval_T *argvars, typval_T *rettv)
7594{
7595 max_min(argvars, rettv, FALSE);
7596}
7597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598/*
7599 * Create the directory in which "dir" is located, and higher levels when
7600 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007601 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 */
7603 static int
7604mkdir_recurse(char_u *dir, int prot)
7605{
7606 char_u *p;
7607 char_u *updir;
7608 int r = FAIL;
7609
7610 /* Get end of directory name in "dir".
7611 * We're done when it's "/" or "c:/". */
7612 p = gettail_sep(dir);
7613 if (p <= get_past_head(dir))
7614 return OK;
7615
7616 /* If the directory exists we're done. Otherwise: create it.*/
7617 updir = vim_strnsave(dir, (int)(p - dir));
7618 if (updir == NULL)
7619 return FAIL;
7620 if (mch_isdir(updir))
7621 r = OK;
7622 else if (mkdir_recurse(updir, prot) == OK)
7623 r = vim_mkdir_emsg(updir, prot);
7624 vim_free(updir);
7625 return r;
7626}
7627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628/*
7629 * "mkdir()" function
7630 */
7631 static void
7632f_mkdir(typval_T *argvars, typval_T *rettv)
7633{
7634 char_u *dir;
7635 char_u buf[NUMBUFLEN];
7636 int prot = 0755;
7637
7638 rettv->vval.v_number = FAIL;
7639 if (check_restricted() || check_secure())
7640 return;
7641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007642 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007644 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007645
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007646 if (*gettail(dir) == NUL)
7647 /* remove trailing slashes */
7648 *gettail_sep(dir) = NUL;
7649
7650 if (argvars[1].v_type != VAR_UNKNOWN)
7651 {
7652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007654 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007655 if (prot == -1)
7656 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007657 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007658 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007659 {
7660 if (mch_isdir(dir))
7661 {
7662 /* With the "p" flag it's OK if the dir already exists. */
7663 rettv->vval.v_number = OK;
7664 return;
7665 }
7666 mkdir_recurse(dir, prot);
7667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007669 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671
7672/*
7673 * "mode()" function
7674 */
7675 static void
7676f_mode(typval_T *argvars, typval_T *rettv)
7677{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007678 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679
Bram Moolenaar612cc382018-07-29 15:34:26 +02007680 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681
7682 if (time_for_testing == 93784)
7683 {
7684 /* Testing the two-character code. */
7685 buf[0] = 'x';
7686 buf[1] = '!';
7687 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007688#ifdef FEAT_TERMINAL
7689 else if (term_use_loop())
7690 buf[0] = 't';
7691#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 else if (VIsual_active)
7693 {
7694 if (VIsual_select)
7695 buf[0] = VIsual_mode + 's' - 'v';
7696 else
7697 buf[0] = VIsual_mode;
7698 }
7699 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7700 || State == CONFIRM)
7701 {
7702 buf[0] = 'r';
7703 if (State == ASKMORE)
7704 buf[1] = 'm';
7705 else if (State == CONFIRM)
7706 buf[1] = '?';
7707 }
7708 else if (State == EXTERNCMD)
7709 buf[0] = '!';
7710 else if (State & INSERT)
7711 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 if (State & VREPLACE_FLAG)
7713 {
7714 buf[0] = 'R';
7715 buf[1] = 'v';
7716 }
7717 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01007718 {
7719 if (State & REPLACE_FLAG)
7720 buf[0] = 'R';
7721 else
7722 buf[0] = 'i';
7723#ifdef FEAT_INS_EXPAND
7724 if (ins_compl_active())
7725 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01007726 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01007727 buf[1] = 'x';
7728#endif
7729 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007731 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732 {
7733 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007734 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007735 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007736 else if (exmode_active == EXMODE_NORMAL)
7737 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738 }
7739 else
7740 {
7741 buf[0] = 'n';
7742 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007743 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007745 // to be able to detect force-linewise/blockwise/characterwise operations
7746 buf[2] = motion_force;
7747 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02007748 else if (restart_edit == 'I' || restart_edit == 'R'
7749 || restart_edit == 'V')
7750 {
7751 buf[1] = 'i';
7752 buf[2] = restart_edit;
7753 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 }
7755
7756 /* Clear out the minor mode when the argument is not a non-zero number or
7757 * non-empty string. */
7758 if (!non_zero_arg(&argvars[0]))
7759 buf[1] = NUL;
7760
7761 rettv->vval.v_string = vim_strsave(buf);
7762 rettv->v_type = VAR_STRING;
7763}
7764
7765#if defined(FEAT_MZSCHEME) || defined(PROTO)
7766/*
7767 * "mzeval()" function
7768 */
7769 static void
7770f_mzeval(typval_T *argvars, typval_T *rettv)
7771{
7772 char_u *str;
7773 char_u buf[NUMBUFLEN];
7774
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007775 if (check_restricted() || check_secure())
7776 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007777 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 do_mzeval(str, rettv);
7779}
7780
7781 void
7782mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7783{
7784 typval_T argvars[3];
7785
7786 argvars[0].v_type = VAR_STRING;
7787 argvars[0].vval.v_string = name;
7788 copy_tv(args, &argvars[1]);
7789 argvars[2].v_type = VAR_UNKNOWN;
7790 f_call(argvars, rettv);
7791 clear_tv(&argvars[1]);
7792}
7793#endif
7794
7795/*
7796 * "nextnonblank()" function
7797 */
7798 static void
7799f_nextnonblank(typval_T *argvars, typval_T *rettv)
7800{
7801 linenr_T lnum;
7802
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 {
7805 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7806 {
7807 lnum = 0;
7808 break;
7809 }
7810 if (*skipwhite(ml_get(lnum)) != NUL)
7811 break;
7812 }
7813 rettv->vval.v_number = lnum;
7814}
7815
7816/*
7817 * "nr2char()" function
7818 */
7819 static void
7820f_nr2char(typval_T *argvars, typval_T *rettv)
7821{
7822 char_u buf[NUMBUFLEN];
7823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007824 if (has_mbyte)
7825 {
7826 int utf8 = 0;
7827
7828 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01007831 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007833 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 }
7835 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007837 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 buf[1] = NUL;
7839 }
7840 rettv->v_type = VAR_STRING;
7841 rettv->vval.v_string = vim_strsave(buf);
7842}
7843
7844/*
7845 * "or(expr, expr)" function
7846 */
7847 static void
7848f_or(typval_T *argvars, typval_T *rettv)
7849{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007850 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
7851 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852}
7853
7854/*
7855 * "pathshorten()" function
7856 */
7857 static void
7858f_pathshorten(typval_T *argvars, typval_T *rettv)
7859{
7860 char_u *p;
7861
7862 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007863 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007864 if (p == NULL)
7865 rettv->vval.v_string = NULL;
7866 else
7867 {
7868 p = vim_strsave(p);
7869 rettv->vval.v_string = p;
7870 if (p != NULL)
7871 shorten_dir(p);
7872 }
7873}
7874
7875#ifdef FEAT_PERL
7876/*
7877 * "perleval()" function
7878 */
7879 static void
7880f_perleval(typval_T *argvars, typval_T *rettv)
7881{
7882 char_u *str;
7883 char_u buf[NUMBUFLEN];
7884
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007885 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007886 do_perleval(str, rettv);
7887}
7888#endif
7889
7890#ifdef FEAT_FLOAT
7891/*
7892 * "pow()" function
7893 */
7894 static void
7895f_pow(typval_T *argvars, typval_T *rettv)
7896{
7897 float_T fx = 0.0, fy = 0.0;
7898
7899 rettv->v_type = VAR_FLOAT;
7900 if (get_float_arg(argvars, &fx) == OK
7901 && get_float_arg(&argvars[1], &fy) == OK)
7902 rettv->vval.v_float = pow(fx, fy);
7903 else
7904 rettv->vval.v_float = 0.0;
7905}
7906#endif
7907
7908/*
7909 * "prevnonblank()" function
7910 */
7911 static void
7912f_prevnonblank(typval_T *argvars, typval_T *rettv)
7913{
7914 linenr_T lnum;
7915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007916 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7918 lnum = 0;
7919 else
7920 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7921 --lnum;
7922 rettv->vval.v_number = lnum;
7923}
7924
7925/* This dummy va_list is here because:
7926 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7927 * - locally in the function results in a "used before set" warning
7928 * - using va_start() to initialize it gives "function with fixed args" error */
7929static va_list ap;
7930
7931/*
7932 * "printf()" function
7933 */
7934 static void
7935f_printf(typval_T *argvars, typval_T *rettv)
7936{
7937 char_u buf[NUMBUFLEN];
7938 int len;
7939 char_u *s;
7940 int saved_did_emsg = did_emsg;
7941 char *fmt;
7942
7943 rettv->v_type = VAR_STRING;
7944 rettv->vval.v_string = NULL;
7945
7946 /* Get the required length, allocate the buffer and do it for real. */
7947 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007948 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007949 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007950 if (!did_emsg)
7951 {
7952 s = alloc(len + 1);
7953 if (s != NULL)
7954 {
7955 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007956 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7957 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958 }
7959 }
7960 did_emsg |= saved_did_emsg;
7961}
7962
7963/*
7964 * "pumvisible()" function
7965 */
7966 static void
7967f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7968{
7969#ifdef FEAT_INS_EXPAND
7970 if (pum_visible())
7971 rettv->vval.v_number = 1;
7972#endif
7973}
7974
7975#ifdef FEAT_PYTHON3
7976/*
7977 * "py3eval()" function
7978 */
7979 static void
7980f_py3eval(typval_T *argvars, typval_T *rettv)
7981{
7982 char_u *str;
7983 char_u buf[NUMBUFLEN];
7984
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007985 if (check_restricted() || check_secure())
7986 return;
7987
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007988 if (p_pyx == 0)
7989 p_pyx = 3;
7990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007991 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 do_py3eval(str, rettv);
7993}
7994#endif
7995
7996#ifdef FEAT_PYTHON
7997/*
7998 * "pyeval()" function
7999 */
8000 static void
8001f_pyeval(typval_T *argvars, typval_T *rettv)
8002{
8003 char_u *str;
8004 char_u buf[NUMBUFLEN];
8005
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008006 if (check_restricted() || check_secure())
8007 return;
8008
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008009 if (p_pyx == 0)
8010 p_pyx = 2;
8011
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008012 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013 do_pyeval(str, rettv);
8014}
8015#endif
8016
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008017#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8018/*
8019 * "pyxeval()" function
8020 */
8021 static void
8022f_pyxeval(typval_T *argvars, typval_T *rettv)
8023{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008024 if (check_restricted() || check_secure())
8025 return;
8026
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008027# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8028 init_pyxversion();
8029 if (p_pyx == 2)
8030 f_pyeval(argvars, rettv);
8031 else
8032 f_py3eval(argvars, rettv);
8033# elif defined(FEAT_PYTHON)
8034 f_pyeval(argvars, rettv);
8035# elif defined(FEAT_PYTHON3)
8036 f_py3eval(argvars, rettv);
8037# endif
8038}
8039#endif
8040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008041/*
8042 * "range()" function
8043 */
8044 static void
8045f_range(typval_T *argvars, typval_T *rettv)
8046{
8047 varnumber_T start;
8048 varnumber_T end;
8049 varnumber_T stride = 1;
8050 varnumber_T i;
8051 int error = FALSE;
8052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008053 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008054 if (argvars[1].v_type == VAR_UNKNOWN)
8055 {
8056 end = start - 1;
8057 start = 0;
8058 }
8059 else
8060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008061 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008062 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008063 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008064 }
8065
8066 if (error)
8067 return; /* type error; errmsg already given */
8068 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008069 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008070 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008071 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008072 else
8073 {
8074 if (rettv_list_alloc(rettv) == OK)
8075 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8076 if (list_append_number(rettv->vval.v_list,
8077 (varnumber_T)i) == FAIL)
8078 break;
8079 }
8080}
8081
8082/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008083 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008084 */
8085 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008086readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008087{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008088 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008089 typval_T save_val;
8090 typval_T rettv;
8091 typval_T argv[2];
8092 int retval = 0;
8093 int error = FALSE;
8094
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008095 if (expr->v_type == VAR_UNKNOWN)
8096 return 1;
8097
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008098 prepare_vimvar(VV_VAL, &save_val);
8099 set_vim_var_string(VV_VAL, name, -1);
8100 argv[0].v_type = VAR_STRING;
8101 argv[0].vval.v_string = name;
8102
8103 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8104 goto theend;
8105
8106 retval = tv_get_number_chk(&rettv, &error);
8107 if (error)
8108 retval = -1;
8109 clear_tv(&rettv);
8110
8111theend:
8112 set_vim_var_string(VV_VAL, NULL, 0);
8113 restore_vimvar(VV_VAL, &save_val);
8114 return retval;
8115}
8116
8117/*
8118 * "readdir()" function
8119 */
8120 static void
8121f_readdir(typval_T *argvars, typval_T *rettv)
8122{
8123 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008124 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008125 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008126 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008127 garray_T ga;
8128 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008129
8130 if (rettv_list_alloc(rettv) == FAIL)
8131 return;
8132 path = tv_get_string(&argvars[0]);
8133 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008134
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008135 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8136 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008137 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008138 for (i = 0; i < ga.ga_len; i++)
8139 {
8140 p = ((char_u **)ga.ga_data)[i];
8141 list_append_string(rettv->vval.v_list, p, -1);
8142 }
8143 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008144 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008145}
8146
8147/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 * "readfile()" function
8149 */
8150 static void
8151f_readfile(typval_T *argvars, typval_T *rettv)
8152{
8153 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008154 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 int failed = FALSE;
8156 char_u *fname;
8157 FILE *fd;
8158 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8159 int io_size = sizeof(buf);
8160 int readlen; /* size of last fread() */
8161 char_u *prev = NULL; /* previously read bytes, if any */
8162 long prevlen = 0; /* length of data in prev */
8163 long prevsize = 0; /* size of prev buffer */
8164 long maxline = MAXLNUM;
8165 long cnt = 0;
8166 char_u *p; /* position in buf */
8167 char_u *start; /* start of current line */
8168
8169 if (argvars[1].v_type != VAR_UNKNOWN)
8170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008171 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008173 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8174 blob = TRUE;
8175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008177 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 }
8179
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008180 if (blob)
8181 {
8182 if (rettv_blob_alloc(rettv) == FAIL)
8183 return;
8184 }
8185 else
8186 {
8187 if (rettv_list_alloc(rettv) == FAIL)
8188 return;
8189 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008190
8191 /* Always open the file in binary mode, library functions have a mind of
8192 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008193 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008196 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197 return;
8198 }
8199
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008200 if (blob)
8201 {
8202 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008204 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008205 blob_free(rettv->vval.v_blob);
8206 }
8207 fclose(fd);
8208 return;
8209 }
8210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 while (cnt < maxline || maxline < 0)
8212 {
8213 readlen = (int)fread(buf, 1, io_size, fd);
8214
8215 /* This for loop processes what was read, but is also entered at end
8216 * of file so that either:
8217 * - an incomplete line gets written
8218 * - a "binary" file gets an empty line at the end if it ends in a
8219 * newline. */
8220 for (p = buf, start = buf;
8221 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8222 ++p)
8223 {
8224 if (*p == '\n' || readlen <= 0)
8225 {
8226 listitem_T *li;
8227 char_u *s = NULL;
8228 long_u len = p - start;
8229
8230 /* Finished a line. Remove CRs before NL. */
8231 if (readlen > 0 && !binary)
8232 {
8233 while (len > 0 && start[len - 1] == '\r')
8234 --len;
8235 /* removal may cross back to the "prev" string */
8236 if (len == 0)
8237 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8238 --prevlen;
8239 }
8240 if (prevlen == 0)
8241 s = vim_strnsave(start, (int)len);
8242 else
8243 {
8244 /* Change "prev" buffer to be the right size. This way
8245 * the bytes are only copied once, and very long lines are
8246 * allocated only once. */
8247 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8248 {
8249 mch_memmove(s + prevlen, start, len);
8250 s[prevlen + len] = NUL;
8251 prev = NULL; /* the list will own the string */
8252 prevlen = prevsize = 0;
8253 }
8254 }
8255 if (s == NULL)
8256 {
8257 do_outofmem_msg((long_u) prevlen + len + 1);
8258 failed = TRUE;
8259 break;
8260 }
8261
8262 if ((li = listitem_alloc()) == NULL)
8263 {
8264 vim_free(s);
8265 failed = TRUE;
8266 break;
8267 }
8268 li->li_tv.v_type = VAR_STRING;
8269 li->li_tv.v_lock = 0;
8270 li->li_tv.vval.v_string = s;
8271 list_append(rettv->vval.v_list, li);
8272
8273 start = p + 1; /* step over newline */
8274 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8275 break;
8276 }
8277 else if (*p == NUL)
8278 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008279 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8280 * when finding the BF and check the previous two bytes. */
8281 else if (*p == 0xbf && enc_utf8 && !binary)
8282 {
8283 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8284 * + 1, these may be in the "prev" string. */
8285 char_u back1 = p >= buf + 1 ? p[-1]
8286 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8287 char_u back2 = p >= buf + 2 ? p[-2]
8288 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8289 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8290
8291 if (back2 == 0xef && back1 == 0xbb)
8292 {
8293 char_u *dest = p - 2;
8294
8295 /* Usually a BOM is at the beginning of a file, and so at
8296 * the beginning of a line; then we can just step over it.
8297 */
8298 if (start == dest)
8299 start = p + 1;
8300 else
8301 {
8302 /* have to shuffle buf to close gap */
8303 int adjust_prevlen = 0;
8304
8305 if (dest < buf)
8306 {
8307 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8308 dest = buf;
8309 }
8310 if (readlen > p - buf + 1)
8311 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8312 readlen -= 3 - adjust_prevlen;
8313 prevlen -= adjust_prevlen;
8314 p = dest - 1;
8315 }
8316 }
8317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008318 } /* for */
8319
8320 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8321 break;
8322 if (start < p)
8323 {
8324 /* There's part of a line in buf, store it in "prev". */
8325 if (p - start + prevlen >= prevsize)
8326 {
8327 /* need bigger "prev" buffer */
8328 char_u *newprev;
8329
8330 /* A common use case is ordinary text files and "prev" gets a
8331 * fragment of a line, so the first allocation is made
8332 * small, to avoid repeatedly 'allocing' large and
8333 * 'reallocing' small. */
8334 if (prevsize == 0)
8335 prevsize = (long)(p - start);
8336 else
8337 {
8338 long grow50pc = (prevsize * 3) / 2;
8339 long growmin = (long)((p - start) * 2 + prevlen);
8340 prevsize = grow50pc > growmin ? grow50pc : growmin;
8341 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008342 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 if (newprev == NULL)
8344 {
8345 do_outofmem_msg((long_u)prevsize);
8346 failed = TRUE;
8347 break;
8348 }
8349 prev = newprev;
8350 }
8351 /* Add the line part to end of "prev". */
8352 mch_memmove(prev + prevlen, start, p - start);
8353 prevlen += (long)(p - start);
8354 }
8355 } /* while */
8356
8357 /*
8358 * For a negative line count use only the lines at the end of the file,
8359 * free the rest.
8360 */
8361 if (!failed && maxline < 0)
8362 while (cnt > -maxline)
8363 {
8364 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8365 --cnt;
8366 }
8367
8368 if (failed)
8369 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008370 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008372 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 }
8374
8375 vim_free(prev);
8376 fclose(fd);
8377}
8378
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008379 static void
8380return_register(int regname, typval_T *rettv)
8381{
8382 char_u buf[2] = {0, 0};
8383
8384 buf[0] = (char_u)regname;
8385 rettv->v_type = VAR_STRING;
8386 rettv->vval.v_string = vim_strsave(buf);
8387}
8388
8389/*
8390 * "reg_executing()" function
8391 */
8392 static void
8393f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8394{
8395 return_register(reg_executing, rettv);
8396}
8397
8398/*
8399 * "reg_recording()" function
8400 */
8401 static void
8402f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8403{
8404 return_register(reg_recording, rettv);
8405}
8406
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408/*
8409 * Convert a List to proftime_T.
8410 * Return FAIL when there is something wrong.
8411 */
8412 static int
8413list2proftime(typval_T *arg, proftime_T *tm)
8414{
8415 long n1, n2;
8416 int error = FALSE;
8417
8418 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8419 || arg->vval.v_list->lv_len != 2)
8420 return FAIL;
8421 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8422 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008423# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008424 tm->HighPart = n1;
8425 tm->LowPart = n2;
8426# else
8427 tm->tv_sec = n1;
8428 tm->tv_usec = n2;
8429# endif
8430 return error ? FAIL : OK;
8431}
8432#endif /* FEAT_RELTIME */
8433
8434/*
8435 * "reltime()" function
8436 */
8437 static void
8438f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8439{
8440#ifdef FEAT_RELTIME
8441 proftime_T res;
8442 proftime_T start;
8443
8444 if (argvars[0].v_type == VAR_UNKNOWN)
8445 {
8446 /* No arguments: get current time. */
8447 profile_start(&res);
8448 }
8449 else if (argvars[1].v_type == VAR_UNKNOWN)
8450 {
8451 if (list2proftime(&argvars[0], &res) == FAIL)
8452 return;
8453 profile_end(&res);
8454 }
8455 else
8456 {
8457 /* Two arguments: compute the difference. */
8458 if (list2proftime(&argvars[0], &start) == FAIL
8459 || list2proftime(&argvars[1], &res) == FAIL)
8460 return;
8461 profile_sub(&res, &start);
8462 }
8463
8464 if (rettv_list_alloc(rettv) == OK)
8465 {
8466 long n1, n2;
8467
Bram Moolenaar4f974752019-02-17 17:44:42 +01008468# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 n1 = res.HighPart;
8470 n2 = res.LowPart;
8471# else
8472 n1 = res.tv_sec;
8473 n2 = res.tv_usec;
8474# endif
8475 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8476 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8477 }
8478#endif
8479}
8480
8481#ifdef FEAT_FLOAT
8482/*
8483 * "reltimefloat()" function
8484 */
8485 static void
8486f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8487{
8488# ifdef FEAT_RELTIME
8489 proftime_T tm;
8490# endif
8491
8492 rettv->v_type = VAR_FLOAT;
8493 rettv->vval.v_float = 0;
8494# ifdef FEAT_RELTIME
8495 if (list2proftime(&argvars[0], &tm) == OK)
8496 rettv->vval.v_float = profile_float(&tm);
8497# endif
8498}
8499#endif
8500
8501/*
8502 * "reltimestr()" function
8503 */
8504 static void
8505f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8506{
8507#ifdef FEAT_RELTIME
8508 proftime_T tm;
8509#endif
8510
8511 rettv->v_type = VAR_STRING;
8512 rettv->vval.v_string = NULL;
8513#ifdef FEAT_RELTIME
8514 if (list2proftime(&argvars[0], &tm) == OK)
8515 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8516#endif
8517}
8518
8519#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 static void
8521make_connection(void)
8522{
8523 if (X_DISPLAY == NULL
8524# ifdef FEAT_GUI
8525 && !gui.in_use
8526# endif
8527 )
8528 {
8529 x_force_connect = TRUE;
8530 setup_term_clip();
8531 x_force_connect = FALSE;
8532 }
8533}
8534
8535 static int
8536check_connection(void)
8537{
8538 make_connection();
8539 if (X_DISPLAY == NULL)
8540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008541 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 return FAIL;
8543 }
8544 return OK;
8545}
8546#endif
8547
8548#ifdef FEAT_CLIENTSERVER
8549 static void
8550remote_common(typval_T *argvars, typval_T *rettv, int expr)
8551{
8552 char_u *server_name;
8553 char_u *keys;
8554 char_u *r = NULL;
8555 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008556 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008557# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 HWND w;
8559# else
8560 Window w;
8561# endif
8562
8563 if (check_restricted() || check_secure())
8564 return;
8565
8566# ifdef FEAT_X11
8567 if (check_connection() == FAIL)
8568 return;
8569# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008570 if (argvars[2].v_type != VAR_UNKNOWN
8571 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008572 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008574 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 if (server_name == NULL)
8576 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008577 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008578# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008579 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008581 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8582 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583# endif
8584 {
8585 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008588 vim_free(r);
8589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008592 return;
8593 }
8594
8595 rettv->vval.v_string = r;
8596
8597 if (argvars[2].v_type != VAR_UNKNOWN)
8598 {
8599 dictitem_T v;
8600 char_u str[30];
8601 char_u *idvar;
8602
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008603 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008604 if (idvar != NULL && *idvar != NUL)
8605 {
8606 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8607 v.di_tv.v_type = VAR_STRING;
8608 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008610 vim_free(v.di_tv.vval.v_string);
8611 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 }
8613}
8614#endif
8615
8616/*
8617 * "remote_expr()" function
8618 */
8619 static void
8620f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8621{
8622 rettv->v_type = VAR_STRING;
8623 rettv->vval.v_string = NULL;
8624#ifdef FEAT_CLIENTSERVER
8625 remote_common(argvars, rettv, TRUE);
8626#endif
8627}
8628
8629/*
8630 * "remote_foreground()" function
8631 */
8632 static void
8633f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8634{
8635#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008636# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 /* On Win32 it's done in this application. */
8638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008639 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008640
8641 if (server_name != NULL)
8642 serverForeground(server_name);
8643 }
8644# else
8645 /* Send a foreground() expression to the server. */
8646 argvars[1].v_type = VAR_STRING;
8647 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8648 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008649 rettv->v_type = VAR_STRING;
8650 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 remote_common(argvars, rettv, TRUE);
8652 vim_free(argvars[1].vval.v_string);
8653# endif
8654#endif
8655}
8656
8657 static void
8658f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8659{
8660#ifdef FEAT_CLIENTSERVER
8661 dictitem_T v;
8662 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008663# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 long_u n = 0;
8665# endif
8666 char_u *serverid;
8667
8668 if (check_restricted() || check_secure())
8669 {
8670 rettv->vval.v_number = -1;
8671 return;
8672 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008673 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 if (serverid == NULL)
8675 {
8676 rettv->vval.v_number = -1;
8677 return; /* type error; errmsg already given */
8678 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008679# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8681 if (n == 0)
8682 rettv->vval.v_number = -1;
8683 else
8684 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008685 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 rettv->vval.v_number = (s != NULL);
8687 }
8688# else
8689 if (check_connection() == FAIL)
8690 return;
8691
8692 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8693 serverStrToWin(serverid), &s);
8694# endif
8695
8696 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8697 {
8698 char_u *retvar;
8699
8700 v.di_tv.v_type = VAR_STRING;
8701 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008702 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 if (retvar != NULL)
8704 set_var(retvar, &v.di_tv, FALSE);
8705 vim_free(v.di_tv.vval.v_string);
8706 }
8707#else
8708 rettv->vval.v_number = -1;
8709#endif
8710}
8711
8712 static void
8713f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8714{
8715 char_u *r = NULL;
8716
8717#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008718 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719
8720 if (serverid != NULL && !check_restricted() && !check_secure())
8721 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008722 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008723# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008724 /* The server's HWND is encoded in the 'id' parameter */
8725 long_u n = 0;
8726# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008727
8728 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008729 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008730
Bram Moolenaar4f974752019-02-17 17:44:42 +01008731# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8733 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008734 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 if (r == NULL)
8736# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008737 if (check_connection() == FAIL
8738 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8739 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008741 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008742 }
8743#endif
8744 rettv->v_type = VAR_STRING;
8745 rettv->vval.v_string = r;
8746}
8747
8748/*
8749 * "remote_send()" function
8750 */
8751 static void
8752f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8753{
8754 rettv->v_type = VAR_STRING;
8755 rettv->vval.v_string = NULL;
8756#ifdef FEAT_CLIENTSERVER
8757 remote_common(argvars, rettv, FALSE);
8758#endif
8759}
8760
8761/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008762 * "remote_startserver()" function
8763 */
8764 static void
8765f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8766{
8767#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008768 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008769
8770 if (server == NULL)
8771 return; /* type error; errmsg already given */
8772 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008773 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008774 else
8775 {
8776# ifdef FEAT_X11
8777 if (check_connection() == OK)
8778 serverRegisterName(X_DISPLAY, server);
8779# else
8780 serverSetName(server);
8781# endif
8782 }
8783#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008784 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008785#endif
8786}
8787
8788/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 * "remove()" function
8790 */
8791 static void
8792f_remove(typval_T *argvars, typval_T *rettv)
8793{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8795
8796 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008797 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008798 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008799 blob_remove(argvars, rettv);
8800 else if (argvars[0].v_type == VAR_LIST)
8801 list_remove(argvars, rettv, arg_errmsg);
8802 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01008803 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008804}
8805
8806/*
8807 * "rename({from}, {to})" function
8808 */
8809 static void
8810f_rename(typval_T *argvars, typval_T *rettv)
8811{
8812 char_u buf[NUMBUFLEN];
8813
8814 if (check_restricted() || check_secure())
8815 rettv->vval.v_number = -1;
8816 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
8818 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819}
8820
8821/*
8822 * "repeat()" function
8823 */
8824 static void
8825f_repeat(typval_T *argvars, typval_T *rettv)
8826{
8827 char_u *p;
8828 int n;
8829 int slen;
8830 int len;
8831 char_u *r;
8832 int i;
8833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 if (argvars[0].v_type == VAR_LIST)
8836 {
8837 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8838 while (n-- > 0)
8839 if (list_extend(rettv->vval.v_list,
8840 argvars[0].vval.v_list, NULL) == FAIL)
8841 break;
8842 }
8843 else
8844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008845 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 rettv->v_type = VAR_STRING;
8847 rettv->vval.v_string = NULL;
8848
8849 slen = (int)STRLEN(p);
8850 len = slen * n;
8851 if (len <= 0)
8852 return;
8853
8854 r = alloc(len + 1);
8855 if (r != NULL)
8856 {
8857 for (i = 0; i < n; i++)
8858 mch_memmove(r + i * slen, p, (size_t)slen);
8859 r[len] = NUL;
8860 }
8861
8862 rettv->vval.v_string = r;
8863 }
8864}
8865
8866/*
8867 * "resolve()" function
8868 */
8869 static void
8870f_resolve(typval_T *argvars, typval_T *rettv)
8871{
8872 char_u *p;
8873#ifdef HAVE_READLINK
8874 char_u *buf = NULL;
8875#endif
8876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008877 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878#ifdef FEAT_SHORTCUT
8879 {
8880 char_u *v = NULL;
8881
Bram Moolenaardce1e892019-02-10 23:18:53 +01008882 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 if (v != NULL)
8884 rettv->vval.v_string = v;
8885 else
8886 rettv->vval.v_string = vim_strsave(p);
8887 }
8888#else
8889# ifdef HAVE_READLINK
8890 {
8891 char_u *cpy;
8892 int len;
8893 char_u *remain = NULL;
8894 char_u *q;
8895 int is_relative_to_current = FALSE;
8896 int has_trailing_pathsep = FALSE;
8897 int limit = 100;
8898
8899 p = vim_strsave(p);
8900
8901 if (p[0] == '.' && (vim_ispathsep(p[1])
8902 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8903 is_relative_to_current = TRUE;
8904
8905 len = STRLEN(p);
8906 if (len > 0 && after_pathsep(p, p + len))
8907 {
8908 has_trailing_pathsep = TRUE;
8909 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8910 }
8911
8912 q = getnextcomp(p);
8913 if (*q != NUL)
8914 {
8915 /* Separate the first path component in "p", and keep the
8916 * remainder (beginning with the path separator). */
8917 remain = vim_strsave(q - 1);
8918 q[-1] = NUL;
8919 }
8920
8921 buf = alloc(MAXPATHL + 1);
8922 if (buf == NULL)
8923 goto fail;
8924
8925 for (;;)
8926 {
8927 for (;;)
8928 {
8929 len = readlink((char *)p, (char *)buf, MAXPATHL);
8930 if (len <= 0)
8931 break;
8932 buf[len] = NUL;
8933
8934 if (limit-- == 0)
8935 {
8936 vim_free(p);
8937 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008938 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 rettv->vval.v_string = NULL;
8940 goto fail;
8941 }
8942
8943 /* Ensure that the result will have a trailing path separator
8944 * if the argument has one. */
8945 if (remain == NULL && has_trailing_pathsep)
8946 add_pathsep(buf);
8947
8948 /* Separate the first path component in the link value and
8949 * concatenate the remainders. */
8950 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8951 if (*q != NUL)
8952 {
8953 if (remain == NULL)
8954 remain = vim_strsave(q - 1);
8955 else
8956 {
8957 cpy = concat_str(q - 1, remain);
8958 if (cpy != NULL)
8959 {
8960 vim_free(remain);
8961 remain = cpy;
8962 }
8963 }
8964 q[-1] = NUL;
8965 }
8966
8967 q = gettail(p);
8968 if (q > p && *q == NUL)
8969 {
8970 /* Ignore trailing path separator. */
8971 q[-1] = NUL;
8972 q = gettail(p);
8973 }
8974 if (q > p && !mch_isFullName(buf))
8975 {
8976 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02008977 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008978 if (cpy != NULL)
8979 {
8980 STRCPY(cpy, p);
8981 STRCPY(gettail(cpy), buf);
8982 vim_free(p);
8983 p = cpy;
8984 }
8985 }
8986 else
8987 {
8988 vim_free(p);
8989 p = vim_strsave(buf);
8990 }
8991 }
8992
8993 if (remain == NULL)
8994 break;
8995
8996 /* Append the first path component of "remain" to "p". */
8997 q = getnextcomp(remain + 1);
8998 len = q - remain - (*q != NUL);
8999 cpy = vim_strnsave(p, STRLEN(p) + len);
9000 if (cpy != NULL)
9001 {
9002 STRNCAT(cpy, remain, len);
9003 vim_free(p);
9004 p = cpy;
9005 }
9006 /* Shorten "remain". */
9007 if (*q != NUL)
9008 STRMOVE(remain, q - 1);
9009 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009010 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 }
9012
9013 /* If the result is a relative path name, make it explicitly relative to
9014 * the current directory if and only if the argument had this form. */
9015 if (!vim_ispathsep(*p))
9016 {
9017 if (is_relative_to_current
9018 && *p != NUL
9019 && !(p[0] == '.'
9020 && (p[1] == NUL
9021 || vim_ispathsep(p[1])
9022 || (p[1] == '.'
9023 && (p[2] == NUL
9024 || vim_ispathsep(p[2]))))))
9025 {
9026 /* Prepend "./". */
9027 cpy = concat_str((char_u *)"./", p);
9028 if (cpy != NULL)
9029 {
9030 vim_free(p);
9031 p = cpy;
9032 }
9033 }
9034 else if (!is_relative_to_current)
9035 {
9036 /* Strip leading "./". */
9037 q = p;
9038 while (q[0] == '.' && vim_ispathsep(q[1]))
9039 q += 2;
9040 if (q > p)
9041 STRMOVE(p, p + 2);
9042 }
9043 }
9044
9045 /* Ensure that the result will have no trailing path separator
9046 * if the argument had none. But keep "/" or "//". */
9047 if (!has_trailing_pathsep)
9048 {
9049 q = p + STRLEN(p);
9050 if (after_pathsep(p, q))
9051 *gettail_sep(p) = NUL;
9052 }
9053
9054 rettv->vval.v_string = p;
9055 }
9056# else
9057 rettv->vval.v_string = vim_strsave(p);
9058# endif
9059#endif
9060
9061 simplify_filename(rettv->vval.v_string);
9062
9063#ifdef HAVE_READLINK
9064fail:
9065 vim_free(buf);
9066#endif
9067 rettv->v_type = VAR_STRING;
9068}
9069
9070/*
9071 * "reverse({list})" function
9072 */
9073 static void
9074f_reverse(typval_T *argvars, typval_T *rettv)
9075{
9076 list_T *l;
9077 listitem_T *li, *ni;
9078
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009079 if (argvars[0].v_type == VAR_BLOB)
9080 {
9081 blob_T *b = argvars[0].vval.v_blob;
9082 int i, len = blob_len(b);
9083
9084 for (i = 0; i < len / 2; i++)
9085 {
9086 int tmp = blob_get(b, i);
9087
9088 blob_set(b, i, blob_get(b, len - i - 1));
9089 blob_set(b, len - i - 1, tmp);
9090 }
9091 rettv_blob_set(rettv, b);
9092 return;
9093 }
9094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009096 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009098 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009099 (char_u *)N_("reverse() argument"), TRUE))
9100 {
9101 li = l->lv_last;
9102 l->lv_first = l->lv_last = NULL;
9103 l->lv_len = 0;
9104 while (li != NULL)
9105 {
9106 ni = li->li_prev;
9107 list_append(l, li);
9108 li = ni;
9109 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009110 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 l->lv_idx = l->lv_len - l->lv_idx - 1;
9112 }
9113}
9114
9115#define SP_NOMOVE 0x01 /* don't move cursor */
9116#define SP_REPEAT 0x02 /* repeat to find outer pair */
9117#define SP_RETCOUNT 0x04 /* return matchcount */
9118#define SP_SETPCMARK 0x08 /* set previous context mark */
9119#define SP_START 0x10 /* accept match at start position */
9120#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9121#define SP_END 0x40 /* leave cursor at end of match */
9122#define SP_COLUMN 0x80 /* start at cursor column */
9123
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124/*
9125 * Get flags for a search function.
9126 * Possibly sets "p_ws".
9127 * Returns BACKWARD, FORWARD or zero (for an error).
9128 */
9129 static int
9130get_search_arg(typval_T *varp, int *flagsp)
9131{
9132 int dir = FORWARD;
9133 char_u *flags;
9134 char_u nbuf[NUMBUFLEN];
9135 int mask;
9136
9137 if (varp->v_type != VAR_UNKNOWN)
9138 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009139 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009140 if (flags == NULL)
9141 return 0; /* type error; errmsg already given */
9142 while (*flags != NUL)
9143 {
9144 switch (*flags)
9145 {
9146 case 'b': dir = BACKWARD; break;
9147 case 'w': p_ws = TRUE; break;
9148 case 'W': p_ws = FALSE; break;
9149 default: mask = 0;
9150 if (flagsp != NULL)
9151 switch (*flags)
9152 {
9153 case 'c': mask = SP_START; break;
9154 case 'e': mask = SP_END; break;
9155 case 'm': mask = SP_RETCOUNT; break;
9156 case 'n': mask = SP_NOMOVE; break;
9157 case 'p': mask = SP_SUBPAT; break;
9158 case 'r': mask = SP_REPEAT; break;
9159 case 's': mask = SP_SETPCMARK; break;
9160 case 'z': mask = SP_COLUMN; break;
9161 }
9162 if (mask == 0)
9163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009164 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 dir = 0;
9166 }
9167 else
9168 *flagsp |= mask;
9169 }
9170 if (dir == 0)
9171 break;
9172 ++flags;
9173 }
9174 }
9175 return dir;
9176}
9177
9178/*
9179 * Shared by search() and searchpos() functions.
9180 */
9181 static int
9182search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9183{
9184 int flags;
9185 char_u *pat;
9186 pos_T pos;
9187 pos_T save_cursor;
9188 int save_p_ws = p_ws;
9189 int dir;
9190 int retval = 0; /* default: FAIL */
9191 long lnum_stop = 0;
9192 proftime_T tm;
9193#ifdef FEAT_RELTIME
9194 long time_limit = 0;
9195#endif
9196 int options = SEARCH_KEEP;
9197 int subpatnum;
9198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009199 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9201 if (dir == 0)
9202 goto theend;
9203 flags = *flagsp;
9204 if (flags & SP_START)
9205 options |= SEARCH_START;
9206 if (flags & SP_END)
9207 options |= SEARCH_END;
9208 if (flags & SP_COLUMN)
9209 options |= SEARCH_COL;
9210
9211 /* Optional arguments: line number to stop searching and timeout. */
9212 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009214 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009215 if (lnum_stop < 0)
9216 goto theend;
9217#ifdef FEAT_RELTIME
9218 if (argvars[3].v_type != VAR_UNKNOWN)
9219 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009220 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221 if (time_limit < 0)
9222 goto theend;
9223 }
9224#endif
9225 }
9226
9227#ifdef FEAT_RELTIME
9228 /* Set the time limit, if there is one. */
9229 profile_setlimit(time_limit, &tm);
9230#endif
9231
9232 /*
9233 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9234 * Check to make sure only those flags are set.
9235 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9236 * flags cannot be set. Check for that condition also.
9237 */
9238 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9239 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009241 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242 goto theend;
9243 }
9244
9245 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009246 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009247 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009248 if (subpatnum != FAIL)
9249 {
9250 if (flags & SP_SUBPAT)
9251 retval = subpatnum;
9252 else
9253 retval = pos.lnum;
9254 if (flags & SP_SETPCMARK)
9255 setpcmark();
9256 curwin->w_cursor = pos;
9257 if (match_pos != NULL)
9258 {
9259 /* Store the match cursor position */
9260 match_pos->lnum = pos.lnum;
9261 match_pos->col = pos.col + 1;
9262 }
9263 /* "/$" will put the cursor after the end of the line, may need to
9264 * correct that here */
9265 check_cursor();
9266 }
9267
9268 /* If 'n' flag is used: restore cursor position. */
9269 if (flags & SP_NOMOVE)
9270 curwin->w_cursor = save_cursor;
9271 else
9272 curwin->w_set_curswant = TRUE;
9273theend:
9274 p_ws = save_p_ws;
9275
9276 return retval;
9277}
9278
9279#ifdef FEAT_FLOAT
9280
9281/*
9282 * round() is not in C90, use ceil() or floor() instead.
9283 */
9284 float_T
9285vim_round(float_T f)
9286{
9287 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9288}
9289
9290/*
9291 * "round({float})" function
9292 */
9293 static void
9294f_round(typval_T *argvars, typval_T *rettv)
9295{
9296 float_T f = 0.0;
9297
9298 rettv->v_type = VAR_FLOAT;
9299 if (get_float_arg(argvars, &f) == OK)
9300 rettv->vval.v_float = vim_round(f);
9301 else
9302 rettv->vval.v_float = 0.0;
9303}
9304#endif
9305
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009306#ifdef FEAT_RUBY
9307/*
9308 * "rubyeval()" function
9309 */
9310 static void
9311f_rubyeval(typval_T *argvars, typval_T *rettv)
9312{
9313 char_u *str;
9314 char_u buf[NUMBUFLEN];
9315
9316 str = tv_get_string_buf(&argvars[0], buf);
9317 do_rubyeval(str, rettv);
9318}
9319#endif
9320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009321/*
9322 * "screenattr()" function
9323 */
9324 static void
9325f_screenattr(typval_T *argvars, typval_T *rettv)
9326{
9327 int row;
9328 int col;
9329 int c;
9330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009331 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9332 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 if (row < 0 || row >= screen_Rows
9334 || col < 0 || col >= screen_Columns)
9335 c = -1;
9336 else
9337 c = ScreenAttrs[LineOffset[row] + col];
9338 rettv->vval.v_number = c;
9339}
9340
9341/*
9342 * "screenchar()" function
9343 */
9344 static void
9345f_screenchar(typval_T *argvars, typval_T *rettv)
9346{
9347 int row;
9348 int col;
9349 int off;
9350 int c;
9351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9353 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009354 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009355 c = -1;
9356 else
9357 {
9358 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009359 if (enc_utf8 && ScreenLinesUC[off] != 0)
9360 c = ScreenLinesUC[off];
9361 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 c = ScreenLines[off];
9363 }
9364 rettv->vval.v_number = c;
9365}
9366
9367/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009368 * "screenchars()" function
9369 */
9370 static void
9371f_screenchars(typval_T *argvars, typval_T *rettv)
9372{
9373 int row;
9374 int col;
9375 int off;
9376 int c;
9377 int i;
9378
9379 if (rettv_list_alloc(rettv) == FAIL)
9380 return;
9381 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9382 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9383 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9384 return;
9385
9386 off = LineOffset[row] + col;
9387 if (enc_utf8 && ScreenLinesUC[off] != 0)
9388 c = ScreenLinesUC[off];
9389 else
9390 c = ScreenLines[off];
9391 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9392
9393 if (enc_utf8)
9394
9395 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9396 list_append_number(rettv->vval.v_list,
9397 (varnumber_T)ScreenLinesC[i][off]);
9398}
9399
9400/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009401 * "screencol()" function
9402 *
9403 * First column is 1 to be consistent with virtcol().
9404 */
9405 static void
9406f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9407{
9408 rettv->vval.v_number = screen_screencol() + 1;
9409}
9410
9411/*
9412 * "screenrow()" function
9413 */
9414 static void
9415f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9416{
9417 rettv->vval.v_number = screen_screenrow() + 1;
9418}
9419
9420/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009421 * "screenstring()" function
9422 */
9423 static void
9424f_screenstring(typval_T *argvars, typval_T *rettv)
9425{
9426 int row;
9427 int col;
9428 int off;
9429 int c;
9430 int i;
9431 char_u buf[MB_MAXBYTES + 1];
9432 int buflen = 0;
9433
9434 rettv->vval.v_string = NULL;
9435 rettv->v_type = VAR_STRING;
9436
9437 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9438 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9439 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9440 return;
9441
9442 off = LineOffset[row] + col;
9443 if (enc_utf8 && ScreenLinesUC[off] != 0)
9444 c = ScreenLinesUC[off];
9445 else
9446 c = ScreenLines[off];
9447 buflen += mb_char2bytes(c, buf);
9448
9449 if (enc_utf8)
9450 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9451 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9452
9453 buf[buflen] = NUL;
9454 rettv->vval.v_string = vim_strsave(buf);
9455}
9456
9457/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 * "search()" function
9459 */
9460 static void
9461f_search(typval_T *argvars, typval_T *rettv)
9462{
9463 int flags = 0;
9464
9465 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9466}
9467
9468/*
9469 * "searchdecl()" function
9470 */
9471 static void
9472f_searchdecl(typval_T *argvars, typval_T *rettv)
9473{
9474 int locally = 1;
9475 int thisblock = 0;
9476 int error = FALSE;
9477 char_u *name;
9478
9479 rettv->vval.v_number = 1; /* default: FAIL */
9480
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009481 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 if (argvars[1].v_type != VAR_UNKNOWN)
9483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009484 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009486 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 }
9488 if (!error && name != NULL)
9489 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9490 locally, thisblock, SEARCH_KEEP) == FAIL;
9491}
9492
9493/*
9494 * Used by searchpair() and searchpairpos()
9495 */
9496 static int
9497searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9498{
9499 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009500 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 int save_p_ws = p_ws;
9502 int dir;
9503 int flags = 0;
9504 char_u nbuf1[NUMBUFLEN];
9505 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 int retval = 0; /* default: FAIL */
9507 long lnum_stop = 0;
9508 long time_limit = 0;
9509
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009510 /* Get the three pattern arguments: start, middle, end. Will result in an
9511 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009512 spat = tv_get_string_chk(&argvars[0]);
9513 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9514 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009515 if (spat == NULL || mpat == NULL || epat == NULL)
9516 goto theend; /* type error */
9517
9518 /* Handle the optional fourth argument: flags */
9519 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9520 if (dir == 0)
9521 goto theend;
9522
9523 /* Don't accept SP_END or SP_SUBPAT.
9524 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9525 */
9526 if ((flags & (SP_END | SP_SUBPAT)) != 0
9527 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009529 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 goto theend;
9531 }
9532
9533 /* Using 'r' implies 'W', otherwise it doesn't work. */
9534 if (flags & SP_REPEAT)
9535 p_ws = FALSE;
9536
9537 /* Optional fifth argument: skip expression */
9538 if (argvars[3].v_type == VAR_UNKNOWN
9539 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009540 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 else
9542 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009543 skip = &argvars[4];
9544 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9545 && skip->v_type != VAR_STRING)
9546 {
9547 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009548 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009549 goto theend;
9550 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 if (argvars[5].v_type != VAR_UNKNOWN)
9552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009553 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009556 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009557 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009558 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559#ifdef FEAT_RELTIME
9560 if (argvars[6].v_type != VAR_UNKNOWN)
9561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009562 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009565 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009566 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009567 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009568 }
9569#endif
9570 }
9571 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572
9573 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9574 match_pos, lnum_stop, time_limit);
9575
9576theend:
9577 p_ws = save_p_ws;
9578
9579 return retval;
9580}
9581
9582/*
9583 * "searchpair()" function
9584 */
9585 static void
9586f_searchpair(typval_T *argvars, typval_T *rettv)
9587{
9588 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9589}
9590
9591/*
9592 * "searchpairpos()" function
9593 */
9594 static void
9595f_searchpairpos(typval_T *argvars, typval_T *rettv)
9596{
9597 pos_T match_pos;
9598 int lnum = 0;
9599 int col = 0;
9600
9601 if (rettv_list_alloc(rettv) == FAIL)
9602 return;
9603
9604 if (searchpair_cmn(argvars, &match_pos) > 0)
9605 {
9606 lnum = match_pos.lnum;
9607 col = match_pos.col;
9608 }
9609
9610 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9611 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9612}
9613
9614/*
9615 * Search for a start/middle/end thing.
9616 * Used by searchpair(), see its documentation for the details.
9617 * Returns 0 or -1 for no match,
9618 */
9619 long
9620do_searchpair(
9621 char_u *spat, /* start pattern */
9622 char_u *mpat, /* middle pattern */
9623 char_u *epat, /* end pattern */
9624 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009625 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626 int flags, /* SP_SETPCMARK and other SP_ values */
9627 pos_T *match_pos,
9628 linenr_T lnum_stop, /* stop at this line if not zero */
9629 long time_limit UNUSED) /* stop after this many msec */
9630{
9631 char_u *save_cpo;
9632 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9633 long retval = 0;
9634 pos_T pos;
9635 pos_T firstpos;
9636 pos_T foundpos;
9637 pos_T save_cursor;
9638 pos_T save_pos;
9639 int n;
9640 int r;
9641 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009642 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 int err;
9644 int options = SEARCH_KEEP;
9645 proftime_T tm;
9646
9647 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9648 save_cpo = p_cpo;
9649 p_cpo = empty_option;
9650
9651#ifdef FEAT_RELTIME
9652 /* Set the time limit, if there is one. */
9653 profile_setlimit(time_limit, &tm);
9654#endif
9655
9656 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9657 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009658 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9659 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 if (pat2 == NULL || pat3 == NULL)
9661 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009662 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009663 if (*mpat == NUL)
9664 STRCPY(pat3, pat2);
9665 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009666 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009667 spat, epat, mpat);
9668 if (flags & SP_START)
9669 options |= SEARCH_START;
9670
Bram Moolenaar48570482017-10-30 21:48:41 +01009671 if (skip != NULL)
9672 {
9673 /* Empty string means to not use the skip expression. */
9674 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9675 use_skip = skip->vval.v_string != NULL
9676 && *skip->vval.v_string != NUL;
9677 }
9678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 save_cursor = curwin->w_cursor;
9680 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009681 CLEAR_POS(&firstpos);
9682 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 pat = pat3;
9684 for (;;)
9685 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009686 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009687 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009688 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 /* didn't find it or found the first match again: FAIL */
9690 break;
9691
9692 if (firstpos.lnum == 0)
9693 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009694 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009695 {
9696 /* Found the same position again. Can happen with a pattern that
9697 * has "\zs" at the end and searching backwards. Advance one
9698 * character and try again. */
9699 if (dir == BACKWARD)
9700 decl(&pos);
9701 else
9702 incl(&pos);
9703 }
9704 foundpos = pos;
9705
9706 /* clear the start flag to avoid getting stuck here */
9707 options &= ~SEARCH_START;
9708
9709 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009710 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 {
9712 save_pos = curwin->w_cursor;
9713 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009714 err = FALSE;
9715 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716 curwin->w_cursor = save_pos;
9717 if (err)
9718 {
9719 /* Evaluating {skip} caused an error, break here. */
9720 curwin->w_cursor = save_cursor;
9721 retval = -1;
9722 break;
9723 }
9724 if (r)
9725 continue;
9726 }
9727
9728 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9729 {
9730 /* Found end when searching backwards or start when searching
9731 * forward: nested pair. */
9732 ++nest;
9733 pat = pat2; /* nested, don't search for middle */
9734 }
9735 else
9736 {
9737 /* Found end when searching forward or start when searching
9738 * backward: end of (nested) pair; or found middle in outer pair. */
9739 if (--nest == 1)
9740 pat = pat3; /* outer level, search for middle */
9741 }
9742
9743 if (nest == 0)
9744 {
9745 /* Found the match: return matchcount or line number. */
9746 if (flags & SP_RETCOUNT)
9747 ++retval;
9748 else
9749 retval = pos.lnum;
9750 if (flags & SP_SETPCMARK)
9751 setpcmark();
9752 curwin->w_cursor = pos;
9753 if (!(flags & SP_REPEAT))
9754 break;
9755 nest = 1; /* search for next unmatched */
9756 }
9757 }
9758
9759 if (match_pos != NULL)
9760 {
9761 /* Store the match cursor position */
9762 match_pos->lnum = curwin->w_cursor.lnum;
9763 match_pos->col = curwin->w_cursor.col + 1;
9764 }
9765
9766 /* If 'n' flag is used or search failed: restore cursor position. */
9767 if ((flags & SP_NOMOVE) || retval == 0)
9768 curwin->w_cursor = save_cursor;
9769
9770theend:
9771 vim_free(pat2);
9772 vim_free(pat3);
9773 if (p_cpo == empty_option)
9774 p_cpo = save_cpo;
9775 else
9776 /* Darn, evaluating the {skip} expression changed the value. */
9777 free_string_option(save_cpo);
9778
9779 return retval;
9780}
9781
9782/*
9783 * "searchpos()" function
9784 */
9785 static void
9786f_searchpos(typval_T *argvars, typval_T *rettv)
9787{
9788 pos_T match_pos;
9789 int lnum = 0;
9790 int col = 0;
9791 int n;
9792 int flags = 0;
9793
9794 if (rettv_list_alloc(rettv) == FAIL)
9795 return;
9796
9797 n = search_cmn(argvars, &match_pos, &flags);
9798 if (n > 0)
9799 {
9800 lnum = match_pos.lnum;
9801 col = match_pos.col;
9802 }
9803
9804 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9805 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9806 if (flags & SP_SUBPAT)
9807 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9808}
9809
9810 static void
9811f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9812{
9813#ifdef FEAT_CLIENTSERVER
9814 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009815 char_u *server = tv_get_string_chk(&argvars[0]);
9816 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817
9818 rettv->vval.v_number = -1;
9819 if (server == NULL || reply == NULL)
9820 return;
9821 if (check_restricted() || check_secure())
9822 return;
9823# ifdef FEAT_X11
9824 if (check_connection() == FAIL)
9825 return;
9826# endif
9827
9828 if (serverSendReply(server, reply) < 0)
9829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009830 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 return;
9832 }
9833 rettv->vval.v_number = 0;
9834#else
9835 rettv->vval.v_number = -1;
9836#endif
9837}
9838
9839 static void
9840f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9841{
9842 char_u *r = NULL;
9843
9844#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009845# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009846 r = serverGetVimNames();
9847# else
9848 make_connection();
9849 if (X_DISPLAY != NULL)
9850 r = serverGetVimNames(X_DISPLAY);
9851# endif
9852#endif
9853 rettv->v_type = VAR_STRING;
9854 rettv->vval.v_string = r;
9855}
9856
9857/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009858 * "setbufline()" function
9859 */
9860 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009861f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009862{
9863 linenr_T lnum;
9864 buf_T *buf;
9865
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009866 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009867 if (buf == NULL)
9868 rettv->vval.v_number = 1; /* FAIL */
9869 else
9870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009871 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02009872 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009873 }
9874}
9875
9876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009877 * "setbufvar()" function
9878 */
9879 static void
9880f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9881{
9882 buf_T *buf;
9883 char_u *varname, *bufvarname;
9884 typval_T *varp;
9885 char_u nbuf[NUMBUFLEN];
9886
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009887 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009889 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
9890 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009891 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 varp = &argvars[2];
9893
9894 if (buf != NULL && varname != NULL && varp != NULL)
9895 {
9896 if (*varname == '&')
9897 {
9898 long numval;
9899 char_u *strval;
9900 int error = FALSE;
9901 aco_save_T aco;
9902
9903 /* set curbuf to be our buf, temporarily */
9904 aucmd_prepbuf(&aco, buf);
9905
9906 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009907 numval = (long)tv_get_number_chk(varp, &error);
9908 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909 if (!error && strval != NULL)
9910 set_option_value(varname, numval, strval, OPT_LOCAL);
9911
9912 /* reset notion of buffer */
9913 aucmd_restbuf(&aco);
9914 }
9915 else
9916 {
9917 buf_T *save_curbuf = curbuf;
9918
Bram Moolenaar964b3742019-05-24 18:54:09 +02009919 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 if (bufvarname != NULL)
9921 {
9922 curbuf = buf;
9923 STRCPY(bufvarname, "b:");
9924 STRCPY(bufvarname + 2, varname);
9925 set_var(bufvarname, varp, TRUE);
9926 vim_free(bufvarname);
9927 curbuf = save_curbuf;
9928 }
9929 }
9930 }
9931}
9932
9933 static void
9934f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9935{
9936 dict_T *d;
9937 dictitem_T *di;
9938 char_u *csearch;
9939
9940 if (argvars[0].v_type != VAR_DICT)
9941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009942 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 return;
9944 }
9945
9946 if ((d = argvars[0].vval.v_dict) != NULL)
9947 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01009948 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 if (csearch != NULL)
9950 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 if (enc_utf8)
9952 {
9953 int pcc[MAX_MCO];
9954 int c = utfc_ptr2char(csearch, pcc);
9955
9956 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9957 }
9958 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 set_last_csearch(PTR2CHAR(csearch),
9960 csearch, MB_PTR2LEN(csearch));
9961 }
9962
9963 di = dict_find(d, (char_u *)"forward", -1);
9964 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009965 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 ? FORWARD : BACKWARD);
9967
9968 di = dict_find(d, (char_u *)"until", -1);
9969 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009970 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009971 }
9972}
9973
9974/*
9975 * "setcmdpos()" function
9976 */
9977 static void
9978f_setcmdpos(typval_T *argvars, typval_T *rettv)
9979{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009980 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981
9982 if (pos >= 0)
9983 rettv->vval.v_number = set_cmdline_pos(pos);
9984}
9985
9986/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02009987 * "setenv()" function
9988 */
9989 static void
9990f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
9991{
9992 char_u namebuf[NUMBUFLEN];
9993 char_u valbuf[NUMBUFLEN];
9994 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
9995
9996 if (argvars[1].v_type == VAR_SPECIAL
9997 && argvars[1].vval.v_number == VVAL_NULL)
9998 vim_unsetenv(name);
9999 else
10000 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
10001}
10002
10003/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 * "setfperm({fname}, {mode})" function
10005 */
10006 static void
10007f_setfperm(typval_T *argvars, typval_T *rettv)
10008{
10009 char_u *fname;
10010 char_u modebuf[NUMBUFLEN];
10011 char_u *mode_str;
10012 int i;
10013 int mask;
10014 int mode = 0;
10015
10016 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010017 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010018 if (fname == NULL)
10019 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010020 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 if (mode_str == NULL)
10022 return;
10023 if (STRLEN(mode_str) != 9)
10024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010025 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010026 return;
10027 }
10028
10029 mask = 1;
10030 for (i = 8; i >= 0; --i)
10031 {
10032 if (mode_str[i] != '-')
10033 mode |= mask;
10034 mask = mask << 1;
10035 }
10036 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10037}
10038
10039/*
10040 * "setline()" function
10041 */
10042 static void
10043f_setline(typval_T *argvars, typval_T *rettv)
10044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010045 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010046
Bram Moolenaarca851592018-06-06 21:04:07 +020010047 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010048}
10049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050/*
10051 * Used by "setqflist()" and "setloclist()" functions
10052 */
10053 static void
10054set_qf_ll_list(
10055 win_T *wp UNUSED,
10056 typval_T *list_arg UNUSED,
10057 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010058 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010059 typval_T *rettv)
10060{
10061#ifdef FEAT_QUICKFIX
10062 static char *e_invact = N_("E927: Invalid action: '%s'");
10063 char_u *act;
10064 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010065 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066#endif
10067
10068 rettv->vval.v_number = -1;
10069
10070#ifdef FEAT_QUICKFIX
10071 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010072 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010073 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010074 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010075 else
10076 {
10077 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010078 dict_T *d = NULL;
10079 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080
10081 if (action_arg->v_type == VAR_STRING)
10082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010083 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 if (act == NULL)
10085 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010086 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10087 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 action = *act;
10089 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010090 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010091 }
10092 else if (action_arg->v_type == VAR_UNKNOWN)
10093 action = ' ';
10094 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010095 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010096
Bram Moolenaard823fa92016-08-12 16:29:27 +020010097 if (action_arg->v_type != VAR_UNKNOWN
10098 && what_arg->v_type != VAR_UNKNOWN)
10099 {
10100 if (what_arg->v_type == VAR_DICT)
10101 d = what_arg->vval.v_dict;
10102 else
10103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010104 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010105 valid_dict = FALSE;
10106 }
10107 }
10108
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010109 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010110 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010111 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10112 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010113 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010114 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115 }
10116#endif
10117}
10118
10119/*
10120 * "setloclist()" function
10121 */
10122 static void
10123f_setloclist(typval_T *argvars, typval_T *rettv)
10124{
10125 win_T *win;
10126
10127 rettv->vval.v_number = -1;
10128
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010129 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010130 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010131 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132}
10133
10134/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010135 * "setpos()" function
10136 */
10137 static void
10138f_setpos(typval_T *argvars, typval_T *rettv)
10139{
10140 pos_T pos;
10141 int fnum;
10142 char_u *name;
10143 colnr_T curswant = -1;
10144
10145 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010146 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010147 if (name != NULL)
10148 {
10149 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10150 {
10151 if (--pos.col < 0)
10152 pos.col = 0;
10153 if (name[0] == '.' && name[1] == NUL)
10154 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010155 /* set cursor; "fnum" is ignored */
10156 curwin->w_cursor = pos;
10157 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010159 curwin->w_curswant = curswant - 1;
10160 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010162 check_cursor();
10163 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 }
10165 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10166 {
10167 /* set mark */
10168 if (setmark_pos(name[1], &pos, fnum) == OK)
10169 rettv->vval.v_number = 0;
10170 }
10171 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010172 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 }
10174 }
10175}
10176
10177/*
10178 * "setqflist()" function
10179 */
10180 static void
10181f_setqflist(typval_T *argvars, typval_T *rettv)
10182{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010183 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184}
10185
10186/*
10187 * "setreg()" function
10188 */
10189 static void
10190f_setreg(typval_T *argvars, typval_T *rettv)
10191{
10192 int regname;
10193 char_u *strregname;
10194 char_u *stropt;
10195 char_u *strval;
10196 int append;
10197 char_u yank_type;
10198 long block_len;
10199
10200 block_len = -1;
10201 yank_type = MAUTO;
10202 append = FALSE;
10203
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010204 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 rettv->vval.v_number = 1; /* FAIL is default */
10206
10207 if (strregname == NULL)
10208 return; /* type error; errmsg already given */
10209 regname = *strregname;
10210 if (regname == 0 || regname == '@')
10211 regname = '"';
10212
10213 if (argvars[2].v_type != VAR_UNKNOWN)
10214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010215 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010216 if (stropt == NULL)
10217 return; /* type error */
10218 for (; *stropt != NUL; ++stropt)
10219 switch (*stropt)
10220 {
10221 case 'a': case 'A': /* append */
10222 append = TRUE;
10223 break;
10224 case 'v': case 'c': /* character-wise selection */
10225 yank_type = MCHAR;
10226 break;
10227 case 'V': case 'l': /* line-wise selection */
10228 yank_type = MLINE;
10229 break;
10230 case 'b': case Ctrl_V: /* block-wise selection */
10231 yank_type = MBLOCK;
10232 if (VIM_ISDIGIT(stropt[1]))
10233 {
10234 ++stropt;
10235 block_len = getdigits(&stropt) - 1;
10236 --stropt;
10237 }
10238 break;
10239 }
10240 }
10241
10242 if (argvars[1].v_type == VAR_LIST)
10243 {
10244 char_u **lstval;
10245 char_u **allocval;
10246 char_u buf[NUMBUFLEN];
10247 char_u **curval;
10248 char_u **curallocval;
10249 list_T *ll = argvars[1].vval.v_list;
10250 listitem_T *li;
10251 int len;
10252
10253 /* If the list is NULL handle like an empty list. */
10254 len = ll == NULL ? 0 : ll->lv_len;
10255
10256 /* First half: use for pointers to result lines; second half: use for
10257 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010258 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010259 if (lstval == NULL)
10260 return;
10261 curval = lstval;
10262 allocval = lstval + len + 2;
10263 curallocval = allocval;
10264
10265 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10266 li = li->li_next)
10267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010268 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269 if (strval == NULL)
10270 goto free_lstval;
10271 if (strval == buf)
10272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010273 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010274 * overwrite the string. */
10275 strval = vim_strsave(buf);
10276 if (strval == NULL)
10277 goto free_lstval;
10278 *curallocval++ = strval;
10279 }
10280 *curval++ = strval;
10281 }
10282 *curval++ = NULL;
10283
10284 write_reg_contents_lst(regname, lstval, -1,
10285 append, yank_type, block_len);
10286free_lstval:
10287 while (curallocval > allocval)
10288 vim_free(*--curallocval);
10289 vim_free(lstval);
10290 }
10291 else
10292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010293 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010294 if (strval == NULL)
10295 return;
10296 write_reg_contents_ex(regname, strval, -1,
10297 append, yank_type, block_len);
10298 }
10299 rettv->vval.v_number = 0;
10300}
10301
10302/*
10303 * "settabvar()" function
10304 */
10305 static void
10306f_settabvar(typval_T *argvars, typval_T *rettv)
10307{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010308 tabpage_T *save_curtab;
10309 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010310 char_u *varname, *tabvarname;
10311 typval_T *varp;
10312
10313 rettv->vval.v_number = 0;
10314
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010315 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010316 return;
10317
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010318 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10319 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010320 varp = &argvars[2];
10321
Bram Moolenaar4033c552017-09-16 20:54:51 +020010322 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010324 save_curtab = curtab;
10325 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010326
Bram Moolenaar964b3742019-05-24 18:54:09 +020010327 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 if (tabvarname != NULL)
10329 {
10330 STRCPY(tabvarname, "t:");
10331 STRCPY(tabvarname + 2, varname);
10332 set_var(tabvarname, varp, TRUE);
10333 vim_free(tabvarname);
10334 }
10335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010336 /* Restore current tabpage */
10337 if (valid_tabpage(save_curtab))
10338 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 }
10340}
10341
10342/*
10343 * "settabwinvar()" function
10344 */
10345 static void
10346f_settabwinvar(typval_T *argvars, typval_T *rettv)
10347{
10348 setwinvar(argvars, rettv, 1);
10349}
10350
10351/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010352 * "settagstack()" function
10353 */
10354 static void
10355f_settagstack(typval_T *argvars, typval_T *rettv)
10356{
10357 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10358 win_T *wp;
10359 dict_T *d;
10360 int action = 'r';
10361
10362 rettv->vval.v_number = -1;
10363
10364 // first argument: window number or id
10365 wp = find_win_by_nr_or_id(&argvars[0]);
10366 if (wp == NULL)
10367 return;
10368
10369 // second argument: dict with items to set in the tag stack
10370 if (argvars[1].v_type != VAR_DICT)
10371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010372 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010373 return;
10374 }
10375 d = argvars[1].vval.v_dict;
10376 if (d == NULL)
10377 return;
10378
10379 // third argument: action - 'a' for append and 'r' for replace.
10380 // default is to replace the stack.
10381 if (argvars[2].v_type == VAR_UNKNOWN)
10382 action = 'r';
10383 else if (argvars[2].v_type == VAR_STRING)
10384 {
10385 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010386 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010387 if (actstr == NULL)
10388 return;
10389 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10390 action = *actstr;
10391 else
10392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010393 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010394 return;
10395 }
10396 }
10397 else
10398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010399 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010400 return;
10401 }
10402
10403 if (set_tagstack(wp, d, action) == OK)
10404 rettv->vval.v_number = 0;
10405}
10406
10407/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 * "setwinvar()" function
10409 */
10410 static void
10411f_setwinvar(typval_T *argvars, typval_T *rettv)
10412{
10413 setwinvar(argvars, rettv, 0);
10414}
10415
10416#ifdef FEAT_CRYPT
10417/*
10418 * "sha256({string})" function
10419 */
10420 static void
10421f_sha256(typval_T *argvars, typval_T *rettv)
10422{
10423 char_u *p;
10424
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010425 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010426 rettv->vval.v_string = vim_strsave(
10427 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10428 rettv->v_type = VAR_STRING;
10429}
10430#endif /* FEAT_CRYPT */
10431
10432/*
10433 * "shellescape({string})" function
10434 */
10435 static void
10436f_shellescape(typval_T *argvars, typval_T *rettv)
10437{
Bram Moolenaar20615522017-06-05 18:46:26 +020010438 int do_special = non_zero_arg(&argvars[1]);
10439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010440 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010441 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010442 rettv->v_type = VAR_STRING;
10443}
10444
10445/*
10446 * shiftwidth() function
10447 */
10448 static void
10449f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10450{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010451 rettv->vval.v_number = 0;
10452
10453 if (argvars[0].v_type != VAR_UNKNOWN)
10454 {
10455 long col;
10456
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010457 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010458 if (col < 0)
10459 return; // type error; errmsg already given
10460#ifdef FEAT_VARTABS
10461 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10462 return;
10463#endif
10464 }
10465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010466 rettv->vval.v_number = get_sw_value(curbuf);
10467}
10468
10469/*
10470 * "simplify()" function
10471 */
10472 static void
10473f_simplify(typval_T *argvars, typval_T *rettv)
10474{
10475 char_u *p;
10476
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010477 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010478 rettv->vval.v_string = vim_strsave(p);
10479 simplify_filename(rettv->vval.v_string); /* simplify in place */
10480 rettv->v_type = VAR_STRING;
10481}
10482
10483#ifdef FEAT_FLOAT
10484/*
10485 * "sin()" function
10486 */
10487 static void
10488f_sin(typval_T *argvars, typval_T *rettv)
10489{
10490 float_T f = 0.0;
10491
10492 rettv->v_type = VAR_FLOAT;
10493 if (get_float_arg(argvars, &f) == OK)
10494 rettv->vval.v_float = sin(f);
10495 else
10496 rettv->vval.v_float = 0.0;
10497}
10498
10499/*
10500 * "sinh()" function
10501 */
10502 static void
10503f_sinh(typval_T *argvars, typval_T *rettv)
10504{
10505 float_T f = 0.0;
10506
10507 rettv->v_type = VAR_FLOAT;
10508 if (get_float_arg(argvars, &f) == OK)
10509 rettv->vval.v_float = sinh(f);
10510 else
10511 rettv->vval.v_float = 0.0;
10512}
10513#endif
10514
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010515/*
10516 * "soundfold({word})" function
10517 */
10518 static void
10519f_soundfold(typval_T *argvars, typval_T *rettv)
10520{
10521 char_u *s;
10522
10523 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010524 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525#ifdef FEAT_SPELL
10526 rettv->vval.v_string = eval_soundfold(s);
10527#else
10528 rettv->vval.v_string = vim_strsave(s);
10529#endif
10530}
10531
10532/*
10533 * "spellbadword()" function
10534 */
10535 static void
10536f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10537{
10538 char_u *word = (char_u *)"";
10539 hlf_T attr = HLF_COUNT;
10540 int len = 0;
10541
10542 if (rettv_list_alloc(rettv) == FAIL)
10543 return;
10544
10545#ifdef FEAT_SPELL
10546 if (argvars[0].v_type == VAR_UNKNOWN)
10547 {
10548 /* Find the start and length of the badly spelled word. */
10549 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10550 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010551 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010553 curwin->w_set_curswant = TRUE;
10554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010555 }
10556 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010558 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 int capcol = -1;
10560
10561 if (str != NULL)
10562 {
10563 /* Check the argument for spelling. */
10564 while (*str != NUL)
10565 {
10566 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10567 if (attr != HLF_COUNT)
10568 {
10569 word = str;
10570 break;
10571 }
10572 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010573 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +020010574 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010575 }
10576 }
10577 }
10578#endif
10579
10580 list_append_string(rettv->vval.v_list, word, len);
10581 list_append_string(rettv->vval.v_list, (char_u *)(
10582 attr == HLF_SPB ? "bad" :
10583 attr == HLF_SPR ? "rare" :
10584 attr == HLF_SPL ? "local" :
10585 attr == HLF_SPC ? "caps" :
10586 ""), -1);
10587}
10588
10589/*
10590 * "spellsuggest()" function
10591 */
10592 static void
10593f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10594{
10595#ifdef FEAT_SPELL
10596 char_u *str;
10597 int typeerr = FALSE;
10598 int maxcount;
10599 garray_T ga;
10600 int i;
10601 listitem_T *li;
10602 int need_capital = FALSE;
10603#endif
10604
10605 if (rettv_list_alloc(rettv) == FAIL)
10606 return;
10607
10608#ifdef FEAT_SPELL
10609 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10610 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010611 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010612 if (argvars[1].v_type != VAR_UNKNOWN)
10613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010614 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 if (maxcount <= 0)
10616 return;
10617 if (argvars[2].v_type != VAR_UNKNOWN)
10618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010619 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010620 if (typeerr)
10621 return;
10622 }
10623 }
10624 else
10625 maxcount = 25;
10626
10627 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10628
10629 for (i = 0; i < ga.ga_len; ++i)
10630 {
10631 str = ((char_u **)ga.ga_data)[i];
10632
10633 li = listitem_alloc();
10634 if (li == NULL)
10635 vim_free(str);
10636 else
10637 {
10638 li->li_tv.v_type = VAR_STRING;
10639 li->li_tv.v_lock = 0;
10640 li->li_tv.vval.v_string = str;
10641 list_append(rettv->vval.v_list, li);
10642 }
10643 }
10644 ga_clear(&ga);
10645 }
10646#endif
10647}
10648
10649 static void
10650f_split(typval_T *argvars, typval_T *rettv)
10651{
10652 char_u *str;
10653 char_u *end;
10654 char_u *pat = NULL;
10655 regmatch_T regmatch;
10656 char_u patbuf[NUMBUFLEN];
10657 char_u *save_cpo;
10658 int match;
10659 colnr_T col = 0;
10660 int keepempty = FALSE;
10661 int typeerr = FALSE;
10662
10663 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10664 save_cpo = p_cpo;
10665 p_cpo = (char_u *)"";
10666
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010667 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668 if (argvars[1].v_type != VAR_UNKNOWN)
10669 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010670 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010671 if (pat == NULL)
10672 typeerr = TRUE;
10673 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010674 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 }
10676 if (pat == NULL || *pat == NUL)
10677 pat = (char_u *)"[\\x01- ]\\+";
10678
10679 if (rettv_list_alloc(rettv) == FAIL)
10680 return;
10681 if (typeerr)
10682 return;
10683
10684 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10685 if (regmatch.regprog != NULL)
10686 {
10687 regmatch.rm_ic = FALSE;
10688 while (*str != NUL || keepempty)
10689 {
10690 if (*str == NUL)
10691 match = FALSE; /* empty item at the end */
10692 else
10693 match = vim_regexec_nl(&regmatch, str, col);
10694 if (match)
10695 end = regmatch.startp[0];
10696 else
10697 end = str + STRLEN(str);
10698 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10699 && *str != NUL && match && end < regmatch.endp[0]))
10700 {
10701 if (list_append_string(rettv->vval.v_list, str,
10702 (int)(end - str)) == FAIL)
10703 break;
10704 }
10705 if (!match)
10706 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010010707 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 if (regmatch.endp[0] > str)
10709 col = 0;
10710 else
Bram Moolenaar13505972019-01-24 15:04:48 +010010711 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010712 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010713 str = regmatch.endp[0];
10714 }
10715
10716 vim_regfree(regmatch.regprog);
10717 }
10718
10719 p_cpo = save_cpo;
10720}
10721
10722#ifdef FEAT_FLOAT
10723/*
10724 * "sqrt()" function
10725 */
10726 static void
10727f_sqrt(typval_T *argvars, typval_T *rettv)
10728{
10729 float_T f = 0.0;
10730
10731 rettv->v_type = VAR_FLOAT;
10732 if (get_float_arg(argvars, &f) == OK)
10733 rettv->vval.v_float = sqrt(f);
10734 else
10735 rettv->vval.v_float = 0.0;
10736}
10737
10738/*
10739 * "str2float()" function
10740 */
10741 static void
10742f_str2float(typval_T *argvars, typval_T *rettv)
10743{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010744 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010745 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010746
Bram Moolenaar08243d22017-01-10 16:12:29 +010010747 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 p = skipwhite(p + 1);
10749 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010010750 if (isneg)
10751 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 rettv->v_type = VAR_FLOAT;
10753}
10754#endif
10755
10756/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020010757 * "str2list()" function
10758 */
10759 static void
10760f_str2list(typval_T *argvars, typval_T *rettv)
10761{
10762 char_u *p;
10763 int utf8 = FALSE;
10764
10765 if (rettv_list_alloc(rettv) == FAIL)
10766 return;
10767
10768 if (argvars[1].v_type != VAR_UNKNOWN)
10769 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
10770
10771 p = tv_get_string(&argvars[0]);
10772
10773 if (has_mbyte || utf8)
10774 {
10775 int (*ptr2len)(char_u *);
10776 int (*ptr2char)(char_u *);
10777
10778 if (utf8 || enc_utf8)
10779 {
10780 ptr2len = utf_ptr2len;
10781 ptr2char = utf_ptr2char;
10782 }
10783 else
10784 {
10785 ptr2len = mb_ptr2len;
10786 ptr2char = mb_ptr2char;
10787 }
10788
10789 for ( ; *p != NUL; p += (*ptr2len)(p))
10790 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
10791 }
10792 else
10793 for ( ; *p != NUL; ++p)
10794 list_append_number(rettv->vval.v_list, *p);
10795}
10796
10797/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 * "str2nr()" function
10799 */
10800 static void
10801f_str2nr(typval_T *argvars, typval_T *rettv)
10802{
10803 int base = 10;
10804 char_u *p;
10805 varnumber_T n;
10806 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010010807 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010808
10809 if (argvars[1].v_type != VAR_UNKNOWN)
10810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010811 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 if (base != 2 && base != 8 && base != 10 && base != 16)
10813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010814 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 return;
10816 }
10817 }
10818
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010819 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010820 isneg = (*p == '-');
10821 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010822 p = skipwhite(p + 1);
10823 switch (base)
10824 {
10825 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
10826 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
10827 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
10828 default: what = 0;
10829 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020010830 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
10831 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010010832 if (isneg)
10833 rettv->vval.v_number = -n;
10834 else
10835 rettv->vval.v_number = n;
10836
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837}
10838
10839#ifdef HAVE_STRFTIME
10840/*
10841 * "strftime({format}[, {time}])" function
10842 */
10843 static void
10844f_strftime(typval_T *argvars, typval_T *rettv)
10845{
10846 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020010847 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 struct tm *curtime;
10849 time_t seconds;
10850 char_u *p;
10851
10852 rettv->v_type = VAR_STRING;
10853
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010854 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 if (argvars[1].v_type == VAR_UNKNOWN)
10856 seconds = time(NULL);
10857 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010858 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020010859 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 /* MSVC returns NULL for an invalid value of seconds. */
10861 if (curtime == NULL)
10862 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
10863 else
10864 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010865 vimconv_T conv;
10866 char_u *enc;
10867
10868 conv.vc_type = CONV_NONE;
10869 enc = enc_locale();
10870 convert_setup(&conv, p_enc, enc);
10871 if (conv.vc_type != CONV_NONE)
10872 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010873 if (p != NULL)
10874 (void)strftime((char *)result_buf, sizeof(result_buf),
10875 (char *)p, curtime);
10876 else
10877 result_buf[0] = NUL;
10878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879 if (conv.vc_type != CONV_NONE)
10880 vim_free(p);
10881 convert_setup(&conv, enc, p_enc);
10882 if (conv.vc_type != CONV_NONE)
10883 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
10884 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 rettv->vval.v_string = vim_strsave(result_buf);
10886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010887 /* Release conversion descriptors */
10888 convert_setup(&conv, NULL, NULL);
10889 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 }
10891}
10892#endif
10893
10894/*
10895 * "strgetchar()" function
10896 */
10897 static void
10898f_strgetchar(typval_T *argvars, typval_T *rettv)
10899{
10900 char_u *str;
10901 int len;
10902 int error = FALSE;
10903 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010010904 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905
10906 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010907 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 if (str == NULL)
10909 return;
10910 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010911 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912 if (error)
10913 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010914
Bram Moolenaar13505972019-01-24 15:04:48 +010010915 while (charidx >= 0 && byteidx < len)
10916 {
10917 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010918 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010919 rettv->vval.v_number = mb_ptr2char(str + byteidx);
10920 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010922 --charidx;
10923 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010925}
10926
10927/*
10928 * "stridx()" function
10929 */
10930 static void
10931f_stridx(typval_T *argvars, typval_T *rettv)
10932{
10933 char_u buf[NUMBUFLEN];
10934 char_u *needle;
10935 char_u *haystack;
10936 char_u *save_haystack;
10937 char_u *pos;
10938 int start_idx;
10939
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010940 needle = tv_get_string_chk(&argvars[1]);
10941 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010942 rettv->vval.v_number = -1;
10943 if (needle == NULL || haystack == NULL)
10944 return; /* type error; errmsg already given */
10945
10946 if (argvars[2].v_type != VAR_UNKNOWN)
10947 {
10948 int error = FALSE;
10949
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010950 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010951 if (error || start_idx >= (int)STRLEN(haystack))
10952 return;
10953 if (start_idx >= 0)
10954 haystack += start_idx;
10955 }
10956
10957 pos = (char_u *)strstr((char *)haystack, (char *)needle);
10958 if (pos != NULL)
10959 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
10960}
10961
10962/*
10963 * "string()" function
10964 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010010965 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010966f_string(typval_T *argvars, typval_T *rettv)
10967{
10968 char_u *tofree;
10969 char_u numbuf[NUMBUFLEN];
10970
10971 rettv->v_type = VAR_STRING;
10972 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
10973 get_copyID());
10974 /* Make a copy if we have a value but it's not in allocated memory. */
10975 if (rettv->vval.v_string != NULL && tofree == NULL)
10976 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
10977}
10978
10979/*
10980 * "strlen()" function
10981 */
10982 static void
10983f_strlen(typval_T *argvars, typval_T *rettv)
10984{
10985 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010986 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987}
10988
10989/*
10990 * "strchars()" function
10991 */
10992 static void
10993f_strchars(typval_T *argvars, typval_T *rettv)
10994{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010995 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997 varnumber_T len = 0;
10998 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999
11000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011001 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011002 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011003 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 else
11005 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11007 while (*s != NUL)
11008 {
11009 func_mb_ptr2char_adv(&s);
11010 ++len;
11011 }
11012 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 }
11014}
11015
11016/*
11017 * "strdisplaywidth()" function
11018 */
11019 static void
11020f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11021{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011022 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 int col = 0;
11024
11025 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011026 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027
11028 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11029}
11030
11031/*
11032 * "strwidth()" function
11033 */
11034 static void
11035f_strwidth(typval_T *argvars, typval_T *rettv)
11036{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011037 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011038
Bram Moolenaar13505972019-01-24 15:04:48 +010011039 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040}
11041
11042/*
11043 * "strcharpart()" function
11044 */
11045 static void
11046f_strcharpart(typval_T *argvars, typval_T *rettv)
11047{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 char_u *p;
11049 int nchar;
11050 int nbyte = 0;
11051 int charlen;
11052 int len = 0;
11053 int slen;
11054 int error = FALSE;
11055
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011056 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 slen = (int)STRLEN(p);
11058
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011059 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 if (!error)
11061 {
11062 if (nchar > 0)
11063 while (nchar > 0 && nbyte < slen)
11064 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011065 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066 --nchar;
11067 }
11068 else
11069 nbyte = nchar;
11070 if (argvars[2].v_type != VAR_UNKNOWN)
11071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011072 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 while (charlen > 0 && nbyte + len < slen)
11074 {
11075 int off = nbyte + len;
11076
11077 if (off < 0)
11078 len += 1;
11079 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011080 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 --charlen;
11082 }
11083 }
11084 else
11085 len = slen - nbyte; /* default: all bytes that are available. */
11086 }
11087
11088 /*
11089 * Only return the overlap between the specified part and the actual
11090 * string.
11091 */
11092 if (nbyte < 0)
11093 {
11094 len += nbyte;
11095 nbyte = 0;
11096 }
11097 else if (nbyte > slen)
11098 nbyte = slen;
11099 if (len < 0)
11100 len = 0;
11101 else if (nbyte + len > slen)
11102 len = slen - nbyte;
11103
11104 rettv->v_type = VAR_STRING;
11105 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106}
11107
11108/*
11109 * "strpart()" function
11110 */
11111 static void
11112f_strpart(typval_T *argvars, typval_T *rettv)
11113{
11114 char_u *p;
11115 int n;
11116 int len;
11117 int slen;
11118 int error = FALSE;
11119
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011120 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 slen = (int)STRLEN(p);
11122
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011123 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 if (error)
11125 len = 0;
11126 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011127 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 else
11129 len = slen - n; /* default len: all bytes that are available. */
11130
11131 /*
11132 * Only return the overlap between the specified part and the actual
11133 * string.
11134 */
11135 if (n < 0)
11136 {
11137 len += n;
11138 n = 0;
11139 }
11140 else if (n > slen)
11141 n = slen;
11142 if (len < 0)
11143 len = 0;
11144 else if (n + len > slen)
11145 len = slen - n;
11146
11147 rettv->v_type = VAR_STRING;
11148 rettv->vval.v_string = vim_strnsave(p + n, len);
11149}
11150
11151/*
11152 * "strridx()" function
11153 */
11154 static void
11155f_strridx(typval_T *argvars, typval_T *rettv)
11156{
11157 char_u buf[NUMBUFLEN];
11158 char_u *needle;
11159 char_u *haystack;
11160 char_u *rest;
11161 char_u *lastmatch = NULL;
11162 int haystack_len, end_idx;
11163
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011164 needle = tv_get_string_chk(&argvars[1]);
11165 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166
11167 rettv->vval.v_number = -1;
11168 if (needle == NULL || haystack == NULL)
11169 return; /* type error; errmsg already given */
11170
11171 haystack_len = (int)STRLEN(haystack);
11172 if (argvars[2].v_type != VAR_UNKNOWN)
11173 {
11174 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011175 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 if (end_idx < 0)
11177 return; /* can never find a match */
11178 }
11179 else
11180 end_idx = haystack_len;
11181
11182 if (*needle == NUL)
11183 {
11184 /* Empty string matches past the end. */
11185 lastmatch = haystack + end_idx;
11186 }
11187 else
11188 {
11189 for (rest = haystack; *rest != '\0'; ++rest)
11190 {
11191 rest = (char_u *)strstr((char *)rest, (char *)needle);
11192 if (rest == NULL || rest > haystack + end_idx)
11193 break;
11194 lastmatch = rest;
11195 }
11196 }
11197
11198 if (lastmatch == NULL)
11199 rettv->vval.v_number = -1;
11200 else
11201 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11202}
11203
11204/*
11205 * "strtrans()" function
11206 */
11207 static void
11208f_strtrans(typval_T *argvars, typval_T *rettv)
11209{
11210 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011211 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011212}
11213
11214/*
11215 * "submatch()" function
11216 */
11217 static void
11218f_submatch(typval_T *argvars, typval_T *rettv)
11219{
11220 int error = FALSE;
11221 int no;
11222 int retList = 0;
11223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011224 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011225 if (error)
11226 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011227 if (no < 0 || no >= NSUBEXP)
11228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011229 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011230 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011231 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011232 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011233 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011234 if (error)
11235 return;
11236
11237 if (retList == 0)
11238 {
11239 rettv->v_type = VAR_STRING;
11240 rettv->vval.v_string = reg_submatch(no);
11241 }
11242 else
11243 {
11244 rettv->v_type = VAR_LIST;
11245 rettv->vval.v_list = reg_submatch_list(no);
11246 }
11247}
11248
11249/*
11250 * "substitute()" function
11251 */
11252 static void
11253f_substitute(typval_T *argvars, typval_T *rettv)
11254{
11255 char_u patbuf[NUMBUFLEN];
11256 char_u subbuf[NUMBUFLEN];
11257 char_u flagsbuf[NUMBUFLEN];
11258
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011259 char_u *str = tv_get_string_chk(&argvars[0]);
11260 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011261 char_u *sub = NULL;
11262 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011263 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011264
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011265 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11266 expr = &argvars[2];
11267 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011268 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011269
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011270 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011271 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11272 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011273 rettv->vval.v_string = NULL;
11274 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011275 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011276}
11277
11278/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011279 * "swapinfo(swap_filename)" function
11280 */
11281 static void
11282f_swapinfo(typval_T *argvars, typval_T *rettv)
11283{
11284 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011285 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011286}
11287
11288/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011289 * "swapname(expr)" function
11290 */
11291 static void
11292f_swapname(typval_T *argvars, typval_T *rettv)
11293{
11294 buf_T *buf;
11295
11296 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011297 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011298 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11299 || buf->b_ml.ml_mfp->mf_fname == NULL)
11300 rettv->vval.v_string = NULL;
11301 else
11302 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11303}
11304
11305/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 * "synID(lnum, col, trans)" function
11307 */
11308 static void
11309f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11310{
11311 int id = 0;
11312#ifdef FEAT_SYN_HL
11313 linenr_T lnum;
11314 colnr_T col;
11315 int trans;
11316 int transerr = FALSE;
11317
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011318 lnum = tv_get_lnum(argvars); /* -1 on type error */
11319 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11320 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321
11322 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11323 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11324 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11325#endif
11326
11327 rettv->vval.v_number = id;
11328}
11329
11330/*
11331 * "synIDattr(id, what [, mode])" function
11332 */
11333 static void
11334f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11335{
11336 char_u *p = NULL;
11337#ifdef FEAT_SYN_HL
11338 int id;
11339 char_u *what;
11340 char_u *mode;
11341 char_u modebuf[NUMBUFLEN];
11342 int modec;
11343
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011344 id = (int)tv_get_number(&argvars[0]);
11345 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011346 if (argvars[2].v_type != VAR_UNKNOWN)
11347 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011348 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011349 modec = TOLOWER_ASC(mode[0]);
11350 if (modec != 't' && modec != 'c' && modec != 'g')
11351 modec = 0; /* replace invalid with current */
11352 }
11353 else
11354 {
11355#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11356 if (USE_24BIT)
11357 modec = 'g';
11358 else
11359#endif
11360 if (t_colors > 1)
11361 modec = 'c';
11362 else
11363 modec = 't';
11364 }
11365
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 switch (TOLOWER_ASC(what[0]))
11367 {
11368 case 'b':
11369 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11370 p = highlight_color(id, what, modec);
11371 else /* bold */
11372 p = highlight_has_attr(id, HL_BOLD, modec);
11373 break;
11374
11375 case 'f': /* fg[#] or font */
11376 p = highlight_color(id, what, modec);
11377 break;
11378
11379 case 'i':
11380 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11381 p = highlight_has_attr(id, HL_INVERSE, modec);
11382 else /* italic */
11383 p = highlight_has_attr(id, HL_ITALIC, modec);
11384 break;
11385
11386 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011387 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011388 break;
11389
11390 case 'r': /* reverse */
11391 p = highlight_has_attr(id, HL_INVERSE, modec);
11392 break;
11393
11394 case 's':
11395 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11396 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011397 /* strikeout */
11398 else if (TOLOWER_ASC(what[1]) == 't' &&
11399 TOLOWER_ASC(what[2]) == 'r')
11400 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 else /* standout */
11402 p = highlight_has_attr(id, HL_STANDOUT, modec);
11403 break;
11404
11405 case 'u':
11406 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11407 /* underline */
11408 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11409 else
11410 /* undercurl */
11411 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11412 break;
11413 }
11414
11415 if (p != NULL)
11416 p = vim_strsave(p);
11417#endif
11418 rettv->v_type = VAR_STRING;
11419 rettv->vval.v_string = p;
11420}
11421
11422/*
11423 * "synIDtrans(id)" function
11424 */
11425 static void
11426f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11427{
11428 int id;
11429
11430#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011431 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432
11433 if (id > 0)
11434 id = syn_get_final_id(id);
11435 else
11436#endif
11437 id = 0;
11438
11439 rettv->vval.v_number = id;
11440}
11441
11442/*
11443 * "synconcealed(lnum, col)" function
11444 */
11445 static void
11446f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11447{
11448#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11449 linenr_T lnum;
11450 colnr_T col;
11451 int syntax_flags = 0;
11452 int cchar;
11453 int matchid = 0;
11454 char_u str[NUMBUFLEN];
11455#endif
11456
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011457 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458
11459#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011460 lnum = tv_get_lnum(argvars); /* -1 on type error */
11461 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011462
11463 vim_memset(str, NUL, sizeof(str));
11464
11465 if (rettv_list_alloc(rettv) != FAIL)
11466 {
11467 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11468 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11469 && curwin->w_p_cole > 0)
11470 {
11471 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11472 syntax_flags = get_syntax_info(&matchid);
11473
11474 /* get the conceal character */
11475 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11476 {
11477 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011478 if (cchar == NUL && curwin->w_p_cole == 1)
11479 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011480 if (cchar != NUL)
11481 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011482 if (has_mbyte)
11483 (*mb_char2bytes)(cchar, str);
11484 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011485 str[0] = cchar;
11486 }
11487 }
11488 }
11489
11490 list_append_number(rettv->vval.v_list,
11491 (syntax_flags & HL_CONCEAL) != 0);
11492 /* -1 to auto-determine strlen */
11493 list_append_string(rettv->vval.v_list, str, -1);
11494 list_append_number(rettv->vval.v_list, matchid);
11495 }
11496#endif
11497}
11498
11499/*
11500 * "synstack(lnum, col)" function
11501 */
11502 static void
11503f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11504{
11505#ifdef FEAT_SYN_HL
11506 linenr_T lnum;
11507 colnr_T col;
11508 int i;
11509 int id;
11510#endif
11511
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011512 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513
11514#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011515 lnum = tv_get_lnum(argvars); /* -1 on type error */
11516 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011517
11518 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11519 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11520 && rettv_list_alloc(rettv) != FAIL)
11521 {
11522 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11523 for (i = 0; ; ++i)
11524 {
11525 id = syn_get_stack_item(i);
11526 if (id < 0)
11527 break;
11528 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11529 break;
11530 }
11531 }
11532#endif
11533}
11534
11535 static void
11536get_cmd_output_as_rettv(
11537 typval_T *argvars,
11538 typval_T *rettv,
11539 int retlist)
11540{
11541 char_u *res = NULL;
11542 char_u *p;
11543 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544 int err = FALSE;
11545 FILE *fd;
11546 list_T *list = NULL;
11547 int flags = SHELL_SILENT;
11548
11549 rettv->v_type = VAR_STRING;
11550 rettv->vval.v_string = NULL;
11551 if (check_restricted() || check_secure())
11552 goto errret;
11553
11554 if (argvars[1].v_type != VAR_UNKNOWN)
11555 {
11556 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011557 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558 * command.
11559 */
11560 if ((infile = vim_tempname('i', TRUE)) == NULL)
11561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011562 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011563 goto errret;
11564 }
11565
11566 fd = mch_fopen((char *)infile, WRITEBIN);
11567 if (fd == NULL)
11568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011569 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 goto errret;
11571 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011572 if (argvars[1].v_type == VAR_NUMBER)
11573 {
11574 linenr_T lnum;
11575 buf_T *buf;
11576
11577 buf = buflist_findnr(argvars[1].vval.v_number);
11578 if (buf == NULL)
11579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011580 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011581 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011582 goto errret;
11583 }
11584
11585 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11586 {
11587 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11588 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11589 {
11590 err = TRUE;
11591 break;
11592 }
11593 if (putc(NL, fd) == EOF)
11594 {
11595 err = TRUE;
11596 break;
11597 }
11598 }
11599 }
11600 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011601 {
11602 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11603 err = TRUE;
11604 }
11605 else
11606 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011607 size_t len;
11608 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011609
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011610 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011611 if (p == NULL)
11612 {
11613 fclose(fd);
11614 goto errret; /* type error; errmsg already given */
11615 }
11616 len = STRLEN(p);
11617 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11618 err = TRUE;
11619 }
11620 if (fclose(fd) != 0)
11621 err = TRUE;
11622 if (err)
11623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011624 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011625 goto errret;
11626 }
11627 }
11628
11629 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11630 * echoes typeahead, that messes up the display. */
11631 if (!msg_silent)
11632 flags += SHELL_COOKED;
11633
11634 if (retlist)
11635 {
11636 int len;
11637 listitem_T *li;
11638 char_u *s = NULL;
11639 char_u *start;
11640 char_u *end;
11641 int i;
11642
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011643 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 if (res == NULL)
11645 goto errret;
11646
11647 list = list_alloc();
11648 if (list == NULL)
11649 goto errret;
11650
11651 for (i = 0; i < len; ++i)
11652 {
11653 start = res + i;
11654 while (i < len && res[i] != NL)
11655 ++i;
11656 end = res + i;
11657
Bram Moolenaar964b3742019-05-24 18:54:09 +020011658 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011659 if (s == NULL)
11660 goto errret;
11661
11662 for (p = s; start < end; ++p, ++start)
11663 *p = *start == NUL ? NL : *start;
11664 *p = NUL;
11665
11666 li = listitem_alloc();
11667 if (li == NULL)
11668 {
11669 vim_free(s);
11670 goto errret;
11671 }
11672 li->li_tv.v_type = VAR_STRING;
11673 li->li_tv.v_lock = 0;
11674 li->li_tv.vval.v_string = s;
11675 list_append(list, li);
11676 }
11677
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011678 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 list = NULL;
11680 }
11681 else
11682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011683 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011684#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011685 /* translate <CR><NL> into <NL> */
11686 if (res != NULL)
11687 {
11688 char_u *s, *d;
11689
11690 d = res;
11691 for (s = res; *s; ++s)
11692 {
11693 if (s[0] == CAR && s[1] == NL)
11694 ++s;
11695 *d++ = *s;
11696 }
11697 *d = NUL;
11698 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011699#endif
11700 rettv->vval.v_string = res;
11701 res = NULL;
11702 }
11703
11704errret:
11705 if (infile != NULL)
11706 {
11707 mch_remove(infile);
11708 vim_free(infile);
11709 }
11710 if (res != NULL)
11711 vim_free(res);
11712 if (list != NULL)
11713 list_free(list);
11714}
11715
11716/*
11717 * "system()" function
11718 */
11719 static void
11720f_system(typval_T *argvars, typval_T *rettv)
11721{
11722 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11723}
11724
11725/*
11726 * "systemlist()" function
11727 */
11728 static void
11729f_systemlist(typval_T *argvars, typval_T *rettv)
11730{
11731 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11732}
11733
11734/*
11735 * "tabpagebuflist()" function
11736 */
11737 static void
11738f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11739{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011740 tabpage_T *tp;
11741 win_T *wp = NULL;
11742
11743 if (argvars[0].v_type == VAR_UNKNOWN)
11744 wp = firstwin;
11745 else
11746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011748 if (tp != NULL)
11749 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11750 }
11751 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11752 {
11753 for (; wp != NULL; wp = wp->w_next)
11754 if (list_append_number(rettv->vval.v_list,
11755 wp->w_buffer->b_fnum) == FAIL)
11756 break;
11757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011758}
11759
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011760/*
11761 * "tabpagenr()" function
11762 */
11763 static void
11764f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
11765{
11766 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 char_u *arg;
11768
11769 if (argvars[0].v_type != VAR_UNKNOWN)
11770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011771 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 nr = 0;
11773 if (arg != NULL)
11774 {
11775 if (STRCMP(arg, "$") == 0)
11776 nr = tabpage_index(NULL) - 1;
11777 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011778 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011779 }
11780 }
11781 else
11782 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011783 rettv->vval.v_number = nr;
11784}
11785
11786
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011787/*
11788 * Common code for tabpagewinnr() and winnr().
11789 */
11790 static int
11791get_winnr(tabpage_T *tp, typval_T *argvar)
11792{
11793 win_T *twin;
11794 int nr = 1;
11795 win_T *wp;
11796 char_u *arg;
11797
11798 twin = (tp == curtab) ? curwin : tp->tp_curwin;
11799 if (argvar->v_type != VAR_UNKNOWN)
11800 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011801 int invalid_arg = FALSE;
11802
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011803 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011804 if (arg == NULL)
11805 nr = 0; /* type error; errmsg already given */
11806 else if (STRCMP(arg, "$") == 0)
11807 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
11808 else if (STRCMP(arg, "#") == 0)
11809 {
11810 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
11811 if (twin == NULL)
11812 nr = 0;
11813 }
11814 else
11815 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011816 long count;
11817 char_u *endp;
11818
11819 // Extract the window count (if specified). e.g. winnr('3j')
11820 count = strtol((char *)arg, (char **)&endp, 10);
11821 if (count <= 0)
11822 count = 1; // if count is not specified, default to 1
11823 if (endp != NULL && *endp != '\0')
11824 {
11825 if (STRCMP(endp, "j") == 0)
11826 twin = win_vert_neighbor(tp, twin, FALSE, count);
11827 else if (STRCMP(endp, "k") == 0)
11828 twin = win_vert_neighbor(tp, twin, TRUE, count);
11829 else if (STRCMP(endp, "h") == 0)
11830 twin = win_horz_neighbor(tp, twin, TRUE, count);
11831 else if (STRCMP(endp, "l") == 0)
11832 twin = win_horz_neighbor(tp, twin, FALSE, count);
11833 else
11834 invalid_arg = TRUE;
11835 }
11836 else
11837 invalid_arg = TRUE;
11838 }
11839
11840 if (invalid_arg)
11841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011842 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843 nr = 0;
11844 }
11845 }
11846
11847 if (nr > 0)
11848 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11849 wp != twin; wp = wp->w_next)
11850 {
11851 if (wp == NULL)
11852 {
11853 /* didn't find it in this tabpage */
11854 nr = 0;
11855 break;
11856 }
11857 ++nr;
11858 }
11859 return nr;
11860}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861
11862/*
11863 * "tabpagewinnr()" function
11864 */
11865 static void
11866f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
11867{
11868 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 tabpage_T *tp;
11870
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011871 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011872 if (tp == NULL)
11873 nr = 0;
11874 else
11875 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011876 rettv->vval.v_number = nr;
11877}
11878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879/*
11880 * "tagfiles()" function
11881 */
11882 static void
11883f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
11884{
11885 char_u *fname;
11886 tagname_T tn;
11887 int first;
11888
11889 if (rettv_list_alloc(rettv) == FAIL)
11890 return;
11891 fname = alloc(MAXPATHL);
11892 if (fname == NULL)
11893 return;
11894
11895 for (first = TRUE; ; first = FALSE)
11896 if (get_tagfname(&tn, first, fname) == FAIL
11897 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
11898 break;
11899 tagname_free(&tn);
11900 vim_free(fname);
11901}
11902
11903/*
11904 * "taglist()" function
11905 */
11906 static void
11907f_taglist(typval_T *argvars, typval_T *rettv)
11908{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011909 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011910 char_u *tag_pattern;
11911
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011912 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011913
11914 rettv->vval.v_number = FALSE;
11915 if (*tag_pattern == NUL)
11916 return;
11917
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011918 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011919 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011921 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011922}
11923
11924/*
11925 * "tempname()" function
11926 */
11927 static void
11928f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
11929{
11930 static int x = 'A';
11931
11932 rettv->v_type = VAR_STRING;
11933 rettv->vval.v_string = vim_tempname(x, FALSE);
11934
11935 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11936 * names. Skip 'I' and 'O', they are used for shell redirection. */
11937 do
11938 {
11939 if (x == 'Z')
11940 x = '0';
11941 else if (x == '9')
11942 x = 'A';
11943 else
11944 {
11945#ifdef EBCDIC
11946 if (x == 'I')
11947 x = 'J';
11948 else if (x == 'R')
11949 x = 'S';
11950 else
11951#endif
11952 ++x;
11953 }
11954 } while (x == 'I' || x == 'O');
11955}
11956
11957#ifdef FEAT_FLOAT
11958/*
11959 * "tan()" function
11960 */
11961 static void
11962f_tan(typval_T *argvars, typval_T *rettv)
11963{
11964 float_T f = 0.0;
11965
11966 rettv->v_type = VAR_FLOAT;
11967 if (get_float_arg(argvars, &f) == OK)
11968 rettv->vval.v_float = tan(f);
11969 else
11970 rettv->vval.v_float = 0.0;
11971}
11972
11973/*
11974 * "tanh()" function
11975 */
11976 static void
11977f_tanh(typval_T *argvars, typval_T *rettv)
11978{
11979 float_T f = 0.0;
11980
11981 rettv->v_type = VAR_FLOAT;
11982 if (get_float_arg(argvars, &f) == OK)
11983 rettv->vval.v_float = tanh(f);
11984 else
11985 rettv->vval.v_float = 0.0;
11986}
11987#endif
11988
11989/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011990 * Get a callback from "arg". It can be a Funcref or a function name.
11991 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011992 * "cb_name" is not allocated.
11993 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011994 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011995 callback_T
11996get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011997{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011998 callback_T res;
11999
12000 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012001 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12002 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012003 res.cb_partial = arg->vval.v_partial;
12004 ++res.cb_partial->pt_refcount;
12005 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012006 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012007 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012008 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012009 res.cb_partial = NULL;
12010 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
12011 {
12012 // Note that we don't make a copy of the string.
12013 res.cb_name = arg->vval.v_string;
12014 func_ref(res.cb_name);
12015 }
12016 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12017 {
12018 res.cb_name = (char_u *)"";
12019 }
12020 else
12021 {
12022 emsg(_("E921: Invalid callback argument"));
12023 res.cb_name = NULL;
12024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012025 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012026 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012027}
12028
12029/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012030 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012031 */
12032 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012033put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012034{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012035 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012036 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012037 tv->v_type = VAR_PARTIAL;
12038 tv->vval.v_partial = cb->cb_partial;
12039 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012041 else
12042 {
12043 tv->v_type = VAR_FUNC;
12044 tv->vval.v_string = vim_strsave(cb->cb_name);
12045 func_ref(cb->cb_name);
12046 }
12047}
12048
12049/*
12050 * Make a copy of "src" into "dest", allocating the function name if needed,
12051 * without incrementing the refcount.
12052 */
12053 void
12054set_callback(callback_T *dest, callback_T *src)
12055{
12056 if (src->cb_partial == NULL)
12057 {
12058 // just a function name, make a copy
12059 dest->cb_name = vim_strsave(src->cb_name);
12060 dest->cb_free_name = TRUE;
12061 }
12062 else
12063 {
12064 // cb_name is a pointer into cb_partial
12065 dest->cb_name = src->cb_name;
12066 dest->cb_free_name = FALSE;
12067 }
12068 dest->cb_partial = src->cb_partial;
12069}
12070
12071/*
12072 * Unref/free "callback" returned by get_callback() or set_callback().
12073 */
12074 void
12075free_callback(callback_T *callback)
12076{
12077 if (callback->cb_partial != NULL)
12078 {
12079 partial_unref(callback->cb_partial);
12080 callback->cb_partial = NULL;
12081 }
12082 else if (callback->cb_name != NULL)
12083 func_unref(callback->cb_name);
12084 if (callback->cb_free_name)
12085 {
12086 vim_free(callback->cb_name);
12087 callback->cb_free_name = FALSE;
12088 }
12089 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012090}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091
12092#ifdef FEAT_TIMERS
12093/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012094 * "timer_info([timer])" function
12095 */
12096 static void
12097f_timer_info(typval_T *argvars, typval_T *rettv)
12098{
12099 timer_T *timer = NULL;
12100
12101 if (rettv_list_alloc(rettv) != OK)
12102 return;
12103 if (argvars[0].v_type != VAR_UNKNOWN)
12104 {
12105 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012106 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012107 else
12108 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012109 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012110 if (timer != NULL)
12111 add_timer_info(rettv, timer);
12112 }
12113 }
12114 else
12115 add_timer_info_all(rettv);
12116}
12117
12118/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012119 * "timer_pause(timer, paused)" function
12120 */
12121 static void
12122f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12123{
12124 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012125 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012126
12127 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012128 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012129 else
12130 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012131 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012132 if (timer != NULL)
12133 timer->tr_paused = paused;
12134 }
12135}
12136
12137/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012138 * "timer_start(time, callback [, options])" function
12139 */
12140 static void
12141f_timer_start(typval_T *argvars, typval_T *rettv)
12142{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012143 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012144 timer_T *timer;
12145 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012146 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012147 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012148
Bram Moolenaar75537a92016-09-05 22:45:28 +020012149 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012150 if (check_secure())
12151 return;
12152 if (argvars[2].v_type != VAR_UNKNOWN)
12153 {
12154 if (argvars[2].v_type != VAR_DICT
12155 || (dict = argvars[2].vval.v_dict) == NULL)
12156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012157 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012158 return;
12159 }
12160 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012161 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012162 }
12163
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012164 callback = get_callback(&argvars[1]);
12165 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012166 return;
12167
12168 timer = create_timer(msec, repeat);
12169 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012170 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012171 else
12172 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012173 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012174 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012175 }
12176}
12177
12178/*
12179 * "timer_stop(timer)" function
12180 */
12181 static void
12182f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12183{
12184 timer_T *timer;
12185
12186 if (argvars[0].v_type != VAR_NUMBER)
12187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012188 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012189 return;
12190 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012191 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012192 if (timer != NULL)
12193 stop_timer(timer);
12194}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012195
12196/*
12197 * "timer_stopall()" function
12198 */
12199 static void
12200f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12201{
12202 stop_all_timers();
12203}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012204#endif
12205
12206/*
12207 * "tolower(string)" function
12208 */
12209 static void
12210f_tolower(typval_T *argvars, typval_T *rettv)
12211{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012212 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012213 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012214}
12215
12216/*
12217 * "toupper(string)" function
12218 */
12219 static void
12220f_toupper(typval_T *argvars, typval_T *rettv)
12221{
12222 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012223 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012224}
12225
12226/*
12227 * "tr(string, fromstr, tostr)" function
12228 */
12229 static void
12230f_tr(typval_T *argvars, typval_T *rettv)
12231{
12232 char_u *in_str;
12233 char_u *fromstr;
12234 char_u *tostr;
12235 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012236 int inlen;
12237 int fromlen;
12238 int tolen;
12239 int idx;
12240 char_u *cpstr;
12241 int cplen;
12242 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012243 char_u buf[NUMBUFLEN];
12244 char_u buf2[NUMBUFLEN];
12245 garray_T ga;
12246
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012247 in_str = tv_get_string(&argvars[0]);
12248 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12249 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250
12251 /* Default return value: empty string. */
12252 rettv->v_type = VAR_STRING;
12253 rettv->vval.v_string = NULL;
12254 if (fromstr == NULL || tostr == NULL)
12255 return; /* type error; errmsg already given */
12256 ga_init2(&ga, (int)sizeof(char), 80);
12257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012258 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012259 /* not multi-byte: fromstr and tostr must be the same length */
12260 if (STRLEN(fromstr) != STRLEN(tostr))
12261 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012262error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012263 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264 ga_clear(&ga);
12265 return;
12266 }
12267
12268 /* fromstr and tostr have to contain the same number of chars */
12269 while (*in_str != NUL)
12270 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012271 if (has_mbyte)
12272 {
12273 inlen = (*mb_ptr2len)(in_str);
12274 cpstr = in_str;
12275 cplen = inlen;
12276 idx = 0;
12277 for (p = fromstr; *p != NUL; p += fromlen)
12278 {
12279 fromlen = (*mb_ptr2len)(p);
12280 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12281 {
12282 for (p = tostr; *p != NUL; p += tolen)
12283 {
12284 tolen = (*mb_ptr2len)(p);
12285 if (idx-- == 0)
12286 {
12287 cplen = tolen;
12288 cpstr = p;
12289 break;
12290 }
12291 }
12292 if (*p == NUL) /* tostr is shorter than fromstr */
12293 goto error;
12294 break;
12295 }
12296 ++idx;
12297 }
12298
12299 if (first && cpstr == in_str)
12300 {
12301 /* Check that fromstr and tostr have the same number of
12302 * (multi-byte) characters. Done only once when a character
12303 * of in_str doesn't appear in fromstr. */
12304 first = FALSE;
12305 for (p = tostr; *p != NUL; p += tolen)
12306 {
12307 tolen = (*mb_ptr2len)(p);
12308 --idx;
12309 }
12310 if (idx != 0)
12311 goto error;
12312 }
12313
12314 (void)ga_grow(&ga, cplen);
12315 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12316 ga.ga_len += cplen;
12317
12318 in_str += inlen;
12319 }
12320 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012321 {
12322 /* When not using multi-byte chars we can do it faster. */
12323 p = vim_strchr(fromstr, *in_str);
12324 if (p != NULL)
12325 ga_append(&ga, tostr[p - fromstr]);
12326 else
12327 ga_append(&ga, *in_str);
12328 ++in_str;
12329 }
12330 }
12331
12332 /* add a terminating NUL */
12333 (void)ga_grow(&ga, 1);
12334 ga_append(&ga, NUL);
12335
12336 rettv->vval.v_string = ga.ga_data;
12337}
12338
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012339/*
12340 * "trim({expr})" function
12341 */
12342 static void
12343f_trim(typval_T *argvars, typval_T *rettv)
12344{
12345 char_u buf1[NUMBUFLEN];
12346 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012347 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012348 char_u *mask = NULL;
12349 char_u *tail;
12350 char_u *prev;
12351 char_u *p;
12352 int c1;
12353
12354 rettv->v_type = VAR_STRING;
12355 if (head == NULL)
12356 {
12357 rettv->vval.v_string = NULL;
12358 return;
12359 }
12360
12361 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012362 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012363
12364 while (*head != NUL)
12365 {
12366 c1 = PTR2CHAR(head);
12367 if (mask == NULL)
12368 {
12369 if (c1 > ' ' && c1 != 0xa0)
12370 break;
12371 }
12372 else
12373 {
12374 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12375 if (c1 == PTR2CHAR(p))
12376 break;
12377 if (*p == NUL)
12378 break;
12379 }
12380 MB_PTR_ADV(head);
12381 }
12382
12383 for (tail = head + STRLEN(head); tail > head; tail = prev)
12384 {
12385 prev = tail;
12386 MB_PTR_BACK(head, prev);
12387 c1 = PTR2CHAR(prev);
12388 if (mask == NULL)
12389 {
12390 if (c1 > ' ' && c1 != 0xa0)
12391 break;
12392 }
12393 else
12394 {
12395 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12396 if (c1 == PTR2CHAR(p))
12397 break;
12398 if (*p == NUL)
12399 break;
12400 }
12401 }
12402 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12403}
12404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012405#ifdef FEAT_FLOAT
12406/*
12407 * "trunc({float})" function
12408 */
12409 static void
12410f_trunc(typval_T *argvars, typval_T *rettv)
12411{
12412 float_T f = 0.0;
12413
12414 rettv->v_type = VAR_FLOAT;
12415 if (get_float_arg(argvars, &f) == OK)
12416 /* trunc() is not in C90, use floor() or ceil() instead. */
12417 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12418 else
12419 rettv->vval.v_float = 0.0;
12420}
12421#endif
12422
12423/*
12424 * "type(expr)" function
12425 */
12426 static void
12427f_type(typval_T *argvars, typval_T *rettv)
12428{
12429 int n = -1;
12430
12431 switch (argvars[0].v_type)
12432 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012433 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12434 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012435 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012436 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12437 case VAR_LIST: n = VAR_TYPE_LIST; break;
12438 case VAR_DICT: n = VAR_TYPE_DICT; break;
12439 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440 case VAR_SPECIAL:
12441 if (argvars[0].vval.v_number == VVAL_FALSE
12442 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012443 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012445 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012447 case VAR_JOB: n = VAR_TYPE_JOB; break;
12448 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012449 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012451 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012452 n = -1;
12453 break;
12454 }
12455 rettv->vval.v_number = n;
12456}
12457
12458/*
12459 * "undofile(name)" function
12460 */
12461 static void
12462f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12463{
12464 rettv->v_type = VAR_STRING;
12465#ifdef FEAT_PERSISTENT_UNDO
12466 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012467 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468
12469 if (*fname == NUL)
12470 {
12471 /* If there is no file name there will be no undo file. */
12472 rettv->vval.v_string = NULL;
12473 }
12474 else
12475 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012476 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477
12478 if (ffname != NULL)
12479 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12480 vim_free(ffname);
12481 }
12482 }
12483#else
12484 rettv->vval.v_string = NULL;
12485#endif
12486}
12487
12488/*
12489 * "undotree()" function
12490 */
12491 static void
12492f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12493{
12494 if (rettv_dict_alloc(rettv) == OK)
12495 {
12496 dict_T *dict = rettv->vval.v_dict;
12497 list_T *list;
12498
Bram Moolenaare0be1672018-07-08 16:50:37 +020012499 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12500 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12501 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12502 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12503 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12504 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505
12506 list = list_alloc();
12507 if (list != NULL)
12508 {
12509 u_eval_tree(curbuf->b_u_oldhead, list);
12510 dict_add_list(dict, "entries", list);
12511 }
12512 }
12513}
12514
12515/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516 * "virtcol(string)" function
12517 */
12518 static void
12519f_virtcol(typval_T *argvars, typval_T *rettv)
12520{
12521 colnr_T vcol = 0;
12522 pos_T *fp;
12523 int fnum = curbuf->b_fnum;
12524
12525 fp = var2fpos(&argvars[0], FALSE, &fnum);
12526 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12527 && fnum == curbuf->b_fnum)
12528 {
12529 getvvcol(curwin, fp, NULL, NULL, &vcol);
12530 ++vcol;
12531 }
12532
12533 rettv->vval.v_number = vcol;
12534}
12535
12536/*
12537 * "visualmode()" function
12538 */
12539 static void
12540f_visualmode(typval_T *argvars, typval_T *rettv)
12541{
12542 char_u str[2];
12543
12544 rettv->v_type = VAR_STRING;
12545 str[0] = curbuf->b_visual_mode_eval;
12546 str[1] = NUL;
12547 rettv->vval.v_string = vim_strsave(str);
12548
12549 /* A non-zero number or non-empty string argument: reset mode. */
12550 if (non_zero_arg(&argvars[0]))
12551 curbuf->b_visual_mode_eval = NUL;
12552}
12553
12554/*
12555 * "wildmenumode()" function
12556 */
12557 static void
12558f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12559{
12560#ifdef FEAT_WILDMENU
12561 if (wild_menu_showing)
12562 rettv->vval.v_number = 1;
12563#endif
12564}
12565
12566/*
12567 * "winbufnr(nr)" function
12568 */
12569 static void
12570f_winbufnr(typval_T *argvars, typval_T *rettv)
12571{
12572 win_T *wp;
12573
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012574 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575 if (wp == NULL)
12576 rettv->vval.v_number = -1;
12577 else
12578 rettv->vval.v_number = wp->w_buffer->b_fnum;
12579}
12580
12581/*
12582 * "wincol()" function
12583 */
12584 static void
12585f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12586{
12587 validate_cursor();
12588 rettv->vval.v_number = curwin->w_wcol + 1;
12589}
12590
12591/*
12592 * "winheight(nr)" function
12593 */
12594 static void
12595f_winheight(typval_T *argvars, typval_T *rettv)
12596{
12597 win_T *wp;
12598
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012599 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012600 if (wp == NULL)
12601 rettv->vval.v_number = -1;
12602 else
12603 rettv->vval.v_number = wp->w_height;
12604}
12605
12606/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012607 * "winlayout()" function
12608 */
12609 static void
12610f_winlayout(typval_T *argvars, typval_T *rettv)
12611{
12612 tabpage_T *tp;
12613
12614 if (rettv_list_alloc(rettv) != OK)
12615 return;
12616
12617 if (argvars[0].v_type == VAR_UNKNOWN)
12618 tp = curtab;
12619 else
12620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012621 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012622 if (tp == NULL)
12623 return;
12624 }
12625
12626 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12627}
12628
12629/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630 * "winline()" function
12631 */
12632 static void
12633f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12634{
12635 validate_cursor();
12636 rettv->vval.v_number = curwin->w_wrow + 1;
12637}
12638
12639/*
12640 * "winnr()" function
12641 */
12642 static void
12643f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12644{
12645 int nr = 1;
12646
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012647 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648 rettv->vval.v_number = nr;
12649}
12650
12651/*
12652 * "winrestcmd()" function
12653 */
12654 static void
12655f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12656{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012657 win_T *wp;
12658 int winnr = 1;
12659 garray_T ga;
12660 char_u buf[50];
12661
12662 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012663 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012664 {
12665 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12666 ga_concat(&ga, buf);
12667 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12668 ga_concat(&ga, buf);
12669 ++winnr;
12670 }
12671 ga_append(&ga, NUL);
12672
12673 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012674 rettv->v_type = VAR_STRING;
12675}
12676
12677/*
12678 * "winrestview()" function
12679 */
12680 static void
12681f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12682{
12683 dict_T *dict;
12684
12685 if (argvars[0].v_type != VAR_DICT
12686 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012687 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688 else
12689 {
12690 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012691 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012693 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012695 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12697 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010012698 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012699 curwin->w_set_curswant = FALSE;
12700 }
12701
12702 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012703 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704#ifdef FEAT_DIFF
12705 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012706 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012707#endif
12708 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012709 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012710 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012711 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012712
12713 check_cursor();
12714 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020012715 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012716 changed_window_setting();
12717
12718 if (curwin->w_topline <= 0)
12719 curwin->w_topline = 1;
12720 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12721 curwin->w_topline = curbuf->b_ml.ml_line_count;
12722#ifdef FEAT_DIFF
12723 check_topfill(curwin, TRUE);
12724#endif
12725 }
12726}
12727
12728/*
12729 * "winsaveview()" function
12730 */
12731 static void
12732f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12733{
12734 dict_T *dict;
12735
12736 if (rettv_dict_alloc(rettv) == FAIL)
12737 return;
12738 dict = rettv->vval.v_dict;
12739
Bram Moolenaare0be1672018-07-08 16:50:37 +020012740 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
12741 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020012742 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012743 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020012744 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012745
Bram Moolenaare0be1672018-07-08 16:50:37 +020012746 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020012748 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020012750 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
12751 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752}
12753
12754/*
12755 * "winwidth(nr)" function
12756 */
12757 static void
12758f_winwidth(typval_T *argvars, typval_T *rettv)
12759{
12760 win_T *wp;
12761
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012762 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012763 if (wp == NULL)
12764 rettv->vval.v_number = -1;
12765 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012767}
12768
12769/*
12770 * "wordcount()" function
12771 */
12772 static void
12773f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
12774{
12775 if (rettv_dict_alloc(rettv) == FAIL)
12776 return;
12777 cursor_pos_info(rettv->vval.v_dict);
12778}
12779
12780/*
12781 * "writefile()" function
12782 */
12783 static void
12784f_writefile(typval_T *argvars, typval_T *rettv)
12785{
12786 int binary = FALSE;
12787 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012788#ifdef HAVE_FSYNC
12789 int do_fsync = p_fs;
12790#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 char_u *fname;
12792 FILE *fd;
12793 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012794 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012795 list_T *list = NULL;
12796 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012797
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012798 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010012799 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012800 return;
12801
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012802 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012803 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012804 list = argvars[0].vval.v_list;
12805 if (list == NULL)
12806 return;
12807 for (li = list->lv_first; li != NULL; li = li->li_next)
12808 if (tv_get_string_chk(&li->li_tv) == NULL)
12809 return;
12810 }
12811 else if (argvars[0].v_type == VAR_BLOB)
12812 {
12813 blob = argvars[0].vval.v_blob;
12814 if (blob == NULL)
12815 return;
12816 }
12817 else
12818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012819 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 return;
12821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822
12823 if (argvars[2].v_type != VAR_UNKNOWN)
12824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012826
12827 if (arg2 == NULL)
12828 return;
12829 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012831 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012832 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012833#ifdef HAVE_FSYNC
12834 if (vim_strchr(arg2, 's') != NULL)
12835 do_fsync = TRUE;
12836 else if (vim_strchr(arg2, 'S') != NULL)
12837 do_fsync = FALSE;
12838#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839 }
12840
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012841 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012842 if (fname == NULL)
12843 return;
12844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012845 /* Always open the file in binary mode, library functions have a mind of
12846 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012847 if (*fname == NUL || (fd = mch_fopen((char *)fname,
12848 append ? APPENDBIN : WRITEBIN)) == NULL)
12849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012850 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851 ret = -1;
12852 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012853 else if (blob)
12854 {
12855 if (write_blob(fd, blob) == FAIL)
12856 ret = -1;
12857#ifdef HAVE_FSYNC
12858 else if (do_fsync)
12859 // Ignore the error, the user wouldn't know what to do about it.
12860 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010012861 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012862#endif
12863 fclose(fd);
12864 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865 else
12866 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012867 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012869#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010012870 else if (do_fsync)
12871 /* Ignore the error, the user wouldn't know what to do about it.
12872 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010012873 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012875 fclose(fd);
12876 }
12877
12878 rettv->vval.v_number = ret;
12879}
12880
12881/*
12882 * "xor(expr, expr)" function
12883 */
12884 static void
12885f_xor(typval_T *argvars, typval_T *rettv)
12886{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012887 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
12888 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012889}
12890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891#endif /* FEAT_EVAL */