blob: d328f87818c31d5e1940882669a5a455bd549184 [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);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200138static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
139static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
140static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
141static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200142static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200143static void f_getfontname(typval_T *argvars, typval_T *rettv);
144static void f_getfperm(typval_T *argvars, typval_T *rettv);
145static void f_getfsize(typval_T *argvars, typval_T *rettv);
146static void f_getftime(typval_T *argvars, typval_T *rettv);
147static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100148static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200149static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200150static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200151static void f_getpid(typval_T *argvars, typval_T *rettv);
152static void f_getcurpos(typval_T *argvars, typval_T *rettv);
153static void f_getpos(typval_T *argvars, typval_T *rettv);
154static void f_getqflist(typval_T *argvars, typval_T *rettv);
155static void f_getreg(typval_T *argvars, typval_T *rettv);
156static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200157static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_gettabvar(typval_T *argvars, typval_T *rettv);
159static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100160static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200161static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100162static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_getwinposx(typval_T *argvars, typval_T *rettv);
164static void f_getwinposy(typval_T *argvars, typval_T *rettv);
165static void f_getwinvar(typval_T *argvars, typval_T *rettv);
166static void f_glob(typval_T *argvars, typval_T *rettv);
167static void f_globpath(typval_T *argvars, typval_T *rettv);
168static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
169static void f_has(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
171static void f_hasmapto(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_hlID(typval_T *argvars, typval_T *rettv);
173static void f_hlexists(typval_T *argvars, typval_T *rettv);
174static void f_hostname(typval_T *argvars, typval_T *rettv);
175static void f_iconv(typval_T *argvars, typval_T *rettv);
176static void f_indent(typval_T *argvars, typval_T *rettv);
177static void f_index(typval_T *argvars, typval_T *rettv);
178static void f_input(typval_T *argvars, typval_T *rettv);
179static void f_inputdialog(typval_T *argvars, typval_T *rettv);
180static void f_inputlist(typval_T *argvars, typval_T *rettv);
181static void f_inputrestore(typval_T *argvars, typval_T *rettv);
182static void f_inputsave(typval_T *argvars, typval_T *rettv);
183static void f_inputsecret(typval_T *argvars, typval_T *rettv);
184static void f_insert(typval_T *argvars, typval_T *rettv);
185static void f_invert(typval_T *argvars, typval_T *rettv);
186static void f_isdirectory(typval_T *argvars, typval_T *rettv);
187static void f_islocked(typval_T *argvars, typval_T *rettv);
188#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200189static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_isnan(typval_T *argvars, typval_T *rettv);
191#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
193static void f_len(typval_T *argvars, typval_T *rettv);
194static void f_libcall(typval_T *argvars, typval_T *rettv);
195static void f_libcallnr(typval_T *argvars, typval_T *rettv);
196static void f_line(typval_T *argvars, typval_T *rettv);
197static void f_line2byte(typval_T *argvars, typval_T *rettv);
198static void f_lispindent(typval_T *argvars, typval_T *rettv);
199static void f_localtime(typval_T *argvars, typval_T *rettv);
200#ifdef FEAT_FLOAT
201static void f_log(typval_T *argvars, typval_T *rettv);
202static void f_log10(typval_T *argvars, typval_T *rettv);
203#endif
204#ifdef FEAT_LUA
205static void f_luaeval(typval_T *argvars, typval_T *rettv);
206#endif
207static void f_map(typval_T *argvars, typval_T *rettv);
208static void f_maparg(typval_T *argvars, typval_T *rettv);
209static void f_mapcheck(typval_T *argvars, typval_T *rettv);
210static void f_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200211static void f_matchend(typval_T *argvars, typval_T *rettv);
212static void f_matchlist(typval_T *argvars, typval_T *rettv);
213static void f_matchstr(typval_T *argvars, typval_T *rettv);
214static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
215static void f_max(typval_T *argvars, typval_T *rettv);
216static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200217static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200218static void f_mode(typval_T *argvars, typval_T *rettv);
219#ifdef FEAT_MZSCHEME
220static void f_mzeval(typval_T *argvars, typval_T *rettv);
221#endif
222static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
223static void f_nr2char(typval_T *argvars, typval_T *rettv);
224static void f_or(typval_T *argvars, typval_T *rettv);
225static void f_pathshorten(typval_T *argvars, typval_T *rettv);
226#ifdef FEAT_PERL
227static void f_perleval(typval_T *argvars, typval_T *rettv);
228#endif
229#ifdef FEAT_FLOAT
230static void f_pow(typval_T *argvars, typval_T *rettv);
231#endif
232static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
233static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200234static void f_pum_getpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200235static void f_pumvisible(typval_T *argvars, typval_T *rettv);
236#ifdef FEAT_PYTHON3
237static void f_py3eval(typval_T *argvars, typval_T *rettv);
238#endif
239#ifdef FEAT_PYTHON
240static void f_pyeval(typval_T *argvars, typval_T *rettv);
241#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100242#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
243static void f_pyxeval(typval_T *argvars, typval_T *rettv);
244#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200246static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200247static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200248static void f_reg_executing(typval_T *argvars, typval_T *rettv);
249static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200250static void f_reltime(typval_T *argvars, typval_T *rettv);
251#ifdef FEAT_FLOAT
252static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
253#endif
254static void f_reltimestr(typval_T *argvars, typval_T *rettv);
255static void f_remote_expr(typval_T *argvars, typval_T *rettv);
256static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
257static void f_remote_peek(typval_T *argvars, typval_T *rettv);
258static void f_remote_read(typval_T *argvars, typval_T *rettv);
259static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100260static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200261static void f_remove(typval_T *argvars, typval_T *rettv);
262static void f_rename(typval_T *argvars, typval_T *rettv);
263static void f_repeat(typval_T *argvars, typval_T *rettv);
264static void f_resolve(typval_T *argvars, typval_T *rettv);
265static void f_reverse(typval_T *argvars, typval_T *rettv);
266#ifdef FEAT_FLOAT
267static void f_round(typval_T *argvars, typval_T *rettv);
268#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100269#ifdef FEAT_RUBY
270static void f_rubyeval(typval_T *argvars, typval_T *rettv);
271#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200272static void f_screenattr(typval_T *argvars, typval_T *rettv);
273static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100274static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200275static void f_screencol(typval_T *argvars, typval_T *rettv);
276static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100277static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200278static void f_search(typval_T *argvars, typval_T *rettv);
279static void f_searchdecl(typval_T *argvars, typval_T *rettv);
280static void f_searchpair(typval_T *argvars, typval_T *rettv);
281static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
282static void f_searchpos(typval_T *argvars, typval_T *rettv);
283static void f_server2client(typval_T *argvars, typval_T *rettv);
284static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200285static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200286static void f_setbufvar(typval_T *argvars, typval_T *rettv);
287static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
288static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200289static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200290static void f_setfperm(typval_T *argvars, typval_T *rettv);
291static void f_setline(typval_T *argvars, typval_T *rettv);
292static void f_setloclist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200293static void f_setpos(typval_T *argvars, typval_T *rettv);
294static void f_setqflist(typval_T *argvars, typval_T *rettv);
295static void f_setreg(typval_T *argvars, typval_T *rettv);
296static void f_settabvar(typval_T *argvars, typval_T *rettv);
297static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100298static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_setwinvar(typval_T *argvars, typval_T *rettv);
300#ifdef FEAT_CRYPT
301static void f_sha256(typval_T *argvars, typval_T *rettv);
302#endif /* FEAT_CRYPT */
303static void f_shellescape(typval_T *argvars, typval_T *rettv);
304static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
305static void f_simplify(typval_T *argvars, typval_T *rettv);
306#ifdef FEAT_FLOAT
307static void f_sin(typval_T *argvars, typval_T *rettv);
308static void f_sinh(typval_T *argvars, typval_T *rettv);
309#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200310static void f_soundfold(typval_T *argvars, typval_T *rettv);
311static void f_spellbadword(typval_T *argvars, typval_T *rettv);
312static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
313static void f_split(typval_T *argvars, typval_T *rettv);
314#ifdef FEAT_FLOAT
315static void f_sqrt(typval_T *argvars, typval_T *rettv);
316static void f_str2float(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200318static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_str2nr(typval_T *argvars, typval_T *rettv);
320static void f_strchars(typval_T *argvars, typval_T *rettv);
321#ifdef HAVE_STRFTIME
322static void f_strftime(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_strgetchar(typval_T *argvars, typval_T *rettv);
325static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_strlen(typval_T *argvars, typval_T *rettv);
327static void f_strcharpart(typval_T *argvars, typval_T *rettv);
328static void f_strpart(typval_T *argvars, typval_T *rettv);
329static void f_strridx(typval_T *argvars, typval_T *rettv);
330static void f_strtrans(typval_T *argvars, typval_T *rettv);
331static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
332static void f_strwidth(typval_T *argvars, typval_T *rettv);
333static void f_submatch(typval_T *argvars, typval_T *rettv);
334static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200335static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200336static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200337static void f_synID(typval_T *argvars, typval_T *rettv);
338static void f_synIDattr(typval_T *argvars, typval_T *rettv);
339static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
340static void f_synstack(typval_T *argvars, typval_T *rettv);
341static void f_synconcealed(typval_T *argvars, typval_T *rettv);
342static void f_system(typval_T *argvars, typval_T *rettv);
343static void f_systemlist(typval_T *argvars, typval_T *rettv);
344static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
345static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
346static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
347static void f_taglist(typval_T *argvars, typval_T *rettv);
348static void f_tagfiles(typval_T *argvars, typval_T *rettv);
349static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200350#ifdef FEAT_FLOAT
351static void f_tan(typval_T *argvars, typval_T *rettv);
352static void f_tanh(typval_T *argvars, typval_T *rettv);
353#endif
354#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200355static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200356static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_timer_start(typval_T *argvars, typval_T *rettv);
358static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200359static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360#endif
361static void f_tolower(typval_T *argvars, typval_T *rettv);
362static void f_toupper(typval_T *argvars, typval_T *rettv);
363static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100364static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200365#ifdef FEAT_FLOAT
366static void f_trunc(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_type(typval_T *argvars, typval_T *rettv);
369static void f_undofile(typval_T *argvars, typval_T *rettv);
370static void f_undotree(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371static void f_virtcol(typval_T *argvars, typval_T *rettv);
372static void f_visualmode(typval_T *argvars, typval_T *rettv);
373static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200374static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200375static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
376static void f_win_getid(typval_T *argvars, typval_T *rettv);
377static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
378static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
379static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100380static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_winbufnr(typval_T *argvars, typval_T *rettv);
382static void f_wincol(typval_T *argvars, typval_T *rettv);
383static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200384static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200385static void f_winline(typval_T *argvars, typval_T *rettv);
386static void f_winnr(typval_T *argvars, typval_T *rettv);
387static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
388static void f_winrestview(typval_T *argvars, typval_T *rettv);
389static void f_winsaveview(typval_T *argvars, typval_T *rettv);
390static void f_winwidth(typval_T *argvars, typval_T *rettv);
391static void f_writefile(typval_T *argvars, typval_T *rettv);
392static void f_wordcount(typval_T *argvars, typval_T *rettv);
393static void f_xor(typval_T *argvars, typval_T *rettv);
394
395/*
396 * Array with names and number of arguments of all internal functions
397 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
398 */
Bram Moolenaarac92e252019-08-03 21:58:38 +0200399typedef struct
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200400{
Bram Moolenaar25e42232019-08-04 15:04:10 +0200401 char *f_name; // function name
402 char f_min_argc; // minimal number of arguments
403 char f_max_argc; // maximal number of arguments
404 char f_argtype; // for method: FEARG_ values
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200405 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaar25e42232019-08-04 15:04:10 +0200406 // implementation of function
Bram Moolenaarac92e252019-08-03 21:58:38 +0200407} funcentry_T;
408
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200409// values for f_argtype; zero means it cannot be used as a method
410#define FEARG_1 1 // base is the first argument
411#define FEARG_2 2 // base is the second argument
Bram Moolenaar24278d22019-08-16 21:49:22 +0200412#define FEARG_3 3 // base is the third argument
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200413#define FEARG_LAST 9 // base is the last argument
414
Bram Moolenaarac92e252019-08-03 21:58:38 +0200415static funcentry_T global_functions[] =
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416{
417#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200418 {"abs", 1, 1, FEARG_1, f_abs},
419 {"acos", 1, 1, FEARG_1, f_acos}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200421 {"add", 2, 2, FEARG_1, f_add},
422 {"and", 2, 2, 0, f_and},
423 {"append", 2, 2, FEARG_LAST, f_append},
424 {"appendbufline", 3, 3, FEARG_LAST, f_appendbufline},
425 {"argc", 0, 1, 0, f_argc},
426 {"argidx", 0, 0, 0, f_argidx},
427 {"arglistid", 0, 2, 0, f_arglistid},
428 {"argv", 0, 2, 0, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200430 {"asin", 1, 1, FEARG_1, f_asin}, // WJMc
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431#endif
Bram Moolenaar24278d22019-08-16 21:49:22 +0200432 {"assert_beeps", 1, 2, FEARG_1, f_assert_beeps},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200433 {"assert_equal", 2, 3, FEARG_2, f_assert_equal},
434 {"assert_equalfile", 2, 2, 0, f_assert_equalfile},
435 {"assert_exception", 1, 2, 0, f_assert_exception},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200436 {"assert_fails", 1, 3, FEARG_1, f_assert_fails},
437 {"assert_false", 1, 2, FEARG_1, f_assert_false},
438 {"assert_inrange", 3, 4, FEARG_3, f_assert_inrange},
439 {"assert_match", 2, 3, FEARG_2, f_assert_match},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200440 {"assert_notequal", 2, 3, FEARG_2, f_assert_notequal},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200441 {"assert_notmatch", 2, 3, FEARG_2, f_assert_notmatch},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200442 {"assert_report", 1, 1, 0, f_assert_report},
Bram Moolenaar24278d22019-08-16 21:49:22 +0200443 {"assert_true", 1, 2, FEARG_1, f_assert_true},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200445 {"atan", 1, 1, FEARG_1, f_atan},
446 {"atan2", 2, 2, FEARG_1, f_atan2},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100448#ifdef FEAT_BEVAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200449 {"balloon_gettext", 0, 0, 0, f_balloon_gettext},
450 {"balloon_show", 1, 1, 0, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100451# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200452 {"balloon_split", 1, 1, 0, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100453# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100454#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200455 {"browse", 4, 4, 0, f_browse},
456 {"browsedir", 2, 2, 0, f_browsedir},
457 {"bufadd", 1, 1, 0, f_bufadd},
458 {"bufexists", 1, 1, 0, f_bufexists},
459 {"buffer_exists", 1, 1, 0, f_bufexists}, // obsolete
460 {"buffer_name", 1, 1, 0, f_bufname}, // obsolete
461 {"buffer_number", 1, 1, 0, f_bufnr}, // obsolete
462 {"buflisted", 1, 1, 0, f_buflisted},
463 {"bufload", 1, 1, 0, f_bufload},
464 {"bufloaded", 1, 1, 0, f_bufloaded},
465 {"bufname", 1, 1, 0, f_bufname},
466 {"bufnr", 1, 2, 0, f_bufnr},
467 {"bufwinid", 1, 1, 0, f_bufwinid},
468 {"bufwinnr", 1, 1, 0, f_bufwinnr},
469 {"byte2line", 1, 1, 0, f_byte2line},
470 {"byteidx", 2, 2, 0, f_byteidx},
471 {"byteidxcomp", 2, 2, 0, f_byteidxcomp},
472 {"call", 2, 3, 0, f_call},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200474 {"ceil", 1, 1, FEARG_1, f_ceil},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475#endif
476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200477 {"ch_canread", 1, 1, 0, f_ch_canread},
478 {"ch_close", 1, 1, 0, f_ch_close},
479 {"ch_close_in", 1, 1, 0, f_ch_close_in},
480 {"ch_evalexpr", 2, 3, 0, f_ch_evalexpr},
481 {"ch_evalraw", 2, 3, 0, f_ch_evalraw},
482 {"ch_getbufnr", 2, 2, 0, f_ch_getbufnr},
483 {"ch_getjob", 1, 1, 0, f_ch_getjob},
484 {"ch_info", 1, 1, 0, f_ch_info},
485 {"ch_log", 1, 2, 0, f_ch_log},
486 {"ch_logfile", 1, 2, 0, f_ch_logfile},
487 {"ch_open", 1, 2, 0, f_ch_open},
488 {"ch_read", 1, 2, 0, f_ch_read},
489 {"ch_readblob", 1, 2, 0, f_ch_readblob},
490 {"ch_readraw", 1, 2, 0, f_ch_readraw},
491 {"ch_sendexpr", 2, 3, 0, f_ch_sendexpr},
492 {"ch_sendraw", 2, 3, 0, f_ch_sendraw},
493 {"ch_setoptions", 2, 2, 0, f_ch_setoptions},
494 {"ch_status", 1, 2, 0, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200496 {"changenr", 0, 0, 0, f_changenr},
497 {"char2nr", 1, 2, 0, f_char2nr},
498 {"chdir", 1, 1, 0, f_chdir},
499 {"cindent", 1, 1, 0, f_cindent},
500 {"clearmatches", 0, 1, 0, f_clearmatches},
501 {"col", 1, 1, 0, f_col},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502#if defined(FEAT_INS_EXPAND)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200503 {"complete", 2, 2, 0, f_complete},
504 {"complete_add", 1, 1, 0, f_complete_add},
505 {"complete_check", 0, 0, 0, f_complete_check},
506 {"complete_info", 0, 1, 0, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200508 {"confirm", 1, 4, 0, f_confirm},
509 {"copy", 1, 1, FEARG_1, f_copy},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200510#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200511 {"cos", 1, 1, FEARG_1, f_cos},
512 {"cosh", 1, 1, FEARG_1, f_cosh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200514 {"count", 2, 4, FEARG_1, f_count},
515 {"cscope_connection",0,3, 0, f_cscope_connection},
516 {"cursor", 1, 3, 0, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100517#ifdef MSWIN
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200518 {"debugbreak", 1, 1, 0, f_debugbreak},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200519#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200520 {"deepcopy", 1, 2, 0, f_deepcopy},
521 {"delete", 1, 2, 0, f_delete},
522 {"deletebufline", 2, 3, 0, f_deletebufline},
523 {"did_filetype", 0, 0, 0, f_did_filetype},
524 {"diff_filler", 1, 1, 0, f_diff_filler},
525 {"diff_hlID", 2, 2, 0, f_diff_hlID},
526 {"empty", 1, 1, FEARG_1, f_empty},
527 {"environ", 0, 0, 0, f_environ},
528 {"escape", 2, 2, 0, f_escape},
529 {"eval", 1, 1, FEARG_1, f_eval},
530 {"eventhandler", 0, 0, 0, f_eventhandler},
531 {"executable", 1, 1, 0, f_executable},
532 {"execute", 1, 2, 0, f_execute},
533 {"exepath", 1, 1, 0, f_exepath},
534 {"exists", 1, 1, 0, f_exists},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200536 {"exp", 1, 1, FEARG_1, f_exp},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200538 {"expand", 1, 3, 0, f_expand},
539 {"expandcmd", 1, 1, 0, f_expandcmd},
540 {"extend", 2, 3, FEARG_1, f_extend},
541 {"feedkeys", 1, 2, 0, f_feedkeys},
542 {"file_readable", 1, 1, 0, f_filereadable}, // obsolete
543 {"filereadable", 1, 1, 0, f_filereadable},
544 {"filewritable", 1, 1, 0, f_filewritable},
545 {"filter", 2, 2, FEARG_1, f_filter},
546 {"finddir", 1, 3, 0, f_finddir},
547 {"findfile", 1, 3, 0, f_findfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200549 {"float2nr", 1, 1, FEARG_1, f_float2nr},
550 {"floor", 1, 1, FEARG_1, f_floor},
551 {"fmod", 2, 2, FEARG_1, f_fmod},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200553 {"fnameescape", 1, 1, 0, f_fnameescape},
554 {"fnamemodify", 2, 2, 0, f_fnamemodify},
555 {"foldclosed", 1, 1, 0, f_foldclosed},
556 {"foldclosedend", 1, 1, 0, f_foldclosedend},
557 {"foldlevel", 1, 1, 0, f_foldlevel},
558 {"foldtext", 0, 0, 0, f_foldtext},
559 {"foldtextresult", 1, 1, 0, f_foldtextresult},
560 {"foreground", 0, 0, 0, f_foreground},
561 {"funcref", 1, 3, 0, f_funcref},
562 {"function", 1, 3, 0, f_function},
563 {"garbagecollect", 0, 1, 0, f_garbagecollect},
564 {"get", 2, 3, FEARG_1, f_get},
565 {"getbufinfo", 0, 1, 0, f_getbufinfo},
566 {"getbufline", 2, 3, 0, f_getbufline},
567 {"getbufvar", 2, 3, 0, f_getbufvar},
568 {"getchangelist", 1, 1, 0, f_getchangelist},
569 {"getchar", 0, 1, 0, f_getchar},
570 {"getcharmod", 0, 0, 0, f_getcharmod},
571 {"getcharsearch", 0, 0, 0, f_getcharsearch},
572 {"getcmdline", 0, 0, 0, f_getcmdline},
573 {"getcmdpos", 0, 0, 0, f_getcmdpos},
574 {"getcmdtype", 0, 0, 0, f_getcmdtype},
575 {"getcmdwintype", 0, 0, 0, f_getcmdwintype},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200576#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200577 {"getcompletion", 2, 3, 0, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200578#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200579 {"getcurpos", 0, 0, 0, f_getcurpos},
580 {"getcwd", 0, 2, 0, f_getcwd},
581 {"getenv", 1, 1, 0, f_getenv},
582 {"getfontname", 0, 1, 0, f_getfontname},
583 {"getfperm", 1, 1, 0, f_getfperm},
584 {"getfsize", 1, 1, 0, f_getfsize},
585 {"getftime", 1, 1, 0, f_getftime},
586 {"getftype", 1, 1, 0, f_getftype},
587 {"getjumplist", 0, 2, 0, f_getjumplist},
588 {"getline", 1, 2, 0, f_getline},
589 {"getloclist", 1, 2, 0, f_getloclist},
590 {"getmatches", 0, 1, 0, f_getmatches},
591 {"getpid", 0, 0, 0, f_getpid},
592 {"getpos", 1, 1, 0, f_getpos},
593 {"getqflist", 0, 1, 0, f_getqflist},
594 {"getreg", 0, 3, 0, f_getreg},
595 {"getregtype", 0, 1, 0, f_getregtype},
596 {"gettabinfo", 0, 1, 0, f_gettabinfo},
597 {"gettabvar", 2, 3, 0, f_gettabvar},
598 {"gettabwinvar", 3, 4, 0, f_gettabwinvar},
599 {"gettagstack", 0, 1, 0, f_gettagstack},
600 {"getwininfo", 0, 1, 0, f_getwininfo},
601 {"getwinpos", 0, 1, 0, f_getwinpos},
602 {"getwinposx", 0, 0, 0, f_getwinposx},
603 {"getwinposy", 0, 0, 0, f_getwinposy},
604 {"getwinvar", 2, 3, 0, f_getwinvar},
605 {"glob", 1, 4, 0, f_glob},
606 {"glob2regpat", 1, 1, 0, f_glob2regpat},
607 {"globpath", 2, 5, 0, f_globpath},
608 {"has", 1, 1, 0, f_has},
609 {"has_key", 2, 2, FEARG_1, f_has_key},
610 {"haslocaldir", 0, 2, 0, f_haslocaldir},
611 {"hasmapto", 1, 3, 0, f_hasmapto},
612 {"highlightID", 1, 1, 0, f_hlID}, // obsolete
613 {"highlight_exists",1, 1, 0, f_hlexists}, // obsolete
614 {"histadd", 2, 2, 0, f_histadd},
615 {"histdel", 1, 2, 0, f_histdel},
616 {"histget", 1, 2, 0, f_histget},
617 {"histnr", 1, 1, 0, f_histnr},
618 {"hlID", 1, 1, 0, f_hlID},
619 {"hlexists", 1, 1, 0, f_hlexists},
620 {"hostname", 0, 0, 0, f_hostname},
621 {"iconv", 3, 3, 0, f_iconv},
622 {"indent", 1, 1, 0, f_indent},
623 {"index", 2, 4, FEARG_1, f_index},
624 {"input", 1, 3, 0, f_input},
625 {"inputdialog", 1, 3, 0, f_inputdialog},
626 {"inputlist", 1, 1, 0, f_inputlist},
627 {"inputrestore", 0, 0, 0, f_inputrestore},
628 {"inputsave", 0, 0, 0, f_inputsave},
629 {"inputsecret", 1, 2, 0, f_inputsecret},
630 {"insert", 2, 3, FEARG_1, f_insert},
631 {"invert", 1, 1, 0, f_invert},
632 {"isdirectory", 1, 1, 0, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200633#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200634 {"isinf", 1, 1, FEARG_1, f_isinf},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200635#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200636 {"islocked", 1, 1, 0, f_islocked},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200638 {"isnan", 1, 1, FEARG_1, f_isnan},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200640 {"items", 1, 1, FEARG_1, f_items},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200642 {"job_getchannel", 1, 1, 0, f_job_getchannel},
643 {"job_info", 0, 1, 0, f_job_info},
644 {"job_setoptions", 2, 2, 0, f_job_setoptions},
645 {"job_start", 1, 2, 0, f_job_start},
646 {"job_status", 1, 1, 0, f_job_status},
647 {"job_stop", 1, 2, 0, f_job_stop},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200649 {"join", 1, 2, FEARG_1, f_join},
650 {"js_decode", 1, 1, 0, f_js_decode},
651 {"js_encode", 1, 1, 0, f_js_encode},
652 {"json_decode", 1, 1, 0, f_json_decode},
653 {"json_encode", 1, 1, 0, f_json_encode},
654 {"keys", 1, 1, FEARG_1, f_keys},
655 {"last_buffer_nr", 0, 0, 0, f_last_buffer_nr}, // obsolete
656 {"len", 1, 1, FEARG_1, f_len},
657 {"libcall", 3, 3, 0, f_libcall},
658 {"libcallnr", 3, 3, 0, f_libcallnr},
659 {"line", 1, 1, 0, f_line},
660 {"line2byte", 1, 1, 0, f_line2byte},
661 {"lispindent", 1, 1, 0, f_lispindent},
662 {"list2str", 1, 2, 0, f_list2str},
663 {"listener_add", 1, 2, 0, f_listener_add},
664 {"listener_flush", 0, 1, 0, f_listener_flush},
665 {"listener_remove", 1, 1, 0, f_listener_remove},
666 {"localtime", 0, 0, 0, f_localtime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200668 {"log", 1, 1, FEARG_1, f_log},
669 {"log10", 1, 1, FEARG_1, f_log10},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670#endif
671#ifdef FEAT_LUA
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200672 {"luaeval", 1, 2, 0, f_luaeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200674 {"map", 2, 2, FEARG_1, f_map},
675 {"maparg", 1, 4, 0, f_maparg},
676 {"mapcheck", 1, 3, 0, f_mapcheck},
677 {"match", 2, 4, 0, f_match},
678 {"matchadd", 2, 5, 0, f_matchadd},
679 {"matchaddpos", 2, 5, 0, f_matchaddpos},
680 {"matcharg", 1, 1, 0, f_matcharg},
681 {"matchdelete", 1, 2, 0, f_matchdelete},
682 {"matchend", 2, 4, 0, f_matchend},
683 {"matchlist", 2, 4, 0, f_matchlist},
684 {"matchstr", 2, 4, 0, f_matchstr},
685 {"matchstrpos", 2, 4, 0, f_matchstrpos},
686 {"max", 1, 1, FEARG_1, f_max},
687 {"min", 1, 1, FEARG_1, f_min},
688 {"mkdir", 1, 3, 0, f_mkdir},
689 {"mode", 0, 1, 0, f_mode},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690#ifdef FEAT_MZSCHEME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200691 {"mzeval", 1, 1, 0, f_mzeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200692#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200693 {"nextnonblank", 1, 1, 0, f_nextnonblank},
694 {"nr2char", 1, 2, 0, f_nr2char},
695 {"or", 2, 2, 0, f_or},
696 {"pathshorten", 1, 1, 0, f_pathshorten},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697#ifdef FEAT_PERL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200698 {"perleval", 1, 1, 0, f_perleval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200700#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200701 {"popup_atcursor", 2, 2, 0, f_popup_atcursor},
702 {"popup_beval", 2, 2, 0, f_popup_beval},
703 {"popup_clear", 0, 0, 0, f_popup_clear},
704 {"popup_close", 1, 2, 0, f_popup_close},
705 {"popup_create", 2, 2, 0, f_popup_create},
706 {"popup_dialog", 2, 2, 0, f_popup_dialog},
707 {"popup_filter_menu", 2, 2, 0, f_popup_filter_menu},
708 {"popup_filter_yesno", 2, 2, 0, f_popup_filter_yesno},
709 {"popup_getoptions", 1, 1, 0, f_popup_getoptions},
710 {"popup_getpos", 1, 1, 0, f_popup_getpos},
711 {"popup_getpreview", 0, 0, 0, f_popup_getpreview},
712 {"popup_hide", 1, 1, 0, f_popup_hide},
713 {"popup_locate", 2, 2, 0, f_popup_locate},
714 {"popup_menu", 2, 2, 0, f_popup_menu},
715 {"popup_move", 2, 2, 0, f_popup_move},
716 {"popup_notification", 2, 2, 0, f_popup_notification},
717 {"popup_setoptions", 2, 2, 0, f_popup_setoptions},
718 {"popup_settext", 2, 2, 0, f_popup_settext},
719 {"popup_show", 1, 1, 0, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200720#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200721#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200722 {"pow", 2, 2, FEARG_1, f_pow},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200723#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200724 {"prevnonblank", 1, 1, 0, f_prevnonblank},
Bram Moolenaarfd8ca212019-08-10 00:13:30 +0200725 {"printf", 1, 19, FEARG_2, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200727 {"prompt_setcallback", 2, 2, 0, f_prompt_setcallback},
728 {"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt},
729 {"prompt_setprompt", 2, 2, 0, f_prompt_setprompt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200730#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100731#ifdef FEAT_TEXT_PROP
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200732 {"prop_add", 3, 3, 0, f_prop_add},
733 {"prop_clear", 1, 3, 0, f_prop_clear},
734 {"prop_list", 1, 2, 0, f_prop_list},
735 {"prop_remove", 1, 3, 0, f_prop_remove},
736 {"prop_type_add", 2, 2, 0, f_prop_type_add},
737 {"prop_type_change", 2, 2, 0, f_prop_type_change},
738 {"prop_type_delete", 1, 2, 0, f_prop_type_delete},
739 {"prop_type_get", 1, 2, 0, f_prop_type_get},
740 {"prop_type_list", 0, 1, 0, f_prop_type_list},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100741#endif
Bram Moolenaare9bd5722019-08-17 19:36:06 +0200742 {"pum_getpos", 0, 0, 0, f_pum_getpos},
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200743 {"pumvisible", 0, 0, 0, f_pumvisible},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744#ifdef FEAT_PYTHON3
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200745 {"py3eval", 1, 1, 0, f_py3eval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746#endif
747#ifdef FEAT_PYTHON
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200748 {"pyeval", 1, 1, 0, f_pyeval},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200749#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100750#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200751 {"pyxeval", 1, 1, 0, f_pyxeval},
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100752#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200753 {"range", 1, 3, 0, f_range},
754 {"readdir", 1, 2, 0, f_readdir},
755 {"readfile", 1, 3, 0, f_readfile},
756 {"reg_executing", 0, 0, 0, f_reg_executing},
757 {"reg_recording", 0, 0, 0, f_reg_recording},
758 {"reltime", 0, 2, 0, f_reltime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759#ifdef FEAT_FLOAT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200760 {"reltimefloat", 1, 1, 0, f_reltimefloat},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200761#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200762 {"reltimestr", 1, 1, 0, f_reltimestr},
763 {"remote_expr", 2, 4, 0, f_remote_expr},
764 {"remote_foreground", 1, 1, 0, f_remote_foreground},
765 {"remote_peek", 1, 2, 0, f_remote_peek},
766 {"remote_read", 1, 2, 0, f_remote_read},
767 {"remote_send", 2, 3, 0, f_remote_send},
768 {"remote_startserver", 1, 1, 0, f_remote_startserver},
769 {"remove", 2, 3, FEARG_1, f_remove},
770 {"rename", 2, 2, 0, f_rename},
771 {"repeat", 2, 2, FEARG_1, f_repeat},
772 {"resolve", 1, 1, 0, f_resolve},
773 {"reverse", 1, 1, FEARG_1, f_reverse},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200775 {"round", 1, 1, FEARG_1, f_round},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100777#ifdef FEAT_RUBY
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200778 {"rubyeval", 1, 1, 0, f_rubyeval},
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100779#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200780 {"screenattr", 2, 2, 0, f_screenattr},
781 {"screenchar", 2, 2, 0, f_screenchar},
782 {"screenchars", 2, 2, 0, f_screenchars},
783 {"screencol", 0, 0, 0, f_screencol},
784 {"screenpos", 3, 3, 0, f_screenpos},
785 {"screenrow", 0, 0, 0, f_screenrow},
786 {"screenstring", 2, 2, 0, f_screenstring},
787 {"search", 1, 4, 0, f_search},
788 {"searchdecl", 1, 3, 0, f_searchdecl},
789 {"searchpair", 3, 7, 0, f_searchpair},
790 {"searchpairpos", 3, 7, 0, f_searchpairpos},
791 {"searchpos", 1, 4, 0, f_searchpos},
792 {"server2client", 2, 2, 0, f_server2client},
793 {"serverlist", 0, 0, 0, f_serverlist},
794 {"setbufline", 3, 3, 0, f_setbufline},
795 {"setbufvar", 3, 3, 0, f_setbufvar},
796 {"setcharsearch", 1, 1, 0, f_setcharsearch},
797 {"setcmdpos", 1, 1, 0, f_setcmdpos},
798 {"setenv", 2, 2, 0, f_setenv},
799 {"setfperm", 2, 2, 0, f_setfperm},
800 {"setline", 2, 2, 0, f_setline},
801 {"setloclist", 2, 4, 0, f_setloclist},
802 {"setmatches", 1, 2, 0, f_setmatches},
803 {"setpos", 2, 2, 0, f_setpos},
804 {"setqflist", 1, 3, 0, f_setqflist},
805 {"setreg", 2, 3, 0, f_setreg},
806 {"settabvar", 3, 3, 0, f_settabvar},
807 {"settabwinvar", 4, 4, 0, f_settabwinvar},
808 {"settagstack", 2, 3, 0, f_settagstack},
809 {"setwinvar", 3, 3, 0, f_setwinvar},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200810#ifdef FEAT_CRYPT
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200811 {"sha256", 1, 1, 0, f_sha256},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200813 {"shellescape", 1, 2, 0, f_shellescape},
814 {"shiftwidth", 0, 1, 0, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100815#ifdef FEAT_SIGNS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200816 {"sign_define", 1, 2, 0, f_sign_define},
817 {"sign_getdefined", 0, 1, 0, f_sign_getdefined},
818 {"sign_getplaced", 0, 2, 0, f_sign_getplaced},
819 {"sign_jump", 3, 3, 0, f_sign_jump},
820 {"sign_place", 4, 5, 0, f_sign_place},
821 {"sign_placelist", 1, 1, 0, f_sign_placelist},
822 {"sign_undefine", 0, 1, 0, f_sign_undefine},
823 {"sign_unplace", 1, 2, 0, f_sign_unplace},
824 {"sign_unplacelist", 1, 2, 0, f_sign_unplacelist},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100825#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200826 {"simplify", 1, 1, 0, f_simplify},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200828 {"sin", 1, 1, FEARG_1, f_sin},
829 {"sinh", 1, 1, FEARG_1, f_sinh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200831 {"sort", 1, 3, FEARG_1, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200832#ifdef FEAT_SOUND
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200833 {"sound_clear", 0, 0, 0, f_sound_clear},
834 {"sound_playevent", 1, 2, 0, f_sound_playevent},
835 {"sound_playfile", 1, 2, 0, f_sound_playfile},
836 {"sound_stop", 1, 1, 0, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200837#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200838 {"soundfold", 1, 1, 0, f_soundfold},
839 {"spellbadword", 0, 1, 0, f_spellbadword},
840 {"spellsuggest", 1, 3, 0, f_spellsuggest},
841 {"split", 1, 3, FEARG_1, f_split},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200843 {"sqrt", 1, 1, FEARG_1, f_sqrt},
844 {"str2float", 1, 1, FEARG_1, f_str2float},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200846 {"str2list", 1, 2, FEARG_1, f_str2list},
847 {"str2nr", 1, 2, 0, f_str2nr},
848 {"strcharpart", 2, 3, 0, f_strcharpart},
849 {"strchars", 1, 2, 0, f_strchars},
850 {"strdisplaywidth", 1, 2, 0, f_strdisplaywidth},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851#ifdef HAVE_STRFTIME
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200852 {"strftime", 1, 2, 0, f_strftime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200853#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200854 {"strgetchar", 2, 2, 0, f_strgetchar},
855 {"stridx", 2, 3, 0, f_stridx},
856 {"string", 1, 1, FEARG_1, f_string},
857 {"strlen", 1, 1, FEARG_1, f_strlen},
858 {"strpart", 2, 3, 0, f_strpart},
859 {"strridx", 2, 3, 0, f_strridx},
860 {"strtrans", 1, 1, FEARG_1, f_strtrans},
861 {"strwidth", 1, 1, FEARG_1, f_strwidth},
862 {"submatch", 1, 2, 0, f_submatch},
863 {"substitute", 4, 4, FEARG_1, f_substitute},
864 {"swapinfo", 1, 1, 0, f_swapinfo},
865 {"swapname", 1, 1, 0, f_swapname},
866 {"synID", 3, 3, 0, f_synID},
867 {"synIDattr", 2, 3, FEARG_1, f_synIDattr},
868 {"synIDtrans", 1, 1, FEARG_1, f_synIDtrans},
869 {"synconcealed", 2, 2, 0, f_synconcealed},
870 {"synstack", 2, 2, 0, f_synstack},
871 {"system", 1, 2, FEARG_1, f_system},
872 {"systemlist", 1, 2, FEARG_1, f_systemlist},
873 {"tabpagebuflist", 0, 1, 0, f_tabpagebuflist},
874 {"tabpagenr", 0, 1, 0, f_tabpagenr},
875 {"tabpagewinnr", 1, 2, 0, f_tabpagewinnr},
876 {"tagfiles", 0, 0, 0, f_tagfiles},
877 {"taglist", 1, 2, 0, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200879 {"tan", 1, 1, FEARG_1, f_tan},
880 {"tanh", 1, 1, FEARG_1, f_tanh},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200882 {"tempname", 0, 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200883#ifdef FEAT_TERMINAL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200884 {"term_dumpdiff", 2, 3, 0, f_term_dumpdiff},
885 {"term_dumpload", 1, 2, 0, f_term_dumpload},
886 {"term_dumpwrite", 2, 3, 0, f_term_dumpwrite},
887 {"term_getaltscreen", 1, 1, 0, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200888# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200889 {"term_getansicolors", 1, 1, 0, f_term_getansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200890# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200891 {"term_getattr", 2, 2, 0, f_term_getattr},
892 {"term_getcursor", 1, 1, 0, f_term_getcursor},
893 {"term_getjob", 1, 1, 0, f_term_getjob},
894 {"term_getline", 2, 2, 0, f_term_getline},
895 {"term_getscrolled", 1, 1, 0, f_term_getscrolled},
896 {"term_getsize", 1, 1, 0, f_term_getsize},
897 {"term_getstatus", 1, 1, 0, f_term_getstatus},
898 {"term_gettitle", 1, 1, 0, f_term_gettitle},
899 {"term_gettty", 1, 2, 0, f_term_gettty},
900 {"term_list", 0, 0, 0, f_term_list},
901 {"term_scrape", 2, 2, 0, f_term_scrape},
902 {"term_sendkeys", 2, 2, 0, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200903# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200904 {"term_setansicolors", 2, 2, 0, f_term_setansicolors},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200905# endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200906 {"term_setkill", 2, 2, 0, f_term_setkill},
907 {"term_setrestore", 2, 2, 0, f_term_setrestore},
908 {"term_setsize", 3, 3, 0, f_term_setsize},
909 {"term_start", 1, 2, 0, f_term_start},
910 {"term_wait", 1, 2, 0, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200911#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200912 {"test_alloc_fail", 3, 3, 0, f_test_alloc_fail},
913 {"test_autochdir", 0, 0, 0, f_test_autochdir},
914 {"test_feedinput", 1, 1, 0, f_test_feedinput},
915 {"test_garbagecollect_now", 0, 0, 0, f_test_garbagecollect_now},
916 {"test_garbagecollect_soon", 0, 0, 0, f_test_garbagecollect_soon},
917 {"test_getvalue", 1, 1, 0, f_test_getvalue},
918 {"test_ignore_error", 1, 1, 0, f_test_ignore_error},
919 {"test_null_blob", 0, 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200920#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200921 {"test_null_channel", 0, 0, 0, f_test_null_channel},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200922#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200923 {"test_null_dict", 0, 0, 0, f_test_null_dict},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200925 {"test_null_job", 0, 0, 0, f_test_null_job},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200927 {"test_null_list", 0, 0, 0, f_test_null_list},
928 {"test_null_partial", 0, 0, 0, f_test_null_partial},
929 {"test_null_string", 0, 0, 0, f_test_null_string},
930 {"test_option_not_set", 1, 1, 0, f_test_option_not_set},
931 {"test_override", 2, 2, 0, f_test_override},
932 {"test_refcount", 1, 1, 0, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +0200933#ifdef FEAT_GUI
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200934 {"test_scrollbar", 3, 3, 0, f_test_scrollbar},
Bram Moolenaarab186732018-09-14 21:27:06 +0200935#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200936#ifdef FEAT_MOUSE
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200937 {"test_setmouse", 2, 2, 0, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200938#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200939 {"test_settime", 1, 1, 0, f_test_settime},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940#ifdef FEAT_TIMERS
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200941 {"timer_info", 0, 1, 0, f_timer_info},
942 {"timer_pause", 2, 2, 0, f_timer_pause},
943 {"timer_start", 2, 3, 0, f_timer_start},
944 {"timer_stop", 1, 1, 0, f_timer_stop},
945 {"timer_stopall", 0, 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200947 {"tolower", 1, 1, 0, f_tolower},
948 {"toupper", 1, 1, 0, f_toupper},
949 {"tr", 3, 3, 0, f_tr},
950 {"trim", 1, 2, 0, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200951#ifdef FEAT_FLOAT
Bram Moolenaar93cf85f2019-08-17 21:36:28 +0200952 {"trunc", 1, 1, FEARG_1, f_trunc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953#endif
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +0200954 {"type", 1, 1, FEARG_1, f_type},
955 {"undofile", 1, 1, 0, f_undofile},
956 {"undotree", 0, 0, 0, f_undotree},
957 {"uniq", 1, 3, FEARG_1, f_uniq},
958 {"values", 1, 1, FEARG_1, f_values},
959 {"virtcol", 1, 1, 0, f_virtcol},
960 {"visualmode", 0, 1, 0, f_visualmode},
961 {"wildmenumode", 0, 0, 0, f_wildmenumode},
962 {"win_execute", 2, 3, 0, f_win_execute},
963 {"win_findbuf", 1, 1, 0, f_win_findbuf},
964 {"win_getid", 0, 2, 0, f_win_getid},
965 {"win_gotoid", 1, 1, 0, f_win_gotoid},
966 {"win_id2tabwin", 1, 1, 0, f_win_id2tabwin},
967 {"win_id2win", 1, 1, 0, f_win_id2win},
968 {"win_screenpos", 1, 1, 0, f_win_screenpos},
969 {"winbufnr", 1, 1, 0, f_winbufnr},
970 {"wincol", 0, 0, 0, f_wincol},
971 {"winheight", 1, 1, 0, f_winheight},
972 {"winlayout", 0, 1, 0, f_winlayout},
973 {"winline", 0, 0, 0, f_winline},
974 {"winnr", 0, 1, 0, f_winnr},
975 {"winrestcmd", 0, 0, 0, f_winrestcmd},
976 {"winrestview", 1, 1, 0, f_winrestview},
977 {"winsaveview", 0, 0, 0, f_winsaveview},
978 {"winwidth", 1, 1, 0, f_winwidth},
979 {"wordcount", 0, 0, 0, f_wordcount},
980 {"writefile", 2, 3, 0, f_writefile},
981 {"xor", 2, 2, 0, f_xor},
Bram Moolenaarac92e252019-08-03 21:58:38 +0200982};
983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
985
986/*
987 * Function given to ExpandGeneric() to obtain the list of internal
988 * or user defined function names.
989 */
990 char_u *
991get_function_name(expand_T *xp, int idx)
992{
993 static int intidx = -1;
994 char_u *name;
995
996 if (idx == 0)
997 intidx = -1;
998 if (intidx < 0)
999 {
1000 name = get_user_func_name(xp, idx);
1001 if (name != NULL)
1002 return name;
1003 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001004 if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001005 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02001006 STRCPY(IObuff, global_functions[intidx].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001007 STRCAT(IObuff, "(");
Bram Moolenaarac92e252019-08-03 21:58:38 +02001008 if (global_functions[intidx].f_max_argc == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009 STRCAT(IObuff, ")");
1010 return IObuff;
1011 }
1012
1013 return NULL;
1014}
1015
1016/*
1017 * Function given to ExpandGeneric() to obtain the list of internal or
1018 * user defined variable or function names.
1019 */
1020 char_u *
1021get_expr_name(expand_T *xp, int idx)
1022{
1023 static int intidx = -1;
1024 char_u *name;
1025
1026 if (idx == 0)
1027 intidx = -1;
1028 if (intidx < 0)
1029 {
1030 name = get_function_name(xp, idx);
1031 if (name != NULL)
1032 return name;
1033 }
1034 return get_user_var_name(xp, ++intidx);
1035}
1036
1037#endif /* FEAT_CMDL_COMPL */
1038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001039/*
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001040 * Find internal function "name" in table "global_functions".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001041 * Return index, or -1 if not found
1042 */
Bram Moolenaarac92e252019-08-03 21:58:38 +02001043 static int
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001044find_internal_func(char_u *name)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001045{
1046 int first = 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001047 int last;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048 int cmp;
1049 int x;
1050
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001051 last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001052
1053 // Find the function name in the table. Binary search.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001054 while (first <= last)
1055 {
1056 x = first + ((unsigned)(last - first) >> 1);
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001057 cmp = STRCMP(name, global_functions[x].f_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058 if (cmp < 0)
1059 last = x - 1;
1060 else if (cmp > 0)
1061 first = x + 1;
1062 else
1063 return x;
1064 }
1065 return -1;
1066}
1067
1068 int
Bram Moolenaarac92e252019-08-03 21:58:38 +02001069has_internal_func(char_u *name)
1070{
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001071 return find_internal_func(name) >= 0;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001072}
1073
1074 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001075call_internal_func(
1076 char_u *name,
1077 int argcount,
1078 typval_T *argvars,
1079 typval_T *rettv)
1080{
1081 int i;
1082
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001083 i = find_internal_func(name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001084 if (i < 0)
1085 return ERROR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001086 if (argcount < global_functions[i].f_min_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001087 return ERROR_TOOFEW;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001088 if (argcount > global_functions[i].f_max_argc)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001089 return ERROR_TOOMANY;
1090 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02001091 global_functions[i].f_func(argvars, rettv);
1092 return ERROR_NONE;
1093}
1094
1095/*
1096 * Invoke a method for base->method().
1097 */
1098 int
1099call_internal_method(
1100 char_u *name,
1101 int argcount,
1102 typval_T *argvars,
1103 typval_T *rettv,
1104 typval_T *basetv)
1105{
1106 int i;
1107 int fi;
1108 typval_T argv[MAX_FUNC_ARGS + 1];
1109
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001110 fi = find_internal_func(name);
Bram Moolenaar91746392019-08-16 22:22:31 +02001111 if (fi < 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001112 return ERROR_UNKNOWN;
Bram Moolenaar91746392019-08-16 22:22:31 +02001113 if (global_functions[fi].f_argtype == 0)
1114 return ERROR_NOTMETHOD;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001115 if (argcount + 1 < global_functions[fi].f_min_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001116 return ERROR_TOOFEW;
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001117 if (argcount + 1 > global_functions[fi].f_max_argc)
Bram Moolenaarac92e252019-08-03 21:58:38 +02001118 return ERROR_TOOMANY;
1119
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001120 if (global_functions[fi].f_argtype == FEARG_LAST)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001121 {
1122 // base value goes last
1123 for (i = 0; i < argcount; ++i)
1124 argv[i] = argvars[i];
1125 argv[argcount] = *basetv;
1126 }
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001127 else if (global_functions[fi].f_argtype == FEARG_2)
Bram Moolenaar25e42232019-08-04 15:04:10 +02001128 {
1129 // base value goes second
1130 argv[0] = argvars[0];
1131 argv[1] = *basetv;
1132 for (i = 1; i < argcount; ++i)
1133 argv[i + 1] = argvars[i];
1134 }
Bram Moolenaar24278d22019-08-16 21:49:22 +02001135 else if (global_functions[fi].f_argtype == FEARG_3)
1136 {
1137 // base value goes third
1138 argv[0] = argvars[0];
1139 argv[1] = argvars[1];
1140 argv[2] = *basetv;
1141 for (i = 2; i < argcount; ++i)
1142 argv[i + 1] = argvars[i];
1143 }
Bram Moolenaar25e42232019-08-04 15:04:10 +02001144 else
1145 {
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001146 // FEARG_1: base value goes first
Bram Moolenaar25e42232019-08-04 15:04:10 +02001147 argv[0] = *basetv;
1148 for (i = 0; i < argcount; ++i)
1149 argv[i + 1] = argvars[i];
1150 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001151 argv[argcount + 1].v_type = VAR_UNKNOWN;
1152
Bram Moolenaar7a4ea1d2019-08-04 21:35:12 +02001153 global_functions[fi].f_func(argv, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001154 return ERROR_NONE;
1155}
1156
1157/*
1158 * Return TRUE for a non-zero Number and a non-empty String.
1159 */
1160 static int
1161non_zero_arg(typval_T *argvars)
1162{
1163 return ((argvars[0].v_type == VAR_NUMBER
1164 && argvars[0].vval.v_number != 0)
1165 || (argvars[0].v_type == VAR_SPECIAL
1166 && argvars[0].vval.v_number == VVAL_TRUE)
1167 || (argvars[0].v_type == VAR_STRING
1168 && argvars[0].vval.v_string != NULL
1169 && *argvars[0].vval.v_string != NUL));
1170}
1171
1172/*
1173 * Get the lnum from the first argument.
1174 * Also accepts ".", "$", etc., but that only works for the current buffer.
1175 * Returns -1 on error.
1176 */
Bram Moolenaarb60d8512019-06-29 07:59:04 +02001177 linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001179{
1180 typval_T rettv;
1181 linenr_T lnum;
1182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001184 if (lnum == 0) /* no valid number, try using line() */
1185 {
1186 rettv.v_type = VAR_NUMBER;
1187 f_line(argvars, &rettv);
1188 lnum = (linenr_T)rettv.vval.v_number;
1189 clear_tv(&rettv);
1190 }
1191 return lnum;
1192}
1193
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001194/*
1195 * Get the lnum from the first argument.
1196 * Also accepts "$", then "buf" is used.
1197 * Returns 0 on error.
1198 */
1199 static linenr_T
1200tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1201{
1202 if (argvars[0].v_type == VAR_STRING
1203 && argvars[0].vval.v_string != NULL
1204 && argvars[0].vval.v_string[0] == '$'
1205 && buf != NULL)
1206 return buf->b_ml.ml_line_count;
1207 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1208}
1209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001210#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211/*
1212 * Get the float value of "argvars[0]" into "f".
1213 * Returns FAIL when the argument is not a Number or Float.
1214 */
1215 static int
1216get_float_arg(typval_T *argvars, float_T *f)
1217{
1218 if (argvars[0].v_type == VAR_FLOAT)
1219 {
1220 *f = argvars[0].vval.v_float;
1221 return OK;
1222 }
1223 if (argvars[0].v_type == VAR_NUMBER)
1224 {
1225 *f = (float_T)argvars[0].vval.v_number;
1226 return OK;
1227 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229 return FAIL;
1230}
1231
1232/*
1233 * "abs(expr)" function
1234 */
1235 static void
1236f_abs(typval_T *argvars, typval_T *rettv)
1237{
1238 if (argvars[0].v_type == VAR_FLOAT)
1239 {
1240 rettv->v_type = VAR_FLOAT;
1241 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1242 }
1243 else
1244 {
1245 varnumber_T n;
1246 int error = FALSE;
1247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001248 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249 if (error)
1250 rettv->vval.v_number = -1;
1251 else if (n > 0)
1252 rettv->vval.v_number = n;
1253 else
1254 rettv->vval.v_number = -n;
1255 }
1256}
1257
1258/*
1259 * "acos()" function
1260 */
1261 static void
1262f_acos(typval_T *argvars, typval_T *rettv)
1263{
1264 float_T f = 0.0;
1265
1266 rettv->v_type = VAR_FLOAT;
1267 if (get_float_arg(argvars, &f) == OK)
1268 rettv->vval.v_float = acos(f);
1269 else
1270 rettv->vval.v_float = 0.0;
1271}
1272#endif
1273
1274/*
1275 * "add(list, item)" function
1276 */
1277 static void
1278f_add(typval_T *argvars, typval_T *rettv)
1279{
1280 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001281 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282
1283 rettv->vval.v_number = 1; /* Default: Failed */
1284 if (argvars[0].v_type == VAR_LIST)
1285 {
1286 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001287 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001288 (char_u *)N_("add() argument"), TRUE)
1289 && list_append_tv(l, &argvars[1]) == OK)
1290 copy_tv(&argvars[0], rettv);
1291 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 else if (argvars[0].v_type == VAR_BLOB)
1293 {
1294 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001295 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 (char_u *)N_("add() argument"), TRUE))
1297 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001298 int error = FALSE;
1299 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1300
1301 if (!error)
1302 {
1303 ga_append(&b->bv_ga, (int)n);
1304 copy_tv(&argvars[0], rettv);
1305 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 }
1307 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001309 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310}
1311
1312/*
1313 * "and(expr, expr)" function
1314 */
1315 static void
1316f_and(typval_T *argvars, typval_T *rettv)
1317{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1319 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001320}
1321
1322/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001323 * If there is a window for "curbuf", make it the current window.
1324 */
1325 static void
1326find_win_for_curbuf(void)
1327{
1328 wininfo_T *wip;
1329
1330 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1331 {
1332 if (wip->wi_win != NULL)
1333 {
1334 curwin = wip->wi_win;
1335 break;
1336 }
1337 }
1338}
1339
1340/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001341 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342 */
1343 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001344set_buffer_lines(
1345 buf_T *buf,
1346 linenr_T lnum_arg,
1347 int append,
1348 typval_T *lines,
1349 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001350{
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1352 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001353 list_T *l = NULL;
1354 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001356 linenr_T append_lnum;
1357 buf_T *curbuf_save = NULL;
1358 win_T *curwin_save = NULL;
1359 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 /* When using the current buffer ml_mfp will be set if needed. Useful when
1362 * setline() is used on startup. For other buffers the buffer must be
1363 * loaded. */
1364 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001366 rettv->vval.v_number = 1; /* FAIL */
1367 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368 }
1369
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 curbuf_save = curbuf;
1373 curwin_save = curwin;
1374 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001375 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 }
1377
1378 if (append)
1379 // appendbufline() uses the line number below which we insert
1380 append_lnum = lnum - 1;
1381 else
1382 // setbufline() uses the line number above which we insert, we only
1383 // append if it's below the last line
1384 append_lnum = curbuf->b_ml.ml_line_count;
1385
1386 if (lines->v_type == VAR_LIST)
1387 {
1388 l = lines->vval.v_list;
1389 li = l->lv_first;
1390 }
1391 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001393
1394 /* default result is zero == OK */
1395 for (;;)
1396 {
1397 if (l != NULL)
1398 {
1399 /* list argument, get next string */
1400 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 li = li->li_next;
1404 }
1405
Bram Moolenaarca851592018-06-06 21:04:07 +02001406 rettv->vval.v_number = 1; /* FAIL */
1407 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1408 break;
1409
1410 /* When coming here from Insert mode, sync undo, so that this can be
1411 * undone separately from what was previously inserted. */
1412 if (u_sync_once == 2)
1413 {
1414 u_sync_once = 1; /* notify that u_sync() was called */
1415 u_sync(TRUE);
1416 }
1417
1418 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1419 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001420 // Existing line, replace it.
1421 // Removes any existing text properties.
1422 if (u_savesub(lnum) == OK && ml_replace_len(
1423 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001424 {
1425 changed_bytes(lnum, 0);
1426 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1427 check_cursor_col();
1428 rettv->vval.v_number = 0; /* OK */
1429 }
1430 }
1431 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1432 {
1433 /* append the line */
1434 ++added;
1435 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1436 rettv->vval.v_number = 0; /* OK */
1437 }
1438
1439 if (l == NULL) /* only one string argument */
1440 break;
1441 ++lnum;
1442 }
1443
1444 if (added > 0)
1445 {
1446 win_T *wp;
1447 tabpage_T *tp;
1448
1449 appended_lines_mark(append_lnum, added);
1450 FOR_ALL_TAB_WINDOWS(tp, wp)
1451 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1452 wp->w_cursor.lnum += added;
1453 check_cursor_col();
Bram Moolenaar29846662019-07-27 17:39:15 +02001454 update_topline();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001455 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001456
1457 if (!is_curbuf)
1458 {
1459 curbuf = curbuf_save;
1460 curwin = curwin_save;
1461 }
1462}
1463
1464/*
1465 * "append(lnum, string/list)" function
1466 */
1467 static void
1468f_append(typval_T *argvars, typval_T *rettv)
1469{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001470 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001471
1472 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1473}
1474
1475/*
1476 * "appendbufline(buf, lnum, string/list)" function
1477 */
1478 static void
1479f_appendbufline(typval_T *argvars, typval_T *rettv)
1480{
1481 linenr_T lnum;
1482 buf_T *buf;
1483
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001484 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001485 if (buf == NULL)
1486 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001489 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001490 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1491 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492}
1493
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494#ifdef FEAT_FLOAT
1495/*
1496 * "asin()" function
1497 */
1498 static void
1499f_asin(typval_T *argvars, typval_T *rettv)
1500{
1501 float_T f = 0.0;
1502
1503 rettv->v_type = VAR_FLOAT;
1504 if (get_float_arg(argvars, &f) == OK)
1505 rettv->vval.v_float = asin(f);
1506 else
1507 rettv->vval.v_float = 0.0;
1508}
1509
1510/*
1511 * "atan()" function
1512 */
1513 static void
1514f_atan(typval_T *argvars, typval_T *rettv)
1515{
1516 float_T f = 0.0;
1517
1518 rettv->v_type = VAR_FLOAT;
1519 if (get_float_arg(argvars, &f) == OK)
1520 rettv->vval.v_float = atan(f);
1521 else
1522 rettv->vval.v_float = 0.0;
1523}
1524
1525/*
1526 * "atan2()" function
1527 */
1528 static void
1529f_atan2(typval_T *argvars, typval_T *rettv)
1530{
1531 float_T fx = 0.0, fy = 0.0;
1532
1533 rettv->v_type = VAR_FLOAT;
1534 if (get_float_arg(argvars, &fx) == OK
1535 && get_float_arg(&argvars[1], &fy) == OK)
1536 rettv->vval.v_float = atan2(fx, fy);
1537 else
1538 rettv->vval.v_float = 0.0;
1539}
1540#endif
1541
1542/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001543 * "balloon_show()" function
1544 */
1545#ifdef FEAT_BEVAL
1546 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001547f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1548{
1549 rettv->v_type = VAR_STRING;
1550 if (balloonEval != NULL)
1551 {
1552 if (balloonEval->msg == NULL)
1553 rettv->vval.v_string = NULL;
1554 else
1555 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1556 }
1557}
1558
1559 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001560f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1561{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001562 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001563 {
1564 if (argvars[0].v_type == VAR_LIST
1565# ifdef FEAT_GUI
1566 && !gui.in_use
1567# endif
1568 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001569 {
1570 list_T *l = argvars[0].vval.v_list;
1571
1572 // empty list removes the balloon
1573 post_balloon(balloonEval, NULL,
1574 l == NULL || l->lv_len == 0 ? NULL : l);
1575 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001576 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001577 {
1578 char_u *mesg = tv_get_string_chk(&argvars[0]);
1579
1580 if (mesg != NULL)
1581 // empty string removes the balloon
1582 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1583 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001584 }
1585}
1586
Bram Moolenaar669a8282017-11-19 20:13:05 +01001587# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001588 static void
1589f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1590{
1591 if (rettv_list_alloc(rettv) == OK)
1592 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001594
1595 if (msg != NULL)
1596 {
1597 pumitem_T *array;
1598 int size = split_message(msg, &array);
1599 int i;
1600
1601 /* Skip the first and last item, they are always empty. */
1602 for (i = 1; i < size - 1; ++i)
1603 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001604 while (size > 0)
1605 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001606 vim_free(array);
1607 }
1608 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001609}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001610# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001611#endif
1612
1613/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 * "browse(save, title, initdir, default)" function
1615 */
1616 static void
1617f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1618{
1619#ifdef FEAT_BROWSE
1620 int save;
1621 char_u *title;
1622 char_u *initdir;
1623 char_u *defname;
1624 char_u buf[NUMBUFLEN];
1625 char_u buf2[NUMBUFLEN];
1626 int error = FALSE;
1627
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001628 save = (int)tv_get_number_chk(&argvars[0], &error);
1629 title = tv_get_string_chk(&argvars[1]);
1630 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1631 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632
1633 if (error || title == NULL || initdir == NULL || defname == NULL)
1634 rettv->vval.v_string = NULL;
1635 else
1636 rettv->vval.v_string =
1637 do_browse(save ? BROWSE_SAVE : 0,
1638 title, defname, NULL, initdir, NULL, curbuf);
1639#else
1640 rettv->vval.v_string = NULL;
1641#endif
1642 rettv->v_type = VAR_STRING;
1643}
1644
1645/*
1646 * "browsedir(title, initdir)" function
1647 */
1648 static void
1649f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1650{
1651#ifdef FEAT_BROWSE
1652 char_u *title;
1653 char_u *initdir;
1654 char_u buf[NUMBUFLEN];
1655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001656 title = tv_get_string_chk(&argvars[0]);
1657 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658
1659 if (title == NULL || initdir == NULL)
1660 rettv->vval.v_string = NULL;
1661 else
1662 rettv->vval.v_string = do_browse(BROWSE_DIR,
1663 title, NULL, NULL, initdir, NULL, curbuf);
1664#else
1665 rettv->vval.v_string = NULL;
1666#endif
1667 rettv->v_type = VAR_STRING;
1668}
1669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670/*
1671 * Find a buffer by number or exact name.
1672 */
1673 static buf_T *
1674find_buffer(typval_T *avar)
1675{
1676 buf_T *buf = NULL;
1677
1678 if (avar->v_type == VAR_NUMBER)
1679 buf = buflist_findnr((int)avar->vval.v_number);
1680 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1681 {
1682 buf = buflist_findname_exp(avar->vval.v_string);
1683 if (buf == NULL)
1684 {
1685 /* No full path name match, try a match with a URL or a "nofile"
1686 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001687 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688 if (buf->b_fname != NULL
1689 && (path_with_url(buf->b_fname)
1690#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001691 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692#endif
1693 )
1694 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1695 break;
1696 }
1697 }
1698 return buf;
1699}
1700
1701/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001702 * "bufadd(expr)" function
1703 */
1704 static void
1705f_bufadd(typval_T *argvars, typval_T *rettv)
1706{
Bram Moolenaar892ae722019-06-30 20:33:01 +02001707 char_u *name = tv_get_string(&argvars[0]);
1708
1709 rettv->vval.v_number = buflist_add(*name == NUL ? NULL : name, 0);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001710}
1711
1712/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713 * "bufexists(expr)" function
1714 */
1715 static void
1716f_bufexists(typval_T *argvars, typval_T *rettv)
1717{
1718 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1719}
1720
1721/*
1722 * "buflisted(expr)" function
1723 */
1724 static void
1725f_buflisted(typval_T *argvars, typval_T *rettv)
1726{
1727 buf_T *buf;
1728
1729 buf = find_buffer(&argvars[0]);
1730 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1731}
1732
1733/*
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001734 * "bufload(expr)" function
1735 */
1736 static void
1737f_bufload(typval_T *argvars, typval_T *rettv UNUSED)
1738{
1739 buf_T *buf = get_buf_arg(&argvars[0]);
1740
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001741 if (buf != NULL)
1742 buffer_ensure_loaded(buf);
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001743}
1744
1745/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746 * "bufloaded(expr)" function
1747 */
1748 static void
1749f_bufloaded(typval_T *argvars, typval_T *rettv)
1750{
1751 buf_T *buf;
1752
1753 buf = find_buffer(&argvars[0]);
1754 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1755}
1756
1757 buf_T *
1758buflist_find_by_name(char_u *name, int curtab_only)
1759{
1760 int save_magic;
1761 char_u *save_cpo;
1762 buf_T *buf;
1763
1764 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1765 save_magic = p_magic;
1766 p_magic = TRUE;
1767 save_cpo = p_cpo;
1768 p_cpo = (char_u *)"";
1769
1770 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1771 TRUE, FALSE, curtab_only));
1772
1773 p_magic = save_magic;
1774 p_cpo = save_cpo;
1775 return buf;
1776}
1777
1778/*
1779 * Get buffer by number or pattern.
1780 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001781 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001782tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001783{
1784 char_u *name = tv->vval.v_string;
1785 buf_T *buf;
1786
1787 if (tv->v_type == VAR_NUMBER)
1788 return buflist_findnr((int)tv->vval.v_number);
1789 if (tv->v_type != VAR_STRING)
1790 return NULL;
1791 if (name == NULL || *name == NUL)
1792 return curbuf;
1793 if (name[0] == '$' && name[1] == NUL)
1794 return lastbuf;
1795
1796 buf = buflist_find_by_name(name, curtab_only);
1797
1798 /* If not found, try expanding the name, like done for bufexists(). */
1799 if (buf == NULL)
1800 buf = find_buffer(tv);
1801
1802 return buf;
1803}
1804
1805/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001806 * Get the buffer from "arg" and give an error and return NULL if it is not
1807 * valid.
1808 */
Bram Moolenaara3347722019-05-11 21:14:24 +02001809 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001810get_buf_arg(typval_T *arg)
1811{
1812 buf_T *buf;
1813
1814 ++emsg_off;
1815 buf = tv_get_buf(arg, FALSE);
1816 --emsg_off;
1817 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001818 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001819 return buf;
1820}
1821
1822/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001823 * "bufname(expr)" function
1824 */
1825 static void
1826f_bufname(typval_T *argvars, typval_T *rettv)
1827{
1828 buf_T *buf;
1829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001830 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001831 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001832 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001833 rettv->v_type = VAR_STRING;
1834 if (buf != NULL && buf->b_fname != NULL)
1835 rettv->vval.v_string = vim_strsave(buf->b_fname);
1836 else
1837 rettv->vval.v_string = NULL;
1838 --emsg_off;
1839}
1840
1841/*
1842 * "bufnr(expr)" function
1843 */
1844 static void
1845f_bufnr(typval_T *argvars, typval_T *rettv)
1846{
1847 buf_T *buf;
1848 int error = FALSE;
1849 char_u *name;
1850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001851 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001852 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001853 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 --emsg_off;
1855
1856 /* If the buffer isn't found and the second argument is not zero create a
1857 * new buffer. */
1858 if (buf == NULL
1859 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001860 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001861 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001862 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001863 && !error)
1864 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1865
1866 if (buf != NULL)
1867 rettv->vval.v_number = buf->b_fnum;
1868 else
1869 rettv->vval.v_number = -1;
1870}
1871
1872 static void
1873buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1874{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875 win_T *wp;
1876 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877 buf_T *buf;
1878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001879 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001880 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001881 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001882 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001883 {
1884 ++winnr;
1885 if (wp->w_buffer == buf)
1886 break;
1887 }
1888 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001889 --emsg_off;
1890}
1891
1892/*
1893 * "bufwinid(nr)" function
1894 */
1895 static void
1896f_bufwinid(typval_T *argvars, typval_T *rettv)
1897{
1898 buf_win_common(argvars, rettv, FALSE);
1899}
1900
1901/*
1902 * "bufwinnr(nr)" function
1903 */
1904 static void
1905f_bufwinnr(typval_T *argvars, typval_T *rettv)
1906{
1907 buf_win_common(argvars, rettv, TRUE);
1908}
1909
1910/*
1911 * "byte2line(byte)" function
1912 */
1913 static void
1914f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1915{
1916#ifndef FEAT_BYTEOFF
1917 rettv->vval.v_number = -1;
1918#else
1919 long boff = 0;
1920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001921 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 if (boff < 0)
1923 rettv->vval.v_number = -1;
1924 else
1925 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1926 (linenr_T)0, &boff);
1927#endif
1928}
1929
1930 static void
1931byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1932{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 char_u *str;
1935 varnumber_T idx;
1936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001937 str = tv_get_string_chk(&argvars[0]);
1938 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001939 rettv->vval.v_number = -1;
1940 if (str == NULL || idx < 0)
1941 return;
1942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943 t = str;
1944 for ( ; idx > 0; idx--)
1945 {
1946 if (*t == NUL) /* EOL reached */
1947 return;
1948 if (enc_utf8 && comp)
1949 t += utf_ptr2len(t);
1950 else
1951 t += (*mb_ptr2len)(t);
1952 }
1953 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001954}
1955
1956/*
1957 * "byteidx()" function
1958 */
1959 static void
1960f_byteidx(typval_T *argvars, typval_T *rettv)
1961{
1962 byteidx(argvars, rettv, FALSE);
1963}
1964
1965/*
1966 * "byteidxcomp()" function
1967 */
1968 static void
1969f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1970{
1971 byteidx(argvars, rettv, TRUE);
1972}
1973
1974/*
1975 * "call(func, arglist [, dict])" function
1976 */
1977 static void
1978f_call(typval_T *argvars, typval_T *rettv)
1979{
1980 char_u *func;
1981 partial_T *partial = NULL;
1982 dict_T *selfdict = NULL;
1983
1984 if (argvars[1].v_type != VAR_LIST)
1985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001987 return;
1988 }
1989 if (argvars[1].vval.v_list == NULL)
1990 return;
1991
1992 if (argvars[0].v_type == VAR_FUNC)
1993 func = argvars[0].vval.v_string;
1994 else if (argvars[0].v_type == VAR_PARTIAL)
1995 {
1996 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001997 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001998 }
1999 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002000 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002001 if (*func == NUL)
2002 return; /* type error or empty name */
2003
2004 if (argvars[2].v_type != VAR_UNKNOWN)
2005 {
2006 if (argvars[2].v_type != VAR_DICT)
2007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002009 return;
2010 }
2011 selfdict = argvars[2].vval.v_dict;
2012 }
2013
2014 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2015}
2016
2017#ifdef FEAT_FLOAT
2018/*
2019 * "ceil({float})" function
2020 */
2021 static void
2022f_ceil(typval_T *argvars, typval_T *rettv)
2023{
2024 float_T f = 0.0;
2025
2026 rettv->v_type = VAR_FLOAT;
2027 if (get_float_arg(argvars, &f) == OK)
2028 rettv->vval.v_float = ceil(f);
2029 else
2030 rettv->vval.v_float = 0.0;
2031}
2032#endif
2033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002034/*
2035 * "changenr()" function
2036 */
2037 static void
2038f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2039{
2040 rettv->vval.v_number = curbuf->b_u_seq_cur;
2041}
2042
2043/*
2044 * "char2nr(string)" function
2045 */
2046 static void
2047f_char2nr(typval_T *argvars, typval_T *rettv)
2048{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 if (has_mbyte)
2050 {
2051 int utf8 = 0;
2052
2053 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002054 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055
2056 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002057 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002059 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002060 }
2061 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002062 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063}
2064
2065/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002066 * "chdir(dir)" function
2067 */
2068 static void
2069f_chdir(typval_T *argvars, typval_T *rettv)
2070{
2071 char_u *cwd;
2072 cdscope_T scope = CDSCOPE_GLOBAL;
2073
2074 rettv->v_type = VAR_STRING;
2075 rettv->vval.v_string = NULL;
2076
2077 if (argvars[0].v_type != VAR_STRING)
2078 return;
2079
2080 // Return the current directory
2081 cwd = alloc(MAXPATHL);
2082 if (cwd != NULL)
2083 {
2084 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2085 {
2086#ifdef BACKSLASH_IN_FILENAME
2087 slash_adjust(cwd);
2088#endif
2089 rettv->vval.v_string = vim_strsave(cwd);
2090 }
2091 vim_free(cwd);
2092 }
2093
2094 if (curwin->w_localdir != NULL)
2095 scope = CDSCOPE_WINDOW;
2096 else if (curtab->tp_localdir != NULL)
2097 scope = CDSCOPE_TABPAGE;
2098
2099 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2100 // Directory change failed
2101 VIM_CLEAR(rettv->vval.v_string);
2102}
2103
2104/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 * "cindent(lnum)" function
2106 */
2107 static void
2108f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2109{
2110#ifdef FEAT_CINDENT
2111 pos_T pos;
2112 linenr_T lnum;
2113
2114 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002115 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002116 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2117 {
2118 curwin->w_cursor.lnum = lnum;
2119 rettv->vval.v_number = get_c_indent();
2120 curwin->w_cursor = pos;
2121 }
2122 else
2123#endif
2124 rettv->vval.v_number = -1;
2125}
2126
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02002127 win_T *
Bram Moolenaaraff74912019-03-30 18:11:49 +01002128get_optional_window(typval_T *argvars, int idx)
2129{
2130 win_T *win = curwin;
2131
2132 if (argvars[idx].v_type != VAR_UNKNOWN)
2133 {
2134 win = find_win_by_nr_or_id(&argvars[idx]);
2135 if (win == NULL)
2136 {
2137 emsg(_(e_invalwindow));
2138 return NULL;
2139 }
2140 }
2141 return win;
2142}
2143
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002145 * "col(string)" function
2146 */
2147 static void
2148f_col(typval_T *argvars, typval_T *rettv)
2149{
2150 colnr_T col = 0;
2151 pos_T *fp;
2152 int fnum = curbuf->b_fnum;
2153
2154 fp = var2fpos(&argvars[0], FALSE, &fnum);
2155 if (fp != NULL && fnum == curbuf->b_fnum)
2156 {
2157 if (fp->col == MAXCOL)
2158 {
2159 /* '> can be MAXCOL, get the length of the line then */
2160 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2161 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2162 else
2163 col = MAXCOL;
2164 }
2165 else
2166 {
2167 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 /* col(".") when the cursor is on the NUL at the end of the line
2169 * because of "coladd" can be seen as an extra column. */
2170 if (virtual_active() && fp == &curwin->w_cursor)
2171 {
2172 char_u *p = ml_get_cursor();
2173
2174 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2175 curwin->w_virtcol - curwin->w_cursor.coladd))
2176 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002177 int l;
2178
2179 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2180 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002181 }
2182 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002183 }
2184 }
2185 rettv->vval.v_number = col;
2186}
2187
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002188/*
2189 * "confirm(message, buttons[, default [, type]])" function
2190 */
2191 static void
2192f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2193{
2194#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2195 char_u *message;
2196 char_u *buttons = NULL;
2197 char_u buf[NUMBUFLEN];
2198 char_u buf2[NUMBUFLEN];
2199 int def = 1;
2200 int type = VIM_GENERIC;
2201 char_u *typestr;
2202 int error = FALSE;
2203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002204 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002205 if (message == NULL)
2206 error = TRUE;
2207 if (argvars[1].v_type != VAR_UNKNOWN)
2208 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002209 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002210 if (buttons == NULL)
2211 error = TRUE;
2212 if (argvars[2].v_type != VAR_UNKNOWN)
2213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002214 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002215 if (argvars[3].v_type != VAR_UNKNOWN)
2216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 if (typestr == NULL)
2219 error = TRUE;
2220 else
2221 {
2222 switch (TOUPPER_ASC(*typestr))
2223 {
2224 case 'E': type = VIM_ERROR; break;
2225 case 'Q': type = VIM_QUESTION; break;
2226 case 'I': type = VIM_INFO; break;
2227 case 'W': type = VIM_WARNING; break;
2228 case 'G': type = VIM_GENERIC; break;
2229 }
2230 }
2231 }
2232 }
2233 }
2234
2235 if (buttons == NULL || *buttons == NUL)
2236 buttons = (char_u *)_("&Ok");
2237
2238 if (!error)
2239 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2240 def, NULL, FALSE);
2241#endif
2242}
2243
2244/*
2245 * "copy()" function
2246 */
2247 static void
2248f_copy(typval_T *argvars, typval_T *rettv)
2249{
2250 item_copy(&argvars[0], rettv, FALSE, 0);
2251}
2252
2253#ifdef FEAT_FLOAT
2254/*
2255 * "cos()" function
2256 */
2257 static void
2258f_cos(typval_T *argvars, typval_T *rettv)
2259{
2260 float_T f = 0.0;
2261
2262 rettv->v_type = VAR_FLOAT;
2263 if (get_float_arg(argvars, &f) == OK)
2264 rettv->vval.v_float = cos(f);
2265 else
2266 rettv->vval.v_float = 0.0;
2267}
2268
2269/*
2270 * "cosh()" function
2271 */
2272 static void
2273f_cosh(typval_T *argvars, typval_T *rettv)
2274{
2275 float_T f = 0.0;
2276
2277 rettv->v_type = VAR_FLOAT;
2278 if (get_float_arg(argvars, &f) == OK)
2279 rettv->vval.v_float = cosh(f);
2280 else
2281 rettv->vval.v_float = 0.0;
2282}
2283#endif
2284
2285/*
2286 * "count()" function
2287 */
2288 static void
2289f_count(typval_T *argvars, typval_T *rettv)
2290{
2291 long n = 0;
2292 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002293 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002294
Bram Moolenaar9966b212017-07-28 16:46:57 +02002295 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002296 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002297
2298 if (argvars[0].v_type == VAR_STRING)
2299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002300 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002301 char_u *p = argvars[0].vval.v_string;
2302 char_u *next;
2303
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002304 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002305 {
2306 if (ic)
2307 {
2308 size_t len = STRLEN(expr);
2309
2310 while (*p != NUL)
2311 {
2312 if (MB_STRNICMP(p, expr, len) == 0)
2313 {
2314 ++n;
2315 p += len;
2316 }
2317 else
2318 MB_PTR_ADV(p);
2319 }
2320 }
2321 else
2322 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2323 != NULL)
2324 {
2325 ++n;
2326 p = next + STRLEN(expr);
2327 }
2328 }
2329
2330 }
2331 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002332 {
2333 listitem_T *li;
2334 list_T *l;
2335 long idx;
2336
2337 if ((l = argvars[0].vval.v_list) != NULL)
2338 {
2339 li = l->lv_first;
2340 if (argvars[2].v_type != VAR_UNKNOWN)
2341 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002342 if (argvars[3].v_type != VAR_UNKNOWN)
2343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002344 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002345 if (!error)
2346 {
2347 li = list_find(l, idx);
2348 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002349 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002350 }
2351 }
2352 if (error)
2353 li = NULL;
2354 }
2355
2356 for ( ; li != NULL; li = li->li_next)
2357 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2358 ++n;
2359 }
2360 }
2361 else if (argvars[0].v_type == VAR_DICT)
2362 {
2363 int todo;
2364 dict_T *d;
2365 hashitem_T *hi;
2366
2367 if ((d = argvars[0].vval.v_dict) != NULL)
2368 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369 if (argvars[2].v_type != VAR_UNKNOWN)
2370 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002372 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 }
2374
2375 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2376 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2377 {
2378 if (!HASHITEM_EMPTY(hi))
2379 {
2380 --todo;
2381 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2382 ++n;
2383 }
2384 }
2385 }
2386 }
2387 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002388 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002389 rettv->vval.v_number = n;
2390}
2391
2392/*
2393 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2394 *
2395 * Checks the existence of a cscope connection.
2396 */
2397 static void
2398f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2399{
2400#ifdef FEAT_CSCOPE
2401 int num = 0;
2402 char_u *dbpath = NULL;
2403 char_u *prepend = NULL;
2404 char_u buf[NUMBUFLEN];
2405
2406 if (argvars[0].v_type != VAR_UNKNOWN
2407 && argvars[1].v_type != VAR_UNKNOWN)
2408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002409 num = (int)tv_get_number(&argvars[0]);
2410 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002412 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 }
2414
2415 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2416#endif
2417}
2418
2419/*
2420 * "cursor(lnum, col)" function, or
2421 * "cursor(list)"
2422 *
2423 * Moves the cursor to the specified line and column.
2424 * Returns 0 when the position could be set, -1 otherwise.
2425 */
2426 static void
2427f_cursor(typval_T *argvars, typval_T *rettv)
2428{
2429 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002430 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431 int set_curswant = TRUE;
2432
2433 rettv->vval.v_number = -1;
2434 if (argvars[1].v_type == VAR_UNKNOWN)
2435 {
2436 pos_T pos;
2437 colnr_T curswant = -1;
2438
2439 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002441 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002442 return;
2443 }
2444 line = pos.lnum;
2445 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002447 if (curswant >= 0)
2448 {
2449 curwin->w_curswant = curswant - 1;
2450 set_curswant = FALSE;
2451 }
2452 }
2453 else
2454 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002455 line = tv_get_lnum(argvars);
2456 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002458 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002459 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002460 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002461 return; /* type error; errmsg already given */
2462 if (line > 0)
2463 curwin->w_cursor.lnum = line;
2464 if (col > 0)
2465 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002467
2468 /* Make sure the cursor is in a valid position. */
2469 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002470 /* Correct cursor for multi-byte character. */
2471 if (has_mbyte)
2472 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002473
2474 curwin->w_set_curswant = set_curswant;
2475 rettv->vval.v_number = 0;
2476}
2477
Bram Moolenaar4f974752019-02-17 17:44:42 +01002478#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002479/*
2480 * "debugbreak()" function
2481 */
2482 static void
2483f_debugbreak(typval_T *argvars, typval_T *rettv)
2484{
2485 int pid;
2486
2487 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002488 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002489 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002490 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002491 else
2492 {
2493 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2494
2495 if (hProcess != NULL)
2496 {
2497 DebugBreakProcess(hProcess);
2498 CloseHandle(hProcess);
2499 rettv->vval.v_number = OK;
2500 }
2501 }
2502}
2503#endif
2504
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505/*
2506 * "deepcopy()" function
2507 */
2508 static void
2509f_deepcopy(typval_T *argvars, typval_T *rettv)
2510{
2511 int noref = 0;
2512 int copyID;
2513
2514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002515 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002516 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518 else
2519 {
2520 copyID = get_copyID();
2521 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2522 }
2523}
2524
2525/*
2526 * "delete()" function
2527 */
2528 static void
2529f_delete(typval_T *argvars, typval_T *rettv)
2530{
2531 char_u nbuf[NUMBUFLEN];
2532 char_u *name;
2533 char_u *flags;
2534
2535 rettv->vval.v_number = -1;
2536 if (check_restricted() || check_secure())
2537 return;
2538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002539 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540 if (name == NULL || *name == NUL)
2541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543 return;
2544 }
2545
2546 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548 else
2549 flags = (char_u *)"";
2550
2551 if (*flags == NUL)
2552 /* delete a file */
2553 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2554 else if (STRCMP(flags, "d") == 0)
2555 /* delete an empty directory */
2556 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2557 else if (STRCMP(flags, "rf") == 0)
2558 /* delete a directory recursively */
2559 rettv->vval.v_number = delete_recursive(name);
2560 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562}
2563
2564/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002565 * "deletebufline()" function
2566 */
2567 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002568f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002569{
2570 buf_T *buf;
2571 linenr_T first, last;
2572 linenr_T lnum;
2573 long count;
2574 int is_curbuf;
2575 buf_T *curbuf_save = NULL;
2576 win_T *curwin_save = NULL;
2577 tabpage_T *tp;
2578 win_T *wp;
2579
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002580 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002581 if (buf == NULL)
2582 {
2583 rettv->vval.v_number = 1; /* FAIL */
2584 return;
2585 }
2586 is_curbuf = buf == curbuf;
2587
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002590 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002591 else
2592 last = first;
2593
2594 if (buf->b_ml.ml_mfp == NULL || first < 1
2595 || first > buf->b_ml.ml_line_count || last < first)
2596 {
2597 rettv->vval.v_number = 1; /* FAIL */
2598 return;
2599 }
2600
2601 if (!is_curbuf)
2602 {
2603 curbuf_save = curbuf;
2604 curwin_save = curwin;
2605 curbuf = buf;
2606 find_win_for_curbuf();
2607 }
2608 if (last > curbuf->b_ml.ml_line_count)
2609 last = curbuf->b_ml.ml_line_count;
2610 count = last - first + 1;
2611
2612 // When coming here from Insert mode, sync undo, so that this can be
2613 // undone separately from what was previously inserted.
2614 if (u_sync_once == 2)
2615 {
2616 u_sync_once = 1; // notify that u_sync() was called
2617 u_sync(TRUE);
2618 }
2619
2620 if (u_save(first - 1, last + 1) == FAIL)
2621 {
2622 rettv->vval.v_number = 1; /* FAIL */
2623 return;
2624 }
2625
2626 for (lnum = first; lnum <= last; ++lnum)
2627 ml_delete(first, TRUE);
2628
2629 FOR_ALL_TAB_WINDOWS(tp, wp)
2630 if (wp->w_buffer == buf)
2631 {
2632 if (wp->w_cursor.lnum > last)
2633 wp->w_cursor.lnum -= count;
2634 else if (wp->w_cursor.lnum> first)
2635 wp->w_cursor.lnum = first;
2636 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2637 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2638 }
2639 check_cursor_col();
2640 deleted_lines_mark(first, count);
2641
2642 if (!is_curbuf)
2643 {
2644 curbuf = curbuf_save;
2645 curwin = curwin_save;
2646 }
2647}
2648
2649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002650 * "did_filetype()" function
2651 */
2652 static void
2653f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2654{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656}
2657
2658/*
2659 * "diff_filler()" function
2660 */
2661 static void
2662f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002665 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002666#endif
2667}
2668
2669/*
2670 * "diff_hlID()" function
2671 */
2672 static void
2673f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2674{
2675#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002676 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002678 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 static int fnum = 0;
2680 static int change_start = 0;
2681 static int change_end = 0;
2682 static hlf_T hlID = (hlf_T)0;
2683 int filler_lines;
2684 int col;
2685
2686 if (lnum < 0) /* ignore type error in {lnum} arg */
2687 lnum = 0;
2688 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002689 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 || fnum != curbuf->b_fnum)
2691 {
2692 /* New line, buffer, change: need to get the values. */
2693 filler_lines = diff_check(curwin, lnum);
2694 if (filler_lines < 0)
2695 {
2696 if (filler_lines == -1)
2697 {
2698 change_start = MAXCOL;
2699 change_end = -1;
2700 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2701 hlID = HLF_ADD; /* added line */
2702 else
2703 hlID = HLF_CHD; /* changed line */
2704 }
2705 else
2706 hlID = HLF_ADD; /* added line */
2707 }
2708 else
2709 hlID = (hlf_T)0;
2710 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002711 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 fnum = curbuf->b_fnum;
2713 }
2714
2715 if (hlID == HLF_CHD || hlID == HLF_TXD)
2716 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002717 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 if (col >= change_start && col <= change_end)
2719 hlID = HLF_TXD; /* changed text */
2720 else
2721 hlID = HLF_CHD; /* changed line */
2722 }
2723 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2724#endif
2725}
2726
2727/*
2728 * "empty({expr})" function
2729 */
2730 static void
2731f_empty(typval_T *argvars, typval_T *rettv)
2732{
2733 int n = FALSE;
2734
2735 switch (argvars[0].v_type)
2736 {
2737 case VAR_STRING:
2738 case VAR_FUNC:
2739 n = argvars[0].vval.v_string == NULL
2740 || *argvars[0].vval.v_string == NUL;
2741 break;
2742 case VAR_PARTIAL:
2743 n = FALSE;
2744 break;
2745 case VAR_NUMBER:
2746 n = argvars[0].vval.v_number == 0;
2747 break;
2748 case VAR_FLOAT:
2749#ifdef FEAT_FLOAT
2750 n = argvars[0].vval.v_float == 0.0;
2751 break;
2752#endif
2753 case VAR_LIST:
2754 n = argvars[0].vval.v_list == NULL
2755 || argvars[0].vval.v_list->lv_first == NULL;
2756 break;
2757 case VAR_DICT:
2758 n = argvars[0].vval.v_dict == NULL
2759 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2760 break;
2761 case VAR_SPECIAL:
2762 n = argvars[0].vval.v_number != VVAL_TRUE;
2763 break;
2764
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002765 case VAR_BLOB:
2766 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002767 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
2768 break;
2769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 case VAR_JOB:
2771#ifdef FEAT_JOB_CHANNEL
2772 n = argvars[0].vval.v_job == NULL
2773 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2774 break;
2775#endif
2776 case VAR_CHANNEL:
2777#ifdef FEAT_JOB_CHANNEL
2778 n = argvars[0].vval.v_channel == NULL
2779 || !channel_is_open(argvars[0].vval.v_channel);
2780 break;
2781#endif
2782 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002783 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 n = TRUE;
2785 break;
2786 }
2787
2788 rettv->vval.v_number = n;
2789}
2790
2791/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02002792 * "environ()" function
2793 */
2794 static void
2795f_environ(typval_T *argvars UNUSED, typval_T *rettv)
2796{
2797#if !defined(AMIGA)
2798 int i = 0;
2799 char_u *entry, *value;
2800# ifdef MSWIN
2801 extern wchar_t **_wenviron;
2802# else
2803 extern char **environ;
2804# endif
2805
2806 if (rettv_dict_alloc(rettv) != OK)
2807 return;
2808
2809# ifdef MSWIN
2810 if (*_wenviron == NULL)
2811 return;
2812# else
2813 if (*environ == NULL)
2814 return;
2815# endif
2816
2817 for (i = 0; ; ++i)
2818 {
2819# ifdef MSWIN
2820 short_u *p;
2821
2822 if ((p = (short_u *)_wenviron[i]) == NULL)
2823 return;
2824 entry = utf16_to_enc(p, NULL);
2825# else
2826 if ((entry = (char_u *)environ[i]) == NULL)
2827 return;
2828 entry = vim_strsave(entry);
2829# endif
2830 if (entry == NULL) // out of memory
2831 return;
2832 if ((value = vim_strchr(entry, '=')) == NULL)
2833 {
2834 vim_free(entry);
2835 continue;
2836 }
2837 *value++ = NUL;
2838 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
2839 vim_free(entry);
2840 }
2841#endif
2842}
2843
2844/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002845 * "escape({string}, {chars})" function
2846 */
2847 static void
2848f_escape(typval_T *argvars, typval_T *rettv)
2849{
2850 char_u buf[NUMBUFLEN];
2851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002852 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
2853 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854 rettv->v_type = VAR_STRING;
2855}
2856
2857/*
2858 * "eval()" function
2859 */
2860 static void
2861f_eval(typval_T *argvars, typval_T *rettv)
2862{
2863 char_u *s, *p;
2864
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002865 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 if (s != NULL)
2867 s = skipwhite(s);
2868
2869 p = s;
2870 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2871 {
2872 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874 need_clr_eos = FALSE;
2875 rettv->v_type = VAR_NUMBER;
2876 rettv->vval.v_number = 0;
2877 }
2878 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002879 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002880}
2881
2882/*
2883 * "eventhandler()" function
2884 */
2885 static void
2886f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2887{
2888 rettv->vval.v_number = vgetc_busy;
2889}
2890
2891/*
2892 * "executable()" function
2893 */
2894 static void
2895f_executable(typval_T *argvars, typval_T *rettv)
2896{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002897 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002898
2899 /* Check in $PATH and also check directly if there is a directory name. */
Bram Moolenaard08b8c42019-07-24 14:59:45 +02002900 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002901}
2902
2903static garray_T redir_execute_ga;
2904
2905/*
2906 * Append "value[value_len]" to the execute() output.
2907 */
2908 void
2909execute_redir_str(char_u *value, int value_len)
2910{
2911 int len;
2912
2913 if (value_len == -1)
2914 len = (int)STRLEN(value); /* Append the entire string */
2915 else
2916 len = value_len; /* Append only "value_len" characters */
2917 if (ga_grow(&redir_execute_ga, len) == OK)
2918 {
2919 mch_memmove((char *)redir_execute_ga.ga_data
2920 + redir_execute_ga.ga_len, value, len);
2921 redir_execute_ga.ga_len += len;
2922 }
2923}
2924
2925/*
2926 * Get next line from a list.
2927 * Called by do_cmdline() to get the next line.
2928 * Returns allocated string, or NULL for end of function.
2929 */
2930
2931 static char_u *
2932get_list_line(
2933 int c UNUSED,
2934 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002935 int indent UNUSED,
2936 int do_concat UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937{
2938 listitem_T **p = (listitem_T **)cookie;
2939 listitem_T *item = *p;
2940 char_u buf[NUMBUFLEN];
2941 char_u *s;
2942
2943 if (item == NULL)
2944 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002945 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002946 *p = item->li_next;
2947 return s == NULL ? NULL : vim_strsave(s);
2948}
2949
2950/*
2951 * "execute()" function
2952 */
2953 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002954execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955{
2956 char_u *cmd = NULL;
2957 list_T *list = NULL;
2958 int save_msg_silent = msg_silent;
2959 int save_emsg_silent = emsg_silent;
2960 int save_emsg_noredir = emsg_noredir;
2961 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002962 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01002964 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002965 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966
2967 rettv->vval.v_string = NULL;
2968 rettv->v_type = VAR_STRING;
2969
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002970 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002972 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 if (list == NULL || list->lv_first == NULL)
2974 /* empty list, no commands, empty output */
2975 return;
2976 ++list->lv_refcount;
2977 }
2978 else
2979 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002980 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 if (cmd == NULL)
2982 return;
2983 }
2984
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002985 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002986 {
2987 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02002988 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989
2990 if (s == NULL)
2991 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01002992 if (*s == NUL)
2993 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 if (STRNCMP(s, "silent", 6) == 0)
2995 ++msg_silent;
2996 if (STRCMP(s, "silent!") == 0)
2997 {
2998 emsg_silent = TRUE;
2999 emsg_noredir = TRUE;
3000 }
3001 }
3002 else
3003 ++msg_silent;
3004
3005 if (redir_execute)
3006 save_ga = redir_execute_ga;
3007 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3008 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003009 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003010 if (!echo_output)
3011 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012
3013 if (cmd != NULL)
3014 do_cmdline_cmd(cmd);
3015 else
3016 {
3017 listitem_T *item = list->lv_first;
3018
3019 do_cmdline(NULL, get_list_line, (void *)&item,
3020 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3021 --list->lv_refcount;
3022 }
3023
Bram Moolenaard297f352017-01-29 20:31:21 +01003024 /* Need to append a NUL to the result. */
3025 if (ga_grow(&redir_execute_ga, 1) == OK)
3026 {
3027 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3028 rettv->vval.v_string = redir_execute_ga.ga_data;
3029 }
3030 else
3031 {
3032 ga_clear(&redir_execute_ga);
3033 rettv->vval.v_string = NULL;
3034 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035 msg_silent = save_msg_silent;
3036 emsg_silent = save_emsg_silent;
3037 emsg_noredir = save_emsg_noredir;
3038
3039 redir_execute = save_redir_execute;
3040 if (redir_execute)
3041 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003042 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003044 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003045 if (echo_output)
3046 // When not working silently: put it in column zero. A following
3047 // "echon" will overwrite the message, unavoidably.
3048 msg_col = 0;
3049 else
3050 // When working silently: Put it back where it was, since nothing
3051 // should have been written.
3052 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053}
3054
3055/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003056 * "execute()" function
3057 */
3058 static void
3059f_execute(typval_T *argvars, typval_T *rettv)
3060{
3061 execute_common(argvars, rettv, 0);
3062}
3063
3064/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065 * "exepath()" function
3066 */
3067 static void
3068f_exepath(typval_T *argvars, typval_T *rettv)
3069{
3070 char_u *p = NULL;
3071
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003072 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 rettv->v_type = VAR_STRING;
3074 rettv->vval.v_string = p;
3075}
3076
3077/*
3078 * "exists()" function
3079 */
3080 static void
3081f_exists(typval_T *argvars, typval_T *rettv)
3082{
3083 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003086 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087 if (*p == '$') /* environment variable */
3088 {
3089 /* first try "normal" environment variables (fast) */
3090 if (mch_getenv(p + 1) != NULL)
3091 n = TRUE;
3092 else
3093 {
3094 /* try expanding things like $VIM and ${HOME} */
3095 p = expand_env_save(p);
3096 if (p != NULL && *p != '$')
3097 n = TRUE;
3098 vim_free(p);
3099 }
3100 }
3101 else if (*p == '&' || *p == '+') /* option */
3102 {
3103 n = (get_option_tv(&p, NULL, TRUE) == OK);
3104 if (*skipwhite(p) != NUL)
3105 n = FALSE; /* trailing garbage */
3106 }
3107 else if (*p == '*') /* internal or user defined function */
3108 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003109 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 }
3111 else if (*p == ':')
3112 {
3113 n = cmd_exists(p + 1);
3114 }
3115 else if (*p == '#')
3116 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003117 if (p[1] == '#')
3118 n = autocmd_supported(p + 2);
3119 else
3120 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 }
3122 else /* internal variable */
3123 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003124 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 }
3126
3127 rettv->vval.v_number = n;
3128}
3129
3130#ifdef FEAT_FLOAT
3131/*
3132 * "exp()" function
3133 */
3134 static void
3135f_exp(typval_T *argvars, typval_T *rettv)
3136{
3137 float_T f = 0.0;
3138
3139 rettv->v_type = VAR_FLOAT;
3140 if (get_float_arg(argvars, &f) == OK)
3141 rettv->vval.v_float = exp(f);
3142 else
3143 rettv->vval.v_float = 0.0;
3144}
3145#endif
3146
3147/*
3148 * "expand()" function
3149 */
3150 static void
3151f_expand(typval_T *argvars, typval_T *rettv)
3152{
3153 char_u *s;
3154 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003155 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003156 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3157 expand_T xpc;
3158 int error = FALSE;
3159 char_u *result;
3160
3161 rettv->v_type = VAR_STRING;
3162 if (argvars[1].v_type != VAR_UNKNOWN
3163 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003164 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003165 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003166 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003168 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003169 if (*s == '%' || *s == '#' || *s == '<')
3170 {
3171 ++emsg_off;
3172 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3173 --emsg_off;
3174 if (rettv->v_type == VAR_LIST)
3175 {
3176 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3177 list_append_string(rettv->vval.v_list, result, -1);
3178 else
3179 vim_free(result);
3180 }
3181 else
3182 rettv->vval.v_string = result;
3183 }
3184 else
3185 {
3186 /* When the optional second argument is non-zero, don't remove matches
3187 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3188 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003190 options |= WILD_KEEP_ALL;
3191 if (!error)
3192 {
3193 ExpandInit(&xpc);
3194 xpc.xp_context = EXPAND_FILES;
3195 if (p_wic)
3196 options += WILD_ICASE;
3197 if (rettv->v_type == VAR_STRING)
3198 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3199 options, WILD_ALL);
3200 else if (rettv_list_alloc(rettv) != FAIL)
3201 {
3202 int i;
3203
3204 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3205 for (i = 0; i < xpc.xp_numfiles; i++)
3206 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3207 ExpandCleanup(&xpc);
3208 }
3209 }
3210 else
3211 rettv->vval.v_string = NULL;
3212 }
3213}
3214
3215/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003216 * "expandcmd()" function
3217 * Expand all the special characters in a command string.
3218 */
3219 static void
3220f_expandcmd(typval_T *argvars, typval_T *rettv)
3221{
3222 exarg_T eap;
3223 char_u *cmdstr;
3224 char *errormsg = NULL;
3225
3226 rettv->v_type = VAR_STRING;
3227 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3228
3229 memset(&eap, 0, sizeof(eap));
3230 eap.cmd = cmdstr;
3231 eap.arg = cmdstr;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003232 eap.argt |= EX_NOSPC;
Bram Moolenaar80dad482019-06-09 17:22:31 +02003233 eap.usefilter = FALSE;
3234 eap.nextcmd = NULL;
3235 eap.cmdidx = CMD_USER;
3236
3237 expand_filename(&eap, &cmdstr, &errormsg);
3238 if (errormsg != NULL && *errormsg != NUL)
3239 emsg(errormsg);
3240
3241 rettv->vval.v_string = cmdstr;
3242}
3243
3244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245 * "extend(list, list [, idx])" function
3246 * "extend(dict, dict [, action])" function
3247 */
3248 static void
3249f_extend(typval_T *argvars, typval_T *rettv)
3250{
3251 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3252
3253 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3254 {
3255 list_T *l1, *l2;
3256 listitem_T *item;
3257 long before;
3258 int error = FALSE;
3259
3260 l1 = argvars[0].vval.v_list;
3261 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003262 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003263 && l2 != NULL)
3264 {
3265 if (argvars[2].v_type != VAR_UNKNOWN)
3266 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003267 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268 if (error)
3269 return; /* type error; errmsg already given */
3270
3271 if (before == l1->lv_len)
3272 item = NULL;
3273 else
3274 {
3275 item = list_find(l1, before);
3276 if (item == NULL)
3277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003278 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 return;
3280 }
3281 }
3282 }
3283 else
3284 item = NULL;
3285 list_extend(l1, l2, item);
3286
3287 copy_tv(&argvars[0], rettv);
3288 }
3289 }
3290 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3291 {
3292 dict_T *d1, *d2;
3293 char_u *action;
3294 int i;
3295
3296 d1 = argvars[0].vval.v_dict;
3297 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003298 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 && d2 != NULL)
3300 {
3301 /* Check the third argument. */
3302 if (argvars[2].v_type != VAR_UNKNOWN)
3303 {
3304 static char *(av[]) = {"keep", "force", "error"};
3305
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003306 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 if (action == NULL)
3308 return; /* type error; errmsg already given */
3309 for (i = 0; i < 3; ++i)
3310 if (STRCMP(action, av[i]) == 0)
3311 break;
3312 if (i == 3)
3313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003315 return;
3316 }
3317 }
3318 else
3319 action = (char_u *)"force";
3320
3321 dict_extend(d1, d2, action);
3322
3323 copy_tv(&argvars[0], rettv);
3324 }
3325 }
3326 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003327 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003328}
3329
3330/*
3331 * "feedkeys()" function
3332 */
3333 static void
3334f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3335{
3336 int remap = TRUE;
3337 int insert = FALSE;
3338 char_u *keys, *flags;
3339 char_u nbuf[NUMBUFLEN];
3340 int typed = FALSE;
3341 int execute = FALSE;
3342 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003343 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 char_u *keys_esc;
3345
3346 /* This is not allowed in the sandbox. If the commands would still be
3347 * executed in the sandbox it would be OK, but it probably happens later,
3348 * when "sandbox" is no longer set. */
3349 if (check_secure())
3350 return;
3351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003352 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003353
3354 if (argvars[1].v_type != VAR_UNKNOWN)
3355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003356 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357 for ( ; *flags != NUL; ++flags)
3358 {
3359 switch (*flags)
3360 {
3361 case 'n': remap = FALSE; break;
3362 case 'm': remap = TRUE; break;
3363 case 't': typed = TRUE; break;
3364 case 'i': insert = TRUE; break;
3365 case 'x': execute = TRUE; break;
3366 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003367 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368 }
3369 }
3370 }
3371
3372 if (*keys != NUL || execute)
3373 {
3374 /* Need to escape K_SPECIAL and CSI before putting the string in the
3375 * typeahead buffer. */
3376 keys_esc = vim_strsave_escape_csi(keys);
3377 if (keys_esc != NULL)
3378 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003379 if (lowlevel)
3380 {
3381#ifdef USE_INPUT_BUF
3382 add_to_input_buf(keys, (int)STRLEN(keys));
3383#else
3384 emsg(_("E980: lowlevel input not supported"));
3385#endif
3386 }
3387 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003388 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003389 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003390 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003391 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003392#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003393 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003394#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003395 )
3396 typebuf_was_filled = TRUE;
3397 }
3398 vim_free(keys_esc);
3399
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003400 if (execute)
3401 {
3402 int save_msg_scroll = msg_scroll;
3403
3404 /* Avoid a 1 second delay when the keys start Insert mode. */
3405 msg_scroll = FALSE;
3406
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003407 if (!dangerous)
3408 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003409 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003410 if (!dangerous)
3411 --ex_normal_busy;
3412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413 msg_scroll |= save_msg_scroll;
3414 }
3415 }
3416 }
3417}
3418
3419/*
3420 * "filereadable()" function
3421 */
3422 static void
3423f_filereadable(typval_T *argvars, typval_T *rettv)
3424{
3425 int fd;
3426 char_u *p;
3427 int n;
3428
3429#ifndef O_NONBLOCK
3430# define O_NONBLOCK 0
3431#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003432 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3434 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3435 {
3436 n = TRUE;
3437 close(fd);
3438 }
3439 else
3440 n = FALSE;
3441
3442 rettv->vval.v_number = n;
3443}
3444
3445/*
3446 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3447 * rights to write into.
3448 */
3449 static void
3450f_filewritable(typval_T *argvars, typval_T *rettv)
3451{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003452 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003453}
3454
3455 static void
3456findfilendir(
3457 typval_T *argvars UNUSED,
3458 typval_T *rettv,
3459 int find_what UNUSED)
3460{
3461#ifdef FEAT_SEARCHPATH
3462 char_u *fname;
3463 char_u *fresult = NULL;
3464 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3465 char_u *p;
3466 char_u pathbuf[NUMBUFLEN];
3467 int count = 1;
3468 int first = TRUE;
3469 int error = FALSE;
3470#endif
3471
3472 rettv->vval.v_string = NULL;
3473 rettv->v_type = VAR_STRING;
3474
3475#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003476 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003477
3478 if (argvars[1].v_type != VAR_UNKNOWN)
3479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003480 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003481 if (p == NULL)
3482 error = TRUE;
3483 else
3484 {
3485 if (*p != NUL)
3486 path = p;
3487
3488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003489 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003490 }
3491 }
3492
3493 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3494 error = TRUE;
3495
3496 if (*fname != NUL && !error)
3497 {
3498 do
3499 {
3500 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3501 vim_free(fresult);
3502 fresult = find_file_in_path_option(first ? fname : NULL,
3503 first ? (int)STRLEN(fname) : 0,
3504 0, first, path,
3505 find_what,
3506 curbuf->b_ffname,
3507 find_what == FINDFILE_DIR
3508 ? (char_u *)"" : curbuf->b_p_sua);
3509 first = FALSE;
3510
3511 if (fresult != NULL && rettv->v_type == VAR_LIST)
3512 list_append_string(rettv->vval.v_list, fresult, -1);
3513
3514 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3515 }
3516
3517 if (rettv->v_type == VAR_STRING)
3518 rettv->vval.v_string = fresult;
3519#endif
3520}
3521
3522/*
3523 * "filter()" function
3524 */
3525 static void
3526f_filter(typval_T *argvars, typval_T *rettv)
3527{
3528 filter_map(argvars, rettv, FALSE);
3529}
3530
3531/*
3532 * "finddir({fname}[, {path}[, {count}]])" function
3533 */
3534 static void
3535f_finddir(typval_T *argvars, typval_T *rettv)
3536{
3537 findfilendir(argvars, rettv, FINDFILE_DIR);
3538}
3539
3540/*
3541 * "findfile({fname}[, {path}[, {count}]])" function
3542 */
3543 static void
3544f_findfile(typval_T *argvars, typval_T *rettv)
3545{
3546 findfilendir(argvars, rettv, FINDFILE_FILE);
3547}
3548
3549#ifdef FEAT_FLOAT
3550/*
3551 * "float2nr({float})" function
3552 */
3553 static void
3554f_float2nr(typval_T *argvars, typval_T *rettv)
3555{
3556 float_T f = 0.0;
3557
3558 if (get_float_arg(argvars, &f) == OK)
3559 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003560 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003561 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003562 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003563 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 else
3565 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 }
3567}
3568
3569/*
3570 * "floor({float})" function
3571 */
3572 static void
3573f_floor(typval_T *argvars, typval_T *rettv)
3574{
3575 float_T f = 0.0;
3576
3577 rettv->v_type = VAR_FLOAT;
3578 if (get_float_arg(argvars, &f) == OK)
3579 rettv->vval.v_float = floor(f);
3580 else
3581 rettv->vval.v_float = 0.0;
3582}
3583
3584/*
3585 * "fmod()" function
3586 */
3587 static void
3588f_fmod(typval_T *argvars, typval_T *rettv)
3589{
3590 float_T fx = 0.0, fy = 0.0;
3591
3592 rettv->v_type = VAR_FLOAT;
3593 if (get_float_arg(argvars, &fx) == OK
3594 && get_float_arg(&argvars[1], &fy) == OK)
3595 rettv->vval.v_float = fmod(fx, fy);
3596 else
3597 rettv->vval.v_float = 0.0;
3598}
3599#endif
3600
3601/*
3602 * "fnameescape({string})" function
3603 */
3604 static void
3605f_fnameescape(typval_T *argvars, typval_T *rettv)
3606{
3607 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003608 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003609 rettv->v_type = VAR_STRING;
3610}
3611
3612/*
3613 * "fnamemodify({fname}, {mods})" function
3614 */
3615 static void
3616f_fnamemodify(typval_T *argvars, typval_T *rettv)
3617{
3618 char_u *fname;
3619 char_u *mods;
3620 int usedlen = 0;
3621 int len;
3622 char_u *fbuf = NULL;
3623 char_u buf[NUMBUFLEN];
3624
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003625 fname = tv_get_string_chk(&argvars[0]);
3626 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 if (fname == NULL || mods == NULL)
3628 fname = NULL;
3629 else
3630 {
3631 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003632 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633 }
3634
3635 rettv->v_type = VAR_STRING;
3636 if (fname == NULL)
3637 rettv->vval.v_string = NULL;
3638 else
3639 rettv->vval.v_string = vim_strnsave(fname, len);
3640 vim_free(fbuf);
3641}
3642
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643/*
3644 * "foldclosed()" function
3645 */
3646 static void
3647foldclosed_both(
3648 typval_T *argvars UNUSED,
3649 typval_T *rettv,
3650 int end UNUSED)
3651{
3652#ifdef FEAT_FOLDING
3653 linenr_T lnum;
3654 linenr_T first, last;
3655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003656 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3658 {
3659 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3660 {
3661 if (end)
3662 rettv->vval.v_number = (varnumber_T)last;
3663 else
3664 rettv->vval.v_number = (varnumber_T)first;
3665 return;
3666 }
3667 }
3668#endif
3669 rettv->vval.v_number = -1;
3670}
3671
3672/*
3673 * "foldclosed()" function
3674 */
3675 static void
3676f_foldclosed(typval_T *argvars, typval_T *rettv)
3677{
3678 foldclosed_both(argvars, rettv, FALSE);
3679}
3680
3681/*
3682 * "foldclosedend()" function
3683 */
3684 static void
3685f_foldclosedend(typval_T *argvars, typval_T *rettv)
3686{
3687 foldclosed_both(argvars, rettv, TRUE);
3688}
3689
3690/*
3691 * "foldlevel()" function
3692 */
3693 static void
3694f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3695{
3696#ifdef FEAT_FOLDING
3697 linenr_T lnum;
3698
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003699 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003700 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3701 rettv->vval.v_number = foldLevel(lnum);
3702#endif
3703}
3704
3705/*
3706 * "foldtext()" function
3707 */
3708 static void
3709f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3710{
3711#ifdef FEAT_FOLDING
3712 linenr_T foldstart;
3713 linenr_T foldend;
3714 char_u *dashes;
3715 linenr_T lnum;
3716 char_u *s;
3717 char_u *r;
3718 int len;
3719 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003720 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003721#endif
3722
3723 rettv->v_type = VAR_STRING;
3724 rettv->vval.v_string = NULL;
3725#ifdef FEAT_FOLDING
3726 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3727 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3728 dashes = get_vim_var_str(VV_FOLDDASHES);
3729 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3730 && dashes != NULL)
3731 {
3732 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003733 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734 if (!linewhite(lnum))
3735 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736
3737 /* Find interesting text in this line. */
3738 s = skipwhite(ml_get(lnum));
3739 /* skip C comment-start */
3740 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3741 {
3742 s = skipwhite(s + 2);
3743 if (*skipwhite(s) == NUL
3744 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3745 {
3746 s = skipwhite(ml_get(lnum + 1));
3747 if (*s == '*')
3748 s = skipwhite(s + 1);
3749 }
3750 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003751 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003752 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02003753 r = alloc(STRLEN(txt)
3754 + STRLEN(dashes) // for %s
3755 + 20 // for %3ld
3756 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 if (r != NULL)
3758 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003759 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 len = (int)STRLEN(r);
3761 STRCAT(r, s);
3762 /* remove 'foldmarker' and 'commentstring' */
3763 foldtext_cleanup(r + len);
3764 rettv->vval.v_string = r;
3765 }
3766 }
3767#endif
3768}
3769
3770/*
3771 * "foldtextresult(lnum)" function
3772 */
3773 static void
3774f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3775{
3776#ifdef FEAT_FOLDING
3777 linenr_T lnum;
3778 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003779 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003780 foldinfo_T foldinfo;
3781 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003782 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003783#endif
3784
3785 rettv->v_type = VAR_STRING;
3786 rettv->vval.v_string = NULL;
3787#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003788 if (entered)
3789 return; /* reject recursive use */
3790 entered = TRUE;
3791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003792 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003793 /* treat illegal types and illegal string values for {lnum} the same */
3794 if (lnum < 0)
3795 lnum = 0;
3796 fold_count = foldedCount(curwin, lnum, &foldinfo);
3797 if (fold_count > 0)
3798 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003799 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3800 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 if (text == buf)
3802 text = vim_strsave(text);
3803 rettv->vval.v_string = text;
3804 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003805
3806 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003807#endif
3808}
3809
3810/*
3811 * "foreground()" function
3812 */
3813 static void
3814f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3815{
3816#ifdef FEAT_GUI
3817 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003820 return;
3821 }
3822#endif
3823#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825#endif
3826}
3827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003828 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003829common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830{
3831 char_u *s;
3832 char_u *name;
3833 int use_string = FALSE;
3834 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003835 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003836
3837 if (argvars[0].v_type == VAR_FUNC)
3838 {
3839 /* function(MyFunc, [arg], dict) */
3840 s = argvars[0].vval.v_string;
3841 }
3842 else if (argvars[0].v_type == VAR_PARTIAL
3843 && argvars[0].vval.v_partial != NULL)
3844 {
3845 /* function(dict.MyFunc, [arg]) */
3846 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003847 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848 }
3849 else
3850 {
3851 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003852 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 use_string = TRUE;
3854 }
3855
Bram Moolenaar843b8842016-08-21 14:36:15 +02003856 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003857 {
3858 name = s;
3859 trans_name = trans_function_name(&name, FALSE,
3860 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3861 if (*name != NUL)
3862 s = NULL;
3863 }
3864
Bram Moolenaar843b8842016-08-21 14:36:15 +02003865 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3866 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003867 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003868 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003869 else if (trans_name != NULL && (is_funcref
3870 ? find_func(trans_name) == NULL
3871 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003872 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 else
3874 {
3875 int dict_idx = 0;
3876 int arg_idx = 0;
3877 list_T *list = NULL;
3878
3879 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3880 {
3881 char sid_buf[25];
3882 int off = *s == 's' ? 2 : 5;
3883
3884 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3885 * also be called from another script. Using trans_function_name()
3886 * would also work, but some plugins depend on the name being
3887 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003888 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003889 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890 if (name != NULL)
3891 {
3892 STRCPY(name, sid_buf);
3893 STRCAT(name, s + off);
3894 }
3895 }
3896 else
3897 name = vim_strsave(s);
3898
3899 if (argvars[1].v_type != VAR_UNKNOWN)
3900 {
3901 if (argvars[2].v_type != VAR_UNKNOWN)
3902 {
3903 /* function(name, [args], dict) */
3904 arg_idx = 1;
3905 dict_idx = 2;
3906 }
3907 else if (argvars[1].v_type == VAR_DICT)
3908 /* function(name, dict) */
3909 dict_idx = 1;
3910 else
3911 /* function(name, [args]) */
3912 arg_idx = 1;
3913 if (dict_idx > 0)
3914 {
3915 if (argvars[dict_idx].v_type != VAR_DICT)
3916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003918 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003919 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 }
3921 if (argvars[dict_idx].vval.v_dict == NULL)
3922 dict_idx = 0;
3923 }
3924 if (arg_idx > 0)
3925 {
3926 if (argvars[arg_idx].v_type != VAR_LIST)
3927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003928 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003930 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 }
3932 list = argvars[arg_idx].vval.v_list;
3933 if (list == NULL || list->lv_len == 0)
3934 arg_idx = 0;
3935 }
3936 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003937 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003939 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940
3941 /* result is a VAR_PARTIAL */
3942 if (pt == NULL)
3943 vim_free(name);
3944 else
3945 {
3946 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3947 {
3948 listitem_T *li;
3949 int i = 0;
3950 int arg_len = 0;
3951 int lv_len = 0;
3952
3953 if (arg_pt != NULL)
3954 arg_len = arg_pt->pt_argc;
3955 if (list != NULL)
3956 lv_len = list->lv_len;
3957 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003958 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003959 if (pt->pt_argv == NULL)
3960 {
3961 vim_free(pt);
3962 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003963 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003965 for (i = 0; i < arg_len; i++)
3966 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3967 if (lv_len > 0)
3968 for (li = list->lv_first; li != NULL;
3969 li = li->li_next)
3970 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 }
3972
3973 /* For "function(dict.func, [], dict)" and "func" is a partial
3974 * use "dict". That is backwards compatible. */
3975 if (dict_idx > 0)
3976 {
3977 /* The dict is bound explicitly, pt_auto is FALSE. */
3978 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3979 ++pt->pt_dict->dv_refcount;
3980 }
3981 else if (arg_pt != NULL)
3982 {
3983 /* If the dict was bound automatically the result is also
3984 * bound automatically. */
3985 pt->pt_dict = arg_pt->pt_dict;
3986 pt->pt_auto = arg_pt->pt_auto;
3987 if (pt->pt_dict != NULL)
3988 ++pt->pt_dict->dv_refcount;
3989 }
3990
3991 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003992 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3993 {
3994 pt->pt_func = arg_pt->pt_func;
3995 func_ptr_ref(pt->pt_func);
3996 vim_free(name);
3997 }
3998 else if (is_funcref)
3999 {
4000 pt->pt_func = find_func(trans_name);
4001 func_ptr_ref(pt->pt_func);
4002 vim_free(name);
4003 }
4004 else
4005 {
4006 pt->pt_name = name;
4007 func_ref(name);
4008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009 }
4010 rettv->v_type = VAR_PARTIAL;
4011 rettv->vval.v_partial = pt;
4012 }
4013 else
4014 {
4015 /* result is a VAR_FUNC */
4016 rettv->v_type = VAR_FUNC;
4017 rettv->vval.v_string = name;
4018 func_ref(name);
4019 }
4020 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004021theend:
4022 vim_free(trans_name);
4023}
4024
4025/*
4026 * "funcref()" function
4027 */
4028 static void
4029f_funcref(typval_T *argvars, typval_T *rettv)
4030{
4031 common_function(argvars, rettv, TRUE);
4032}
4033
4034/*
4035 * "function()" function
4036 */
4037 static void
4038f_function(typval_T *argvars, typval_T *rettv)
4039{
4040 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041}
4042
4043/*
4044 * "garbagecollect()" function
4045 */
4046 static void
4047f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4048{
4049 /* This is postponed until we are back at the toplevel, because we may be
4050 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4051 want_garbage_collect = TRUE;
4052
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004053 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 garbage_collect_at_exit = TRUE;
4055}
4056
4057/*
4058 * "get()" function
4059 */
4060 static void
4061f_get(typval_T *argvars, typval_T *rettv)
4062{
4063 listitem_T *li;
4064 list_T *l;
4065 dictitem_T *di;
4066 dict_T *d;
4067 typval_T *tv = NULL;
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004068 int what_is_dict = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004070 if (argvars[0].v_type == VAR_BLOB)
4071 {
4072 int error = FALSE;
4073 int idx = tv_get_number_chk(&argvars[1], &error);
4074
4075 if (!error)
4076 {
4077 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004078 if (idx < 0)
4079 idx = blob_len(argvars[0].vval.v_blob) + idx;
4080 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4081 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004082 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004083 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004084 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004085 tv = rettv;
4086 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004087 }
4088 }
4089 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090 {
4091 if ((l = argvars[0].vval.v_list) != NULL)
4092 {
4093 int error = FALSE;
4094
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004095 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 if (!error && li != NULL)
4097 tv = &li->li_tv;
4098 }
4099 }
4100 else if (argvars[0].v_type == VAR_DICT)
4101 {
4102 if ((d = argvars[0].vval.v_dict) != NULL)
4103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004104 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 if (di != NULL)
4106 tv = &di->di_tv;
4107 }
4108 }
4109 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4110 {
4111 partial_T *pt;
4112 partial_T fref_pt;
4113
4114 if (argvars[0].v_type == VAR_PARTIAL)
4115 pt = argvars[0].vval.v_partial;
4116 else
4117 {
4118 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4119 fref_pt.pt_name = argvars[0].vval.v_string;
4120 pt = &fref_pt;
4121 }
4122
4123 if (pt != NULL)
4124 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004125 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004126 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004127
4128 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4129 {
4130 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004131 n = partial_name(pt);
4132 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004133 rettv->vval.v_string = NULL;
4134 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004135 {
4136 rettv->vval.v_string = vim_strsave(n);
4137 if (rettv->v_type == VAR_FUNC)
4138 func_ref(rettv->vval.v_string);
4139 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004140 }
4141 else if (STRCMP(what, "dict") == 0)
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004142 {
4143 what_is_dict = TRUE;
4144 if (pt->pt_dict != NULL)
4145 rettv_dict_set(rettv, pt->pt_dict);
4146 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 else if (STRCMP(what, "args") == 0)
4148 {
4149 rettv->v_type = VAR_LIST;
4150 if (rettv_list_alloc(rettv) == OK)
4151 {
4152 int i;
4153
4154 for (i = 0; i < pt->pt_argc; ++i)
4155 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4156 }
4157 }
4158 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004159 semsg(_(e_invarg2), what);
Bram Moolenaarf91aac52019-07-28 13:21:01 +02004160
4161 // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
4162 // third argument
4163 if (!what_is_dict)
4164 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165 }
4166 }
4167 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004168 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004169
4170 if (tv == NULL)
4171 {
4172 if (argvars[2].v_type != VAR_UNKNOWN)
4173 copy_tv(&argvars[2], rettv);
4174 }
4175 else
4176 copy_tv(tv, rettv);
4177}
4178
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004179/*
4180 * Returns buffer options, variables and other attributes in a dictionary.
4181 */
4182 static dict_T *
4183get_buffer_info(buf_T *buf)
4184{
4185 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004186 tabpage_T *tp;
4187 win_T *wp;
4188 list_T *windows;
4189
4190 dict = dict_alloc();
4191 if (dict == NULL)
4192 return NULL;
4193
Bram Moolenaare0be1672018-07-08 16:50:37 +02004194 dict_add_number(dict, "bufnr", buf->b_fnum);
4195 dict_add_string(dict, "name", buf->b_ffname);
4196 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4197 : buflist_findlnum(buf));
4198 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4199 dict_add_number(dict, "listed", buf->b_p_bl);
4200 dict_add_number(dict, "changed", bufIsChanged(buf));
4201 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4202 dict_add_number(dict, "hidden",
4203 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004204
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004205 // Get a reference to buffer variables
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004206 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004207
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004208 // List of windows displaying this buffer
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004209 windows = list_alloc();
4210 if (windows != NULL)
4211 {
4212 FOR_ALL_TAB_WINDOWS(tp, wp)
4213 if (wp->w_buffer == buf)
4214 list_append_number(windows, (varnumber_T)wp->w_id);
4215 dict_add_list(dict, "windows", windows);
4216 }
4217
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02004218#ifdef FEAT_TEXT_PROP
4219 // List of popup windows displaying this buffer
4220 windows = list_alloc();
4221 if (windows != NULL)
4222 {
4223 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4224 if (wp->w_buffer == buf)
4225 list_append_number(windows, (varnumber_T)wp->w_id);
4226 FOR_ALL_TABPAGES(tp)
4227 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
4228 if (wp->w_buffer == buf)
4229 list_append_number(windows, (varnumber_T)wp->w_id);
4230
4231 dict_add_list(dict, "popups", windows);
4232 }
4233#endif
4234
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004235#ifdef FEAT_SIGNS
4236 if (buf->b_signlist != NULL)
4237 {
4238 /* List of signs placed in this buffer */
4239 list_T *signs = list_alloc();
4240 if (signs != NULL)
4241 {
4242 get_buffer_signs(buf, signs);
4243 dict_add_list(dict, "signs", signs);
4244 }
4245 }
4246#endif
4247
4248 return dict;
4249}
4250
4251/*
4252 * "getbufinfo()" function
4253 */
4254 static void
4255f_getbufinfo(typval_T *argvars, typval_T *rettv)
4256{
4257 buf_T *buf = NULL;
4258 buf_T *argbuf = NULL;
4259 dict_T *d;
4260 int filtered = FALSE;
4261 int sel_buflisted = FALSE;
4262 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004263 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004264
4265 if (rettv_list_alloc(rettv) != OK)
4266 return;
4267
4268 /* List of all the buffers or selected buffers */
4269 if (argvars[0].v_type == VAR_DICT)
4270 {
4271 dict_T *sel_d = argvars[0].vval.v_dict;
4272
4273 if (sel_d != NULL)
4274 {
4275 dictitem_T *di;
4276
4277 filtered = TRUE;
4278
4279 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004280 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004281 sel_buflisted = TRUE;
4282
4283 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004284 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004285 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004286
4287 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004288 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004289 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004290 }
4291 }
4292 else if (argvars[0].v_type != VAR_UNKNOWN)
4293 {
4294 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004295 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004296 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004297 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004298 --emsg_off;
4299 if (argbuf == NULL)
4300 return;
4301 }
4302
4303 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004304 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004305 {
4306 if (argbuf != NULL && argbuf != buf)
4307 continue;
4308 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004309 || (sel_buflisted && !buf->b_p_bl)
4310 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004311 continue;
4312
4313 d = get_buffer_info(buf);
4314 if (d != NULL)
4315 list_append_dict(rettv->vval.v_list, d);
4316 if (argbuf != NULL)
4317 return;
4318 }
4319}
4320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321/*
4322 * Get line or list of lines from buffer "buf" into "rettv".
4323 * Return a range (from start to end) of lines in rettv from the specified
4324 * buffer.
4325 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4326 */
4327 static void
4328get_buffer_lines(
4329 buf_T *buf,
4330 linenr_T start,
4331 linenr_T end,
4332 int retlist,
4333 typval_T *rettv)
4334{
4335 char_u *p;
4336
4337 rettv->v_type = VAR_STRING;
4338 rettv->vval.v_string = NULL;
4339 if (retlist && rettv_list_alloc(rettv) == FAIL)
4340 return;
4341
4342 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4343 return;
4344
4345 if (!retlist)
4346 {
4347 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4348 p = ml_get_buf(buf, start, FALSE);
4349 else
4350 p = (char_u *)"";
4351 rettv->vval.v_string = vim_strsave(p);
4352 }
4353 else
4354 {
4355 if (end < start)
4356 return;
4357
4358 if (start < 1)
4359 start = 1;
4360 if (end > buf->b_ml.ml_line_count)
4361 end = buf->b_ml.ml_line_count;
4362 while (start <= end)
4363 if (list_append_string(rettv->vval.v_list,
4364 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4365 break;
4366 }
4367}
4368
4369/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 * "getbufline()" function
4371 */
4372 static void
4373f_getbufline(typval_T *argvars, typval_T *rettv)
4374{
4375 linenr_T lnum;
4376 linenr_T end;
4377 buf_T *buf;
4378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004379 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004380 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004381 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 --emsg_off;
4383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004384 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004385 if (argvars[2].v_type == VAR_UNKNOWN)
4386 end = lnum;
4387 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004388 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389
4390 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4391}
4392
4393/*
4394 * "getbufvar()" function
4395 */
4396 static void
4397f_getbufvar(typval_T *argvars, typval_T *rettv)
4398{
4399 buf_T *buf;
4400 buf_T *save_curbuf;
4401 char_u *varname;
4402 dictitem_T *v;
4403 int done = FALSE;
4404
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004405 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4406 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004407 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004408 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409
4410 rettv->v_type = VAR_STRING;
4411 rettv->vval.v_string = NULL;
4412
4413 if (buf != NULL && varname != NULL)
4414 {
4415 /* set curbuf to be our buf, temporarily */
4416 save_curbuf = curbuf;
4417 curbuf = buf;
4418
Bram Moolenaar30567352016-08-27 21:25:44 +02004419 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004421 if (varname[1] == NUL)
4422 {
4423 /* get all buffer-local options in a dict */
4424 dict_T *opts = get_winbuf_options(TRUE);
4425
4426 if (opts != NULL)
4427 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004428 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004429 done = TRUE;
4430 }
4431 }
4432 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4433 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434 done = TRUE;
4435 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436 else
4437 {
4438 /* Look up the variable. */
4439 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4440 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4441 'b', varname, FALSE);
4442 if (v != NULL)
4443 {
4444 copy_tv(&v->di_tv, rettv);
4445 done = TRUE;
4446 }
4447 }
4448
4449 /* restore previous notion of curbuf */
4450 curbuf = save_curbuf;
4451 }
4452
4453 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4454 /* use the default value */
4455 copy_tv(&argvars[2], rettv);
4456
4457 --emsg_off;
4458}
4459
4460/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004461 * "getchangelist()" function
4462 */
4463 static void
4464f_getchangelist(typval_T *argvars, typval_T *rettv)
4465{
4466#ifdef FEAT_JUMPLIST
4467 buf_T *buf;
4468 int i;
4469 list_T *l;
4470 dict_T *d;
4471#endif
4472
4473 if (rettv_list_alloc(rettv) != OK)
4474 return;
4475
4476#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004477 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004478 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004479 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004480 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004481 if (buf == NULL)
4482 return;
4483
4484 l = list_alloc();
4485 if (l == NULL)
4486 return;
4487
4488 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4489 return;
4490 /*
4491 * The current window change list index tracks only the position in the
4492 * current buffer change list. For other buffers, use the change list
4493 * length as the current index.
4494 */
4495 list_append_number(rettv->vval.v_list,
4496 (varnumber_T)((buf == curwin->w_buffer)
4497 ? curwin->w_changelistidx : buf->b_changelistlen));
4498
4499 for (i = 0; i < buf->b_changelistlen; ++i)
4500 {
4501 if (buf->b_changelist[i].lnum == 0)
4502 continue;
4503 if ((d = dict_alloc()) == NULL)
4504 return;
4505 if (list_append_dict(l, d) == FAIL)
4506 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004507 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4508 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004509 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004510 }
4511#endif
4512}
4513/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514 * "getchar()" function
4515 */
4516 static void
4517f_getchar(typval_T *argvars, typval_T *rettv)
4518{
4519 varnumber_T n;
4520 int error = FALSE;
4521
Bram Moolenaar84d93902018-09-11 20:10:20 +02004522#ifdef MESSAGE_QUEUE
4523 // vpeekc() used to check for messages, but that caused problems, invoking
4524 // a callback where it was not expected. Some plugins use getchar(1) in a
4525 // loop to await a message, therefore make sure we check for messages here.
4526 parse_queued_messages();
4527#endif
4528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004529 /* Position the cursor. Needed after a message that ends in a space. */
4530 windgoto(msg_row, msg_col);
4531
4532 ++no_mapping;
4533 ++allow_keys;
4534 for (;;)
4535 {
4536 if (argvars[0].v_type == VAR_UNKNOWN)
4537 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004538 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004539 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 /* getchar(1): only check if char avail */
4541 n = vpeekc_any();
4542 else if (error || vpeekc_any() == NUL)
4543 /* illegal argument or getchar(0) and no char avail: return zero */
4544 n = 0;
4545 else
4546 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004547 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548
4549 if (n == K_IGNORE)
4550 continue;
4551 break;
4552 }
4553 --no_mapping;
4554 --allow_keys;
4555
4556 set_vim_var_nr(VV_MOUSE_WIN, 0);
4557 set_vim_var_nr(VV_MOUSE_WINID, 0);
4558 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4559 set_vim_var_nr(VV_MOUSE_COL, 0);
4560
4561 rettv->vval.v_number = n;
4562 if (IS_SPECIAL(n) || mod_mask != 0)
4563 {
4564 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4565 int i = 0;
4566
4567 /* Turn a special key into three bytes, plus modifier. */
4568 if (mod_mask != 0)
4569 {
4570 temp[i++] = K_SPECIAL;
4571 temp[i++] = KS_MODIFIER;
4572 temp[i++] = mod_mask;
4573 }
4574 if (IS_SPECIAL(n))
4575 {
4576 temp[i++] = K_SPECIAL;
4577 temp[i++] = K_SECOND(n);
4578 temp[i++] = K_THIRD(n);
4579 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004580 else if (has_mbyte)
4581 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582 else
4583 temp[i++] = n;
4584 temp[i++] = NUL;
4585 rettv->v_type = VAR_STRING;
4586 rettv->vval.v_string = vim_strsave(temp);
4587
4588#ifdef FEAT_MOUSE
4589 if (is_mouse_key(n))
4590 {
4591 int row = mouse_row;
4592 int col = mouse_col;
4593 win_T *win;
4594 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 int winnr = 1;
4597
4598 if (row >= 0 && col >= 0)
4599 {
4600 /* Find the window at the mouse coordinates and compute the
4601 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004602 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004603 if (win == NULL)
4604 return;
Bram Moolenaar9d5ffce2019-07-26 21:01:29 +02004605 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004606# ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004607 if (WIN_IS_POPUP(win))
Bram Moolenaar451d4b52019-06-12 20:22:27 +02004608 winnr = 0;
4609 else
4610# endif
4611 for (wp = firstwin; wp != win && wp != NULL;
4612 wp = wp->w_next)
4613 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004614 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4615 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4616 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4617 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4618 }
4619 }
4620#endif
4621 }
4622}
4623
4624/*
4625 * "getcharmod()" function
4626 */
4627 static void
4628f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4629{
4630 rettv->vval.v_number = mod_mask;
4631}
4632
4633/*
4634 * "getcharsearch()" function
4635 */
4636 static void
4637f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4638{
4639 if (rettv_dict_alloc(rettv) != FAIL)
4640 {
4641 dict_T *dict = rettv->vval.v_dict;
4642
Bram Moolenaare0be1672018-07-08 16:50:37 +02004643 dict_add_string(dict, "char", last_csearch());
4644 dict_add_number(dict, "forward", last_csearch_forward());
4645 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004646 }
4647}
4648
4649/*
4650 * "getcmdline()" function
4651 */
4652 static void
4653f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4654{
4655 rettv->v_type = VAR_STRING;
4656 rettv->vval.v_string = get_cmdline_str();
4657}
4658
4659/*
4660 * "getcmdpos()" function
4661 */
4662 static void
4663f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4664{
4665 rettv->vval.v_number = get_cmdline_pos() + 1;
4666}
4667
4668/*
4669 * "getcmdtype()" function
4670 */
4671 static void
4672f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4673{
4674 rettv->v_type = VAR_STRING;
4675 rettv->vval.v_string = alloc(2);
4676 if (rettv->vval.v_string != NULL)
4677 {
4678 rettv->vval.v_string[0] = get_cmdline_type();
4679 rettv->vval.v_string[1] = NUL;
4680 }
4681}
4682
4683/*
4684 * "getcmdwintype()" function
4685 */
4686 static void
4687f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4688{
4689 rettv->v_type = VAR_STRING;
4690 rettv->vval.v_string = NULL;
4691#ifdef FEAT_CMDWIN
4692 rettv->vval.v_string = alloc(2);
4693 if (rettv->vval.v_string != NULL)
4694 {
4695 rettv->vval.v_string[0] = cmdwin_type;
4696 rettv->vval.v_string[1] = NUL;
4697 }
4698#endif
4699}
4700
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701/*
4702 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004703 *
4704 * Return the current working directory of a window in a tab page.
4705 * First optional argument 'winnr' is the window number or -1 and the second
4706 * optional argument 'tabnr' is the tab page number.
4707 *
4708 * If no arguments are supplied, then return the directory of the current
4709 * window.
4710 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
4711 * the specified window.
4712 * If 'winnr' is 0 then return the directory of the current window.
4713 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
4714 * directory of the specified tab page. Otherwise return the directory of the
4715 * specified window in the specified tab page.
4716 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717 */
4718 static void
4719f_getcwd(typval_T *argvars, typval_T *rettv)
4720{
4721 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004722 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004723 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004724 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004725
4726 rettv->v_type = VAR_STRING;
4727 rettv->vval.v_string = NULL;
4728
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004729 if (argvars[0].v_type == VAR_NUMBER
4730 && argvars[0].vval.v_number == -1
4731 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01004732 global = TRUE;
4733 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004734 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01004735
4736 if (wp != NULL && wp->w_localdir != NULL)
4737 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004738 else if (tp != NULL && tp->tp_localdir != NULL)
4739 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
4740 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004742 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 rettv->vval.v_string = vim_strsave(globaldir);
4744 else
4745 {
4746 cwd = alloc(MAXPATHL);
4747 if (cwd != NULL)
4748 {
4749 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4750 rettv->vval.v_string = vim_strsave(cwd);
4751 vim_free(cwd);
4752 }
4753 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004754 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004755#ifdef BACKSLASH_IN_FILENAME
4756 if (rettv->vval.v_string != NULL)
4757 slash_adjust(rettv->vval.v_string);
4758#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004759}
4760
4761/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02004762 * "getenv()" function
4763 */
4764 static void
4765f_getenv(typval_T *argvars, typval_T *rettv)
4766{
4767 int mustfree = FALSE;
4768 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
4769
4770 if (p == NULL)
4771 {
4772 rettv->v_type = VAR_SPECIAL;
4773 rettv->vval.v_number = VVAL_NULL;
4774 return;
4775 }
4776 if (!mustfree)
4777 p = vim_strsave(p);
4778 rettv->vval.v_string = p;
4779 rettv->v_type = VAR_STRING;
4780}
4781
4782/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004783 * "getfontname()" function
4784 */
4785 static void
4786f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4787{
4788 rettv->v_type = VAR_STRING;
4789 rettv->vval.v_string = NULL;
4790#ifdef FEAT_GUI
4791 if (gui.in_use)
4792 {
4793 GuiFont font;
4794 char_u *name = NULL;
4795
4796 if (argvars[0].v_type == VAR_UNKNOWN)
4797 {
4798 /* Get the "Normal" font. Either the name saved by
4799 * hl_set_font_name() or from the font ID. */
4800 font = gui.norm_font;
4801 name = hl_get_font_name();
4802 }
4803 else
4804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004806 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4807 return;
4808 font = gui_mch_get_font(name, FALSE);
4809 if (font == NOFONT)
4810 return; /* Invalid font name, return empty string. */
4811 }
4812 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4813 if (argvars[0].v_type != VAR_UNKNOWN)
4814 gui_mch_free_font(font);
4815 }
4816#endif
4817}
4818
4819/*
4820 * "getfperm({fname})" function
4821 */
4822 static void
4823f_getfperm(typval_T *argvars, typval_T *rettv)
4824{
4825 char_u *fname;
4826 stat_T st;
4827 char_u *perm = NULL;
4828 char_u flags[] = "rwx";
4829 int i;
4830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004831 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004832
4833 rettv->v_type = VAR_STRING;
4834 if (mch_stat((char *)fname, &st) >= 0)
4835 {
4836 perm = vim_strsave((char_u *)"---------");
4837 if (perm != NULL)
4838 {
4839 for (i = 0; i < 9; i++)
4840 {
4841 if (st.st_mode & (1 << (8 - i)))
4842 perm[i] = flags[i % 3];
4843 }
4844 }
4845 }
4846 rettv->vval.v_string = perm;
4847}
4848
4849/*
4850 * "getfsize({fname})" function
4851 */
4852 static void
4853f_getfsize(typval_T *argvars, typval_T *rettv)
4854{
4855 char_u *fname;
4856 stat_T st;
4857
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004858 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859
4860 rettv->v_type = VAR_NUMBER;
4861
4862 if (mch_stat((char *)fname, &st) >= 0)
4863 {
4864 if (mch_isdir(fname))
4865 rettv->vval.v_number = 0;
4866 else
4867 {
4868 rettv->vval.v_number = (varnumber_T)st.st_size;
4869
4870 /* non-perfect check for overflow */
4871 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4872 rettv->vval.v_number = -2;
4873 }
4874 }
4875 else
4876 rettv->vval.v_number = -1;
4877}
4878
4879/*
4880 * "getftime({fname})" function
4881 */
4882 static void
4883f_getftime(typval_T *argvars, typval_T *rettv)
4884{
4885 char_u *fname;
4886 stat_T st;
4887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004888 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889
4890 if (mch_stat((char *)fname, &st) >= 0)
4891 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4892 else
4893 rettv->vval.v_number = -1;
4894}
4895
4896/*
4897 * "getftype({fname})" function
4898 */
4899 static void
4900f_getftype(typval_T *argvars, typval_T *rettv)
4901{
4902 char_u *fname;
4903 stat_T st;
4904 char_u *type = NULL;
4905 char *t;
4906
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004907 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908
4909 rettv->v_type = VAR_STRING;
4910 if (mch_lstat((char *)fname, &st) >= 0)
4911 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 if (S_ISREG(st.st_mode))
4913 t = "file";
4914 else if (S_ISDIR(st.st_mode))
4915 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004916 else if (S_ISLNK(st.st_mode))
4917 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 else if (S_ISBLK(st.st_mode))
4919 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004920 else if (S_ISCHR(st.st_mode))
4921 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922 else if (S_ISFIFO(st.st_mode))
4923 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02004925 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926 else
4927 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928 type = vim_strsave((char_u *)t);
4929 }
4930 rettv->vval.v_string = type;
4931}
4932
4933/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004934 * "getjumplist()" function
4935 */
4936 static void
4937f_getjumplist(typval_T *argvars, typval_T *rettv)
4938{
4939#ifdef FEAT_JUMPLIST
4940 win_T *wp;
4941 int i;
4942 list_T *l;
4943 dict_T *d;
4944#endif
4945
4946 if (rettv_list_alloc(rettv) != OK)
4947 return;
4948
4949#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004950 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004951 if (wp == NULL)
4952 return;
4953
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01004954 cleanup_jumplist(wp, TRUE);
4955
Bram Moolenaar4f505882018-02-10 21:06:32 +01004956 l = list_alloc();
4957 if (l == NULL)
4958 return;
4959
4960 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4961 return;
4962 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4963
4964 for (i = 0; i < wp->w_jumplistlen; ++i)
4965 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004966 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4967 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004968 if ((d = dict_alloc()) == NULL)
4969 return;
4970 if (list_append_dict(l, d) == FAIL)
4971 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004972 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
4973 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004974 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004975 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004976 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02004977 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004978 }
4979#endif
4980}
4981
4982/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004983 * "getline(lnum, [end])" function
4984 */
4985 static void
4986f_getline(typval_T *argvars, typval_T *rettv)
4987{
4988 linenr_T lnum;
4989 linenr_T end;
4990 int retlist;
4991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004993 if (argvars[1].v_type == VAR_UNKNOWN)
4994 {
4995 end = 0;
4996 retlist = FALSE;
4997 }
4998 else
4999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005000 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001 retlist = TRUE;
5002 }
5003
5004 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5005}
5006
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005007#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005008 static void
5009get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5010{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005011 if (what_arg->v_type == VAR_UNKNOWN)
5012 {
5013 if (rettv_list_alloc(rettv) == OK)
5014 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005015 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005016 }
5017 else
5018 {
5019 if (rettv_dict_alloc(rettv) == OK)
5020 if (is_qf || (wp != NULL))
5021 {
5022 if (what_arg->v_type == VAR_DICT)
5023 {
5024 dict_T *d = what_arg->vval.v_dict;
5025
5026 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005027 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005028 }
5029 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005030 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005031 }
5032 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005033}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005034#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005035
5036/*
5037 * "getloclist()" function
5038 */
5039 static void
5040f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5041{
5042#ifdef FEAT_QUICKFIX
5043 win_T *wp;
5044
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005045 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005046 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5047#endif
5048}
5049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005050/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005051 * "getpid()" function
5052 */
5053 static void
5054f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5055{
5056 rettv->vval.v_number = mch_get_pid();
5057}
5058
5059 static void
5060getpos_both(
5061 typval_T *argvars,
5062 typval_T *rettv,
5063 int getcurpos)
5064{
5065 pos_T *fp;
5066 list_T *l;
5067 int fnum = -1;
5068
5069 if (rettv_list_alloc(rettv) == OK)
5070 {
5071 l = rettv->vval.v_list;
5072 if (getcurpos)
5073 fp = &curwin->w_cursor;
5074 else
5075 fp = var2fpos(&argvars[0], TRUE, &fnum);
5076 if (fnum != -1)
5077 list_append_number(l, (varnumber_T)fnum);
5078 else
5079 list_append_number(l, (varnumber_T)0);
5080 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5081 : (varnumber_T)0);
5082 list_append_number(l, (fp != NULL)
5083 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5084 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005085 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 (varnumber_T)0);
5087 if (getcurpos)
5088 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005089 int save_set_curswant = curwin->w_set_curswant;
5090 colnr_T save_curswant = curwin->w_curswant;
5091 colnr_T save_virtcol = curwin->w_virtcol;
5092
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 update_curswant();
5094 list_append_number(l, curwin->w_curswant == MAXCOL ?
5095 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005096
5097 // Do not change "curswant", as it is unexpected that a get
5098 // function has a side effect.
5099 if (save_set_curswant)
5100 {
5101 curwin->w_set_curswant = save_set_curswant;
5102 curwin->w_curswant = save_curswant;
5103 curwin->w_virtcol = save_virtcol;
5104 curwin->w_valid &= ~VALID_VIRTCOL;
5105 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005106 }
5107 }
5108 else
5109 rettv->vval.v_number = FALSE;
5110}
5111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112/*
5113 * "getcurpos()" function
5114 */
5115 static void
5116f_getcurpos(typval_T *argvars, typval_T *rettv)
5117{
5118 getpos_both(argvars, rettv, TRUE);
5119}
5120
5121/*
5122 * "getpos(string)" function
5123 */
5124 static void
5125f_getpos(typval_T *argvars, typval_T *rettv)
5126{
5127 getpos_both(argvars, rettv, FALSE);
5128}
5129
5130/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005131 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005132 */
5133 static void
5134f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5135{
5136#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005137 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005138#endif
5139}
5140
5141/*
5142 * "getreg()" function
5143 */
5144 static void
5145f_getreg(typval_T *argvars, typval_T *rettv)
5146{
5147 char_u *strregname;
5148 int regname;
5149 int arg2 = FALSE;
5150 int return_list = FALSE;
5151 int error = FALSE;
5152
5153 if (argvars[0].v_type != VAR_UNKNOWN)
5154 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005155 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005156 error = strregname == NULL;
5157 if (argvars[1].v_type != VAR_UNKNOWN)
5158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005159 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005161 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005162 }
5163 }
5164 else
5165 strregname = get_vim_var_str(VV_REG);
5166
5167 if (error)
5168 return;
5169
5170 regname = (strregname == NULL ? '"' : *strregname);
5171 if (regname == 0)
5172 regname = '"';
5173
5174 if (return_list)
5175 {
5176 rettv->v_type = VAR_LIST;
5177 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5178 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5179 if (rettv->vval.v_list == NULL)
5180 (void)rettv_list_alloc(rettv);
5181 else
5182 ++rettv->vval.v_list->lv_refcount;
5183 }
5184 else
5185 {
5186 rettv->v_type = VAR_STRING;
5187 rettv->vval.v_string = get_reg_contents(regname,
5188 arg2 ? GREG_EXPR_SRC : 0);
5189 }
5190}
5191
5192/*
5193 * "getregtype()" function
5194 */
5195 static void
5196f_getregtype(typval_T *argvars, typval_T *rettv)
5197{
5198 char_u *strregname;
5199 int regname;
5200 char_u buf[NUMBUFLEN + 2];
5201 long reglen = 0;
5202
5203 if (argvars[0].v_type != VAR_UNKNOWN)
5204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206 if (strregname == NULL) /* type error; errmsg already given */
5207 {
5208 rettv->v_type = VAR_STRING;
5209 rettv->vval.v_string = NULL;
5210 return;
5211 }
5212 }
5213 else
5214 /* Default to v:register */
5215 strregname = get_vim_var_str(VV_REG);
5216
5217 regname = (strregname == NULL ? '"' : *strregname);
5218 if (regname == 0)
5219 regname = '"';
5220
5221 buf[0] = NUL;
5222 buf[1] = NUL;
5223 switch (get_reg_type(regname, &reglen))
5224 {
5225 case MLINE: buf[0] = 'V'; break;
5226 case MCHAR: buf[0] = 'v'; break;
5227 case MBLOCK:
5228 buf[0] = Ctrl_V;
5229 sprintf((char *)buf + 1, "%ld", reglen + 1);
5230 break;
5231 }
5232 rettv->v_type = VAR_STRING;
5233 rettv->vval.v_string = vim_strsave(buf);
5234}
5235
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005236/*
5237 * Returns information (variables, options, etc.) about a tab page
5238 * as a dictionary.
5239 */
5240 static dict_T *
5241get_tabpage_info(tabpage_T *tp, int tp_idx)
5242{
5243 win_T *wp;
5244 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005245 list_T *l;
5246
5247 dict = dict_alloc();
5248 if (dict == NULL)
5249 return NULL;
5250
Bram Moolenaare0be1672018-07-08 16:50:37 +02005251 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005252
5253 l = list_alloc();
5254 if (l != NULL)
5255 {
5256 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02005257 wp != NULL; wp = wp->w_next)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005258 list_append_number(l, (varnumber_T)wp->w_id);
5259 dict_add_list(dict, "windows", l);
5260 }
5261
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005262 /* Make a reference to tabpage variables */
5263 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005264
5265 return dict;
5266}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005267
5268/*
5269 * "gettabinfo()" function
5270 */
5271 static void
5272f_gettabinfo(typval_T *argvars, typval_T *rettv)
5273{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005274 tabpage_T *tp, *tparg = NULL;
5275 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005276 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005277
5278 if (rettv_list_alloc(rettv) != OK)
5279 return;
5280
5281 if (argvars[0].v_type != VAR_UNKNOWN)
5282 {
5283 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005284 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005285 if (tparg == NULL)
5286 return;
5287 }
5288
5289 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005290 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005291 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005292 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005293 if (tparg != NULL && tp != tparg)
5294 continue;
5295 d = get_tabpage_info(tp, tpnr);
5296 if (d != NULL)
5297 list_append_dict(rettv->vval.v_list, d);
5298 if (tparg != NULL)
5299 return;
5300 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005301}
5302
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005303/*
5304 * "gettabvar()" function
5305 */
5306 static void
5307f_gettabvar(typval_T *argvars, typval_T *rettv)
5308{
5309 win_T *oldcurwin;
5310 tabpage_T *tp, *oldtabpage;
5311 dictitem_T *v;
5312 char_u *varname;
5313 int done = FALSE;
5314
5315 rettv->v_type = VAR_STRING;
5316 rettv->vval.v_string = NULL;
5317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005318 varname = tv_get_string_chk(&argvars[1]);
5319 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005320 if (tp != NULL && varname != NULL)
5321 {
5322 /* Set tp to be our tabpage, temporarily. Also set the window to the
5323 * first window in the tabpage, otherwise the window is not valid. */
5324 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005325 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5326 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 {
5328 /* look up the variable */
5329 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5330 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5331 if (v != NULL)
5332 {
5333 copy_tv(&v->di_tv, rettv);
5334 done = TRUE;
5335 }
5336 }
5337
5338 /* restore previous notion of curwin */
5339 restore_win(oldcurwin, oldtabpage, TRUE);
5340 }
5341
5342 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5343 copy_tv(&argvars[2], rettv);
5344}
5345
5346/*
5347 * "gettabwinvar()" function
5348 */
5349 static void
5350f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5351{
5352 getwinvar(argvars, rettv, 1);
5353}
5354
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005355/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005356 * "gettagstack()" function
5357 */
5358 static void
5359f_gettagstack(typval_T *argvars, typval_T *rettv)
5360{
5361 win_T *wp = curwin; // default is current window
5362
5363 if (rettv_dict_alloc(rettv) != OK)
5364 return;
5365
5366 if (argvars[0].v_type != VAR_UNKNOWN)
5367 {
5368 wp = find_win_by_nr_or_id(&argvars[0]);
5369 if (wp == NULL)
5370 return;
5371 }
5372
5373 get_tagstack(wp, rettv->vval.v_dict);
5374}
5375
5376/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005377 * Returns information about a window as a dictionary.
5378 */
5379 static dict_T *
5380get_win_info(win_T *wp, short tpnr, short winnr)
5381{
5382 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005383
5384 dict = dict_alloc();
5385 if (dict == NULL)
5386 return NULL;
5387
Bram Moolenaare0be1672018-07-08 16:50:37 +02005388 dict_add_number(dict, "tabnr", tpnr);
5389 dict_add_number(dict, "winnr", winnr);
5390 dict_add_number(dict, "winid", wp->w_id);
5391 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005392 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005393 dict_add_number(dict, "topline", wp->w_topline);
5394 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005395#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005396 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005397#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005398 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005399 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005400 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005401
Bram Moolenaar69905d12017-08-13 18:14:47 +02005402#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005403 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005404#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005405#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005406 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5407 dict_add_number(dict, "loclist",
5408 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005409#endif
5410
Bram Moolenaar30567352016-08-27 21:25:44 +02005411 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005412 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005413
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005414 return dict;
5415}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005416
5417/*
5418 * "getwininfo()" function
5419 */
5420 static void
5421f_getwininfo(typval_T *argvars, typval_T *rettv)
5422{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005423 tabpage_T *tp;
5424 win_T *wp = NULL, *wparg = NULL;
5425 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005426 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005427
5428 if (rettv_list_alloc(rettv) != OK)
5429 return;
5430
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005431 if (argvars[0].v_type != VAR_UNKNOWN)
5432 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005433 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005434 if (wparg == NULL)
5435 return;
5436 }
5437
5438 /* Collect information about either all the windows across all the tab
5439 * pages or one particular window.
5440 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005441 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005442 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005443 tabnr++;
5444 winnr = 0;
5445 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005446 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005447 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005448 if (wparg != NULL && wp != wparg)
5449 continue;
5450 d = get_win_info(wp, tabnr, winnr);
5451 if (d != NULL)
5452 list_append_dict(rettv->vval.v_list, d);
5453 if (wparg != NULL)
5454 /* found information about a specific window */
5455 return;
5456 }
5457 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005458}
5459
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005461 * "win_execute()" function
5462 */
5463 static void
5464f_win_execute(typval_T *argvars, typval_T *rettv)
5465{
5466 int id = (int)tv_get_number(argvars);
Bram Moolenaar820680b2019-08-09 14:56:22 +02005467 tabpage_T *tp;
5468 win_T *wp = win_id2wp_tp(id, &tp);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005469 win_T *save_curwin;
5470 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005471
Bram Moolenaar820680b2019-08-09 14:56:22 +02005472 if (wp != NULL && tp != NULL)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005473 {
Bram Moolenaar820680b2019-08-09 14:56:22 +02005474 if (switch_win_noblock(&save_curwin, &save_curtab, wp, tp, TRUE) == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005475 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005476 check_cursor();
5477 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005478 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02005479 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02005480 }
5481}
5482
5483/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 * "win_findbuf()" function
5485 */
5486 static void
5487f_win_findbuf(typval_T *argvars, typval_T *rettv)
5488{
5489 if (rettv_list_alloc(rettv) != FAIL)
5490 win_findbuf(argvars, rettv->vval.v_list);
5491}
5492
5493/*
5494 * "win_getid()" function
5495 */
5496 static void
5497f_win_getid(typval_T *argvars, typval_T *rettv)
5498{
5499 rettv->vval.v_number = win_getid(argvars);
5500}
5501
5502/*
5503 * "win_gotoid()" function
5504 */
5505 static void
5506f_win_gotoid(typval_T *argvars, typval_T *rettv)
5507{
5508 rettv->vval.v_number = win_gotoid(argvars);
5509}
5510
5511/*
5512 * "win_id2tabwin()" function
5513 */
5514 static void
5515f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5516{
5517 if (rettv_list_alloc(rettv) != FAIL)
5518 win_id2tabwin(argvars, rettv->vval.v_list);
5519}
5520
5521/*
5522 * "win_id2win()" function
5523 */
5524 static void
5525f_win_id2win(typval_T *argvars, typval_T *rettv)
5526{
5527 rettv->vval.v_number = win_id2win(argvars);
5528}
5529
5530/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005531 * "win_screenpos()" function
5532 */
5533 static void
5534f_win_screenpos(typval_T *argvars, typval_T *rettv)
5535{
5536 win_T *wp;
5537
5538 if (rettv_list_alloc(rettv) == FAIL)
5539 return;
5540
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005541 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005542 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5543 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5544}
5545
5546/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005547 * "getwinpos({timeout})" function
5548 */
5549 static void
5550f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5551{
5552 int x = -1;
5553 int y = -1;
5554
5555 if (rettv_list_alloc(rettv) == FAIL)
5556 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005557#if defined(FEAT_GUI) \
5558 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5559 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005560 {
5561 varnumber_T timeout = 100;
5562
5563 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005564 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005565
5566 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005567 }
5568#endif
5569 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5570 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5571}
5572
5573
5574/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005575 * "getwinposx()" function
5576 */
5577 static void
5578f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5579{
5580 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005581#if defined(FEAT_GUI) \
5582 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5583 || defined(MSWIN)
5584
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005585 {
5586 int x, y;
5587
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005588 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005589 rettv->vval.v_number = x;
5590 }
5591#endif
5592}
5593
5594/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005595 * "getwinposy()" function
5596 */
5597 static void
5598f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5599{
5600 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02005601#if defined(FEAT_GUI) \
5602 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
5603 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 {
5605 int x, y;
5606
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02005607 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005608 rettv->vval.v_number = y;
5609 }
5610#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611}
5612
5613/*
5614 * "getwinvar()" function
5615 */
5616 static void
5617f_getwinvar(typval_T *argvars, typval_T *rettv)
5618{
5619 getwinvar(argvars, rettv, 0);
5620}
5621
5622/*
5623 * "glob()" function
5624 */
5625 static void
5626f_glob(typval_T *argvars, typval_T *rettv)
5627{
5628 int options = WILD_SILENT|WILD_USE_NL;
5629 expand_T xpc;
5630 int error = FALSE;
5631
5632 /* When the optional second argument is non-zero, don't remove matches
5633 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5634 rettv->v_type = VAR_STRING;
5635 if (argvars[1].v_type != VAR_UNKNOWN)
5636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005637 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638 options |= WILD_KEEP_ALL;
5639 if (argvars[2].v_type != VAR_UNKNOWN)
5640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005641 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005642 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005644 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 options |= WILD_ALLLINKS;
5646 }
5647 }
5648 if (!error)
5649 {
5650 ExpandInit(&xpc);
5651 xpc.xp_context = EXPAND_FILES;
5652 if (p_wic)
5653 options += WILD_ICASE;
5654 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 NULL, options, WILD_ALL);
5657 else if (rettv_list_alloc(rettv) != FAIL)
5658 {
5659 int i;
5660
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005661 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662 NULL, options, WILD_ALL_KEEP);
5663 for (i = 0; i < xpc.xp_numfiles; i++)
5664 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5665
5666 ExpandCleanup(&xpc);
5667 }
5668 }
5669 else
5670 rettv->vval.v_string = NULL;
5671}
5672
5673/*
5674 * "globpath()" function
5675 */
5676 static void
5677f_globpath(typval_T *argvars, typval_T *rettv)
5678{
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005679 int flags = WILD_IGNORE_COMPLETESLASH;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005681 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682 int error = FALSE;
5683 garray_T ga;
5684 int i;
5685
Bram Moolenaar50f91d22019-08-02 19:52:15 +02005686 // When the optional second argument is non-zero, don't remove matches
5687 // for 'wildignore' and don't put matches for 'suffixes' at the end.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 rettv->v_type = VAR_STRING;
5689 if (argvars[2].v_type != VAR_UNKNOWN)
5690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005691 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692 flags |= WILD_KEEP_ALL;
5693 if (argvars[3].v_type != VAR_UNKNOWN)
5694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005695 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005696 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005697 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005698 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699 flags |= WILD_ALLLINKS;
5700 }
5701 }
5702 if (file != NULL && !error)
5703 {
5704 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005705 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706 if (rettv->v_type == VAR_STRING)
5707 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5708 else if (rettv_list_alloc(rettv) != FAIL)
5709 for (i = 0; i < ga.ga_len; ++i)
5710 list_append_string(rettv->vval.v_list,
5711 ((char_u **)(ga.ga_data))[i], -1);
5712 ga_clear_strings(&ga);
5713 }
5714 else
5715 rettv->vval.v_string = NULL;
5716}
5717
5718/*
5719 * "glob2regpat()" function
5720 */
5721 static void
5722f_glob2regpat(typval_T *argvars, typval_T *rettv)
5723{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005724 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725
5726 rettv->v_type = VAR_STRING;
5727 rettv->vval.v_string = (pat == NULL)
5728 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5729}
5730
5731/* for VIM_VERSION_ defines */
5732#include "version.h"
5733
5734/*
5735 * "has()" function
5736 */
5737 static void
5738f_has(typval_T *argvars, typval_T *rettv)
5739{
5740 int i;
5741 char_u *name;
5742 int n = FALSE;
5743 static char *(has_list[]) =
5744 {
5745#ifdef AMIGA
5746 "amiga",
5747# ifdef FEAT_ARP
5748 "arp",
5749# endif
5750#endif
5751#ifdef __BEOS__
5752 "beos",
5753#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005754#if defined(BSD) && !defined(MACOS_X)
5755 "bsd",
5756#endif
5757#ifdef hpux
5758 "hpux",
5759#endif
5760#ifdef __linux__
5761 "linux",
5762#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005763#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005764 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5765 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005766# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005767 "macunix", /* Mac OS X, with the darwin feature */
5768 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005769# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770#endif
5771#ifdef __QNX__
5772 "qnx",
5773#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005774#ifdef SUN_SYSTEM
5775 "sun",
5776#else
5777 "moon",
5778#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005779#ifdef UNIX
5780 "unix",
5781#endif
5782#ifdef VMS
5783 "vms",
5784#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005785#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786 "win32",
5787#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01005788#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005789 "win32unix",
5790#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01005791#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 "win64",
5793#endif
5794#ifdef EBCDIC
5795 "ebcdic",
5796#endif
5797#ifndef CASE_INSENSITIVE_FILENAME
5798 "fname_case",
5799#endif
5800#ifdef HAVE_ACL
5801 "acl",
5802#endif
5803#ifdef FEAT_ARABIC
5804 "arabic",
5805#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005807#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01005808 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02005809#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005810#ifdef FEAT_AUTOSERVERNAME
5811 "autoservername",
5812#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005813#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01005815# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005816 "balloon_multiline",
5817# endif
5818#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005819#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005820 "balloon_eval_term",
5821#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005822#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5823 "builtin_terms",
5824# ifdef ALL_BUILTIN_TCAPS
5825 "all_builtin_terms",
5826# endif
5827#endif
5828#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01005829 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 || defined(FEAT_GUI_MOTIF))
5831 "browsefilter",
5832#endif
5833#ifdef FEAT_BYTEOFF
5834 "byte_offset",
5835#endif
5836#ifdef FEAT_JOB_CHANNEL
5837 "channel",
5838#endif
5839#ifdef FEAT_CINDENT
5840 "cindent",
5841#endif
5842#ifdef FEAT_CLIENTSERVER
5843 "clientserver",
5844#endif
5845#ifdef FEAT_CLIPBOARD
5846 "clipboard",
5847#endif
5848#ifdef FEAT_CMDL_COMPL
5849 "cmdline_compl",
5850#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 "cmdline_hist",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852#ifdef FEAT_COMMENTS
5853 "comments",
5854#endif
5855#ifdef FEAT_CONCEAL
5856 "conceal",
5857#endif
5858#ifdef FEAT_CRYPT
5859 "cryptv",
5860 "crypt-blowfish",
5861 "crypt-blowfish2",
5862#endif
5863#ifdef FEAT_CSCOPE
5864 "cscope",
5865#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867#ifdef CURSOR_SHAPE
5868 "cursorshape",
5869#endif
5870#ifdef DEBUG
5871 "debug",
5872#endif
5873#ifdef FEAT_CON_DIALOG
5874 "dialog_con",
5875#endif
5876#ifdef FEAT_GUI_DIALOG
5877 "dialog_gui",
5878#endif
5879#ifdef FEAT_DIFF
5880 "diff",
5881#endif
5882#ifdef FEAT_DIGRAPHS
5883 "digraphs",
5884#endif
5885#ifdef FEAT_DIRECTX
5886 "directx",
5887#endif
5888#ifdef FEAT_DND
5889 "dnd",
5890#endif
5891#ifdef FEAT_EMACS_TAGS
5892 "emacs_tags",
5893#endif
5894 "eval", /* always present, of course! */
5895 "ex_extra", /* graduated feature */
5896#ifdef FEAT_SEARCH_EXTRA
5897 "extra_search",
5898#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005899#ifdef FEAT_SEARCHPATH
5900 "file_in_path",
5901#endif
5902#ifdef FEAT_FILTERPIPE
5903 "filterpipe",
5904#endif
5905#ifdef FEAT_FIND_ID
5906 "find_in_path",
5907#endif
5908#ifdef FEAT_FLOAT
5909 "float",
5910#endif
5911#ifdef FEAT_FOLDING
5912 "folding",
5913#endif
5914#ifdef FEAT_FOOTER
5915 "footer",
5916#endif
5917#if !defined(USE_SYSTEM) && defined(UNIX)
5918 "fork",
5919#endif
5920#ifdef FEAT_GETTEXT
5921 "gettext",
5922#endif
5923#ifdef FEAT_GUI
5924 "gui",
5925#endif
5926#ifdef FEAT_GUI_ATHENA
5927# ifdef FEAT_GUI_NEXTAW
5928 "gui_neXtaw",
5929# else
5930 "gui_athena",
5931# endif
5932#endif
5933#ifdef FEAT_GUI_GTK
5934 "gui_gtk",
5935# ifdef USE_GTK3
5936 "gui_gtk3",
5937# else
5938 "gui_gtk2",
5939# endif
5940#endif
5941#ifdef FEAT_GUI_GNOME
5942 "gui_gnome",
5943#endif
5944#ifdef FEAT_GUI_MAC
5945 "gui_mac",
5946#endif
5947#ifdef FEAT_GUI_MOTIF
5948 "gui_motif",
5949#endif
5950#ifdef FEAT_GUI_PHOTON
5951 "gui_photon",
5952#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005953#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005954 "gui_win32",
5955#endif
5956#ifdef FEAT_HANGULIN
5957 "hangul_input",
5958#endif
5959#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5960 "iconv",
5961#endif
5962#ifdef FEAT_INS_EXPAND
5963 "insert_expand",
5964#endif
5965#ifdef FEAT_JOB_CHANNEL
5966 "job",
5967#endif
5968#ifdef FEAT_JUMPLIST
5969 "jumplist",
5970#endif
5971#ifdef FEAT_KEYMAP
5972 "keymap",
5973#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005974 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005975#ifdef FEAT_LANGMAP
5976 "langmap",
5977#endif
5978#ifdef FEAT_LIBCALL
5979 "libcall",
5980#endif
5981#ifdef FEAT_LINEBREAK
5982 "linebreak",
5983#endif
5984#ifdef FEAT_LISP
5985 "lispindent",
5986#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005987 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988 "localmap",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989#ifdef FEAT_LUA
5990# ifndef DYNAMIC_LUA
5991 "lua",
5992# endif
5993#endif
5994#ifdef FEAT_MENU
5995 "menu",
5996#endif
5997#ifdef FEAT_SESSION
5998 "mksession",
5999#endif
6000#ifdef FEAT_MODIFY_FNAME
6001 "modify_fname",
6002#endif
6003#ifdef FEAT_MOUSE
6004 "mouse",
6005#endif
6006#ifdef FEAT_MOUSESHAPE
6007 "mouseshape",
6008#endif
6009#if defined(UNIX) || defined(VMS)
6010# ifdef FEAT_MOUSE_DEC
6011 "mouse_dec",
6012# endif
6013# ifdef FEAT_MOUSE_GPM
6014 "mouse_gpm",
6015# endif
6016# ifdef FEAT_MOUSE_JSB
6017 "mouse_jsbterm",
6018# endif
6019# ifdef FEAT_MOUSE_NET
6020 "mouse_netterm",
6021# endif
6022# ifdef FEAT_MOUSE_PTERM
6023 "mouse_pterm",
6024# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006025# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 "mouse_sgr",
6027# endif
6028# ifdef FEAT_SYSMOUSE
6029 "mouse_sysmouse",
6030# endif
6031# ifdef FEAT_MOUSE_URXVT
6032 "mouse_urxvt",
6033# endif
6034# ifdef FEAT_MOUSE_XTERM
6035 "mouse_xterm",
6036# endif
6037#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006038 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006039#ifdef FEAT_MBYTE_IME
6040 "multi_byte_ime",
6041#endif
6042#ifdef FEAT_MULTI_LANG
6043 "multi_lang",
6044#endif
6045#ifdef FEAT_MZSCHEME
6046#ifndef DYNAMIC_MZSCHEME
6047 "mzscheme",
6048#endif
6049#endif
6050#ifdef FEAT_NUM64
6051 "num64",
6052#endif
6053#ifdef FEAT_OLE
6054 "ole",
6055#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006056#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006058#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006059#ifdef FEAT_PATH_EXTRA
6060 "path_extra",
6061#endif
6062#ifdef FEAT_PERL
6063#ifndef DYNAMIC_PERL
6064 "perl",
6065#endif
6066#endif
6067#ifdef FEAT_PERSISTENT_UNDO
6068 "persistent_undo",
6069#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006070#if defined(FEAT_PYTHON)
6071 "python_compiled",
6072# if defined(DYNAMIC_PYTHON)
6073 "python_dynamic",
6074# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006076 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006077# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006079#if defined(FEAT_PYTHON3)
6080 "python3_compiled",
6081# if defined(DYNAMIC_PYTHON3)
6082 "python3_dynamic",
6083# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006085 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006086# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087#endif
6088#ifdef FEAT_POSTSCRIPT
6089 "postscript",
6090#endif
6091#ifdef FEAT_PRINTER
6092 "printer",
6093#endif
6094#ifdef FEAT_PROFILE
6095 "profile",
6096#endif
6097#ifdef FEAT_RELTIME
6098 "reltime",
6099#endif
6100#ifdef FEAT_QUICKFIX
6101 "quickfix",
6102#endif
6103#ifdef FEAT_RIGHTLEFT
6104 "rightleft",
6105#endif
6106#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6107 "ruby",
6108#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006109 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110#ifdef FEAT_CMDL_INFO
6111 "showcmd",
6112 "cmdline_info",
6113#endif
6114#ifdef FEAT_SIGNS
6115 "signs",
6116#endif
6117#ifdef FEAT_SMARTINDENT
6118 "smartindent",
6119#endif
6120#ifdef STARTUPTIME
6121 "startuptime",
6122#endif
6123#ifdef FEAT_STL_OPT
6124 "statusline",
6125#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006126#ifdef FEAT_NETBEANS_INTG
6127 "netbeans_intg",
6128#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006129#ifdef FEAT_SOUND
6130 "sound",
6131#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132#ifdef FEAT_SPELL
6133 "spell",
6134#endif
6135#ifdef FEAT_SYN_HL
6136 "syntax",
6137#endif
6138#if defined(USE_SYSTEM) || !defined(UNIX)
6139 "system",
6140#endif
6141#ifdef FEAT_TAG_BINS
6142 "tag_binary",
6143#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006144#ifdef FEAT_TCL
6145# ifndef DYNAMIC_TCL
6146 "tcl",
6147# endif
6148#endif
6149#ifdef FEAT_TERMGUICOLORS
6150 "termguicolors",
6151#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006152#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006153 "terminal",
6154#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155#ifdef TERMINFO
6156 "terminfo",
6157#endif
6158#ifdef FEAT_TERMRESPONSE
6159 "termresponse",
6160#endif
6161#ifdef FEAT_TEXTOBJ
6162 "textobjects",
6163#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006164#ifdef FEAT_TEXT_PROP
6165 "textprop",
6166#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006167#ifdef HAVE_TGETENT
6168 "tgetent",
6169#endif
6170#ifdef FEAT_TIMERS
6171 "timers",
6172#endif
6173#ifdef FEAT_TITLE
6174 "title",
6175#endif
6176#ifdef FEAT_TOOLBAR
6177 "toolbar",
6178#endif
6179#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6180 "unnamedplus",
6181#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182 "user-commands", /* was accidentally included in 5.4 */
6183 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006184#ifdef FEAT_VARTABS
6185 "vartabs",
6186#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006187 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188#ifdef FEAT_VIMINFO
6189 "viminfo",
6190#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006191 "vimscript-1",
6192 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006193 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006194 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006198#ifdef FEAT_VTP
6199 "vtp",
6200#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201#ifdef FEAT_WILDIGN
6202 "wildignore",
6203#endif
6204#ifdef FEAT_WILDMENU
6205 "wildmenu",
6206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208#ifdef FEAT_WAK
6209 "winaltkeys",
6210#endif
6211#ifdef FEAT_WRITEBACKUP
6212 "writebackup",
6213#endif
6214#ifdef FEAT_XIM
6215 "xim",
6216#endif
6217#ifdef FEAT_XFONTSET
6218 "xfontset",
6219#endif
6220#ifdef FEAT_XPM_W32
6221 "xpm",
6222 "xpm_w32", /* for backward compatibility */
6223#else
6224# if defined(HAVE_XPM)
6225 "xpm",
6226# endif
6227#endif
6228#ifdef USE_XSMP
6229 "xsmp",
6230#endif
6231#ifdef USE_XSMP_INTERACT
6232 "xsmp_interact",
6233#endif
6234#ifdef FEAT_XCLIPBOARD
6235 "xterm_clipboard",
6236#endif
6237#ifdef FEAT_XTERM_SAVE
6238 "xterm_save",
6239#endif
6240#if defined(UNIX) && defined(FEAT_X11)
6241 "X11",
6242#endif
6243 NULL
6244 };
6245
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006246 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 for (i = 0; has_list[i] != NULL; ++i)
6248 if (STRICMP(name, has_list[i]) == 0)
6249 {
6250 n = TRUE;
6251 break;
6252 }
6253
6254 if (n == FALSE)
6255 {
6256 if (STRNICMP(name, "patch", 5) == 0)
6257 {
6258 if (name[5] == '-'
6259 && STRLEN(name) >= 11
6260 && vim_isdigit(name[6])
6261 && vim_isdigit(name[8])
6262 && vim_isdigit(name[10]))
6263 {
6264 int major = atoi((char *)name + 6);
6265 int minor = atoi((char *)name + 8);
6266
6267 /* Expect "patch-9.9.01234". */
6268 n = (major < VIM_VERSION_MAJOR
6269 || (major == VIM_VERSION_MAJOR
6270 && (minor < VIM_VERSION_MINOR
6271 || (minor == VIM_VERSION_MINOR
6272 && has_patch(atoi((char *)name + 10))))));
6273 }
6274 else
6275 n = has_patch(atoi((char *)name + 5));
6276 }
6277 else if (STRICMP(name, "vim_starting") == 0)
6278 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006279 else if (STRICMP(name, "ttyin") == 0)
6280 n = mch_input_isatty();
6281 else if (STRICMP(name, "ttyout") == 0)
6282 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283 else if (STRICMP(name, "multi_byte_encoding") == 0)
6284 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006285#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286 else if (STRICMP(name, "balloon_multiline") == 0)
6287 n = multiline_balloon_available();
6288#endif
6289#ifdef DYNAMIC_TCL
6290 else if (STRICMP(name, "tcl") == 0)
6291 n = tcl_enabled(FALSE);
6292#endif
6293#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6294 else if (STRICMP(name, "iconv") == 0)
6295 n = iconv_enabled(FALSE);
6296#endif
6297#ifdef DYNAMIC_LUA
6298 else if (STRICMP(name, "lua") == 0)
6299 n = lua_enabled(FALSE);
6300#endif
6301#ifdef DYNAMIC_MZSCHEME
6302 else if (STRICMP(name, "mzscheme") == 0)
6303 n = mzscheme_enabled(FALSE);
6304#endif
6305#ifdef DYNAMIC_RUBY
6306 else if (STRICMP(name, "ruby") == 0)
6307 n = ruby_enabled(FALSE);
6308#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#ifdef DYNAMIC_PYTHON
6310 else if (STRICMP(name, "python") == 0)
6311 n = python_enabled(FALSE);
6312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313#ifdef DYNAMIC_PYTHON3
6314 else if (STRICMP(name, "python3") == 0)
6315 n = python3_enabled(FALSE);
6316#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006317#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6318 else if (STRICMP(name, "pythonx") == 0)
6319 {
6320# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6321 if (p_pyx == 0)
6322 n = python3_enabled(FALSE) || python_enabled(FALSE);
6323 else if (p_pyx == 3)
6324 n = python3_enabled(FALSE);
6325 else if (p_pyx == 2)
6326 n = python_enabled(FALSE);
6327# elif defined(DYNAMIC_PYTHON)
6328 n = python_enabled(FALSE);
6329# elif defined(DYNAMIC_PYTHON3)
6330 n = python3_enabled(FALSE);
6331# endif
6332 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333#endif
6334#ifdef DYNAMIC_PERL
6335 else if (STRICMP(name, "perl") == 0)
6336 n = perl_enabled(FALSE);
6337#endif
6338#ifdef FEAT_GUI
6339 else if (STRICMP(name, "gui_running") == 0)
6340 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341# ifdef FEAT_BROWSE
6342 else if (STRICMP(name, "browse") == 0)
6343 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6344# endif
6345#endif
6346#ifdef FEAT_SYN_HL
6347 else if (STRICMP(name, "syntax_items") == 0)
6348 n = syntax_present(curwin);
6349#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006350#ifdef FEAT_VTP
6351 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006352 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006353#endif
6354#ifdef FEAT_NETBEANS_INTG
6355 else if (STRICMP(name, "netbeans_enabled") == 0)
6356 n = netbeans_active();
6357#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006358#ifdef FEAT_MOUSE_GPM
6359 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6360 n = gpm_enabled();
6361#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006362#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006363 else if (STRICMP(name, "terminal") == 0)
6364 n = terminal_enabled();
6365#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006366#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006367 else if (STRICMP(name, "conpty") == 0)
6368 n = use_conpty();
6369#endif
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02006370#ifdef FEAT_CLIPBOARD
6371 else if (STRICMP(name, "clipboard_working") == 0)
6372 n = clip_star.available;
6373#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374 }
6375
6376 rettv->vval.v_number = n;
6377}
6378
6379/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006380 * "haslocaldir()" function
6381 */
6382 static void
6383f_haslocaldir(typval_T *argvars, typval_T *rettv)
6384{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006385 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 win_T *wp = NULL;
6387
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006388 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
6389
6390 // Check for window-local and tab-local directories
6391 if (wp != NULL && wp->w_localdir != NULL)
6392 rettv->vval.v_number = 1;
6393 else if (tp != NULL && tp->tp_localdir != NULL)
6394 rettv->vval.v_number = 2;
6395 else
6396 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397}
6398
6399/*
6400 * "hasmapto()" function
6401 */
6402 static void
6403f_hasmapto(typval_T *argvars, typval_T *rettv)
6404{
6405 char_u *name;
6406 char_u *mode;
6407 char_u buf[NUMBUFLEN];
6408 int abbr = FALSE;
6409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006410 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006411 if (argvars[1].v_type == VAR_UNKNOWN)
6412 mode = (char_u *)"nvo";
6413 else
6414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006415 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006416 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006417 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418 }
6419
6420 if (map_to_exists(name, mode, abbr))
6421 rettv->vval.v_number = TRUE;
6422 else
6423 rettv->vval.v_number = FALSE;
6424}
6425
6426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427 * "highlightID(name)" function
6428 */
6429 static void
6430f_hlID(typval_T *argvars, typval_T *rettv)
6431{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006432 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433}
6434
6435/*
6436 * "highlight_exists()" function
6437 */
6438 static void
6439f_hlexists(typval_T *argvars, typval_T *rettv)
6440{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006441 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442}
6443
6444/*
6445 * "hostname()" function
6446 */
6447 static void
6448f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6449{
6450 char_u hostname[256];
6451
6452 mch_get_host_name(hostname, 256);
6453 rettv->v_type = VAR_STRING;
6454 rettv->vval.v_string = vim_strsave(hostname);
6455}
6456
6457/*
6458 * iconv() function
6459 */
6460 static void
6461f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6462{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006463 char_u buf1[NUMBUFLEN];
6464 char_u buf2[NUMBUFLEN];
6465 char_u *from, *to, *str;
6466 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467
6468 rettv->v_type = VAR_STRING;
6469 rettv->vval.v_string = NULL;
6470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006471 str = tv_get_string(&argvars[0]);
6472 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6473 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474 vimconv.vc_type = CONV_NONE;
6475 convert_setup(&vimconv, from, to);
6476
6477 /* If the encodings are equal, no conversion needed. */
6478 if (vimconv.vc_type == CONV_NONE)
6479 rettv->vval.v_string = vim_strsave(str);
6480 else
6481 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6482
6483 convert_setup(&vimconv, NULL, NULL);
6484 vim_free(from);
6485 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486}
6487
6488/*
6489 * "indent()" function
6490 */
6491 static void
6492f_indent(typval_T *argvars, typval_T *rettv)
6493{
6494 linenr_T lnum;
6495
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006496 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6498 rettv->vval.v_number = get_indent_lnum(lnum);
6499 else
6500 rettv->vval.v_number = -1;
6501}
6502
6503/*
6504 * "index()" function
6505 */
6506 static void
6507f_index(typval_T *argvars, typval_T *rettv)
6508{
6509 list_T *l;
6510 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006511 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 long idx = 0;
6513 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006514 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006515
6516 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006517 if (argvars[0].v_type == VAR_BLOB)
6518 {
6519 typval_T tv;
6520 int start = 0;
6521
6522 if (argvars[2].v_type != VAR_UNKNOWN)
6523 {
6524 start = tv_get_number_chk(&argvars[2], &error);
6525 if (error)
6526 return;
6527 }
6528 b = argvars[0].vval.v_blob;
6529 if (b == NULL)
6530 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01006531 if (start < 0)
6532 {
6533 start = blob_len(b) + start;
6534 if (start < 0)
6535 start = 0;
6536 }
6537
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006538 for (idx = start; idx < blob_len(b); ++idx)
6539 {
6540 tv.v_type = VAR_NUMBER;
6541 tv.vval.v_number = blob_get(b, idx);
6542 if (tv_equal(&tv, &argvars[1], ic, FALSE))
6543 {
6544 rettv->vval.v_number = idx;
6545 return;
6546 }
6547 }
6548 return;
6549 }
6550 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006552 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553 return;
6554 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006556 l = argvars[0].vval.v_list;
6557 if (l != NULL)
6558 {
6559 item = l->lv_first;
6560 if (argvars[2].v_type != VAR_UNKNOWN)
6561 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562 /* Start at specified item. Use the cached index that list_find()
6563 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006564 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 idx = l->lv_idx;
6566 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006567 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568 if (error)
6569 item = NULL;
6570 }
6571
6572 for ( ; item != NULL; item = item->li_next, ++idx)
6573 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6574 {
6575 rettv->vval.v_number = idx;
6576 break;
6577 }
6578 }
6579}
6580
6581static int inputsecret_flag = 0;
6582
6583/*
6584 * "input()" function
6585 * Also handles inputsecret() when inputsecret is set.
6586 */
6587 static void
6588f_input(typval_T *argvars, typval_T *rettv)
6589{
6590 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6591}
6592
6593/*
6594 * "inputdialog()" function
6595 */
6596 static void
6597f_inputdialog(typval_T *argvars, typval_T *rettv)
6598{
6599#if defined(FEAT_GUI_TEXTDIALOG)
6600 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6601 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6602 {
6603 char_u *message;
6604 char_u buf[NUMBUFLEN];
6605 char_u *defstr = (char_u *)"";
6606
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006607 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006609 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006610 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6611 else
6612 IObuff[0] = NUL;
6613 if (message != NULL && defstr != NULL
6614 && do_dialog(VIM_QUESTION, NULL, message,
6615 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6616 rettv->vval.v_string = vim_strsave(IObuff);
6617 else
6618 {
6619 if (message != NULL && defstr != NULL
6620 && argvars[1].v_type != VAR_UNKNOWN
6621 && argvars[2].v_type != VAR_UNKNOWN)
6622 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006623 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006624 else
6625 rettv->vval.v_string = NULL;
6626 }
6627 rettv->v_type = VAR_STRING;
6628 }
6629 else
6630#endif
6631 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6632}
6633
6634/*
6635 * "inputlist()" function
6636 */
6637 static void
6638f_inputlist(typval_T *argvars, typval_T *rettv)
6639{
6640 listitem_T *li;
6641 int selected;
6642 int mouse_used;
6643
6644#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006645 /* While starting up, there is no place to enter text. When running tests
6646 * with --not-a-term we assume feedkeys() will be used. */
6647 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006648 return;
6649#endif
6650 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006652 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 return;
6654 }
6655
6656 msg_start();
6657 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6658 lines_left = Rows; /* avoid more prompt */
6659 msg_scroll = TRUE;
6660 msg_clr_eos();
6661
6662 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6663 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006664 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006665 msg_putchar('\n');
6666 }
6667
6668 /* Ask for choice. */
6669 selected = prompt_for_number(&mouse_used);
6670 if (mouse_used)
6671 selected -= lines_left;
6672
6673 rettv->vval.v_number = selected;
6674}
6675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6677
6678/*
6679 * "inputrestore()" function
6680 */
6681 static void
6682f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6683{
6684 if (ga_userinput.ga_len > 0)
6685 {
6686 --ga_userinput.ga_len;
6687 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6688 + ga_userinput.ga_len);
6689 /* default return is zero == OK */
6690 }
6691 else if (p_verbose > 1)
6692 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006693 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 rettv->vval.v_number = 1; /* Failed */
6695 }
6696}
6697
6698/*
6699 * "inputsave()" function
6700 */
6701 static void
6702f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6703{
6704 /* Add an entry to the stack of typeahead storage. */
6705 if (ga_grow(&ga_userinput, 1) == OK)
6706 {
6707 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6708 + ga_userinput.ga_len);
6709 ++ga_userinput.ga_len;
6710 /* default return is zero == OK */
6711 }
6712 else
6713 rettv->vval.v_number = 1; /* Failed */
6714}
6715
6716/*
6717 * "inputsecret()" function
6718 */
6719 static void
6720f_inputsecret(typval_T *argvars, typval_T *rettv)
6721{
6722 ++cmdline_star;
6723 ++inputsecret_flag;
6724 f_input(argvars, rettv);
6725 --cmdline_star;
6726 --inputsecret_flag;
6727}
6728
6729/*
6730 * "insert()" function
6731 */
6732 static void
6733f_insert(typval_T *argvars, typval_T *rettv)
6734{
6735 long before = 0;
6736 listitem_T *item;
6737 list_T *l;
6738 int error = FALSE;
6739
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006740 if (argvars[0].v_type == VAR_BLOB)
6741 {
6742 int val, len;
6743 char_u *p;
6744
6745 len = blob_len(argvars[0].vval.v_blob);
6746 if (argvars[2].v_type != VAR_UNKNOWN)
6747 {
6748 before = (long)tv_get_number_chk(&argvars[2], &error);
6749 if (error)
6750 return; // type error; errmsg already given
6751 if (before < 0 || before > len)
6752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006753 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006754 return;
6755 }
6756 }
6757 val = tv_get_number_chk(&argvars[1], &error);
6758 if (error)
6759 return;
6760 if (val < 0 || val > 255)
6761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006762 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006763 return;
6764 }
6765
6766 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
6767 return;
6768 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
6769 mch_memmove(p + before + 1, p + before, (size_t)len - before);
6770 *(p + before) = val;
6771 ++argvars[0].vval.v_blob->bv_ga.ga_len;
6772
6773 copy_tv(&argvars[0], rettv);
6774 }
6775 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01006776 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01006777 else if ((l = argvars[0].vval.v_list) != NULL
6778 && !var_check_lock(l->lv_lock,
6779 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780 {
6781 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006782 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783 if (error)
6784 return; /* type error; errmsg already given */
6785
6786 if (before == l->lv_len)
6787 item = NULL;
6788 else
6789 {
6790 item = list_find(l, before);
6791 if (item == NULL)
6792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006793 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006794 l = NULL;
6795 }
6796 }
6797 if (l != NULL)
6798 {
6799 list_insert_tv(l, &argvars[1], item);
6800 copy_tv(&argvars[0], rettv);
6801 }
6802 }
6803}
6804
6805/*
6806 * "invert(expr)" function
6807 */
6808 static void
6809f_invert(typval_T *argvars, typval_T *rettv)
6810{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006811 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812}
6813
6814/*
6815 * "isdirectory()" function
6816 */
6817 static void
6818f_isdirectory(typval_T *argvars, typval_T *rettv)
6819{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006820 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821}
6822
6823/*
6824 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6825 * or it refers to a List or Dictionary that is locked.
6826 */
6827 static int
6828tv_islocked(typval_T *tv)
6829{
6830 return (tv->v_lock & VAR_LOCKED)
6831 || (tv->v_type == VAR_LIST
6832 && tv->vval.v_list != NULL
6833 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6834 || (tv->v_type == VAR_DICT
6835 && tv->vval.v_dict != NULL
6836 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6837}
6838
6839/*
6840 * "islocked()" function
6841 */
6842 static void
6843f_islocked(typval_T *argvars, typval_T *rettv)
6844{
6845 lval_T lv;
6846 char_u *end;
6847 dictitem_T *di;
6848
6849 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006850 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006851 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006852 if (end != NULL && lv.ll_name != NULL)
6853 {
6854 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006855 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006856 else
6857 {
6858 if (lv.ll_tv == NULL)
6859 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006860 di = find_var(lv.ll_name, NULL, TRUE);
6861 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006862 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006863 /* Consider a variable locked when:
6864 * 1. the variable itself is locked
6865 * 2. the value of the variable is locked.
6866 * 3. the List or Dict value is locked.
6867 */
6868 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6869 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006870 }
6871 }
6872 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006873 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006875 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 else if (lv.ll_list != NULL)
6877 /* List item. */
6878 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6879 else
6880 /* Dictionary item. */
6881 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6882 }
6883 }
6884
6885 clear_lval(&lv);
6886}
6887
6888#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6889/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02006890 * "isinf()" function
6891 */
6892 static void
6893f_isinf(typval_T *argvars, typval_T *rettv)
6894{
6895 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
6896 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
6897}
6898
6899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 * "isnan()" function
6901 */
6902 static void
6903f_isnan(typval_T *argvars, typval_T *rettv)
6904{
6905 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6906 && isnan(argvars[0].vval.v_float);
6907}
6908#endif
6909
6910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 * "last_buffer_nr()" function.
6912 */
6913 static void
6914f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6915{
6916 int n = 0;
6917 buf_T *buf;
6918
Bram Moolenaar29323592016-07-24 22:04:11 +02006919 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006920 if (n < buf->b_fnum)
6921 n = buf->b_fnum;
6922
6923 rettv->vval.v_number = n;
6924}
6925
6926/*
6927 * "len()" function
6928 */
6929 static void
6930f_len(typval_T *argvars, typval_T *rettv)
6931{
6932 switch (argvars[0].v_type)
6933 {
6934 case VAR_STRING:
6935 case VAR_NUMBER:
6936 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006937 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006939 case VAR_BLOB:
6940 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
6941 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 case VAR_LIST:
6943 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6944 break;
6945 case VAR_DICT:
6946 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6947 break;
6948 case VAR_UNKNOWN:
6949 case VAR_SPECIAL:
6950 case VAR_FLOAT:
6951 case VAR_FUNC:
6952 case VAR_PARTIAL:
6953 case VAR_JOB:
6954 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006956 break;
6957 }
6958}
6959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006960 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006961libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962{
6963#ifdef FEAT_LIBCALL
6964 char_u *string_in;
6965 char_u **string_result;
6966 int nr_result;
6967#endif
6968
6969 rettv->v_type = type;
6970 if (type != VAR_NUMBER)
6971 rettv->vval.v_string = NULL;
6972
6973 if (check_restricted() || check_secure())
6974 return;
6975
6976#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006977 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006978 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6979 {
6980 string_in = NULL;
6981 if (argvars[2].v_type == VAR_STRING)
6982 string_in = argvars[2].vval.v_string;
6983 if (type == VAR_NUMBER)
6984 string_result = NULL;
6985 else
6986 string_result = &rettv->vval.v_string;
6987 if (mch_libcall(argvars[0].vval.v_string,
6988 argvars[1].vval.v_string,
6989 string_in,
6990 argvars[2].vval.v_number,
6991 string_result,
6992 &nr_result) == OK
6993 && type == VAR_NUMBER)
6994 rettv->vval.v_number = nr_result;
6995 }
6996#endif
6997}
6998
6999/*
7000 * "libcall()" function
7001 */
7002 static void
7003f_libcall(typval_T *argvars, typval_T *rettv)
7004{
7005 libcall_common(argvars, rettv, VAR_STRING);
7006}
7007
7008/*
7009 * "libcallnr()" function
7010 */
7011 static void
7012f_libcallnr(typval_T *argvars, typval_T *rettv)
7013{
7014 libcall_common(argvars, rettv, VAR_NUMBER);
7015}
7016
7017/*
7018 * "line(string)" function
7019 */
7020 static void
7021f_line(typval_T *argvars, typval_T *rettv)
7022{
7023 linenr_T lnum = 0;
7024 pos_T *fp;
7025 int fnum;
7026
7027 fp = var2fpos(&argvars[0], TRUE, &fnum);
7028 if (fp != NULL)
7029 lnum = fp->lnum;
7030 rettv->vval.v_number = lnum;
7031}
7032
7033/*
7034 * "line2byte(lnum)" function
7035 */
7036 static void
7037f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7038{
7039#ifndef FEAT_BYTEOFF
7040 rettv->vval.v_number = -1;
7041#else
7042 linenr_T lnum;
7043
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007044 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7046 rettv->vval.v_number = -1;
7047 else
7048 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7049 if (rettv->vval.v_number >= 0)
7050 ++rettv->vval.v_number;
7051#endif
7052}
7053
7054/*
7055 * "lispindent(lnum)" function
7056 */
7057 static void
7058f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7059{
7060#ifdef FEAT_LISP
7061 pos_T pos;
7062 linenr_T lnum;
7063
7064 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007065 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007066 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7067 {
7068 curwin->w_cursor.lnum = lnum;
7069 rettv->vval.v_number = get_lisp_indent();
7070 curwin->w_cursor = pos;
7071 }
7072 else
7073#endif
7074 rettv->vval.v_number = -1;
7075}
7076
7077/*
7078 * "localtime()" function
7079 */
7080 static void
7081f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7082{
7083 rettv->vval.v_number = (varnumber_T)time(NULL);
7084}
7085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007086#ifdef FEAT_FLOAT
7087/*
7088 * "log()" function
7089 */
7090 static void
7091f_log(typval_T *argvars, typval_T *rettv)
7092{
7093 float_T f = 0.0;
7094
7095 rettv->v_type = VAR_FLOAT;
7096 if (get_float_arg(argvars, &f) == OK)
7097 rettv->vval.v_float = log(f);
7098 else
7099 rettv->vval.v_float = 0.0;
7100}
7101
7102/*
7103 * "log10()" function
7104 */
7105 static void
7106f_log10(typval_T *argvars, typval_T *rettv)
7107{
7108 float_T f = 0.0;
7109
7110 rettv->v_type = VAR_FLOAT;
7111 if (get_float_arg(argvars, &f) == OK)
7112 rettv->vval.v_float = log10(f);
7113 else
7114 rettv->vval.v_float = 0.0;
7115}
7116#endif
7117
7118#ifdef FEAT_LUA
7119/*
7120 * "luaeval()" function
7121 */
7122 static void
7123f_luaeval(typval_T *argvars, typval_T *rettv)
7124{
7125 char_u *str;
7126 char_u buf[NUMBUFLEN];
7127
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007128 if (check_restricted() || check_secure())
7129 return;
7130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 do_luaeval(str, argvars + 1, rettv);
7133}
7134#endif
7135
7136/*
7137 * "map()" function
7138 */
7139 static void
7140f_map(typval_T *argvars, typval_T *rettv)
7141{
7142 filter_map(argvars, rettv, TRUE);
7143}
7144
7145/*
7146 * "maparg()" function
7147 */
7148 static void
7149f_maparg(typval_T *argvars, typval_T *rettv)
7150{
7151 get_maparg(argvars, rettv, TRUE);
7152}
7153
7154/*
7155 * "mapcheck()" function
7156 */
7157 static void
7158f_mapcheck(typval_T *argvars, typval_T *rettv)
7159{
7160 get_maparg(argvars, rettv, FALSE);
7161}
7162
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007163typedef enum
7164{
7165 MATCH_END, /* matchend() */
7166 MATCH_MATCH, /* match() */
7167 MATCH_STR, /* matchstr() */
7168 MATCH_LIST, /* matchlist() */
7169 MATCH_POS /* matchstrpos() */
7170} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007171
7172 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007173find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174{
7175 char_u *str = NULL;
7176 long len = 0;
7177 char_u *expr = NULL;
7178 char_u *pat;
7179 regmatch_T regmatch;
7180 char_u patbuf[NUMBUFLEN];
7181 char_u strbuf[NUMBUFLEN];
7182 char_u *save_cpo;
7183 long start = 0;
7184 long nth = 1;
7185 colnr_T startcol = 0;
7186 int match = 0;
7187 list_T *l = NULL;
7188 listitem_T *li = NULL;
7189 long idx = 0;
7190 char_u *tofree = NULL;
7191
7192 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7193 save_cpo = p_cpo;
7194 p_cpo = (char_u *)"";
7195
7196 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007197 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007199 /* type MATCH_LIST: return empty list when there are no matches.
7200 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 if (rettv_list_alloc(rettv) == FAIL)
7202 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007203 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 && (list_append_string(rettv->vval.v_list,
7205 (char_u *)"", 0) == FAIL
7206 || list_append_number(rettv->vval.v_list,
7207 (varnumber_T)-1) == FAIL
7208 || list_append_number(rettv->vval.v_list,
7209 (varnumber_T)-1) == FAIL
7210 || list_append_number(rettv->vval.v_list,
7211 (varnumber_T)-1) == FAIL))
7212 {
7213 list_free(rettv->vval.v_list);
7214 rettv->vval.v_list = NULL;
7215 goto theend;
7216 }
7217 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007218 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219 {
7220 rettv->v_type = VAR_STRING;
7221 rettv->vval.v_string = NULL;
7222 }
7223
7224 if (argvars[0].v_type == VAR_LIST)
7225 {
7226 if ((l = argvars[0].vval.v_list) == NULL)
7227 goto theend;
7228 li = l->lv_first;
7229 }
7230 else
7231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007232 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007233 len = (long)STRLEN(str);
7234 }
7235
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007236 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237 if (pat == NULL)
7238 goto theend;
7239
7240 if (argvars[2].v_type != VAR_UNKNOWN)
7241 {
7242 int error = FALSE;
7243
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245 if (error)
7246 goto theend;
7247 if (l != NULL)
7248 {
7249 li = list_find(l, start);
7250 if (li == NULL)
7251 goto theend;
7252 idx = l->lv_idx; /* use the cached index */
7253 }
7254 else
7255 {
7256 if (start < 0)
7257 start = 0;
7258 if (start > len)
7259 goto theend;
7260 /* When "count" argument is there ignore matches before "start",
7261 * otherwise skip part of the string. Differs when pattern is "^"
7262 * or "\<". */
7263 if (argvars[3].v_type != VAR_UNKNOWN)
7264 startcol = start;
7265 else
7266 {
7267 str += start;
7268 len -= start;
7269 }
7270 }
7271
7272 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007273 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 if (error)
7275 goto theend;
7276 }
7277
7278 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7279 if (regmatch.regprog != NULL)
7280 {
7281 regmatch.rm_ic = p_ic;
7282
7283 for (;;)
7284 {
7285 if (l != NULL)
7286 {
7287 if (li == NULL)
7288 {
7289 match = FALSE;
7290 break;
7291 }
7292 vim_free(tofree);
7293 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7294 if (str == NULL)
7295 break;
7296 }
7297
7298 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7299
7300 if (match && --nth <= 0)
7301 break;
7302 if (l == NULL && !match)
7303 break;
7304
7305 /* Advance to just after the match. */
7306 if (l != NULL)
7307 {
7308 li = li->li_next;
7309 ++idx;
7310 }
7311 else
7312 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007313 startcol = (colnr_T)(regmatch.startp[0]
7314 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 if (startcol > (colnr_T)len
7316 || str + startcol <= regmatch.startp[0])
7317 {
7318 match = FALSE;
7319 break;
7320 }
7321 }
7322 }
7323
7324 if (match)
7325 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007326 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327 {
7328 listitem_T *li1 = rettv->vval.v_list->lv_first;
7329 listitem_T *li2 = li1->li_next;
7330 listitem_T *li3 = li2->li_next;
7331 listitem_T *li4 = li3->li_next;
7332
7333 vim_free(li1->li_tv.vval.v_string);
7334 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7335 (int)(regmatch.endp[0] - regmatch.startp[0]));
7336 li3->li_tv.vval.v_number =
7337 (varnumber_T)(regmatch.startp[0] - expr);
7338 li4->li_tv.vval.v_number =
7339 (varnumber_T)(regmatch.endp[0] - expr);
7340 if (l != NULL)
7341 li2->li_tv.vval.v_number = (varnumber_T)idx;
7342 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007343 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 {
7345 int i;
7346
7347 /* return list with matched string and submatches */
7348 for (i = 0; i < NSUBEXP; ++i)
7349 {
7350 if (regmatch.endp[i] == NULL)
7351 {
7352 if (list_append_string(rettv->vval.v_list,
7353 (char_u *)"", 0) == FAIL)
7354 break;
7355 }
7356 else if (list_append_string(rettv->vval.v_list,
7357 regmatch.startp[i],
7358 (int)(regmatch.endp[i] - regmatch.startp[i]))
7359 == FAIL)
7360 break;
7361 }
7362 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007363 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 {
7365 /* return matched string */
7366 if (l != NULL)
7367 copy_tv(&li->li_tv, rettv);
7368 else
7369 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7370 (int)(regmatch.endp[0] - regmatch.startp[0]));
7371 }
7372 else if (l != NULL)
7373 rettv->vval.v_number = idx;
7374 else
7375 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007376 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 rettv->vval.v_number =
7378 (varnumber_T)(regmatch.startp[0] - str);
7379 else
7380 rettv->vval.v_number =
7381 (varnumber_T)(regmatch.endp[0] - str);
7382 rettv->vval.v_number += (varnumber_T)(str - expr);
7383 }
7384 }
7385 vim_regfree(regmatch.regprog);
7386 }
7387
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007388theend:
7389 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 /* matchstrpos() without a list: drop the second item. */
7391 listitem_remove(rettv->vval.v_list,
7392 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 vim_free(tofree);
7394 p_cpo = save_cpo;
7395}
7396
7397/*
7398 * "match()" function
7399 */
7400 static void
7401f_match(typval_T *argvars, typval_T *rettv)
7402{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007403 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404}
7405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406/*
7407 * "matchend()" function
7408 */
7409 static void
7410f_matchend(typval_T *argvars, typval_T *rettv)
7411{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007412 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007413}
7414
7415/*
7416 * "matchlist()" function
7417 */
7418 static void
7419f_matchlist(typval_T *argvars, typval_T *rettv)
7420{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007421 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007422}
7423
7424/*
7425 * "matchstr()" function
7426 */
7427 static void
7428f_matchstr(typval_T *argvars, typval_T *rettv)
7429{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007430 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431}
7432
7433/*
7434 * "matchstrpos()" function
7435 */
7436 static void
7437f_matchstrpos(typval_T *argvars, typval_T *rettv)
7438{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007439 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007440}
7441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 static void
7443max_min(typval_T *argvars, typval_T *rettv, int domax)
7444{
7445 varnumber_T n = 0;
7446 varnumber_T i;
7447 int error = FALSE;
7448
7449 if (argvars[0].v_type == VAR_LIST)
7450 {
7451 list_T *l;
7452 listitem_T *li;
7453
7454 l = argvars[0].vval.v_list;
7455 if (l != NULL)
7456 {
7457 li = l->lv_first;
7458 if (li != NULL)
7459 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007460 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 for (;;)
7462 {
7463 li = li->li_next;
7464 if (li == NULL)
7465 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007466 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 if (domax ? i > n : i < n)
7468 n = i;
7469 }
7470 }
7471 }
7472 }
7473 else if (argvars[0].v_type == VAR_DICT)
7474 {
7475 dict_T *d;
7476 int first = TRUE;
7477 hashitem_T *hi;
7478 int todo;
7479
7480 d = argvars[0].vval.v_dict;
7481 if (d != NULL)
7482 {
7483 todo = (int)d->dv_hashtab.ht_used;
7484 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7485 {
7486 if (!HASHITEM_EMPTY(hi))
7487 {
7488 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007489 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490 if (first)
7491 {
7492 n = i;
7493 first = FALSE;
7494 }
7495 else if (domax ? i > n : i < n)
7496 n = i;
7497 }
7498 }
7499 }
7500 }
7501 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007502 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503 rettv->vval.v_number = error ? 0 : n;
7504}
7505
7506/*
7507 * "max()" function
7508 */
7509 static void
7510f_max(typval_T *argvars, typval_T *rettv)
7511{
7512 max_min(argvars, rettv, TRUE);
7513}
7514
7515/*
7516 * "min()" function
7517 */
7518 static void
7519f_min(typval_T *argvars, typval_T *rettv)
7520{
7521 max_min(argvars, rettv, FALSE);
7522}
7523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524/*
7525 * Create the directory in which "dir" is located, and higher levels when
7526 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007527 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007528 */
7529 static int
7530mkdir_recurse(char_u *dir, int prot)
7531{
7532 char_u *p;
7533 char_u *updir;
7534 int r = FAIL;
7535
7536 /* Get end of directory name in "dir".
7537 * We're done when it's "/" or "c:/". */
7538 p = gettail_sep(dir);
7539 if (p <= get_past_head(dir))
7540 return OK;
7541
7542 /* If the directory exists we're done. Otherwise: create it.*/
7543 updir = vim_strnsave(dir, (int)(p - dir));
7544 if (updir == NULL)
7545 return FAIL;
7546 if (mch_isdir(updir))
7547 r = OK;
7548 else if (mkdir_recurse(updir, prot) == OK)
7549 r = vim_mkdir_emsg(updir, prot);
7550 vim_free(updir);
7551 return r;
7552}
7553
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554/*
7555 * "mkdir()" function
7556 */
7557 static void
7558f_mkdir(typval_T *argvars, typval_T *rettv)
7559{
7560 char_u *dir;
7561 char_u buf[NUMBUFLEN];
7562 int prot = 0755;
7563
7564 rettv->vval.v_number = FAIL;
7565 if (check_restricted() || check_secure())
7566 return;
7567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007568 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007569 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007570 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007572 if (*gettail(dir) == NUL)
7573 /* remove trailing slashes */
7574 *gettail_sep(dir) = NUL;
7575
7576 if (argvars[1].v_type != VAR_UNKNOWN)
7577 {
7578 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007580 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007581 if (prot == -1)
7582 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007584 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007585 {
7586 if (mch_isdir(dir))
7587 {
7588 /* With the "p" flag it's OK if the dir already exists. */
7589 rettv->vval.v_number = OK;
7590 return;
7591 }
7592 mkdir_recurse(dir, prot);
7593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02007595 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007596}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597
7598/*
7599 * "mode()" function
7600 */
7601 static void
7602f_mode(typval_T *argvars, typval_T *rettv)
7603{
Bram Moolenaar612cc382018-07-29 15:34:26 +02007604 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605
Bram Moolenaar612cc382018-07-29 15:34:26 +02007606 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607
7608 if (time_for_testing == 93784)
7609 {
7610 /* Testing the two-character code. */
7611 buf[0] = 'x';
7612 buf[1] = '!';
7613 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007614#ifdef FEAT_TERMINAL
7615 else if (term_use_loop())
7616 buf[0] = 't';
7617#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 else if (VIsual_active)
7619 {
7620 if (VIsual_select)
7621 buf[0] = VIsual_mode + 's' - 'v';
7622 else
7623 buf[0] = VIsual_mode;
7624 }
7625 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7626 || State == CONFIRM)
7627 {
7628 buf[0] = 'r';
7629 if (State == ASKMORE)
7630 buf[1] = 'm';
7631 else if (State == CONFIRM)
7632 buf[1] = '?';
7633 }
7634 else if (State == EXTERNCMD)
7635 buf[0] = '!';
7636 else if (State & INSERT)
7637 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638 if (State & VREPLACE_FLAG)
7639 {
7640 buf[0] = 'R';
7641 buf[1] = 'v';
7642 }
7643 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01007644 {
7645 if (State & REPLACE_FLAG)
7646 buf[0] = 'R';
7647 else
7648 buf[0] = 'i';
7649#ifdef FEAT_INS_EXPAND
7650 if (ins_compl_active())
7651 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01007652 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01007653 buf[1] = 'x';
7654#endif
7655 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007656 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007657 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007658 {
7659 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007660 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007662 else if (exmode_active == EXMODE_NORMAL)
7663 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 }
7665 else
7666 {
7667 buf[0] = 'n';
7668 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007669 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007671 // to be able to detect force-linewise/blockwise/characterwise operations
7672 buf[2] = motion_force;
7673 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02007674 else if (restart_edit == 'I' || restart_edit == 'R'
7675 || restart_edit == 'V')
7676 {
7677 buf[1] = 'i';
7678 buf[2] = restart_edit;
7679 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 }
7681
7682 /* Clear out the minor mode when the argument is not a non-zero number or
7683 * non-empty string. */
7684 if (!non_zero_arg(&argvars[0]))
7685 buf[1] = NUL;
7686
7687 rettv->vval.v_string = vim_strsave(buf);
7688 rettv->v_type = VAR_STRING;
7689}
7690
7691#if defined(FEAT_MZSCHEME) || defined(PROTO)
7692/*
7693 * "mzeval()" function
7694 */
7695 static void
7696f_mzeval(typval_T *argvars, typval_T *rettv)
7697{
7698 char_u *str;
7699 char_u buf[NUMBUFLEN];
7700
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007701 if (check_restricted() || check_secure())
7702 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007703 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007704 do_mzeval(str, rettv);
7705}
7706
7707 void
7708mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7709{
7710 typval_T argvars[3];
7711
7712 argvars[0].v_type = VAR_STRING;
7713 argvars[0].vval.v_string = name;
7714 copy_tv(args, &argvars[1]);
7715 argvars[2].v_type = VAR_UNKNOWN;
7716 f_call(argvars, rettv);
7717 clear_tv(&argvars[1]);
7718}
7719#endif
7720
7721/*
7722 * "nextnonblank()" function
7723 */
7724 static void
7725f_nextnonblank(typval_T *argvars, typval_T *rettv)
7726{
7727 linenr_T lnum;
7728
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007729 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 {
7731 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7732 {
7733 lnum = 0;
7734 break;
7735 }
7736 if (*skipwhite(ml_get(lnum)) != NUL)
7737 break;
7738 }
7739 rettv->vval.v_number = lnum;
7740}
7741
7742/*
7743 * "nr2char()" function
7744 */
7745 static void
7746f_nr2char(typval_T *argvars, typval_T *rettv)
7747{
7748 char_u buf[NUMBUFLEN];
7749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750 if (has_mbyte)
7751 {
7752 int utf8 = 0;
7753
7754 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007755 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01007757 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007759 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007760 }
7761 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007763 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 buf[1] = NUL;
7765 }
7766 rettv->v_type = VAR_STRING;
7767 rettv->vval.v_string = vim_strsave(buf);
7768}
7769
7770/*
7771 * "or(expr, expr)" function
7772 */
7773 static void
7774f_or(typval_T *argvars, typval_T *rettv)
7775{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007776 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
7777 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778}
7779
7780/*
7781 * "pathshorten()" function
7782 */
7783 static void
7784f_pathshorten(typval_T *argvars, typval_T *rettv)
7785{
7786 char_u *p;
7787
7788 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007789 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007790 if (p == NULL)
7791 rettv->vval.v_string = NULL;
7792 else
7793 {
7794 p = vim_strsave(p);
7795 rettv->vval.v_string = p;
7796 if (p != NULL)
7797 shorten_dir(p);
7798 }
7799}
7800
7801#ifdef FEAT_PERL
7802/*
7803 * "perleval()" function
7804 */
7805 static void
7806f_perleval(typval_T *argvars, typval_T *rettv)
7807{
7808 char_u *str;
7809 char_u buf[NUMBUFLEN];
7810
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007811 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007812 do_perleval(str, rettv);
7813}
7814#endif
7815
7816#ifdef FEAT_FLOAT
7817/*
7818 * "pow()" function
7819 */
7820 static void
7821f_pow(typval_T *argvars, typval_T *rettv)
7822{
7823 float_T fx = 0.0, fy = 0.0;
7824
7825 rettv->v_type = VAR_FLOAT;
7826 if (get_float_arg(argvars, &fx) == OK
7827 && get_float_arg(&argvars[1], &fy) == OK)
7828 rettv->vval.v_float = pow(fx, fy);
7829 else
7830 rettv->vval.v_float = 0.0;
7831}
7832#endif
7833
7834/*
7835 * "prevnonblank()" function
7836 */
7837 static void
7838f_prevnonblank(typval_T *argvars, typval_T *rettv)
7839{
7840 linenr_T lnum;
7841
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007842 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007843 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7844 lnum = 0;
7845 else
7846 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7847 --lnum;
7848 rettv->vval.v_number = lnum;
7849}
7850
7851/* This dummy va_list is here because:
7852 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7853 * - locally in the function results in a "used before set" warning
7854 * - using va_start() to initialize it gives "function with fixed args" error */
7855static va_list ap;
7856
7857/*
7858 * "printf()" function
7859 */
7860 static void
7861f_printf(typval_T *argvars, typval_T *rettv)
7862{
7863 char_u buf[NUMBUFLEN];
7864 int len;
7865 char_u *s;
7866 int saved_did_emsg = did_emsg;
7867 char *fmt;
7868
7869 rettv->v_type = VAR_STRING;
7870 rettv->vval.v_string = NULL;
7871
7872 /* Get the required length, allocate the buffer and do it for real. */
7873 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007874 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007875 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 if (!did_emsg)
7877 {
7878 s = alloc(len + 1);
7879 if (s != NULL)
7880 {
7881 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02007882 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
7883 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007884 }
7885 }
7886 did_emsg |= saved_did_emsg;
7887}
7888
7889/*
Bram Moolenaare9bd5722019-08-17 19:36:06 +02007890 * "pum_getpos()" function
7891 */
7892 static void
7893f_pum_getpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7894{
7895 if (rettv_dict_alloc(rettv) != OK)
7896 return;
7897#ifdef FEAT_INS_EXPAND
7898 pum_set_event_info(rettv->vval.v_dict);
7899#endif
7900}
7901
7902/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007903 * "pumvisible()" function
7904 */
7905 static void
7906f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7907{
7908#ifdef FEAT_INS_EXPAND
7909 if (pum_visible())
7910 rettv->vval.v_number = 1;
7911#endif
7912}
7913
7914#ifdef FEAT_PYTHON3
7915/*
7916 * "py3eval()" function
7917 */
7918 static void
7919f_py3eval(typval_T *argvars, typval_T *rettv)
7920{
7921 char_u *str;
7922 char_u buf[NUMBUFLEN];
7923
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007924 if (check_restricted() || check_secure())
7925 return;
7926
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007927 if (p_pyx == 0)
7928 p_pyx = 3;
7929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007930 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 do_py3eval(str, rettv);
7932}
7933#endif
7934
7935#ifdef FEAT_PYTHON
7936/*
7937 * "pyeval()" function
7938 */
7939 static void
7940f_pyeval(typval_T *argvars, typval_T *rettv)
7941{
7942 char_u *str;
7943 char_u buf[NUMBUFLEN];
7944
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007945 if (check_restricted() || check_secure())
7946 return;
7947
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007948 if (p_pyx == 0)
7949 p_pyx = 2;
7950
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007951 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 do_pyeval(str, rettv);
7953}
7954#endif
7955
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007956#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
7957/*
7958 * "pyxeval()" function
7959 */
7960 static void
7961f_pyxeval(typval_T *argvars, typval_T *rettv)
7962{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007963 if (check_restricted() || check_secure())
7964 return;
7965
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007966# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
7967 init_pyxversion();
7968 if (p_pyx == 2)
7969 f_pyeval(argvars, rettv);
7970 else
7971 f_py3eval(argvars, rettv);
7972# elif defined(FEAT_PYTHON)
7973 f_pyeval(argvars, rettv);
7974# elif defined(FEAT_PYTHON3)
7975 f_py3eval(argvars, rettv);
7976# endif
7977}
7978#endif
7979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980/*
7981 * "range()" function
7982 */
7983 static void
7984f_range(typval_T *argvars, typval_T *rettv)
7985{
7986 varnumber_T start;
7987 varnumber_T end;
7988 varnumber_T stride = 1;
7989 varnumber_T i;
7990 int error = FALSE;
7991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007992 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 if (argvars[1].v_type == VAR_UNKNOWN)
7994 {
7995 end = start - 1;
7996 start = 0;
7997 }
7998 else
7999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008000 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008002 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 }
8004
8005 if (error)
8006 return; /* type error; errmsg already given */
8007 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008008 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008010 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 else
8012 {
8013 if (rettv_list_alloc(rettv) == OK)
8014 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8015 if (list_append_number(rettv->vval.v_list,
8016 (varnumber_T)i) == FAIL)
8017 break;
8018 }
8019}
8020
8021/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008022 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008023 */
8024 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008025readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008026{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008027 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008028 typval_T save_val;
8029 typval_T rettv;
8030 typval_T argv[2];
8031 int retval = 0;
8032 int error = FALSE;
8033
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008034 if (expr->v_type == VAR_UNKNOWN)
8035 return 1;
8036
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008037 prepare_vimvar(VV_VAL, &save_val);
8038 set_vim_var_string(VV_VAL, name, -1);
8039 argv[0].v_type = VAR_STRING;
8040 argv[0].vval.v_string = name;
8041
8042 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
8043 goto theend;
8044
8045 retval = tv_get_number_chk(&rettv, &error);
8046 if (error)
8047 retval = -1;
8048 clear_tv(&rettv);
8049
8050theend:
8051 set_vim_var_string(VV_VAL, NULL, 0);
8052 restore_vimvar(VV_VAL, &save_val);
8053 return retval;
8054}
8055
8056/*
8057 * "readdir()" function
8058 */
8059 static void
8060f_readdir(typval_T *argvars, typval_T *rettv)
8061{
8062 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008063 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008064 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008065 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008066 garray_T ga;
8067 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008068
8069 if (rettv_list_alloc(rettv) == FAIL)
8070 return;
8071 path = tv_get_string(&argvars[0]);
8072 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008073
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02008074 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
8075 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008076 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008077 for (i = 0; i < ga.ga_len; i++)
8078 {
8079 p = ((char_u **)ga.ga_data)[i];
8080 list_append_string(rettv->vval.v_list, p, -1);
8081 }
8082 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02008083 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02008084}
8085
8086/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 * "readfile()" function
8088 */
8089 static void
8090f_readfile(typval_T *argvars, typval_T *rettv)
8091{
8092 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008093 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 int failed = FALSE;
8095 char_u *fname;
8096 FILE *fd;
8097 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8098 int io_size = sizeof(buf);
8099 int readlen; /* size of last fread() */
8100 char_u *prev = NULL; /* previously read bytes, if any */
8101 long prevlen = 0; /* length of data in prev */
8102 long prevsize = 0; /* size of prev buffer */
8103 long maxline = MAXLNUM;
8104 long cnt = 0;
8105 char_u *p; /* position in buf */
8106 char_u *start; /* start of current line */
8107
8108 if (argvars[1].v_type != VAR_UNKNOWN)
8109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008110 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008112 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
8113 blob = TRUE;
8114
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008116 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008117 }
8118
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008119 if (blob)
8120 {
8121 if (rettv_blob_alloc(rettv) == FAIL)
8122 return;
8123 }
8124 else
8125 {
8126 if (rettv_list_alloc(rettv) == FAIL)
8127 return;
8128 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008129
8130 /* Always open the file in binary mode, library functions have a mind of
8131 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008132 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008135 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 return;
8137 }
8138
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008139 if (blob)
8140 {
8141 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
8142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008143 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008144 blob_free(rettv->vval.v_blob);
8145 }
8146 fclose(fd);
8147 return;
8148 }
8149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 while (cnt < maxline || maxline < 0)
8151 {
8152 readlen = (int)fread(buf, 1, io_size, fd);
8153
8154 /* This for loop processes what was read, but is also entered at end
8155 * of file so that either:
8156 * - an incomplete line gets written
8157 * - a "binary" file gets an empty line at the end if it ends in a
8158 * newline. */
8159 for (p = buf, start = buf;
8160 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8161 ++p)
8162 {
8163 if (*p == '\n' || readlen <= 0)
8164 {
8165 listitem_T *li;
8166 char_u *s = NULL;
8167 long_u len = p - start;
8168
8169 /* Finished a line. Remove CRs before NL. */
8170 if (readlen > 0 && !binary)
8171 {
8172 while (len > 0 && start[len - 1] == '\r')
8173 --len;
8174 /* removal may cross back to the "prev" string */
8175 if (len == 0)
8176 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8177 --prevlen;
8178 }
8179 if (prevlen == 0)
8180 s = vim_strnsave(start, (int)len);
8181 else
8182 {
8183 /* Change "prev" buffer to be the right size. This way
8184 * the bytes are only copied once, and very long lines are
8185 * allocated only once. */
8186 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8187 {
8188 mch_memmove(s + prevlen, start, len);
8189 s[prevlen + len] = NUL;
8190 prev = NULL; /* the list will own the string */
8191 prevlen = prevsize = 0;
8192 }
8193 }
8194 if (s == NULL)
8195 {
8196 do_outofmem_msg((long_u) prevlen + len + 1);
8197 failed = TRUE;
8198 break;
8199 }
8200
8201 if ((li = listitem_alloc()) == NULL)
8202 {
8203 vim_free(s);
8204 failed = TRUE;
8205 break;
8206 }
8207 li->li_tv.v_type = VAR_STRING;
8208 li->li_tv.v_lock = 0;
8209 li->li_tv.vval.v_string = s;
8210 list_append(rettv->vval.v_list, li);
8211
8212 start = p + 1; /* step over newline */
8213 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8214 break;
8215 }
8216 else if (*p == NUL)
8217 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8219 * when finding the BF and check the previous two bytes. */
8220 else if (*p == 0xbf && enc_utf8 && !binary)
8221 {
8222 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8223 * + 1, these may be in the "prev" string. */
8224 char_u back1 = p >= buf + 1 ? p[-1]
8225 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8226 char_u back2 = p >= buf + 2 ? p[-2]
8227 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8228 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8229
8230 if (back2 == 0xef && back1 == 0xbb)
8231 {
8232 char_u *dest = p - 2;
8233
8234 /* Usually a BOM is at the beginning of a file, and so at
8235 * the beginning of a line; then we can just step over it.
8236 */
8237 if (start == dest)
8238 start = p + 1;
8239 else
8240 {
8241 /* have to shuffle buf to close gap */
8242 int adjust_prevlen = 0;
8243
8244 if (dest < buf)
8245 {
8246 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8247 dest = buf;
8248 }
8249 if (readlen > p - buf + 1)
8250 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8251 readlen -= 3 - adjust_prevlen;
8252 prevlen -= adjust_prevlen;
8253 p = dest - 1;
8254 }
8255 }
8256 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 } /* for */
8258
8259 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8260 break;
8261 if (start < p)
8262 {
8263 /* There's part of a line in buf, store it in "prev". */
8264 if (p - start + prevlen >= prevsize)
8265 {
8266 /* need bigger "prev" buffer */
8267 char_u *newprev;
8268
8269 /* A common use case is ordinary text files and "prev" gets a
8270 * fragment of a line, so the first allocation is made
8271 * small, to avoid repeatedly 'allocing' large and
8272 * 'reallocing' small. */
8273 if (prevsize == 0)
8274 prevsize = (long)(p - start);
8275 else
8276 {
8277 long grow50pc = (prevsize * 3) / 2;
8278 long growmin = (long)((p - start) * 2 + prevlen);
8279 prevsize = grow50pc > growmin ? grow50pc : growmin;
8280 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008281 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 if (newprev == NULL)
8283 {
8284 do_outofmem_msg((long_u)prevsize);
8285 failed = TRUE;
8286 break;
8287 }
8288 prev = newprev;
8289 }
8290 /* Add the line part to end of "prev". */
8291 mch_memmove(prev + prevlen, start, p - start);
8292 prevlen += (long)(p - start);
8293 }
8294 } /* while */
8295
8296 /*
8297 * For a negative line count use only the lines at the end of the file,
8298 * free the rest.
8299 */
8300 if (!failed && maxline < 0)
8301 while (cnt > -maxline)
8302 {
8303 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8304 --cnt;
8305 }
8306
8307 if (failed)
8308 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008309 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02008311 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 }
8313
8314 vim_free(prev);
8315 fclose(fd);
8316}
8317
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008318 static void
8319return_register(int regname, typval_T *rettv)
8320{
8321 char_u buf[2] = {0, 0};
8322
8323 buf[0] = (char_u)regname;
8324 rettv->v_type = VAR_STRING;
8325 rettv->vval.v_string = vim_strsave(buf);
8326}
8327
8328/*
8329 * "reg_executing()" function
8330 */
8331 static void
8332f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
8333{
8334 return_register(reg_executing, rettv);
8335}
8336
8337/*
8338 * "reg_recording()" function
8339 */
8340 static void
8341f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
8342{
8343 return_register(reg_recording, rettv);
8344}
8345
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347/*
8348 * Convert a List to proftime_T.
8349 * Return FAIL when there is something wrong.
8350 */
8351 static int
8352list2proftime(typval_T *arg, proftime_T *tm)
8353{
8354 long n1, n2;
8355 int error = FALSE;
8356
8357 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8358 || arg->vval.v_list->lv_len != 2)
8359 return FAIL;
8360 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8361 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008362# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 tm->HighPart = n1;
8364 tm->LowPart = n2;
8365# else
8366 tm->tv_sec = n1;
8367 tm->tv_usec = n2;
8368# endif
8369 return error ? FAIL : OK;
8370}
8371#endif /* FEAT_RELTIME */
8372
8373/*
8374 * "reltime()" function
8375 */
8376 static void
8377f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8378{
8379#ifdef FEAT_RELTIME
8380 proftime_T res;
8381 proftime_T start;
8382
8383 if (argvars[0].v_type == VAR_UNKNOWN)
8384 {
8385 /* No arguments: get current time. */
8386 profile_start(&res);
8387 }
8388 else if (argvars[1].v_type == VAR_UNKNOWN)
8389 {
8390 if (list2proftime(&argvars[0], &res) == FAIL)
8391 return;
8392 profile_end(&res);
8393 }
8394 else
8395 {
8396 /* Two arguments: compute the difference. */
8397 if (list2proftime(&argvars[0], &start) == FAIL
8398 || list2proftime(&argvars[1], &res) == FAIL)
8399 return;
8400 profile_sub(&res, &start);
8401 }
8402
8403 if (rettv_list_alloc(rettv) == OK)
8404 {
8405 long n1, n2;
8406
Bram Moolenaar4f974752019-02-17 17:44:42 +01008407# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 n1 = res.HighPart;
8409 n2 = res.LowPart;
8410# else
8411 n1 = res.tv_sec;
8412 n2 = res.tv_usec;
8413# endif
8414 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8415 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8416 }
8417#endif
8418}
8419
8420#ifdef FEAT_FLOAT
8421/*
8422 * "reltimefloat()" function
8423 */
8424 static void
8425f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8426{
8427# ifdef FEAT_RELTIME
8428 proftime_T tm;
8429# endif
8430
8431 rettv->v_type = VAR_FLOAT;
8432 rettv->vval.v_float = 0;
8433# ifdef FEAT_RELTIME
8434 if (list2proftime(&argvars[0], &tm) == OK)
8435 rettv->vval.v_float = profile_float(&tm);
8436# endif
8437}
8438#endif
8439
8440/*
8441 * "reltimestr()" function
8442 */
8443 static void
8444f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8445{
8446#ifdef FEAT_RELTIME
8447 proftime_T tm;
8448#endif
8449
8450 rettv->v_type = VAR_STRING;
8451 rettv->vval.v_string = NULL;
8452#ifdef FEAT_RELTIME
8453 if (list2proftime(&argvars[0], &tm) == OK)
8454 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8455#endif
8456}
8457
8458#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 static void
8460make_connection(void)
8461{
8462 if (X_DISPLAY == NULL
8463# ifdef FEAT_GUI
8464 && !gui.in_use
8465# endif
8466 )
8467 {
8468 x_force_connect = TRUE;
8469 setup_term_clip();
8470 x_force_connect = FALSE;
8471 }
8472}
8473
8474 static int
8475check_connection(void)
8476{
8477 make_connection();
8478 if (X_DISPLAY == NULL)
8479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008480 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 return FAIL;
8482 }
8483 return OK;
8484}
8485#endif
8486
8487#ifdef FEAT_CLIENTSERVER
8488 static void
8489remote_common(typval_T *argvars, typval_T *rettv, int expr)
8490{
8491 char_u *server_name;
8492 char_u *keys;
8493 char_u *r = NULL;
8494 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008495 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008496# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008497 HWND w;
8498# else
8499 Window w;
8500# endif
8501
8502 if (check_restricted() || check_secure())
8503 return;
8504
8505# ifdef FEAT_X11
8506 if (check_connection() == FAIL)
8507 return;
8508# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008509 if (argvars[2].v_type != VAR_UNKNOWN
8510 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008511 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008513 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 if (server_name == NULL)
8515 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008516 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01008517# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008518 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008520 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8521 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522# endif
8523 {
8524 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008526 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008527 vim_free(r);
8528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008530 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008531 return;
8532 }
8533
8534 rettv->vval.v_string = r;
8535
8536 if (argvars[2].v_type != VAR_UNKNOWN)
8537 {
8538 dictitem_T v;
8539 char_u str[30];
8540 char_u *idvar;
8541
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008542 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008543 if (idvar != NULL && *idvar != NUL)
8544 {
8545 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8546 v.di_tv.v_type = VAR_STRING;
8547 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008549 vim_free(v.di_tv.vval.v_string);
8550 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008551 }
8552}
8553#endif
8554
8555/*
8556 * "remote_expr()" function
8557 */
8558 static void
8559f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8560{
8561 rettv->v_type = VAR_STRING;
8562 rettv->vval.v_string = NULL;
8563#ifdef FEAT_CLIENTSERVER
8564 remote_common(argvars, rettv, TRUE);
8565#endif
8566}
8567
8568/*
8569 * "remote_foreground()" function
8570 */
8571 static void
8572f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8573{
8574#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01008575# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 /* On Win32 it's done in this application. */
8577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008578 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008579
8580 if (server_name != NULL)
8581 serverForeground(server_name);
8582 }
8583# else
8584 /* Send a foreground() expression to the server. */
8585 argvars[1].v_type = VAR_STRING;
8586 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8587 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008588 rettv->v_type = VAR_STRING;
8589 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 remote_common(argvars, rettv, TRUE);
8591 vim_free(argvars[1].vval.v_string);
8592# endif
8593#endif
8594}
8595
8596 static void
8597f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8598{
8599#ifdef FEAT_CLIENTSERVER
8600 dictitem_T v;
8601 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008602# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 long_u n = 0;
8604# endif
8605 char_u *serverid;
8606
8607 if (check_restricted() || check_secure())
8608 {
8609 rettv->vval.v_number = -1;
8610 return;
8611 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008612 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008613 if (serverid == NULL)
8614 {
8615 rettv->vval.v_number = -1;
8616 return; /* type error; errmsg already given */
8617 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01008618# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8620 if (n == 0)
8621 rettv->vval.v_number = -1;
8622 else
8623 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008624 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 rettv->vval.v_number = (s != NULL);
8626 }
8627# else
8628 if (check_connection() == FAIL)
8629 return;
8630
8631 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8632 serverStrToWin(serverid), &s);
8633# endif
8634
8635 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8636 {
8637 char_u *retvar;
8638
8639 v.di_tv.v_type = VAR_STRING;
8640 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 if (retvar != NULL)
8643 set_var(retvar, &v.di_tv, FALSE);
8644 vim_free(v.di_tv.vval.v_string);
8645 }
8646#else
8647 rettv->vval.v_number = -1;
8648#endif
8649}
8650
8651 static void
8652f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8653{
8654 char_u *r = NULL;
8655
8656#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008657 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658
8659 if (serverid != NULL && !check_restricted() && !check_secure())
8660 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008661 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01008662# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008663 /* The server's HWND is encoded in the 'id' parameter */
8664 long_u n = 0;
8665# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008666
8667 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008668 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008669
Bram Moolenaar4f974752019-02-17 17:44:42 +01008670# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8672 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008673 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 if (r == NULL)
8675# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008676 if (check_connection() == FAIL
8677 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8678 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008680 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 }
8682#endif
8683 rettv->v_type = VAR_STRING;
8684 rettv->vval.v_string = r;
8685}
8686
8687/*
8688 * "remote_send()" function
8689 */
8690 static void
8691f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8692{
8693 rettv->v_type = VAR_STRING;
8694 rettv->vval.v_string = NULL;
8695#ifdef FEAT_CLIENTSERVER
8696 remote_common(argvars, rettv, FALSE);
8697#endif
8698}
8699
8700/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008701 * "remote_startserver()" function
8702 */
8703 static void
8704f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8705{
8706#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008707 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008708
8709 if (server == NULL)
8710 return; /* type error; errmsg already given */
8711 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008712 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008713 else
8714 {
8715# ifdef FEAT_X11
8716 if (check_connection() == OK)
8717 serverRegisterName(X_DISPLAY, server);
8718# else
8719 serverSetName(server);
8720# endif
8721 }
8722#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008723 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008724#endif
8725}
8726
8727/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 * "remove()" function
8729 */
8730 static void
8731f_remove(typval_T *argvars, typval_T *rettv)
8732{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008733 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8734
8735 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008736 dict_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008737 else if (argvars[0].v_type == VAR_BLOB)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02008738 blob_remove(argvars, rettv);
8739 else if (argvars[0].v_type == VAR_LIST)
8740 list_remove(argvars, rettv, arg_errmsg);
8741 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01008742 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743}
8744
8745/*
8746 * "rename({from}, {to})" function
8747 */
8748 static void
8749f_rename(typval_T *argvars, typval_T *rettv)
8750{
8751 char_u buf[NUMBUFLEN];
8752
8753 if (check_restricted() || check_secure())
8754 rettv->vval.v_number = -1;
8755 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
8757 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758}
8759
8760/*
8761 * "repeat()" function
8762 */
8763 static void
8764f_repeat(typval_T *argvars, typval_T *rettv)
8765{
8766 char_u *p;
8767 int n;
8768 int slen;
8769 int len;
8770 char_u *r;
8771 int i;
8772
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008773 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774 if (argvars[0].v_type == VAR_LIST)
8775 {
8776 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8777 while (n-- > 0)
8778 if (list_extend(rettv->vval.v_list,
8779 argvars[0].vval.v_list, NULL) == FAIL)
8780 break;
8781 }
8782 else
8783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008784 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 rettv->v_type = VAR_STRING;
8786 rettv->vval.v_string = NULL;
8787
8788 slen = (int)STRLEN(p);
8789 len = slen * n;
8790 if (len <= 0)
8791 return;
8792
8793 r = alloc(len + 1);
8794 if (r != NULL)
8795 {
8796 for (i = 0; i < n; i++)
8797 mch_memmove(r + i * slen, p, (size_t)slen);
8798 r[len] = NUL;
8799 }
8800
8801 rettv->vval.v_string = r;
8802 }
8803}
8804
8805/*
8806 * "resolve()" function
8807 */
8808 static void
8809f_resolve(typval_T *argvars, typval_T *rettv)
8810{
8811 char_u *p;
8812#ifdef HAVE_READLINK
8813 char_u *buf = NULL;
8814#endif
8815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008816 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008817#ifdef FEAT_SHORTCUT
8818 {
8819 char_u *v = NULL;
8820
Bram Moolenaardce1e892019-02-10 23:18:53 +01008821 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 if (v != NULL)
8823 rettv->vval.v_string = v;
8824 else
8825 rettv->vval.v_string = vim_strsave(p);
8826 }
8827#else
8828# ifdef HAVE_READLINK
8829 {
8830 char_u *cpy;
8831 int len;
8832 char_u *remain = NULL;
8833 char_u *q;
8834 int is_relative_to_current = FALSE;
8835 int has_trailing_pathsep = FALSE;
8836 int limit = 100;
8837
8838 p = vim_strsave(p);
8839
8840 if (p[0] == '.' && (vim_ispathsep(p[1])
8841 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8842 is_relative_to_current = TRUE;
8843
8844 len = STRLEN(p);
8845 if (len > 0 && after_pathsep(p, p + len))
8846 {
8847 has_trailing_pathsep = TRUE;
8848 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8849 }
8850
8851 q = getnextcomp(p);
8852 if (*q != NUL)
8853 {
8854 /* Separate the first path component in "p", and keep the
8855 * remainder (beginning with the path separator). */
8856 remain = vim_strsave(q - 1);
8857 q[-1] = NUL;
8858 }
8859
8860 buf = alloc(MAXPATHL + 1);
8861 if (buf == NULL)
8862 goto fail;
8863
8864 for (;;)
8865 {
8866 for (;;)
8867 {
8868 len = readlink((char *)p, (char *)buf, MAXPATHL);
8869 if (len <= 0)
8870 break;
8871 buf[len] = NUL;
8872
8873 if (limit-- == 0)
8874 {
8875 vim_free(p);
8876 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008877 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008878 rettv->vval.v_string = NULL;
8879 goto fail;
8880 }
8881
8882 /* Ensure that the result will have a trailing path separator
8883 * if the argument has one. */
8884 if (remain == NULL && has_trailing_pathsep)
8885 add_pathsep(buf);
8886
8887 /* Separate the first path component in the link value and
8888 * concatenate the remainders. */
8889 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8890 if (*q != NUL)
8891 {
8892 if (remain == NULL)
8893 remain = vim_strsave(q - 1);
8894 else
8895 {
8896 cpy = concat_str(q - 1, remain);
8897 if (cpy != NULL)
8898 {
8899 vim_free(remain);
8900 remain = cpy;
8901 }
8902 }
8903 q[-1] = NUL;
8904 }
8905
8906 q = gettail(p);
8907 if (q > p && *q == NUL)
8908 {
8909 /* Ignore trailing path separator. */
8910 q[-1] = NUL;
8911 q = gettail(p);
8912 }
8913 if (q > p && !mch_isFullName(buf))
8914 {
8915 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +02008916 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917 if (cpy != NULL)
8918 {
8919 STRCPY(cpy, p);
8920 STRCPY(gettail(cpy), buf);
8921 vim_free(p);
8922 p = cpy;
8923 }
8924 }
8925 else
8926 {
8927 vim_free(p);
8928 p = vim_strsave(buf);
8929 }
8930 }
8931
8932 if (remain == NULL)
8933 break;
8934
8935 /* Append the first path component of "remain" to "p". */
8936 q = getnextcomp(remain + 1);
8937 len = q - remain - (*q != NUL);
8938 cpy = vim_strnsave(p, STRLEN(p) + len);
8939 if (cpy != NULL)
8940 {
8941 STRNCAT(cpy, remain, len);
8942 vim_free(p);
8943 p = cpy;
8944 }
8945 /* Shorten "remain". */
8946 if (*q != NUL)
8947 STRMOVE(remain, q - 1);
8948 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01008949 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 }
8951
8952 /* If the result is a relative path name, make it explicitly relative to
8953 * the current directory if and only if the argument had this form. */
8954 if (!vim_ispathsep(*p))
8955 {
8956 if (is_relative_to_current
8957 && *p != NUL
8958 && !(p[0] == '.'
8959 && (p[1] == NUL
8960 || vim_ispathsep(p[1])
8961 || (p[1] == '.'
8962 && (p[2] == NUL
8963 || vim_ispathsep(p[2]))))))
8964 {
8965 /* Prepend "./". */
8966 cpy = concat_str((char_u *)"./", p);
8967 if (cpy != NULL)
8968 {
8969 vim_free(p);
8970 p = cpy;
8971 }
8972 }
8973 else if (!is_relative_to_current)
8974 {
8975 /* Strip leading "./". */
8976 q = p;
8977 while (q[0] == '.' && vim_ispathsep(q[1]))
8978 q += 2;
8979 if (q > p)
8980 STRMOVE(p, p + 2);
8981 }
8982 }
8983
8984 /* Ensure that the result will have no trailing path separator
8985 * if the argument had none. But keep "/" or "//". */
8986 if (!has_trailing_pathsep)
8987 {
8988 q = p + STRLEN(p);
8989 if (after_pathsep(p, q))
8990 *gettail_sep(p) = NUL;
8991 }
8992
8993 rettv->vval.v_string = p;
8994 }
8995# else
8996 rettv->vval.v_string = vim_strsave(p);
8997# endif
8998#endif
8999
9000 simplify_filename(rettv->vval.v_string);
9001
9002#ifdef HAVE_READLINK
9003fail:
9004 vim_free(buf);
9005#endif
9006 rettv->v_type = VAR_STRING;
9007}
9008
9009/*
9010 * "reverse({list})" function
9011 */
9012 static void
9013f_reverse(typval_T *argvars, typval_T *rettv)
9014{
9015 list_T *l;
9016 listitem_T *li, *ni;
9017
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009018 if (argvars[0].v_type == VAR_BLOB)
9019 {
9020 blob_T *b = argvars[0].vval.v_blob;
9021 int i, len = blob_len(b);
9022
9023 for (i = 0; i < len / 2; i++)
9024 {
9025 int tmp = blob_get(b, i);
9026
9027 blob_set(b, i, blob_get(b, len - i - 1));
9028 blob_set(b, len - i - 1, tmp);
9029 }
9030 rettv_blob_set(rettv, b);
9031 return;
9032 }
9033
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009035 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01009037 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009038 (char_u *)N_("reverse() argument"), TRUE))
9039 {
9040 li = l->lv_last;
9041 l->lv_first = l->lv_last = NULL;
9042 l->lv_len = 0;
9043 while (li != NULL)
9044 {
9045 ni = li->li_prev;
9046 list_append(l, li);
9047 li = ni;
9048 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009049 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050 l->lv_idx = l->lv_len - l->lv_idx - 1;
9051 }
9052}
9053
9054#define SP_NOMOVE 0x01 /* don't move cursor */
9055#define SP_REPEAT 0x02 /* repeat to find outer pair */
9056#define SP_RETCOUNT 0x04 /* return matchcount */
9057#define SP_SETPCMARK 0x08 /* set previous context mark */
9058#define SP_START 0x10 /* accept match at start position */
9059#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9060#define SP_END 0x40 /* leave cursor at end of match */
9061#define SP_COLUMN 0x80 /* start at cursor column */
9062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063/*
9064 * Get flags for a search function.
9065 * Possibly sets "p_ws".
9066 * Returns BACKWARD, FORWARD or zero (for an error).
9067 */
9068 static int
9069get_search_arg(typval_T *varp, int *flagsp)
9070{
9071 int dir = FORWARD;
9072 char_u *flags;
9073 char_u nbuf[NUMBUFLEN];
9074 int mask;
9075
9076 if (varp->v_type != VAR_UNKNOWN)
9077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009078 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009079 if (flags == NULL)
9080 return 0; /* type error; errmsg already given */
9081 while (*flags != NUL)
9082 {
9083 switch (*flags)
9084 {
9085 case 'b': dir = BACKWARD; break;
9086 case 'w': p_ws = TRUE; break;
9087 case 'W': p_ws = FALSE; break;
9088 default: mask = 0;
9089 if (flagsp != NULL)
9090 switch (*flags)
9091 {
9092 case 'c': mask = SP_START; break;
9093 case 'e': mask = SP_END; break;
9094 case 'm': mask = SP_RETCOUNT; break;
9095 case 'n': mask = SP_NOMOVE; break;
9096 case 'p': mask = SP_SUBPAT; break;
9097 case 'r': mask = SP_REPEAT; break;
9098 case 's': mask = SP_SETPCMARK; break;
9099 case 'z': mask = SP_COLUMN; break;
9100 }
9101 if (mask == 0)
9102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009103 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 dir = 0;
9105 }
9106 else
9107 *flagsp |= mask;
9108 }
9109 if (dir == 0)
9110 break;
9111 ++flags;
9112 }
9113 }
9114 return dir;
9115}
9116
9117/*
9118 * Shared by search() and searchpos() functions.
9119 */
9120 static int
9121search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9122{
9123 int flags;
9124 char_u *pat;
9125 pos_T pos;
9126 pos_T save_cursor;
9127 int save_p_ws = p_ws;
9128 int dir;
9129 int retval = 0; /* default: FAIL */
9130 long lnum_stop = 0;
9131 proftime_T tm;
9132#ifdef FEAT_RELTIME
9133 long time_limit = 0;
9134#endif
9135 int options = SEARCH_KEEP;
9136 int subpatnum;
9137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009138 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9140 if (dir == 0)
9141 goto theend;
9142 flags = *flagsp;
9143 if (flags & SP_START)
9144 options |= SEARCH_START;
9145 if (flags & SP_END)
9146 options |= SEARCH_END;
9147 if (flags & SP_COLUMN)
9148 options |= SEARCH_COL;
9149
9150 /* Optional arguments: line number to stop searching and timeout. */
9151 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009153 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 if (lnum_stop < 0)
9155 goto theend;
9156#ifdef FEAT_RELTIME
9157 if (argvars[3].v_type != VAR_UNKNOWN)
9158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009159 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009160 if (time_limit < 0)
9161 goto theend;
9162 }
9163#endif
9164 }
9165
9166#ifdef FEAT_RELTIME
9167 /* Set the time limit, if there is one. */
9168 profile_setlimit(time_limit, &tm);
9169#endif
9170
9171 /*
9172 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9173 * Check to make sure only those flags are set.
9174 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9175 * flags cannot be set. Check for that condition also.
9176 */
9177 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9178 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009180 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 goto theend;
9182 }
9183
9184 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009185 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009186 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 if (subpatnum != FAIL)
9188 {
9189 if (flags & SP_SUBPAT)
9190 retval = subpatnum;
9191 else
9192 retval = pos.lnum;
9193 if (flags & SP_SETPCMARK)
9194 setpcmark();
9195 curwin->w_cursor = pos;
9196 if (match_pos != NULL)
9197 {
9198 /* Store the match cursor position */
9199 match_pos->lnum = pos.lnum;
9200 match_pos->col = pos.col + 1;
9201 }
9202 /* "/$" will put the cursor after the end of the line, may need to
9203 * correct that here */
9204 check_cursor();
9205 }
9206
9207 /* If 'n' flag is used: restore cursor position. */
9208 if (flags & SP_NOMOVE)
9209 curwin->w_cursor = save_cursor;
9210 else
9211 curwin->w_set_curswant = TRUE;
9212theend:
9213 p_ws = save_p_ws;
9214
9215 return retval;
9216}
9217
9218#ifdef FEAT_FLOAT
9219
9220/*
9221 * round() is not in C90, use ceil() or floor() instead.
9222 */
9223 float_T
9224vim_round(float_T f)
9225{
9226 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9227}
9228
9229/*
9230 * "round({float})" function
9231 */
9232 static void
9233f_round(typval_T *argvars, typval_T *rettv)
9234{
9235 float_T f = 0.0;
9236
9237 rettv->v_type = VAR_FLOAT;
9238 if (get_float_arg(argvars, &f) == OK)
9239 rettv->vval.v_float = vim_round(f);
9240 else
9241 rettv->vval.v_float = 0.0;
9242}
9243#endif
9244
Bram Moolenaare99be0e2019-03-26 22:51:09 +01009245#ifdef FEAT_RUBY
9246/*
9247 * "rubyeval()" function
9248 */
9249 static void
9250f_rubyeval(typval_T *argvars, typval_T *rettv)
9251{
9252 char_u *str;
9253 char_u buf[NUMBUFLEN];
9254
9255 str = tv_get_string_buf(&argvars[0], buf);
9256 do_rubyeval(str, rettv);
9257}
9258#endif
9259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260/*
9261 * "screenattr()" function
9262 */
9263 static void
9264f_screenattr(typval_T *argvars, typval_T *rettv)
9265{
9266 int row;
9267 int col;
9268 int c;
9269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009270 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9271 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272 if (row < 0 || row >= screen_Rows
9273 || col < 0 || col >= screen_Columns)
9274 c = -1;
9275 else
9276 c = ScreenAttrs[LineOffset[row] + col];
9277 rettv->vval.v_number = c;
9278}
9279
9280/*
9281 * "screenchar()" function
9282 */
9283 static void
9284f_screenchar(typval_T *argvars, typval_T *rettv)
9285{
9286 int row;
9287 int col;
9288 int off;
9289 int c;
9290
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009291 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9292 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009293 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009294 c = -1;
9295 else
9296 {
9297 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009298 if (enc_utf8 && ScreenLinesUC[off] != 0)
9299 c = ScreenLinesUC[off];
9300 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009301 c = ScreenLines[off];
9302 }
9303 rettv->vval.v_number = c;
9304}
9305
9306/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009307 * "screenchars()" function
9308 */
9309 static void
9310f_screenchars(typval_T *argvars, typval_T *rettv)
9311{
9312 int row;
9313 int col;
9314 int off;
9315 int c;
9316 int i;
9317
9318 if (rettv_list_alloc(rettv) == FAIL)
9319 return;
9320 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9321 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9322 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9323 return;
9324
9325 off = LineOffset[row] + col;
9326 if (enc_utf8 && ScreenLinesUC[off] != 0)
9327 c = ScreenLinesUC[off];
9328 else
9329 c = ScreenLines[off];
9330 list_append_number(rettv->vval.v_list, (varnumber_T)c);
9331
9332 if (enc_utf8)
9333
9334 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9335 list_append_number(rettv->vval.v_list,
9336 (varnumber_T)ScreenLinesC[i][off]);
9337}
9338
9339/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 * "screencol()" function
9341 *
9342 * First column is 1 to be consistent with virtcol().
9343 */
9344 static void
9345f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9346{
9347 rettv->vval.v_number = screen_screencol() + 1;
9348}
9349
9350/*
9351 * "screenrow()" function
9352 */
9353 static void
9354f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9355{
9356 rettv->vval.v_number = screen_screenrow() + 1;
9357}
9358
9359/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +01009360 * "screenstring()" function
9361 */
9362 static void
9363f_screenstring(typval_T *argvars, typval_T *rettv)
9364{
9365 int row;
9366 int col;
9367 int off;
9368 int c;
9369 int i;
9370 char_u buf[MB_MAXBYTES + 1];
9371 int buflen = 0;
9372
9373 rettv->vval.v_string = NULL;
9374 rettv->v_type = VAR_STRING;
9375
9376 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
9377 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
9378 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
9379 return;
9380
9381 off = LineOffset[row] + col;
9382 if (enc_utf8 && ScreenLinesUC[off] != 0)
9383 c = ScreenLinesUC[off];
9384 else
9385 c = ScreenLines[off];
9386 buflen += mb_char2bytes(c, buf);
9387
9388 if (enc_utf8)
9389 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
9390 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
9391
9392 buf[buflen] = NUL;
9393 rettv->vval.v_string = vim_strsave(buf);
9394}
9395
9396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397 * "search()" function
9398 */
9399 static void
9400f_search(typval_T *argvars, typval_T *rettv)
9401{
9402 int flags = 0;
9403
9404 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9405}
9406
9407/*
9408 * "searchdecl()" function
9409 */
9410 static void
9411f_searchdecl(typval_T *argvars, typval_T *rettv)
9412{
9413 int locally = 1;
9414 int thisblock = 0;
9415 int error = FALSE;
9416 char_u *name;
9417
9418 rettv->vval.v_number = 1; /* default: FAIL */
9419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009420 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 if (argvars[1].v_type != VAR_UNKNOWN)
9422 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009423 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009425 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 }
9427 if (!error && name != NULL)
9428 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9429 locally, thisblock, SEARCH_KEEP) == FAIL;
9430}
9431
9432/*
9433 * Used by searchpair() and searchpairpos()
9434 */
9435 static int
9436searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9437{
9438 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009439 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009440 int save_p_ws = p_ws;
9441 int dir;
9442 int flags = 0;
9443 char_u nbuf1[NUMBUFLEN];
9444 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445 int retval = 0; /* default: FAIL */
9446 long lnum_stop = 0;
9447 long time_limit = 0;
9448
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009449 /* Get the three pattern arguments: start, middle, end. Will result in an
9450 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009451 spat = tv_get_string_chk(&argvars[0]);
9452 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
9453 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454 if (spat == NULL || mpat == NULL || epat == NULL)
9455 goto theend; /* type error */
9456
9457 /* Handle the optional fourth argument: flags */
9458 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9459 if (dir == 0)
9460 goto theend;
9461
9462 /* Don't accept SP_END or SP_SUBPAT.
9463 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9464 */
9465 if ((flags & (SP_END | SP_SUBPAT)) != 0
9466 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009468 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 goto theend;
9470 }
9471
9472 /* Using 'r' implies 'W', otherwise it doesn't work. */
9473 if (flags & SP_REPEAT)
9474 p_ws = FALSE;
9475
9476 /* Optional fifth argument: skip expression */
9477 if (argvars[3].v_type == VAR_UNKNOWN
9478 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009479 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 else
9481 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009482 skip = &argvars[4];
9483 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9484 && skip->v_type != VAR_STRING)
9485 {
9486 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009487 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +01009488 goto theend;
9489 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490 if (argvars[5].v_type != VAR_UNKNOWN)
9491 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009492 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009493 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009495 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009498#ifdef FEAT_RELTIME
9499 if (argvars[6].v_type != VAR_UNKNOWN)
9500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009501 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009504 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +02009506 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009507 }
9508#endif
9509 }
9510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511
9512 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9513 match_pos, lnum_stop, time_limit);
9514
9515theend:
9516 p_ws = save_p_ws;
9517
9518 return retval;
9519}
9520
9521/*
9522 * "searchpair()" function
9523 */
9524 static void
9525f_searchpair(typval_T *argvars, typval_T *rettv)
9526{
9527 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9528}
9529
9530/*
9531 * "searchpairpos()" function
9532 */
9533 static void
9534f_searchpairpos(typval_T *argvars, typval_T *rettv)
9535{
9536 pos_T match_pos;
9537 int lnum = 0;
9538 int col = 0;
9539
9540 if (rettv_list_alloc(rettv) == FAIL)
9541 return;
9542
9543 if (searchpair_cmn(argvars, &match_pos) > 0)
9544 {
9545 lnum = match_pos.lnum;
9546 col = match_pos.col;
9547 }
9548
9549 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9550 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9551}
9552
9553/*
9554 * Search for a start/middle/end thing.
9555 * Used by searchpair(), see its documentation for the details.
9556 * Returns 0 or -1 for no match,
9557 */
9558 long
9559do_searchpair(
9560 char_u *spat, /* start pattern */
9561 char_u *mpat, /* middle pattern */
9562 char_u *epat, /* end pattern */
9563 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009564 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 int flags, /* SP_SETPCMARK and other SP_ values */
9566 pos_T *match_pos,
9567 linenr_T lnum_stop, /* stop at this line if not zero */
9568 long time_limit UNUSED) /* stop after this many msec */
9569{
9570 char_u *save_cpo;
9571 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9572 long retval = 0;
9573 pos_T pos;
9574 pos_T firstpos;
9575 pos_T foundpos;
9576 pos_T save_cursor;
9577 pos_T save_pos;
9578 int n;
9579 int r;
9580 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009581 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 int err;
9583 int options = SEARCH_KEEP;
9584 proftime_T tm;
9585
9586 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9587 save_cpo = p_cpo;
9588 p_cpo = empty_option;
9589
9590#ifdef FEAT_RELTIME
9591 /* Set the time limit, if there is one. */
9592 profile_setlimit(time_limit, &tm);
9593#endif
9594
9595 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9596 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009597 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
9598 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599 if (pat2 == NULL || pat3 == NULL)
9600 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009601 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 if (*mpat == NUL)
9603 STRCPY(pat3, pat2);
9604 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009605 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009606 spat, epat, mpat);
9607 if (flags & SP_START)
9608 options |= SEARCH_START;
9609
Bram Moolenaar48570482017-10-30 21:48:41 +01009610 if (skip != NULL)
9611 {
9612 /* Empty string means to not use the skip expression. */
9613 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9614 use_skip = skip->vval.v_string != NULL
9615 && *skip->vval.v_string != NUL;
9616 }
9617
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 save_cursor = curwin->w_cursor;
9619 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009620 CLEAR_POS(&firstpos);
9621 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622 pat = pat3;
9623 for (;;)
9624 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01009625 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009626 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009627 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 /* didn't find it or found the first match again: FAIL */
9629 break;
9630
9631 if (firstpos.lnum == 0)
9632 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009633 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 {
9635 /* Found the same position again. Can happen with a pattern that
9636 * has "\zs" at the end and searching backwards. Advance one
9637 * character and try again. */
9638 if (dir == BACKWARD)
9639 decl(&pos);
9640 else
9641 incl(&pos);
9642 }
9643 foundpos = pos;
9644
9645 /* clear the start flag to avoid getting stuck here */
9646 options &= ~SEARCH_START;
9647
9648 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009649 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009650 {
9651 save_pos = curwin->w_cursor;
9652 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009653 err = FALSE;
9654 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009655 curwin->w_cursor = save_pos;
9656 if (err)
9657 {
9658 /* Evaluating {skip} caused an error, break here. */
9659 curwin->w_cursor = save_cursor;
9660 retval = -1;
9661 break;
9662 }
9663 if (r)
9664 continue;
9665 }
9666
9667 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9668 {
9669 /* Found end when searching backwards or start when searching
9670 * forward: nested pair. */
9671 ++nest;
9672 pat = pat2; /* nested, don't search for middle */
9673 }
9674 else
9675 {
9676 /* Found end when searching forward or start when searching
9677 * backward: end of (nested) pair; or found middle in outer pair. */
9678 if (--nest == 1)
9679 pat = pat3; /* outer level, search for middle */
9680 }
9681
9682 if (nest == 0)
9683 {
9684 /* Found the match: return matchcount or line number. */
9685 if (flags & SP_RETCOUNT)
9686 ++retval;
9687 else
9688 retval = pos.lnum;
9689 if (flags & SP_SETPCMARK)
9690 setpcmark();
9691 curwin->w_cursor = pos;
9692 if (!(flags & SP_REPEAT))
9693 break;
9694 nest = 1; /* search for next unmatched */
9695 }
9696 }
9697
9698 if (match_pos != NULL)
9699 {
9700 /* Store the match cursor position */
9701 match_pos->lnum = curwin->w_cursor.lnum;
9702 match_pos->col = curwin->w_cursor.col + 1;
9703 }
9704
9705 /* If 'n' flag is used or search failed: restore cursor position. */
9706 if ((flags & SP_NOMOVE) || retval == 0)
9707 curwin->w_cursor = save_cursor;
9708
9709theend:
9710 vim_free(pat2);
9711 vim_free(pat3);
9712 if (p_cpo == empty_option)
9713 p_cpo = save_cpo;
9714 else
9715 /* Darn, evaluating the {skip} expression changed the value. */
9716 free_string_option(save_cpo);
9717
9718 return retval;
9719}
9720
9721/*
9722 * "searchpos()" function
9723 */
9724 static void
9725f_searchpos(typval_T *argvars, typval_T *rettv)
9726{
9727 pos_T match_pos;
9728 int lnum = 0;
9729 int col = 0;
9730 int n;
9731 int flags = 0;
9732
9733 if (rettv_list_alloc(rettv) == FAIL)
9734 return;
9735
9736 n = search_cmn(argvars, &match_pos, &flags);
9737 if (n > 0)
9738 {
9739 lnum = match_pos.lnum;
9740 col = match_pos.col;
9741 }
9742
9743 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9744 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9745 if (flags & SP_SUBPAT)
9746 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9747}
9748
9749 static void
9750f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9751{
9752#ifdef FEAT_CLIENTSERVER
9753 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009754 char_u *server = tv_get_string_chk(&argvars[0]);
9755 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756
9757 rettv->vval.v_number = -1;
9758 if (server == NULL || reply == NULL)
9759 return;
9760 if (check_restricted() || check_secure())
9761 return;
9762# ifdef FEAT_X11
9763 if (check_connection() == FAIL)
9764 return;
9765# endif
9766
9767 if (serverSendReply(server, reply) < 0)
9768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009769 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 return;
9771 }
9772 rettv->vval.v_number = 0;
9773#else
9774 rettv->vval.v_number = -1;
9775#endif
9776}
9777
9778 static void
9779f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9780{
9781 char_u *r = NULL;
9782
9783#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009784# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 r = serverGetVimNames();
9786# else
9787 make_connection();
9788 if (X_DISPLAY != NULL)
9789 r = serverGetVimNames(X_DISPLAY);
9790# endif
9791#endif
9792 rettv->v_type = VAR_STRING;
9793 rettv->vval.v_string = r;
9794}
9795
9796/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009797 * "setbufline()" function
9798 */
9799 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009800f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009801{
9802 linenr_T lnum;
9803 buf_T *buf;
9804
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009805 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009806 if (buf == NULL)
9807 rettv->vval.v_number = 1; /* FAIL */
9808 else
9809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009810 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02009811 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009812 }
9813}
9814
9815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 * "setbufvar()" function
9817 */
9818 static void
9819f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9820{
9821 buf_T *buf;
9822 char_u *varname, *bufvarname;
9823 typval_T *varp;
9824 char_u nbuf[NUMBUFLEN];
9825
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009826 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009828 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
9829 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009830 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 varp = &argvars[2];
9832
9833 if (buf != NULL && varname != NULL && varp != NULL)
9834 {
9835 if (*varname == '&')
9836 {
9837 long numval;
9838 char_u *strval;
9839 int error = FALSE;
9840 aco_save_T aco;
9841
9842 /* set curbuf to be our buf, temporarily */
9843 aucmd_prepbuf(&aco, buf);
9844
9845 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 numval = (long)tv_get_number_chk(varp, &error);
9847 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009848 if (!error && strval != NULL)
9849 set_option_value(varname, numval, strval, OPT_LOCAL);
9850
9851 /* reset notion of buffer */
9852 aucmd_restbuf(&aco);
9853 }
9854 else
9855 {
9856 buf_T *save_curbuf = curbuf;
9857
Bram Moolenaar964b3742019-05-24 18:54:09 +02009858 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859 if (bufvarname != NULL)
9860 {
9861 curbuf = buf;
9862 STRCPY(bufvarname, "b:");
9863 STRCPY(bufvarname + 2, varname);
9864 set_var(bufvarname, varp, TRUE);
9865 vim_free(bufvarname);
9866 curbuf = save_curbuf;
9867 }
9868 }
9869 }
9870}
9871
9872 static void
9873f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9874{
9875 dict_T *d;
9876 dictitem_T *di;
9877 char_u *csearch;
9878
9879 if (argvars[0].v_type != VAR_DICT)
9880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009881 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 return;
9883 }
9884
9885 if ((d = argvars[0].vval.v_dict) != NULL)
9886 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01009887 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888 if (csearch != NULL)
9889 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 if (enc_utf8)
9891 {
9892 int pcc[MAX_MCO];
9893 int c = utfc_ptr2char(csearch, pcc);
9894
9895 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9896 }
9897 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 set_last_csearch(PTR2CHAR(csearch),
9899 csearch, MB_PTR2LEN(csearch));
9900 }
9901
9902 di = dict_find(d, (char_u *)"forward", -1);
9903 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009904 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 ? FORWARD : BACKWARD);
9906
9907 di = dict_find(d, (char_u *)"until", -1);
9908 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009909 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910 }
9911}
9912
9913/*
9914 * "setcmdpos()" function
9915 */
9916 static void
9917f_setcmdpos(typval_T *argvars, typval_T *rettv)
9918{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009919 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920
9921 if (pos >= 0)
9922 rettv->vval.v_number = set_cmdline_pos(pos);
9923}
9924
9925/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02009926 * "setenv()" function
9927 */
9928 static void
9929f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
9930{
9931 char_u namebuf[NUMBUFLEN];
9932 char_u valbuf[NUMBUFLEN];
9933 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
9934
9935 if (argvars[1].v_type == VAR_SPECIAL
9936 && argvars[1].vval.v_number == VVAL_NULL)
9937 vim_unsetenv(name);
9938 else
9939 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
9940}
9941
9942/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 * "setfperm({fname}, {mode})" function
9944 */
9945 static void
9946f_setfperm(typval_T *argvars, typval_T *rettv)
9947{
9948 char_u *fname;
9949 char_u modebuf[NUMBUFLEN];
9950 char_u *mode_str;
9951 int i;
9952 int mask;
9953 int mode = 0;
9954
9955 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009956 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 if (fname == NULL)
9958 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009959 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 if (mode_str == NULL)
9961 return;
9962 if (STRLEN(mode_str) != 9)
9963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009964 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 return;
9966 }
9967
9968 mask = 1;
9969 for (i = 8; i >= 0; --i)
9970 {
9971 if (mode_str[i] != '-')
9972 mode |= mask;
9973 mask = mask << 1;
9974 }
9975 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9976}
9977
9978/*
9979 * "setline()" function
9980 */
9981 static void
9982f_setline(typval_T *argvars, typval_T *rettv)
9983{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009984 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985
Bram Moolenaarca851592018-06-06 21:04:07 +02009986 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987}
9988
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989/*
9990 * Used by "setqflist()" and "setloclist()" functions
9991 */
9992 static void
9993set_qf_ll_list(
9994 win_T *wp UNUSED,
9995 typval_T *list_arg UNUSED,
9996 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009997 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 typval_T *rettv)
9999{
10000#ifdef FEAT_QUICKFIX
10001 static char *e_invact = N_("E927: Invalid action: '%s'");
10002 char_u *act;
10003 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010004 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005#endif
10006
10007 rettv->vval.v_number = -1;
10008
10009#ifdef FEAT_QUICKFIX
10010 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010011 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010012 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010013 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 else
10015 {
10016 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010017 dict_T *d = NULL;
10018 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010019
10020 if (action_arg->v_type == VAR_STRING)
10021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010022 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010023 if (act == NULL)
10024 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010025 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10026 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 action = *act;
10028 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010029 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030 }
10031 else if (action_arg->v_type == VAR_UNKNOWN)
10032 action = ' ';
10033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010034 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010035
Bram Moolenaard823fa92016-08-12 16:29:27 +020010036 if (action_arg->v_type != VAR_UNKNOWN
10037 && what_arg->v_type != VAR_UNKNOWN)
10038 {
10039 if (what_arg->v_type == VAR_DICT)
10040 d = what_arg->vval.v_dict;
10041 else
10042 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010043 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020010044 valid_dict = FALSE;
10045 }
10046 }
10047
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010048 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010049 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010050 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10051 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010052 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010053 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 }
10055#endif
10056}
10057
10058/*
10059 * "setloclist()" function
10060 */
10061 static void
10062f_setloclist(typval_T *argvars, typval_T *rettv)
10063{
10064 win_T *win;
10065
10066 rettv->vval.v_number = -1;
10067
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020010068 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010069 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010070 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010071}
10072
10073/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010074 * "setpos()" function
10075 */
10076 static void
10077f_setpos(typval_T *argvars, typval_T *rettv)
10078{
10079 pos_T pos;
10080 int fnum;
10081 char_u *name;
10082 colnr_T curswant = -1;
10083
10084 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010085 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 if (name != NULL)
10087 {
10088 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10089 {
10090 if (--pos.col < 0)
10091 pos.col = 0;
10092 if (name[0] == '.' && name[1] == NUL)
10093 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010094 /* set cursor; "fnum" is ignored */
10095 curwin->w_cursor = pos;
10096 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010097 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010098 curwin->w_curswant = curswant - 1;
10099 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010100 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010101 check_cursor();
10102 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010103 }
10104 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10105 {
10106 /* set mark */
10107 if (setmark_pos(name[1], &pos, fnum) == OK)
10108 rettv->vval.v_number = 0;
10109 }
10110 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010111 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010112 }
10113 }
10114}
10115
10116/*
10117 * "setqflist()" function
10118 */
10119 static void
10120f_setqflist(typval_T *argvars, typval_T *rettv)
10121{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010122 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123}
10124
10125/*
10126 * "setreg()" function
10127 */
10128 static void
10129f_setreg(typval_T *argvars, typval_T *rettv)
10130{
10131 int regname;
10132 char_u *strregname;
10133 char_u *stropt;
10134 char_u *strval;
10135 int append;
10136 char_u yank_type;
10137 long block_len;
10138
10139 block_len = -1;
10140 yank_type = MAUTO;
10141 append = FALSE;
10142
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010143 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144 rettv->vval.v_number = 1; /* FAIL is default */
10145
10146 if (strregname == NULL)
10147 return; /* type error; errmsg already given */
10148 regname = *strregname;
10149 if (regname == 0 || regname == '@')
10150 regname = '"';
10151
10152 if (argvars[2].v_type != VAR_UNKNOWN)
10153 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010154 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155 if (stropt == NULL)
10156 return; /* type error */
10157 for (; *stropt != NUL; ++stropt)
10158 switch (*stropt)
10159 {
10160 case 'a': case 'A': /* append */
10161 append = TRUE;
10162 break;
10163 case 'v': case 'c': /* character-wise selection */
10164 yank_type = MCHAR;
10165 break;
10166 case 'V': case 'l': /* line-wise selection */
10167 yank_type = MLINE;
10168 break;
10169 case 'b': case Ctrl_V: /* block-wise selection */
10170 yank_type = MBLOCK;
10171 if (VIM_ISDIGIT(stropt[1]))
10172 {
10173 ++stropt;
10174 block_len = getdigits(&stropt) - 1;
10175 --stropt;
10176 }
10177 break;
10178 }
10179 }
10180
10181 if (argvars[1].v_type == VAR_LIST)
10182 {
10183 char_u **lstval;
10184 char_u **allocval;
10185 char_u buf[NUMBUFLEN];
10186 char_u **curval;
10187 char_u **curallocval;
10188 list_T *ll = argvars[1].vval.v_list;
10189 listitem_T *li;
10190 int len;
10191
10192 /* If the list is NULL handle like an empty list. */
10193 len = ll == NULL ? 0 : ll->lv_len;
10194
10195 /* First half: use for pointers to result lines; second half: use for
10196 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010197 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 if (lstval == NULL)
10199 return;
10200 curval = lstval;
10201 allocval = lstval + len + 2;
10202 curallocval = allocval;
10203
10204 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10205 li = li->li_next)
10206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010207 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 if (strval == NULL)
10209 goto free_lstval;
10210 if (strval == buf)
10211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010212 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010213 * overwrite the string. */
10214 strval = vim_strsave(buf);
10215 if (strval == NULL)
10216 goto free_lstval;
10217 *curallocval++ = strval;
10218 }
10219 *curval++ = strval;
10220 }
10221 *curval++ = NULL;
10222
10223 write_reg_contents_lst(regname, lstval, -1,
10224 append, yank_type, block_len);
10225free_lstval:
10226 while (curallocval > allocval)
10227 vim_free(*--curallocval);
10228 vim_free(lstval);
10229 }
10230 else
10231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010232 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233 if (strval == NULL)
10234 return;
10235 write_reg_contents_ex(regname, strval, -1,
10236 append, yank_type, block_len);
10237 }
10238 rettv->vval.v_number = 0;
10239}
10240
10241/*
10242 * "settabvar()" function
10243 */
10244 static void
10245f_settabvar(typval_T *argvars, typval_T *rettv)
10246{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 tabpage_T *save_curtab;
10248 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 char_u *varname, *tabvarname;
10250 typval_T *varp;
10251
10252 rettv->vval.v_number = 0;
10253
Bram Moolenaar8c62a082019-02-08 14:34:10 +010010254 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 return;
10256
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010257 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
10258 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010259 varp = &argvars[2];
10260
Bram Moolenaar4033c552017-09-16 20:54:51 +020010261 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 save_curtab = curtab;
10264 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265
Bram Moolenaar964b3742019-05-24 18:54:09 +020010266 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010267 if (tabvarname != NULL)
10268 {
10269 STRCPY(tabvarname, "t:");
10270 STRCPY(tabvarname + 2, varname);
10271 set_var(tabvarname, varp, TRUE);
10272 vim_free(tabvarname);
10273 }
10274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275 /* Restore current tabpage */
10276 if (valid_tabpage(save_curtab))
10277 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278 }
10279}
10280
10281/*
10282 * "settabwinvar()" function
10283 */
10284 static void
10285f_settabwinvar(typval_T *argvars, typval_T *rettv)
10286{
10287 setwinvar(argvars, rettv, 1);
10288}
10289
10290/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010291 * "settagstack()" function
10292 */
10293 static void
10294f_settagstack(typval_T *argvars, typval_T *rettv)
10295{
10296 static char *e_invact2 = N_("E962: Invalid action: '%s'");
10297 win_T *wp;
10298 dict_T *d;
10299 int action = 'r';
10300
10301 rettv->vval.v_number = -1;
10302
10303 // first argument: window number or id
10304 wp = find_win_by_nr_or_id(&argvars[0]);
10305 if (wp == NULL)
10306 return;
10307
10308 // second argument: dict with items to set in the tag stack
10309 if (argvars[1].v_type != VAR_DICT)
10310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010311 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010312 return;
10313 }
10314 d = argvars[1].vval.v_dict;
10315 if (d == NULL)
10316 return;
10317
10318 // third argument: action - 'a' for append and 'r' for replace.
10319 // default is to replace the stack.
10320 if (argvars[2].v_type == VAR_UNKNOWN)
10321 action = 'r';
10322 else if (argvars[2].v_type == VAR_STRING)
10323 {
10324 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010325 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010326 if (actstr == NULL)
10327 return;
10328 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
10329 action = *actstr;
10330 else
10331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010332 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010333 return;
10334 }
10335 }
10336 else
10337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010338 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010010339 return;
10340 }
10341
10342 if (set_tagstack(wp, d, action) == OK)
10343 rettv->vval.v_number = 0;
10344}
10345
10346/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010347 * "setwinvar()" function
10348 */
10349 static void
10350f_setwinvar(typval_T *argvars, typval_T *rettv)
10351{
10352 setwinvar(argvars, rettv, 0);
10353}
10354
10355#ifdef FEAT_CRYPT
10356/*
10357 * "sha256({string})" function
10358 */
10359 static void
10360f_sha256(typval_T *argvars, typval_T *rettv)
10361{
10362 char_u *p;
10363
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010364 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010365 rettv->vval.v_string = vim_strsave(
10366 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10367 rettv->v_type = VAR_STRING;
10368}
10369#endif /* FEAT_CRYPT */
10370
10371/*
10372 * "shellescape({string})" function
10373 */
10374 static void
10375f_shellescape(typval_T *argvars, typval_T *rettv)
10376{
Bram Moolenaar20615522017-06-05 18:46:26 +020010377 int do_special = non_zero_arg(&argvars[1]);
10378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010379 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010380 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010381 rettv->v_type = VAR_STRING;
10382}
10383
10384/*
10385 * shiftwidth() function
10386 */
10387 static void
10388f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10389{
Bram Moolenaarf9514162018-11-22 03:08:29 +010010390 rettv->vval.v_number = 0;
10391
10392 if (argvars[0].v_type != VAR_UNKNOWN)
10393 {
10394 long col;
10395
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010396 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010010397 if (col < 0)
10398 return; // type error; errmsg already given
10399#ifdef FEAT_VARTABS
10400 rettv->vval.v_number = get_sw_value_col(curbuf, col);
10401 return;
10402#endif
10403 }
10404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 rettv->vval.v_number = get_sw_value(curbuf);
10406}
10407
10408/*
10409 * "simplify()" function
10410 */
10411 static void
10412f_simplify(typval_T *argvars, typval_T *rettv)
10413{
10414 char_u *p;
10415
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010416 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010417 rettv->vval.v_string = vim_strsave(p);
10418 simplify_filename(rettv->vval.v_string); /* simplify in place */
10419 rettv->v_type = VAR_STRING;
10420}
10421
10422#ifdef FEAT_FLOAT
10423/*
10424 * "sin()" function
10425 */
10426 static void
10427f_sin(typval_T *argvars, typval_T *rettv)
10428{
10429 float_T f = 0.0;
10430
10431 rettv->v_type = VAR_FLOAT;
10432 if (get_float_arg(argvars, &f) == OK)
10433 rettv->vval.v_float = sin(f);
10434 else
10435 rettv->vval.v_float = 0.0;
10436}
10437
10438/*
10439 * "sinh()" function
10440 */
10441 static void
10442f_sinh(typval_T *argvars, typval_T *rettv)
10443{
10444 float_T f = 0.0;
10445
10446 rettv->v_type = VAR_FLOAT;
10447 if (get_float_arg(argvars, &f) == OK)
10448 rettv->vval.v_float = sinh(f);
10449 else
10450 rettv->vval.v_float = 0.0;
10451}
10452#endif
10453
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010454/*
10455 * "soundfold({word})" function
10456 */
10457 static void
10458f_soundfold(typval_T *argvars, typval_T *rettv)
10459{
10460 char_u *s;
10461
10462 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010463 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010464#ifdef FEAT_SPELL
10465 rettv->vval.v_string = eval_soundfold(s);
10466#else
10467 rettv->vval.v_string = vim_strsave(s);
10468#endif
10469}
10470
10471/*
10472 * "spellbadword()" function
10473 */
10474 static void
10475f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10476{
10477 char_u *word = (char_u *)"";
10478 hlf_T attr = HLF_COUNT;
10479 int len = 0;
10480
10481 if (rettv_list_alloc(rettv) == FAIL)
10482 return;
10483
10484#ifdef FEAT_SPELL
10485 if (argvars[0].v_type == VAR_UNKNOWN)
10486 {
10487 /* Find the start and length of the badly spelled word. */
10488 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10489 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010490 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010491 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010010492 curwin->w_set_curswant = TRUE;
10493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010494 }
10495 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10496 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010497 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010498 int capcol = -1;
10499
10500 if (str != NULL)
10501 {
10502 /* Check the argument for spelling. */
10503 while (*str != NUL)
10504 {
10505 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10506 if (attr != HLF_COUNT)
10507 {
10508 word = str;
10509 break;
10510 }
10511 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020010512 capcol -= len;
Bram Moolenaar0c779e82019-08-09 17:01:02 +020010513 len = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010514 }
10515 }
10516 }
10517#endif
10518
10519 list_append_string(rettv->vval.v_list, word, len);
10520 list_append_string(rettv->vval.v_list, (char_u *)(
10521 attr == HLF_SPB ? "bad" :
10522 attr == HLF_SPR ? "rare" :
10523 attr == HLF_SPL ? "local" :
10524 attr == HLF_SPC ? "caps" :
10525 ""), -1);
10526}
10527
10528/*
10529 * "spellsuggest()" function
10530 */
10531 static void
10532f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10533{
10534#ifdef FEAT_SPELL
10535 char_u *str;
10536 int typeerr = FALSE;
10537 int maxcount;
10538 garray_T ga;
10539 int i;
10540 listitem_T *li;
10541 int need_capital = FALSE;
10542#endif
10543
10544 if (rettv_list_alloc(rettv) == FAIL)
10545 return;
10546
10547#ifdef FEAT_SPELL
10548 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010550 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010551 if (argvars[1].v_type != VAR_UNKNOWN)
10552 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010553 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010554 if (maxcount <= 0)
10555 return;
10556 if (argvars[2].v_type != VAR_UNKNOWN)
10557 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010558 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010559 if (typeerr)
10560 return;
10561 }
10562 }
10563 else
10564 maxcount = 25;
10565
10566 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10567
10568 for (i = 0; i < ga.ga_len; ++i)
10569 {
10570 str = ((char_u **)ga.ga_data)[i];
10571
10572 li = listitem_alloc();
10573 if (li == NULL)
10574 vim_free(str);
10575 else
10576 {
10577 li->li_tv.v_type = VAR_STRING;
10578 li->li_tv.v_lock = 0;
10579 li->li_tv.vval.v_string = str;
10580 list_append(rettv->vval.v_list, li);
10581 }
10582 }
10583 ga_clear(&ga);
10584 }
10585#endif
10586}
10587
10588 static void
10589f_split(typval_T *argvars, typval_T *rettv)
10590{
10591 char_u *str;
10592 char_u *end;
10593 char_u *pat = NULL;
10594 regmatch_T regmatch;
10595 char_u patbuf[NUMBUFLEN];
10596 char_u *save_cpo;
10597 int match;
10598 colnr_T col = 0;
10599 int keepempty = FALSE;
10600 int typeerr = FALSE;
10601
10602 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10603 save_cpo = p_cpo;
10604 p_cpo = (char_u *)"";
10605
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010606 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607 if (argvars[1].v_type != VAR_UNKNOWN)
10608 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010609 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 if (pat == NULL)
10611 typeerr = TRUE;
10612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010613 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614 }
10615 if (pat == NULL || *pat == NUL)
10616 pat = (char_u *)"[\\x01- ]\\+";
10617
10618 if (rettv_list_alloc(rettv) == FAIL)
10619 return;
10620 if (typeerr)
10621 return;
10622
10623 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10624 if (regmatch.regprog != NULL)
10625 {
10626 regmatch.rm_ic = FALSE;
10627 while (*str != NUL || keepempty)
10628 {
10629 if (*str == NUL)
10630 match = FALSE; /* empty item at the end */
10631 else
10632 match = vim_regexec_nl(&regmatch, str, col);
10633 if (match)
10634 end = regmatch.startp[0];
10635 else
10636 end = str + STRLEN(str);
10637 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
10638 && *str != NUL && match && end < regmatch.endp[0]))
10639 {
10640 if (list_append_string(rettv->vval.v_list, str,
10641 (int)(end - str)) == FAIL)
10642 break;
10643 }
10644 if (!match)
10645 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010010646 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010647 if (regmatch.endp[0] > str)
10648 col = 0;
10649 else
Bram Moolenaar13505972019-01-24 15:04:48 +010010650 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 str = regmatch.endp[0];
10653 }
10654
10655 vim_regfree(regmatch.regprog);
10656 }
10657
10658 p_cpo = save_cpo;
10659}
10660
10661#ifdef FEAT_FLOAT
10662/*
10663 * "sqrt()" function
10664 */
10665 static void
10666f_sqrt(typval_T *argvars, typval_T *rettv)
10667{
10668 float_T f = 0.0;
10669
10670 rettv->v_type = VAR_FLOAT;
10671 if (get_float_arg(argvars, &f) == OK)
10672 rettv->vval.v_float = sqrt(f);
10673 else
10674 rettv->vval.v_float = 0.0;
10675}
10676
10677/*
10678 * "str2float()" function
10679 */
10680 static void
10681f_str2float(typval_T *argvars, typval_T *rettv)
10682{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010683 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010684 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685
Bram Moolenaar08243d22017-01-10 16:12:29 +010010686 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 p = skipwhite(p + 1);
10688 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010010689 if (isneg)
10690 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 rettv->v_type = VAR_FLOAT;
10692}
10693#endif
10694
10695/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020010696 * "str2list()" function
10697 */
10698 static void
10699f_str2list(typval_T *argvars, typval_T *rettv)
10700{
10701 char_u *p;
10702 int utf8 = FALSE;
10703
10704 if (rettv_list_alloc(rettv) == FAIL)
10705 return;
10706
10707 if (argvars[1].v_type != VAR_UNKNOWN)
10708 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
10709
10710 p = tv_get_string(&argvars[0]);
10711
10712 if (has_mbyte || utf8)
10713 {
10714 int (*ptr2len)(char_u *);
10715 int (*ptr2char)(char_u *);
10716
10717 if (utf8 || enc_utf8)
10718 {
10719 ptr2len = utf_ptr2len;
10720 ptr2char = utf_ptr2char;
10721 }
10722 else
10723 {
10724 ptr2len = mb_ptr2len;
10725 ptr2char = mb_ptr2char;
10726 }
10727
10728 for ( ; *p != NUL; p += (*ptr2len)(p))
10729 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
10730 }
10731 else
10732 for ( ; *p != NUL; ++p)
10733 list_append_number(rettv->vval.v_list, *p);
10734}
10735
10736/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 * "str2nr()" function
10738 */
10739 static void
10740f_str2nr(typval_T *argvars, typval_T *rettv)
10741{
10742 int base = 10;
10743 char_u *p;
10744 varnumber_T n;
10745 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010010746 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747
10748 if (argvars[1].v_type != VAR_UNKNOWN)
10749 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010750 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 if (base != 2 && base != 8 && base != 10 && base != 16)
10752 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010753 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754 return;
10755 }
10756 }
10757
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010758 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010010759 isneg = (*p == '-');
10760 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761 p = skipwhite(p + 1);
10762 switch (base)
10763 {
10764 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
10765 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
10766 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
10767 default: what = 0;
10768 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020010769 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
10770 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010010771 if (isneg)
10772 rettv->vval.v_number = -n;
10773 else
10774 rettv->vval.v_number = n;
10775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776}
10777
10778#ifdef HAVE_STRFTIME
10779/*
10780 * "strftime({format}[, {time}])" function
10781 */
10782 static void
10783f_strftime(typval_T *argvars, typval_T *rettv)
10784{
10785 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020010786 struct tm tmval;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010787 struct tm *curtime;
10788 time_t seconds;
10789 char_u *p;
10790
10791 rettv->v_type = VAR_STRING;
10792
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010793 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 if (argvars[1].v_type == VAR_UNKNOWN)
10795 seconds = time(NULL);
10796 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010797 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaardb517302019-06-18 22:53:24 +020010798 curtime = vim_localtime(&seconds, &tmval);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010799 /* MSVC returns NULL for an invalid value of seconds. */
10800 if (curtime == NULL)
10801 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
10802 else
10803 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 vimconv_T conv;
10805 char_u *enc;
10806
10807 conv.vc_type = CONV_NONE;
10808 enc = enc_locale();
10809 convert_setup(&conv, p_enc, enc);
10810 if (conv.vc_type != CONV_NONE)
10811 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 if (p != NULL)
10813 (void)strftime((char *)result_buf, sizeof(result_buf),
10814 (char *)p, curtime);
10815 else
10816 result_buf[0] = NUL;
10817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 if (conv.vc_type != CONV_NONE)
10819 vim_free(p);
10820 convert_setup(&conv, enc, p_enc);
10821 if (conv.vc_type != CONV_NONE)
10822 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
10823 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010824 rettv->vval.v_string = vim_strsave(result_buf);
10825
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826 /* Release conversion descriptors */
10827 convert_setup(&conv, NULL, NULL);
10828 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010829 }
10830}
10831#endif
10832
10833/*
10834 * "strgetchar()" function
10835 */
10836 static void
10837f_strgetchar(typval_T *argvars, typval_T *rettv)
10838{
10839 char_u *str;
10840 int len;
10841 int error = FALSE;
10842 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010010843 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844
10845 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010846 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010847 if (str == NULL)
10848 return;
10849 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010850 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 if (error)
10852 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010853
Bram Moolenaar13505972019-01-24 15:04:48 +010010854 while (charidx >= 0 && byteidx < len)
10855 {
10856 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010857 {
Bram Moolenaar13505972019-01-24 15:04:48 +010010858 rettv->vval.v_number = mb_ptr2char(str + byteidx);
10859 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 }
Bram Moolenaar13505972019-01-24 15:04:48 +010010861 --charidx;
10862 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010863 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864}
10865
10866/*
10867 * "stridx()" function
10868 */
10869 static void
10870f_stridx(typval_T *argvars, typval_T *rettv)
10871{
10872 char_u buf[NUMBUFLEN];
10873 char_u *needle;
10874 char_u *haystack;
10875 char_u *save_haystack;
10876 char_u *pos;
10877 int start_idx;
10878
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010879 needle = tv_get_string_chk(&argvars[1]);
10880 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010881 rettv->vval.v_number = -1;
10882 if (needle == NULL || haystack == NULL)
10883 return; /* type error; errmsg already given */
10884
10885 if (argvars[2].v_type != VAR_UNKNOWN)
10886 {
10887 int error = FALSE;
10888
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010889 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 if (error || start_idx >= (int)STRLEN(haystack))
10891 return;
10892 if (start_idx >= 0)
10893 haystack += start_idx;
10894 }
10895
10896 pos = (char_u *)strstr((char *)haystack, (char *)needle);
10897 if (pos != NULL)
10898 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
10899}
10900
10901/*
10902 * "string()" function
10903 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010010904 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905f_string(typval_T *argvars, typval_T *rettv)
10906{
10907 char_u *tofree;
10908 char_u numbuf[NUMBUFLEN];
10909
10910 rettv->v_type = VAR_STRING;
10911 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
10912 get_copyID());
10913 /* Make a copy if we have a value but it's not in allocated memory. */
10914 if (rettv->vval.v_string != NULL && tofree == NULL)
10915 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
10916}
10917
10918/*
10919 * "strlen()" function
10920 */
10921 static void
10922f_strlen(typval_T *argvars, typval_T *rettv)
10923{
10924 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010925 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010926}
10927
10928/*
10929 * "strchars()" function
10930 */
10931 static void
10932f_strchars(typval_T *argvars, typval_T *rettv)
10933{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010934 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010936 varnumber_T len = 0;
10937 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010938
10939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010940 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010942 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010943 else
10944 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010945 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
10946 while (*s != NUL)
10947 {
10948 func_mb_ptr2char_adv(&s);
10949 ++len;
10950 }
10951 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010952 }
10953}
10954
10955/*
10956 * "strdisplaywidth()" function
10957 */
10958 static void
10959f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
10960{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010961 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010962 int col = 0;
10963
10964 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010965 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010966
10967 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
10968}
10969
10970/*
10971 * "strwidth()" function
10972 */
10973 static void
10974f_strwidth(typval_T *argvars, typval_T *rettv)
10975{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977
Bram Moolenaar13505972019-01-24 15:04:48 +010010978 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979}
10980
10981/*
10982 * "strcharpart()" function
10983 */
10984 static void
10985f_strcharpart(typval_T *argvars, typval_T *rettv)
10986{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010987 char_u *p;
10988 int nchar;
10989 int nbyte = 0;
10990 int charlen;
10991 int len = 0;
10992 int slen;
10993 int error = FALSE;
10994
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010995 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 slen = (int)STRLEN(p);
10997
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010998 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 if (!error)
11000 {
11001 if (nchar > 0)
11002 while (nchar > 0 && nbyte < slen)
11003 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011004 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 --nchar;
11006 }
11007 else
11008 nbyte = nchar;
11009 if (argvars[2].v_type != VAR_UNKNOWN)
11010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011011 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012 while (charlen > 0 && nbyte + len < slen)
11013 {
11014 int off = nbyte + len;
11015
11016 if (off < 0)
11017 len += 1;
11018 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011019 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 --charlen;
11021 }
11022 }
11023 else
11024 len = slen - nbyte; /* default: all bytes that are available. */
11025 }
11026
11027 /*
11028 * Only return the overlap between the specified part and the actual
11029 * string.
11030 */
11031 if (nbyte < 0)
11032 {
11033 len += nbyte;
11034 nbyte = 0;
11035 }
11036 else if (nbyte > slen)
11037 nbyte = slen;
11038 if (len < 0)
11039 len = 0;
11040 else if (nbyte + len > slen)
11041 len = slen - nbyte;
11042
11043 rettv->v_type = VAR_STRING;
11044 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045}
11046
11047/*
11048 * "strpart()" function
11049 */
11050 static void
11051f_strpart(typval_T *argvars, typval_T *rettv)
11052{
11053 char_u *p;
11054 int n;
11055 int len;
11056 int slen;
11057 int error = FALSE;
11058
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011059 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 slen = (int)STRLEN(p);
11061
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011062 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 if (error)
11064 len = 0;
11065 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011066 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011067 else
11068 len = slen - n; /* default len: all bytes that are available. */
11069
11070 /*
11071 * Only return the overlap between the specified part and the actual
11072 * string.
11073 */
11074 if (n < 0)
11075 {
11076 len += n;
11077 n = 0;
11078 }
11079 else if (n > slen)
11080 n = slen;
11081 if (len < 0)
11082 len = 0;
11083 else if (n + len > slen)
11084 len = slen - n;
11085
11086 rettv->v_type = VAR_STRING;
11087 rettv->vval.v_string = vim_strnsave(p + n, len);
11088}
11089
11090/*
11091 * "strridx()" function
11092 */
11093 static void
11094f_strridx(typval_T *argvars, typval_T *rettv)
11095{
11096 char_u buf[NUMBUFLEN];
11097 char_u *needle;
11098 char_u *haystack;
11099 char_u *rest;
11100 char_u *lastmatch = NULL;
11101 int haystack_len, end_idx;
11102
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011103 needle = tv_get_string_chk(&argvars[1]);
11104 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105
11106 rettv->vval.v_number = -1;
11107 if (needle == NULL || haystack == NULL)
11108 return; /* type error; errmsg already given */
11109
11110 haystack_len = (int)STRLEN(haystack);
11111 if (argvars[2].v_type != VAR_UNKNOWN)
11112 {
11113 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011114 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 if (end_idx < 0)
11116 return; /* can never find a match */
11117 }
11118 else
11119 end_idx = haystack_len;
11120
11121 if (*needle == NUL)
11122 {
11123 /* Empty string matches past the end. */
11124 lastmatch = haystack + end_idx;
11125 }
11126 else
11127 {
11128 for (rest = haystack; *rest != '\0'; ++rest)
11129 {
11130 rest = (char_u *)strstr((char *)rest, (char *)needle);
11131 if (rest == NULL || rest > haystack + end_idx)
11132 break;
11133 lastmatch = rest;
11134 }
11135 }
11136
11137 if (lastmatch == NULL)
11138 rettv->vval.v_number = -1;
11139 else
11140 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11141}
11142
11143/*
11144 * "strtrans()" function
11145 */
11146 static void
11147f_strtrans(typval_T *argvars, typval_T *rettv)
11148{
11149 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011150 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011151}
11152
11153/*
11154 * "submatch()" function
11155 */
11156 static void
11157f_submatch(typval_T *argvars, typval_T *rettv)
11158{
11159 int error = FALSE;
11160 int no;
11161 int retList = 0;
11162
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011163 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 if (error)
11165 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011166 if (no < 0 || no >= NSUBEXP)
11167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011168 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010011169 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011170 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011172 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 if (error)
11174 return;
11175
11176 if (retList == 0)
11177 {
11178 rettv->v_type = VAR_STRING;
11179 rettv->vval.v_string = reg_submatch(no);
11180 }
11181 else
11182 {
11183 rettv->v_type = VAR_LIST;
11184 rettv->vval.v_list = reg_submatch_list(no);
11185 }
11186}
11187
11188/*
11189 * "substitute()" function
11190 */
11191 static void
11192f_substitute(typval_T *argvars, typval_T *rettv)
11193{
11194 char_u patbuf[NUMBUFLEN];
11195 char_u subbuf[NUMBUFLEN];
11196 char_u flagsbuf[NUMBUFLEN];
11197
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011198 char_u *str = tv_get_string_chk(&argvars[0]);
11199 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011200 char_u *sub = NULL;
11201 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011202 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011203
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011204 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11205 expr = &argvars[2];
11206 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011207 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011208
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011209 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011210 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11211 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011212 rettv->vval.v_string = NULL;
11213 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011214 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011215}
11216
11217/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011218 * "swapinfo(swap_filename)" function
11219 */
11220 static void
11221f_swapinfo(typval_T *argvars, typval_T *rettv)
11222{
11223 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011224 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020011225}
11226
11227/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020011228 * "swapname(expr)" function
11229 */
11230 static void
11231f_swapname(typval_T *argvars, typval_T *rettv)
11232{
11233 buf_T *buf;
11234
11235 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011236 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020011237 if (buf == NULL || buf->b_ml.ml_mfp == NULL
11238 || buf->b_ml.ml_mfp->mf_fname == NULL)
11239 rettv->vval.v_string = NULL;
11240 else
11241 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
11242}
11243
11244/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011245 * "synID(lnum, col, trans)" function
11246 */
11247 static void
11248f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11249{
11250 int id = 0;
11251#ifdef FEAT_SYN_HL
11252 linenr_T lnum;
11253 colnr_T col;
11254 int trans;
11255 int transerr = FALSE;
11256
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011257 lnum = tv_get_lnum(argvars); /* -1 on type error */
11258 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
11259 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011260
11261 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11262 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11263 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11264#endif
11265
11266 rettv->vval.v_number = id;
11267}
11268
11269/*
11270 * "synIDattr(id, what [, mode])" function
11271 */
11272 static void
11273f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11274{
11275 char_u *p = NULL;
11276#ifdef FEAT_SYN_HL
11277 int id;
11278 char_u *what;
11279 char_u *mode;
11280 char_u modebuf[NUMBUFLEN];
11281 int modec;
11282
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011283 id = (int)tv_get_number(&argvars[0]);
11284 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011285 if (argvars[2].v_type != VAR_UNKNOWN)
11286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011287 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011288 modec = TOLOWER_ASC(mode[0]);
11289 if (modec != 't' && modec != 'c' && modec != 'g')
11290 modec = 0; /* replace invalid with current */
11291 }
11292 else
11293 {
11294#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11295 if (USE_24BIT)
11296 modec = 'g';
11297 else
11298#endif
11299 if (t_colors > 1)
11300 modec = 'c';
11301 else
11302 modec = 't';
11303 }
11304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 switch (TOLOWER_ASC(what[0]))
11306 {
11307 case 'b':
11308 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11309 p = highlight_color(id, what, modec);
11310 else /* bold */
11311 p = highlight_has_attr(id, HL_BOLD, modec);
11312 break;
11313
11314 case 'f': /* fg[#] or font */
11315 p = highlight_color(id, what, modec);
11316 break;
11317
11318 case 'i':
11319 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11320 p = highlight_has_attr(id, HL_INVERSE, modec);
11321 else /* italic */
11322 p = highlight_has_attr(id, HL_ITALIC, modec);
11323 break;
11324
11325 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011326 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 break;
11328
11329 case 'r': /* reverse */
11330 p = highlight_has_attr(id, HL_INVERSE, modec);
11331 break;
11332
11333 case 's':
11334 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11335 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011336 /* strikeout */
11337 else if (TOLOWER_ASC(what[1]) == 't' &&
11338 TOLOWER_ASC(what[2]) == 'r')
11339 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 else /* standout */
11341 p = highlight_has_attr(id, HL_STANDOUT, modec);
11342 break;
11343
11344 case 'u':
11345 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11346 /* underline */
11347 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11348 else
11349 /* undercurl */
11350 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11351 break;
11352 }
11353
11354 if (p != NULL)
11355 p = vim_strsave(p);
11356#endif
11357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = p;
11359}
11360
11361/*
11362 * "synIDtrans(id)" function
11363 */
11364 static void
11365f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11366{
11367 int id;
11368
11369#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011370 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011371
11372 if (id > 0)
11373 id = syn_get_final_id(id);
11374 else
11375#endif
11376 id = 0;
11377
11378 rettv->vval.v_number = id;
11379}
11380
11381/*
11382 * "synconcealed(lnum, col)" function
11383 */
11384 static void
11385f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11386{
11387#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11388 linenr_T lnum;
11389 colnr_T col;
11390 int syntax_flags = 0;
11391 int cchar;
11392 int matchid = 0;
11393 char_u str[NUMBUFLEN];
11394#endif
11395
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011396 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397
11398#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011399 lnum = tv_get_lnum(argvars); /* -1 on type error */
11400 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401
11402 vim_memset(str, NUL, sizeof(str));
11403
11404 if (rettv_list_alloc(rettv) != FAIL)
11405 {
11406 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11407 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11408 && curwin->w_p_cole > 0)
11409 {
11410 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11411 syntax_flags = get_syntax_info(&matchid);
11412
11413 /* get the conceal character */
11414 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11415 {
11416 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011417 if (cchar == NUL && curwin->w_p_cole == 1)
11418 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011419 if (cchar != NUL)
11420 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011421 if (has_mbyte)
11422 (*mb_char2bytes)(cchar, str);
11423 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011424 str[0] = cchar;
11425 }
11426 }
11427 }
11428
11429 list_append_number(rettv->vval.v_list,
11430 (syntax_flags & HL_CONCEAL) != 0);
11431 /* -1 to auto-determine strlen */
11432 list_append_string(rettv->vval.v_list, str, -1);
11433 list_append_number(rettv->vval.v_list, matchid);
11434 }
11435#endif
11436}
11437
11438/*
11439 * "synstack(lnum, col)" function
11440 */
11441 static void
11442f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11443{
11444#ifdef FEAT_SYN_HL
11445 linenr_T lnum;
11446 colnr_T col;
11447 int i;
11448 int id;
11449#endif
11450
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011451 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011452
11453#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011454 lnum = tv_get_lnum(argvars); /* -1 on type error */
11455 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011456
11457 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11458 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11459 && rettv_list_alloc(rettv) != FAIL)
11460 {
11461 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11462 for (i = 0; ; ++i)
11463 {
11464 id = syn_get_stack_item(i);
11465 if (id < 0)
11466 break;
11467 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11468 break;
11469 }
11470 }
11471#endif
11472}
11473
11474 static void
11475get_cmd_output_as_rettv(
11476 typval_T *argvars,
11477 typval_T *rettv,
11478 int retlist)
11479{
11480 char_u *res = NULL;
11481 char_u *p;
11482 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011483 int err = FALSE;
11484 FILE *fd;
11485 list_T *list = NULL;
11486 int flags = SHELL_SILENT;
11487
11488 rettv->v_type = VAR_STRING;
11489 rettv->vval.v_string = NULL;
11490 if (check_restricted() || check_secure())
11491 goto errret;
11492
11493 if (argvars[1].v_type != VAR_UNKNOWN)
11494 {
11495 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011496 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 * command.
11498 */
11499 if ((infile = vim_tempname('i', TRUE)) == NULL)
11500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011501 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 goto errret;
11503 }
11504
11505 fd = mch_fopen((char *)infile, WRITEBIN);
11506 if (fd == NULL)
11507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011508 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 goto errret;
11510 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011511 if (argvars[1].v_type == VAR_NUMBER)
11512 {
11513 linenr_T lnum;
11514 buf_T *buf;
11515
11516 buf = buflist_findnr(argvars[1].vval.v_number);
11517 if (buf == NULL)
11518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011519 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011520 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011521 goto errret;
11522 }
11523
11524 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11525 {
11526 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11527 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11528 {
11529 err = TRUE;
11530 break;
11531 }
11532 if (putc(NL, fd) == EOF)
11533 {
11534 err = TRUE;
11535 break;
11536 }
11537 }
11538 }
11539 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540 {
11541 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11542 err = TRUE;
11543 }
11544 else
11545 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011546 size_t len;
11547 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011549 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550 if (p == NULL)
11551 {
11552 fclose(fd);
11553 goto errret; /* type error; errmsg already given */
11554 }
11555 len = STRLEN(p);
11556 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11557 err = TRUE;
11558 }
11559 if (fclose(fd) != 0)
11560 err = TRUE;
11561 if (err)
11562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011563 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564 goto errret;
11565 }
11566 }
11567
11568 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11569 * echoes typeahead, that messes up the display. */
11570 if (!msg_silent)
11571 flags += SHELL_COOKED;
11572
11573 if (retlist)
11574 {
11575 int len;
11576 listitem_T *li;
11577 char_u *s = NULL;
11578 char_u *start;
11579 char_u *end;
11580 int i;
11581
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011582 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 if (res == NULL)
11584 goto errret;
11585
11586 list = list_alloc();
11587 if (list == NULL)
11588 goto errret;
11589
11590 for (i = 0; i < len; ++i)
11591 {
11592 start = res + i;
11593 while (i < len && res[i] != NL)
11594 ++i;
11595 end = res + i;
11596
Bram Moolenaar964b3742019-05-24 18:54:09 +020011597 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011598 if (s == NULL)
11599 goto errret;
11600
11601 for (p = s; start < end; ++p, ++start)
11602 *p = *start == NUL ? NL : *start;
11603 *p = NUL;
11604
11605 li = listitem_alloc();
11606 if (li == NULL)
11607 {
11608 vim_free(s);
11609 goto errret;
11610 }
11611 li->li_tv.v_type = VAR_STRING;
11612 li->li_tv.v_lock = 0;
11613 li->li_tv.vval.v_string = s;
11614 list_append(list, li);
11615 }
11616
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011617 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 list = NULL;
11619 }
11620 else
11621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011622 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010011623#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624 /* translate <CR><NL> into <NL> */
11625 if (res != NULL)
11626 {
11627 char_u *s, *d;
11628
11629 d = res;
11630 for (s = res; *s; ++s)
11631 {
11632 if (s[0] == CAR && s[1] == NL)
11633 ++s;
11634 *d++ = *s;
11635 }
11636 *d = NUL;
11637 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011638#endif
11639 rettv->vval.v_string = res;
11640 res = NULL;
11641 }
11642
11643errret:
11644 if (infile != NULL)
11645 {
11646 mch_remove(infile);
11647 vim_free(infile);
11648 }
11649 if (res != NULL)
11650 vim_free(res);
11651 if (list != NULL)
11652 list_free(list);
11653}
11654
11655/*
11656 * "system()" function
11657 */
11658 static void
11659f_system(typval_T *argvars, typval_T *rettv)
11660{
11661 get_cmd_output_as_rettv(argvars, rettv, FALSE);
11662}
11663
11664/*
11665 * "systemlist()" function
11666 */
11667 static void
11668f_systemlist(typval_T *argvars, typval_T *rettv)
11669{
11670 get_cmd_output_as_rettv(argvars, rettv, TRUE);
11671}
11672
11673/*
11674 * "tabpagebuflist()" function
11675 */
11676 static void
11677f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11678{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 tabpage_T *tp;
11680 win_T *wp = NULL;
11681
11682 if (argvars[0].v_type == VAR_UNKNOWN)
11683 wp = firstwin;
11684 else
11685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011686 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011687 if (tp != NULL)
11688 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11689 }
11690 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
11691 {
11692 for (; wp != NULL; wp = wp->w_next)
11693 if (list_append_number(rettv->vval.v_list,
11694 wp->w_buffer->b_fnum) == FAIL)
11695 break;
11696 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011697}
11698
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011699/*
11700 * "tabpagenr()" function
11701 */
11702 static void
11703f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
11704{
11705 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011706 char_u *arg;
11707
11708 if (argvars[0].v_type != VAR_UNKNOWN)
11709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011710 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011711 nr = 0;
11712 if (arg != NULL)
11713 {
11714 if (STRCMP(arg, "$") == 0)
11715 nr = tabpage_index(NULL) - 1;
11716 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011717 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 }
11719 }
11720 else
11721 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011722 rettv->vval.v_number = nr;
11723}
11724
11725
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011726/*
11727 * Common code for tabpagewinnr() and winnr().
11728 */
11729 static int
11730get_winnr(tabpage_T *tp, typval_T *argvar)
11731{
11732 win_T *twin;
11733 int nr = 1;
11734 win_T *wp;
11735 char_u *arg;
11736
11737 twin = (tp == curtab) ? curwin : tp->tp_curwin;
11738 if (argvar->v_type != VAR_UNKNOWN)
11739 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011740 int invalid_arg = FALSE;
11741
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011742 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011743 if (arg == NULL)
11744 nr = 0; /* type error; errmsg already given */
11745 else if (STRCMP(arg, "$") == 0)
11746 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
11747 else if (STRCMP(arg, "#") == 0)
11748 {
11749 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
11750 if (twin == NULL)
11751 nr = 0;
11752 }
11753 else
11754 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020011755 long count;
11756 char_u *endp;
11757
11758 // Extract the window count (if specified). e.g. winnr('3j')
11759 count = strtol((char *)arg, (char **)&endp, 10);
11760 if (count <= 0)
11761 count = 1; // if count is not specified, default to 1
11762 if (endp != NULL && *endp != '\0')
11763 {
11764 if (STRCMP(endp, "j") == 0)
11765 twin = win_vert_neighbor(tp, twin, FALSE, count);
11766 else if (STRCMP(endp, "k") == 0)
11767 twin = win_vert_neighbor(tp, twin, TRUE, count);
11768 else if (STRCMP(endp, "h") == 0)
11769 twin = win_horz_neighbor(tp, twin, TRUE, count);
11770 else if (STRCMP(endp, "l") == 0)
11771 twin = win_horz_neighbor(tp, twin, FALSE, count);
11772 else
11773 invalid_arg = TRUE;
11774 }
11775 else
11776 invalid_arg = TRUE;
11777 }
11778
11779 if (invalid_arg)
11780 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011781 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011782 nr = 0;
11783 }
11784 }
11785
11786 if (nr > 0)
11787 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
11788 wp != twin; wp = wp->w_next)
11789 {
11790 if (wp == NULL)
11791 {
11792 /* didn't find it in this tabpage */
11793 nr = 0;
11794 break;
11795 }
11796 ++nr;
11797 }
11798 return nr;
11799}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011800
11801/*
11802 * "tabpagewinnr()" function
11803 */
11804 static void
11805f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
11806{
11807 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011808 tabpage_T *tp;
11809
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011810 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011811 if (tp == NULL)
11812 nr = 0;
11813 else
11814 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815 rettv->vval.v_number = nr;
11816}
11817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011818/*
11819 * "tagfiles()" function
11820 */
11821 static void
11822f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
11823{
11824 char_u *fname;
11825 tagname_T tn;
11826 int first;
11827
11828 if (rettv_list_alloc(rettv) == FAIL)
11829 return;
11830 fname = alloc(MAXPATHL);
11831 if (fname == NULL)
11832 return;
11833
11834 for (first = TRUE; ; first = FALSE)
11835 if (get_tagfname(&tn, first, fname) == FAIL
11836 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
11837 break;
11838 tagname_free(&tn);
11839 vim_free(fname);
11840}
11841
11842/*
11843 * "taglist()" function
11844 */
11845 static void
11846f_taglist(typval_T *argvars, typval_T *rettv)
11847{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011848 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011849 char_u *tag_pattern;
11850
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011851 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852
11853 rettv->vval.v_number = FALSE;
11854 if (*tag_pattern == NUL)
11855 return;
11856
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011857 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011858 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010011860 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861}
11862
11863/*
11864 * "tempname()" function
11865 */
11866 static void
11867f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
11868{
11869 static int x = 'A';
11870
11871 rettv->v_type = VAR_STRING;
11872 rettv->vval.v_string = vim_tempname(x, FALSE);
11873
11874 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11875 * names. Skip 'I' and 'O', they are used for shell redirection. */
11876 do
11877 {
11878 if (x == 'Z')
11879 x = '0';
11880 else if (x == '9')
11881 x = 'A';
11882 else
11883 {
11884#ifdef EBCDIC
11885 if (x == 'I')
11886 x = 'J';
11887 else if (x == 'R')
11888 x = 'S';
11889 else
11890#endif
11891 ++x;
11892 }
11893 } while (x == 'I' || x == 'O');
11894}
11895
11896#ifdef FEAT_FLOAT
11897/*
11898 * "tan()" function
11899 */
11900 static void
11901f_tan(typval_T *argvars, typval_T *rettv)
11902{
11903 float_T f = 0.0;
11904
11905 rettv->v_type = VAR_FLOAT;
11906 if (get_float_arg(argvars, &f) == OK)
11907 rettv->vval.v_float = tan(f);
11908 else
11909 rettv->vval.v_float = 0.0;
11910}
11911
11912/*
11913 * "tanh()" function
11914 */
11915 static void
11916f_tanh(typval_T *argvars, typval_T *rettv)
11917{
11918 float_T f = 0.0;
11919
11920 rettv->v_type = VAR_FLOAT;
11921 if (get_float_arg(argvars, &f) == OK)
11922 rettv->vval.v_float = tanh(f);
11923 else
11924 rettv->vval.v_float = 0.0;
11925}
11926#endif
11927
11928/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011929 * Get a callback from "arg". It can be a Funcref or a function name.
11930 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011931 * "cb_name" is not allocated.
11932 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011933 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011934 callback_T
11935get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011936{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011937 callback_T res;
11938
11939 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011940 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
11941 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011942 res.cb_partial = arg->vval.v_partial;
11943 ++res.cb_partial->pt_refcount;
11944 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011946 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011947 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011948 res.cb_partial = NULL;
11949 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
11950 {
11951 // Note that we don't make a copy of the string.
11952 res.cb_name = arg->vval.v_string;
11953 func_ref(res.cb_name);
11954 }
11955 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
11956 {
11957 res.cb_name = (char_u *)"";
11958 }
11959 else
11960 {
11961 emsg(_("E921: Invalid callback argument"));
11962 res.cb_name = NULL;
11963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011964 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011965 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011966}
11967
11968/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011969 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011970 */
11971 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011972put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011974 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011976 tv->v_type = VAR_PARTIAL;
11977 tv->vval.v_partial = cb->cb_partial;
11978 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011979 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020011980 else
11981 {
11982 tv->v_type = VAR_FUNC;
11983 tv->vval.v_string = vim_strsave(cb->cb_name);
11984 func_ref(cb->cb_name);
11985 }
11986}
11987
11988/*
11989 * Make a copy of "src" into "dest", allocating the function name if needed,
11990 * without incrementing the refcount.
11991 */
11992 void
11993set_callback(callback_T *dest, callback_T *src)
11994{
11995 if (src->cb_partial == NULL)
11996 {
11997 // just a function name, make a copy
11998 dest->cb_name = vim_strsave(src->cb_name);
11999 dest->cb_free_name = TRUE;
12000 }
12001 else
12002 {
12003 // cb_name is a pointer into cb_partial
12004 dest->cb_name = src->cb_name;
12005 dest->cb_free_name = FALSE;
12006 }
12007 dest->cb_partial = src->cb_partial;
12008}
12009
12010/*
12011 * Unref/free "callback" returned by get_callback() or set_callback().
12012 */
12013 void
12014free_callback(callback_T *callback)
12015{
12016 if (callback->cb_partial != NULL)
12017 {
12018 partial_unref(callback->cb_partial);
12019 callback->cb_partial = NULL;
12020 }
12021 else if (callback->cb_name != NULL)
12022 func_unref(callback->cb_name);
12023 if (callback->cb_free_name)
12024 {
12025 vim_free(callback->cb_name);
12026 callback->cb_free_name = FALSE;
12027 }
12028 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012029}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012030
12031#ifdef FEAT_TIMERS
12032/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012033 * "timer_info([timer])" function
12034 */
12035 static void
12036f_timer_info(typval_T *argvars, typval_T *rettv)
12037{
12038 timer_T *timer = NULL;
12039
12040 if (rettv_list_alloc(rettv) != OK)
12041 return;
12042 if (argvars[0].v_type != VAR_UNKNOWN)
12043 {
12044 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012045 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012046 else
12047 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012048 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012049 if (timer != NULL)
12050 add_timer_info(rettv, timer);
12051 }
12052 }
12053 else
12054 add_timer_info_all(rettv);
12055}
12056
12057/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012058 * "timer_pause(timer, paused)" function
12059 */
12060 static void
12061f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12062{
12063 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012064 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012065
12066 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012067 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012068 else
12069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012070 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012071 if (timer != NULL)
12072 timer->tr_paused = paused;
12073 }
12074}
12075
12076/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012077 * "timer_start(time, callback [, options])" function
12078 */
12079 static void
12080f_timer_start(typval_T *argvars, typval_T *rettv)
12081{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012082 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012083 timer_T *timer;
12084 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012085 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012086 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012087
Bram Moolenaar75537a92016-09-05 22:45:28 +020012088 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012089 if (check_secure())
12090 return;
12091 if (argvars[2].v_type != VAR_UNKNOWN)
12092 {
12093 if (argvars[2].v_type != VAR_DICT
12094 || (dict = argvars[2].vval.v_dict) == NULL)
12095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012096 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097 return;
12098 }
12099 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012100 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012101 }
12102
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012103 callback = get_callback(&argvars[1]);
12104 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012105 return;
12106
12107 timer = create_timer(msec, repeat);
12108 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012109 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012110 else
12111 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020012112 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020012113 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012114 }
12115}
12116
12117/*
12118 * "timer_stop(timer)" function
12119 */
12120 static void
12121f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12122{
12123 timer_T *timer;
12124
12125 if (argvars[0].v_type != VAR_NUMBER)
12126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012127 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012128 return;
12129 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012130 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012131 if (timer != NULL)
12132 stop_timer(timer);
12133}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012134
12135/*
12136 * "timer_stopall()" function
12137 */
12138 static void
12139f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12140{
12141 stop_all_timers();
12142}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143#endif
12144
12145/*
12146 * "tolower(string)" function
12147 */
12148 static void
12149f_tolower(typval_T *argvars, typval_T *rettv)
12150{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012151 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012152 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153}
12154
12155/*
12156 * "toupper(string)" function
12157 */
12158 static void
12159f_toupper(typval_T *argvars, typval_T *rettv)
12160{
12161 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012162 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012163}
12164
12165/*
12166 * "tr(string, fromstr, tostr)" function
12167 */
12168 static void
12169f_tr(typval_T *argvars, typval_T *rettv)
12170{
12171 char_u *in_str;
12172 char_u *fromstr;
12173 char_u *tostr;
12174 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012175 int inlen;
12176 int fromlen;
12177 int tolen;
12178 int idx;
12179 char_u *cpstr;
12180 int cplen;
12181 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012182 char_u buf[NUMBUFLEN];
12183 char_u buf2[NUMBUFLEN];
12184 garray_T ga;
12185
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012186 in_str = tv_get_string(&argvars[0]);
12187 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
12188 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012189
12190 /* Default return value: empty string. */
12191 rettv->v_type = VAR_STRING;
12192 rettv->vval.v_string = NULL;
12193 if (fromstr == NULL || tostr == NULL)
12194 return; /* type error; errmsg already given */
12195 ga_init2(&ga, (int)sizeof(char), 80);
12196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012197 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012198 /* not multi-byte: fromstr and tostr must be the same length */
12199 if (STRLEN(fromstr) != STRLEN(tostr))
12200 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012201error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012202 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012203 ga_clear(&ga);
12204 return;
12205 }
12206
12207 /* fromstr and tostr have to contain the same number of chars */
12208 while (*in_str != NUL)
12209 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 if (has_mbyte)
12211 {
12212 inlen = (*mb_ptr2len)(in_str);
12213 cpstr = in_str;
12214 cplen = inlen;
12215 idx = 0;
12216 for (p = fromstr; *p != NUL; p += fromlen)
12217 {
12218 fromlen = (*mb_ptr2len)(p);
12219 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12220 {
12221 for (p = tostr; *p != NUL; p += tolen)
12222 {
12223 tolen = (*mb_ptr2len)(p);
12224 if (idx-- == 0)
12225 {
12226 cplen = tolen;
12227 cpstr = p;
12228 break;
12229 }
12230 }
12231 if (*p == NUL) /* tostr is shorter than fromstr */
12232 goto error;
12233 break;
12234 }
12235 ++idx;
12236 }
12237
12238 if (first && cpstr == in_str)
12239 {
12240 /* Check that fromstr and tostr have the same number of
12241 * (multi-byte) characters. Done only once when a character
12242 * of in_str doesn't appear in fromstr. */
12243 first = FALSE;
12244 for (p = tostr; *p != NUL; p += tolen)
12245 {
12246 tolen = (*mb_ptr2len)(p);
12247 --idx;
12248 }
12249 if (idx != 0)
12250 goto error;
12251 }
12252
12253 (void)ga_grow(&ga, cplen);
12254 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12255 ga.ga_len += cplen;
12256
12257 in_str += inlen;
12258 }
12259 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260 {
12261 /* When not using multi-byte chars we can do it faster. */
12262 p = vim_strchr(fromstr, *in_str);
12263 if (p != NULL)
12264 ga_append(&ga, tostr[p - fromstr]);
12265 else
12266 ga_append(&ga, *in_str);
12267 ++in_str;
12268 }
12269 }
12270
12271 /* add a terminating NUL */
12272 (void)ga_grow(&ga, 1);
12273 ga_append(&ga, NUL);
12274
12275 rettv->vval.v_string = ga.ga_data;
12276}
12277
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012278/*
12279 * "trim({expr})" function
12280 */
12281 static void
12282f_trim(typval_T *argvars, typval_T *rettv)
12283{
12284 char_u buf1[NUMBUFLEN];
12285 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012286 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012287 char_u *mask = NULL;
12288 char_u *tail;
12289 char_u *prev;
12290 char_u *p;
12291 int c1;
12292
12293 rettv->v_type = VAR_STRING;
12294 if (head == NULL)
12295 {
12296 rettv->vval.v_string = NULL;
12297 return;
12298 }
12299
12300 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012301 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010012302
12303 while (*head != NUL)
12304 {
12305 c1 = PTR2CHAR(head);
12306 if (mask == NULL)
12307 {
12308 if (c1 > ' ' && c1 != 0xa0)
12309 break;
12310 }
12311 else
12312 {
12313 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12314 if (c1 == PTR2CHAR(p))
12315 break;
12316 if (*p == NUL)
12317 break;
12318 }
12319 MB_PTR_ADV(head);
12320 }
12321
12322 for (tail = head + STRLEN(head); tail > head; tail = prev)
12323 {
12324 prev = tail;
12325 MB_PTR_BACK(head, prev);
12326 c1 = PTR2CHAR(prev);
12327 if (mask == NULL)
12328 {
12329 if (c1 > ' ' && c1 != 0xa0)
12330 break;
12331 }
12332 else
12333 {
12334 for (p = mask; *p != NUL; MB_PTR_ADV(p))
12335 if (c1 == PTR2CHAR(p))
12336 break;
12337 if (*p == NUL)
12338 break;
12339 }
12340 }
12341 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
12342}
12343
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012344#ifdef FEAT_FLOAT
12345/*
12346 * "trunc({float})" function
12347 */
12348 static void
12349f_trunc(typval_T *argvars, typval_T *rettv)
12350{
12351 float_T f = 0.0;
12352
12353 rettv->v_type = VAR_FLOAT;
12354 if (get_float_arg(argvars, &f) == OK)
12355 /* trunc() is not in C90, use floor() or ceil() instead. */
12356 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12357 else
12358 rettv->vval.v_float = 0.0;
12359}
12360#endif
12361
12362/*
12363 * "type(expr)" function
12364 */
12365 static void
12366f_type(typval_T *argvars, typval_T *rettv)
12367{
12368 int n = -1;
12369
12370 switch (argvars[0].v_type)
12371 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012372 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12373 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012374 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012375 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12376 case VAR_LIST: n = VAR_TYPE_LIST; break;
12377 case VAR_DICT: n = VAR_TYPE_DICT; break;
12378 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012379 case VAR_SPECIAL:
12380 if (argvars[0].vval.v_number == VVAL_FALSE
12381 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012382 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012383 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012384 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012385 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012386 case VAR_JOB: n = VAR_TYPE_JOB; break;
12387 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012388 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012389 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012390 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012391 n = -1;
12392 break;
12393 }
12394 rettv->vval.v_number = n;
12395}
12396
12397/*
12398 * "undofile(name)" function
12399 */
12400 static void
12401f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12402{
12403 rettv->v_type = VAR_STRING;
12404#ifdef FEAT_PERSISTENT_UNDO
12405 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012406 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407
12408 if (*fname == NUL)
12409 {
12410 /* If there is no file name there will be no undo file. */
12411 rettv->vval.v_string = NULL;
12412 }
12413 else
12414 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020012415 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012416
12417 if (ffname != NULL)
12418 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12419 vim_free(ffname);
12420 }
12421 }
12422#else
12423 rettv->vval.v_string = NULL;
12424#endif
12425}
12426
12427/*
12428 * "undotree()" function
12429 */
12430 static void
12431f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12432{
12433 if (rettv_dict_alloc(rettv) == OK)
12434 {
12435 dict_T *dict = rettv->vval.v_dict;
12436 list_T *list;
12437
Bram Moolenaare0be1672018-07-08 16:50:37 +020012438 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
12439 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
12440 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
12441 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
12442 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
12443 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444
12445 list = list_alloc();
12446 if (list != NULL)
12447 {
12448 u_eval_tree(curbuf->b_u_oldhead, list);
12449 dict_add_list(dict, "entries", list);
12450 }
12451 }
12452}
12453
12454/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012455 * "virtcol(string)" function
12456 */
12457 static void
12458f_virtcol(typval_T *argvars, typval_T *rettv)
12459{
12460 colnr_T vcol = 0;
12461 pos_T *fp;
12462 int fnum = curbuf->b_fnum;
12463
12464 fp = var2fpos(&argvars[0], FALSE, &fnum);
12465 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12466 && fnum == curbuf->b_fnum)
12467 {
12468 getvvcol(curwin, fp, NULL, NULL, &vcol);
12469 ++vcol;
12470 }
12471
12472 rettv->vval.v_number = vcol;
12473}
12474
12475/*
12476 * "visualmode()" function
12477 */
12478 static void
12479f_visualmode(typval_T *argvars, typval_T *rettv)
12480{
12481 char_u str[2];
12482
12483 rettv->v_type = VAR_STRING;
12484 str[0] = curbuf->b_visual_mode_eval;
12485 str[1] = NUL;
12486 rettv->vval.v_string = vim_strsave(str);
12487
12488 /* A non-zero number or non-empty string argument: reset mode. */
12489 if (non_zero_arg(&argvars[0]))
12490 curbuf->b_visual_mode_eval = NUL;
12491}
12492
12493/*
12494 * "wildmenumode()" function
12495 */
12496 static void
12497f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12498{
12499#ifdef FEAT_WILDMENU
12500 if (wild_menu_showing)
12501 rettv->vval.v_number = 1;
12502#endif
12503}
12504
12505/*
12506 * "winbufnr(nr)" function
12507 */
12508 static void
12509f_winbufnr(typval_T *argvars, typval_T *rettv)
12510{
12511 win_T *wp;
12512
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012513 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012514 if (wp == NULL)
12515 rettv->vval.v_number = -1;
12516 else
12517 rettv->vval.v_number = wp->w_buffer->b_fnum;
12518}
12519
12520/*
12521 * "wincol()" function
12522 */
12523 static void
12524f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12525{
12526 validate_cursor();
12527 rettv->vval.v_number = curwin->w_wcol + 1;
12528}
12529
12530/*
12531 * "winheight(nr)" function
12532 */
12533 static void
12534f_winheight(typval_T *argvars, typval_T *rettv)
12535{
12536 win_T *wp;
12537
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012538 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 if (wp == NULL)
12540 rettv->vval.v_number = -1;
12541 else
12542 rettv->vval.v_number = wp->w_height;
12543}
12544
12545/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012546 * "winlayout()" function
12547 */
12548 static void
12549f_winlayout(typval_T *argvars, typval_T *rettv)
12550{
12551 tabpage_T *tp;
12552
12553 if (rettv_list_alloc(rettv) != OK)
12554 return;
12555
12556 if (argvars[0].v_type == VAR_UNKNOWN)
12557 tp = curtab;
12558 else
12559 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012560 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020012561 if (tp == NULL)
12562 return;
12563 }
12564
12565 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
12566}
12567
12568/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 * "winline()" function
12570 */
12571 static void
12572f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12573{
12574 validate_cursor();
12575 rettv->vval.v_number = curwin->w_wrow + 1;
12576}
12577
12578/*
12579 * "winnr()" function
12580 */
12581 static void
12582f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12583{
12584 int nr = 1;
12585
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 rettv->vval.v_number = nr;
12588}
12589
12590/*
12591 * "winrestcmd()" function
12592 */
12593 static void
12594f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12595{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 win_T *wp;
12597 int winnr = 1;
12598 garray_T ga;
12599 char_u buf[50];
12600
12601 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012602 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012603 {
12604 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12605 ga_concat(&ga, buf);
12606 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12607 ga_concat(&ga, buf);
12608 ++winnr;
12609 }
12610 ga_append(&ga, NUL);
12611
12612 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 rettv->v_type = VAR_STRING;
12614}
12615
12616/*
12617 * "winrestview()" function
12618 */
12619 static void
12620f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12621{
12622 dict_T *dict;
12623
12624 if (argvars[0].v_type != VAR_DICT
12625 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012626 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012627 else
12628 {
12629 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012630 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012631 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012632 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012633 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012634 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012635 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12636 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010012637 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 curwin->w_set_curswant = FALSE;
12639 }
12640
12641 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012642 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012643#ifdef FEAT_DIFF
12644 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012645 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012646#endif
12647 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012648 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010012650 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651
12652 check_cursor();
12653 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020012654 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655 changed_window_setting();
12656
12657 if (curwin->w_topline <= 0)
12658 curwin->w_topline = 1;
12659 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
12660 curwin->w_topline = curbuf->b_ml.ml_line_count;
12661#ifdef FEAT_DIFF
12662 check_topfill(curwin, TRUE);
12663#endif
12664 }
12665}
12666
12667/*
12668 * "winsaveview()" function
12669 */
12670 static void
12671f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
12672{
12673 dict_T *dict;
12674
12675 if (rettv_dict_alloc(rettv) == FAIL)
12676 return;
12677 dict = rettv->vval.v_dict;
12678
Bram Moolenaare0be1672018-07-08 16:50:37 +020012679 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
12680 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020012681 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012682 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020012683 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684
Bram Moolenaare0be1672018-07-08 16:50:37 +020012685 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012686#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020012687 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020012689 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
12690 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691}
12692
12693/*
12694 * "winwidth(nr)" function
12695 */
12696 static void
12697f_winwidth(typval_T *argvars, typval_T *rettv)
12698{
12699 win_T *wp;
12700
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020012701 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012702 if (wp == NULL)
12703 rettv->vval.v_number = -1;
12704 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012705 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706}
12707
12708/*
12709 * "wordcount()" function
12710 */
12711 static void
12712f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
12713{
12714 if (rettv_dict_alloc(rettv) == FAIL)
12715 return;
12716 cursor_pos_info(rettv->vval.v_dict);
12717}
12718
12719/*
12720 * "writefile()" function
12721 */
12722 static void
12723f_writefile(typval_T *argvars, typval_T *rettv)
12724{
12725 int binary = FALSE;
12726 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012727#ifdef HAVE_FSYNC
12728 int do_fsync = p_fs;
12729#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730 char_u *fname;
12731 FILE *fd;
12732 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012733 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012734 list_T *list = NULL;
12735 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012737 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010012738 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012739 return;
12740
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012741 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012743 list = argvars[0].vval.v_list;
12744 if (list == NULL)
12745 return;
12746 for (li = list->lv_first; li != NULL; li = li->li_next)
12747 if (tv_get_string_chk(&li->li_tv) == NULL)
12748 return;
12749 }
12750 else if (argvars[0].v_type == VAR_BLOB)
12751 {
12752 blob = argvars[0].vval.v_blob;
12753 if (blob == NULL)
12754 return;
12755 }
12756 else
12757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012758 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012759 return;
12760 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012761
12762 if (argvars[2].v_type != VAR_UNKNOWN)
12763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012764 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012765
12766 if (arg2 == NULL)
12767 return;
12768 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012769 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012770 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012771 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012772#ifdef HAVE_FSYNC
12773 if (vim_strchr(arg2, 's') != NULL)
12774 do_fsync = TRUE;
12775 else if (vim_strchr(arg2, 'S') != NULL)
12776 do_fsync = FALSE;
12777#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012778 }
12779
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012780 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012781 if (fname == NULL)
12782 return;
12783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012784 /* Always open the file in binary mode, library functions have a mind of
12785 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786 if (*fname == NUL || (fd = mch_fopen((char *)fname,
12787 append ? APPENDBIN : WRITEBIN)) == NULL)
12788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012789 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012790 ret = -1;
12791 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012792 else if (blob)
12793 {
12794 if (write_blob(fd, blob) == FAIL)
12795 ret = -1;
12796#ifdef HAVE_FSYNC
12797 else if (do_fsync)
12798 // Ignore the error, the user wouldn't know what to do about it.
12799 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010012800 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010012801#endif
12802 fclose(fd);
12803 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012804 else
12805 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020012806 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012808#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010012809 else if (do_fsync)
12810 /* Ignore the error, the user wouldn't know what to do about it.
12811 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010012812 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010012813#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814 fclose(fd);
12815 }
12816
12817 rettv->vval.v_number = ret;
12818}
12819
12820/*
12821 * "xor(expr, expr)" function
12822 */
12823 static void
12824f_xor(typval_T *argvars, typval_T *rettv)
12825{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012826 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
12827 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828}
12829
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830#endif /* FEAT_EVAL */